-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircd.py
74 lines (57 loc) · 2.24 KB
/
ircd.py
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
#!/usr/bin/env python3
import irc.bot
import re
class IRC(irc.bot.SingleServerIRCBot):
thread_lock = None
running = True
config = None
connection = None
discord = None
def __init__(self, config):
irc.client.ServerConnection.buffer_class.encoding = "latin-1"
irc.bot.SingleServerIRCBot.__init__(self, [
(config["SERVER"],
config["PORT"])],
config["NICK"],
config["NICK"] + " Relay")
self.config = config
def set_discord(self, discordd):
self.discord = discordd
def set_thread_lock(self, lock):
self.thread_lock = lock
def close(self):
self.running = False
self.connection.quit("Adios!")
def privmsg(self, target, message):
self.connection.privmsg(target, message.strip())
def on_nicknameinuse(self, connection, event):
connection.nick(connection.get_nickname() + "_")
def on_welcome(self, connection, event):
self.connection = connection
connection.join(
','.join([channel for channel in self.config['CHANNELS']]))
def on_pubmsg(self, connection, event):
if (event.target in self.config['CHANNELS']):
with self.thread_lock:
message = event.arguments[0].strip()
message = "**<{:s}>** {:s}".format(
re.sub(r"(]|-|\\|[`*_{}[()#+.!])", r'\\\1', event.source.nick), message)
self.discord.privmsg(
self.config['CHANNELS'][event.target], message)
def on_action(self, connection, event):
if (event.target in self.config['CHANNELS']):
with self.thread_lock:
message = event.arguments[0].strip()
message = "* {:s} {:s}".format(
re.sub(r"(]|-|\\|[`*_{}[()#+.!])", r'\\\1', event.source.nick), message)
self.discord.privmsg(
self.config['CHANNELS'][event.target], message)
def run(self):
self.start()
if self.running:
self.running = False
ircd = IRC({"irc": self.config})
ircd.set_discord(self.discord)
self.discord.set_irc(ircd)
ircd.set_thread_lock(self.thread_lock)
ircd.run()