/[shmookey]/killbot/killbot.py


UCC Code Repository

Diff of /killbot/killbot.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 121 by shmookey, Sun Sep 13 03:06:49 2009 UTC revision 122 by shmookey, Wed Sep 16 01:39:09 2009 UTC
# Line 6  Line 6 
6      Distributed under an MIT-style license - see the LICENSE file for details.      Distributed under an MIT-style license - see the LICENSE file for details.
7  '''  '''
8    
9  import re, sys  import re, os, sys
10  import easybot  import conf, easybot
11    
12  BOT_NAME = "kb1"  BOT_NAME = conf.username
13    
14  pfile = open ("kicklist", "r")  patterns = {}
15  patterns = []  
16  lines = pfile.read ().split ("\n")  def load_channel (channel):
17  for line in lines:          patterns[channel] = []
18          if not line == "":          if os.access ("channels/" + channel, os.F_OK | os.R_OK):
19                  patterns.append ( (line,re.compile (line, re.IGNORECASE)) )                  pattern_file = open ("channels/" + channel, "r")
20  pfile.close ()                  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  def msg (chobj, channel, msg_from, msg, is_oper):  def msg (chobj, channel, msg_from, msg, is_oper):
31          words = msg.split (":")          words = msg.split (":")
32          if len (words) > 1:          if len (words) > 1:
33                  if words [0] == BOT_NAME:                  if words [0] == BOT_NAME:
34                          responses = process_command (words[1], msg_from, is_oper)                          responses = process_command (words[1], channel, msg_from, is_oper, chobj)
35                          for response in responses:                          for response in responses:
36                                  chobj.privmsg (channel, response)                                  chobj.privmsg (channel, response)
37                          return                          return
38    
39          for pattern in patterns:          for pattern in patterns[channel]:
40                  if re.search (pattern[1], msg):                  if re.search (pattern[1], msg):
41                          print "Kicking " + msg_from + " for violating kickable expression (\"" + pattern[0] + "\")"                          print "Kicking " + msg_from + " for violating kickable expression (\"" + pattern[0] + "\")"
42                          chobj.kick (channel, msg_from, "Violating kickable expression (\"" + pattern[0] + "\")")                          chobj.kick (channel, msg_from, "Violating kickable expression (\"" + pattern[0] + "\")")
43    
44  def process_command (cmd, msg_from, is_oper):  def process_command (cmd, channel, msg_from, is_oper, connection):
45          words = cmd [1:].split (" ", 1)          words = cmd [1:].split (" ", 1)
46          try:          try:
47                  if words[0] == "help":                  if words[0] == "help":
# Line 45  def process_command (cmd, msg_from, is_o Line 53  def process_command (cmd, msg_from, is_o
53                          return responses                          return responses
54                  if words[0] == "show":                  if words[0] == "show":
55                          responses = ["Kickable Expressions:"]                          responses = ["Kickable Expressions:"]
56                          for i, pattern in enumerate (patterns):                          for i, pattern in enumerate (patterns[channel]):
57                                  responses.append (str(i) + ". " + pattern[0])                                  responses.append (str(i) + ". " + pattern[0])
58                          return responses                          return responses
59                  elif words[0] == "add" and len (words) > 1:                  elif words[0] == "add" and len (words) > 1:
# Line 54  def process_command (cmd, msg_from, is_o Line 62  def process_command (cmd, msg_from, is_o
62                                  return responses                                  return responses
63                          pattern = words[1]                          pattern = words[1]
64                          try:                          try:
65                                  patterns.append ( (pattern,re.compile (pattern, re.IGNORECASE)) )                                  patterns[channel].append ( (pattern,re.compile (pattern, re.IGNORECASE)) )
66                          except:                          except:
67                                  responses = ["There was a problem compiling your expression."]                                  responses = ["There was a problem compiling your expression."]
68                                  return responses                                  return responses
69                          pfile = open ("kicklist", "a+")                          pattern_file = open ("channels/" + channel, "a+")
70                          pfile.write (pattern + "\n")                          pattern_file.write ("\n" + pattern)
71                          pfile.close ()                          pattern_file.close ()
72                          responses = ["Added \"" + pattern + "\" to the list of kickable expressions."]                          responses = ["Added \"" + pattern + "\" to the list of kickable expressions."]
73                          responses.append ("Use \"kb: remove " + str (len (patterns)-1) + "\" to remove.")                          responses.append ("Use \"kb: remove " + str (len (patterns[channel])-1) + "\" to remove.")
74                          return responses                          return responses
75                  elif words[0] == "remove" and len(words) > 1:                  elif words[0] == "remove" and len(words) > 1:
76                          if not is_oper:                          if not is_oper:
77                                  responses = [msg_from + ": I only obey channel operators."]                                  responses = [msg_from + ": I only obey channel operators."]
78                                  return responses                                  return responses
79                          pnum = int (words[1])                          pnum = int (words[1])
80                          pattern = patterns [pnum]                          pattern = patterns[channel] [pnum]
81                          patterns.pop (pnum)                          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                          responses = ["Removed pattern " + str (pnum) + " (\"" + pattern[0] + "\") from kicklist."]                          responses = ["Removed pattern " + str (pnum) + " (\"" + pattern[0] + "\") from kicklist."]
88                          return responses                          return responses
89                    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                  else:                  else:
115                          responses = [msg_from + ": I don't understand."]                          responses = [msg_from + ": I don't understand."]
116                          return responses                          return responses
# Line 80  def process_command (cmd, msg_from, is_o Line 118  def process_command (cmd, msg_from, is_o
118                  responses = [msg_from + ": What?"]                  responses = [msg_from + ": What?"]
119                  return responses                  return responses
120    
121    def on_welcome (c):
122  bot = easybot.EasyBot ("#shmookey",BOT_NAME, "mussel")          for channel in patterns:
123                    c.join (channel)
124    
125    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  bot.message_hook = msg  bot.message_hook = msg
132    bot.welcome_hook = on_welcome
133  bot.start ()  bot.start ()

Legend:
Removed from v.121  
changed lines
  Added in v.122

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26