Annotate bots/jabber_mirror_backend.py
This commit is contained in:
parent
b4fc87745a
commit
476a591ef5
|
@ -44,26 +44,32 @@ import threading
|
||||||
import optparse
|
import optparse
|
||||||
|
|
||||||
from sleekxmpp import ClientXMPP, InvalidJID, JID
|
from sleekxmpp import ClientXMPP, InvalidJID, JID
|
||||||
|
from sleekxmpp.stanza import Message as JabberMessage
|
||||||
from sleekxmpp.exceptions import IqError, IqTimeout
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
||||||
from six.moves.configparser import SafeConfigParser
|
from six.moves.configparser import SafeConfigParser
|
||||||
import os, sys, zulip, getpass
|
import os, sys, zulip, getpass
|
||||||
import re
|
import re
|
||||||
|
from typing import Any, Callable
|
||||||
|
|
||||||
__version__ = "1.1"
|
__version__ = "1.1"
|
||||||
|
|
||||||
def room_to_stream(room):
|
def room_to_stream(room):
|
||||||
|
# type: (str) -> str
|
||||||
return room + "/xmpp"
|
return room + "/xmpp"
|
||||||
|
|
||||||
def stream_to_room(stream):
|
def stream_to_room(stream):
|
||||||
|
# type: (str) -> str
|
||||||
return stream.lower().rpartition("/xmpp")[0]
|
return stream.lower().rpartition("/xmpp")[0]
|
||||||
|
|
||||||
def jid_to_zulip(jid):
|
def jid_to_zulip(jid):
|
||||||
|
# type: (JID) -> str
|
||||||
suffix = ''
|
suffix = ''
|
||||||
if not jid.username.endswith("-bot"):
|
if not jid.username.endswith("-bot"):
|
||||||
suffix = options.zulip_email_suffix
|
suffix = options.zulip_email_suffix
|
||||||
return "%s%s@%s" % (jid.username, suffix, options.zulip_domain)
|
return "%s%s@%s" % (jid.username, suffix, options.zulip_domain)
|
||||||
|
|
||||||
def zulip_to_jid(email, jabber_domain):
|
def zulip_to_jid(email, jabber_domain):
|
||||||
|
# type: (str, str) -> JID
|
||||||
jid = JID(email, domain=jabber_domain)
|
jid = JID(email, domain=jabber_domain)
|
||||||
if (options.zulip_email_suffix
|
if (options.zulip_email_suffix
|
||||||
and options.zulip_email_suffix in jid.username
|
and options.zulip_email_suffix in jid.username
|
||||||
|
@ -73,6 +79,7 @@ def zulip_to_jid(email, jabber_domain):
|
||||||
|
|
||||||
class JabberToZulipBot(ClientXMPP):
|
class JabberToZulipBot(ClientXMPP):
|
||||||
def __init__(self, jid, password, rooms):
|
def __init__(self, jid, password, rooms):
|
||||||
|
# type: (JID, str, List[str]) -> None
|
||||||
if jid.resource:
|
if jid.resource:
|
||||||
self.nick = jid.resource
|
self.nick = jid.resource
|
||||||
else:
|
else:
|
||||||
|
@ -90,15 +97,18 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
self.register_plugin('xep_0199') # XMPP Ping
|
self.register_plugin('xep_0199') # XMPP Ping
|
||||||
|
|
||||||
def set_zulip_client(self, client):
|
def set_zulip_client(self, client):
|
||||||
|
# type: (zulip.Client) -> None
|
||||||
self.zulip = client
|
self.zulip = client
|
||||||
|
|
||||||
def session_start(self, event):
|
def session_start(self, event):
|
||||||
|
# type: (Dict[str, Any]) -> None
|
||||||
self.get_roster()
|
self.get_roster()
|
||||||
self.send_presence()
|
self.send_presence()
|
||||||
for room in self.rooms_to_join:
|
for room in self.rooms_to_join:
|
||||||
self.join_muc(room)
|
self.join_muc(room)
|
||||||
|
|
||||||
def join_muc(self, room):
|
def join_muc(self, room):
|
||||||
|
# type: (str) -> None
|
||||||
if room in self.rooms:
|
if room in self.rooms:
|
||||||
return
|
return
|
||||||
logging.debug("Joining " + room)
|
logging.debug("Joining " + room)
|
||||||
|
@ -124,6 +134,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
logging.error("Could not configure room: " + str(muc_jid))
|
logging.error("Could not configure room: " + str(muc_jid))
|
||||||
|
|
||||||
def leave_muc(self, room):
|
def leave_muc(self, room):
|
||||||
|
# type: (str) -> None
|
||||||
if room not in self.rooms:
|
if room not in self.rooms:
|
||||||
return
|
return
|
||||||
logging.debug("Leaving " + room)
|
logging.debug("Leaving " + room)
|
||||||
|
@ -132,6 +143,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
self.plugin['xep_0045'].leaveMUC(muc_jid, self.nick)
|
self.plugin['xep_0045'].leaveMUC(muc_jid, self.nick)
|
||||||
|
|
||||||
def message(self, msg):
|
def message(self, msg):
|
||||||
|
# type: (JabberMessage) -> Any
|
||||||
try:
|
try:
|
||||||
if msg["type"] == "groupchat":
|
if msg["type"] == "groupchat":
|
||||||
return self.group(msg)
|
return self.group(msg)
|
||||||
|
@ -144,6 +156,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
logging.exception("Error forwarding Jabber => Zulip")
|
logging.exception("Error forwarding Jabber => Zulip")
|
||||||
|
|
||||||
def private(self, msg):
|
def private(self, msg):
|
||||||
|
# type: (JabberMessage) -> None
|
||||||
if options.mode == 'public' or msg['thread'] == u'\u1FFFE':
|
if options.mode == 'public' or msg['thread'] == u'\u1FFFE':
|
||||||
return
|
return
|
||||||
sender = jid_to_zulip(msg["from"])
|
sender = jid_to_zulip(msg["from"])
|
||||||
|
@ -160,6 +173,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
logging.error(ret)
|
logging.error(ret)
|
||||||
|
|
||||||
def group(self, msg):
|
def group(self, msg):
|
||||||
|
# type: (JabberMessage) -> None
|
||||||
if options.mode == 'personal' or msg["thread"] == u'\u1FFFE':
|
if options.mode == 'personal' or msg["thread"] == u'\u1FFFE':
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -187,6 +201,7 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
logging.error(ret)
|
logging.error(ret)
|
||||||
|
|
||||||
def nickname_to_jid(self, room, nick):
|
def nickname_to_jid(self, room, nick):
|
||||||
|
# type: (str, str) -> JID
|
||||||
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 JID(local=nick.replace(' ', ''), domain=self.boundjid.domain)
|
return JID(local=nick.replace(' ', ''), domain=self.boundjid.domain)
|
||||||
|
@ -195,13 +210,16 @@ class JabberToZulipBot(ClientXMPP):
|
||||||
|
|
||||||
class ZulipToJabberBot(object):
|
class ZulipToJabberBot(object):
|
||||||
def __init__(self, zulip_client):
|
def __init__(self, zulip_client):
|
||||||
|
# type: (zulip.Client) -> None
|
||||||
self.client = zulip_client
|
self.client = zulip_client
|
||||||
self.jabber = None # type: JabberToZulipBot
|
self.jabber = None # type: JabberToZulipBot
|
||||||
|
|
||||||
def set_jabber_client(self, client):
|
def set_jabber_client(self, client):
|
||||||
|
# type: (JabberToZulipBot) -> None
|
||||||
self.jabber = client
|
self.jabber = client
|
||||||
|
|
||||||
def process_event(self, event):
|
def process_event(self, event):
|
||||||
|
# type: (Dict[str, Any]) -> None
|
||||||
if event['type'] == 'message':
|
if event['type'] == 'message':
|
||||||
message = event["message"]
|
message = event["message"]
|
||||||
if message['sender_email'] != self.client.email:
|
if message['sender_email'] != self.client.email:
|
||||||
|
@ -220,6 +238,7 @@ class ZulipToJabberBot(object):
|
||||||
self.process_stream(event)
|
self.process_stream(event)
|
||||||
|
|
||||||
def stream_message(self, msg):
|
def stream_message(self, msg):
|
||||||
|
# type: (Dict[str, str]) -> None
|
||||||
stream = msg['display_recipient']
|
stream = msg['display_recipient']
|
||||||
if not stream.endswith("/xmpp"):
|
if not stream.endswith("/xmpp"):
|
||||||
return
|
return
|
||||||
|
@ -234,6 +253,7 @@ class ZulipToJabberBot(object):
|
||||||
outgoing.send()
|
outgoing.send()
|
||||||
|
|
||||||
def private_message(self, msg):
|
def private_message(self, msg):
|
||||||
|
# type: (Dict[str, Any]) -> None
|
||||||
for recipient in msg['display_recipient']:
|
for recipient in msg['display_recipient']:
|
||||||
if recipient["email"] == self.client.email:
|
if recipient["email"] == self.client.email:
|
||||||
continue
|
continue
|
||||||
|
@ -249,6 +269,7 @@ class ZulipToJabberBot(object):
|
||||||
outgoing.send()
|
outgoing.send()
|
||||||
|
|
||||||
def process_subscription(self, event):
|
def process_subscription(self, event):
|
||||||
|
# type: (Dict[str, Any]) -> None
|
||||||
if event['op'] == 'add':
|
if event['op'] == 'add':
|
||||||
streams = [s['name'].lower() for s in event['subscriptions']]
|
streams = [s['name'].lower() for s in event['subscriptions']]
|
||||||
streams = [s for s in streams if s.endswith("/xmpp")]
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
||||||
|
@ -261,6 +282,7 @@ class ZulipToJabberBot(object):
|
||||||
self.jabber.leave_muc(stream_to_room(stream))
|
self.jabber.leave_muc(stream_to_room(stream))
|
||||||
|
|
||||||
def process_stream(self, event):
|
def process_stream(self, event):
|
||||||
|
# type: (Dict[str, Any]) -> None
|
||||||
if event['op'] == 'occupy':
|
if event['op'] == 'occupy':
|
||||||
streams = [s['name'].lower() for s in event['streams']]
|
streams = [s['name'].lower() for s in event['streams']]
|
||||||
streams = [s for s in streams if s.endswith("/xmpp")]
|
streams = [s for s in streams if s.endswith("/xmpp")]
|
||||||
|
@ -273,7 +295,9 @@ class ZulipToJabberBot(object):
|
||||||
self.jabber.leave_muc(stream_to_room(stream))
|
self.jabber.leave_muc(stream_to_room(stream))
|
||||||
|
|
||||||
def get_rooms(zulip):
|
def get_rooms(zulip):
|
||||||
|
# type: (zulip) -> List[str]
|
||||||
def get_stream_infos(key, method):
|
def get_stream_infos(key, method):
|
||||||
|
# type: (str, Callable) -> Any
|
||||||
ret = method()
|
ret = method()
|
||||||
if ret.get("result") != "success":
|
if ret.get("result") != "success":
|
||||||
logging.error(ret)
|
logging.error(ret)
|
||||||
|
@ -285,7 +309,7 @@ def get_rooms(zulip):
|
||||||
else:
|
else:
|
||||||
stream_infos = get_stream_infos("subscriptions", zulip.client.list_subscriptions)
|
stream_infos = get_stream_infos("subscriptions", zulip.client.list_subscriptions)
|
||||||
|
|
||||||
rooms = []
|
rooms = [] # type: List[str]
|
||||||
for stream_info in stream_infos:
|
for stream_info in stream_infos:
|
||||||
stream = stream_info['name']
|
stream = stream_info['name']
|
||||||
if stream.endswith("/xmpp"):
|
if stream.endswith("/xmpp"):
|
||||||
|
@ -293,6 +317,7 @@ def get_rooms(zulip):
|
||||||
return rooms
|
return rooms
|
||||||
|
|
||||||
def config_error(msg):
|
def config_error(msg):
|
||||||
|
# type: (str) -> None
|
||||||
sys.stderr.write("%s\n" % (msg,))
|
sys.stderr.write("%s\n" % (msg,))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue