bridge_with_irc: Use async client to avoid timeout.

This commit is contained in:
rht 2018-08-13 06:56:15 +01:00 committed by Tim Abbott
parent 9be5ec33b3
commit 910f83ee8e

View file

@ -1,10 +1,13 @@
import irc.bot import irc.bot
import irc.strings import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr, Event, ServerConnection from irc.client import ip_numstr_to_quad, ip_quad_to_numstr, Event, ServerConnection
from irc.client_aio import AioReactor
from typing import Any, Dict from typing import Any, Dict
class IRCBot(irc.bot.SingleServerIRCBot): class IRCBot(irc.bot.SingleServerIRCBot):
reactor_class = AioReactor
def __init__(self, zulip_client, stream, channel, nickname, server, port=6667): def __init__(self, zulip_client, stream, channel, nickname, server, port=6667):
# type: (Any, str, irc.bot.Channel, str, str, int) -> None # type: (Any, str, irc.bot.Channel, str, str, int) -> None
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
@ -18,6 +21,15 @@ class IRCBot(irc.bot.SingleServerIRCBot):
nick = sender_string.split("!")[0] nick = sender_string.split("!")[0]
return nick + "@" + self.IRC_DOMAIN return nick + "@" + self.IRC_DOMAIN
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)
)
def on_nicknameinuse(self, c, e): def on_nicknameinuse(self, c, e):
# type: (ServerConnection, Event) -> None # type: (ServerConnection, Event) -> None
c.nick(c.get_nickname().replace("_zulip", "__zulip")) c.nick(c.get_nickname().replace("_zulip", "__zulip"))