### ChanLink.tcl v2.0d 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.0 - ChanLink now kicks ass!  Orac just plain SUX
### compared to this cool ass shit!  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.


set cnver v2.0-d
# beginning.
if {![info exists orac]} {set orac 0}

bind pub - !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 "RCHANLINK $botnick is linking $channel thanks to ChanLink $cnver" }
bind bot - RCHANLINK bot_igotit
proc bot_igotit {bot cmd txt} {
	global channel
	putserv "PRIVMSG $channel :$txt"
}
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"
}

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 ACTION: \* $nick@$channel $arg"
}

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."
}

bind part - % part_orac

proc part_orac {nick uhost hand} {
  global botnick channel orac
  if {!($orac)} {return 0}
  putallbots "ORAC \($botnick\) *** $nick left $channel."
}

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\""
}

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\)" }

bind sign - % sign_orac

proc sign_orac {nick uhost hand reason} {
  global orac channel botnick 
  if {!($orac)} {return 0}
  putallbots "ORAC *** Signoff, $botnick's $channel: $nick \($uhost\ \[$reason\])"
}

bind nick - % nick_orac

proc nick_orac {nick uhost hand newnick} {
  global orac botnick channel
  if {!($orac)} {return 0}
  putallbots "ORAC *** $nick \($uhost\) is now known as $newnick on $botnick's $channel."
}

bind bot - ORAC do_orac

proc do_orac {from command arg} {
  global orac channel
  if {!($orac)} {return 0}
  putserv "PRIVMSG $channel :$arg"
}
bind pub 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.  Authorized by $whodid!"
	putallbots "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\] PING: $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]
	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 kickwho [lindex $arg 0]
	set whodunit [lindex $arg 1]
	set why [lrange $arg 2 end]
	putlog "Link Booting $kickwho: $why by $whodunit"
	putserv "KICK $channel $kickwho :$why"
	putallbots "ORAC \($botnick\) $whodunit Booted $kickwho: $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 "result Users currently on $channel are: \[ $userlist\]" 
  return 1
}

bind bot - result bot_result

proc bot_result {bot cmd arg} {
global channel
putserv "privmsg $channel :$arg"
}
