/[shmookey]/killbot/killbot.py


UCC Code Repository

Contents of /killbot/killbot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 122 - (hide annotations) (download) (as text)
Wed Sep 16 01:39:09 2009 UTC (12 years, 2 months ago) by shmookey
File MIME type: text/x-python
File size: 4983 byte(s)
Added multi-channel support.

1 shmookey 121 #!/usr/bin/python
2     ''' killbot.py
3     Kicks users from an IRC channel when they send banned expressions.
4    
5     Written by Luke Williams <[email protected]>
6     Distributed under an MIT-style license - see the LICENSE file for details.
7     '''
8    
9 shmookey 122 import re, os, sys
10     import conf, easybot
11 shmookey 121
12 shmookey 122 BOT_NAME = conf.username
13 shmookey 121
14 shmookey 122 patterns = {}
15 shmookey 121
16 shmookey 122 def load_channel (channel):
17     patterns[channel] = []
18     if os.access ("channels/" + channel, os.F_OK | os.R_OK):
19     pattern_file = open ("channels/" + channel, "r")
20     for line in pattern_file.read().split("\n"):
21     if (not line == "") and (not line[0] == ";"):
22     patterns[channel].append( (line,re.compile(line,re.IGNORECASE)) )
23     pattern_file.close ()
24     else:
25     os.mknod ("channels/" + channel)
26     pattern_file = open ("channels/" + channel, "w+")
27     pattern_file.write ("; Autogenerated by killbot")
28     pattern_file.close ()
29    
30 shmookey 121 def msg (chobj, channel, msg_from, msg, is_oper):
31     words = msg.split (":")
32     if len (words) > 1:
33     if words [0] == BOT_NAME:
34 shmookey 122 responses = process_command (words[1], channel, msg_from, is_oper, chobj)
35 shmookey 121 for response in responses:
36     chobj.privmsg (channel, response)
37     return
38    
39 shmookey 122 for pattern in patterns[channel]:
40 shmookey 121 if re.search (pattern[1], msg):
41     print "Kicking " + msg_from + " for violating kickable expression (\"" + pattern[0] + "\")"
42     chobj.kick (channel, msg_from, "Violating kickable expression (\"" + pattern[0] + "\")")
43    
44 shmookey 122 def process_command (cmd, channel, msg_from, is_oper, connection):
45 shmookey 121 words = cmd [1:].split (" ", 1)
46     try:
47     if words[0] == "help":
48     responses = ["I kick people when they act like idiots! Channel operators can define what idiotic behaviour looks like with Python-style regex by telling me:"]
49     responses.append (BOT_NAME + ": add kickable_pattern_here")
50     responses.append ("Anyone can get the list of kickable expressions by asking me with \"" + BOT_NAME + ": show\"")
51     responses.append ("Channel operators can remove expressions from the kicklist with \"" + BOT_NAME + ": remove list_index\"")
52     responses.append ("I was originally written in 2009 by shmookey for #ucc on irc.ucc.asn.au")
53     return responses
54     if words[0] == "show":
55     responses = ["Kickable Expressions:"]
56 shmookey 122 for i, pattern in enumerate (patterns[channel]):
57 shmookey 121 responses.append (str(i) + ". " + pattern[0])
58     return responses
59     elif words[0] == "add" and len (words) > 1:
60     if not is_oper:
61     responses = [msg_from + ": I only obey channel operators."]
62     return responses
63     pattern = words[1]
64     try:
65 shmookey 122 patterns[channel].append ( (pattern,re.compile (pattern, re.IGNORECASE)) )
66 shmookey 121 except:
67     responses = ["There was a problem compiling your expression."]
68     return responses
69 shmookey 122 pattern_file = open ("channels/" + channel, "a+")
70     pattern_file.write ("\n" + pattern)
71     pattern_file.close ()
72 shmookey 121 responses = ["Added \"" + pattern + "\" to the list of kickable expressions."]
73 shmookey 122 responses.append ("Use \"kb: remove " + str (len (patterns[channel])-1) + "\" to remove.")
74 shmookey 121 return responses
75     elif words[0] == "remove" and len(words) > 1:
76     if not is_oper:
77     responses = [msg_from + ": I only obey channel operators."]
78     return responses
79     pnum = int (words[1])
80 shmookey 122 pattern = patterns[channel] [pnum]
81     patterns[channel].pop (pnum)
82     pattern_file = open ("channels/" + channel, "r")
83     new_content = "\n".join ([line for line in pattern_file.read().split("\n") if not line == pattern[0]])
84     pattern_file = open ("channels/" + channel, "w")
85     pattern_file.write (new_content)
86     pattern_file.close ()
87 shmookey 121 responses = ["Removed pattern " + str (pnum) + " (\"" + pattern[0] + "\") from kicklist."]
88     return responses
89 shmookey 122 elif words[0] == "join" and len(words) > 1:
90     if not is_oper:
91     responses = [msg_from + ": I only obey channel operators."]
92     return responses
93     new_channel = words[1]
94     load_channel (new_channel)
95     channel_file = open ("channels.conf", "a+")
96     channel_file.write ("\n" + new_channel)
97     channel_file.close ()
98     connection.join (new_channel)
99     responses = ["OK!"]
100     return responses
101     elif words[0] == "leave":
102     if not is_oper:
103     responses = [msg_from + ": I only obey channel operators."]
104     return responses
105     channel_file = open ("channels.conf", "r")
106     new_content = "\n".join([line for line in channel_file.read().split("\n") if not line == channel])
107     print new_content
108     channel_file.close ()
109     channel_file = open ("channels.conf", "w")
110     channel_file.write (new_content)
111     channel_file.close ()
112     connection.part (channel)
113     return []
114 shmookey 121 else:
115     responses = [msg_from + ": I don't understand."]
116     return responses
117     except:
118     responses = [msg_from + ": What?"]
119     return responses
120    
121 shmookey 122 def on_welcome (c):
122     for channel in patterns:
123     c.join (channel)
124 shmookey 121
125 shmookey 122 channel_file = open ("channels.conf", "r")
126     for line in channel_file.read().split("\n"):
127     if (not line == "") and (not line[0] == ";"):
128     load_channel (line)
129     channel_file.close ()
130     bot = easybot.EasyBot (BOT_NAME, conf.server, port=conf.port)
131 shmookey 121 bot.message_hook = msg
132 shmookey 122 bot.welcome_hook = on_welcome
133 shmookey 121 bot.start ()

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26