# randtopic.tcl v2.02 [4 December 1999] 
# Copyright © 1999 Teemu Hjelt <temex@iki.fi>
#
# This script gets a random topic to a channel from a file
# you have specified. You can add new topics or remove topics from 
# the file by using channel commands or dcc commands. 
#
# Current DCC Commands:
#    .rt, .rtall, .addrt, .delrt, .listrt, .sendrt, .rmrt, .rttimer, .rthelp
#
# Current MSG Commands:
#    topic, rt, rtall, addrt, delrt, listrt, sendrt, rmrt, topic, rttimer, rthelp
#
# Current Channel Commands:
#    topic, rt, rtall, addrt, delrt, listrt, sendrt, rmrt, topic, rttimer, rthelp
#
# Tested on eggdrop1.4.0 with TCL 7.6

         
### Settings ###

## [0/1] Enable Channel Commands?
set rt_enablechan 1

## [0/1] Enable MSG Commands?
set rt_enablemsg 1

## [0/1] Enable DCC Commands?
set rt_enabledcc 1

## [0/1] Enable the login thingie?
# Note: If you enable this you must do /msg botnick LOGIN <password> 
# before you can use the channel commands.
set rt_login 0

## [0/1] Do you want that the rtall command sets the same random topic to all channels?
set rt_sametopic 0

## What command prefix do you want to use for channel commands?
set rt_cmdpfix "!"

## List topics via NOTICE or PRIVMSG?
set rt_listmethod "PRIVMSG"

## What users can change the topic?
set rt_tflag "o|o"

## What users can change the topic on all channels?
set rt_rtallflag "o"

## What users can add topic to the file? 
set rt_taddflag "o|o"

## What users can delete topics from the file?
set rt_tdelflag "m|m"

## What users can remove the topic file? 
set rt_trmflag "n"

## What users can request sending of the topic file? 
set rt_sendflag "m|m"

## What users can list the topics?
set rt_listflag "o"

## What users can start or stop the timer?
set rt_timerflag "o|o"

## What file do you want to use for storing topics?
set rt_tfile "text/topics.txt"

## List of channels where the rtall command shouldn't take effect on.
set rt_nochans "#nukie"

## [0/1] Let the rtall command change only blank topics?
set rt_onlyblank 0

## [0/1] Make a html-file of the topics?
set rt_makehtml 0

## What is the filename for the html-file?
# Note: It is very smart to add the path here too!
set rt_htmlfile "/inetpub/wwwroot/topics.htm"

## What kind of body-tag do you want to use in the html-file?
set rt_bodytag {<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000EE" VLINK="#8318C0" ALINK="#005AAF">}

## What kind of tag do you want to use for stylesheets. 
# Note: Leave this empty if you don't want to use stylesheets.
set rt_styletag {<LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet.css">}

## [0/1] Log when timer does something?
set rt_logtimer 1

## [0/1] Let the timer change only blank topics?
set rt_tmronlyblank 0

###### You don't need to edit below this ######

### Misc Things ###

set rt_ver "2.02"

### Bindings ###

## Channel Commands
if {$rt_enablechan} {
	bind pub $rt_tflag ${rt_cmdpfix}topic pub:rt_topic
	bind pub $rt_tflag ${rt_cmdpfix}rt pub:rt_rt
	bind pub $rt_rtallflag ${rt_cmdpfix}rtall pub:rt_rtall
	bind pub $rt_taddflag ${rt_cmdpfix}addrt pub:rt_addrt
	bind pub $rt_timerflag ${rt_cmdpfix}rttimer pub:rt_rttimer
	bind pub $rt_tdelflag ${rt_cmdpfix}delrt pub:rt_delrt
	bind pub $rt_trmflag ${rt_cmdpfix}rmrt pub:rt_rmrt
	bind pub $rt_sendflag ${rt_cmdpfix}sendrt pub:rt_sendrt
	bind pub $rt_listflag ${rt_cmdpfix}listrt pub:rt_listrt
	bind pub - ${rt_cmdpfix}rthelp pub:rt_rthelp
}

## MSG Commands
if {$rt_enablemsg} {
	bind msg $rt_tflag topic msg:rt_topic
	bind msg $rt_tflag rt msg:rt_rt
	bind msg $rt_rtallflag rtall msg:rt_rtall
	bind msg $rt_timerflag rttimer msg:rt_rttimer
	bind msg $rt_taddflag addrt msg:rt_addrt
	bind msg $rt_tdelflag delrt msg:rt_delrt
	bind msg $rt_trmflag rmrt msg:rt_rmrt
	bind msg $rt_sendflag sendrt msg:rt_sendrt
	bind msg $rt_listflag listrt msg:rt_listrt
	bind msg - rthelp msg:rt_rthelp
}

## DCC Commands
if {$rt_enabledcc} {
	bind dcc $rt_tflag rt dcc:rt_rt
	bind dcc $rt_rtallflag rtall dcc:rt_rtall
	bind dcc $rt_timerflag rttimer dcc:rt_rttimer
	bind dcc $rt_taddflag addrt dcc:rt_addrt
	bind dcc $rt_tdelflag delrt dcc:rt_delrt
	bind dcc $rt_trmflag rmrt dcc:rt_rmrt
	bind dcc $rt_sendflag sendrt dcc:rt_sendrt
	bind dcc $rt_listflag listrt dcc:rt_listrt
	bind dcc - rthelp dcc:rt_rthelp
}

## Login Commands
if {$rt_login} {
	bind msg - login msg:rt_login
	bind msg - logout msg:rt_logout
	bind part - * part:rt_check
	bind sign - * sign:rt_check
}

### Login Thingie ###

proc msg:rt_login {nick uhost hand arg} {
global botnick 
set pass [lindex [split $arg] 0]
set foundchan 0
	if {$hand != "*"} {
		foreach chan [channels] {
			if {[onchan $nick $chan]} {
				set foundchan 1
			}
		}
		if {!$foundchan} {
			putserv "NOTICE $nick :\[4x]: you need to be on a channel which is monitored by me."
			putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log in - Not on any channel."
			return 1
		}
		if {$pass == ""} {
			putserv "NOTICE $nick :\[4x]: usage: /msg $botnick LOGIN <password>"
			putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log in - No password."
			return 1
		}
		if {[getuser $hand XTRA "LOGIN"] == "1"} {
			putserv "NOTICE $nick :\[4x]: login unsuccessful - you have already logged in."
			putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log in as $hand - already logged in."
			return 1
		}
		if {[passwdok $hand $pass]} {
			setuser $hand XTRA "LOGIN" "1"
			putserv "NOTICE $nick :\[4x]: login successful."
			putlog "\[4x]: RandomTopic: $nick ($uhost) logged in as $hand."
			return 1
		} else {
			putserv "NOTICE $nick :\[4x]: login unsuccessful - wrong password."
			putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log in as $hand - wrong password."
			return 1
		}
	} else {
		putserv "NOTICE $nick :\[4x]: login unsuccessful - you are not allowed to use my commands."
		putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log in - not allowed to use commands."
		return 1
	}
}

proc msg:rt_logout {nick uhost hand arg} {
	if {$hand != "*"} {
		if {[getuser $hand XTRA "LOGIN"] == "1"} {
			setuser $hand XTRA "LOGIN" "0"
			putserv "NOTICE $nick :\[4x]: successfully logged out."
			putlog "\[4x]: RandomTopic: $nick ($uhost) logged out as $hand."
			return 1
		} else {
			putserv "NOTICE $nick :\[4x]: logout unsuccessful - you're already logged out."
			putlog "\[4x]: RandomTopic: $nick ($uhost) tried to log out - already logged out."
			return 1
		}		
	}
}

proc sign:rt_check {nick uhost hand chan reason} {
	if {($hand != "*") && ([getuser $hand XTRA "LOGIN"] == "1")} {
		setuser $hand XTRA "LOGIN" "0"
		putlog "\[4x]: RandomTopic: $nick ($uhost) signed off - login for $hand expired."
	}
}

proc part:rt_check {nick uhost hand chan} {
	if {($hand != "*") && ([getuser $hand XTRA "LOGIN"] == "1")} {
		utimer 10 "rt_checkpart $nick $uhost $hand $chan"
	}
}

proc rt_checkpart {nick uhost hand chan} {
set foundchan 0
	if {$hand != "*"} {
		foreach chan [channels] {
			if {[handonchan $hand $chan]} {
				set foundchan 1
			}
		}
		if {(!$foundchan) && ([getuser $hand XTRA "LOGIN"] == "1")} {
			setuser $hand XTRA "LOGIN" "0"
			putlog "\[4x]: RandomTopic: $nick ($uhost) parted all channels - login for $hand expired."
		}
	}
}

proc rt_login_check {hand} {
	if {[getuser $hand XTRA "LOGIN"] == "1"} { 
		return 1 
	} else {
		return 0
	}
}

### Channel Commands ###

proc pub:rt_topic {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set topic [join [lrange [split $arg] 0 end]]
	if {[botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]]} {
		if {$topic == ""} { 
			putserv "TOPIC $chan :" 
		} else {
			putserv "TOPIC $chan :$topic"
		}
	} else {
		putserv "NOTICE $nick :\[4x]: unable to change topic of $chan."
	}
return 1
}

proc pub:rt_rt {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
if {[lindex [split $arg] 0] != ""} { set chan [lindex [split $arg] 0] }
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {![rt_botonchan $chan]} {
			putserv "NOTICE $nick :\[4x]: I'm not on $chan."
		} else {
			set topic [rt_gettopic]
			if {($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
				putserv "TOPIC $chan :$topic" 
			} else {
				putserv "NOTICE $nick :\[4x]: unable to change topic of $chan."
			}
		}
	}
return 1
}

proc pub:rt_rtall {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans rt_onlyblank
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set chanlist ""
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		set topic [rt_gettopic]
		foreach chan [channels] {
			if {$rt_nochans != ""} { 
				if {[lsearch -exact [split [string tolower $rt_nochans]] [string tolower $chan]] != -1} {
					continue
				}
			}
			if {$rt_onlyblank} {
				if {[topic $chan] == ""} {
					if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
						lappend chanlist $chan
						if {!$rt_sametopic} { 
							set topic [rt_gettopic] 
						} 
						putserv "TOPIC $chan :$topic" 
					}
				}
			} else {
				if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					lappend chanlist $chan
					if {!$rt_sametopic} { 
						set topic [rt_gettopic] 
					} 
					putserv "TOPIC $chan :$topic" 
				}
			}
		}
		if {$chanlist != ""} {
			if {[llength $chanlist] == 1} {
				putserv "NOTICE $nick :\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channel)"
			} else {
				putserv "NOTICE $nick :\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channels)"
			}
		} else {
			putserv "NOTICE $nick :\[4x]: changed topic of 0 channels."
		}
	}
return 1
}

proc pub:rt_rttimer {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_interval rt_chan rt_option
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set option [string tolower [lindex [split $arg] 0]]
set interval [lindex [split $arg] 1]
if {[lindex [split $arg] 2] != ""} { set chan [lindex [split $arg] 2] }
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {($option != "-stat") && ($option != "-stop") && (($interval == "") || ($option == "") || ((($option != "-rt") || ($chan == "")) && ($option != "-rtall")))} {
			putserv "NOTICE $nick :\[4x]: usage: ${rt_cmdpfix}rttimer <option> \[<interval> \[channel\]\]"
		} elseif {($option == "-rt") && ($chan != "") && ![rt_botonchan $chan]} {
			putserv "NOTICE $nick :\[4x]: I'm not on $chan"
		} elseif {$option == "-stat"} {
			if {[string match *rt_timer* [timers]] && ([info exists rt_interval] && [info exists rt_chan])} {
				if {$rt_option == "-rt"} {
					putserv "NOTICE $nick :\[4x]: timer is running! changing topic of $rt_chan every $rt_interval mins."
				} elseif {$rt_option == "-rtall"} {
					putserv "NOTICE $nick :\[4x]: timer is running! changing topic of all channels every $rt_interval mins."
				}
			} else {
				putserv "NOTICE $nick :\[4x]: timer is not running!"
			}
		} elseif {$option == "-stop"} {
			if {[string match *rt_timer* [timers]]} {
				rt_stoptimer
				putserv "NOTICE $nick :\[4x]: timer stopped!"
			} else {
				putserv "NOTICE $nick :\[4x]: timer is already stopped!"
			}
		} else {
			if {![rt_isnum $interval]} {
				putserv "NOTICE $nick :\[4x]: '$interval' is not a valid interval."
			} else {
				if {$option == "-rt"} {
					rt_stoptimer
					rt_timer $option $interval $chan
					putserv "NOTICE $nick :\[4x]: timer started! changing topic of $chan every $interval mins."
				} elseif {$option == "-rtall"} {
					rt_stoptimer
					rt_timer $option $interval "*"
					putserv "NOTICE $nick :\[4x]: timer started! changing topic of all channels every $interval mins."
				}
			}
		}
	}
return 1
}

proc pub:rt_addrt {nick uhost hand chan arg}  {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set arg [join [lrange [split $arg] 0 end]]
	if {$arg == ""} {
		putserv "NOTICE $nick :\[4x]: usage: ${rt_cmdpfix}addrt <topic>"
	} else {
		rt_addtopic $arg
		putserv "NOTICE $nick :\[4x]: a topic was added to $rt_tfile."
	}
return 1
}

proc pub:rt_delrt {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set arg [join [lrange [split $arg] 0 end]]
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {$arg == ""} {
			putserv "NOTICE $nick :\[4x]: usage: ${rt_cmdpfix}delrt <topic>"
		} else {
			if {[rt_deltopic $arg]} { 
				putserv "NOTICE $nick :\[4x]: a topic was deleted from $rt_tfile."
			} else {
				putserv "NOTICE $nick :\[4x]: unable to delete topic from $rt_tfile."
			}
		}
	}
return 1
}

proc pub:rt_rmrt {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
	if {[rt_delfile $rt_tfile]} {
		putserv "NOTICE $nick :\[4x]: $rt_tfile was succesfully deleted."			
	} else {
		putserv "NOTICE $nick :\[4x]: unable to delete $rt_tfile."			
	}
	if {[rt_delfile $rt_htmlfile]} {
		putserv "NOTICE $nick :\[4x]: $rt_htmlfile was succesfully deleted."			
	} else {
		putserv "NOTICE $nick :\[4x]: unable to delete $rt_htmlfile."
	}
return 1
}	

proc pub:rt_sendrt {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
	if {![string match "*dccsend*" [string tolower [info commands]]]} {
		putserv "NOTICE $nick :\[4x]: unable to use DCC send. check that the transfer module is loaded."
	} else {
		if {[lindex [split $arg] 0] != ""} { set nick [lindex [split $arg] 0] }
		switch -exact -- [dccsend $rt_tfile $nick] {
			0 { putserv "NOTICE $nick :\[4x]: sending you $rt_tfile ([expr [file size $rt_tfile] /1024] KB)"}
			1 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (too many connections)"              }
			2 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (can't open socket)"                 }
			3 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (file doesn't exist)"                }
			4 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (user has too many transfers)"       }
		}
	}
return 1
}

proc pub:rt_listrt {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set amount 0
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {[lindex [split $arg] 0] != ""} { set nick [lindex [split $arg] 0] }
		putserv "$rt_listmethod $nick :\[4x]: Utility, RandomTopics. -- Listing contents of $rt_tfile:"
		set source [open $rt_tfile r]
		while {![eof $source]} {
			set text [gets $source]
			if {$text != ""} {
				incr amount
				putserv "$rt_listmethod $nick : $text"
			}
		}
		close $source
		putserv "$rt_listmethod $nick :\[4x]: -- Listing done ($amount topics)."
	}
return 1
}

proc pub:rt_rthelp {nick uhost hand chan arg} {
global rt_tfile rt_listmethod rt_login rt_cmdpfix rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_ver rt_tflag rt_rtallflag rt_taddflag rt_tdelflag rt_trmflag rt_sendflag rt_listflag rt_timerflag
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set command [lindex [split $arg] 0]
	if {![matchattr $hand $rt_tflag] && ![matchattr $hand $rt_taddflag] && ![matchattr $hand $rt_tdelflag] && ![matchattr $hand $rt_trmflag] && ![matchattr $hand $rt_sendflag] && ![matchattr $hand $rt_listflag]} {
		return 0
	} else {
		if {[string index $command 0] == $rt_cmdpfix} { set command [string range $command 1 end] }
		switch -exact -- [string tolower $command] {
			"" {
				putserv "NOTICE $nick :\[4x]: Utility, Random Topic v$rt_ver commands:"
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :   ${rt_cmdpfix}topic \[topic\]"
					putserv "NOTICE $nick :   ${rt_cmdpfix}rt \[channel\]"
				}
				if {[matchattr $hand $rt_rtallflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}rtall" }
				if {[matchattr $hand $rt_timerflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}rttimer <option> \[<interval> \[channel\]\]" }
				if {[matchattr $hand $rt_taddflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}addrt <topic>" }
				if {[matchattr $hand $rt_tdelflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}delrt <topic>" }
				if {[matchattr $hand $rt_trmflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}rmrt" }
				if {[matchattr $hand $rt_sendflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}sendrt \[nick\]" }
				if {[matchattr $hand $rt_listflag]} { putserv "NOTICE $nick :   ${rt_cmdpfix}listrt \[nick\]" }
				putserv "NOTICE $nick :To get more help on individual commands, type: '${rt_cmdpfix}rthelp <command>'"
			}
			"topic" {
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}topic \[topic\]"
					putserv "NOTICE $nick :   will change or clear the topic of $chan."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"rt" {
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}rt \[channel\]"
					putserv "NOTICE $nick :   will change the topic of $chan or the"
					putserv "NOTICE $nick :   channel you have specified to a random topic."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"rtall" {
				if {[matchattr $hand $rt_rtalllag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}rtall"
					putserv "NOTICE $nick :   will change the topic of the all"
					putserv "NOTICE $nick :   channels where the bot is to a random topic."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"rttimer" {
				if {[matchattr $hand $rt_timerflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}rttimer <option> \[<interval> \[channel\]\]"
					putserv "NOTICE $nick :   will start a timer that changes the topic of all"
					putserv "NOTICE $nick :   channels where your bot is or only the topic of the"
					putserv "NOTICE $nick :   channel you have specified."
					putserv "NOTICE $nick :   Possible options:"
					putserv "NOTICE $nick :      -rt    - Changes the topic of the channel you have specified."
					putserv "NOTICE $nick :      -rtall - Changes the topic of all channels."
					putserv "NOTICE $nick :      -stat  - Shows the status of the timer."
					putserv "NOTICE $nick :      -stop  - Stops the timer."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"addrt" {
				if {[matchattr $hand $rt_taddflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}addrt <topic>"
					putserv "NOTICE $nick :   will add a topic you have specified to the database."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"delrt" {
				if {[matchattr $hand $rt_tdelflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}delrt <topic>"
					putserv "NOTICE $nick :   will remove the topic you have specified from the database."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"rmrt" {
				if {[matchattr $hand $rt_trmflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}rmrt"
					putserv "NOTICE $nick :   will remove $rt_tfile and $rt_htmlfile."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"sendrt" {
				if {[matchattr $hand $rt_sendflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}sendrt \[nick\]"
					putserv "NOTICE $nick :   will send to you or to the nick" 
					putserv "NOTICE $nick :   you have specified $rt_tfile file."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"listrt" {
				if {[matchattr $hand $rt_listflag]} {
					putserv "NOTICE $nick :###  ${rt_cmdpfix}listrt \[nick\]"
					putserv "NOTICE $nick :   will list all the topics in the database" 
					putserv "NOTICE $nick :   to you or to the nick you have specified." 					
				} else {
					putserv "NOTICE $nick :No help available on '$command'."	
				}
			}
			"default" { 
				putserv "NOTICE $nick :No help available on '$command'."
			}
		}
	}
return 1
}

### MSG Commands ###

proc msg:rt_topic {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set chan [lindex [split $arg] 0]
set topic [join [lrange [split $arg] 1 end]]
	if {$chan == ""} {
		putserv "NOTICE $nick :\[4x]: usage: topic <channel> \[topic\]"
	} else {
		if {![rt_botonchan $chan]} {
			putserv "NOTICE $nick :\[4x]: I'm not on $chan."
		} else {
			if {[botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]]} {
				if {$topic == ""} { 
					putserv "TOPIC $chan :" 
				} else {
					putserv "TOPIC $chan :$topic"
				}
			} else {
				putserv "NOTICE $nick :\[4x]: unable to change topic of $chan."
			}
		}
	}
return 1
}

proc msg:rt_rt {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set chan [lindex [split $arg] 0]
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {$chan == ""} {
			putserv "NOTICE $nick :\[4x]: usage: rt <channel>"
		} else {
			if {![rt_botonchan $chan]} {
				putserv "NOTICE $nick :\[4x]: I'm not on $chan."
			} else {
				set topic [rt_gettopic]
				if {($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					putserv "TOPIC $chan :$topic" 
				} else {
					putserv "NOTICE $nick :\[4x]: unable to change topic of $chan."
				}
			}
		}
	}
return 1
}

proc msg:rt_rtall {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans rt_onlyblank
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set chanlist ""
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		set topic [rt_gettopic]
		foreach chan [channels] {
			if {$rt_nochans != ""} { 
				if {[lsearch -exact [split [string tolower $rt_nochans]] [string tolower $chan]] != -1} {
					continue
				}
			}
			if {$rt_onlyblank} {
				if {[topic $chan] == ""} {
					if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
						if {!$rt_sametopic} { 
							set topic [rt_gettopic] 
						} 
						putserv "TOPIC $chan :$topic" 
					}
				}
			} else {
				if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					if {!$rt_sametopic} { 
						set topic [rt_gettopic] 
					} 
					putserv "TOPIC $chan :$topic" 
				}
			}
		}
		if {$chanlist != ""} {
			if {[llength $chanlist] == 1} {
				putserv "NOTICE $nick :\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channel)"
			} else {
				putserv "NOTICE $nick :\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channels)"
			}
		} else {
			putserv "NOTICE $nick :\[4x]: changed topic of 0 channels."
		}
	}
return 1
}

proc msg:rt_rttimer {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_interval rt_chan rt_option
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set option [string tolower [lindex [split $arg] 0]]
set interval [lindex [split $arg] 1]
set chan [lindex [split $arg] 2]
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {($option != "-stat") && ($option != "-stop") && (($interval == "") || ($option == "") || ((($option != "-rt") || ($chan == "")) && ($option != "-rtall")))} {
			putserv "NOTICE $nick :\[4x]: usage: rttimer <option> \[<interval> \[channel\]\]"
		} elseif {($option == "-rt") && ($chan != "") && ![rt_botonchan $chan]} {
			putserv "NOTICE $nick :\[4x]: I'm not on $chan"
		} elseif {$option == "-stat"} {
			if {[string match *rt_timer* [timers]] && ([info exists rt_interval] && [info exists rt_chan])} {
				if {$rt_option == "-rt"} {
					putserv "NOTICE $nick :\[4x]: timer is running! changing topic of $rt_chan every $rt_interval mins."
				} elseif {$rt_option == "-rtall"} {
					putserv "NOTICE $nick :\[4x]: timer is running! changing topic of all channels every $rt_interval mins."
				}
			} else {
				putserv "NOTICE $nick :\[4x]: timer is not running!"
			}
		} elseif {$option == "-stop"} {
			if {[string match *rt_timer* [timers]]} {
				rt_stoptimer
				putserv "NOTICE $nick :\[4x]: timer stopped!"
			} else {
				putserv "NOTICE $nick :\[4x]: timer is already stopped!"
			}
		} else {
			if {![rt_isnum $interval]} {
				putserv "NOTICE $nick :\[4x]: '$interval' is not a valid interval."
			} else {
				if {$option == "-rt"} {
					rt_stoptimer
					rt_timer $option $interval $chan
					putserv "NOTICE $nick :\[4x]: timer started! changing topic of $chan every $interval mins."
				} elseif {$option == "-rtall"} {
					rt_stoptimer
					rt_timer $option $interval "*"
					putserv "NOTICE $nick :\[4x]: timer started! changing topic of all channels every $interval mins."
				}
			}
		}
	}
return 1
}

proc msg:rt_addrt {nick uhost hand arg}  {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set arg [join [lrange [split $arg] 0 end]]
	if {$arg == ""} {
		putserv "NOTICE $nick :\[4x]: usage: addrt <topic>"
	} else {
		rt_addtopic $arg
		putserv "NOTICE $nick :\[4x]: a topic was added to $rt_tfile."
	}
return 1
}

proc msg:rt_delrt {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set arg [join [lrange [split $arg] 0 end]]
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putserv "NOTICE $nick :\[4x]: unable to read from file $rt_tfile."
	} else {
		if {$arg == ""} {
			putserv "NOTICE $nick :\[4x]: usage: delrt <topic>"
		} else {
			if {[rt_deltopic $arg]} { 
				putserv "NOTICE $nick :\[4x]: a topic was deleted from $rt_tfile."
			} else {
				putserv "NOTICE $nick :\[4x]: unable to remove topic from $rt_tfile."
			}
		}
	}
return 1
}

proc msg:rt_rmrt {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
	if {[rt_delfile $rt_tfile]} {
		putserv "NOTICE $nick :\[4x]: $rt_tfile was succesfully deleted."
	} else {
		putserv "NOTICE $nick :\[4x]: unable to delete $rt_tfile."
	}
	if {[rt_delfile $rt_htmlfile]} {
		putserv "NOTICE $nick :\[4x]: $rt_htmlfile was succesfully deleted."
	} else {
		putserv "NOTICE $nick :\[4x]: unable to delete $rt_htmlfile."
	}
return 1
}	

proc msg:rt_sendrt {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
	if {![string match "*dccsend*" [string tolower [info commands]]]} {
		putserv "NOTICE $nick :\[4x]: unable to use DCC send. check that the transfer module is loaded."
	} else {
		if {[lindex [split $arg] 0] != ""} { set nick [lindex [split $arg] 0] }
		switch -exact -- [dccsend $rt_tfile $nick] {
			0 { putserv "NOTICE $nick :\[4x]: sending you $rt_tfile ([expr [file size $rt_tfile] /1024] KB)"}
			1 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (too many connections)"              }
			2 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (can't open socket)"                 }
			3 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (file doesn't exist)"                }
			4 { putserv "NOTICE $nick :\[4x]: unable to send $rt_tfile (user has too many transfers)"       }
		}
	}
return 1
}

proc msg:rt_listrt {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set amount 0
	if {![file exists $rt_tfile]} { 
		putserv "NOTICE $nick :\[4x]: $rt_tfile doesn't exist."
	} else {
		if {[lindex [split $arg] 0] != ""} { set nick [lindex [split $arg] 0] }
		putserv "$rt_listmethod $nick :\[4x]: -- Listing contents of $rt_tfile:"
		set source [open $rt_tfile r]
		while {![eof $source]} {
			set text [gets $source]
			if {$text != ""} {
				incr amount
				putserv "$rt_listmethod $nick : $text"
			}
		}
		close $source
		putserv "$rt_listmethod $nick :\[4x]: -- Listing done ($amount topics)."
	}
return 1
}

proc msg:rt_rthelp {nick uhost hand arg} {
global rt_tfile rt_listmethod rt_login rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_ver rt_tflag rt_rtallflag rt_taddflag rt_tdelflag rt_trmflag rt_sendflag rt_listflag rt_timerflag
if {$rt_login} { if {![rt_login_check $hand]} { return 0 } }
set command [lindex [split $arg] 0]
	if {![matchattr $hand $rt_tflag] && ![matchattr $hand $rt_taddflag] && ![matchattr $hand $rt_tdelflag] && ![matchattr $hand $rt_trmflag] && ![matchattr $hand $rt_sendflag] && ![matchattr $hand $rt_listflag]} {
		return 0
	} else {
		switch -exact -- [string tolower $command] {
			"" {
				putserv "NOTICE $nick :\[4x]: Utility, Random Topic v$rt_ver commands:"
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :   topic <channel> \[topic\]"
					putserv "NOTICE $nick :   rt <channel>"
				}
				if {[matchattr $hand $rt_rtallflag } { putserv "NOTICE $nick :   rtall" }
				if {[matchattr $hand $rt_timerflag } { putserv "NOTICE $nick :   rttimer <option> \[<interval> \[channel\]\]" }
				if {[matchattr $hand $rt_taddflag]} { putserv "NOTICE $nick :   addrt <topic>" }
				if {[matchattr $hand $rt_tdelflag]} { putserv "NOTICE $nick :   delrt <topic>" }
				if {[matchattr $hand $rt_trmflag]} { putserv "NOTICE $nick :   rmrt" }
				if {[matchattr $hand $rt_sendflag]} { putserv "NOTICE $nick :   sendrt \[nick\]" }
				if {[matchattr $hand $rt_listflag]} { putserv "NOTICE $nick :   listrt \[nick\]" }
				putserv "NOTICE $nick :To get more help on individual commands, type: 'rthelp <command>'"
			}
			"topic" {
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :###  topic <channel> \[topic\]"
					putserv "NOTICE $nick :   will change or clear the topic of the channel you have specified."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"rt" {
				if {[matchattr $hand $rt_tflag]} {
					putserv "NOTICE $nick :###  rt <channel>"
					putserv "NOTICE $nick :   will change the topic of the channel"
					putserv "NOTICE $nick :   you have specified to a random topic."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"rtall" {
				if {[matchattr $hand $rt_rtallflag]} {
					putserv "NOTICE $nick :###  rtall"
					putserv "NOTICE $nick :   will change the topic of the all"
					putserv "NOTICE $nick :   channels where the bot is to a random topic."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"rttimer" {
				if {[matchattr $hand $rt_timerflag]} {
					putserv "NOTICE $nick :###  rttimer <option> \[<interval> \[channel\]\]"
					putserv "NOTICE $nick :   will start a timer that changes the topic of all"
					putserv "NOTICE $nick :   channels where your bot is or only the topic of the"
					putserv "NOTICE $nick :   channel you have specified."
					putserv "NOTICE $nick :   Possible options:"
					putserv "NOTICE $nick :      -rt    - Changes the topic of the channel you have specified."
					putserv "NOTICE $nick :      -rtall - Changes the topic of all channels."
					putserv "NOTICE $nick :      -stat  - Shows the status of the timer."
					putserv "NOTICE $nick :      -stop  - Stops the timer."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"addrt" {
				if {[matchattr $hand $rt_taddflag]} {
					putserv "NOTICE $nick :###  addrt <topic>"
					putserv "NOTICE $nick :   will add a topic you have specified to the database."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"delrt" {
				if {[matchattr $hand $rt_tdelflag]} {
					putserv "NOTICE $nick :###  delrt <topic>"
					putserv "NOTICE $nick :   will remove the topic you have specified from the database."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"rmrt" {
				if {[matchattr $hand $rt_trmflag]} {
					putserv "NOTICE $nick :###  rmrt"
					putserv "NOTICE $nick :   will remove $rt_tfile file and $rt_htmlfile file."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"sendrt" {
				if {[matchattr $hand $rt_sendflag]} {
					putserv "NOTICE $nick :###  sendrt \[nick\]"
					putserv "NOTICE $nick :   will send to you or to the nick" 
					putserv "NOTICE $nick :   you have specified $rt_tfile file."
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"listrt" {
				if {[matchattr $hand $rt_listflag]} {
					putserv "NOTICE $nick :###  listrt \[nick\]"
					putserv "NOTICE $nick :   will list all the topics in the database" 
					putserv "NOTICE $nick :   to you or to the nick you have specified." 
				} else {
					putserv "NOTICE $nick :No help available on '$command'."
				}
			}
			"default" { 
				putserv "NOTICE $nick :No help available on '$command'."
			}
		}
	}
return 1
}

### DCC Commands ### 

proc dcc:rt_rt {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
set chan [lindex [split $arg] 0]
	putcmdlog "#$hand# rt $arg"
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putidx $idx "\[4x]: unable to read from file $rt_tfile."
	} else {
		if {$chan == ""} { set chan [lindex [console $idx] 0] }
		if {$chan == ""} {
			putidx $idx "\[4x]: usage: .rt <channel>"
		} else {
			if {![rt_botonchan $chan]} {
				putidx $idx "\[4x]: I'm not on $chan."
			} else {
				set topic [rt_gettopic]
				if {($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					putserv "TOPIC $chan :$topic"
					putidx $idx "\[4x]: topic of $chan changed." 
				} else {
					putidx $idx "\[4x]: unable to change topic of $chan."
				}
			}
		}
	}
}

proc dcc:rt_rtall {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans rt_onlyblank
set chanlist ""
	putcmdlog "#$hand# rtall $arg"
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putidx $idx "Unable to read from file $rt_tfile."
	} else {
		set topic [rt_gettopic]
		foreach chan [channels] {
			if {$rt_nochans != ""} { 
				if {[lsearch -exact [split [string tolower $rt_nochans]] [string tolower $chan]] != -1} {
					continue
				}
			}
			if {$rt_onlyblank} {
				if {[topic $chan] == ""} {
					if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
						lappend chanlist $chan
						if {!$rt_sametopic} {
							set topic [rt_gettopic] 
						} 
						putserv "TOPIC $chan :$topic"
					}
				}
			} else {
				if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					lappend chanlist $chan
					if {!$rt_sametopic} {
						set topic [rt_gettopic] 
					} 
					putserv "TOPIC $chan :$topic"
				}
			}
		}
		if {$chanlist != ""} {
			if {[llength $chanlist] == 1} {
				putidx $idx "\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channel)"
			} else {
				putidx $idx "\[4x]: changed topic of [join $chanlist ", "] ([llength $chanlist] channels)"
			}
		} else {
			putidx $idx "\[4x]: changed topic of 0 channels."
		}
	}
}

proc dcc:rt_rttimer {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_interval rt_chan rt_option
set option [string tolower [lindex [split $arg] 0]]
set interval [lindex [split $arg] 1]
	if {[lindex [split $arg] 2] != ""} { 
		set chan [lindex [split $arg] 2]
	} else {
		set chan [lindex [console $idx] 0]
	}
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putidx $idx "\[4x]: unable to read from file $rt_tfile."
	} else {
		if {($option != "-stat") && ($option != "-stop") && (($interval == "") || ($option == "") || ((($option != "-rt") || ($chan == "")) && ($option != "-rtall")))} {
			putidx $idx "Usage: .rttimer <option> \[<interval> \[channel\]\]"
		} elseif {($option == "-rt") && ($chan != "") && ![rt_botonchan $chan]} {
			putidx $idx "\[4x]: I'm not on $chan"
		} elseif {$option == "-stat"} {
			if {[string match *rt_timer* [timers]] && ([info exists rt_interval] && [info exists rt_chan])} {
				if {$rt_option == "-rt"} {
					putidx $idx "\[4x]: timer is running! changing topic of $rt_chan every $rt_interval mins."
				} elseif {$rt_option == "-rtall"} {
					putidx $idx "\[4x]: timer is running! changing topic of all channels every $rt_interval mins."
				}
			} else {
				putidx $idx "\[4x]: timer is not running!"
			}
		} elseif {$option == "-stop"} {
			if {[string match *rt_timer* [timers]]} {
				rt_stoptimer
				putidx $idx "\[4x]: timer stopped!"
			} else {
				putidx $idx "\[4x]: timer is already stopped!"
			}
		} else {
			if {![rt_isnum $interval]} {
				putidx $idx "\[4x]: '$interval' is not a valid interval."
			} else {
				if {$option == "-rt"} {
					rt_stoptimer
					rt_timer $option $interval $chan
					putidx $idx "\[4x]: timer started! changing topic of $chan every $interval mins."
				} elseif {$option == "-rtall"} {
					rt_stoptimer
					rt_timer $option $interval "*"
					putidx $idx "\[4x]: timer started! changing topic of all channels every $interval mins."
				}
			}
		}
	}
return 1
}

proc dcc:rt_addrt {hand idx arg}  {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
set arg [join [lrange [split $arg] 0 end]]
	putcmdlog "#$hand# addrt $arg"
	if {$arg == ""} {
		putidx $idx "\[4x]: usage: .addrt <topic>"
	} else {
		rt_addtopic $arg
		putidx $idx "\[4x]: a topic was added to $rt_tfile."
	} 
}

proc dcc:rt_delrt {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
set arg [join [lrange [split $arg] 0 end]]
	putcmdlog "#$hand# delrt $arg"
	if {(![file exists $rt_tfile]) || (![file readable $rt_tfile])} { 
		putidx $idx "\[4x]: unable to read from file $rt_tfile."
	} else {
		if {$arg == ""} {
			putidx $idx "\[4x]: usage: .delrt <topic>"
		} else {
			if {[rt_deltopic $arg]} { 
				putidx $idx "\[4x]: a topic was deleted from $rt_tfile."
			} else {
				putidx $idx "\[4x]: unable to remove topic from $rt_tfile."
			}
		}
	}
}

proc dcc:rt_rmrt {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
	putcmdlog "#$hand# rmrt $arg"
	if {[rt_delfile $rt_tfile]} { 
		putidx $idx "\[4x]: unable to delete $rt_tfile." 
	} else { 
		putidx $idx "\[4x]: $rt_tfile was succesfully deleted." 
	}
	if {[rt_delfile $rt_htmlfile]} { 
		putidx $idx "\[4x]: unable to delete $rt_htmlfile." 
	} else { 
		putidx $idx "\[4x]: $rt_htmlfile was succesfully deleted." 
	}
}	

proc dcc:rt_sendrt {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
set nick [lindex [split $arg] 0]
	putcmdlog "#$hand# sendrt $arg"
	if {![string match "*dccsend*" [string tolower [info commands]]]} {
		putidx $idx "\[4x]: unable to use DCC send. check that the transfer module is loaded."
	} else {
		if {$nick == ""} { set nick [rt_getnick $hand] }
		if {$nick != ""} {
			switch -exact -- [dccsend $rt_tfile $nick] {
				0 { putidx $idx "\[4x]: sending $rt_tfile to $nick ([expr [file size $rt_tfile] /1024] KB)" }
				1 { putidx $idx "\[4x]: unable to send $rt_tfile (too many connections)"                    } 
				2 { putidx $idx "\[4x]: unable to send $rt_tfile (can't open socket)"                       } 
				3 { putidx $idx "\[4x]: unable to send $rt_tfile (file doesn't exist)"                      } 
				4 { putidx $idx "\[4x]: unable to send $rt_tfile (user has too many transfers)"             }
			}
		}
	} else {
		putidx $idx "\[4x]: can't find your nickname - use .sendrt <nick> instead."
	}
}

proc dcc:rt_listrt {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
set amount 0
	putcmdlog "#$hand# listrt $arg"
	if {![file exists $rt_tfile]} { 
		putidx $idx "\[4x]: $rt_tfile doesn't exist."
	} else {
		putidx $idx "\[4x]: -- Listing contents of $rt_tfile:"
		set source [open $rt_tfile r]
		while {![eof $source]} {
			set text [gets $source]
			if {$text != ""} {
				incr amount
				putidx $idx " $text"
			}
		}
		close $source
		putidx $idx "\[4x]: -- Listing done ($amount topics)."
	}
}

proc dcc:rt_rthelp {hand idx arg} {
global rt_tfile rt_sametopic rt_makehtml rt_htmlfile rt_nochans
global rt_ver rt_tflag rt_rtallflag rt_taddflag rt_tdelflag rt_trmflag rt_sendflag rt_listflag rt_timerflag
set command [lindex [split $arg] 0]
	if {![matchattr $hand $rt_tflag] && ![matchattr $hand $rt_taddflag] && ![matchattr $hand $rt_tdelflag] && ![matchattr $hand $rt_trmflag] && ![matchattr $hand $rt_sendflag] && ![matchattr $hand $rt_listflag]} {
		putidx $idx "What?  You need '.help'"
		return 0
	} else {
		putcmdlog "#$hand# rthelp $arg"
		if {[string index $command 0] == "."} { set command [string range $command 1 end] }
		switch -exact -- [string tolower $command] {
			"" {
				putidx $idx "\[4x]: Utility, Random Topic v$rt_ver commands:"
				if {[matchattr $hand $rt_tflag]} { putidx $idx "   .rt \[channel\]" }
				if {[matchattr $hand $rt_rtallflag]} { putidx $idx "   .rtall" }
				if {[matchattr $hand $rt_timerflag]} { putidx $idx "   .rttimer <option> \[<interval> \[channel\]\]" }
				if {[matchattr $hand $rt_taddflag]} { putidx $idx "   .addrt <topic>" }
				if {[matchattr $hand $rt_tdelflag]} { putidx $idx "   .delrt <topic>" }
				if {[matchattr $hand $rt_trmflag]} { putidx $idx "   .rmrt" }
				if {[matchattr $hand $rt_sendflag]} { putidx $idx "   .sendrt \[nick\]" }
				if {[matchattr $hand $rt_listflag]} { putidx $idx "   .listrt" }
				putidx $idx "To get more help on individual commands, type: '.rthelp <command>'"
			}
			"rt" {
				if {[matchattr $hand $rt_tflag]} {
					putidx $idx "###  .rt \[channel\]"
					putidx $idx "   will change the topic of the channel you have specified"
					putidx $idx "   or your current console channel to a random topic."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"rtall" {
				if {[matchattr $hand $rt_rtallflag]} {
					putidx $idx "###  .rtall"
					putidx $idx "   will change the topic of the all"
					putidx $idx "   channels where the bot is to a random topic."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"rttimer" {
				if {[matchattr $hand $rt_timerflag]} {
					putidx $idx "###  .rttimer <option> \[<interval> \[channel\]\]"
					putidx $idx "   will start a timer that changes the topic of all"
					putidx $idx "   channels where your bot is or only the topic of the"
					putidx $idx "   channel you have specified."
					putidx $idx "   Possible options:"
					putidx $idx "      -rt    - Changes the topic of the channel you have specified."
					putidx $idx "      -rtall - Changes the topic of all channels."
					putidx $idx "      -stat  - Shows the status of the timer."
					putidx $idx "      -stop  - Stops the timer."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"addrt" {
				if {[matchattr $hand $rt_taddflag]} {
					putidx $idx "###  .addrt <topic>"
					putidx $idx "   will add a topic to you have specified to the database."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"delrt" {
				if {[matchattr $hand $rt_tdelflag]} {
					putidx $idx "###  .delrt <topic>"
					putidx $idx "   will remove the topic you have specified from the database."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"rmrt" {
				if {[matchattr $hand $rt_trmflag]} {
					putidx $idx "###  .rmrt"
					putidx $idx "   will remove $rt_tfile file and $rt_htmlfile file."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"sendrt" {
				if {[matchattr $hand $rt_sendflag]} {
					putidx $idx "###  .sendrt \[nick\]"
					putidx $idx "   will send to you or to the nick" 
					putidx $idx "   you have specified $rt_tfile file."
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"listrt" {
				if {[matchattr $hand $rt_listflag]} {
					putidx $idx "###  .listrt"
					putidx $idx "   will list all the topics in the database." 
				} else {
					putidx $idx "No help available on '$command'."
				}
			}
			"default" { 
				putidx $idx "No help available on '$command'."
			}
		}
	}
}

### Other Procs ###

proc rt_getnick {hand} {
global numversion
	if {$hand != "*"} {
		if {$numversion < 1032700} {
			foreach chan [channels] {
				return [hand2nick $hand $chan]
			}
		} else {
			return [hand2nick $hand]
		}
	}
}

proc rt_botonchan {chan} {
global botnick numversion
	if {$numversion < 1032400} {
		if {[validchan $chan] && [onchan $botnick $chan]} {
			return 1
		} else {
			return 0
		}
	} else {
		if {[validchan $chan] && [botonchan $chan]} {
			return 1
		} else {
			return 0
		}
	}
}

proc rt_gettopic { } {
global rt_tfile
set topics ""
	set target [open $rt_tfile r]
	while {![eof $target]} { 
		set text [gets $target]
		if {$text != ""} {
			lappend topics $text
		}
	}
	close $target
	return [lindex $topics [rand [llength $topics]]]
}

proc rt_addtopic {arg} {
global rt_tfile rt_makehtml
	set target [open $rt_tfile a]
	puts $target $arg
	close $target
	if {$rt_makehtml} { rt_convert }
}

proc rt_deltopic {arg} {
global rt_tfile rt_makehtml
set what ""
set topicrem 0
	set source [open $rt_tfile r]
	while {![eof $source]} { 
		set topic [gets $source] 
		if {[string tolower $topic] != [string tolower $arg]} { 
			append what "$topic\n"
		} else { 
			set topicrem 1 
		} 
	}
	close $source
	set target [open $rt_tfile w]
	puts $target $what
	close $target
	if {$rt_makehtml} { rt_convert }
	return $topicrem
}

proc rt_delfile {file} {
	if {[file exists $file]} {
		file delete -force $file
		if {[file exists $file]} {
			return 0
		} else {
			return 1
		}
	} else {
		return 0
	}
}

proc rt_isnum {number} {
set i 0
	while {$i < [string length $number]} {
		if {[lsearch -exact "1 2 3 4 5 6 7 8 9 0" [string index $number $i]] == -1} {
			return 0
		}
		incr i
	}
	return 1
}

proc rt_timer {option interval chan} {
global rt_logtimer rt_interval rt_chan rt_option
	if {![string match *rt_timer* [timers]]} { 
		timer $interval "rt_timer $option $interval $chan" 
		set rt_option $option
		set rt_interval $interval
		set rt_chan $chan
	} 
	if {$option == "-rt"} { 
		rt_dort $chan
	} elseif {$option == "-rtall"} { 
		rt_dortall
	}
}

proc rt_stoptimer { } {
	foreach timer [timers] {
		if {[string match *rt_timer* $timer]} { 
			killtimer [lindex $timer 2]
		}
	}
}

proc rt_dort {chan} {
global rt_logtimer rt_tmronlyblank
	set topic [rt_gettopic]
	if {$rt_tmronlyblank} {
		if {[topic $chan] == ""} {
			if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
				putserv "TOPIC $chan :$topic"
				if {$rt_logtimer} { putlog "\[4x]: RandTopic changed topic of $chan." }
			} else {
				if {$rt_logtimer} { putlog "\[4x]: RandTopic unable to change topic of $chan." }
			}
		}
	} else {
		if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
			putserv "TOPIC $chan :$topic"
			if {$rt_logtimer} { putlog "\[4x]: RandTopic changed topic of $chan." }
		} else {
			if {$rt_logtimer} { putlog "\[4x]: RandTopic unable to change topic of $chan." }
		}
	}
}

proc rt_dortall { } {
global rt_logtimer rt_sametopic rt_nochans rt_tmronlyblank
set chanlist ""
	set topic [rt_gettopic]
	foreach chan [channels] {
		if {$rt_nochans != ""} { 
			if {[lsearch -exact [split [string tolower $rt_nochans]] [string tolower $chan]] != -1} {
				continue
			}
		}
		if {$rt_tmronlyblank} {
			if {[topic $chan] == ""} {
				if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
					lappend chanlist $chan
					if {!$rt_sametopic} {
						set topic [rt_gettopic] 
					} 
					putserv "TOPIC $chan :$topic"
				}
			}
		} else {
			if {[rt_botonchan $chan] && ($topic != "") && ([botisop $chan] || ![string match *t* [lindex [getchanmode $chan] 0]])} {
				lappend chanlist $chan
				if {!$rt_sametopic} {
					set topic [rt_gettopic] 
				} 
				putserv "TOPIC $chan :$topic"
			}
		}
	}
	if {$rt_logtimer} {
		if {$chanlist != ""} {
			if {[llength $chanlist] == 1} {
				putlog "\[4x]: RandTopic changed topic of [join $chanlist ", "] ([llength $chanlist] channel)"
			} else {
				putlog "\[4x]: RandTopic changed topic of [join $chanlist ", "] ([llength $chanlist] channels)"
			}
		} else {
			putlog "\[4x]: RandTopic changed topic of 0 channels."
		}
	}
}

proc rt_convert { } {
global rt_tfile rt_htmlfile rt_bodytag rt_styletag rt_ver
set what ""
	if {([file exists $rt_tfile]) && ([file readable $rt_tfile])} {
		set source [open $rt_tfile r]
		append what "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
		append what "<HTML>\n"
		append what "<HEAD>\n"
		append what "	<TITLE>Random Topics</TITLE>\n"
		append what "\n"
		append what "<META http-equiv=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n"
		append what "\n"
		if {$rt_styletag != ""} { 
			append what "$rt_styletag\n" 
			append what "\n"
		}
		append what "</HEAD>\n"
		append what "\n"
		append what "$rt_bodytag\n"
		append what "\n"
		append what "<BR>\n"
		append what "\n"
		append what "<CENTER>\n"
		append what "<FONT FACE=\"Verdana\" SIZE=\"6\"><B>Random Topics</B></FONT><BR>\n"
		append what "<FONT FACE=\"Verdana\" SIZE=\"2\">created at [ctime [unixtime]]</FONT>\n"
		append what "</CENTER>\n"
		append what "\n"
		append what "<BR><BR>\n"
		append what "\n"
		while {![eof $source]} {
			set text [gets $source]
			if {$text != ""} {
				append what "$text<BR>\n"
			}
		}
		append what "\n"
		append what "<BR><BR>\n"
		append what "\n"
		append what "<CENTER><FONT FACE=\"Verdana\" SIZE=\"2\"><B>This file was automatically generated by <A HREF=\"http://www.iki.fi/temex/tcls/\" TARGET=\"_top\" TITLE=\"randtopic.tcl\">randtopic.tcl</A> v$rt_ver by <A HREF=\"mailto:temex@iki.fi?subject=randtopic.tcl v$rt_ver\" TITLE=\"Sup\">Sup</A></B></FONT></CENTER>\n"
		append what "\n"
		append what "<BR><BR>\n"
		append what "\n"
		append what "</BODY>\n"
		append what "</HTML>"
		close $source
		set target [open $rt_htmlfile w]
		puts $target $what
		close $target
		unset what
	}
}

### End ###

if {$rt_makehtml} { rt_convert }

putlog "\[4x]: Utility, Random Topic. Loaded."