zephyr_mirror: Get new classes to subscribe to from the humbug server.

(imported from commit 7cf321cdc97cf10719ca89b4ee1e7c2e8d979e40)
This commit is contained in:
Tim Abbott 2012-10-11 14:39:52 -04:00
parent 468a3cbe98
commit 6aac490dc8

View file

@ -57,6 +57,7 @@ humbug_client = api.common.HumbugAPI(email=os.environ["USER"] + "@mit.edu",
import zephyr import zephyr
zephyr.init() zephyr.init()
subs = zephyr.Subscriptions()
def compute_humbug_username(zephyr_username): def compute_humbug_username(zephyr_username):
return zephyr_username.lower().split("@")[0] + "@mit.edu" return zephyr_username.lower().split("@")[0] + "@mit.edu"
@ -97,12 +98,39 @@ def username_to_fullname(username):
fullnames[username] = fetch_fullname(username) fullnames[username] = fetch_fullname(username)
return fullnames[username] return fullnames[username]
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)
def process_loop(log): def process_loop(log):
import mit_subs_list sleep_count = 0
sleep_time = 0.1
while True: while True:
notice = zephyr.receive(block=False)
if notice is None and options.forward_class_messages:
# Ask the Humbug server about any new classes to subscribe to
time.sleep(sleep_time)
sleep_count += sleep_time
if sleep_count > 15:
sleep_count = 0
update_subscriptions_from_humbug()
continue
try: try:
notice = zephyr.receive(block=True)
zsig, body = notice.message.split("\x00", 1) zsig, body = notice.message.split("\x00", 1)
is_personal = False is_personal = False
is_huddle = False is_huddle = False
@ -133,7 +161,7 @@ def process_loop(log):
huddle_recipients = ",".join(huddle_recipients_list) huddle_recipients = ",".join(huddle_recipients_list)
# Drop messages not to the listed subscriptions # Drop messages not to the listed subscriptions
if (notice.cls.lower() not in mit_subs_list.all_subs) and not \ if (notice.cls.lower() not in current_zephyr_subs) and not \
(is_personal and options.forward_personals): (is_personal and options.forward_personals):
print "Skipping ...", notice.cls, notice.instance, is_personal print "Skipping ...", notice.cls, notice.instance, is_personal
continue continue
@ -179,10 +207,10 @@ def process_loop(log):
def zephyr_to_humbug(options): def zephyr_to_humbug(options):
import mit_subs_list import mit_subs_list
subs = zephyr.Subscriptions()
if options.forward_class_messages: if options.forward_class_messages:
for sub in mit_subs_list.all_subs: for sub in mit_subs_list.all_subs:
subs.add((sub, '*', '*')) ensure_subscribed(sub)
update_subscriptions_from_humbug()
if options.forward_personals: if options.forward_personals:
subs.add(("message", "personal", "*")) subs.add(("message", "personal", "*"))