/[shmookey]/killbot/killbot.py


UCC Code Repository

Contents of /killbot/killbot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (hide annotations) (download) (as text)
Sun Sep 13 03:06:49 2009 UTC (12 years, 2 months ago) by shmookey
File MIME type: text/x-python
File size: 3078 byte(s)
Initial commit of killbot.

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     import re, sys
10     import easybot
11    
12     BOT_NAME = "kb1"
13    
14     pfile = open ("kicklist", "r")
15     patterns = []
16     lines = pfile.read ().split ("\n")
17     for line in lines:
18     if not line == "":
19     patterns.append ( (line,re.compile (line, re.IGNORECASE)) )
20     pfile.close ()
21    
22     def msg (chobj, channel, msg_from, msg, is_oper):
23     words = msg.split (":")
24     if len (words) > 1:
25     if words [0] == BOT_NAME:
26     responses = process_command (words[1], msg_from, is_oper)
27     for response in responses:
28     chobj.privmsg (channel, response)
29     return
30    
31     for pattern in patterns:
32     if re.search (pattern[1], msg):
33     print "Kicking " + msg_from + " for violating kickable expression (\"" + pattern[0] + "\")"
34     chobj.kick (channel, msg_from, "Violating kickable expression (\"" + pattern[0] + "\")")
35    
36     def process_command (cmd, msg_from, is_oper):
37     words = cmd [1:].split (" ", 1)
38     try:
39     if words[0] == "help":
40     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:"]
41     responses.append (BOT_NAME + ": add kickable_pattern_here")
42     responses.append ("Anyone can get the list of kickable expressions by asking me with \"" + BOT_NAME + ": show\"")
43     responses.append ("Channel operators can remove expressions from the kicklist with \"" + BOT_NAME + ": remove list_index\"")
44     responses.append ("I was originally written in 2009 by shmookey for #ucc on irc.ucc.asn.au")
45     return responses
46     if words[0] == "show":
47     responses = ["Kickable Expressions:"]
48     for i, pattern in enumerate (patterns):
49     responses.append (str(i) + ". " + pattern[0])
50     return responses
51     elif words[0] == "add" and len (words) > 1:
52     if not is_oper:
53     responses = [msg_from + ": I only obey channel operators."]
54     return responses
55     pattern = words[1]
56     try:
57     patterns.append ( (pattern,re.compile (pattern, re.IGNORECASE)) )
58     except:
59     responses = ["There was a problem compiling your expression."]
60     return responses
61     pfile = open ("kicklist", "a+")
62     pfile.write (pattern + "\n")
63     pfile.close ()
64     responses = ["Added \"" + pattern + "\" to the list of kickable expressions."]
65     responses.append ("Use \"kb: remove " + str (len (patterns)-1) + "\" to remove.")
66     return responses
67     elif words[0] == "remove" and len(words) > 1:
68     if not is_oper:
69     responses = [msg_from + ": I only obey channel operators."]
70     return responses
71     pnum = int (words[1])
72     pattern = patterns [pnum]
73     patterns.pop (pnum)
74     responses = ["Removed pattern " + str (pnum) + " (\"" + pattern[0] + "\") from kicklist."]
75     return responses
76     else:
77     responses = [msg_from + ": I don't understand."]
78     return responses
79     except:
80     responses = [msg_from + ": What?"]
81     return responses
82    
83    
84     bot = easybot.EasyBot ("#shmookey",BOT_NAME, "mussel")
85     bot.message_hook = msg
86     bot.start ()

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26