-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_IRC_bot.py
85 lines (63 loc) · 2.23 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
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
from twisted.internet import defer
class IRCLogger(irc.IRCClient):
#logfile = file('/tmp/hanirc.txt', 'a+')
nickname = 'dice_bot'
CHANNEL = '#bottest'
def signedOn(self):
self.join('#bottest')
def joined(self, channel):
"""This will get called when the bot joins the channel."""
print channel
def got_names(self,nicklist):
print nicklist
def callAll(self, nicklist, message, user):
#self.sendline(' '.join(nicklist))
self.msg(self.CHANNEL, user.split('!')[0]+":"+message +" "+ ' '.join(nicklist))
def privmsg(self, user, channel, message):
print "[got msg]%s" % message
print "2", self.CHANNEL
#self.msg(message, self.CHANNEL)
if message.startswith("callAll"):
print "accepted"
splited = message.split(" ",1)[1:]
if len(splited) > 0 :
msg = splited[0]
else :
msg = ""
self.names("#bottest").addCallback(self.callAll, msg, user)
#self.logfile.write(" %s said %s \n" % ( user.split('!')[0], message ))
#self.logfile.flush()
def __init__(self, *args, **kwargs):
self._namescallback = {}
def names(self, channel):
channel = channel.lower()
d = defer.Deferred()
if channel not in self._namescallback:
self._namescallback[channel] = ([], [])
self._namescallback[channel][0].append(d)
self.sendLine("names %s" % channel)
return d
def irc_RPL_NAMREPLY(self, prefix, params):
channel = params[2].lower()
nicklist = params[3].split(' ')
if channel not in self._namescallback:
return
n = self._namescallback[channel][1]
n += nicklist
def irc_RPL_ENDOFNAMES(self, prefix, params):
channel = params[1].lower()
if channel not in self._namescallback:
return
callbacks, namelist = self._namescallback[channel]
for cb in callbacks:
cb.callback(namelist)
del self._namescallback[channel]
def main():
f = protocol.ReconnectingClientFactory()
f.protocol = IRCLogger
reactor.connectTCP('apink.hanirc.org', 6667, f)
reactor.run()
if __name__ == '__main__':
main()