Commit dbbb3b64 authored by Benjamin Graillot's avatar Benjamin Graillot

[phabricator] Meilleure gestion des headers

parent f8294adb
...@@ -47,10 +47,10 @@ class Ninja(Bot): ...@@ -47,10 +47,10 @@ class Ninja(Bot):
except OSError: except OSError:
pass pass
os.mkfifo(config.socket_path) os.mkfifo(config.socket_path)
fifo = open(config.socket_path)
while True: while True:
select.select([fifo],[],[fifo]) fifo = open(config.socket_path)
data = fifo.read() data = fifo.read()
fifo.close()
try: try:
if data.startswith('SOURCES '): if data.startswith('SOURCES '):
self.sources = data[8:].split(',') self.sources = data[8:].split(',')
......
...@@ -11,6 +11,17 @@ import secrets ...@@ -11,6 +11,17 @@ import secrets
import source 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): class Phabricator(source.Source):
def __init__(self): def __init__(self):
source.Source.__init__(self, 'phabricator', 'noreply@phabricator.crans.org') source.Source.__init__(self, 'phabricator', 'noreply@phabricator.crans.org')
...@@ -22,7 +33,7 @@ class Phabricator(source.Source): ...@@ -22,7 +33,7 @@ class Phabricator(source.Source):
imap_client = imaplib.IMAP4_SSL(host=secrets.host) imap_client = imaplib.IMAP4_SSL(host=secrets.host)
imap_client.login(secrets.user, secrets.password) imap_client.login(secrets.user, secrets.password)
imap_client.select() imap_client.select()
print('Running...') print('phabricator: Running...')
while True: while True:
for uid, msg in imap_client.idle(): for uid, msg in imap_client.idle():
if msg == b'EXISTS': if msg == b'EXISTS':
...@@ -30,16 +41,17 @@ class Phabricator(source.Source): ...@@ -30,16 +41,17 @@ class Phabricator(source.Source):
fetcher.login(secrets.user, secrets.password) fetcher.login(secrets.user, secrets.password)
fetcher.select() fetcher.select()
mail = fetcher.fetch(uid.decode('ascii'), '(RFC822)') mail = fetcher.fetch(uid.decode('ascii'), '(RFC822)')
fetcher.close()
fetcher.logout() fetcher.logout()
mail = email.message_from_bytes(mail[1][0][1]) mail = email.message_from_bytes(mail[1][0][1])
sender = mail['From'] sender = decode_header(mail['From'])
if self.address not in sender: if self.address not in sender:
continue continue
try: try:
projects = [project.strip()[2:-1] for project in mail['X-Phabricator-Projects'].split(',')] projects = [project.strip()[2:-1] for project in mail['X-Phabricator-Projects'].split(',')]
except: except:
projects = [] projects = []
subject = mail['Subject'] subject = decode_header(mail['Subject'])
tags = [] tags = []
for m in re.finditer(r'\[(?P<tag>[^]]*)\]', subject): for m in re.finditer(r'\[(?P<tag>[^]]*)\]', subject):
tags.append(m.group('tag')) tags.append(m.group('tag'))
...@@ -51,12 +63,12 @@ class Phabricator(source.Source): ...@@ -51,12 +63,12 @@ class Phabricator(source.Source):
if projects: if projects:
task = projects[0] + '/' + task task = projects[0] + '/' + task
task_name = m.group('name') task_name = m.group('name')
m = re.search(r'"(?P<author>[^"]*)"', sender) m = re.search(r'"?(?P<author>[^"<]*)"?\s*<', sender)
if m is None: if m is None:
print("No author:", sender) print("No author:", sender)
continue continue
author = m.group('author') 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: except Exception as e:
raise 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