def run_zephyr(): zephyr.init() subs = zephyr.Subscriptions() logging.info("Subscribing to: " + ','.join(zephyr_classes.keys())) for c in zephyr_classes.keys(): subs.add((c, '*', '')) while not SHUTDOWN: while True: try: m = from_jabber_q.get(False) except Queue.Empty: break (src, sender, msg) = m if src not in jabber_chats: continue note = zephyr.ZNotice() note.fields = [src, msg] note.sender = sender note.auth = False note.cls = jabber_chats[src] note.instance = '' note.opcode = 'jabber' note.send() note = zephyr.receive(False) if note: body = note.fields[1] if len(note.fields) > 1 else '' logging.debug("ZEPHYR: %s/%s[%s]: %s", note.sender, note.cls, note.opcode, body) if note.opcode.lower() not in ('jabber', 'ping'): from_zephyr_q.put((note.cls, note.sender.split('@')[0], body)) else: select.select([zephyr._z.getFD()], [], [], 1)
def build_reply(zgram): z = zephyr.ZNotice() z.cls = zgram.cls z.instance = zgram.instance z.recipient = zgram.sender z.opcode = 'auto' z.fields = ['zscore bot: http://zscore.mit.edu/', ''] return z
def _prep_zgram(self): zgram = self._zgram z = zephyr.ZNotice() z.cls = zgram.cls z.instance = zgram.instance #z.format = "http://zephyr.1ts.org/wiki/df" # The following default format will cause messages not to be mirrored to MIT Zulip. #z.format = "Zephyr error: See http://zephyr.1ts.org/wiki/df" z.opcode = 'auto' return z
def post(self, *args, **kwargs): class_name = self.get_argument('class', 'message').encode("utf-8") instance = self.get_argument('instance', 'personal').encode("utf-8") recipient = self.get_argument('recipient', '').encode("utf-8") signature = self.get_argument('signature', None) message = self.get_argument('message', '').encode("utf-8") username = self.current_user.username if signature is not None: signature += ") (" else: signature = "" signature += django.conf.settings.SIGNATURE or "" signature = signature.encode("utf-8") log("Send " + class_name + " " + instance + " " + recipient + " " + username.encode("utf-8") + " " + message) zephyr.ZNotice(cls=class_name, instance=instance, recipient=recipient, message=signature + '\x00' + message + '\n', sender=username if '@' in username else username + '@ATHENA.MIT.EDU', format='http://zephyr.1ts.org/wiki/df').send()
def send(self, mess, name=None): """ Sends a zephyr to the specified cls, addressing a specific person if specified. """ if name != None: mess = name + ": " + mess mess = mess.decode("utf-8") # decode the message mess = custom_fill(mess) # try to send the message try: z = zephyr.ZNotice(cls=self.cls, fields=["", mess], sender='*****@*****.**') foo = str(z.__dict__) z.send() except KeyboardInterrupt: "Dodona is no longer running." raise except: print traceback.format_exc()
def insertZephyr(self, zMsg): # Check that the msg is an actual message and not other types. if zMsg.kind > 2: # Only allow UNSAFE, UNACKED, ACKED messages return # Create a valid destination field for our zephyr class_name = unicode(zMsg.cls.lower(), 'utf-8') instance = unicode(zMsg.instance.lower(), 'utf-8') recipient = unicode(zMsg.recipient.lower(), 'utf-8') if recipient == '': recipient = '*' s = Subscription.objects.get_or_create(class_name=class_name, instance=instance, recipient=recipient)[0] # Sender + Signature Processing athena = '@ATHENA.MIT.EDU' if (len(zMsg.sender) >= len(athena)) and (zMsg.sender[-len(athena):] == athena): sender = zMsg.sender[:-len(athena)] else: sender = zMsg.sender while len(zMsg.fields) < 2: zMsg.fields = [''] + zMsg.fields signature = zMsg.fields[0] #if sender == "daemon/zephyrplus.xvm.mit.edu": #sender = signature.split(" ")[0] #if signature.find("(") != -1: #signature = signature[signature.find("(")+1:signature.rfind(")")] #else: #signature = "" if django.conf.settings.SIGNATURE is not None: signature = signature.replace( ") (%s" % django.conf.settings.SIGNATURE, "").replace(django.conf.settings.SIGNATURE, "") # Authentication check if not zMsg.auth and zMsg.uid.address != self.ip: sender += " (UNAUTH)" if django.conf.settings.SIGNATURE is not None and django.conf.settings.SIGNATURE in zMsg.fields[ 0].lower(): zephyr.ZNotice( cls=zMsg.cls, instance=zMsg.instance, recipient=zMsg.recipient, opcode='AUTO', message="ZephyrPlus Server\x00" + "The previous zephyr,\n\n" + zMsg.fields[1].strip() + "\n\nwas FORGED (not sent from ZephyrPlus).\n").send() # Convert to unicode msg = unicode(zMsg.fields[1].rstrip(), 'utf-8') sender = unicode(sender, 'utf-8') signature = unicode(signature, 'utf-8') # Database insert z = Zephyr(message=msg, sender=sender, date=datetime.datetime.now(), dst=s, signature=signature) z.save() #pdb.set_trace() logMsg = u"Zephyr(%d): %s %s %s %s" % (z.id, unicode(s), sender, msg, signature) self.log(logMsg) # Tell server to update z_id = z.id #sys.stdout.write(str(z_id)) #sys.stdout.flush() try: h = httplib.HTTPConnection('localhost:8888') h.request('GET', '/update?id=' + str(z_id)) r = h.getresponse() #print(r.status, r.reason) except: print("Could not notify tornado server of new zephyr.") # Clean up our database queries db.reset_queries()