Skip to content

Commit 2814acc

Browse files
rhtandersk
authored andcommitted
IRC: Add option for SASL authentication.
This additionally reverts to using sync IRC client, because upstream https://github.com/jaraco/irc only supports it for the sync client.
1 parent f11e960 commit 2814acc

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

zulip/integrations/bridge_with_irc/irc-mirror.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
1919
--stream is a Zulip stream.
2020
--topic is a Zulip topic, is optionally specified, defaults to "IRC".
21-
--nickserv-pw is a password for the nickserv, is optionally specified.
21+
Optional arguments:
22+
--nickserv-pw is a password for the nickserv.
23+
--sasl-password is a password for SASL authentication.
2224
2325
Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options.
2426
@@ -36,6 +38,7 @@
3638
parser.add_argument("--stream", default="general")
3739
parser.add_argument("--topic", default="IRC")
3840
parser.add_argument("--nickserv-pw", default="")
41+
parser.add_argument("--sasl-password", default=None)
3942

4043
options = parser.parse_args()
4144
# Setting the client to irc_mirror is critical for this to work
@@ -64,5 +67,6 @@
6467
options.irc_server,
6568
options.nickserv_pw,
6669
options.port,
70+
sasl_password=options.sasl_password,
6771
)
6872
bot.start()

zulip/integrations/bridge_with_irc/irc_mirror_backend.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import multiprocessing as mp
22
import sys
3-
from typing import Any, Dict
3+
from typing import Any, Dict, Optional
44

55
import irc.bot
66
import irc.strings
77
from irc.client import Event, ServerConnection, ip_numstr_to_quad
8-
from irc.client_aio import AioReactor
98

109

1110
class IRCBot(irc.bot.SingleServerIRCBot):
12-
reactor_class = AioReactor
13-
1411
def __init__(
1512
self,
1613
zulip_client: Any,
@@ -21,6 +18,7 @@ def __init__(
2118
server: str,
2219
nickserv_password: str = "",
2320
port: int = 6667,
21+
sasl_password: Optional[str] = None,
2422
) -> None:
2523
self.channel: irc.bot.Channel = channel
2624
self.zulip_client = zulip_client
@@ -31,19 +29,17 @@ def __init__(
3129
# Make sure the bot is subscribed to the stream
3230
self.check_subscription_or_die()
3331
# Initialize IRC bot after proper connection to Zulip server has been confirmed.
34-
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
32+
if sasl_password is not None:
33+
irc.bot.SingleServerIRCBot.__init__(
34+
self, [(server, port, sasl_password)], nickname, nickname, sasl_login=nickname
35+
)
36+
else:
37+
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
3538

3639
def zulip_sender(self, sender_string: str) -> str:
3740
nick = sender_string.split("!")[0]
3841
return nick + "@" + self.IRC_DOMAIN
3942

40-
def connect(self, *args: Any, **kwargs: Any) -> None:
41-
# Taken from
42-
# https://github.com/jaraco/irc/blob/main/irc/client_aio.py,
43-
# in particular the method of AioSimpleIRCClient
44-
self.c = self.reactor.loop.run_until_complete(self.connection.connect(*args, **kwargs))
45-
print("Listening now. Please send an IRC message to verify operation")
46-
4743
def check_subscription_or_die(self) -> None:
4844
resp = self.zulip_client.get_subscriptions()
4945
if resp["result"] != "success":
@@ -76,7 +72,7 @@ def forward_to_irc(msg: Dict[str, Any]) -> None:
7672
at_the_specified_subject = msg["subject"].casefold() == self.topic.casefold()
7773
if in_the_specified_stream and at_the_specified_subject:
7874
msg["content"] = "@**{}**: ".format(msg["sender_full_name"]) + msg["content"]
79-
send = lambda x: self.c.privmsg(self.channel, x)
75+
send = lambda x: c.privmsg(self.channel, x)
8076
else:
8177
return
8278
else:
@@ -86,9 +82,9 @@ def forward_to_irc(msg: Dict[str, Any]) -> None:
8682
if u["email"] != msg["sender_email"]
8783
]
8884
if len(recipients) == 1:
89-
send = lambda x: self.c.privmsg(recipients[0], x)
85+
send = lambda x: c.privmsg(recipients[0], x)
9086
else:
91-
send = lambda x: self.c.privmsg_many(recipients, x)
87+
send = lambda x: c.privmsg_many(recipients, x)
9288
for line in msg["content"].split("\n"):
9389
send(line)
9490

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
irc==18.0
1+
irc~=20.3

0 commit comments

Comments
 (0)