#######################################################################
# idlerpg.tcl                                                         #
# Copyright (c) 2004, Brian Hartvigsen                                #
#######################################################################
# This work is licensed under the Creative Commons                    #
# Attribution-NonCommercial-ShareAlike License. To view a copy of     #
# this license, visit                                                 # 
# http://creativecommons.org/licenses/by-nc-sa/2.0/ or send a letter  #
# to Creative Commons, 559 Nathan Abbott Way, Stanford, California    #
# 94305, USA.                                                         #
#######################################################################
# Configuration at the end of file!                                   #
#######################################################################

print "idlerpg.tcl for X-Chat"
print "Copyright (c) 2004, Brian Hartvigsen"
print "This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License. To view a copy of  this license, visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA."

if { [info exists ::idlerpgconfig] } {
	unset ::idlerpgconfig
}

proc IdleRPG { my_network my_channel my_bot my_host my_user my_pass } {
	lappend ::idlerpgconfig [list $my_network $my_channel $my_bot $my_host $my_user $my_pass]
}

proc IdleRPGCheck { {my_delay 30} } {
	timer -repeat $my_delay IdleRPGCallback
}

proc IdleRPGCallback {} {
	foreach s [servers] {
		foreach idlerpg $::idlerpgconfig {
			if { [string compare -nocase [network $s] [lindex $idlerpg 0]] != 0 } continue

			set found false
			foreach chan [channels $s] {
				if { [string compare -nocase $chan [lindex $idlerpg 1]] == 0 } {
					set found true
					break
				}
			}
			if { !$found } continue;

			set found false
			foreach u [users $s $chan] {
				if { [string compare -nocase [lindex $u 0] [lindex $idlerpg 2]] != 0 } {
					if { [string compare [lindex $u 0] [me $s]] != 0 } continue
					if { [lsearch [lindex $u 2] "+"] != -1 } return
					continue
				}
				if { [string compare -nocase [lindex $u 1] [lindex $idlerpg 3]] != 0 } continue
				set found true
				set bot $u
			}
			if { !$found } continue;

			command $s "msg [lindex $bot 0] LOGIN [lindex $idlerpg 4] [lindex $idlerpg 5]"
		}
	}
}

#######################################################################
# Configuration:                                                      #
#  IdleRPG <network> <channel> <bot> <bothostname> <uname> <pass>     #
#  IdleRPGCheck <sec. between checks>                                 #
#######################################################################
# Example:
IdleRPG FreeNode #idlerpg IdleBot "~botty@ds9.ethernal.org" tester password
IdleRPGCheck 60
