:autobot system
:module
:help.m misc.m
eval addhelp addbot&addbot $(c4)[$(c3)nick$(c4)|$(c3)nick!ident@host$(c4)] [$(c3)channel$(c4)] [$(c3)password$(c4)] [$(c3)op command$(c4)]&adds a bot to your autobot list.
alias addbot {
	if ([$0]) {
		@ tmp = [$0]
	}
	{
		@ tmp = [$"Enter nick or nick!ident@host: "]
	}
        if (ismask($tmp)) {
                @ bhost = sar(g/~/\*/$after(! $tmp))
                @ bnick = before(! $tmp)
        }
        {
                if (!getuhost($tmp)) {
                        xecho -b $tmp is not on irc.
                }                                 
                {                                 
                        @ bhost = sar(g/~/\*/$getuhost($tmp))
                        @ bnick = [$tmp]                     
                }
        }
        if (bhost && bnick) {
		if ([$1]) {
	                @ bchan = [$1]
		}
		{
			@ bchan = [$"Enter channel: "]
		}
		if ([$2]) {
	                @ bpass = encode($2)
		}
		{
			@ bpass = encode($"Enter password: ")
		}
		if ([$3]) {
	                @ bcmnd = [$3-]
		}
		{
			@ bcmnd = [$"Enter op command: "]
		}
                @ setitem(abot 0 *)
                @ setitem(abot $numitems(abot) $bnick!$bhost $bchan $bpass $bcmnd)
                xecho -b bot added: $(c1)$(bnick)$(c4)\($(c2)$before(@ $bhost)$(c1)@$(c2)$after(@ $bhost)$(c4))$(c3) channel: $(c2)$bchan$(c3) pass: $(c2)$decode($bpass)$(c3) op command: $(c2)$bcmnd$(c3)
        }                     
        ^assign -bnick
        ^assign -bhost
        ^assign -bchan
        ^assign -bpass
        ^assign -bcmnd
	^restub addbot autobot.m
}
eval addhelp delbot&delbot $(c4)[$(c3)number$(c4)|$(c3)nick$(c4)|$(c3)nick!ident@host$(c4)]&deletes the specified bot from your autobot list.
alias delbot {
	if ([$0]) {
		@tmp = [$0]
	}
	{
		@tmp = [$"Enter bot's nick, nick!ident@host, or number from bot list: "]
	}
	if (isdigit($tmp)) {
		if (numitems(abot) <= $tmp) { xecho -b error: invalid bot number }
		if (numitems(abot) <= 1) { xecho -b error: invalid bot number }
		if (numitems(abot) > 1) {
			@ bnick = before(! $word(0 $getitem(abot $tmp)))
			@ bhost = after(! $word(0 $getitem(abot $tmp)))
			@ bchan = word(1 $getitem(abot $tmp))
			@ bpass = decode($word(2 $getitem(abot $tmp)))
			@ bcmnd = word(3 $getitem(abot $tmp))
			@ delitem(abot $tmp)
			xecho -b bot removed: $(c1)$bnick$(c4)\($(c2)$before(@ $bhost)$(c1)@$(c2)$after(@ $bhost)$(c4))$(c3) channel: $(c2)$bchan$(c3) pass: $(c2)$bpass$(c3) op command: $(c2)$bcmnd
		}
	}
	{
		if (![$ismask($tmp)]) {
			for (@i=0,i<=numitems(abot),@i++) {
				if (left($strlen($tmp) $word(0 $getitem(abot $i))) == [$tmp]) {
                                        @ bnick = before(! $word(0 $getitem(abot $i)))
                                        @ bhost = after(! $word(0 $getitem(abot $i)))
                                        @ bchan = word(1 $getitem(abot $i))    
                                        @ bpass = decode($word(2 $getitem(abot $i))) 	
                                        @ bcmnd = word(3 $getitem(abot $i))    
                                        @ delitem(abot $i)                     
                                        xecho -b bot removed: $(c1)$bnick$(c4)\($(c2)$before(@ $bhost)$(c1)@$(c2)$after(@ $bhost)$(c4))$(c3) channel: $(c2)$bchan$(c3) pass: $(c2)$bpass$(c3) op command: $(c2)$bcmnd
					@ i--
                                }
			}
			if (![$bnick]) { xecho -b error: invalid bot nick }
		}
		{
		        for (@i=0,i<=numitems(abot),@i++) {              
		                if (word(0 $getitem(abot $i)) == [$tmp]) {    
		                        @ bnick = before(! $word(0 $getitem(abot $i)))        
		                        @ bhost = after(! $word(0 $getitem(abot $i)))         
		                        @ bchan = word(1 $getitem(abot $i))                   
		                        @ bpass = decode($word(2 $getitem(abot $i)))          
		                        @ bcmnd = word(3 $getitem(abot $i))                   
		                        @ delitem(abot $i)                                    
		                        xecho -b bot removed: $(c1)$bnick$(c4)\($(c2)$before(@ $bhost)$(c1)@$(c2)$after(@ $bhost)$(c4))$(c3) channel: $(c2)$bchan$(c3) pass: $(c2)$bpass$(c3) op command: $(c2)$bcmnd
					@ i--
		                }                                                             
		        }
			if (![$bnick]) { xecho -b error: invalid bot nick!ident@host }
		}                      
	}
        ^assign -bnick
        ^assign -bhost
        ^assign -bchan
        ^assign -bpass
        ^assign -bcmnd
        ^restub delbot autobot.m
}
eval addhelp listbot&listbot&lists all bots currently in your autobot list.
alias listbot {
	if (numitems(abot) > 1) { 
		xecho -c $fparse(format_header autobot list)
		xecho -c $fparse(format_midbar $(c4)[$(c2)num$(c4)][$(c2)     nick!ident@host               $(c4)][$(c2)channel   $(c4)][$(c2)password  $(c4)][$(c2)command   $(c4)]) 
	} 
	for (@i=1,i<numitems(abot),@i++) {                                           
                @ bnick = before(! $word(0 $getitem(abot $i)))                
                @ bhost = after(! $word(0 $getitem(abot $i)))                 
                @ bchan = word(1 $getitem(abot $i))                           
                @ bpass = decode($word(2 $getitem(abot $i)))                  
                @ bcmnd = word(3 $getitem(abot $i))                           
                xecho -c $fparse(format_midbar $(c4)[$(c3)$[-3]i$(c4)][$(c3)$(c3)$[-9]bnick$(c4)!$(c3)$[25]bhost$(c4)][$(c3)$[10]bchan$(c4)][$(c3)$[10]bpass$(c4)][$(c3)$[10]bcmnd$(c4)])
        }
	if (![$bnick]) {
		xecho -b error: bot list is empty 
	}
	{
		xecho -c $fparse(format_footer autobot list)
	}
	^assign -bnick
	^assign -bhost
	^assign -bchan
	^assign -bpass
	^assign -bcmnd
	^restub listbot autobot.m
} 
eval addhelp savebot&savebot&saves autobot settings.
alias savebot {
        @ rename($bsavefile $bsavefile~)
        @ botfile = open($bsavefile W T)
        @ write($botfile # autobot configuration last saved: $strftime($time() %D %T))
        for (@i=0,i<numitems(abot),@i++) {
        	@ write($botfile @ bot.$i  = [$getitem(abot $i)])
	}
        @ close($botfile)
        if ([$i] > 1) {
		xecho -b autobot settings saved to $(bsavefile).
	}
	{
		xecho -b error: bot list is empty.
	}
	^restub savebot autobot.m
}
eval addhelp botload&botload&loads saved autobot information into your autobot list. 
alias botload {
        for (@i=0,bot[$i],@i++) {
                ^assign -bot[$i]
        }
	^load $bsavefile
	@ delarray(abot)
	for (@i=0,bot[$i],@i++) {
		@ setitem(abot $i $bot[$i])
	}
	if ([$i] > 0) {
		if (![$0]) {
			xecho -b autobot settings loaded from $(bsavefile).
		}
	}
	{
		if (![$0]) {
			xecho -b error: $bsavefile contains no bot list data
		}
	}
        ^restub botload autobot.m
}
^on #-channel_synch 99 * {
	fe ($chops($0)) j {
	        for (@i=0,i<=numitems(abot),@i++) {      
	                if (left($strlen($j) $word(0 $getitem(abot $i))) == [$j]) {    
	                        @ bnick = before(! $word(0 $getitem(abot $i)))         
	                        @ bhost = after(! $word(0 $getitem(abot $i)))       
	                        @ bchan = word(1 $getitem(abot $i))                
	                        @ bpass = decode($word(2 $getitem(abot $i)))           
	                        @ bcmnd = word(3 $getitem(abot $i))
				if ([$0] == [$bchan]) {
					if (userhost($j) =~ bhost) {
						msg $bnick $bcmnd $bpass
						xecho -c $fparse(format_autobot $bchan $bnick)
					}
				}				       
	                }                  
	        }
	}
}
if ([$autosave] == [on]) {                           
        ^loadbot sil
}
