forked from dilawar/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_multiple_imap.py
executable file
·155 lines (120 loc) · 4.64 KB
/
check_multiple_imap.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
#!/usr/bin/env python
#
# Copyright 2013 by Martin Bley <martin@mb-oss.de>
#
# maic.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# maic.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with maic.py. If not, see <http://www.gnu.org/licenses/>.
#
import time, sys, os, stat, ConfigParser, imaplib, re
class ImapChecker(object):
Error = imaplib.IMAP4.error
AbortError = imaplib.IMAP4.abort
ReadOnlyError = imaplib.IMAP4.readonly
version = "0.2"
def __init__(self, host, ssl=False, port=None):
self.host = host
self.ssl = ssl
self.port = port
if self.port is None:
if self.ssl is True:
self.port = 993
else:
self.port = 143
def login(self, user, passwd):
if self.ssl is True:
self.conn = imaplib.IMAP4_SSL(self.host, self.port)
else:
self.conn = imaplib.IMAP4(self.host, self.port)
m = self.conn
user = bytes(user)
passwd = bytes(passwd)
m.login( user, passwd )
def logout(self):
self.conn.logout()
def check(self, folder="INBOX"):
(rtn, msg) = self.conn.status(folder, "(MESSAGES RECENT UNSEEN)")
re_status = re.compile(r'.*\((?P<status_items>.*)\).*$')
if rtn == 'OK':
match = re_status.search(str(msg[0]))
if not match:
raise self.Error('Could not get the folder status')
status_items = match.group('status_items').strip().split()
out = {}
while status_items:
key = status_items.pop(0)
value = status_items.pop(0)
out[key] = value
return out
raise self.Error('Status command failed')
def shutdown(self):
self.conn.shutdown()
def main():
version = "0.4"
options_required = ['servername', 'username', 'password']
outstring = ""
# config file
configfile = os.path.expanduser('~/.config/awesome/ImapWidget.cfg')
config = ConfigParser.ConfigParser()
# read config
try:
config.read(configfile)
mode = os.stat(configfile).st_mode
uid = os.stat(configfile).st_uid
except (IOError, OSError):
sys.stderr.write("Error opening config file " + str(configfile))
sys.exit(1)
else:
# check and fix mode of the config file
if os.getuid() != uid:
sys.stderr.write("You are not the owner of the config file.")
if oct(stat.S_IMODE(mode)) != '0600':
sys.stderr.write("Mode is not OK. Fixing permissions.")
os.chmod(configfile, 0600)
# iterate through the sections of the config
configSections = config.sections()
# check, if all needed parameters are given in configfile
for sec in configSections:
options = config.options(sec)
for req in options_required:
if not(req in options):
sys.stderr.write("Section " + sec + ": Missing option " + req + ". Removing it from Accountlist")
configSections.remove(sec)
# create IMAP connection for each account
for sec in configSections:
try:
ssl = config.get(sec, 'ssl')
if ssl in [ "True", "1", "Yes"]:
imap = ImapChecker(config.get(sec, 'servername'), True)
else:
imap = ImapChecker(config.get(sec, 'servername'))
except ConfigParser.NoOptionError:
imap = ImapChecker(config.get(sec, 'servername'))
if outstring is not "":
outstring += " | "
try:
imap.login(config.get(sec, 'username'), config.get(sec, 'password'))
ret = imap.check()
new = ret['UNSEEN']
recent = ret['RECENT']
if int(new) > 0:
new = "<span color=\"red\">" + new + "</span>"
if int(recent) > 0:
recent = "<span color=\"red\">" + recent + "</span>"
imap.logout()
outstring += sec + ": " + new + "/" + recent
except:
outstring += sec + ": ~"
print(outstring)
sys.exit(0)
if __name__ == '__main__':
main()