2012-09-06 20:45:48 +02:00
|
|
|
#!/usr/bin/python
|
2012-09-13 02:29:26 +02:00
|
|
|
import mechanize
|
|
|
|
import urllib
|
|
|
|
import cgi
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import zephyr
|
|
|
|
import BeautifulSoup
|
|
|
|
import traceback
|
|
|
|
import simplejson
|
|
|
|
import re
|
|
|
|
import time
|
2012-09-13 03:41:52 +02:00
|
|
|
import subprocess
|
2012-09-13 02:29:26 +02:00
|
|
|
|
|
|
|
from mit_subs_list import subs_list
|
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)
|
|
|
|
browser["username"] = "iago"
|
|
|
|
browser["password"] = "iago"
|
|
|
|
|
|
|
|
global csrf_token
|
2012-09-07 17:14:26 +02:00
|
|
|
soup = BeautifulSoup.BeautifulSoup(browser.submit().read())
|
|
|
|
csrf_token = soup.find('input', attrs={'name': 'csrfmiddlewaretoken'})['value']
|
2012-09-06 20:45:48 +02:00
|
|
|
|
2012-09-07 19:13:00 +02:00
|
|
|
def send_zephyr(zeph):
|
2012-09-13 03:41:52 +02:00
|
|
|
zeph['fullname'] = cgi.escape(username_to_fullname(zeph['sender']))
|
|
|
|
zeph['shortname'] = cgi.escape(zeph['sender'].split('@')[0])
|
|
|
|
|
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-11 19:25:37 +02:00
|
|
|
def unwrap_lines(body):
|
|
|
|
return '\n'.join(p.replace('\n', ' ') for p in re.split(r'\n[ \t\n]', body))
|
|
|
|
|
2012-09-13 03:41:52 +02:00
|
|
|
def fetch_fullname(username):
|
|
|
|
try:
|
|
|
|
match_user = re.match(r'([a-zA-Z0-9_]+)@ATHENA\.MIT\.EDU', username)
|
|
|
|
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-13 02:29:26 +02:00
|
|
|
browser_login()
|
2012-09-07 17:45:09 +02:00
|
|
|
|
2012-09-13 02:29:26 +02:00
|
|
|
subs = zephyr.Subscriptions()
|
|
|
|
for sub in subs_list:
|
|
|
|
subs.add((sub, '*', '*'))
|
2012-09-07 17:45:09 +02:00
|
|
|
|
2012-09-13 02:29:26 +02:00
|
|
|
if sys.argv[1:] == ['--resend-log']:
|
|
|
|
try:
|
|
|
|
with open('zephyrs', 'r') as log:
|
|
|
|
for ln in log:
|
|
|
|
zeph = simplejson.loads(ln)
|
|
|
|
print "sending saved message to %s from %s..." % (zeph['class'], zeph['sender'])
|
|
|
|
send_zephyr(zeph)
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, 'Could not load zephyr log'
|
|
|
|
traceback.print_exc()
|
2012-09-07 17:45:09 +02:00
|
|
|
|
2012-09-13 02:29:26 +02:00
|
|
|
with open('zephyrs', 'a') as log:
|
|
|
|
print "Starting receive loop"
|
|
|
|
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)
|
|
|
|
|
|
|
|
if notice.cls not in subs_list:
|
|
|
|
continue
|
|
|
|
zeph = { 'type' : 'class',
|
|
|
|
'time' : str(notice.time),
|
|
|
|
'sender' : notice.sender,
|
|
|
|
'class' : notice.cls,
|
|
|
|
'instance' : notice.instance,
|
|
|
|
'zsig' : zsig, # logged here but not used by app
|
|
|
|
'new_zephyr': unwrap_lines(body) }
|
|
|
|
for k,v in zeph.items():
|
|
|
|
zeph[k] = cgi.escape(v)
|
|
|
|
|
|
|
|
log.write(simplejson.dumps(zeph) + '\n')
|
|
|
|
log.flush()
|
|
|
|
|
|
|
|
print "received a message on %s from %s..." % (zeph['class'], zeph['sender'])
|
|
|
|
send_zephyr(zeph)
|
|
|
|
print "forwarded"
|
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)
|