forked from tcheneau/awesome-checkmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkmail.py
executable file
·193 lines (155 loc) · 6.63 KB
/
checkmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/env python3
# -*- coding : utf8 -*-
import socket, ssl, getpass, imaplib, time, os, argparse, sys
from subprocess import *
from functools import partial
import configparser
# delays between 2 checks
checkdelay = 60
debug = False
DEFAULT_CFG = "~/.config/awesome/mail.cfg"
def print_debug(string):
if debug:
print(string)
class awesome:
p=None
_widget = ""
def __init__(self, widget_name):
self._widget = widget_name
def write_message(self,message):
# may need exception handling
self.p = Popen("awesome-client", stdin=PIPE)
text = self._widget + "=\"" + message + "\"\n"
self.p.stdin.write(text.encode("utf8"))
self.p.communicate()
# awesome-client call should have terminated by now
class MailServer:
_server_name=""
_port=993
_name=""
_login=""
_password=None
_color="blue"
_directories=[]
# connexion to the IMAP server
_connexion=None
# indicates the status: "logued" or "not logued"
_status="not logued"
def __init__(self,
server_name,
port,
login,
password,
directories,
color,
printed_name):
self._server_name=server_name
self._login= login
self._port = port
self._password=password
self._directories=directories
self._name=printed_name
self._color=color
self.connect()
self.login()
def connect(self):
"""function to connecting to the mail server"""
try:
print("login on: ", self._name)
self._connexion = imaplib.IMAP4_SSL(self._server_name, self._port)
except (imaplib.IMAP4.abort, socket.gaierror, socket.error) as err:
print("unable to connect to ", self._name, "(", self._port, "):", err)
self._connexion=None
def login(self):
"""function to log in the mail server"""
try:
if self._password:
self._connexion.login(bytes(self._login, encoding='utf8').decode("utf8"), bytes(self._password, encoding='utf8').decode("utf8"))
# or prompt the password to the user
else:
print("Please enter password for user ", self._login, " on ", self._name)
self._connexion.login(self._login, getpass.getpass())
self._status="logued"
except (imaplib.IMAP4.error, AttributeError) as err:
print("unable to login on: ", self._name, ":", err)
else:
print("Successfully logued in ", self._name, " as " , self._login)
def list_mailbox(self):
if self._connexion==None or self._status=="not logued":
self.connect()
self.login()
# might be modified in the future
return ""
else:
localstring = ""
try:
for mailbox in self._directories:
(status,message) = self._connexion.select(mailbox,readonly=1)
if status=="NO":
print("the mailbox ", mailbox, " doesn't exist on", self._server_name)
continue
(status,message)= self._connexion.search(None,'UNSEEN')
if message!=[b'']:
localstring += " " + mailbox + '(' + str(len(message[0].decode("utf8").split(" "))) + ')'
except (imaplib.IMAP4.error,socket.gaierror,ssl.SSLError,socket.error) as err:
print("unable to fetch data from ", self._server_name, ":", err)
# resetting connexion
self._connexion=None
return localstring
def __str__(self):
local_string=self.list_mailbox()
if self._connexion==None or self._status=="not logued":
return "<span color=\'%(_color)s\'>Not connect/logued on %(_server_name)s</span>" % self.__dict__
else:
#we have no mail on this server
if local_string=="":
return ""
else:
return '<span color=\''+ self._color + '\'> [' \
+ self._name \
+ local_string \
+ ']</span>'
def parser_server_config(cfg_obj):
servers = []
for server in cfg_obj.sections():
getelm = partial(cfg_obj.get, server)
servers.append(MailServer(server_name = server,
port = getelm("Port"),
login = getelm("Username"),
password = getelm("Password"),
directories = eval(getelm("Folders")),
color = getelm("Color"),
printed_name= getelm("Name")))
return servers
if __name__ == "__main__":
# read the configuration file
config = configparser.ConfigParser()
parser = argparse.ArgumentParser(description="Print unread mail nicely in Awesome")
parser.add_argument("--path", default="~/.config/awesome/mail.cfg")
args = parser.parse_args()
# try to load the default path
configfile = os.path.expanduser(args.path)
if os.path.isfile(configfile):
print("reading configuration file {}".format(configfile))
config.read(configfile)
else:
# no default configuration file is present, user needs to create a new one
print("no configure file is present, please copy a sample configuration from \
/usr/share/doc/awesome-checkmail/example/ to {}".format(DEFAULT_CFG))
# TODO:
# create the directory if needed (os.mkdir)
# copy the file
sys.exit(-1)
debug = config.get("global","debug")
widget = config.get("global","widget_name")
checkdelay = config.getint("global", "check_delay")
config.remove_section("global")
server_list= parser_server_config(config)
client = awesome(widget)
while True:
new_mail=[]
for server in server_list:
new_mail.append(str(server))
client.write_message("".join(new_mail))
print_debug(client._widget + "=\"" +"".join(new_mail) + "\"\n")
time.sleep(checkdelay)