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