Mercurial > p > roundup > code
annotate scripts/imapServer.py @ 5378:35ea9b1efc14
Python 3 preparation: "raise" syntax.
Changing "raise Exception, value" to "raise Exception(value)".
Tool-assisted patch. Particular cases to check carefully are the one
place in frontends/ZRoundup/ZRoundup.py where a string exception
needed to be fixed, and the one in roundup/cgi/client.py involving
raising an exception with a traceback (requires three-argument form of
raise in Python 2, which as I understand it requires exec() to avoid a
Python 3 syntax error).
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Tue, 24 Jul 2018 21:39:58 +0000 |
| parents | 64b05e24dbd8 |
| children | 0942fe89e82e |
| rev | line source |
|---|---|
| 3203 | 1 #!/usr/bin/env python |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 """\ |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
3 This script is a wrapper around the mailgw.py script that exists in roundup. |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 It runs as service instead of running as a one-time shot. |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
5 It also connects to a secure IMAP server. The main reasons for this script are: |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 |
| 3203 | 7 1) The roundup-mailgw script isn't designed to run as a server. It |
| 8 expects that you either run it by hand, and enter the password each | |
| 9 time, or you supply the password on the command line. I prefer to | |
| 10 run a server that I initialize with the password, and then it just | |
| 11 runs. I don't want to have to pass it on the command line, so | |
| 12 running through crontab isn't a possibility. (This wouldn't be a | |
| 13 problem on a local machine running through a mailspool.) | |
| 14 2) mailgw.py somehow screws up SSL support so IMAP4_SSL doesn't work. So | |
| 15 hopefully running that work outside of the mailgw will allow it to work. | |
| 16 3) I wanted to be able to check multiple projects at the same time. | |
| 17 roundup-mailgw is only for 1 mailbox and 1 project. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 *TODO*: |
| 3203 | 21 For the first round, the program spawns a new roundup-mailgw for |
| 22 each imap message that it finds and pipes the result in. In the | |
| 23 future it might be more practical to actually include the roundup | |
| 24 files and run the appropriate commands using python. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 *TODO*: |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 Look into supporting a logfile instead of using 2>/logfile |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
29 *TODO*: |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 Add an option for changing the uid/gid of the running process. |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 """ |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5141
diff
changeset
|
33 from __future__ import print_function |
| 3203 | 34 import getpass |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 import logging |
| 3203 | 36 import imaplib |
| 37 import optparse | |
| 38 import os | |
| 39 import re | |
| 40 import time | |
| 41 | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 logging.basicConfig() |
|
4421
67bef70ab9b9
- more logger fixes, sorry for the noise.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3609
diff
changeset
|
43 log = logging.getLogger('roundup.IMAPServer') |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 version = '0.1.2' |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 class RoundupMailbox: |
| 3203 | 48 """This contains all the info about each mailbox. |
| 49 Username, Password, server, security, roundup database | |
| 50 """ | |
| 51 def __init__(self, dbhome='', username=None, password=None, mailbox=None | |
| 52 , server=None, protocol='imaps'): | |
| 53 self.username = username | |
| 54 self.password = password | |
| 55 self.mailbox = mailbox | |
| 56 self.server = server | |
| 57 self.protocol = protocol | |
| 58 self.dbhome = dbhome | |
| 59 | |
| 60 try: | |
| 61 if not self.dbhome: | |
| 62 self.dbhome = raw_input('Tracker home: ') | |
| 63 if not os.path.exists(self.dbhome): | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
64 raise ValueError('Invalid home address: ' \ |
|
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
65 'directory "%s" does not exist.' % self.dbhome) |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
66 |
| 3203 | 67 if not self.server: |
| 68 self.server = raw_input('Server: ') | |
| 69 if not self.server: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
70 raise ValueError('No Servername supplied') |
| 3203 | 71 protocol = raw_input('protocol [imaps]? ') |
| 72 self.protocol = protocol | |
| 73 | |
| 74 if not self.username: | |
| 75 self.username = raw_input('Username: ') | |
| 76 if not self.username: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
77 raise ValueError('Invalid Username') |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
78 |
| 3203 | 79 if not self.password: |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5141
diff
changeset
|
80 print('For server %s, user %s' % (self.server, self.username)) |
| 3203 | 81 self.password = getpass.getpass() |
| 82 # password can be empty because it could be superceeded | |
| 83 # by a later entry | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
84 |
| 3203 | 85 #if self.mailbox is None: |
| 86 # self.mailbox = raw_input('Mailbox [INBOX]: ') | |
| 87 # # We allow an empty mailbox because that will | |
| 88 # # select the INBOX, whatever it is called | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
89 |
| 3203 | 90 except (KeyboardInterrupt, EOFError): |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
91 raise ValueError('Canceled by User') |
| 3203 | 92 |
| 93 def __str__(self): | |
| 94 return 'Mailbox{ server:%(server)s, protocol:%(protocol)s, ' \ | |
| 95 'username:%(username)s, mailbox:%(mailbox)s, ' \ | |
| 96 'dbhome:%(dbhome)s }' % self.__dict__ | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
97 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
98 |
| 3203 | 99 # [als] class name is misleading. this is imap client, not imap server |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
100 class IMAPServer: |
| 3203 | 101 |
| 102 """IMAP mail gatherer. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
103 |
| 3203 | 104 This class runs as a server process. It is configured with a list of |
| 105 mailboxes to connect to, along with the roundup database directories | |
| 106 that correspond with each email address. It then connects to each | |
| 107 mailbox at a specified interval, and if there are new messages it | |
| 108 reads them, and sends the result to the roundup.mailgw. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
109 |
| 3203 | 110 *TODO*: |
| 111 Try to be smart about how you access the mailboxes so that you can | |
| 112 connect once, and access multiple mailboxes and possibly multiple | |
| 113 usernames. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
114 |
| 3203 | 115 *NOTE*: |
| 116 This assumes that if you are using the same user on the same | |
| 117 server, you are using the same password. (the last one supplied is | |
| 118 used.) Empty passwords are ignored. Only the last protocol | |
| 119 supplied is used. | |
| 120 """ | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
121 |
| 3203 | 122 def __init__(self, pidfile=None, delay=5, daemon=False): |
| 123 #This is sorted by servername, then username, then mailboxes | |
| 124 self.mailboxes = {} | |
| 125 self.delay = float(delay) | |
| 126 self.pidfile = pidfile | |
| 127 self.daemon = daemon | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
128 |
| 3203 | 129 def setDelay(self, delay): |
| 130 self.delay = delay | |
| 131 | |
| 132 def addMailbox(self, mailbox): | |
| 133 """ The linkage is as follows: | |
| 134 servers -- users - mailbox:dbhome | |
| 135 So there can be multiple servers, each with multiple users. | |
| 136 Each username can be associated with multiple mailboxes. | |
| 137 each mailbox is associated with 1 database home | |
| 138 """ | |
| 139 log.info('Adding mailbox %s', mailbox) | |
| 140 if not self.mailboxes.has_key(mailbox.server): | |
| 141 self.mailboxes[mailbox.server] = {'protocol':'imaps', 'users':{}} | |
| 142 server = self.mailboxes[mailbox.server] | |
| 143 if mailbox.protocol: | |
| 144 server['protocol'] = mailbox.protocol | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
145 |
| 3203 | 146 if not server['users'].has_key(mailbox.username): |
| 147 server['users'][mailbox.username] = {'password':'', 'mailboxes':{}} | |
| 148 user = server['users'][mailbox.username] | |
| 149 if mailbox.password: | |
| 150 user['password'] = mailbox.password | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
151 |
| 3203 | 152 if user['mailboxes'].has_key(mailbox.mailbox): |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
153 raise ValueError('Mailbox is already defined') |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
154 |
| 3203 | 155 user['mailboxes'][mailbox.mailbox] = mailbox.dbhome |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
156 |
| 3203 | 157 def _process(self, message, dbhome): |
| 158 """Actually process one of the email messages""" | |
| 159 child = os.popen('roundup-mailgw %s' % dbhome, 'wb') | |
| 160 child.write(message) | |
| 161 child.close() | |
| 162 #print message | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
163 |
| 3203 | 164 def _getMessages(self, serv, count, dbhome): |
| 165 """This assumes that you currently have a mailbox open, and want to | |
| 166 process all messages that are inside. | |
| 167 """ | |
| 168 for n in range(1, count+1): | |
| 169 (t, data) = serv.fetch(n, '(RFC822)') | |
| 170 if t == 'OK': | |
| 171 self._process(data[0][1], dbhome) | |
| 172 serv.store(n, '+FLAGS', r'(\Deleted)') | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
173 |
| 3203 | 174 def checkBoxes(self): |
| 175 """This actually goes out and does all the checking. | |
| 176 Returns False if there were any errors, otherwise returns true. | |
| 177 """ | |
| 178 noErrors = True | |
| 179 for server in self.mailboxes: | |
| 180 log.info('Connecting to server: %s', server) | |
| 181 s_vals = self.mailboxes[server] | |
| 182 | |
| 183 try: | |
| 184 for user in s_vals['users']: | |
| 185 u_vals = s_vals['users'][user] | |
| 186 # TODO: As near as I can tell, you can only | |
| 187 # login with 1 username for each connection to a server. | |
| 188 protocol = s_vals['protocol'].lower() | |
| 189 if protocol == 'imaps': | |
| 190 serv = imaplib.IMAP4_SSL(server) | |
| 191 elif protocol == 'imap': | |
| 192 serv = imaplib.IMAP4(server) | |
| 193 else: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
194 raise ValueError('Unknown protocol %s' % protocol) |
| 3203 | 195 |
| 196 password = u_vals['password'] | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
197 |
| 3203 | 198 try: |
| 199 log.info('Connecting as user: %s', user) | |
| 200 serv.login(user, password) | |
| 201 | |
| 202 for mbox in u_vals['mailboxes']: | |
| 203 dbhome = u_vals['mailboxes'][mbox] | |
| 204 log.info('Using mailbox: %s, home: %s', | |
| 205 mbox, dbhome) | |
| 206 #access a specific mailbox | |
| 207 if mbox: | |
| 208 (t, data) = serv.select(mbox) | |
| 209 else: | |
| 210 # Select the default mailbox (INBOX) | |
| 211 (t, data) = serv.select() | |
| 212 try: | |
| 213 nMessages = int(data[0]) | |
| 214 except ValueError: | |
| 215 nMessages = 0 | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
216 |
| 3203 | 217 log.info('Found %s messages', nMessages) |
| 218 | |
| 219 if nMessages: | |
| 220 self._getMessages(serv, nMessages, dbhome) | |
| 221 serv.expunge() | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
222 |
| 3203 | 223 # We are done with this mailbox |
| 224 serv.close() | |
| 225 except: | |
| 226 log.exception('Exception with server %s user %s', | |
| 227 server, user) | |
| 228 noErrors = False | |
| 229 | |
| 230 serv.logout() | |
| 231 serv.shutdown() | |
| 232 del serv | |
| 233 except: | |
| 234 log.exception('Exception while connecting to %s', server) | |
| 235 noErrors = False | |
| 236 return noErrors | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
237 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
238 |
| 3203 | 239 def makeDaemon(self): |
| 240 """Turn this process into a daemon. | |
| 241 | |
| 242 - make our parent PID 1 | |
| 243 | |
| 244 Write our new PID to the pidfile. | |
| 245 | |
| 246 From A.M. Kuuchling (possibly originally Greg Ward) with | |
| 247 modification from Oren Tirosh, and finally a small mod from me. | |
| 248 Originally taken from roundup.scripts.roundup_server.py | |
| 249 """ | |
| 250 log.info('Running as Daemon') | |
| 251 # Fork once | |
| 252 if os.fork() != 0: | |
| 253 os._exit(0) | |
| 254 | |
| 255 # Create new session | |
| 256 os.setsid() | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
257 |
| 3203 | 258 # Second fork to force PPID=1 |
| 259 pid = os.fork() | |
| 260 if pid: | |
| 261 if self.pidfile: | |
| 262 pidfile = open(self.pidfile, 'w') | |
| 263 pidfile.write(str(pid)) | |
| 264 pidfile.close() | |
| 265 os._exit(0) | |
| 266 | |
| 267 def run(self): | |
| 268 """Run email gathering daemon. | |
| 269 | |
| 270 This spawns itself as a daemon, and then runs continually, just | |
| 271 sleeping inbetween checks. It is recommended that you run | |
| 272 checkBoxes once first before you select run. That way you can | |
| 273 know if there were any failures. | |
| 274 """ | |
| 275 if self.daemon: | |
| 276 self.makeDaemon() | |
| 277 while True: | |
| 278 | |
| 279 time.sleep(self.delay * 60.0) | |
| 280 log.info('Time: %s', time.strftime('%Y-%m-%d %H:%M:%S')) | |
| 281 self.checkBoxes() | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
282 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
283 def getItems(s): |
| 3203 | 284 """Parse a string looking for userame@server""" |
| 285 myRE = re.compile( | |
| 286 r'((?P<protocol>[^:]+)://)?'#You can supply a protocol if you like | |
| 287 r'(' #The username part is optional | |
| 288 r'(?P<username>[^:]+)' #You can supply the password as | |
| 289 r'(:(?P<password>.+))?' #username:password@server | |
| 290 r'@)?' | |
| 291 r'(?P<server>[^/]+)' | |
| 292 r'(/(?P<mailbox>.+))?$' | |
| 293 ) | |
| 294 m = myRE.match(s) | |
| 295 if m: | |
| 296 return m.groupdict() | |
| 297 else: | |
| 298 return None | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
299 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
300 def main(): |
| 3203 | 301 """This is what is called if run at the prompt""" |
| 302 parser = optparse.OptionParser( | |
| 303 version=('%prog ' + version), | |
| 304 usage="""usage: %prog [options] (home server)... | |
| 305 | |
| 306 So each entry has a home, and then the server configuration. Home is just | |
| 307 a path to the roundup issue tracker. The server is something of the form: | |
| 308 | |
| 309 imaps://user:password@server/mailbox | |
| 310 | |
| 311 If you don't supply the protocol, imaps is assumed. Without user or | |
| 312 password, you will be prompted for them. The server must be supplied. | |
| 313 Without mailbox the INBOX is used. | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
314 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
315 Examples: |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
316 %prog /home/roundup/trackers/test imaps://test@imap.example.com/test |
| 3203 | 317 %prog /home/roundup/trackers/test imap.example.com \ |
| 318 /home/roundup/trackers/test2 imap.example.com/test2 | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
319 """ |
| 3203 | 320 ) |
| 321 parser.add_option('-d', '--delay', dest='delay', type='float', | |
| 322 metavar='<sec>', default=5, | |
| 323 help="Set the delay between checks in minutes. (default 5)" | |
| 324 ) | |
| 325 parser.add_option('-p', '--pid-file', dest='pidfile', | |
| 326 metavar='<file>', default=None, | |
| 327 help="The pid of the server process will be written to <file>" | |
| 328 ) | |
| 329 parser.add_option('-n', '--no-daemon', dest='daemon', | |
| 330 action='store_false', default=True, | |
| 331 help="Do not fork into the background after running the first check." | |
| 332 ) | |
| 333 parser.add_option('-v', '--verbose', dest='verbose', | |
| 334 action='store_const', const=logging.INFO, | |
| 335 help="Be more verbose in letting you know what is going on." | |
| 336 " Enables informational messages." | |
| 337 ) | |
| 338 parser.add_option('-V', '--very-verbose', dest='verbose', | |
| 339 action='store_const', const=logging.DEBUG, | |
| 340 help="Be very verbose in letting you know what is going on." | |
| 341 " Enables debugging messages." | |
| 342 ) | |
| 343 parser.add_option('-q', '--quiet', dest='verbose', | |
| 344 action='store_const', const=logging.ERROR, | |
| 345 help="Be less verbose. Ignores warnings, only prints errors." | |
| 346 ) | |
| 347 parser.add_option('-Q', '--very-quiet', dest='verbose', | |
| 348 action='store_const', const=logging.CRITICAL, | |
| 349 help="Be much less verbose. Ignores warnings and errors." | |
| 350 " Only print CRITICAL messages." | |
| 351 ) | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
352 |
| 3203 | 353 (opts, args) = parser.parse_args() |
| 354 if (len(args) == 0) or (len(args) % 2 == 1): | |
| 355 parser.error('Invalid number of arguments. ' | |
| 356 'Each site needs a home and a server.') | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
357 |
|
5141
939dce88cfc2
issue2550776: fix missing intialization
John Rouillard <rouilj@ieee.org>
parents:
4421
diff
changeset
|
358 if opts.verbose == None: |
|
939dce88cfc2
issue2550776: fix missing intialization
John Rouillard <rouilj@ieee.org>
parents:
4421
diff
changeset
|
359 opts.verbose = logging.WARNING |
|
939dce88cfc2
issue2550776: fix missing intialization
John Rouillard <rouilj@ieee.org>
parents:
4421
diff
changeset
|
360 |
| 3203 | 361 log.setLevel(opts.verbose) |
| 362 myServer = IMAPServer(delay=opts.delay, pidfile=opts.pidfile, | |
| 363 daemon=opts.daemon) | |
| 364 for i in range(0,len(args),2): | |
| 365 home = args[i] | |
| 366 server = args[i+1] | |
| 367 if not os.path.exists(home): | |
| 368 parser.error('Home: "%s" does not exist' % home) | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
369 |
| 3203 | 370 info = getItems(server) |
| 371 if not info: | |
| 372 parser.error('Invalid server string: "%s"' % server) | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
373 |
| 3203 | 374 myServer.addMailbox( |
| 375 RoundupMailbox(dbhome=home, mailbox=info['mailbox'] | |
| 376 , username=info['username'], password=info['password'] | |
| 377 , server=info['server'], protocol=info['protocol'] | |
| 378 ) | |
| 379 ) | |
| 380 | |
| 381 if myServer.checkBoxes(): | |
| 382 myServer.run() | |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
383 |
|
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
384 if __name__ == '__main__': |
| 3203 | 385 main() |
|
3176
18ad9d702a5b
added "imapServer.py" script (patch [SF#934567])
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
386 |
| 3203 | 387 # vim: et ft=python si sts=4 sw=4 |
