1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#!/usr/bin/env python3
#
# miniirc bouncer thing
#
# © 2022 by luk3yx
#
import miniirc, socket, threading, miniirc_idc
from miniirc_extras.utils import (
ircv2_message_unparser,
ircv3_message_parser,
)
from concurrent.futures import ThreadPoolExecutor
miniirc.version = None
# A single network
class Network:
_buffer = b""
IRC = miniirc_idc.IDC
encoding = "utf-8"
_001 = False
_main_lock = False
block_incoming = frozenset(("PING", "CAP", "AUTHENTICATE"))
block_outgoing = frozenset(("CAP",))
# :( what socket is this even
# Send messages
def send(self, cmd, hostmask, tags, args):
raw = ircv2_message_unparser(
cmd,
hostmask or (cmd, cmd, cmd),
tags,
args,
colon=False,
encoding=self.encoding,
)
print("->", raw)
self.sock.sendall(raw[:510] + b"\r\n")
# Receive messages
def recv(self):
while True:
while b"\n" not in self._buffer:
buf = self.sock.recv(4096)
assert buf, "The socket has been closed!"
self._buffer += buf.replace(b"\r", b"\n")
msg, self._buffer = self._buffer.split(b"\n", 1)
msg = msg.decode(self.encoding, "replace")
if msg:
cmd, _, tags, args = ircv3_message_parser(
msg, colon=False
)
return tags, cmd.upper(), args
# Handle everything
def _miniirc_handler(self, irc, cmd, hostmask, tags, args):
if cmd.startswith("IRCV3 ") or cmd in self.block_incoming:
return
elif cmd == "001":
if self._001:
return
self._001 = True
# Clear the SendQ
if self._sendq:
while len(self._sendq) > 0:
self._sendcmd(*self._sendq.pop(0))
# Start the main loop
self._main()
elif cmd == "ERROR":
self.send("PING", None, {}, [":ERROR"])
elif cmd == "PONG" and args and args[-1] == "miniirc-ping":
return
# Send the command to the client
try:
self.send(cmd, hostmask, tags, args)
except Exception as e:
print(repr(e))
self.irc.disconnect(
"Connection closed.", auto_reconnect=False
)
# The initial main loop
def _init_thread(self):
self._sendq = []
nick = None
user = None
# Wait for NICK and USER to be sent
while not nick or not user:
tags, cmd, args = self.recv()
if cmd == "NICK" and len(args) == 1:
nick = args[0]
elif cmd == "USER" and len(args) > 1:
user = args
else:
self._sendq.append((tags, cmd, args))
# Set values
self.irc.nick = nick
self.irc.ident = user[0]
self.irc.realname = user[-1]
# Connect
self.irc.connect()
# Send a command
def _sendcmd(self, tags, cmd, args):
if cmd not in self.block_outgoing:
raw = ircv2_message_unparser(
cmd,
(cmd, cmd, cmd),
{},
args,
colon=False,
encoding=None,
)
self.irc.quote(raw, tags=tags)
# The more permanent main loop
def _main(self, single_thread=False):
if not single_thread:
if self._main_lock and self._main_lock.is_alive():
return self._main_lock
t = threading.Thread(target=self._main, args=(True,))
t.start()
return t
# Clear the RecvQ
if self._recvq:
while len(self._recvq) > 0:
self.send(*self._recvq.pop(0))
self._recvq = None
# Send everything to IRC
while True:
try:
tags, cmd, args = self.recv()
except Exception as e:
print(repr(e))
return self.irc.disconnect()
self._sendcmd(tags, cmd, args)
# Generic init function
def _init(self, conn, irc):
# self.sock is the socket returned by sock.accept()
# It's the socket it uses to talk to WeeChat
# This bouncer was made for IRC-to-IRC so there isn't
# anything IDC-specific here (except for changing it to use IDC()
# instead of IRC())
self.sock, self.ip = conn
self.irc = irc
# Add the IRC handler
self._recvq = []
self.irc.CmdHandler(ircv3=True, colon=False)(
self._miniirc_handler
)
# Start the main loop
self.thread = threading.Thread(target=self._init_thread)
self.thread.start()
# Create the IRC object
def __init__(self, conn, *args, bad_cmds=None, **kwargs):
if bad_cmds is not None:
self.bad_cmds = bad_cmds
self._init(
conn,
self.IRC(
*args,
auto_connect=False,
executor=ThreadPoolExecutor(1),
**kwargs
),
)
# The bouncer class
class Bouncer:
addr = ("", 1025)
Network = Network
# Main loop
def main(self):
# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(self.addr)
sock.listen(1)
net = self.Network(sock.accept(), *self.args, **self.kwargs)
net.thread.join()
net.irc.wait_until_disconnected()
# The main init
def __init__(self, *args, **kwargs):
self.args, self.kwargs = args, kwargs
# Debugging
def main():
Bouncer(
"127.0.0.1",
6835,
"luk3yx",
debug=True,
ns_identity="luk3yx billy",
).main()
if __name__ == "__main__":
main()
|