2012-09-06 20:45:48 +02:00
|
|
|
#!/usr/bin/python
|
2012-09-13 02:29:26 +02:00
|
|
|
import mechanize
|
|
|
|
import urllib
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import zephyr
|
|
|
|
import traceback
|
|
|
|
import simplejson
|
|
|
|
import re
|
|
|
|
import time
|
2012-09-13 03:41:52 +02:00
|
|
|
import subprocess
|
2012-09-21 21:17:25 +02:00
|
|
|
import optparse
|
2012-09-13 02:29:26 +02:00
|
|
|
|
2012-09-21 21:17:25 +02:00
|
|
|
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('--no-forward-personals',
|
|
|
|
dest='forward_personals',
|
|
|
|
default=True,
|
|
|
|
action='store_false')
|
2012-09-24 20:29:33 +02:00
|
|
|
parser.add_option('--forward-from-humbug',
|
|
|
|
dest='forward_to_humbug',
|
|
|
|
default=True,
|
|
|
|
action='store_false')
|
2012-09-21 21:17:25 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-09-07 17:45:09 +02:00
|
|
|
browser = None
|
2012-09-07 16:54:28 +02:00
|
|
|
csrf_token = None
|
|
|
|
|
|
|
|
def browser_login():
|
2012-09-07 17:45:09 +02:00
|
|
|
logger = logging.getLogger("mechanize")
|
|
|
|
logger.addHandler(logging.StreamHandler(sys.stdout))
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
global browser
|
|
|
|
browser = mechanize.Browser()
|
2012-09-07 16:54:28 +02:00
|
|
|
browser.set_handle_robots(False)
|
2012-09-06 20:45:48 +02:00
|
|
|
## debugging code to consider
|
2012-09-07 16:54:28 +02:00
|
|
|
# browser.set_debug_http(True)
|
|
|
|
# browser.set_debug_responses(True)
|
|
|
|
# browser.set_debug_redirects(True)
|
|
|
|
# browser.set_handle_refresh(False)
|
2012-09-07 17:14:26 +02:00
|
|
|
|
2012-09-07 16:54:28 +02:00
|
|
|
browser.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki")
|
|
|
|
browser.open("https://app.humbughq.com/")
|
|
|
|
browser.follow_link(text_regex="\s*Log in\s*")
|
|
|
|
browser.select_form(nr=0)
|
2012-09-21 00:26:59 +02:00
|
|
|
browser["username"] = "starnine@mit.edu"
|
|
|
|
browser["password"] = "xxxxxxxx"
|
2012-09-07 16:54:28 +02:00
|
|
|
|
|
|
|
global csrf_token
|
2012-09-18 01:52:04 +02:00
|
|
|
csrf_token = browser["csrfmiddlewaretoken"]
|
|
|
|
|
|
|
|
browser.submit()
|
2012-09-06 20:45:48 +02:00
|
|
|
|
2012-09-07 19:13:00 +02:00
|
|
|
def send_zephyr(zeph):
|
2012-09-17 21:39:01 +02:00
|
|
|
zeph['fullname'] = username_to_fullname(zeph['sender'])
|
|
|
|
zeph['shortname'] = zeph['sender'].split('@')[0]
|
2012-09-13 03:41:52 +02:00
|
|
|
|
2012-09-07 16:54:28 +02:00
|
|
|
browser.addheaders.append(('X-CSRFToken', csrf_token))
|
2012-09-11 23:17:10 +02:00
|
|
|
zephyr_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in zeph.items()])
|
2012-09-07 16:54:28 +02:00
|
|
|
browser.open("https://app.humbughq.com/forge_zephyr/", zephyr_data)
|
2012-09-06 22:10:48 +02:00
|
|
|
|
2012-09-13 03:41:52 +02:00
|
|
|
def fetch_fullname(username):
|
|
|
|
try:
|
2012-09-21 21:17:25 +02:00
|
|
|
match_user = re.match(r'([a-zA-Z0-9_]+)@mit\.edu', username)
|
2012-09-13 03:41:52 +02:00
|
|
|
if match_user:
|
|
|
|
proc = subprocess.Popen(['hesinfo', match_user.group(1), 'passwd'], stdout=subprocess.PIPE)
|
|
|
|
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()
|
|
|
|
|
|
|
|
return username.title().replace('@', ' at ').replace('.', ' dot ')
|
|
|
|
|
|
|
|
fullnames = {}
|
|
|
|
def username_to_fullname(username):
|
|
|
|
if username not in fullnames:
|
|
|
|
fullnames[username] = fetch_fullname(username)
|
|
|
|
return fullnames[username]
|
|
|
|
|
2012-09-07 17:45:09 +02:00
|
|
|
|
2012-09-24 20:29:33 +02:00
|
|
|
def process_loop(log):
|
2012-09-13 02:29:26 +02:00
|
|
|
while True:
|
2012-09-07 19:17:56 +02:00
|
|
|
try:
|
2012-09-13 02:29:26 +02:00
|
|
|
notice = zephyr.receive(block=True)
|
|
|
|
zsig, body = notice.message.split("\x00", 1)
|
2012-09-21 22:24:16 +02:00
|
|
|
is_personal = False
|
|
|
|
is_huddle = False
|
|
|
|
|
2012-09-24 20:29:33 +02:00
|
|
|
if zsig.endswith(" "):
|
|
|
|
print "Skipping message from Humbug!"
|
|
|
|
continue
|
|
|
|
|
2012-09-21 22:24:16 +02:00
|
|
|
sender = notice.sender.lower().replace("athena.mit.edu", "mit.edu")
|
|
|
|
recipient = notice.recipient.lower().replace("athena.mit.edu", "mit.edu")
|
2012-09-13 02:29:26 +02:00
|
|
|
|
2012-09-21 21:17:25 +02:00
|
|
|
if (notice.cls == "message" and
|
|
|
|
notice.instance == "personal"):
|
|
|
|
is_personal = True
|
2012-09-21 22:24:16 +02:00
|
|
|
if body.startswith("CC:"):
|
|
|
|
is_huddle = True
|
|
|
|
# Map "CC: sipbtest espuser" => "starnine@mit.edu,espuser@mit.edu"
|
|
|
|
huddle_recipients_list = [x + "@mit.edu" for x in
|
2012-09-24 20:29:33 +02:00
|
|
|
body.split("\n")[0][4:].split()]
|
2012-09-21 22:24:16 +02:00
|
|
|
if sender not in huddle_recipients_list:
|
|
|
|
huddle_recipients_list.append(sender)
|
|
|
|
huddle_recipients = ",".join(huddle_recipients_list)
|
2012-09-21 21:17:25 +02:00
|
|
|
|
|
|
|
if notice.opcode != "":
|
|
|
|
# skip PING messages
|
2012-09-13 02:29:26 +02:00
|
|
|
continue
|
2012-09-21 21:17:25 +02:00
|
|
|
|
|
|
|
# Drop messages not to the listed subscriptions
|
2012-09-21 23:54:23 +02:00
|
|
|
if (notice.cls not in mit_subs_list.all_subs) and not (is_personal and
|
|
|
|
options.forward_personals):
|
2012-09-21 21:17:25 +02:00
|
|
|
print "Skipping ...", notice.cls, notice.instance, is_personal
|
|
|
|
continue
|
|
|
|
|
2012-09-21 22:24:16 +02:00
|
|
|
if is_huddle:
|
|
|
|
zeph = { 'type' : 'personal',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'recipient' : huddle_recipients,
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'new_zephyr': body.split("\n", 1)[1] }
|
|
|
|
elif is_personal:
|
2012-09-21 21:17:25 +02:00
|
|
|
zeph = { 'type' : 'personal',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'recipient' : recipient,
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'new_zephyr': body }
|
|
|
|
else:
|
|
|
|
zeph = { 'type' : 'class',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : sender,
|
|
|
|
'class' : notice.cls,
|
|
|
|
'instance' : notice.instance,
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'new_zephyr': body }
|
2012-09-13 02:29:26 +02:00
|
|
|
|
|
|
|
log.write(simplejson.dumps(zeph) + '\n')
|
|
|
|
log.flush()
|
|
|
|
|
2012-09-21 21:17:25 +02:00
|
|
|
print "received a message on %s/%s from %s..." % \
|
|
|
|
(notice.cls, notice.instance, notice.sender)
|
2012-09-13 02:29:26 +02:00
|
|
|
send_zephyr(zeph)
|
2012-09-07 19:17:56 +02:00
|
|
|
except:
|
2012-09-13 02:29:26 +02:00
|
|
|
print >>sys.stderr, 'Error relaying zephyr'
|
2012-09-07 19:17:56 +02:00
|
|
|
traceback.print_exc()
|
2012-09-13 02:29:26 +02:00
|
|
|
time.sleep(2)
|
2012-09-24 20:29:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def zephyr_to_humbug(options):
|
|
|
|
browser_login()
|
|
|
|
|
|
|
|
import mit_subs_list
|
|
|
|
subs = zephyr.Subscriptions()
|
|
|
|
if options.forward_class_messages:
|
|
|
|
for sub in mit_subs_list.all_subs:
|
|
|
|
subs.add((sub, '*', '*'))
|
|
|
|
if options.forward_personals:
|
|
|
|
subs.add(("message", "personal", "*"))
|
|
|
|
|
|
|
|
if options.resend_log:
|
|
|
|
with open('zephyrs', 'r') as log:
|
|
|
|
try:
|
|
|
|
for ln in log:
|
|
|
|
zeph = simplejson.loads(ln)
|
|
|
|
print "sending saved message to %s from %s..." % \
|
|
|
|
(zeph.get('class', zeph.get('recipient')), zeph['sender'])
|
|
|
|
send_zephyr(zeph)
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, 'Could not send saved zephyr'
|
|
|
|
traceback.print_exc()
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
print "Starting receive loop"
|
|
|
|
|
|
|
|
with open('zephyrs', 'a') as log:
|
|
|
|
process_loop(log)
|
|
|
|
|
|
|
|
def get_zephyrs(last_received):
|
|
|
|
browser.addheaders.append(('X-CSRFToken', csrf_token))
|
|
|
|
submit_hash = {'last_received': last_received,
|
2012-09-24 22:30:11 +02:00
|
|
|
"mit_sync_bot": 'yes'}
|
2012-09-24 20:29:33 +02:00
|
|
|
submit_data = urllib.urlencode([(k, v.encode('utf-8')) for k,v in submit_hash.items()])
|
|
|
|
res = browser.open("https://app.humbughq.com/get_updates_longpoll", submit_data)
|
|
|
|
return simplejson.loads(res.read())['zephyrs']
|
|
|
|
|
|
|
|
def humbug_to_zephyr(options):
|
|
|
|
# Sync messages from zephyr to humbug
|
|
|
|
browser_login()
|
|
|
|
print "Starting get_updates_longpoll."
|
|
|
|
zephyrs = get_zephyrs('0')
|
|
|
|
while True:
|
|
|
|
last_received = str(max([z["id"] for z in zephyrs]))
|
|
|
|
new_zephyrs = get_zephyrs(last_received)
|
|
|
|
for zephyr in new_zephyrs:
|
|
|
|
print zephyr
|
|
|
|
zephyrs.extend(new_zephyrs)
|
|
|
|
|
|
|
|
if options.forward_to_humbug:
|
|
|
|
zephyr_to_humbug(options)
|
|
|
|
else:
|
|
|
|
humbug_to_zephyr(options)
|