Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P110
Display notification for e-mail
Archived
Public
Actions
Authored by
pmoreau
on Sep 8 2017, 7:05 PM.
Edit Paste
Activate Paste
View Raw File
Subscribe
Mute Notifications
Flag For Later
Award Token
Tags
None
Subscribers
None
#!/bin/python
import
gi
import
email
import
re
import
sys
gi
.
require_version
(
'Notify'
,
'0.7'
)
from
gi.repository
import
Notify
message_path
=
sys
.
argv
[
1
]
with
open
(
message_path
,
'r'
)
as
message_file
:
message_content
=
message_file
.
read
()
# Assuming that the mail path has the format
# `$some_random_path/mail/$accountname/$foldername/new/$other_stuff`, extract
# the `$accountname/$foldername` for display.
maildir
=
re
.
sub
(
r"^.*mail\/"
,
""
,
re
.
sub
(
r"\/new.*$"
,
""
,
message_path
))
message
=
email
.
message_from_string
(
message_content
)
sender
=
str
(
email
.
header
.
make_header
(
email
.
header
.
decode_header
(
message
[
'From'
])))
# If the name is surrounded by double quotes, remove them.
sender
=
re
.
sub
(
r"\"
([
^
\
"]*)
\"
"
,
"\g<1>"
,
sender
)
# If the name is in format “$last_name, $first_name”, reorder to have it in
# format “$first_name $last_name”
sender
=
re
.
sub
(
r"([^,]+), ?([^<]*)"
,
"\g<2>\g<1> "
,
sender
)
# The address will fail to display if surrounded by angles. Replace those by
# square brackets.
sender
=
re
.
sub
(
r"<([^>]*)>"
,
"[\g<1>]"
,
sender
)
subject
=
str
(
email
.
header
.
make_header
(
email
.
header
.
decode_header
(
message
[
'Subject'
])))
# Remove any newline from the subject, as the notification displayer will
# already take care of the wrapping
subject
=
re
.
sub
(
r"\n"
,
""
,
re
.
sub
(
r" \n"
,
" "
,
subject
))
Notify
.
init
(
"offlineimap"
)
notif
=
Notify
.
Notification
.
new
(
"New e-mail in
\"
{0}
\"
"
.
format
(
maildir
),
"From: {0}
\n
Subject: {1}"
.
format
(
sender
,
subject
),
"mail-message-new"
)
notif
.
set_timeout
(
5000
)
notif
.
set_urgency
(
Notify
.
Urgency
.
NORMAL
)
notif
.
show
()
Event Timeline
pmoreau
created this paste.
Sep 8 2017, 7:05 PM
2017-09-08 19:05:42 (UTC+2)
pmoreau
created this object with visibility "Public (No Login Required)".
pmoreau
created this object with edit policy "
pmoreau (Pierre Moreau)
".
pmoreau
archived this paste.
Aug 23 2020, 10:23 AM
2020-08-23 10:23:33 (UTC+2)
Log In to Comment