2017-09-04 07:57:08 -04:00
|
|
|
import irc.bot
|
|
|
|
import irc.strings
|
|
|
|
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr, Event, ServerConnection
|
2018-08-13 01:56:15 -04:00
|
|
|
from irc.client_aio import AioReactor
|
2017-10-10 00:28:04 -04:00
|
|
|
from typing import Any, Dict
|
2017-09-04 07:57:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
class IRCBot(irc.bot.SingleServerIRCBot):
|
2018-08-13 01:56:15 -04:00
|
|
|
reactor_class = AioReactor
|
|
|
|
|
2018-08-13 02:39:31 -04:00
|
|
|
def __init__(self, zulip_client, stream, channel, nickname, server, port=6667):
|
|
|
|
# type: (Any, str, irc.bot.Channel, str, str, int) -> None
|
2017-09-04 07:57:08 -04:00
|
|
|
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
|
|
|
self.channel = channel # type: irc.bot.Channel
|
|
|
|
self.zulip_client = zulip_client
|
2018-08-13 02:39:31 -04:00
|
|
|
self.stream = stream
|
2018-08-13 02:45:44 -04:00
|
|
|
self.IRC_DOMAIN = server
|
|
|
|
|
|
|
|
def zulip_sender(self, sender_string):
|
|
|
|
# type: (str) -> str
|
|
|
|
nick = sender_string.split("!")[0]
|
|
|
|
return nick + "@" + self.IRC_DOMAIN
|
2017-09-04 07:57:08 -04:00
|
|
|
|
2018-08-13 01:56:15 -04:00
|
|
|
def connect(self, *args, **kwargs):
|
|
|
|
# type: (*Any, **Any) -> None
|
|
|
|
# Taken from
|
|
|
|
# https://github.com/jaraco/irc/blob/master/irc/client_aio.py,
|
|
|
|
# in particular the method of AioSimpleIRCClient
|
|
|
|
self.reactor.loop.run_until_complete(
|
|
|
|
self.connection.connect(*args, **kwargs)
|
|
|
|
)
|
|
|
|
|
2017-09-04 07:57:08 -04:00
|
|
|
def on_nicknameinuse(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
c.nick(c.get_nickname().replace("_zulip", "__zulip"))
|
|
|
|
|
|
|
|
def on_welcome(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
c.join(self.channel)
|
|
|
|
|
|
|
|
def forward_to_irc(msg):
|
|
|
|
# type: (Dict[str, Any]) -> None
|
|
|
|
if msg["type"] == "stream":
|
|
|
|
send = lambda x: c.privmsg(msg["display_recipient"], x)
|
|
|
|
else:
|
|
|
|
recipients = [u["short_name"] for u in msg["display_recipient"] if
|
|
|
|
u["email"] != msg["sender_email"]]
|
|
|
|
if len(recipients) == 1:
|
|
|
|
send = lambda x: c.privmsg(recipients[0], x)
|
|
|
|
else:
|
|
|
|
send = lambda x: c.privmsg_many(recipients, x)
|
|
|
|
for line in msg["content"].split("\n"):
|
|
|
|
send(line)
|
|
|
|
|
|
|
|
## Forwarding from Zulip => IRC is disabled; uncomment the next
|
|
|
|
## line to make this bot forward in that direction instead.
|
|
|
|
#
|
|
|
|
# self.zulip_client.call_on_each_message(forward_to_irc)
|
|
|
|
|
|
|
|
def on_privmsg(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
content = e.arguments[0]
|
2018-08-13 02:45:44 -04:00
|
|
|
sender = self.zulip_sender(e.source)
|
|
|
|
if sender.endswith("_zulip@" + self.IRC_DOMAIN):
|
2017-09-04 07:57:08 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Forward the PM to Zulip
|
|
|
|
print(self.zulip_client.send_message({
|
|
|
|
"sender": sender,
|
|
|
|
"type": "private",
|
|
|
|
"to": "username@example.com",
|
|
|
|
"content": content,
|
|
|
|
}))
|
|
|
|
|
|
|
|
def on_pubmsg(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
content = e.arguments[0]
|
2018-08-13 02:39:31 -04:00
|
|
|
stream = self.stream
|
2018-08-13 02:45:44 -04:00
|
|
|
sender = self.zulip_sender(e.source)
|
|
|
|
if sender.endswith("_zulip@" + self.IRC_DOMAIN):
|
2017-09-04 07:57:08 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Forward the stream message to Zulip
|
|
|
|
print(self.zulip_client.send_message({
|
|
|
|
"type": "stream",
|
|
|
|
"to": stream,
|
|
|
|
"subject": "IRC",
|
|
|
|
"content": content,
|
2018-08-13 02:45:44 -04:00
|
|
|
"content": "**{0}**: {1}".format(sender, content),
|
2017-09-04 07:57:08 -04:00
|
|
|
}))
|
|
|
|
|
|
|
|
def on_dccmsg(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
c.privmsg("You said: " + e.arguments[0])
|
|
|
|
|
|
|
|
def on_dccchat(self, c, e):
|
|
|
|
# type: (ServerConnection, Event) -> None
|
|
|
|
if len(e.arguments) != 2:
|
|
|
|
return
|
|
|
|
args = e.arguments[1].split()
|
|
|
|
if len(args) == 4:
|
|
|
|
try:
|
|
|
|
address = ip_numstr_to_quad(args[2])
|
|
|
|
port = int(args[3])
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
self.dcc_connect(address, port)
|