-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
215 lines (171 loc) · 7.44 KB
/
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
#Matthew Spain, Kevin Dodd
#derived from devshed example at http://www.devshed.com/c/a/Python/IRC-on-a-Higher-Level/1/
import irclib
import ConfigParser
import random
import pickle
import sys
import string
import datetime
import threading
irclib.DEBUG = True;
class Connection:
def __init__(self, port = 6667, network = 'irc.freenode.net'):
self.port = port
self.network = network
class IRCBot:
def __init__(self):
self.connection = Connection()
self.channels = set([])#['##tokyo-3']
self.nick = 'cslbot'
self.name = 'tjcsl_bot'
self.irc = irclib.IRC()
self.addHandlers()
self.server = self.irc.server()
now = datetime.datetime.now()
self.connection = "temporary"
self.main_thread = threading.Thread(target=self.irc.process_forever)
self.check_period = 2.0
self.checked = True
self.timer = threading.Timer(self.check_period, self.process)
#self.server.join (self.channels[0])
def connect(self, port = 6667, network = 'irc.freenode.net'):
self.connection = Connection(port, network)
print self.connection.network, self.connection.port, self.nick, self.name
self.server.connect(self.connection.network, self.connection.port, self.nick, ircname = self.name)
def joinChannel(self, channel):
self.channels.add(channel)
self.server.join(channel)
#Handlers
# Generic echo handler (space added)
# This is used to output some initial information from the server
def handleEcho (self, connection, event):
print
print ' '.join (event.arguments())
# Handle private messages
def handlePrivMessage (self, connection, event):
line = event.arguments()[0]
connection.privmsg(event.source().split('!')[0], 'Hey, I heard you say that!')
return
makeResponse(event.source().split('!')[0], connection, line)
def handlePubMessage(self, connection, event):
connection.privmsg(event.target(), 'Hey, I heard you say that!')
return
speaker = event.source().split('!')[0]
makeResponse(speaker, connection, event.arguments()[0], event.target())
# Generic echo handler (no space added)
def handleNoSpace (self, connection, event):
print ' '.join (event.arguments())
def addHandlers(self):
self.irc.add_global_handler('privsmg', self.handlePrivMessage) # Private message
self.irc.add_global_handler('pubmsg', self.handlePubMessage) # Public Message
self.irc.add_global_handler ('welcome', self.handleEcho) # Welcome message
self.irc.add_global_handler ('yourhost', self.handleEcho) # Host message
self.irc.add_global_handler ('created', self.handleEcho) # Server creation message
self.irc.add_global_handler ('myinfo', self.handleEcho) # "My info" message
self.irc.add_global_handler ('featurelist', self.handleEcho) # Server feature list
self.irc.add_global_handler ('luserclient', self.handleEcho) # User count
self.irc.add_global_handler ('luserop', self.handleEcho) # Operator count
self.irc.add_global_handler ('luserchannels', self.handleEcho) # Channel count
self.irc.add_global_handler ('luserme', self.handleEcho) # Server client count
self.irc.add_global_handler ('n_local', self.handleEcho) # Server client count/maximum
self.irc.add_global_handler ('n_global', self.handleEcho) # Network client count/maximum
self.irc.add_global_handler ('luserconns', self.handleEcho) # Record client count
self.irc.add_global_handler ('luserunknown', self.handleEcho) # Unknown connections
self.irc.add_global_handler ('motdstart', self.handleEcho) # Message of the day (start)
self.irc.add_global_handler ('motd', self.handleNoSpace) # Message of the day
self.irc.add_global_handler ('edofmotd', self.handleEcho) # Message of the day (end)
self.irc.add_global_handler ('namreply', self.handleNoSpace) # Channel name list
self.irc.add_global_handler ('endofnames', self.handleNoSpace) # Channel name list (end)
def process(self):
def run(self):
self.main_thread.start()
def stop(self):
self.main_thread.join()
def startRuning(self):
try:
# Jump into an infinite loop
self.irc.process_forever()
except KeyboardInterrupt:
print 'ending program'
##local data not in classes
#server = 'temporary';
#bot = IRCBot()
#funcdict = {}
#funchelp = {}
## Commands
##Handlers
## Generic echo handler (space added)
## This is used to output some initial information from the server
#def handleEcho (connection, event):
#print
#print ' '.join (event.arguments())
## Handle private messages
#def handlePrivMessage (connection, event):
#line = event.arguments()[0]
#connection.privmsg(event.source().split('!')[0], 'Hey, I heard you say that!')
#return
#makeResponse(event.source().split('!')[0], connection, line)
#def handlePubMessage(connection, event):
#connection.privmsg(event.target(), 'Hey, I heard you say that!')
#return
#local data not in classes
#server = 'temporary';
#bot = IRCBot()
#funcdict = {}
#funchelp = {}
## Commands
##Handlers
## Generic echo handler (space added)
## This is used to output some initial information from the server
#def handleEcho (connection, event):
#print
#print ' '.join (event.arguments())
## Handle private messages
#def handlePrivMessage (connection, event):
#line = event.arguments()[0]
#connection.privmsg(event.source().split('!')[0], 'Hey, I heard you say that!')
#return
#makeResponse(event.source().split('!')[0], connection, line)
#def handlePubMessage(connection, event):
#connection.privmsg(event.target(), 'Hey, I heard you say that!')
#return
#speaker = event.source().split('!')[0]
#makeResponse(speaker, connection, event.arguments()[0], event.target())
## Generic echo handler (no space added)
#def handleNoSpace (connection, event):
#print ' '.join (event.arguments())
## Create an IRC object
#irc = irclib.IRC()
## Register Handlers
#irc.add_global_handler('privmsg', handlePrivMessage) # Private message
#irc.add_global_handler('pubmsg', handlePubMessage) # Public Message
#irc.add_global_handler ('welcome', handleEcho) # Welcome message
#irc.add_global_handler ('yourhost', handleEcho) # Host message
#irc.add_global_handler ('created', handleEcho) # Server creation message
#irc.add_global_handler ('myinfo', handleEcho) # "My info" message
#irc.add_global_handler ('featurelist', handleEcho) # Server feature list
#irc.add_global_handler ('luserclient', handleEcho) # User count
#irc.add_global_handler ('luserop', handleEcho) # Operator count
#irc.add_global_handler ('luserchannels', handleEcho) # Channel count
#irc.add_global_handler ('luserme', handleEcho) # Server client count
#irc.add_global_handler ('n_local', handleEcho) # Server client count/maximum
#irc.add_global_handler ('n_global', handleEcho) # Network client count/maximum
#irc.add_global_handler ('luserconns', handleEcho) # Record client count
#irc.add_global_handler ('luserunknown', handleEcho) # Unknown connections
#irc.add_global_handler ('motdstart', handleEcho) # Message of the day (start)
#irc.add_global_handler ('motd', handleNoSpace) # Message of the day
#irc.add_global_handler ('edofmotd', handleEcho) # Message of the day (end)
#irc.add_global_handler ('namreply', handleNoSpace) # Channel name list
#irc.add_global_handler ('endofnames', handleNoSpace) # Channel name list (end)
## Create a server object, connect and join the channel
#server = irc.server()
#connection = Connection()
#print connection.network, connection.port, bot.nick, bot.name
#server.connect(connection.network, connection.port, bot.nick, ircname = bot.name)
#server.join (bot.channels[0])
#try:
## Jump into an infinite loop
#irc.process_forever()
#except KeyboardInterrupt:
#print 'ending program'