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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
import threading
from os import getenv
from queue import Queue, Empty
from time import time, sleep
from cherrypy.process import wspbus, plugins
import cherrypy
import re
import imap_tools
from imap_tools import MailBox, MailMessage
from typing import Literal
from . import html
class ImapPlugin(plugins.SimplePlugin):
def __init__(self,bus):
plugins.SimplePlugin.__init__(self,bus)
self.imap_server=getenv("ACIT_IMAP_SERVER")
self.imap_user=getenv("ACIT_IMAP_USER")
self.imap_pass=getenv("ACIT_IMAP_PASS")
self.imap_port=getenv("ACIT_IMAP_PORT",0)
self.smtp_server=getenv("ACIT_SMTP_SERVER")
self.smtp_user=getenv("ACIT_SMTP_USER")
self.smtp_pass=getenv("ACIT_SMTP_PASS")
self.smtp_port=getenv("ACIT_SMTP_PORT",0)
if not ( self.imap_server and self.imap_user and self.smtp_server and self.smtp_user ):
raise ValueError("Missing ACIT_IMAP_SERVER, ACIT_IMAP_USER, ACIT_SMTP_SERVER, or ACIT_SMTP_USER")
self.mailin_thread=None
self.mailout_thread=None
self.stopping=threading.Event()
self.mailout_queue=Queue()
self.mailbox_per_thread={}
emailcache={}
def start(self):
self.mlog("Starting email-related loops, doing setup")
self.stopping.clear()
self.mailin_thread=threading.Thread(target=self.imap_loop_controller)
self.mailout_thread=threading.Thread(target=self.smtp_loop)
# the following commented block was too free to be parsed. If you can get it to work, feel free to email a patch.
#self.addrformat=getenv("ACIT_ADDRESSFORMAT",
# self.imap_user.replace("@","+{proj}#{bug}@",1) # default to something like something+example#15@example.com
# )
#self.addrregex=getenv("ACIT_ADDRESSREGEX",
# self.addrformat.replace("+","\\+").format(proj="[^@#]*",bug="[0-9]*")
# # note above default only works if your email address doesn't contain any character regex picks up, and you use plus-addresses
# )
self.uses_aliases=getenv("ACIT_MAIL_USES_ALIASES",False)
name,domain=self.imap_user.rsplit("@",1)
self.emaildomain=getenv("ACIT_MAIL_DOMAIN",domain)
self.emailname=getenv("ACIT_MAIL_NAME",name)
if self.uses_aliases:
self.addr_format="{proj}#{bug}@"+self.emaildomain
self.addr_regex="[^@#]*(#[0-9]*)?@"+ self.emaildomain.replace(".","\\.")
else:
self.addr_format=self.emailname+"+{proj}#{bug}@"+self.emaildomain
self.addr_regex=self.emailname+r"+[^@#]*(#[0-9]*)?@"+self.emaildomain.replace(".","\\.")
self.mailin_thread.start()
#self.mailout_thread.start()
self.mlog("Done")
def get_full_projectname(self,proj):
projects=["cgit","acit","folder","subfolder/folder"]
matches=[]
for project in projects:
if proj==project:
matches.clear()
matches.append(project)
break
else:
if ('/'+project).endswith(proj):
matches.append(proj)
return matches
def stripInfoFromMailAddr(self,address:str):
addr=address.removesuffix("@"+self.emaildomain)
if not self.uses_aliases:
if not '+' in addr:
return (None,None)
addr=addr.removeprefix(self.emailname)
addr=addr.removeprefix("+")
if '#' in addr:
proj,bug=addr.rsplit("#",1)
else:
proj=addr
bug=None
return (proj,bug)
def ensurefolder(self,mailbox:MailBox,*path):
# assuming from rfc3501 section 7.2.2:
# > All children of a top-level hierarchy node MUST use
# > the same separator character.
# hoping the whole mailserver uses the same delimiter
delim=mailbox.folder.list('')[0].delim
fname=delim.join([ str(i) for i in path])
if not mailbox.folder.exists(fname):
mailbox.folder.create(fname)
mailbox.folder.subscribe(fname,True)
return fname
def get_bug_folder(self,mailbox:MailBox,proj,bug):
return self.ensurefolder(mailbox,"bugs",proj,str(bug))
def assign_new_bugnr(self,mailbox:MailBox,proj):
current=mailbox.folder.list(self.ensurefolder(mailbox,"bugs",proj))
nrs=[ folder.name.rsplit(folder.delim,1)[-1] for folder in current if folder.delim in folder.name ]
nrs=[ int(i) for i in nrs if i.isdigit() ]
high=max([0]+nrs)
return high+1
def get_MailBox(self):
tid=threading.current_thread()
if not tid in self.mailbox_per_thread:
self.mailbox_per_thread[tid]=MailBox(self.imap_server).login(self.imap_user,self.imap_pass, None)
return self.mailbox_per_thread[tid]
def generate_page(self,mailbox:MailBox,project=None,bug=None):
return
if not bug and not project:
return self.generate_main_page(mailbox)
if project and not bug:
return self.generate_project_page(mailbox,project)
try:
mailbox.folder.set(self.get_bug_folder(mailbox,project,bug))
pagelimit=25
currentpage=""
for msg in mailbox.fetch():
pass
except Exception:
self.mlog("Error occured generating bug page for %s/%d"%(project,bug),traceback=True)
def imap_magic(self,mailbox:MailBox):
mailbox.folder.set("INBOX")
# block: wait 5 minutes and poll after that
mailbox.idle.start()
if self.stopping.wait(5): # if not stopping, this just times out after 300 seconds, so this is a nice timer
mailbox.idle.stop()
return
responses=mailbox.idle.poll(timeout=1)
mailbox.idle.stop()
# end block
if responses or mailbox.folder.status()["MESSAGES"]>0:
refreshable={}
for msg in mailbox.fetch():
self.mlog("Processing email with subject '%s'"%msg.subject)
for addr in msg.to + msg.cc + msg.bcc + msg.reply_to:
if re.fullmatch(self.addr_regex,addr):
proj,bug=self.stripInfoFromMailAddr(addr)
break
else:
proj=None
bug=None
# block: make sure a project was specified
if not proj:
self.mlog("No project specified.")
self.mail_error(msg,"Please specify a project by mailing to:\n "+\
("" if self.uses_aliases else self.emailname+"+")+"PROJECT@"+self.emaildomain+\
"\nwhere PROJECT is the name of your target project")
self.move_errored_mail(mailbox,msg)
continue
# end block
# block: make sure project exists
proj_matches=self.get_full_projectname(proj)
if not proj_matches:
self.mlog("Received email for nonexistent project %s"%proj)
self.mail_error(msg,notice="Project '%s' doesn't exist"%proj)
self.move_errored_mail(mailbox,msg)
continue
# end block
# block: make sure only 1 project matches
if len(proj_matches)>1:
self.mlog("Conficting projectname. Sending projectlist.")
self.mail_error(msg,notice="Multiple projects found to match your query. Please specify. Options:\n%s"%"\n".join(proj_matches))
self.move_errored_mail(mailbox,msg)
continue
proj=proj_matches[0]
# block: parse bug id
if not bug:
bug=self.assign_new_bugnr(mailbox,proj)
self.mlog("Assigned new bugnr %d to '%s'"%(bug,msg.subject))
try:
bug=int(bug)
except ValueError as e:
self.mlog("Error decoding value to int:",e,traceback=True)
self.mail_error(msg,notice="Exception while trying to convert bug number to integer",exception=e)
self.move_errored_mail(mailbox,msg)
continue
# end block
# block: move mail to folder specific for this project/bug
try:
path=self.get_bug_folder(mailbox,proj,bug)
mailbox.move([msg.uid], path)
refreshable.setdefault(proj,[]).append(bug)
except Exception:
self.mlog("Error processing email '%s' for %s/%d"%(msg.subject,proj,bug),traceback=True)
# end block
# block: update all webpages that received new mail
for proj,bugs in refreshable.items():
self.generate_page(proj,None) # project page needs to be regenerated too (counters)
for bug in bugs:
self.generate_page(proj,bug)
self.generate_page(None,None) # main page needs to be generated too (a new project may have appeared + counters)
# end block
def imap_loop_controller(self):
threading.current_thread().setName("IMAPrunner")
self.mlog("Connecting to IMAP")
try:
with self.get_MailBox() as mailbox:
self.mlog("IMAP monitor thread started (connected)")
self.ensurefolder(mailbox,"INBOX")
while not self.stopping.is_set():
try:
self.imap_magic(mailbox)
except Exception:
import traceback
exc=traceback.format_exc()
self.mlog("Exception occured:\n%s"%exc)
self.mlog("!! this may lead to emails not appearing or appearing later !!")
except imap_tools.errors.MailboxLoginError:
self.mlog("Error logging in.")
cherrypy.engine.exit()
self.mlog("IMAP monitor thread stopped.")
def mail_error(self,msg:MailMessage,notice:str=None,exception:Exception=None):
pass
def move_errored_mail(self,mailbox:MailBox,msg:MailMessage,):
target=self.ensurefolder(mailbox,"INBOX","Errors")
return mailbox.move(msg.uid,target)
def smtp_loop(self):
pass
def stop(self):
self.mlog("Stopping. This can take a while.")
self.stopping.set()
for thread in ( self.mailin_thread, self.mailout_thread ):
if thread.is_alive() and not thread==threading.current_thread():
thread.join()
self.mlog("Stopped")
def mlog(self,*msg,**kwargs):
import traceback
function=traceback.extract_stack(limit=2)[0].name
cherrypy.log(context="MAIL:%s:%s"%(threading.current_thread().name, function), msg=" ".join([str(i) for i in msg]),**kwargs)
|