Commit dbbb3b64 authored by Benjamin Graillot's avatar Benjamin Graillot

[phabricator] Meilleure gestion des headers

parent f8294adb
......@@ -47,10 +47,10 @@ class Ninja(Bot):
except OSError:
pass
os.mkfifo(config.socket_path)
fifo = open(config.socket_path)
while True:
select.select([fifo],[],[fifo])
fifo = open(config.socket_path)
data = fifo.read()
fifo.close()
try:
if data.startswith('SOURCES '):
self.sources = data[8:].split(',')
......
......@@ -11,6 +11,17 @@ import secrets
import source
def decode_header(header):
header_parts = email.header.decode_header(header)
header = ''
for part, encoding in header_parts:
if isinstance(part, str):
header += part
else:
header += part.decode('ascii' if encoding is None else encoding, 'ignore')
return header
class Phabricator(source.Source):
def __init__(self):
source.Source.__init__(self, 'phabricator', 'noreply@phabricator.crans.org')
......@@ -22,7 +33,7 @@ class Phabricator(source.Source):
imap_client = imaplib.IMAP4_SSL(host=secrets.host)
imap_client.login(secrets.user, secrets.password)
imap_client.select()
print('Running...')
print('phabricator: Running...')
while True:
for uid, msg in imap_client.idle():
if msg == b'EXISTS':
......@@ -30,16 +41,17 @@ class Phabricator(source.Source):
fetcher.login(secrets.user, secrets.password)
fetcher.select()
mail = fetcher.fetch(uid.decode('ascii'), '(RFC822)')
fetcher.close()
fetcher.logout()
mail = email.message_from_bytes(mail[1][0][1])
sender = mail['From']
sender = decode_header(mail['From'])
if self.address not in sender:
continue
try:
projects = [project.strip()[2:-1] for project in mail['X-Phabricator-Projects'].split(',')]
except:
projects = []
subject = mail['Subject']
subject = decode_header(mail['Subject'])
tags = []
for m in re.finditer(r'\[(?P<tag>[^]]*)\]', subject):
tags.append(m.group('tag'))
......@@ -51,12 +63,12 @@ class Phabricator(source.Source):
if projects:
task = projects[0] + '/' + task
task_name = m.group('name')
m = re.search(r'"(?P<author>[^"]*)"', sender)
m = re.search(r'"?(?P<author>[^"<]*)"?\s*<', sender)
if m is None:
print("No author:", sender)
continue
author = m.group('author')
self.push_update(task, '\x0310{task}\x03 \x033{author}\x03 {tag}\x03 \x0315{name}\x03'.format(task=task, author=author, tag=tags[1], name=task_name))
self.push_update(task, '\x0310{task}\x03 \x033{author}\x03 \x02{tag}\x02\x03 \x0315{name}\x03'.format(task=task, author=author, tag=tags[1], name=task_name))
except Exception as e:
raise e
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment