2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2014-03-17 13:17:20 -04:00
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Permabit, Inc.
|
|
|
|
# Copyright (C) 2013--2014 Zulip, Inc.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation files
|
|
|
|
# (the "Software"), to deal in the Software without restriction,
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
# The following is a table showing which kinds of messages are handled by the
|
|
|
|
# mirror in each mode:
|
|
|
|
#
|
|
|
|
# Message origin/type --> | Jabber | Zulip
|
|
|
|
# Mode/sender-, +-----+----+--------+----
|
|
|
|
# V | MUC | PM | stream | PM
|
|
|
|
# --------------+-------------+-----+----+--------+----
|
|
|
|
# | other sender| | x | |
|
|
|
|
# personal mode +-------------+-----+----+--------+----
|
|
|
|
# | self sender | | x | x | x
|
|
|
|
# ------------- +-------------+-----+----+--------+----
|
|
|
|
# | other sender| x | | |
|
|
|
|
# public mode +-------------+-----+----+--------+----
|
|
|
|
# | self sender | | | |
|
2019-11-06 09:31:30 -05:00
|
|
|
from typing import Dict, List, Set, Optional
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
from sleekxmpp import ClientXMPP, InvalidJID, JID
|
2016-12-04 14:55:41 -05:00
|
|
|
from sleekxmpp.stanza import Message as JabberMessage
|
2014-03-17 13:17:20 -04:00
|
|
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
2020-04-02 11:11:08 -04:00
|
|
|
from configparser import SafeConfigParser
|
2016-12-28 00:39:25 -05:00
|
|
|
import getpass
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zulip
|
|
|
|
from zulip import Client
|
2014-03-17 13:17:20 -04:00
|
|
|
import re
|
2016-12-04 14:55:41 -05:00
|
|
|
from typing import Any, Callable
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
__version__ = "1.1"
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def room_to_stream(room: str) -> str:
|
2014-03-17 13:17:20 -04:00
|
|
|
return room + "/xmpp"
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def stream_to_room(stream: str) -> str:
|
2014-03-17 13:17:20 -04:00
|
|
|
return stream.lower().rpartition("/xmpp")[0]
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def jid_to_zulip(jid: JID) -> str:
|
2014-03-17 13:17:20 -04:00
|
|
|
suffix = ''
|
|
|
|
if not jid.username.endswith("-bot"):
|
|
|
|
suffix = options.zulip_email_suffix
|
|
|
|
return "%s%s@%s" % (jid.username, suffix, options.zulip_domain)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def zulip_to_jid(email: str, jabber_domain: str) -> JID:
|
2014-03-17 13:17:20 -04:00
|
|
|
jid = JID(email, domain=jabber_domain)
|
2017-01-23 23:50:04 -05:00
|
|
|
if (options.zulip_email_suffix and
|
|
|
|
options.zulip_email_suffix in jid.username and
|
|
|
|
not jid.username.endswith("-bot")):
|
2014-03-17 13:17:20 -04:00
|
|
|
jid.username = jid.username.rpartition(options.zulip_email_suffix)[0]
|
|
|
|
return jid
|
|
|
|
|
|
|
|
class JabberToZulipBot(ClientXMPP):
|
2020-04-18 18:59:12 -04:00
|
|
|
def __init__(self, jid: JID, password: str, rooms: List[str]) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
if jid.resource:
|
|
|
|
self.nick = jid.resource
|
|
|
|
else:
|
|
|
|
self.nick = jid.username
|
|
|
|
jid.resource = "zulip"
|
|
|
|
ClientXMPP.__init__(self, jid, password)
|
2017-05-07 10:44:29 -04:00
|
|
|
self.rooms = set() # type: Set[str]
|
2014-03-17 13:17:20 -04:00
|
|
|
self.rooms_to_join = rooms
|
|
|
|
self.add_event_handler("session_start", self.session_start)
|
|
|
|
self.add_event_handler("message", self.message)
|
2018-01-08 10:15:55 -05:00
|
|
|
self.zulip = None
|
2014-03-17 13:17:20 -04:00
|
|
|
self.use_ipv6 = False
|
|
|
|
|
2017-05-07 10:44:29 -04:00
|
|
|
self.register_plugin('xep_0045') # Jabber chatrooms
|
|
|
|
self.register_plugin('xep_0199') # XMPP Ping
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def set_zulip_client(self, zulipToJabberClient: 'ZulipToJabberBot') -> None:
|
2016-12-28 00:39:25 -05:00
|
|
|
self.zulipToJabber = zulipToJabberClient
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def session_start(self, event: Dict[str, Any]) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
self.get_roster()
|
|
|
|
self.send_presence()
|
|
|
|
for room in self.rooms_to_join:
|
|
|
|
self.join_muc(room)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def join_muc(self, room: str) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
if room in self.rooms:
|
|
|
|
return
|
|
|
|
logging.debug("Joining " + room)
|
|
|
|
self.rooms.add(room)
|
|
|
|
muc_jid = JID(local=room, domain=options.conference_domain)
|
|
|
|
xep0045 = self.plugin['xep_0045']
|
|
|
|
try:
|
|
|
|
xep0045.joinMUC(muc_jid, self.nick, wait=True)
|
|
|
|
except InvalidJID:
|
|
|
|
logging.error("Could not join room: " + str(muc_jid))
|
|
|
|
return
|
|
|
|
|
|
|
|
# Configure the room. Really, we should only do this if the room is
|
|
|
|
# newly created.
|
|
|
|
form = None
|
|
|
|
try:
|
|
|
|
form = xep0045.getRoomConfig(muc_jid)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if form:
|
|
|
|
xep0045.configureRoom(muc_jid, form)
|
|
|
|
else:
|
|
|
|
logging.error("Could not configure room: " + str(muc_jid))
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def leave_muc(self, room: str) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
if room not in self.rooms:
|
|
|
|
return
|
|
|
|
logging.debug("Leaving " + room)
|
|
|
|
self.rooms.remove(room)
|
|
|
|
muc_jid = JID(local=room, domain=options.conference_domain)
|
|
|
|
self.plugin['xep_0045'].leaveMUC(muc_jid, self.nick)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def message(self, msg: JabberMessage) -> Any:
|
2014-03-17 13:17:20 -04:00
|
|
|
try:
|
|
|
|
if msg["type"] == "groupchat":
|
|
|
|
return self.group(msg)
|
|
|
|
elif msg["type"] == "chat":
|
|
|
|
return self.private(msg)
|
|
|
|
else:
|
|
|
|
logging.warning("Got unexpected message type")
|
|
|
|
logging.warning(msg)
|
|
|
|
except Exception:
|
|
|
|
logging.exception("Error forwarding Jabber => Zulip")
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def private(self, msg: JabberMessage) -> None:
|
2020-04-09 20:14:01 -04:00
|
|
|
if options.mode == 'public' or msg['thread'] == '\u1FFFE':
|
2014-03-17 13:17:20 -04:00
|
|
|
return
|
|
|
|
sender = jid_to_zulip(msg["from"])
|
|
|
|
recipient = jid_to_zulip(msg["to"])
|
|
|
|
|
|
|
|
zulip_message = dict(
|
|
|
|
sender = sender,
|
|
|
|
type = "private",
|
|
|
|
to = recipient,
|
|
|
|
content = msg["body"],
|
2017-01-24 00:34:26 -05:00
|
|
|
)
|
2016-12-28 00:39:25 -05:00
|
|
|
ret = self.zulipToJabber.client.send_message(zulip_message)
|
2014-03-17 13:17:20 -04:00
|
|
|
if ret.get("result") != "success":
|
2016-12-28 00:39:25 -05:00
|
|
|
logging.error(str(ret))
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def group(self, msg: JabberMessage) -> None:
|
2020-04-09 20:14:01 -04:00
|
|
|
if options.mode == 'personal' or msg["thread"] == '\u1FFFE':
|
2014-03-17 13:17:20 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
subject = msg["subject"]
|
|
|
|
if len(subject) == 0:
|
|
|
|
subject = "(no topic)"
|
|
|
|
stream = room_to_stream(msg['from'].local)
|
|
|
|
sender_nick = msg.get_mucnick()
|
|
|
|
if not sender_nick:
|
|
|
|
# Messages from the room itself have no nickname. We should not try
|
|
|
|
# to mirror these
|
|
|
|
return
|
|
|
|
jid = self.nickname_to_jid(msg.get_mucroom(), sender_nick)
|
|
|
|
sender = jid_to_zulip(jid)
|
|
|
|
zulip_message = dict(
|
|
|
|
forged = "yes",
|
|
|
|
sender = sender,
|
|
|
|
type = "stream",
|
|
|
|
subject = subject,
|
|
|
|
to = stream,
|
|
|
|
content = msg["body"],
|
2017-01-24 00:34:26 -05:00
|
|
|
)
|
2016-12-28 00:39:25 -05:00
|
|
|
ret = self.zulipToJabber.client.send_message(zulip_message)
|
2014-03-17 13:17:20 -04:00
|
|
|
if ret.get("result") != "success":
|
2016-12-28 00:39:25 -05:00
|
|
|
logging.error(str(ret))
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def nickname_to_jid(self, room: str, nick: str) -> JID:
|
2014-03-17 13:17:20 -04:00
|
|
|
jid = self.plugin['xep_0045'].getJidProperty(room, nick, "jid")
|
|
|
|
if (jid is None or jid == ''):
|
|
|
|
return JID(local=nick.replace(' ', ''), domain=self.boundjid.domain)
|
|
|
|
else:
|
|
|
|
return jid
|
|
|
|
|
2020-04-09 20:14:01 -04:00
|
|
|
class ZulipToJabberBot:
|
2020-04-18 18:59:12 -04:00
|
|
|
def __init__(self, zulip_client: Client) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
self.client = zulip_client
|
2019-11-06 09:31:30 -05:00
|
|
|
self.jabber = None # type: Optional[JabberToZulipBot]
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def set_jabber_client(self, client: JabberToZulipBot) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
self.jabber = client
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def process_event(self, event: Dict[str, Any]) -> None:
|
2014-03-17 13:17:20 -04:00
|
|
|
if event['type'] == 'message':
|
|
|
|
message = event["message"]
|
|
|
|
if message['sender_email'] != self.client.email:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
if message['type'] == 'stream':
|
|
|
|
self.stream_message(message)
|
|
|
|
elif message['type'] == 'private':
|
|
|
|
self.private_message(message)
|
2017-03-05 04:25:27 -05:00
|
|
|
except Exception:
|
2014-03-17 13:17:20 -04:00
|
|
|
logging.exception("Exception forwarding Zulip => Jabber")
|
|
|
|
elif event['type'] == 'subscription':
|
|
|
|
self.process_subscription(event)
|
|
|
|
elif event['type'] == 'stream':
|
|
|
|
self.process_stream(event)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def stream_message(self, msg: Dict[str, str]) -> None:
|
2018-01-08 10:15:55 -05:00
|
|
|
assert(self.jabber is not None)
|
2014-03-17 13:17:20 -04:00
|
|
|
stream = msg['display_recipient']
|
|
|
|
if not stream.endswith("/xmpp"):
|
|
|
|
return
|
|
|
|
|
|
|
|
room = stream_to_room(stream)
|
|
|
|
jabber_recipient = JID(local=room, domain=options.conference_domain)
|
|
|
|
outgoing = self.jabber.make_message(
|
|
|
|
mto = jabber_recipient,
|
|
|
|
mbody = msg['content'],
|
|
|
|
mtype = 'groupchat')
|
2020-04-09 20:14:01 -04:00
|
|
|
outgoing['thread'] = '\u1FFFE'
|
2014-03-17 13:17:20 -04:00
|
|
|
outgoing.send()
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def private_message(self, msg: Dict[str, Any]) -> None:
|
2018-01-08 10:15:55 -05:00
|
|
|
assert(self.jabber is not None)
|
2014-03-17 13:17:20 -04:00
|
|
|
for recipient in msg['display_recipient']:
|
|
|
|
if recipient["email"] == self.client.email:
|
|
|
|
continue
|
2014-07-17 18:18:06 -04:00
|
|
|
if not recipient["is_mirror_dummy"]:
|
|
|
|
continue
|
2014-03-17 13:17:20 -04:00
|
|
|
recip_email = recipient['email']
|
|
|
|
jabber_recipient = zulip_to_jid(recip_email, self.jabber.boundjid.domain)
|
|
|
|
outgoing = self.jabber.make_message(
|
|
|
|
mto = jabber_recipient,
|
|
|
|
mbody = msg['content'],
|
|
|
|
mtype = 'chat')
|
2020-04-09 20:14:01 -04:00
|
|
|
outgoing['thread'] = '\u1FFFE'
|
2014-03-17 13:17:20 -04:00
|
|
|
outgoing.send()
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def process_subscription(self, event: Dict[str, Any]) -> None:
|
2018-01-08 10:15:55 -05:00
|
|
|
assert(self.jabber is not None)
|
2014-03-17 13:17:20 -04:00
|
|
|
if event['op'] == 'add':
|
|
|
|
streams = [s['name'].lower() for s in event['subscriptions']]
|
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.join_muc(stream_to_room(stream))
|
|
|
|
if event['op'] == 'remove':
|
|
|
|
streams = [s['name'].lower() for s in event['subscriptions']]
|
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.leave_muc(stream_to_room(stream))
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def process_stream(self, event: Dict[str, Any]) -> None:
|
2018-01-08 10:15:55 -05:00
|
|
|
assert(self.jabber is not None)
|
2014-03-17 13:17:20 -04:00
|
|
|
if event['op'] == 'occupy':
|
|
|
|
streams = [s['name'].lower() for s in event['streams']]
|
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.join_muc(stream_to_room(stream))
|
|
|
|
if event['op'] == 'vacate':
|
|
|
|
streams = [s['name'].lower() for s in event['streams']]
|
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.leave_muc(stream_to_room(stream))
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def get_rooms(zulipToJabber: ZulipToJabberBot) -> List[str]:
|
|
|
|
def get_stream_infos(key: str, method: Callable[[], Dict[str, Any]]) -> Any:
|
2014-03-17 13:17:20 -04:00
|
|
|
ret = method()
|
|
|
|
if ret.get("result") != "success":
|
2017-12-11 16:30:47 -05:00
|
|
|
logging.error(str(ret))
|
2014-03-17 13:17:20 -04:00
|
|
|
sys.exit("Could not get initial list of Zulip %s" % (key,))
|
|
|
|
return ret[key]
|
|
|
|
|
|
|
|
if options.mode == 'public':
|
2016-12-28 00:39:25 -05:00
|
|
|
stream_infos = get_stream_infos("streams", zulipToJabber.client.get_streams)
|
2014-03-17 13:17:20 -04:00
|
|
|
else:
|
2016-12-28 00:39:25 -05:00
|
|
|
stream_infos = get_stream_infos("subscriptions", zulipToJabber.client.list_subscriptions)
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2017-05-07 10:44:29 -04:00
|
|
|
rooms = [] # type: List[str]
|
2014-03-17 13:17:20 -04:00
|
|
|
for stream_info in stream_infos:
|
|
|
|
stream = stream_info['name']
|
|
|
|
if stream.endswith("/xmpp"):
|
|
|
|
rooms.append(stream_to_room(stream))
|
|
|
|
return rooms
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def config_error(msg: str) -> None:
|
2014-03-17 13:18:23 -04:00
|
|
|
sys.stderr.write("%s\n" % (msg,))
|
|
|
|
sys.exit(2)
|
|
|
|
|
2014-03-17 13:17:20 -04:00
|
|
|
if __name__ == '__main__':
|
2016-12-11 08:30:45 -05:00
|
|
|
parser = optparse.OptionParser(
|
|
|
|
epilog='''Most general and Jabber configuration options may also be specified in the
|
2014-03-17 13:17:20 -04:00
|
|
|
zulip configuration file under the jabber_mirror section (exceptions are noted
|
|
|
|
in their help sections). Keys have the same name as options with hyphens
|
|
|
|
replaced with underscores. Zulip configuration options go in the api section,
|
|
|
|
as normal.'''.replace("\n", " ")
|
2017-01-24 01:06:13 -05:00
|
|
|
)
|
2016-12-11 08:30:45 -05:00
|
|
|
parser.add_option(
|
|
|
|
'--mode',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
|
|
|
help='''Which mode to run in. Valid options are "personal" and "public". In
|
2014-03-17 13:17:20 -04:00
|
|
|
"personal" mode, the mirror uses an individual users' credentials and mirrors
|
|
|
|
all messages they send on Zulip to Jabber and all private Jabber messages to
|
|
|
|
Zulip. In "public" mode, the mirror uses the credentials for a dedicated mirror
|
|
|
|
user and mirrors messages sent to Jabber rooms to Zulip. Defaults to
|
|
|
|
"personal"'''.replace("\n", " "))
|
2016-12-11 08:30:45 -05:00
|
|
|
parser.add_option(
|
|
|
|
'--zulip-email-suffix',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
|
|
|
help='''Add the specified suffix to the local part of email addresses constructed
|
2014-03-17 13:17:20 -04:00
|
|
|
from JIDs and nicks before sending requests to the Zulip server, and remove the
|
|
|
|
suffix before sending requests to the Jabber server. For example, specifying
|
|
|
|
"+foo" will cause messages that are sent to the "bar" room by nickname "qux" to
|
|
|
|
be mirrored to the "bar/xmpp" stream in Zulip by user "qux+foo@example.com". This
|
|
|
|
option does not affect login credentials.'''.replace("\n", " "))
|
|
|
|
parser.add_option('-d', '--debug',
|
|
|
|
help='set logging to DEBUG. Can not be set via config file.',
|
|
|
|
action='store_const',
|
|
|
|
dest='log_level',
|
|
|
|
const=logging.DEBUG,
|
|
|
|
default=logging.INFO)
|
|
|
|
|
2017-11-15 12:02:07 -05:00
|
|
|
jabber_group = optparse.OptionGroup(parser, "Jabber configuration")
|
2016-12-11 08:30:45 -05:00
|
|
|
jabber_group.add_option(
|
|
|
|
'--jid',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
|
|
|
help="Your Jabber JID. If a resource is specified, "
|
2017-01-23 23:50:04 -05:00
|
|
|
"it will be used as the nickname when joining MUCs. "
|
|
|
|
"Specifying the nickname is mostly useful if you want "
|
|
|
|
"to run the public mirror from a regular user instead of "
|
|
|
|
"from a dedicated account.")
|
2014-03-17 13:17:20 -04:00
|
|
|
jabber_group.add_option('--jabber-password',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
|
|
|
help="Your Jabber password")
|
|
|
|
jabber_group.add_option('--conference-domain',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
|
|
|
help="Your Jabber conference domain (E.g. conference.jabber.example.com). "
|
2017-01-23 23:50:04 -05:00
|
|
|
"If not specifed, \"conference.\" will be prepended to your JID's domain.")
|
2014-03-17 13:17:20 -04:00
|
|
|
jabber_group.add_option('--no-use-tls',
|
|
|
|
default=None,
|
|
|
|
action='store_true')
|
2014-07-17 18:52:29 -04:00
|
|
|
jabber_group.add_option('--jabber-server-address',
|
|
|
|
default=None,
|
|
|
|
action='store',
|
2016-12-11 08:30:45 -05:00
|
|
|
help="The hostname of your Jabber server. This is only needed if "
|
2014-07-17 18:52:29 -04:00
|
|
|
"your server is missing SRV records")
|
|
|
|
jabber_group.add_option('--jabber-server-port',
|
|
|
|
default='5222',
|
|
|
|
action='store',
|
2016-12-11 08:30:45 -05:00
|
|
|
help="The port of your Jabber server. This is only needed if "
|
2014-07-17 18:52:29 -04:00
|
|
|
"your server is missing SRV records")
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
parser.add_option_group(jabber_group)
|
|
|
|
parser.add_option_group(zulip.generate_option_group(parser, "zulip-"))
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
logging.basicConfig(level=options.log_level,
|
|
|
|
format='%(levelname)-8s %(message)s')
|
|
|
|
|
|
|
|
if options.zulip_config_file is None:
|
2018-01-08 10:40:12 -05:00
|
|
|
default_config_file = zulip.get_default_config_filename()
|
|
|
|
if default_config_file is not None:
|
|
|
|
config_file = default_config_file
|
|
|
|
else:
|
|
|
|
config_error("Config file not found via --zulip-config_file or environment variable.")
|
2014-03-17 13:17:20 -04:00
|
|
|
else:
|
|
|
|
config_file = options.zulip_config_file
|
|
|
|
|
|
|
|
config = SafeConfigParser()
|
|
|
|
try:
|
2020-04-09 20:14:01 -04:00
|
|
|
with open(config_file) as f:
|
2014-03-17 13:17:20 -04:00
|
|
|
config.readfp(f, config_file)
|
2020-04-09 20:14:01 -04:00
|
|
|
except OSError:
|
2014-03-17 13:17:20 -04:00
|
|
|
pass
|
2016-07-02 13:53:19 -04:00
|
|
|
for option in ("jid", "jabber_password", "conference_domain", "mode", "zulip_email_suffix",
|
|
|
|
"jabber_server_address", "jabber_server_port"):
|
2017-01-23 23:50:04 -05:00
|
|
|
if (getattr(options, option) is None and
|
|
|
|
config.has_option("jabber_mirror", option)):
|
2014-03-17 13:17:20 -04:00
|
|
|
setattr(options, option, config.get("jabber_mirror", option))
|
|
|
|
|
|
|
|
for option in ("no_use_tls",):
|
|
|
|
if getattr(options, option) is None:
|
|
|
|
if config.has_option("jabber_mirror", option):
|
|
|
|
setattr(options, option, config.getboolean("jabber_mirror", option))
|
|
|
|
else:
|
|
|
|
setattr(options, option, False)
|
|
|
|
|
|
|
|
if options.mode is None:
|
|
|
|
options.mode = "personal"
|
|
|
|
|
|
|
|
if options.zulip_email_suffix is None:
|
|
|
|
options.zulip_email_suffix = ''
|
|
|
|
|
|
|
|
if options.mode not in ('public', 'personal'):
|
2014-03-17 13:18:23 -04:00
|
|
|
config_error("Bad value for --mode: must be one of 'public' or 'personal'")
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
if None in (options.jid, options.jabber_password):
|
2014-03-17 13:18:23 -04:00
|
|
|
config_error("You must specify your Jabber JID and Jabber password either "
|
2017-01-23 23:50:04 -05:00
|
|
|
"in the Zulip configuration file or on the commandline")
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2016-12-28 00:39:25 -05:00
|
|
|
zulipToJabber = ZulipToJabberBot(zulip.init_from_options(options, "JabberMirror/" + __version__))
|
2014-03-17 13:17:20 -04:00
|
|
|
# This won't work for open realms that don't have a consistent domain
|
2016-12-28 00:39:25 -05:00
|
|
|
options.zulip_domain = zulipToJabber.client.email.partition('@')[-1]
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
jid = JID(options.jid)
|
|
|
|
except InvalidJID as e:
|
2014-03-17 13:18:23 -04:00
|
|
|
config_error("Bad JID: %s: %s" % (options.jid, e.message))
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
if options.conference_domain is None:
|
|
|
|
options.conference_domain = "conference.%s" % (jid.domain,)
|
|
|
|
|
2016-12-28 00:39:25 -05:00
|
|
|
xmpp = JabberToZulipBot(jid, options.jabber_password, get_rooms(zulipToJabber))
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2014-07-17 18:52:29 -04:00
|
|
|
address = None
|
|
|
|
if options.jabber_server_address:
|
|
|
|
address = (options.jabber_server_address, options.jabber_server_port)
|
|
|
|
|
|
|
|
if not xmpp.connect(use_tls=not options.no_use_tls, address=address):
|
2014-03-17 13:17:20 -04:00
|
|
|
sys.exit("Unable to connect to Jabber server")
|
|
|
|
|
2016-12-28 00:39:25 -05:00
|
|
|
xmpp.set_zulip_client(zulipToJabber)
|
|
|
|
zulipToJabber.set_jabber_client(xmpp)
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
xmpp.process(block=False)
|
|
|
|
if options.mode == 'public':
|
|
|
|
event_types = ['stream']
|
|
|
|
else:
|
|
|
|
event_types = ['message', 'subscription']
|
|
|
|
|
|
|
|
try:
|
|
|
|
logging.info("Connecting to Zulip.")
|
2016-12-28 00:39:25 -05:00
|
|
|
zulipToJabber.client.call_on_each_event(zulipToJabber.process_event,
|
|
|
|
event_types=event_types)
|
2014-03-17 13:17:20 -04:00
|
|
|
except BaseException as e:
|
|
|
|
logging.exception("Exception in main loop")
|
|
|
|
xmpp.abort()
|
2014-03-17 13:20:16 -04:00
|
|
|
sys.exit(1)
|