瀏覽代碼

refactor on_mode

tBKwtWS 7 年之前
父節點
當前提交
483b1ffe17
共有 3 個文件被更改,包括 39 次插入40 次删除
  1. 8 0
      common/do_everything_to.py
  2. 8 1
      events/common.py
  3. 23 39
      events/on_mode.py

+ 8 - 0
common/do_everything_to.py

@@ -19,3 +19,11 @@ def join(self, connection, channel, key=False):
 def unban(connection, channel, user, mask):
     ChanServ.unban(connection, channel, user)
     connection.mode(connection, "-b " + mask)
+
+def ban(connection, channel, user, mask, reason):
+    ChanServ.ban(connection, channel, user, reason)
+    connection.mode(connection, "+b " + mask)
+
+def kick(connection, channel, user, reason):
+    ChanServ.kick(connection, channel, user, reason)
+    connection.kick(channel, user, reason)

+ 8 - 1
events/common.py

@@ -1,5 +1,5 @@
 from common import userstatus
-class Protectees:
+class Protectees():
     def update(self, nick, user, host):
         if nick in self.protectees: # On record.
             if userstatus.atleast_halfop(self, user, self.homechannel) or nick == self.connection.get_nickname():   # Update. Is atleast halfop or bot itself.
@@ -9,3 +9,10 @@ class Protectees:
         else:   # Append.
             if userstatus.atleast_halfop(self, user, self.homechannel) or nick == self.connection.get_nickname():   # Update. Is atleast halfop or bot itself.
                 self.protectees[nick] = {'ident': nick + "!" + user + "@" + host}
+
+class Aggressiveness():
+    def retalliation_reason(self, connection, protectee, behaviour):
+        if protectee == connection.get_nickname():  # Bot itself.
+             return "Aggression channel function = " + behaviour + ": Self defense."
+        else:
+            return "Aggression channel function = " + behaviour + ": " + protectee + " is atlast halfop in " + self.homechannel + "."

+ 23 - 39
events/on_mode.py

@@ -1,6 +1,9 @@
 import fnmatch
 from irc.modes import parse_channel_modes
 from common.networkservices import ChanServ
+from common import do_everything_to
+from events.common import Aggressiveness
+
 bold = "\x02"
 italic = "\x1D"
 underline = "\x1F"
@@ -34,26 +37,28 @@ def process_event(self, connection, event):
                 self.db.run("UPDATE channels SET key='" + mode[2] + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
             else:   # Key removed.
                 self.db.run("UPDATE channels SET key=NULL WHERE name='" + event.target + "' AND network='" + self.network + "'")
-    
-    # Do not revert the actions of retalliate upon one self.
-    if event.source.nick == connection.get_nickname():
-        return
-    
+
     # React.
     behaviour = self.db.one("SELECT aggressiveness FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
     for mode in modes:
+        
+        # Unban if bot is banned from home channel
+        if mode[1] == "b" and fnmatch.fnmatch(self.protectees[connection.get_nickname()]['ident'], mode[2]) and mode[0] == "+" and event.target == self.homechannel:
+            connection.privmsg("ChanServ", "UNBAN")
+            do_everything_to.unban(connection, event.target, connection.get_nickname(), mode[2])
+            connection.mode(event.target, "-b " + mode[2])
+        
+        # Do not revert the actions of retalliate upon one self.
+        if event.source.nick == connection.get_nickname():
+            return
+        
         if behaviour == "passive":    # Passive behaviour.
-            if event.target == self.homechannel:    # Home channel
-                if mode[1] == "b" and fnmatch.fnmatch(self.protectees[connection.get_nickname()]['ident'], mode[2]) and mode[0] == "+": # Bot banned.
-                    ChanServ.unban(connection, event.target, connection.get_nickname())
-                    connection.privmsg("ChanServ", "UNBAN")
-                    connection.mode(event.target, "-b " + mode[2])
+            return
         elif behaviour == "defense_only":   # Defensive only behaviour.
             if mode[1] == "b" and mode[0] == "+":  # Ban.
                 for protectee in self.protectees:
                     if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]):  # Protectee.
-                        ChanServ.unban(connection, event.target, protectee)
-                        connection.mode(event.target, "-b " + modes[1])
+                        do_everything_to.unban(connection, event.target, protectee, mode[2])
             elif mode[1] == "e" and mode[0] == "-":  # Removed exception.
                 for protectee in self.protectees:
                     if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]):  # Protectee.
@@ -62,8 +67,7 @@ def process_event(self, connection, event):
             if modes[1] == "b" and mode[0] == "+":  # Ban.
                 for protectee in self.protectees:
                     if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]):  # Protectee.
-                        ChanServ.unban(connection, event.target, protectee)
-                        connection.mode(event.target, "-b " + mode[2])
+                        do_everything_to.unban(connection, event.target, protectee, mode[2])
                         if protectee == connection.get_nickname():  # Bot banned.
                             ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = equal_retalliation.")
                         else:
@@ -77,35 +81,15 @@ def process_event(self, connection, event):
             if mode[1] == "b" and mode[0] == "+":  # Ban.
                 for protectee in self.protectees:
                     if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]):  # Protectee.
-                        ChanServ.unban(connection, event.target, protectee)
-                        connection.mode(event.target, "-b " + mode[2])
-                        if protectee == connection.get_nickname():  # Bot banned.
-                            ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = battlebot.")
-                        else:
-                            ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
-                        connection.mode(event.target, "+b " + event.source)
+                        do_everything_to.unban(connection, event.target, protectee, mode[2])
+                        do_everything_to.ban(connection, event.target, event.source.nick, event.source, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
                         connection.mode(event.target, "+e " + mode[2])
                         ChanServ.akick_add(connection, event.target, event.source.nick)
-                        if protectee == connection.get_nickname():  # Bot banned.
-                            ChanServ.kick(connection, event.target, event.source.nick, "Aggression channel function = battlebot.")
-                            connection.kick(event.target, event.source.nick, "Aggression channel function = battlebot.")
-                        else:
-                            ChanServ.kick(connection, event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
-                            connection.kick(event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
+                        do_everything_to.kick(connection, event.target, event.source.nick, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
             elif mode[1] == "e" and mode[0] == "-":  # Removed exception.
                 for protectee in self.protectees:
                     if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]):  # Protectee.
-                        if protectee == connection.get_nickname():  # Bot.
-                            ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = battlebot.")
-                        else:
-                            ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
-                        print(event.target, "+b " + event.source)
-                        connection.mode(event.target, "+b " + event.source)
+                        do_everything_to.ban(connection, event.target, event.source.nick, event.source, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
                         connection.mode(event.target, "+e " + modes[mode]['value'])
                         ChanServ.akick_add(connection, event.target, event.source.nick)
-                        if protectee == connection.get_nickname():  # Bot.
-                            ChanServ.kick(connection, event.target, event.source.nick, "Aggression channel function = battlebot..")
-                            connection.kick(event.target, event.source.nick, "Aggression channel function = battlebot.")
-                        else:
-                            ChanServ.kick(connection, event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
-                            connection.kick(event.target, event.source.nick, "Aggression channel function = battlebot: " + protectee + " is an operator of " + connection.get_nickname() + ".")
+                        do_everything_to.kick(connection, event.target, event.source.nick, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))