mirror of https://github.com/zulip/zulip.git
jabber_mirror: Take a JID on the commandline instead of a separate Jabber username and domain
We also take the opportunity to use the sleekxmpp JID parsing more instead of doing string manipulation. (imported from commit 6e4ba0bd2c241666fcde42333ff68b879d8ab2b7)
This commit is contained in:
parent
5f7df38b8a
commit
868b071eb2
|
@ -42,7 +42,7 @@ import logging
|
||||||
import threading
|
import threading
|
||||||
import optparse
|
import optparse
|
||||||
|
|
||||||
from sleekxmpp import ClientXMPP, InvalidJID
|
from sleekxmpp import ClientXMPP, InvalidJID, JID
|
||||||
from sleekxmpp.exceptions import IqError, IqTimeout
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
||||||
from ConfigParser import SafeConfigParser
|
from ConfigParser import SafeConfigParser
|
||||||
import os, sys, zulip, getpass
|
import os, sys, zulip, getpass
|
||||||
|
@ -57,12 +57,13 @@ def stream_to_room(stream):
|
||||||
return stream.lower().rpartition("/xmpp")[0]
|
return stream.lower().rpartition("/xmpp")[0]
|
||||||
|
|
||||||
def jid_to_zulip(jid):
|
def jid_to_zulip(jid):
|
||||||
return "%s@%s" % (str(jid).rpartition("@")[0], options.zulip_domain)
|
return "%s@%s" % (jid.username, options.zulip_domain)
|
||||||
|
|
||||||
class JabberToZulipBot(ClientXMPP):
|
class JabberToZulipBot(ClientXMPP):
|
||||||
def __init__(self, nick, domain, password, rooms, openfire=False):
|
def __init__(self, jid, password, rooms, openfire=False):
|
||||||
self.nick = nick
|
self.nick = jid.username
|
||||||
jid = "%s@%s/jabber_mirror" % (nick, domain)
|
if not jid.resource:
|
||||||
|
jid.resource = "jabber_mirror"
|
||||||
ClientXMPP.__init__(self, jid, password)
|
ClientXMPP.__init__(self, jid, password)
|
||||||
self.password = password
|
self.password = password
|
||||||
self.rooms = set()
|
self.rooms = set()
|
||||||
|
@ -159,11 +160,12 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
if len(subject) == 0:
|
if len(subject) == 0:
|
||||||
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())
|
sender_nick = msg.get_mucnick()
|
||||||
if str(jid) == "@" + options.jabber_domain:
|
if not sender_nick:
|
||||||
# Messages from the room itself have no nickname. We should not try
|
# Messages from the room itself have no nickname. We should not try
|
||||||
# to mirror these
|
# to mirror these
|
||||||
return
|
return
|
||||||
|
jid = self.nickname_to_jid(msg.get_mucroom(), sender_nick)
|
||||||
sender = jid_to_zulip(jid)
|
sender = jid_to_zulip(jid)
|
||||||
zulip_message = dict(
|
zulip_message = dict(
|
||||||
forged = "yes",
|
forged = "yes",
|
||||||
|
@ -180,7 +182,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
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 == ''):
|
||||||
return nick.replace(' ', '') + "@" + options.jabber_domain
|
return JID(local=nick.replace(' ', ''), domain=self.boundjid.domain)
|
||||||
else:
|
else:
|
||||||
return jid
|
return jid
|
||||||
|
|
||||||
|
@ -230,7 +232,7 @@ class ZulipToJabberBot(object):
|
||||||
continue
|
continue
|
||||||
recip_email = recipient['email']
|
recip_email = recipient['email']
|
||||||
username = recip_email[:recip_email.rfind(options.zulip_domain)]
|
username = recip_email[:recip_email.rfind(options.zulip_domain)]
|
||||||
jabber_recipient = username + options.jabber_domain
|
jabber_recipient = username + self.jabber.boundjid.domain
|
||||||
outgoing = self.jabber.make_message(
|
outgoing = self.jabber.make_message(
|
||||||
mto = jabber_recipient,
|
mto = jabber_recipient,
|
||||||
mbody = msg['content'],
|
mbody = msg['content'],
|
||||||
|
@ -298,18 +300,14 @@ user and mirrors messages sent to Jabber rooms to Zulip.'''.replace("\n", " "))
|
||||||
default=logging.INFO)
|
default=logging.INFO)
|
||||||
|
|
||||||
jabber_group = optparse.OptionGroup(parser, "Jabber configuration")
|
jabber_group = optparse.OptionGroup(parser, "Jabber configuration")
|
||||||
jabber_group.add_option('--jabber-username',
|
jabber_group.add_option('--jid',
|
||||||
default=None,
|
default=None,
|
||||||
action='store',
|
action='store',
|
||||||
help="Your Jabber username")
|
help="Your Jabber JID")
|
||||||
jabber_group.add_option('--jabber-password',
|
jabber_group.add_option('--jabber-password',
|
||||||
default=None,
|
default=None,
|
||||||
action='store',
|
action='store',
|
||||||
help="Your Jabber password")
|
help="Your Jabber password")
|
||||||
jabber_group.add_option('--jabber-domain',
|
|
||||||
default=None,
|
|
||||||
action='store',
|
|
||||||
help="Your Jabber server")
|
|
||||||
jabber_group.add_option('--conference-domain',
|
jabber_group.add_option('--conference-domain',
|
||||||
default=None,
|
default=None,
|
||||||
action='store',
|
action='store',
|
||||||
|
@ -341,8 +339,7 @@ user and mirrors messages sent to Jabber rooms to Zulip.'''.replace("\n", " "))
|
||||||
config.readfp(f, config_file)
|
config.readfp(f, config_file)
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
for option in ("jabber_username", "jabber_password", "jabber_domain",
|
for option in ("jid", "jabber_password", "conference_domain"):
|
||||||
"conference_domain"):
|
|
||||||
if (getattr(options, option) is None
|
if (getattr(options, option) is None
|
||||||
and config.has_option("jabber_mirror", option)):
|
and config.has_option("jabber_mirror", option)):
|
||||||
setattr(options, option, config.get("jabber_mirror", option))
|
setattr(options, option, config.get("jabber_mirror", option))
|
||||||
|
@ -360,18 +357,19 @@ user and mirrors messages sent to Jabber rooms to Zulip.'''.replace("\n", " "))
|
||||||
if options.mode == 'public' and options.conference_domain is None:
|
if options.mode == 'public' and options.conference_domain is None:
|
||||||
sys.exit("--conference-domain is required when running in 'public' mode")
|
sys.exit("--conference-domain is required when running in 'public' mode")
|
||||||
|
|
||||||
if None in (options.jabber_username, options.jabber_password,
|
if None in (options.jid, options.jabber_password):
|
||||||
options.jabber_domain):
|
sys.exit("You must specify your Jabber JID and Jabber password either "
|
||||||
sys.exit("You must specify your Jabber username, Jabber password, and "
|
+ "in the Zulip configuration file or on the commandline")
|
||||||
+ "Jabber domain either in the Zulip configuration file or on "
|
|
||||||
+ "the commandline")
|
|
||||||
|
|
||||||
# This won't work for open realms
|
# This won't work for open realms
|
||||||
options.zulip_domain = options.zulip_email.partition('@')[-1]
|
options.zulip_domain = options.zulip_email.partition('@')[-1]
|
||||||
|
|
||||||
zulip = ZulipToJabberBot(zulip.init_from_options(options, "JabberMirror/" + __version__))
|
zulip = ZulipToJabberBot(zulip.init_from_options(options, "JabberMirror/" + __version__))
|
||||||
xmpp = JabberToZulipBot(options.jabber_username, options.jabber_domain,
|
try:
|
||||||
options.jabber_password, get_rooms(zulip),
|
jid = JID(options.jid)
|
||||||
|
except InvalidJID as e:
|
||||||
|
sys.exit("Bad JID: %s: %s" % (options.jid, e.message))
|
||||||
|
xmpp = JabberToZulipBot(jid, options.jabber_password, get_rooms(zulip),
|
||||||
openfire=options.openfire)
|
openfire=options.openfire)
|
||||||
|
|
||||||
if not xmpp.connect(use_tls=not options.no_use_tls):
|
if not xmpp.connect(use_tls=not options.no_use_tls):
|
||||||
|
|
Loading…
Reference in New Issue