Language: EN FR PT ES NL DE SV RU
######################################################################
#                                                                    #
#====================================================================#
# gt-support.tcl by slackin                                          #
#====================================================================#
# Slackin's mass commands script!                                    #
# Custom written for #pugbot & GT                                    #
######################################################################
#                                                                    #
######################################################################
######################################################################
# Config section, please edit with                                   #
# proper info                                                        #
#--------------------------------------------------------------------#
 
### Define data directory
set dir "./support"
 
### Define channels
set adminchannel "#pugbot-priv"
set supportchannel "#pugbot.support"
 
#--------------------------------------------------------------------#
# End of config section                                              #
######################################################################
######################################################################
 
# Bind's for the commands and catches
bind pub * .support pub_support
bind pub * !support pub_support
bind pub o .res res_done
bind pub o .r support_reply
bind pub o !res res_done
bind pub o !r support_reply
bind msgm * * pos_reply
bind pub o .queue list_queue
bind pub o .hold place_hold
bind pub o !queue list_queue
bind pub o !hold place_hold
bind part * * request_part
bind sign * * request_part
bind pub o !supporthelp support_help
bind pub o .supporthelp support_help
 
proc support_help {nick host hand chan arg} {
	puthelp "NOTICE $nick :GT-Support ----------------------------------------"
	puthelp "NOTICE $nick :Support request system by slackin"
	puthelp "NOTICE $nick :Admin functions help"
	puthelp "NOTICE $nick :---------------------------------------------------"
	puthelp "NOTICE $nick :To reply to a request       : .r <RID> <MESSAGE>"
	puthelp "NOTICE $nick :To close a request          : .res <RID>"
	puthelp "NOTICE $nick :To force a close            : .res <RID> f"
	puthelp "NOTICE $nick :To place a request on hold  : .hold <RID>"
	puthelp "NOTICE $nick :To list the current queue   : .queue"
	puthelp "NOTICE $nick :---------------------------------------------------"
	puthelp "NOTICE $nick :All messages are logged"
	puthelp "NOTICE $nick :When a request is closed, who closed it is logged"
	puthelp "NOTICE $nick :Only admins can use these functions"
	puthelp "NOTICE $nick :When a request is inactive all messages from the"
	puthelp "NOTICE $nick :user are discarded, when you place a request on"
	puthelp "NOTICE $nick :hold it is inactive. Replying to a request makes"
	puthelp "NOTICE $nick :that request active"
	puthelp "NOTICE $nick :---------------------------------------------------"
	puthelp "NOTICE $nick :If you need any further help, please ask your admin"
}
 
# When a user parts
proc request_part {nick uhost hand chan msg} {
	global adminchannel
 
	# Clean nick for file name user
	set clnick [gt_cleannick $nick]
 
	# Load RID info
	set queuedata [check_queue 0 $clnick]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	if {$nickrid != "0"} {
		puthelp "PRIVMSG $adminchannel :9,1REQUEST ID #$nickrid DELETED, user parted channel $chan"
		log_msgs "cls" $nickrid $clnick ""
		update_queue "del" $nickrid "" ""
	}
}
 
# Place caller on hold
proc place_hold {nick host hand chan arg} {
	global dir adminchannel supportchannel
 
	# Clean nick for file name user
	set clnick [gt_cleannick $nick]
 
	# Load RID info
	set queuedata [check_queue $arg 0]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	# Check active flag
	if {$nickact != "1"} {
		putserv "PRIVMSG $adminchannel :8,1REQUEST ID NOT ACTIVE"
	return 1
	}
 
	putserv "PRIVMSG $nicknick :9,1You have been placed on hold, all incoming message are discarded, please wait for admin to respond"
	putserv "PRIVMSG $adminchannel :9,1REQUEST ID #$arg PLACE ON HOLD"
	log_msgs "hold" $nickrid $clnick ""
	update_queue "upd" $nickrid $nicknick "0"
return 1
}
 
# Process incomming messages
proc pos_reply {nick host hand arg} {
	global dir adminchannel supportchannel
 
	# Clean nick for file name user
	set clnick [gt_cleannick $nick]
 
	# Load RID info
	set queuedata [check_queue 0 $clnick]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	# Make sure rid came back properly
	if {$nickrid == "0"} {
		puthelp "PRIVMSG $nick :8,1REQUEST ID NOT FOUND"
 
	return 1
	}
 
	# Check if active
	if {$nickact != "1"} {
		puthelp "PRIVMSG $nick :9,1You are currently in queue, please wait for help."
 
	return 1
	}
 
	# Relay message to channel
	puthelp "PRIVMSG $adminchannel :#$nickrid [join $clnick]: [join $arg]"
	log_msgs "app" $nickrid $nicknick $arg
}
 
# Reply to help request from the admins
proc support_reply {nick host hand chan arg} {
	global dir adminchannel supportchannel
	set clnick [gt_cleannick $nick]
 
	# Parse args
	set rid [lindex $arg 0]
	set replymsg [lrange $arg 1 end]
 
	# Load RID info
	set queuedata [check_queue $rid 0]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	# Make sure rid came back properly
	if {$nickrid == "0"} {
		putserv "PRIVMSG $adminchannel :8,1REQUEST ID NOT FOUND"
 
	return 1
	}
 
	# Check if active
	if {$nickact != "1"} {
		update_queue "upd" $nickrid $nicknick "1"
	}
 
	# Relay message
	putserv "PRIVMSG $nicknick :#$nickrid: [join $replymsg]"
	log_msgs "app" $nickrid $clnick $replymsg
 
return 1
}
 
# Resolved help issue clean up function
proc res_done {nick host hand chan arg} {
	global dir adminchannel
	set clnick [gt_cleannick $nick]
	set rid [lindex $arg 0]
	set forced [lindex $arg 1]
 
	# Get active status, pid, current user
	set queuedata [check_queue $rid 0]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	# Check RID to make sure its real
	if {$nickrid == "0"} {
		putserv "PRIVMSG $adminchannel :8,1INVALID REQUEST ID"
 
	return 1
	}
	# Check RID to make sure its active
	if {$nickact != "1" && $forced != "f"} {
		putserv "PRIVMSG $adminchannel :8,1REQUEST ID #$rid IS NOT ACTIVE, PLEASE MAKE SURE IT HAS BEEN ATTENDED TO"
 
	return 1
	}
	putserv "PRIVMSG $nicknick :9,1Your request has been deleted"
	putserv "PRIVMSG $adminchannel :9,1REQUEST ID #$rid DELETED"
	log_msgs "cls" $rid $clnick ""
	update_queue "del" $rid "" ""
 
return 1
}
 
# Public command function
proc pub_support {nick host hand channel arg} {
	global dir adminchannel
 
	if {$arg == ""} {
		puthelp "PRIVMSG $nick :9,1Please put your problem in the request, IE:"
		puthelp "PRIVMSG $nick :9,1.support I am having problems loging into pugbot."
	return 1
	}
 
	# Clean nick for file name user
	set clnick [gt_cleannick $nick]
 
	# Get active status, pid, current user
	set queuedata [check_queue 0 $clnick]
	set nickrid [lindex $queuedata 0]
	set nicknick [lindex $queuedata 1]
	set nickact [lindex $queuedata 2]
 
	# Check nick RID
	if {$nickrid != "0"} {
 
		# If act then tellem to talk to the bot, if not act then tellem to wait
		if {$nickact != "0"} {
			puthelp "PRIVMSG $clnick :9,1$nick you already have active request open, please direct further input to the bot"
		return 1
		} else {
			puthelp "PRIVMSG $clnick :9,1$nick you are currently in queue, please wait your turn"
		return 1
		}
	} else {
 
		# If no RID found then add to queue and notify
		set queueinfo [update_queue add 0 $clnick 0]
		set nickrid [lindex $queueinfo 0]
		set nickpos [lindex $queueinfo 1]
		putserv "PRIVMSG $adminchannel :9,1NEW HELP REQUEST FROM $clnick, REQUEST ID #$nickrid"
		putserv "PRIVMSG $adminchannel :[join $clnick]: [join $arg]"
 
		# Start log with new message
		log_msgs "new" $nickrid $clnick $arg
 
		# Save the question for queue info
		set reqfile [open $dir/$nickrid.req w]
		puts $reqfile $arg
		close $reqfile
 
		# If active user then tell him he's active, if not tell'em to wait
		if {$nickpos == "1"} {
			puthelp "PRIVMSG $nick :9,1You are currently waiting for an admin response, your request ID is $nickrid"
		return 1
		} else {
			puthelp "PRIVMSG $nick :9,1You have been placed in queue, your request ID is $nickrid"
			puthelp "PRIVMSG $nick :9,1You are currently $nickpos in line"
		return 1
		}
	}
}
 
# Log function
proc log_msgs {cmd rid clnick msg} {
	global dir
	set log [open $dir/$rid.log a]
	switch $cmd {
		new {
			puts $log "Start of new request ID #$rid"
			puts $log "---------------------------------------------------------------"
			puts $log "$clnick :$msg"
 
		}
		app {
			puts $log "$clnick :$msg"
		}
		cls {
			puts $log "---------------------------------------------------------------"
			puts $log "End of request ID #$rid. Request ended by $clnick"
			puts $log ""
			puts $log ""
			puts $log ""
		}
		hold {
			puts $log "---------------------------------------------------------------"
			puts $log "Placed on hold by $clnick"
			puts $log "---------------------------------------------------------------"
		}
	}
	close $log
}
 
# Update queue
proc update_queue {cammand rid clnick activate} {
	# Load globals
	global dir
 
	# Load queue file in to an array
	set qufile [open $dir/queue.support r]
	for {set cnt 0} { ![eof $qufile] } { incr cnt } {
		gets $qufile tdata($cnt)
	}
	close $qufile
	putlog "GT-Support.-02 DEBUG INFO: pre-switch"
	# Run code for command given
	switch $cammand {
		add	{
 
			# Command add code, start by checking if queue is empty
			if {$tdata(0) != ""} {
 
				# If not empty get the last RID
				set lastrid [lindex $tdata([expr $cnt - 2]) 0]
 
				# Set new RID
				set newrid [expr $lastrid + 1]
 
				# Set the position
				set position $cnt
 
				putlog "GT-Support.-02 DEBUG INFO: $tdata(0) $tdata([expr $cnt - 2]) $lastrid $newrid $position"
				set qufile [open $dir/queue.support a]
				puts $qufile "$newrid $clnick 0"
				close $qufile
 
			} else {
 
				# If queue is empty, set the RID at one
				set newrid 1
 
				# Set the position
				set position 1
 
				putlog "GT-Support.-02 DEBUG INFO: $tdata(0) $newrid $position"
				set qufile [open $dir/queue.support w]
				puts $qufile "$newrid $clnick 0"
				close $qufile
 
			}
 
		# Return new rid and position
		return "$newrid $position"
		}
		del	{
 
			# Command del code, update the queue file and remove the RID given
			set qufile [open $dir/queue.support w]
			for {set tnc 0} {$tnc != [expr $cnt - 1]} { incr tnc } {
				if {$rid != [lindex $tdata($tnc) 0]} {
					puts $qufile $tdata($tnc)
				}
			}
			close $qufile
 
		return 1
		}
		upd	{
 
			# Command upd code, update the queue file by setting act for RID given
			set qufile [open $dir/queue.support w]
			for {set tnc 0} {$tnc != [expr $cnt - 1]} { incr tnc } {
				if {$rid != [lindex $tdata($tnc) 0]} {
					puts $qufile $tdata($tnc)
				} else {
					puts $qufile "$rid $clnick $activate"
				}
			}
			close $qufile
 
		return 1
		}
		default {
 
			# If no matches on command, put error in log
			putlog "IMPROPER CALL OF update_queue FUNCTION IN GT-Support-.02"
 
		return 1
		}
	}
}
 
# List queue
proc list_queue {nick host hand chan arg} {
	# Load globals
	global dir adminchannel
 
	set qufile [open $dir/queue.support r]
	for {set cnt 0} { ![eof $qufile] } { incr cnt } {
		gets $qufile tdata($cnt)
	}
	close $qufile
	set chck "0"
	for {set tnc 0} {$tnc != [expr $cnt - 1]} { incr tnc } {
		set nickrid [lindex $tdata($tnc) 0]
		set nicknick [lindex $tdata($tnc) 1]
		set nickact [lindex $tdata($tnc) 2]
		set reqfile [open $dir/$nickrid.req r]
		gets $reqfile reqmsg
		close $reqfile
		if {$nickact != "0"} {
			set isact "yes"
		} else {
			set isact "no"
		}
 
		putserv "PRIVMSG $adminchannel :9,1Request ID #$nickrid | Active: $isact"
		putserv "PRIVMSG $adminchannel :[join $nicknick]: [join $reqmsg]"
		set chck "1"
	}
 
	if {$chck != "1"} {
		putserv "PRIVMSG $adminchannel :9,1NO SUPPORT REQUESTS IN QUEUE AT THIS TIME."
	}
 
return 1
}
 
# Check queue function
proc check_queue {id nick} {
	# Load globals
	global dir
 
	# Check for queue file before loading
	if {[file exists $dir/queue.support]} {
 
		# If file exists then parse in to an array
		set qufile [open $dir/queue.support r]
		for {set cnt 0} { ![eof $qufile] } { incr cnt } {
			gets $qufile tdata($cnt)
		}
		close $qufile
 
		# Set the search criteria
		if {$id != "0"} {
 
			# If ID is not empty then search by ID
			set searchcmd "0"
			set searchmatch $id
		} else {
 
			# If ID is empty then assume we are searching by nick
			set searchcmd "1"
			set searchmatch $nick
		}
 
		# Do the search
		set rtinfo 0
		for {set tnc 0} {$tnc != [expr $cnt - 1]} { incr tnc } {
			if {[lindex $tdata($tnc) $searchcmd] == $searchmatch} {
				set rtinfo $tdata($tnc)
			}
		}
 
		# If rtinfo is not blank, return the data, else return all 0's
		if {$rtinfo != "0"} {
 
		return $rtinfo
		} else {
 
		return "0 0 0"
		}
 
	} else {
 
		# If no queue file exists, create a blank one.
		set qufile [open $dir/queue.support w]
		close $qufile
 
	return "0 0 0"
	}
}
 
# Clean up the nick
proc gt_cleannick {nick} {
	if {[string range $nick 0 0] == "\{"} {
	      set nick "\\$nick"
	}
        return $nick
}
 
putlog "GT-support.tcl by slackin LOADED"