-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathircd.py
More file actions
230 lines (202 loc) Β· 7.96 KB
/
Copy pathircd.py
File metadata and controls
230 lines (202 loc) Β· 7.96 KB
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
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
import asyncio
import irc.bot
import os
import ssl
class IRC(irc.bot.SingleServerIRCBot):
running = True
config = None
connection = None
game = None
auth = None
def __init__(self, config, game, auth):
ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
irc.client.ServerConnection.buffer_class.encoding = "latin-1"
irc.bot.SingleServerIRCBot.__init__(
self,
[(config["IRC_SERVER"], config["PORT"])],
config["NICK"],
"SwiftRPG",
connect_factory=ssl_factory,
)
self.config = config
self.game = game
self.game.set_irc_privmsg(self.privmsg)
self.auth = auth
def close(self):
self.running = False
self.connection.quit("Adios!")
def privmsg(self, target, message):
if 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
if len(self.config["PASSWORD"]):
self.connection.privmsg(
"NickServ", "IDENTIFY {}".format(self.config["PASSWORD"])
)
connection.join(",".join([channel for channel in self.config["CHANNELS"]]))
def on_pubmsg(self, connection, event):
if event.target in self.config["CHANNELS"]:
message = event.arguments[0].strip()
split = message.split()
print("[IRC] [{}] ({}) {}".format(event.target, event.source.nick, message))
if (
message.startswith("+")
or message.startswith("-")
or message.startswith("!")
or message.startswith("@")
or message.startswith(".")
):
if split[0][1:] == "help":
self.privmsg(
event.target, "{}/help".format(self.config["API_HOSTNAME"])
)
return
elif split[0][1:] == "login":
if len(split) != 2:
self.privmsg(
event.target,
"Syntax: {} <token from {}/user>".format(
split[0], os.getenv("API_HOSTNAME")
),
)
return
response = self.auth.login(
self.game,
self.privmsg,
event.target,
event.source.nick,
split[1],
)
if response and "name" in response:
self.privmsg(
event.target,
"[{}] π Successfully logged in".format(
response.get("name"),
),
)
else:
self.privmsg(
event.target,
"[{}] π Error: invalid token".format(event.source.nick),
)
return
elif split[0][1:] == "logout":
if self.auth.check(event.source.nick):
self.auth.logout(event.source.nick)
self.privmsg(event.target, "Logout successful!")
else:
self.privmsg(event.target, "You are not logged in.")
return
elif split[0][1:] == "loggedin":
if self.auth.check(event.source.nick):
self.privmsg(event.target, "Logged in!")
else:
self.privmsg(event.target, "Not currently logged in.")
return
elif not self.auth.check(event.source.nick):
self.privmsg(event.target, "You are not logged in.")
return
print(
"[IRC] [{}] CMD DETECTED: ({}) {}".format(
event.target, event.source.nick, message
)
)
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(
self.game.command(
self.auth,
self.privmsg,
event.target,
event.source.nick,
message,
)
)
finally:
loop.close()
def on_privmsg(self, connection, event):
message = event.arguments[0].strip()
split = message.split()
print("[IRC] [{}] ({}) {}".format(event.target, event.source.nick, message))
if (
message.startswith("+")
or message.startswith("-")
or message.startswith("!")
or message.startswith("@")
or message.startswith(".")
):
if split[0][1:] == "login":
if len(split) != 2:
self.privmsg(
event.source.nick,
"Syntax: {} <token from {}/user>".format(
split[0], os.getenv("API_HOSTNAME")
),
)
return
response = self.auth.login(
self.game,
self.privmsg,
event.target,
event.source.nick,
split[1],
)
if response and "name" in response:
self.privmsg(
event.source.nick,
"[{}] π Successfully logged in".format(
response.get("name"),
),
)
else:
self.privmsg(
event.source.nick,
"[{}] π Error: invalid token".format(event.source.nick),
)
return
elif split[0][1:] == "register":
self.privmsg(
event.source.nick,
"Syntax: {} <token from {}/user>".format(
split[0], os.getenv("API_HOSTNAME")
),
)
elif split[0][1:] == "logout":
if self.auth.check(event.source.nick):
self.auth.logout(event.source.nick)
self.privmsg(event.source.nick, "Logout successful!")
else:
self.privmsg(event.source.nick, "You are not logged in.")
elif split[0][1:] == "loggedin":
if self.auth.check(event.source.nick):
self.privmsg(event.source.nick, "Logged in!")
else:
self.privmsg(event.source.nick, "Not currently logged in.")
elif split[0][1:] == "help":
self.privmsg(
event.source.nick, "{}/help".format(self.config["API_HOSTNAME"])
)
def on_nick(self, nick, event):
old_name = event.source.nick
new_name = event.target
if self.auth.check(old_name):
self.auth.rename(old_name, new_name)
def on_quit(self, quit, event):
if self.auth.check(event.source.nick):
self.auth.logout(event.source.nick)
def on_kick(self, kick, event):
print(event.arguments[0])
if event.target in self.config["CHANNELS"] and self.auth.check(
event.arguments[0]
):
self.auth.logout(event.arguments[0])
def run(self):
self.start()
if self.running:
self.running = False
ircd = IRC({"irc": self.config})
ircd.run()