2012-10-02 14:09:24 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
import urllib
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
import simplejson
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import textwrap
|
|
|
|
from urllib2 import HTTPError
|
|
|
|
|
|
|
|
sys.path.append("/mit/tabbott/Public/python-zephyr/")
|
|
|
|
sys.path.append("/mit/tabbott/Public/python-zephyr/build/lib.linux-x86_64-2.6/")
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--forward-class-messages',
|
|
|
|
dest='forward_class_messages',
|
|
|
|
default=False,
|
|
|
|
action='store_true')
|
|
|
|
parser.add_option('--resend-log',
|
|
|
|
dest='resend_log',
|
|
|
|
default=False,
|
|
|
|
action='store_true')
|
|
|
|
parser.add_option('--enable-log',
|
|
|
|
dest='enable_log',
|
|
|
|
default=False,
|
|
|
|
action='store_true')
|
|
|
|
parser.add_option('--no-forward-personals',
|
|
|
|
dest='forward_personals',
|
|
|
|
default=True,
|
|
|
|
action='store_false')
|
|
|
|
parser.add_option('--forward-from-humbug',
|
|
|
|
dest='forward_to_humbug',
|
|
|
|
default=True,
|
|
|
|
action='store_false')
|
2012-10-11 16:20:38 -04:00
|
|
|
parser.add_option('--no-auto-subscribe',
|
|
|
|
dest='auto_subscribe',
|
|
|
|
default=True,
|
|
|
|
action='store_false')
|
2012-10-02 15:47:59 -04:00
|
|
|
parser.add_option('--site',
|
|
|
|
dest='site',
|
|
|
|
default="https://app.humbughq.com",
|
|
|
|
action='store')
|
|
|
|
parser.add_option('--api-key',
|
|
|
|
dest='api_key',
|
|
|
|
default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
action='store')
|
2012-10-02 14:09:24 -04:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-10-02 15:47:59 -04:00
|
|
|
sys.path.append(".")
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
import api.common
|
|
|
|
humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu",
|
|
|
|
api_key=options.api_key,
|
|
|
|
verbose=True,
|
|
|
|
site=options.site)
|
|
|
|
|
|
|
|
import zephyr
|
|
|
|
zephyr.init()
|
2012-10-11 14:39:52 -04:00
|
|
|
subs = zephyr.Subscriptions()
|
2012-10-02 15:47:59 -04:00
|
|
|
|
2012-10-12 13:53:29 -04:00
|
|
|
def humbug_username(zephyr_username):
|
2012-10-02 14:09:24 -04:00
|
|
|
return zephyr_username.lower().split("@")[0] + "@mit.edu"
|
|
|
|
|
|
|
|
def send_humbug(zeph):
|
2012-10-03 16:32:50 -04:00
|
|
|
zeph["forged"] = "yes"
|
2012-10-12 13:53:29 -04:00
|
|
|
zeph["sender"] = humbug_username(zeph["sender"])
|
2012-10-02 14:09:24 -04:00
|
|
|
zeph['fullname'] = username_to_fullname(zeph['sender'])
|
|
|
|
zeph['shortname'] = zeph['sender'].split('@')[0]
|
2012-10-10 18:01:39 -04:00
|
|
|
if "subject" in zeph:
|
2012-10-11 10:35:50 -04:00
|
|
|
zeph["subject"] = zeph["subject"][:60]
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
for key in zeph.keys():
|
|
|
|
if isinstance(zeph[key], unicode):
|
2012-10-03 16:32:50 -04:00
|
|
|
zeph[key] = zeph[key].encode("utf-8")
|
2012-10-02 14:09:24 -04:00
|
|
|
elif isinstance(zeph[key], str):
|
2012-10-03 16:32:50 -04:00
|
|
|
zeph[key] = zeph[key].decode("utf-8")
|
2012-10-02 14:09:24 -04:00
|
|
|
|
2012-10-04 16:13:47 -04:00
|
|
|
return humbug_client.send_message(zeph)
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
def fetch_fullname(username):
|
|
|
|
try:
|
|
|
|
match_user = re.match(r'([a-zA-Z0-9_]+)@mit\.edu', username)
|
|
|
|
if match_user:
|
2012-10-12 13:53:29 -04:00
|
|
|
proc = subprocess.Popen(['hesinfo', match_user.group(1), 'passwd'],
|
|
|
|
stdout=subprocess.PIPE)
|
2012-10-02 14:09:24 -04:00
|
|
|
out, _err_unused = proc.communicate()
|
|
|
|
if proc.returncode == 0:
|
|
|
|
return out.split(':')[4].split(',')[0]
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, 'Error getting fullname for', username
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2012-10-12 13:21:21 -04:00
|
|
|
domains = [
|
|
|
|
("@CS.CMU.EDU", " (CMU)"),
|
|
|
|
("@ANDREW.CMU.EDU", " (CMU)"),
|
|
|
|
("@IASTATE.EDU", " (IASTATE)"),
|
|
|
|
("@1TS.ORG", " (1TS)"),
|
|
|
|
("@DEMENTIA.ORG", " (DEMENTIA)"),
|
|
|
|
("@MIT.EDU", ""),
|
|
|
|
]
|
|
|
|
for (domain, tag) in domains:
|
|
|
|
if username.upper().endswith(domain):
|
|
|
|
return username.split("@")[0] + tag
|
2012-10-12 11:45:41 -04:00
|
|
|
return username
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
fullnames = {}
|
|
|
|
def username_to_fullname(username):
|
|
|
|
if username not in fullnames:
|
|
|
|
fullnames[username] = fetch_fullname(username)
|
|
|
|
return fullnames[username]
|
|
|
|
|
2012-10-11 14:39:52 -04:00
|
|
|
current_zephyr_subs = {}
|
|
|
|
def ensure_subscribed(sub):
|
|
|
|
if sub in current_zephyr_subs:
|
|
|
|
return
|
|
|
|
subs.add((sub, '*', '*'))
|
|
|
|
current_zephyr_subs[sub] = True
|
|
|
|
|
|
|
|
def update_subscriptions_from_humbug():
|
|
|
|
try:
|
|
|
|
res = humbug_client.get_public_streams()
|
|
|
|
streams = res["streams"]
|
|
|
|
except:
|
|
|
|
print "Error getting public streams:"
|
|
|
|
traceback.print_exc()
|
|
|
|
return
|
|
|
|
for stream in streams:
|
|
|
|
ensure_subscribed(stream)
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
def process_loop(log):
|
2012-10-11 14:39:52 -04:00
|
|
|
sleep_count = 0
|
|
|
|
sleep_time = 0.1
|
2012-10-02 14:09:24 -04:00
|
|
|
while True:
|
2012-10-11 14:39:52 -04:00
|
|
|
notice = zephyr.receive(block=False)
|
2012-10-12 10:16:13 -04:00
|
|
|
if notice is None:
|
2012-10-11 14:39:52 -04:00
|
|
|
time.sleep(sleep_time)
|
|
|
|
sleep_count += sleep_time
|
|
|
|
if sleep_count > 15:
|
|
|
|
sleep_count = 0
|
2012-10-12 10:16:13 -04:00
|
|
|
if options.forward_class_messages:
|
|
|
|
# Ask the Humbug server about any new classes to subscribe to
|
|
|
|
update_subscriptions_from_humbug()
|
2012-10-11 14:39:52 -04:00
|
|
|
continue
|
|
|
|
|
2012-10-02 14:09:24 -04:00
|
|
|
try:
|
2012-10-12 14:39:47 -04:00
|
|
|
process_notice(notice, log)
|
2012-10-02 14:09:24 -04:00
|
|
|
except:
|
|
|
|
print >>sys.stderr, 'Error relaying zephyr'
|
|
|
|
traceback.print_exc()
|
|
|
|
time.sleep(2)
|
|
|
|
|
2012-10-12 14:39:47 -04:00
|
|
|
def process_notice(notice, log):
|
|
|
|
zsig, body = notice.message.split("\x00", 1)
|
|
|
|
is_personal = False
|
|
|
|
is_huddle = False
|
|
|
|
|
|
|
|
if notice.opcode == "PING":
|
|
|
|
# skip PING messages
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(zsig, str):
|
|
|
|
# Check for width unicode character u'\u200B'.encode("utf-8")
|
|
|
|
if u'\u200B'.encode("utf-8") in zsig:
|
|
|
|
print "Skipping message from Humbug!"
|
|
|
|
return
|
|
|
|
|
|
|
|
sender = notice.sender.lower().replace("athena.mit.edu", "mit.edu")
|
|
|
|
recipient = notice.recipient.lower().replace("athena.mit.edu", "mit.edu")
|
|
|
|
|
|
|
|
if (notice.cls.lower() == "message" and
|
|
|
|
notice.instance.lower() == "personal"):
|
|
|
|
is_personal = True
|
|
|
|
if body.startswith("CC:"):
|
|
|
|
is_huddle = True
|
|
|
|
# Map "CC: sipbtest espuser" => "starnine@mit.edu,espuser@mit.edu"
|
|
|
|
huddle_recipients_list = [humbug_username(x.strip()) for x in
|
|
|
|
body.split("\n")[0][4:].split()]
|
|
|
|
if sender not in huddle_recipients_list:
|
|
|
|
huddle_recipients_list.append(sender)
|
|
|
|
huddle_recipients = ",".join(huddle_recipients_list)
|
|
|
|
if (notice.cls.lower() == "mail" and
|
|
|
|
notice.instance.lower() == "inbox"):
|
|
|
|
is_personal = True
|
|
|
|
|
|
|
|
# Drop messages not to the listed subscriptions
|
|
|
|
if (notice.cls.lower() not in current_zephyr_subs) and not \
|
|
|
|
(is_personal and options.forward_personals):
|
|
|
|
print "Skipping ...", notice.cls, notice.instance, is_personal
|
|
|
|
return
|
|
|
|
|
|
|
|
if is_huddle:
|
|
|
|
zeph = { 'type' : 'personal',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'recipient' : huddle_recipients,
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'content' : body.split("\n", 1)[1] }
|
|
|
|
elif is_personal:
|
|
|
|
zeph = { 'type' : 'personal',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'recipient' : humbug_username(recipient),
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'content' : body }
|
|
|
|
else:
|
|
|
|
zeph = { 'type' : 'stream',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'stream' : notice.cls.lower(),
|
|
|
|
'subject' : notice.instance.lower(),
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'content' : body }
|
|
|
|
|
|
|
|
print "%s: received a message on %s/%s from %s..." % \
|
|
|
|
(datetime.datetime.now(), notice.cls, notice.instance,
|
|
|
|
notice.sender)
|
|
|
|
log.write(simplejson.dumps(zeph) + '\n')
|
|
|
|
log.flush()
|
|
|
|
|
|
|
|
res = send_humbug(zeph)
|
|
|
|
if res.get("result") != "success":
|
|
|
|
print >>sys.stderr, 'Error relaying zephyr'
|
|
|
|
print zeph
|
|
|
|
print res
|
|
|
|
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
def zephyr_to_humbug(options):
|
|
|
|
import mit_subs_list
|
2012-10-12 13:40:57 -04:00
|
|
|
if options.auto_subscribe:
|
|
|
|
add_humbug_subscriptions()
|
2012-10-02 14:09:24 -04:00
|
|
|
if options.forward_class_messages:
|
|
|
|
for sub in mit_subs_list.all_subs:
|
2012-10-11 14:39:52 -04:00
|
|
|
ensure_subscribed(sub)
|
|
|
|
update_subscriptions_from_humbug()
|
2012-10-02 14:09:24 -04:00
|
|
|
if options.forward_personals:
|
2012-10-12 13:40:57 -04:00
|
|
|
subs.add(("message", "personal", os.environ["USER"] + "@ATHENA.MIT.EDU"))
|
|
|
|
if subscribed_to_mail_messages():
|
|
|
|
subs.add(("mail", "inbox", os.environ["USER"] + "@ATHENA.MIT.EDU"))
|
2012-10-02 14:09:24 -04:00
|
|
|
|
|
|
|
if options.resend_log:
|
|
|
|
with open('zephyrs', 'r') as log:
|
|
|
|
for ln in log:
|
|
|
|
try:
|
|
|
|
zeph = simplejson.loads(ln)
|
|
|
|
print "sending saved message to %s from %s..." % \
|
|
|
|
(zeph.get('class', zeph.get('recipient')), zeph['sender'])
|
|
|
|
send_humbug(zeph)
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, 'Could not send saved zephyr'
|
|
|
|
traceback.print_exc()
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
print "Starting receive loop"
|
|
|
|
|
|
|
|
if options.enable_log:
|
|
|
|
log_file = "zephyrs"
|
|
|
|
else:
|
|
|
|
log_file = "/dev/null"
|
|
|
|
|
|
|
|
with open(log_file, 'a') as log:
|
|
|
|
process_loop(log)
|
|
|
|
|
2012-10-02 15:47:59 -04:00
|
|
|
def forward_to_zephyr(message):
|
2012-10-02 14:09:24 -04:00
|
|
|
zsig = u"%s\u200B" % (username_to_fullname(message["sender_email"]))
|
|
|
|
if ' dot ' in zsig:
|
2012-10-12 13:53:29 -04:00
|
|
|
print "ERROR! Couldn't compute zsig for %s!" % \
|
|
|
|
(message["sender_email"])
|
2012-10-02 14:09:24 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
wrapped_content = "\n".join("\n".join(textwrap.wrap(line))
|
2012-10-11 13:34:30 -04:00
|
|
|
for line in message["content"].split("\n"))
|
2012-10-02 14:09:24 -04:00
|
|
|
|
2012-10-12 13:53:29 -04:00
|
|
|
sender_email = message["sender_email"].replace("mit.edu", "ATHENA.MIT.EDU")
|
|
|
|
print "Sending message from %s humbug=>zephyr at %s" % \
|
|
|
|
(sender_email, datetime.datetime.now())
|
2012-10-10 16:57:21 -04:00
|
|
|
if message['type'] == "stream":
|
2012-10-12 13:53:29 -04:00
|
|
|
zeph = zephyr.ZNotice(sender=sender_email, auth=True,
|
|
|
|
cls=message["display_recipient"],
|
2012-10-10 18:01:39 -04:00
|
|
|
instance=message["subject"])
|
2012-10-02 14:09:24 -04:00
|
|
|
body = "%s\0%s" % (zsig, wrapped_content)
|
|
|
|
zeph.setmessage(body)
|
|
|
|
zeph.send()
|
|
|
|
elif message['type'] == "personal":
|
2012-10-12 13:53:29 -04:00
|
|
|
recipient = message["display_recipient"]["email"]
|
|
|
|
recipient.replace("mit.edu", "ATHENA.MIT.EDU")
|
|
|
|
zeph = zephyr.ZNotice(sender=sender_email,
|
|
|
|
auth=True, recipient=recipient,
|
2012-10-02 14:09:24 -04:00
|
|
|
cls="message", instance="personal")
|
|
|
|
body = "%s\0%s" % (zsig, wrapped_content)
|
|
|
|
zeph.setmessage(body)
|
|
|
|
zeph.send()
|
|
|
|
elif message['type'] == "huddle":
|
|
|
|
cc_list = ["CC:"]
|
|
|
|
cc_list.extend([user["email"].replace("@mit.edu", "")
|
|
|
|
for user in message["display_recipient"]])
|
|
|
|
body = "%s\0%s\n%s" % (zsig, " ".join(cc_list), wrapped_content)
|
|
|
|
for r in message["display_recipient"]:
|
2012-10-12 13:53:29 -04:00
|
|
|
recipient = r["email"].replace("mit.edu", "ATHENA.MIT.EDU")
|
|
|
|
zeph = zephyr.ZNotice(sender=sender_email, auth=True,
|
|
|
|
recipient=recipient, cls="message",
|
|
|
|
instance="personal")
|
2012-10-02 14:09:24 -04:00
|
|
|
zeph.setmessage(body)
|
|
|
|
zeph.send()
|
|
|
|
|
2012-10-02 15:47:59 -04:00
|
|
|
def maybe_forward_to_zephyr(message):
|
|
|
|
if message["sender_email"] == os.environ["USER"] + "@mit.edu":
|
2012-10-12 13:53:29 -04:00
|
|
|
timestamp_now = datetime.datetime.now().strftime("%s")
|
|
|
|
if float(message["timestamp"]) < float(timestamp_now) - 15:
|
|
|
|
print "Alert! Out of order message: %s < %s" % \
|
|
|
|
(message["timestamp"], timestamp_now)
|
2012-10-02 15:47:59 -04:00
|
|
|
return
|
|
|
|
forward_to_zephyr(message)
|
|
|
|
|
2012-10-02 14:09:24 -04:00
|
|
|
def humbug_to_zephyr(options):
|
|
|
|
# Sync messages from zephyr to humbug
|
|
|
|
print "Starting syncing messages."
|
2012-10-02 15:47:59 -04:00
|
|
|
humbug_client.call_on_each_message(maybe_forward_to_zephyr,
|
|
|
|
options={"mit_sync_bot": 'yes'})
|
2012-10-02 14:09:24 -04:00
|
|
|
|
2012-10-12 13:40:57 -04:00
|
|
|
def subscribed_to_mail_messages():
|
|
|
|
for (cls, instance, recipient) in parse_zephyr_subs(verbose=False):
|
|
|
|
if (cls.lower() == "mail" and instance.lower() == "inbox"):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-10-11 16:20:38 -04:00
|
|
|
def add_humbug_subscriptions():
|
2012-10-12 13:40:57 -04:00
|
|
|
zephyr_subscriptions = set()
|
|
|
|
for (cls, instance, recipient) in parse_zephyr_subs(verbose=True):
|
|
|
|
if instance != "*" or recipient != "*":
|
|
|
|
print "Skipping ~/.zephyr.subs line: [%s,%s,%s]: Non-* values" % \
|
|
|
|
(cls, instance, recipient)
|
|
|
|
continue
|
|
|
|
zephyr_subscriptions.add(cls)
|
|
|
|
if len(zephyr_subscriptions) != 0:
|
|
|
|
humbug_client.subscribe(list(zephyr_subscriptions))
|
|
|
|
|
|
|
|
def parse_zephyr_subs(verbose=False):
|
|
|
|
if verbose:
|
|
|
|
print "Adding your ~/.zephyr.subs subscriptions to Humbug!"
|
2012-10-11 16:20:38 -04:00
|
|
|
zephyr_subscriptions = set()
|
|
|
|
subs_file = os.path.join(os.environ["HOME"], ".zephyr.subs")
|
|
|
|
if not os.path.exists(subs_file):
|
2012-10-12 13:40:57 -04:00
|
|
|
if verbose:
|
|
|
|
print >>sys.stderr, "Couldn't find .zephyr.subs!"
|
|
|
|
print >>sys.stderr, "Do you mean to run with --no-auto-subscribe?"
|
|
|
|
return
|
2012-10-11 16:20:38 -04:00
|
|
|
|
|
|
|
for line in file(subs_file, "r").readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) == 0:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
(cls, instance, recipient) = line.split(",")
|
|
|
|
except:
|
2012-10-12 13:40:57 -04:00
|
|
|
if verbose:
|
|
|
|
print >>sys.stderr, "Couldn't parse ~/.zephyr.subs line: [%s]" % (line,)
|
|
|
|
continue
|
|
|
|
zephyr_subscriptions.add((cls, instance, recipient))
|
|
|
|
return zephyr_subscriptions
|
2012-10-11 16:20:38 -04:00
|
|
|
|
2012-10-02 14:09:24 -04:00
|
|
|
if options.forward_to_humbug:
|
|
|
|
zephyr_to_humbug(options)
|
|
|
|
else:
|
|
|
|
humbug_to_zephyr(options)
|