### ChanLink.tcl v2.1 by JunKie <seizure@atlantic.net>

### Requires Eggdrop v0.9tp or better to run properly.  
### Sorry, all you 0.9r's out there.. your version is buggy.

### This is version 2.1 - No more Bugs!!
### Added in this version:  A little more explanation in the script itself..
### Also added a MSG Command:  LINKMSG (able to remote MSG people now...)

### Lamer Patch:  Lamers were trying to kick the bots themselves.  
### This no longer works; !LINKBOOT *tries* to strip the botnick's out.

### Orac just plain SUX compared to this cool script!  
### This can even send ACTIONS! ;)

### Command Summary:
### !LINKHELP in your channel gives a list of commands.
### !LINKALL send out a command, and all bots on the net that have 
###             ChanLink will link in, and give their info.
### !LINKSHUTDOWN tells all bots on the net to quit the link.
### !LINKUP tells only your bot to join the link, quietly.
### !LINKDOWN tells only your bot to quit the link, quietly.
### !UNLINK <bot> discretely cuts a bot's access to the link. 
### !LINKIN <bot> discretely tells a bot to join in!
### !LINKBOTS tells you what bots are currently running ChanLink.
### !LINKBOOT <bot> <nick> <reason> kicks a user remotely!   (Useful!)
### !LINKUSERS <bot> lists the people on <bot>'s channel.
### !LINKPING <bot> pings a remote linked bot and gives a reply in seconds.
### All commands can also be accessed by /MSG to the bot, no !.

### Setus current version information.
set cnver v2.1

### If ORAC variable doesn't exist, turn it off.
if {![info exists orac]} {set orac 0}

### Ask who on the botnet is running ChanLink
bind pub - !LINKBOTS pub_whohaschanlink
bind msg - linkbots pub_whohaschanlink
proc pub_whohaschanlink {nick uhost hand text} {
	global botnick
	putallbots "CHANLINK $botnick"
}
bind bot - CHANLINK bot_whosgotit
proc bot_whosgotit {bot cmd txt} {
	global botnick channel cnver
	putbot $txt "ORAC $botnick is linking $channel thanks to ChanLink $cnver" 
}

### Sends all public messages, save those sent by the bot, out to the net.
bind pubm - * pub_orac
proc pub_orac {nick uhost hand text} {
  global orac botnick channel
  if {!($orac)} {return 0}
  if {$botnick == $nick} {return 0}
  putallbots "ORAC \<$nick@$channel\> $text"
}

### Send all ACTIONS out on the net.
bind ctcp - ACTION ctcp_orac
proc ctcp_orac {nick uhost hand dest cmd arg} {
	global botnick channel orac
	if {!($orac)} {return 0}
	if {$botnick == $nick} {return 0}
	putallbots "ORAC \($botnick\) \* $nick@$channel $arg"
}

### Sends information about people who Joined out to the net.
bind join - % join_orac
proc join_orac {nick uhost hand} {
  global channel orac botnick
  if {!($orac)} {return 0}
  putallbots "ORAC \($botnick\) *** $nick \($uhost\) just joined $channel."
}

### Sends information about people who have LEFT, out to the net.
bind part - % part_orac
proc part_orac {nick uhost hand} {
  global botnick channel orac
  if {!($orac)} {return 0}
  putallbots "ORAC \($botnick\) *** $nick left $channel."
}

### Lets the net know someone changed the topic!
bind topc - * topc_orac
proc topc_orac {nick uhost hand topic} {
  global channel orac botnick
  if {!($orac)} {return 0}
  putallbots "ORAC *** $nick changed the topic on $botnick's $channel to:"
  putallbots "ORAC *** \"$topic\""
}

### Announces when a Channel Kick occurs.
bind kick - % kick_orac
proc kick_orac {nick uhost hand kicked reason} {
  global channel orac botnick
  if {!($orac)} {return 0}
  putallbots "ORAC *** $kicked has been kicked off $botnick's $channel by $nick \($reason\)" 
}

### Annoounces when a user /QUIT's.
bind sign - % sign_orac
proc sign_orac {nick uhost hand reason} {
  global orac channel botnick 
  if {!($orac)} {return 0}
  putallbots "ORAC \($botnick\) *** Signoff: $nick \[$uhost\] \($reason\)"
}

### Announces any changes of nicks.
bind nick - % nick_orac
proc nick_orac {nick uhost hand newnick} {
  global orac botnick channel
  if {!($orac)} {return 0}
  putallbots "ORAC \($botnick\) *** $nick \($uhost\) is now known as $newnick on $channel."
}

bind msg - linkmsg msg_linkmsg
proc msg_linkmsg {nick uhost hand txt} {
	if {$txt == ""} { 
	putserv "PRIVMSG $nick :USAGE: linkmsg <botnick> <user> <message>"
	return 0
	}
	set whatbot [lindex $txt 0]
	set whoto [lindex $txt 1]
	set msg [lrange $txt 2 end]
	putbot $whatbot "LMSG $nick $whoto $msg"
}
bind bot - LMSG msg_sendlinkmsg
proc msg_sendlinkmsg {bot cmd arg} {
	set whofrom [lindex $arg 0]
	set whoto [lindex $arg 1]
	set msg [lrange $arg 2 end]
	putserv "privmsg $whoto :Link Message from $whofrom:"
	putserv "privmsg $whoto :$msg"
	putbot $bot "ORAC Message sent to $whoto."
}

bind pub o !UNLINK pub_unlinkabot
bind msg o unlink pub_unlinkabot
proc pub_unlinkabot {nick uhost hand bot} {
	putbot $bot "LINKDOWN $nick"
}
bind pub o !LINKIN pub_linkupabot
proc pub_linkupabot {nick uhost hand bot} {
	global channel botnick
	putbot $bot "LINKUP $channel $botnick"
}
bind pub o !LINKALL pub_linkup
proc pub_linkup {nick uhost hand text} {
	global channel orac botnick
	set orac 1
	putallbots "LINKUP $nick $channel $botnick"
	putserv "PRIVMSG $channel :Linking Channels..."
}

bind pub m !linkshutdown pub_linkdown
bind pub o !linkup pub_linkulocal
proc pub_linkulocal {nick uhost hand text} {
	global orac
	set orac 1
}
bind pub o !LINKDOWN pub_linkdlocal

proc pub_linkdlocal {nick uhost hand text} {
	global orac
	set orac 0
}

proc pub_linkdown {nick uhost hand text} {
	global channel orac
	putallbots "LINKDOWN $nick"
	set orac 0
	putserv "PRIVMSG $channel :Link Disconnected."
}

bind pub - !linkhelp pub_linkhelp
proc pub_linkhelp {nick uhost hand text} {
	global cnver
	putserv "PRIVMSG $nick :Channel Link $cnver by JunKie"
	putserv "PRIVMSG $nick :!LINKUP    !LINKDOWN  !LINKBOTS"
	putserv "PRIVMSG $nick :!LINKUSERS !LINKPING  !LINKHELP"
	putserv "PRIVMSG $nick :E-Mail <seizure@atlantic.net> for more help."
}
bind bot - LINKUP do_link
proc do_link {from command arg} {
	global channel botnick orac
	set orac 1
	set whodid [lindex $arg 0]
	set whatbot [lindex $arg 2]
	set whatchan [lindex $arg 1]
	putserv "privmsg $channel :Channel Link Established!"
	putserv "privmsg $channel :We are now linked to $whatchan via $whatbot."
	putserv "privmsg $channel :This link was authorized by $whodid!"
	putbot $whatbot "ORAC Channel Link-Up Established to $channel via $botnick."
}

bind bot - LINKDOWN do_unlink
proc do_unlink {from command arg} {
	global channel botnick orac
	putallbots "ORAC Disconnected from Link."
	set orac 0
	putserv "privmsg $channel :Channel Link Disconnected by $arg."
}

# ping <bot> or <*> for all bots

bind pub - !LINKPING dcc_ping
proc dcc_ping {nick uhost hand arg} {
global botnick
  if {$arg == ""} {
	putserv "PRIVMSG $nick :USAGE: !LINKPING <bot> or !LINKPING * (pings all bots)"
	return 0
  }
if {[string compare [string tolower $arg] [string tolower $botnick]] == 0} {
  putserv "PRIVMSG $nick :$nick, you must specify a bot that is linked to $botnick!"
  return 0
  }
  if {$arg == "\*"} {
    putallbots "ping $hand [unixtime]"
    return 0
  }
  putbot $arg "ping $hand [unixtime]"
  return 1
}

bind bot - ping bot_ping
proc bot_ping {bot cmd arg} {
  set from [lindex $arg 0]
  set rtime [lindex $arg 1] 
  putbot $bot "pong $from $rtime"
}

bind bot - pong bot_pong
proc bot_pong {bot cmd arg} {
  global channel
  set to [lindex $arg 0]
  set pingtime [expr [unixtime] - [lindex $arg 1] + 1]
  set outstr "*** \($bot\) PONG!  \[$pingtime seconds\]"
  putserv "PRIVMSG $channel :$outstr"
  return 1
}

# chlist <bot>
bind pub o !LINKBOOT pub_linkboot 
proc pub_linkboot {nick uhost hand arg} {
	if {$arg == ""} {
	putserv "PRIVMSG $nick :USAGE: !LINKBOOT <bot> <nick> <reason>"
	return 0
	}
	set whatbot [lindex $arg 0]
	set who [lindex $arg 1]
	if {$who == "W"} {return 0}
	if {$who == "w"} {return 0}
	set why [lrange $arg 2 end]
	putbot $whatbot "KICK $who $nick $why"
	putserv "PRIVMSG $nick :Trying to boot $who via $whatbot."
}
bind bot - KICK bot_kick
proc bot_kick {bot cmd arg} {
	global channel botnick
	set who [lindex $arg 0]
	set whodunit [lindex $arg 1]
	set why [lrange $arg 2 end]
	if {$who == $botnick} {
	putallbots "ORAC \($botnick\) $whodunit \*TRIED\* to boot me!"
	return 0
	}
  if {![onchan $who]} {
    return 0
  }
  if {[onchanSplit $who]} {
    return 0
  }
  append userhost $who "!" [getchanhost $who]
  set target [finduser $userhost]
  if {$target != "*"} {
    if {([matchattr $target o] || [matchattr $target m] || [matchattr $target f])} {
      putallbots "ORAC \($botnick\) $whodunit \*TRIED\* to boot $who."
	return 0
	}        
  }
	putlog "Link Booting $who: $why by $whodunit"
	if {$who == "W"} {return 0}
	putserv "KICK $channel $who :$why"
	putallbots "ORAC \($botnick\) $whodunit Booted $who: $why" 
}

#bind dcc m chlist dcc_chlist
bind pub - !LINKUSERS dcc_chlist
proc dcc_chlist {nick uhost hand arg} {
global botnick 
  if {$arg == ""} {
  putserv "PRIVMSG $nick :USAGE: !LINKUSERS <bot>"
  return 0
  }
  if {[string compare [string tolower $arg] [string tolower $botnick]] == 0} {
  putserv "PRIVMSG $nick :$nick, you must specify a bot that is linked to $botnick!"
  return 0
  }
     putbot $arg "chlist $nick"
  return 1
}

bind bot - chlist bot_chlist
proc bot_chlist {bot cmd arg} {
global channel botnick pbotlog
  set from [lindex $arg 0] 
    if {$channel == {}} {
    putbot $bot "result $from $from, I'm currently not on a channel!" 
  return 0
  }
  set users [chanlist]
  foreach user $users {
    if {[isop $user]} { 
    append userlist "@" $user " " 
    } else {
    append userlist $user " "
    }
  }
  putbot $bot "ORAC Users currently on $channel are: \[$userlist\]" 
  return 1
}

### Takes any information sent to ORAC users on the net, and displays it.
bind bot - ORAC do_orac
proc do_orac {from command arg} {
  global orac channel
  if {!($orac)} {return 0}
  putserv "PRIVMSG $channel :$arg"
}
