From 47323bf2793d6d2aa036c82ce89b80cc1bfc8e54 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 6 Sep 2012 14:45:48 -0400 Subject: [PATCH] Mechanize code to login to our application and forward zephyrs. (imported from commit bb7aa22a67fe2ca9346d1f192c8d1e1aa102ef43) --- zephyr/zephyr-mirror.py | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 zephyr/zephyr-mirror.py diff --git a/zephyr/zephyr-mirror.py b/zephyr/zephyr-mirror.py new file mode 100644 index 0000000000..32f6c098ef --- /dev/null +++ b/zephyr/zephyr-mirror.py @@ -0,0 +1,49 @@ +#!/usr/bin/python +import mechanize +import re +import urllib + +import sys, logging +logger = logging.getLogger("mechanize") +logger.addHandler(logging.StreamHandler(sys.stdout)) +logger.setLevel(logging.INFO) + +def browser_login(br): + br.set_handle_robots(False) + ## debugging code to consider + # br.set_debug_http(True) + # br.set_debug_responses(True) + # br.set_debug_redirects(True) + # br.set_handle_refresh(False) + br.add_password("https://app.humbughq.com/", "tabbott", "xxxxxxxxxxxxxxxxx", "wiki") + login_info = urllib.urlencode([('username', 'iago'), ('password', 'iago')]) + response = br.open("https://app.humbughq.com/") + br.follow_link(text_regex="\s*Log in\s*") + br.select_form(nr=0) + br["username"] = "iago" + br["password"] = "iago" + response2 = br.submit() + # This is a horrible horrible hack + data = "".join(response2.readlines()) + val = data.index("csrfmiddlewaretoken") + csrf = data[val+28:val+60] + return csrf + +# example: send_zephyr("Verona", "Auto2", "test") +def send_zephyr(sender, klass, instance, content): + br = mechanize.Browser() + hack_content = "Message from MIT Zephyr sender %s\n" % (sender,) + content + csrf = browser_login(br) + br.addheaders.append(('X-CSRFToken', csrf)) + zephyr_data = urllib.urlencode([('type', 'class'), ('class', klass), + ('instance', instance), ('new_zephyr', hack_content)]) + br.open("https://app.humbughq.com/zephyr/", zephyr_data) + +import zephyr +subs = zephyr.Subscriptions() +subs.add(('tabbott-test2', '*', '*')) + +while True: + notice = zephyr.receive(block=True) + [zsig, body] = notice.message.split("\x00") + send_zephyr(notice.sender, notice.cls, notice.instance, body)