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.
|
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import sys
|
|
|
|
from configparser import SafeConfigParser
|
|
|
|
|
2014-03-17 13:17:20 -04:00
|
|
|
# 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 | | | |
|
2021-05-28 05:00:04 -04:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Set
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
from sleekxmpp import JID, ClientXMPP, InvalidJID
|
2016-12-04 14:55:41 -05:00
|
|
|
from sleekxmpp.stanza import Message as JabberMessage
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2016-12-28 00:39:25 -05:00
|
|
|
import zulip
|
|
|
|
from zulip import Client
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
__version__ = "1.1"
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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"
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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]
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def jid_to_zulip(jid: JID) -> str:
|
2021-05-28 05:05:11 -04:00
|
|
|
suffix = ""
|
2014-03-17 13:17:20 -04:00
|
|
|
if not jid.username.endswith("-bot"):
|
|
|
|
suffix = options.zulip_email_suffix
|
|
|
|
return "%s%s@%s" % (jid.username, suffix, options.zulip_domain)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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)
|
2020-04-18 20:33:01 -04: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
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2014-03-17 13:17:20 -04:00
|
|
|
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
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
self.register_plugin("xep_0045") # Jabber chatrooms
|
|
|
|
self.register_plugin("xep_0199") # XMPP Ping
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2021-05-28 05:05:11 -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)
|
2021-05-28 05:05:11 -04:00
|
|
|
xep0045 = self.plugin["xep_0045"]
|
2014-03-17 13:17:20 -04:00
|
|
|
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)
|
2021-05-28 05:05:11 -04:00
|
|
|
self.plugin["xep_0045"].leaveMUC(muc_jid, self.nick)
|
2014-03-17 13:17:20 -04:00
|
|
|
|
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:
|
2021-05-28 05:05:11 -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(
|
2021-05-28 05:03:46 -04:00
|
|
|
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:
|
2021-05-28 05:05:11 -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)"
|
2021-05-28 05:05:11 -04:00
|
|
|
stream = room_to_stream(msg["from"].local)
|
2014-03-17 13:17:20 -04:00
|
|
|
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(
|
2021-05-28 05:03:46 -04:00
|
|
|
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:
|
2021-05-28 05:05:11 -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)
|
2014-03-17 13:17:20 -04:00
|
|
|
else:
|
|
|
|
return jid
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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:
|
2021-05-28 05:05:11 -04:00
|
|
|
if event["type"] == "message":
|
2014-03-17 13:17:20 -04:00
|
|
|
message = event["message"]
|
2021-05-28 05:05:11 -04:00
|
|
|
if message["sender_email"] != self.client.email:
|
2014-03-17 13:17:20 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-05-28 05:05:11 -04:00
|
|
|
if message["type"] == "stream":
|
2014-03-17 13:17:20 -04:00
|
|
|
self.stream_message(message)
|
2021-05-28 05:05:11 -04:00
|
|
|
elif message["type"] == "private":
|
2014-03-17 13:17:20 -04:00
|
|
|
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")
|
2021-05-28 05:05:11 -04:00
|
|
|
elif event["type"] == "subscription":
|
2014-03-17 13:17:20 -04:00
|
|
|
self.process_subscription(event)
|
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def stream_message(self, msg: Dict[str, str]) -> None:
|
2021-05-28 05:03:46 -04:00
|
|
|
assert self.jabber is not None
|
2021-05-28 05:05:11 -04:00
|
|
|
stream = msg["display_recipient"]
|
2014-03-17 13:17:20 -04:00
|
|
|
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(
|
2021-05-28 05:05:11 -04:00
|
|
|
mto=jabber_recipient, mbody=msg["content"], mtype="groupchat"
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2021-05-28 05:05:11 -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:
|
2021-05-28 05:03:46 -04:00
|
|
|
assert self.jabber is not None
|
2021-05-28 05:05:11 -04:00
|
|
|
for recipient in msg["display_recipient"]:
|
2014-03-17 13:17:20 -04:00
|
|
|
if recipient["email"] == self.client.email:
|
|
|
|
continue
|
2014-07-17 18:18:06 -04:00
|
|
|
if not recipient["is_mirror_dummy"]:
|
|
|
|
continue
|
2021-05-28 05:05:11 -04:00
|
|
|
recip_email = recipient["email"]
|
2014-03-17 13:17:20 -04:00
|
|
|
jabber_recipient = zulip_to_jid(recip_email, self.jabber.boundjid.domain)
|
|
|
|
outgoing = self.jabber.make_message(
|
2021-05-28 05:05:11 -04:00
|
|
|
mto=jabber_recipient, mbody=msg["content"], mtype="chat"
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2021-05-28 05:05:11 -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:
|
2021-05-28 05:03:46 -04:00
|
|
|
assert self.jabber is not None
|
2021-05-28 05:05:11 -04:00
|
|
|
if event["op"] == "add":
|
|
|
|
streams = [s["name"].lower() for s in event["subscriptions"]]
|
2014-03-17 13:17:20 -04:00
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.join_muc(stream_to_room(stream))
|
2021-05-28 05:05:11 -04:00
|
|
|
if event["op"] == "remove":
|
|
|
|
streams = [s["name"].lower() for s in event["subscriptions"]]
|
2014-03-17 13:17:20 -04:00
|
|
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
|
|
|
for stream in streams:
|
|
|
|
self.jabber.leave_muc(stream_to_room(stream))
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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]
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
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:
|
2021-03-09 23:16:47 -05:00
|
|
|
stream_infos = get_stream_infos("subscriptions", zulipToJabber.client.get_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:
|
2021-05-28 05:05:11 -04:00
|
|
|
stream = stream_info["name"]
|
2020-04-18 19:47:57 -04:00
|
|
|
if stream.endswith("/xmpp"):
|
|
|
|
rooms.append(stream_to_room(stream))
|
2014-03-17 13:17:20 -04:00
|
|
|
return rooms
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
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)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if __name__ == "__main__":
|
2016-12-11 08:30:45 -05:00
|
|
|
parser = optparse.OptionParser(
|
2021-05-28 05:05:11 -04:00
|
|
|
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,
|
2021-05-28 05:05:11 -04:00
|
|
|
as normal.""".replace(
|
2021-05-28 05:03:46 -04:00
|
|
|
"\n", " "
|
|
|
|
)
|
2017-01-24 01:06:13 -05:00
|
|
|
)
|
2016-12-11 08:30:45 -05:00
|
|
|
parser.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--mode",
|
2016-12-11 08:30:45 -05:00
|
|
|
default=None,
|
2021-05-28 05:05:11 -04:00
|
|
|
action="store",
|
2016-12-11 08:30:45 -05:00
|
|
|
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
|
2021-05-28 05:03:46 -04:00
|
|
|
"personal"'''.replace(
|
|
|
|
"\n", " "
|
|
|
|
),
|
|
|
|
)
|
2016-12-11 08:30:45 -05:00
|
|
|
parser.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--zulip-email-suffix",
|
2016-12-11 08:30:45 -05:00
|
|
|
default=None,
|
2021-05-28 05:05:11 -04:00
|
|
|
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
|
2021-05-28 05:05:11 -04:00
|
|
|
option does not affect login credentials.""".replace(
|
2021-05-28 05:03:46 -04:00
|
|
|
"\n", " "
|
|
|
|
),
|
|
|
|
)
|
|
|
|
parser.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"-d",
|
|
|
|
"--debug",
|
|
|
|
help="set logging to DEBUG. Can not be set via config file.",
|
|
|
|
action="store_const",
|
|
|
|
dest="log_level",
|
2021-05-28 05:03:46 -04:00
|
|
|
const=logging.DEBUG,
|
|
|
|
default=logging.INFO,
|
|
|
|
)
|
2014-03-17 13:17:20 -04:00
|
|
|
|
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(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--jid",
|
2016-12-11 08:30:45 -05:00
|
|
|
default=None,
|
2021-05-28 05:05:11 -04:00
|
|
|
action="store",
|
2016-12-11 08:30:45 -05:00
|
|
|
help="Your Jabber JID. If a resource is specified, "
|
2021-05-28 05:03:46 -04: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.",
|
|
|
|
)
|
|
|
|
jabber_group.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--jabber-password", default=None, action="store", help="Your Jabber password"
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
|
|
|
jabber_group.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--conference-domain",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=None,
|
2021-05-28 05:05:11 -04:00
|
|
|
action="store",
|
2021-05-28 05:03:46 -04:00
|
|
|
help="Your Jabber conference domain (E.g. conference.jabber.example.com). "
|
2021-05-28 05:05:11 -04:00
|
|
|
'If not specifed, "conference." will be prepended to your JID\'s domain.',
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2021-05-28 05:05:11 -04:00
|
|
|
jabber_group.add_option("--no-use-tls", default=None, action="store_true")
|
2021-05-28 05:03:46 -04:00
|
|
|
jabber_group.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--jabber-server-address",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=None,
|
2021-05-28 05:05:11 -04:00
|
|
|
action="store",
|
2021-05-28 05:03:46 -04:00
|
|
|
help="The hostname of your Jabber server. This is only needed if "
|
|
|
|
"your server is missing SRV records",
|
|
|
|
)
|
|
|
|
jabber_group.add_option(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--jabber-server-port",
|
|
|
|
default="5222",
|
|
|
|
action="store",
|
2021-05-28 05:03:46 -04:00
|
|
|
help="The port of your Jabber server. This is only needed if "
|
|
|
|
"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()
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
logging.basicConfig(level=options.log_level, format="%(levelname)-8s %(message)s")
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
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
|
2021-05-28 05:03:46 -04:00
|
|
|
for option in (
|
|
|
|
"jid",
|
|
|
|
"jabber_password",
|
|
|
|
"conference_domain",
|
|
|
|
"mode",
|
|
|
|
"zulip_email_suffix",
|
|
|
|
"jabber_server_address",
|
|
|
|
"jabber_server_port",
|
|
|
|
):
|
|
|
|
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:
|
2021-05-28 05:05:11 -04:00
|
|
|
options.zulip_email_suffix = ""
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
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):
|
2021-05-28 05:03:46 -04:00
|
|
|
config_error(
|
|
|
|
"You must specify your Jabber JID and Jabber password either "
|
|
|
|
"in the Zulip configuration file or on the commandline"
|
|
|
|
)
|
2014-03-17 13:17:20 -04:00
|
|
|
|
2021-05-28 05:03:46 -04: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
|
2021-05-28 05:05:11 -04: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)
|
2021-05-28 05:05:11 -04:00
|
|
|
if options.mode == "public":
|
|
|
|
event_types = ["stream"]
|
2014-03-17 13:17:20 -04:00
|
|
|
else:
|
2021-05-28 05:05:11 -04:00
|
|
|
event_types = ["message", "subscription"]
|
2014-03-17 13:17:20 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
logging.info("Connecting to Zulip.")
|
2021-05-28 05:03:46 -04:00
|
|
|
zulipToJabber.client.call_on_each_event(
|
|
|
|
zulipToJabber.process_event, event_types=event_types
|
|
|
|
)
|
2020-04-18 19:36:24 -04:00
|
|
|
except BaseException:
|
2014-03-17 13:17:20 -04:00
|
|
|
logging.exception("Exception in main loop")
|
|
|
|
xmpp.abort()
|
2014-03-17 13:20:16 -04:00
|
|
|
sys.exit(1)
|