Language: EN FR PT ES NL DE SV RU
######################################################################
#                                                                    #
#====================================================================#
# gt-pmsys.tcl by slackin                                            #
#====================================================================#
# Slackin's cross network pm system!                                 #
# Custom written for #pugbot & GT                                    #
######################################################################
#                                                                    #
######################################################################
######################################################################
# Config section, please edit with                                   #
# proper info                                                        #
#--------------------------------------------------------------------#
 
#--------------------------------------------------------------------#
# End of config section                                              #
######################################################################
######################################################################
 
# Bind's for the commands and catches
bind msg * .pm start_pm
bind msg * !pm start_pm
#bind msg * .accept accept_pm
#bind msg * !accept accept_pm
#bind msg * .deny deny_pm
#bind msg * !deny deny_pm
bind msg * .stoppm close_pm
bind msg * !stoppm close_pm
bind msgm * * send_pm
bind pub * !pmhelp pm_help
bind pub * .pmhelp pm_help
bind pub * .finduser search_users
bind pub * !finduser search_users
bind pub - !userlist do_list_users
source ./scripts/gt-nickcol.tcl
 
proc search_users {nick host hand chan arg} {
	global network
	set msg [regsub -all {{^$(\*)}{(\*)}} $arg {{}{\%}}]
	send_msg_db $network $nick $msg "notice"
 
}
proc do_list_users {nick host hand chan arg} {
	global db relayname
	set qry "SELECT * FROM $db(tbl_session) WHERE network = '$relayname'"
	set cnt 0
	set userlist($cnt) ""
	foreach row [mysqlsel $db(sqlhand) $qry -list] {
		# quote
		set session_id [lindex $row 0]
		set db_nick [lindex $row 2]
		set nickcolor [colornick $db_nick]
		set newnick "\002\00300,01$session_id:\002$nickcolor$db_nick "
		set cur_len [string length $userlist($cnt)]
		if { $cur_len > 350 } {
			incr cnt
			set userlist($cnt) "$newnick"
		} elseif {$cur_len != 0} {
			set userlist($cnt) "$userlist($cnt) $newnick"
		} else {
			set userlist($cnt) "$newnick"
		}
	}
	for {set cnt 0} {$cnt != [array size userlist]} { incr cnt } {
		putserv "PRIVMSG $nick :$userlist($cnt)"
	}
}
 
proc pm_help {nick host hand chan arg} {
	puthelp "PRIVMSG $nick :GT-Support ----------------------------------------"
	puthelp "PRIVMSG $nick :Support request system by slackin"
	puthelp "PRIVMSG $nick :Admin functions help"
	puthelp "PRIVMSG $nick :---------------------------------------------------"
	puthelp "PRIVMSG $nick :To reply to a request       : .r <RID> <MESSAGE>"
	puthelp "PRIVMSG $nick :To close a request          : .res <RID>"
	puthelp "PRIVMSG $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"
}
 
# Process incomming messages
proc send_pm {nick host hand arg} {
	global network
	if {[get_botnum] == ""} {return 0}
	set usr(ssid) [get_ssid_by_nick $nick]
	if {[get_pmdata_by_ssid $usr(ssid)] == ""} {
		send_msg_db $network $nick "Sorry no open PMs found!" "[whichbot [get_botnum]]"
		return 0
	}
	set trgt(ssid) [get_trgt_ssid $usr(ssid)]
	if {$trgt(ssid) == ""} {
		send_msg_db $network $nick "No PMs active on this bot!" "[whichbot [get_botname]]"
		return 0
	}
	set trgt(network) [get_trgt_network $trgt(ssid)]
	set trgt(nick) [get_nick_by_ssid $trgt(ssid)]
	set trgt(bot) [get_trgt_bot $usr(ssid) $trgt(ssid)]
	set msg "[colornick $nick]$nick\003: $arg"
	send_msg_db $trgt(network) $trgt(nick) [clean_msg $msg] [whichbot $trgt(bot)]
}
 
# Resolved help issue clean up function
proc close_pm {nick host hand arg} {
	global network
	set usr(ssid) [get_ssid_by_nick $nick]
	if {[get_pmdata_by_ssid $usr(ssid)] == ""} {
		send_msg_db $network $nick "Sorry no open PMs found!" "[whichbot [get_botnum]]"
		return 0
	}
	set trgt(ssid) [get_trgt_ssid $usr(ssid)]
	if {$trgt(ssid) == ""} {
		send_msg_db $network $nick "No PMs active on this bot!" "[whichbot [get_botname]]"
		return 0
	}
	set trgt(nick) [get_nick_by_ssid $trgt(ssid)]
	set trgt(network) [get_trgt_network $trgt(ssid)]
	set trgt(bot) [get_trgt_bot $usr(ssid) $trgt(ssid)]
	del_by_ssid $usr(ssid) $trgt(ssid)
	set msg "PM closed!"
	send_msg_db $network $nick $msg "[whichbot [get_botnum]]"
	send_msg_db $trgt(network) $trgt(nick) $msg "[whichbot $trgt(bot)]"
}
 
# Public command function
proc start_pm {nick host hand arg} {
	global db relayname network
	set ssid $arg
	if {![string is integer -strict $ssid]} {
		putserv "PRIVMSG $nick :Wrong syntax. Usage: .pm <session_id from .userlist>"
		return 0
	}
	set usr(ssid) [get_ssid_by_nick $nick]
	set usr(pmdata) [get_pmdata_by_ssid $usr(ssid)]
	set usr(nick) $nick
	if {$usr(pmdata) != ""} {
		if {[llength $usr(pmdata)] == 3} {
			putserv "PRIVMSG $nick :You have the max number of PM sessions open, please close one before opening another"
			return 0
		}
		set usr(cur_bots) [get_bots_data $usr(pmdata)]
		set usr(cur_ids) [get_ids_data $usr(pmdata)]
		set id_fnd [lsearch $usr(cur_ids) $ssid]
		if { $id_fnd != "-1"} {
			set botname [whichbot [lindex $usr(cur_bots) $id_fnd]]
			putserv "PRIVMSG $nick :You already have a PM session open with that id on bot $botname"
			return 0
		}
		set usr(bot) [find_open_bot $usr(cur_bots)]
	} else {
		set usr(bot) 1
	}
	set usr(bot_name) [whichbot $usr(bot)]
 
	set trgt(nick) [get_nick_by_ssid $ssid]
	set trgt(ssid) $ssid
	set trgt(pmdata) [get_pmdata_by_ssid $ssid]
	if {$trgt(nick) == ""} {
		putserv "PRIVMSG $nick :Session ID $ssid NOT FOUND. If this is an error, please report using .bug!"
		return 0
	}
	if {$trgt(pmdata) != ""} {
		if {[llength $trgt(pmdata)] == 3} {
			putserv "PRIVMSG $nick :User $trgt(nick) has max number of open PM sessions, sorry! Try again later."
			return 0
		}
		set trgt(cur_bots) [get_bots_data $trgt(pmdata)]
		set trgt(cur_ids) [get_ids_data $trgt(pmdata)]
		set trgt(bot) [find_open_bot $trgt(cur_bots)]
	} else {
		set trgt(bot) 1
	}
	set trgt(bot_name) [whichbot $trgt(bot)]
	lappend trgt(pmdata) "$trgt(bot).$usr(ssid).0"
	lappend usr(pmdata) "$usr(bot).$trgt(ssid).1"
	update_ss_pmdata $usr(ssid) $usr(pmdata)
	update_ss_pmdata $trgt(ssid) $trgt(pmdata)
	send_msg_db $network $usr(nick) "Waiting for $trgt(nick) on $relayname to reply, all further input will be ignored until that time." $usr(bot_name)
	send_msg_db $relayname $trgt(nick) "$usr(nick) from $network would like to start a PM with you. Reply to accept the PM or ignore this and you will recieve no further messages." $trgt(bot_name)
 
}
 
proc clean_msg {msg} {
	set msg [regsub -all {(\"|\')} $msg {\\\1}]
	return $msg
}
 
proc get_ss_by_nick {nick} {
	global db network
	set qry "SELECT * FROM $db(tbl_session) WHERE nick = '$nick' AND network = '$network'"
	set ret [lindex [mysqlsel $db(sqlhand) $qry -list] 0]
	return $ret
}
 
proc get_ss_by_ssid {ssid} {
	global db
	set qry "SELECT * FROM $db(tbl_session) WHERE session_id = '$ssid'"
	set ret [lindex [mysqlsel $db(sqlhand) $qry -list] 0]
	return $ret
}
 
proc update_ss_pmdata {ssid pmdata} {
	global db
	set qry "UPDATE $db(tbl_session) SET pmsys = '$pmdata' WHERE session_id = '$ssid'"
	mysqlsel $db(sqlhand) $qry
}
 
proc send_msg_db {network target msg type} {
	global db
	set qry "INSERT INTO `outgoing_messages`(`message`,`type`,`network`,`target`) VALUES('$msg','$type','$network','$target')"
	mysqlsel $db(sqlhand) $qry
}
 
proc get_ssid_by_nick {nick} {
	set ret [get_ss_by_nick $nick]
	set ssid [lindex $ret 0]
	return $ssid
}
 
proc get_nick_by_ssid {ssid} {
	set ret [get_ss_by_ssid $ssid]
	set nick [lindex $ret 2]
	return $nick
}
 
proc get_trgt_bot {usr_ssid trgt_ssid} {
	set pmdata [get_pmdata_by_ssid $trgt_ssid]
	set ids [get_ids_data $pmdata]
	set bots [get_bots_data $pmdata]
	set bot [lindex $bots [lsearch $ids $usr_ssid]]
	return $bot
}
 
proc get_trgt_network {ssid} {
	set trgt_ss [get_ss_by_ssid $ssid]
	set network [lindex $trgt_ss 5]
	return $network
}
proc get_trgt_ssid {ssid} {
	set pmdata [get_pmdata_by_ssid $ssid]
	set bots [get_bots_data $pmdata]
	set ids [get_ids_data $pmdata]
	set trgtssid [lindex $ids [lsearch $bots [get_botnum]]]
	return $trgtssid
}
 
proc get_pmdata_by_ssid {ssid} {
	set ret [get_ss_by_ssid $ssid]
	set pmdata [lindex $ret 18]
	return $pmdata
}
 
proc del_by_ssid {usr_ssid trgt_ssid} {
	global db
	set trgt(pmdata) [get_pmdata_by_ssid $trgt_ssid]
	set usr(pmdata) [get_pmdata_by_ssid $usr_ssid]
	set usr(ids) [get_ids_data $usr(pmdata)]
	set usr(rem_pos) [lsearch $usr(ids) $trgt_ssid]
	set usr(pmdata) [lreplace $usr(pmdata) $usr(rem_pos) 1]
 
	if {$trgt(pmdata) == ""} {
		set qry "UPDATE $db(tbl_session) SET pmsys = '$usr(pmdata)' WHERE session_id = '$usr_ssid'"
		mysqlsel $db(sqlhand) $qry
		return 0
	}
	set trgt(ids) [get_ids_data $trgt(pmdata)]
	set trgt(rem_pos) [lsearch $trgt(ids) $usr_ssid]
	set trgt(pmdata) [lreplace $trgt(pmdata) $trgt(rem_pos) 1]
 
	set qry "UPDATE $db(tbl_session) SET pmsys = '$usr(pmdata)' WHERE session_id = '$usr_ssid'"
	mysqlsel $db(sqlhand) $qry
	set qry "UPDATE $db(tbl_session) SET pmsys = '$trgt(pmdata)' WHERE session_id = '$trgt_ssid'"
	mysqlsel $db(sqlhand) $qry
}
 
proc get_bots_data {pmdata} {
	foreach pmss $pmdata {
		set cur_data [split $pmss .]
		lappend cur_bots [lindex $cur_data 0]
	}
	return $cur_bots
}
 
proc get_ids_data {pmdata} {
	foreach pmss $pmdata {
		set cur_data [split $pmss .]
		lappend cur_ids [lindex $cur_data 1]
	}
	return $cur_ids
}
 
proc find_open_bot {bots} {
	for {set cnt 1} {$cnt < 4} {incr cnt} {
		set bot_fnd [lsearch $bots $cnt]
		if {$bot_fnd == "-1"} {
			return $cnt
		}
	}
}
 
proc get_botnum {} {
	global nick
	switch $nick {
		GT-01	{return 1}
		GT-02	{return 2}
		GT-03	{return 3}
	}
}
 
proc whichbot {botnum} {
	switch $botnum {
		1	{return "GT-01"}
		2	{return "GT-02"}
		3	{return "GT-03"}
	}
}
 
putlog "GT-PMSYS.tcl by slackin LOADED"