11import multiprocessing as mp
22import sys
3- from typing import Any , Dict
3+ from typing import Any , Dict , Optional
44
55import irc .bot
66import irc .strings
77from irc .client import Event , ServerConnection , ip_numstr_to_quad
8- from irc .client_aio import AioReactor
98
109
1110class 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
0 commit comments