e6ffacb29e28d077012563a6b8fe94cfa9ad0936
[grml-etc.git] / etc / skel / .wmi / szs2.1 / biff.py
1 #!/usr/bin/python
2 """
3 szs/biff - an imap/pop biff module for SZS a statusbar script for WMI
4
5
6 CHANGELOG:
7
8    v0.4    2004-12-14
9                 * changed config format
10                 * added multiple server support
11                 * added pop3 support
12                 * added seperate multipliers for pop and imap
13                 * added support for POP3 SSL (python 2.4 only)
14                 * added support for IMAP4 SSL 
15
16    v0.3    2004-11-29  
17                 * show <mailbox>(?) on startup and if serer is not reachable
18
19    v0.2    2004-11-26  
20                 * implemented error handling
21                 * some cleanups
22
23    v0.1    2004-11-25  
24         * initial Release
25
26
27 TODO:
28
29
30
31 COPYRIGHT:
32
33 Copyright 2004  Christoph Wegscheider <cw@wegi.net>
34
35
36 LICENSE:
37
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2 of the License, or
41 (at your option) any later version.
42
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46 GNU General Public License for more details.
47
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
51 """
52
53
54 import imaplib
55 import poplib
56 import socket
57 import types
58 import sys
59 import szstoolbox
60
61
62
63 def error(mailboxes):
64         text = ''
65         if type(mailboxes) is not types.ListType:
66                 mailboxes = [['', mailboxes]]
67         for it in mailboxes:
68                 text += it[1] + '(?) '
69         return text
70
71
72
73 def main():
74         global count
75         global imaptext
76         global poptext
77
78         # check imap server     
79         if count % multiplier['imapmultiplier'] == 1:
80                 imaptext = ''
81                 for acc in accounts['imapaccount']:
82                         try:
83                                 if acc[3] == 'yes':
84                                         server = imaplib.IMAP4_SSL(acc[0])
85                                 else:
86                                         server = imaplib.IMAP4(acc[0])
87                                 server.login(acc[1], acc[2])
88                                 for it in acc[4:]:      
89                                         if server.select(it[0], True)[0] == 'OK':
90                                                 num = len(server.search(None, 'UNSEEN')[1][0].split())
91                                                 if num > 0:
92                                                         imaptext += it[1] + '(' + str(num) + ') '
93                                 server.logout()
94                         except (imaplib.IMAP4.error, socket.error), errormsg:
95                                 msg.debug(acc[0] + ": " + str(errormsg), 2)
96                                 imaptext += error(acc[4:])
97
98         # check pop3 server
99         if count % multiplier['popmultiplier'] == 1:
100                 poptext = ''
101                 for acc in accounts['popaccount']:
102                         try:
103                                 if acc[3] == 'yes':
104                                         if sys.version_info >= (2,4):
105                                                 server = poplib.POP3_SSL(acc[0])
106                                         else:
107                                                 raise szstoolbox.Error('POP3 with SSL support is only availabe in python 2.4 or greater')
108                                 else:
109                                         server = poplib.POP3(acc[0])
110                                 server.user(acc[1])
111                                 server.pass_(acc[2])
112                                 num = server.stat()[0]
113                                 server.quit()
114                                 if num > 0:
115                                         poptext += acc[4] + '(' + str(num) + ') '
116                         except (poplib.error_proto, socket.error, szstoolbox.Error), e:
117                                 msg.debug(acc[0] + ': ' + str(e), 2)
118                                 poptext += error(acc[4])
119                         
120         count += 1
121         return [imaptext + poptext]
122
123
124
125 # init
126 count = 0
127 imaptext = poptext = ''
128 accounts = multiplier = {}
129 msg = szstoolbox.MSG()
130 cfg = szstoolbox.CFG('biff')
131
132 # load configuration  
133 for it in 'imapmultiplier','popmultiplier':
134         multiplier[it] = int(cfg.read(it))
135 for acctype in 'imapaccount','popaccount':
136         accounts[acctype] = []
137         config = cfg.read(acctype)
138         if type(config) is types.StringType:
139                 config = [config]
140         elif type(config) is types.NoneType:
141                 config = []
142         for acc in config:
143                 acc = acc.split(',')
144                 accounts[acctype].append(acc[0].split(':'))
145                 if acctype == 'imapaccount':
146                         accounts[acctype][-1].extend([it.split(':') for it in acc[1:]])
147
148 # set nice start text
149 for acc in accounts['imapaccount']:
150         imaptext += error(acc[3:])
151 for acc in accounts['popaccount']:
152         poptext += error(acc[4])
153
154
155
156 # for testing purpose
157 if __name__ == '__main__':
158         import time
159
160         while True:
161                 print main()
162                 time.sleep(szstoolbox.interval)