import znc


# ['akill', 'nick', 'time', 'reason', 'optional']
#    [0]      [1]    [2]       [3]        [4]


class akill(znc.Module):
    description = "Shortcut commands for akill"
    module_types = [znc.CModInfo.NetworkModule]
    reasons = ["netban", "spam", "given"]
    akill_format = "PRIVMSG OperServ :AKILL ADD +{duration} {mask} {reason}"

    def OnUserRaw(self, linecs):
        line = linecs.s.split()
        if line[0] == "akill":
            if len(line) < 4:
                # too short, respond with error as a notice
                self.send_usage()
                return znc.HALT

            elif line[3][0] == "#" or line[3] in self.reasons:
                if len(line) > 4:
                    _, nick, time, reason, *address = line
                    reason = reason.lower()
                    address = " ".join(address)
                    if line[3][0] == "#":
                        self.evasion(time, nick, reason, address)
                    elif reason == "netban":
                        self.netban(time, nick, address)
                    elif reason == "spam":
                        self.spam(time, nick, address)
                    elif reason == "given":
                        self.do_akill(nick, time, address)

                else:
                    nick, time, reason = line[1], line[2], line[3].lower()
                    if line[3][0] == "#":
                        self.evasion(time, nick, reason, nick)
                    elif reason == "netban":
                        self.netban(time, nick, nick)
                    elif reason == "spam":
                        self.spam(time, nick, nick)
                    elif reason == "given":
                        self.PutModNotice(
                            "given requires a reason to be specified")
                    else:
                        self.PutModNotice("Unknown option, valid options are "
                                          "#channel, netban, spam and "
                                          "given (set the entire message")
            else:
                self.send_usage()
            return znc.HALT

    def evasion(self, time, nick, chan, address):
        reason = "{address}: {time} day network ban for ban evasion in " \
                 "{channel}. Further violations of our network rules will " \
                 "result in an increase in ban length and may become " \
                 "permanent. https://snoonet.org/rules".format(address=address,
                                                               time=time,
                                                               channel=chan)
        self.do_akill(nick, time, reason)

    def netban(self, time, nick, address):
        reason = "{address}: {time} day network ban for evasion of a " \
                 "network ban. Further violations of our network rules will " \
                 "result in an increase in ban length and may become " \
                 "permanent. https://snoonet.org/rules".format(address=address,
                                                               time=time)
        self.do_akill(nick, time, reason)

    def spam(self, time, nick, address):
        reason = "{address}: {time} day network ban for spam. Further " \
                 "violations of our network rules will result in an " \
                 "increase in ban length and may become permanent. " \
                 "https://snoonet.org/rules".format(address=address, time=time)
        self.do_akill(nick, time, reason)

    def do_akill(self, nick, time, reason):
        self.PutIRC(
            self.akill_format.format(duration=str(time) + 'd',
                                     mask=nick,
                                     reason=reason)
        )

    def send_usage(self):
        self.PutModNotice("Usage: /akill nick/mask time(In days) "
                          "reason(Valid reasons are a channel name, spam, "
                          "netban and given. You specify the full message" 
                          "with given) [nick to address]")

@@ -343,16 +343,74 @@
 reason%7D%22
+%0A    echo_format = %22AKILL ADD +%7Bduration%7D %7Bmask%7D %7Breason%7D%22
 %0A%0A    de
@@ -486,16 +486,24 @@
  line%5B0%5D
+.lower()
  == %22aki
@@ -731,46 +731,8 @@
 ns:%0A
-                if len(line) %3E 4:%0A    
     
@@ -794,28 +794,24 @@
             
-    
 reason = rea
@@ -838,20 +838,16 @@
         
-  
-  
 address 
@@ -858,36 +858,33 @@
 %22.join(address)%0A
-    
+%0A
                 
@@ -897,36 +897,32 @@
 e%5B3%5D%5B0%5D == %22#%22:%0A
-    
                 
@@ -957,38 +957,42 @@
  reason, address
-)%0A    
+ or nick)%0A
                 
@@ -1028,36 +1028,32 @@
                 
-    
 self.netban(time
@@ -1059,38 +1059,42 @@
 e, nick, address
-)%0A    
+ or nick)%0A
                 
@@ -1128,36 +1128,32 @@
                 
-    
 self.spam(time, 
@@ -1165,22 +1165,26 @@
  address
-)%0A    
+ or nick)%0A
         
@@ -1227,32 +1227,64 @@
                 
+if address:%0A                    
     self.do_akil
@@ -1308,405 +1308,8 @@
 ress
-)%0A%0A                else:%0A                    nick, time, reason = line%5B1%5D, line%5B2%5D, line%5B3%5D.lower()%0A                    if line%5B3%5D%5B0%5D == %22#%22:%0A                        self.evasion(time, nick, reason, nick)%0A                    elif reason == %22netban%22:%0A                        self.netban(time, nick, nick)%0A                    elif reason == %22spam%22:%0A                        self.spam(time, nick, nick
 )%0A  
@@ -1328,36 +1328,18 @@
       el
-if reason == %22given%22
+se
 :%0A      
@@ -1454,36 +1454,32 @@
                 
-    
 else:%0A          
@@ -1492,212 +1492,24 @@
     
-    
 self.
-PutModNotice(%22Unknown option, valid options are %22%0A                                          %22#channel, netban, spam and %22%0A                                          %22given (set the entire message%22
+send_usage(
 )%0A  
@@ -3028,16 +3028,222 @@
 eason):%0A
+        %0A        self.PutModNotice(%0A            self.echo_format.format(duration=str(time) + 'd',%0A                                    mask=nick,%0A                                    reason=reason)%0A        )%0A
         

made the command case insensitive