# AddedBots.tcl                                      #
# MyCraft                           (c) 2002 Mai 12  #
# EMail                             MyCraft@gmx.net  #
######################################################
# Lists all Added Bots of the Botnet.
# Checks for Linked Bots, OnChan Bots and displays
# Missing Linked/OnChan Bots.
# (netbots.tcl must be loaded, cos this is a modified component for netbots.tcl)

# Set trigger word for dcc, msg, & public.
# (always preclude trigger word with an "!" for public command)
set trigger "netbots"

######### Please do not edit anything below unless you know what you are doing ;) #########

bind dcc n ${trigger} nb_dccnetbots
bind msg n ${trigger} msg_list_bots
bind pub n !${trigger} pub_list_bots

proc pub_list_bots {nick host hand chan arg} {
 global botnick  
 set msg ""
 set added_bot 0
 set on_chan 0
 set not_on_chan 0
 set linked 0
 set linked_msg ""
 set misslink ""
 set missonchan ""
 foreach hand [userlist] {
    set leave 1
    if {[matchattr $hand b] == 1} {incr added_bot; set leave 0}
    if {($leave == 0) && ([onchan [hand2nick $hand] $chan])} {incr on_chan}
    if {($leave == 0) && (![onchan [hand2nick $hand] $chan])} {incr not_on_chan; append msg "$hand, "}
    if {($leave == 0) && ([islinked $hand])} {incr linked}
    if {($leave == 0) && (![islinked $hand]) && ($hand != "$botnick")} {append linked_msg "$hand, "}
 }
 if {[string range $msg [expr [string length $msg] -1] end] != ", "} {
      set msg "[string trimright $msg ", "]"
    }
 if {[string range $linked_msg [expr [string length $linked_msg] -1] end] != ", "} {
      set linked_msg "[string trimright $linked_msg ", "]"
    }
 set plah ""
 set linked [expr $linked + 1]
 set misslink [expr $added_bot - $linked]
 set missonchan [expr $added_bot - $on_chan]

 if {$added_bot != "$on_chan"} {
      set plah "\[4x]: $added_bot bots on record, but only $on_chan are here."
      if {$linked != $on_chan} {append plah " Linked bots: $linked."}
      putserv "PRIVMSG $chan : $plah"
      if {$linked == $on_chan} {
           putserv "PRIVMSG $chan :\[4x]: missing bots ($missonchan)\: $msg."
         } else {
           putserv "PRIVMSG $chan :\[4x]: missing bots on channel ($missonchan)\: $msg."
           if {$misslink != "0"} { 
                putserv "PRIVMSG $chan :\[4x]: unlinked bots ($misslink)\: $linked_msg."
              }
         }
    } else {
      putserv "PRIVMSG $chan :\[4x]: all $added_bot netbots are here."
    }
  putcmdlog "!$nick@$chan! netbots"
}
proc msg_list_bots {nick host hand idx} {
  global botnick nb_control nb_flag
  if {![nb_chkowner $hand $idx]} {return 0}
  putcmdlog "<<$nick>> #$hand# netbots"
  set botlist "" ; set offline ""
  foreach netbot [userlist $nb_flag] {
      lappend botlist $netbot
      if {![islinked $netbot] && ![isbotnetnick $netbot]} {
        lappend offline $netbot
      }
    }
    if {$botlist == ""} {
      puthelp "notice $nick :\[4x]: there are no netbots." ; return 0
    }
    puthelp "notice $nick :\[4x]: current netbots: [join $botlist ", "]."
    if {$offline != ""} {
      puthelp "notice $nick :\[4x]: offline or not linked: [join $offline ", "]."
    } else {
      puthelp "notice $nick :\[4x]: all netbots are online and linked."
    }
}
proc nb_chkowner {hand idx} {
  global botnick nick host nb_owner owner
  if {$nb_owner && [lsearch -exact [split [string tolower $owner] ", "] [string tolower $hand]] == -1} {
    return 0
  }
  return 1
}

return "nb_info 1.03"

putlog "\[4x]: Utility, NetBots List. Loaded"