jabber_mirror: Working room/stream mirroring

(imported from commit 5d0a33423e90edaee3a1094b1d48fae73cfb54ca)
This commit is contained in:
Zev Benjamin 2014-02-28 12:51:07 -05:00
parent f1e3d9ab76
commit 2185d42a90

View file

@ -39,9 +39,9 @@ def jid_to_zulip(jid):
return "%s@%s" % (str(jid).rpartition("@")[0], options.zulip_domain) return "%s@%s" % (str(jid).rpartition("@")[0], options.zulip_domain)
class JabberToZulipBot(ClientXMPP): class JabberToZulipBot(ClientXMPP):
def __init__(self, nick, password, rooms, openfire=False): def __init__(self, nick, domain, password, rooms, openfire=False):
self.nick = nick self.nick = nick
jid = "%s/zulip" % (nick,) jid = "%s@%s/jabber_mirror" % (nick, domain)
ClientXMPP.__init__(self, jid, password) ClientXMPP.__init__(self, jid, password)
self.password = password self.password = password
self.rooms = rooms self.rooms = rooms
@ -51,7 +51,6 @@ class JabberToZulipBot(ClientXMPP):
self.zulip = None self.zulip = None
self.use_ipv6 = False self.use_ipv6 = False
if options.conference_domain is not None:
# Jabber chatroom support. # Jabber chatroom support.
self.register_plugin('xep_0045') self.register_plugin('xep_0045')
@ -66,10 +65,9 @@ class JabberToZulipBot(ClientXMPP):
def session_start(self, event): def session_start(self, event):
self.get_roster() self.get_roster()
self.send_presence() self.send_presence()
if options.mode == "public":
for room in self.rooms: for room in self.rooms:
self.plugin['xep_0045'].joinMUC(room + "@" + options.conference_domain, muc_jid = room + "@" + options.conference_domain
self.nick) self.plugin['xep_0045'].joinMUC(muc_jid, self.nick)
def message(self, msg): def message(self, msg):
try: try:
@ -84,7 +82,7 @@ class JabberToZulipBot(ClientXMPP):
logging.exception("Error forwarding Jabber => Zulip") logging.exception("Error forwarding Jabber => Zulip")
def private(self, msg): def private(self, msg):
if msg["from"] == self.jid or msg['thread'] == u'\u1B80': if options.mode == 'personal' or msg['thread'] == u'\u1B80':
return return
sender = jid_to_zulip(msg["from"]) sender = jid_to_zulip(msg["from"])
recipient = jid_to_zulip(msg["to"]) recipient = jid_to_zulip(msg["to"])
@ -100,7 +98,7 @@ class JabberToZulipBot(ClientXMPP):
logging.error(ret) logging.error(ret)
def group(self, msg): def group(self, msg):
if msg.get_mucnick() == self.nick or msg["thread"] == u'\u1B80': if options.mode == 'personal' or msg["thread"] == u'\u1B80':
return return
subject = msg["subject"] subject = msg["subject"]
@ -229,16 +227,23 @@ user and mirrors messages sent to Jabber rooms to Zulip.'''.replace("\n", " "))
if options.jabber_domain is None: if options.jabber_domain is None:
sys.exit("Must specify a Jabber server") sys.exit("Must specify a Jabber server")
jabber_username = options.jabber_username + '@' + options.jabber_domain
# This won't work for open realms
options.zulip_domain = options.zulip_email.partition('@')[-1]
zulip = ZulipToJabberBot(zulip.init_from_options(options, "jabber_mirror")) zulip = ZulipToJabberBot(zulip.init_from_options(options, "jabber_mirror"))
rooms = [s['name'] for s in zulip.client.get_streams()['streams']] rooms = [s['name'] for s in zulip.client.get_streams()['streams']]
xmpp = JabberToZulipBot(jabber_username, options.jabber_password, rooms, xmpp = JabberToZulipBot(options.jabber_username, options.jabber_domain,
options.jabber_password, rooms,
openfire=options.openfire) openfire=options.openfire)
xmpp.connect(use_tls=not options.no_use_tls) xmpp.connect(use_tls=not options.no_use_tls)
xmpp.process(block=False)
xmpp.set_zulip_client(zulip) xmpp.set_zulip_client(zulip)
zulip.set_jabber_client(xmpp) zulip.set_jabber_client(xmpp)
if options.mode == 'public':
xmpp.process(block=True)
else:
xmpp.process(block=False)
try: try:
logging.info("Connecting to Zulip.") logging.info("Connecting to Zulip.")
zulip.client.call_on_each_event(zulip.process_message) zulip.client.call_on_each_event(zulip.process_message)