-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc-bot.py
executable file
·216 lines (177 loc) · 6.75 KB
/
irc-bot.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
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
#! /usr/bin/env python3
""" IRC protocol module for Acronymph
loosely based on Joel Rosdahl's irc.bot example
bot class implements and gets these messages from the game class:
self.send_channel_message(channel, message)
self.send_private_message(nick, message)
It should talk to the game like this:
self.g = game.game(channel, self) # initialize "g" as game object
self.g.start() # Start the acromania game
if self.g.running:
self.send_channel_message(channel, 'The game is already running. Please finish this one or !stopacro before starting a new game.')
"""
import logging
import time
import ssl
import irc.bot
import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
import acronymph
import mysecrets
class DummyGame():
def __init__(self):
self.running = False
class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
self.log = logging.getLogger(__name__)
self.log.setLevel(logging.DEBUG)
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.g = DummyGame()
self.connection.add_global_handler("error", self.on_error)
def send_channel_message(self, channel, message):
print("To " + channel + ": " + message)
self.connection.privmsg(self.channel, message)
def send_private_message(self, target, message):
print("To " + target + ": " + message)
self.connection.privmsg(target, message)
def on_nicknameinuse(self, c, e):
print(e)
c.nick(c.get_nickname() + "_")
def nickserv_identify(self):
self.send_private_message('nickserv', mysecrets.nickservRegString)
time.sleep(2)
def on_error(self, c, e):
print(e)
def on_mode(self, c, e):
print(e)
def on_kick(self, c, e):
print(e)
def on_quit(self, c, e):
print(e)
def on_welcome(self, c, e):
self.nickserv_identify()
time.sleep(2)
c.join(self.channel)
def on_privmsg(self, c, e):
print(e)
sender = e.source.nick
msg = e.arguments[0]
if self.g.running: # Is the game running? If so...
if self.g.takingacros: # Are we waiting for submissions?
self.g.take_acro(sender, msg) # pass nick/submission to handler
elif self.g.takingvotes: # Are we waiting for votes instead?
self.g.take_vote(sender, msg) # pass nick/vote to handler
else: # We must be inbetween rounds...
self.send_private_message(sender, 'Voting is over. Please wait for the next round to begin.')
else: self.send_private_message(sender, 'The game is currently not running.')
#self.do_command(e, e.arguments[0])
def on_pubmsg(self, c, e):
print(e)
#a = e.arguments[0].split(":", 1)
a = e.arguments[0]
print(a)
#if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(
# self.connection.get_nickname()
#):
#self.do_command(e, a[1].strip())
self.do_command(e, a)
#msg = e.capitalize()
#msg = e.arguments[0].split(":", 1)
return
def on_dccmsg(self, c, e):
# non-chat DCC messages are raw bytes; decode as text
text = e.arguments[0].decode('utf-8')
c.privmsg("You said: " + text)
def on_dccchat(self, c, e):
if len(e.arguments) != 2:
return
args = e.arguments[1].split()
if len(args) == 4:
try:
address = ip_numstr_to_quad(args[2])
port = int(args[3])
except ValueError:
return
self.dcc_connect(address, port)
def do_command(self, e, cmd):
nick = e.source.nick
c = self.connection
channel = self.channel
#if cmd == "disconnect":
# self.disconnect()
#elif cmd == "die":
# self.die()
#elif cmd == "stats":
# for chname, chobj in self.channels.items():
# c.notice(nick, "--- Channel statistics ---")
# c.notice(nick, "Channel: " + chname)
# users = sorted(chobj.users())
# c.notice(nick, "Users: " + ", ".join(users))
# opers = sorted(chobj.opers())
# c.notice(nick, "Opers: " + ", ".join(opers))
# voiced = sorted(chobj.voiced())
# c.notice(nick, "Voiced: " + ", ".join(voiced))
#elif cmd == "dcc":
# dcc = self.dcc_listen()
# c.ctcp(
# "DCC",
# nick,
# "CHAT chat %s %d"
# % (ip_quad_to_numstr(dcc.localaddress), dcc.localport),
# )
if cmd == "!help":
print("Help")
self.send_channel_message(self.channel, 'Commands include: !acro, !stopacro')
#self.send_private_message(nick, 'Commands include: !acro, !stopacro')
#else:
# c.notice(nick, "Not understood: " + cmd)
if cmd == "!acro":
try:
if self.g.running:
self.send_channel_message(channel, 'The game is already running. Please finish this one or !stopacro before starting a new game.')
else:
self.g = acronymph.Game(channel, self) # initialize "g" as game object
self.g.start() # Start the acromania game
except AttributeError:
self.g = acronymph.game(channel, self) # initialize "g" as game object
self.g.start() # Start the acromania game
elif cmd == "!stopacro":
try:
if self.g.running:
self.g.end_game()
else:
self.send_channel_message(channel, 'The game is not running.')
except AttributeError:
self.send_channel_message(channel, 'The game is not running')
def init_ssl():
pass
def main():
import sys
#if len(sys.argv) != 4:
if len(sys.argv) < 4:
print("Usage: irc-bot.py <server[:port]> <channel> <nickname> <-ssl>")
sys.exit(1)
if len(sys.argv) == 5:
if sys.argv[4] == '-ssl':
sslEnabled = True
print("ssl enabled")
init_ssl()
else:
sslEnabled = False
s = sys.argv[1].split(":", 1)
server = s[0]
if len(s) == 2:
try:
port = int(s[1])
except ValueError:
print("Error: Erroneous port.")
sys.exit(1)
else:
port = 6667
channel = sys.argv[2]
nickname = sys.argv[3]
bot = Bot(channel, nickname, server, port)
bot.start()
if __name__ == "__main__":
main()