1
1
import multiprocessing as mp
2
2
import sys
3
- from typing import Any , Dict
3
+ from typing import Any , Dict , Optional
4
4
5
5
import irc .bot
6
6
import irc .strings
7
7
from irc .client import Event , ServerConnection , ip_numstr_to_quad
8
- from irc .client_aio import AioReactor
9
8
10
9
11
10
class IRCBot (irc .bot .SingleServerIRCBot ):
12
- reactor_class = AioReactor
13
-
14
11
def __init__ (
15
12
self ,
16
13
zulip_client : Any ,
@@ -21,6 +18,7 @@ def __init__(
21
18
server : str ,
22
19
nickserv_password : str = "" ,
23
20
port : int = 6667 ,
21
+ sasl_password : Optional [str ] = None ,
24
22
) -> None :
25
23
self .channel : irc .bot .Channel = channel
26
24
self .zulip_client = zulip_client
@@ -31,19 +29,17 @@ def __init__(
31
29
# Make sure the bot is subscribed to the stream
32
30
self .check_subscription_or_die ()
33
31
# 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 )
35
38
36
39
def zulip_sender (self , sender_string : str ) -> str :
37
40
nick = sender_string .split ("!" )[0 ]
38
41
return nick + "@" + self .IRC_DOMAIN
39
42
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
-
47
43
def check_subscription_or_die (self ) -> None :
48
44
resp = self .zulip_client .get_subscriptions ()
49
45
if resp ["result" ] != "success" :
@@ -76,7 +72,7 @@ def forward_to_irc(msg: Dict[str, Any]) -> None:
76
72
at_the_specified_subject = msg ["subject" ].casefold () == self .topic .casefold ()
77
73
if in_the_specified_stream and at_the_specified_subject :
78
74
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 )
80
76
else :
81
77
return
82
78
else :
@@ -86,9 +82,9 @@ def forward_to_irc(msg: Dict[str, Any]) -> None:
86
82
if u ["email" ] != msg ["sender_email" ]
87
83
]
88
84
if len (recipients ) == 1 :
89
- send = lambda x : self . c .privmsg (recipients [0 ], x )
85
+ send = lambda x : c .privmsg (recipients [0 ], x )
90
86
else :
91
- send = lambda x : self . c .privmsg_many (recipients , x )
87
+ send = lambda x : c .privmsg_many (recipients , x )
92
88
for line in msg ["content" ].split ("\n " ):
93
89
send (line )
94
90
0 commit comments