jabber_mirror: Factor out jid_to_zulip and use str.rpartition

(imported from commit 435f076d7125676b5b964c73ec1e3753496c32af)
This commit is contained in:
Zev Benjamin 2014-02-21 13:30:31 -05:00
parent 36f8957b36
commit e7a1d3b0d7

View file

@ -33,7 +33,10 @@ import os, sys, zulip, getpass
import re import re
def room_to_stream(room): def room_to_stream(room):
return str(room).split("@")[0] return str(room).rpartition("@")[0]
def jid_to_zulip(jid):
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, password, rooms, openfire=False):
@ -83,8 +86,8 @@ class JabberToZulipBot(ClientXMPP):
def private(self, msg): def private(self, msg):
if msg["from"] == self.jid or msg['thread'] == u'\u1B80': if msg["from"] == self.jid or msg['thread'] == u'\u1B80':
return return
sender = self.jid_to_zulip(msg["from"]) sender = jid_to_zulip(msg["from"])
recipient = self.jid_to_zulip(msg["to"]) recipient = jid_to_zulip(msg["to"])
zulip_message = dict( zulip_message = dict(
sender = sender, sender = sender,
@ -105,7 +108,7 @@ class JabberToZulipBot(ClientXMPP):
subject = "(no topic)" subject = "(no topic)"
stream = room_to_stream(msg.get_mucroom()) stream = room_to_stream(msg.get_mucroom())
jid = self.nickname_to_jid(msg.get_mucroom(), msg.get_mucnick()) jid = self.nickname_to_jid(msg.get_mucroom(), msg.get_mucnick())
sender = self.jid_to_zulip(jid) sender = jid_to_zulip(jid)
zulip_message = dict( zulip_message = dict(
forged = "yes", forged = "yes",
sender = sender, sender = sender,
@ -118,9 +121,6 @@ class JabberToZulipBot(ClientXMPP):
if ret.get("status") != "success": if ret.get("status") != "success":
logging.error(ret) logging.error(ret)
def jid_to_zulip(self, jid):
return "%s@%s" % (str(jid).split("@")[0], options.zulip_domain)
def nickname_to_jid(self, room, nick): def nickname_to_jid(self, room, nick):
jid = self.plugin['xep_0045'].getJidProperty(room, nick, "jid") jid = self.plugin['xep_0045'].getJidProperty(room, nick, "jid")
if (jid is None or jid == ''): if (jid is None or jid == ''):