############################
### HTMLOpVote.tcl       ###
### Version 2.0          ###
### By Wcc               ###
### wcc@techmonkeys.org  ###
### http://dawg.dynu.com ###
### EFnet #|DAWG|Tcl     ###
############################

############################################################################
### Copyright © 2000 - 2001 |DAWG| Scripting Group. All rights reserved. ###
############################################################################

#########################################################################################
## This script allows ops to vote on a topic. Only ops in the voting channel may vote. ##
#########################################################################################

##############
## COMMANDS ##
################################################################
## MSG ## /msg <bot> vote [pass] <nick> <yes/no> <reason>     ##
######### Places a vote. All votes are logged with the user's ##
######### voter's nick and host, along with the date and time.##
################################################################
## DCC ## .voteclear (Can be changed)                         ##
######### Clears the vote results page.                       ##
################################################################

##########################################################
## Just load the script, edit the settings, and rehash.	##
##########################################################

###########################################################
# Set the dcc command for clearing the vote results here. #
###########################################################

set htmlopvote_setting(cmd_clear) "voteclear"

#############################################################
# Set the flag required for clearing the vote results here. #
#############################################################

set htmlopvote_setting(flag_clear) "+m"

##########################################
# Set the channel ops will vote in here. #
##########################################

set htmlopvote_setting(votechan) "#|DAWG|Tcl"

##################################
# Set the html logfile filename. #
##################################

set htmlopvote_setting(page) "./web/voteresults.html"

#####################################
# Set the table header colors here. #
#####################################

set htmlopvote_setting(tableheader_color_1) 8080c0
set htmlopvote_setting(tableheader_color_2) 8080e0

###################################
# Set the table cell colors here. #
###################################

set htmlopvote_setting(tablecell_color_1) c0c0c0
set htmlopvote_setting(tablecell_color_2) e0e0e0

##################################
# Set the background color here. #
##################################

set htmlopvote_setting(bgcolor) FFFFFF

############################
# Set the text color here. #
############################

set htmlopvote_setting(textcolor) 000000

##############################
# Set the database filename. #
##############################

set htmlopvote_setting(db) "./web/voteresults.db"

###################################
# Enable use of bold in DCC chat? #
###################################

set htmlopvote_setting(bold) 1

##############################################
# Prefix "HTMLOPVOTE:" in DCC chat messages? #
##############################################

set htmlopvote_setting(HTMLOPVOTE:) 1

############################
# Use password protection? #
############################

set htmlopvote_setting(prot) 1

####################
# Code begins here #
####################

if {![string match 1.6.* $version]} {
	putlog "\002HTMLOPVOTE:\002 \002CRITICAL ERROR\002 HTMLOpVote.tcl requires eggdrop 1.6.x to run."
	die "\002HTMLOPVOTE:\002 \002CRITICAL ERROR\002 HTMLOpVote.tcl requires eggdrop 1.6.x to run."
}
bind msg - vote htmlopvote_vote
bind dcc $htmlopvote_setting(flag_clear) $htmlopvote_setting(cmd_clear) htmlopvote_clear
set htmlopvote_setting(version) "2.0"

proc htmlopvote_dopre {} {
	global htmlopvote_setting
	if {!$htmlopvote_setting(HTMLOPVOTE:)} {
		return ""
	} elseif {!$htmlopvote_setting(bold)} {
		return "HTMLOPVOTE: "
	} else {
		return "\002HTMLOPVOTE:\002 "
	}
}
proc htmlopvote_clear {hand idx text} {
	global htmlopvote_setting
	putdcc $idx "[htmlopvote_dopre]Clearing vote results."
	if {[catch [file delete -force $htmlopvote_setting(db)]]} {
		putdcc $idx "[htmlopvote_dopre]Could not clear $htmlopvote_setting(db)"
	}
	set votepageid [open $htmlopvote_setting(page) w+]
	puts $votepageid "<html><head><title>Vote Results</title></head>"
	puts $votepageid "<body bgcolor=$htmlopvote_setting(bgcolor)>"
	puts $votepageid "<font size=\"6\" color=$htmlopvote_setting(textcolor)><center>Vote Results</center></font><br>"
	puts $votepageid "<center><font color=$htmlopvote_setting(textcolor)>Last Updated: [clock format [clock seconds] -format %D] [clock format [clock seconds] -format "%I:%M %p"]</font></center></body></html>"
	close $votepageid
}
proc htmlopvote_vote {nick uhost hand text} {
	global htmlopvote_setting botnick
	if {[isop $nick $htmlopvote_setting(votechan)]} {
		if {$htmlopvote_setting(prot)} {
			if {([lindex [split $text] 3] == "") || (![regexp -all yes|no [lindex [split $text] 2]])} {
				puthelp "NOTICE $nick :Usage: \"/msg $botnick vote <password> <nick> <yes/no> <reason>\"."
			} elseif {([passwdok $hand ""]) || (![passwdok $hand [lindex [split $text] 0]])} {
				puthelp "NOTICE $nick :Invalid password."
			} else {
				regsub -all -- "\\&" "[lrange [split $text] 3 end]" " and " rsn  
				htmlopvote_putlog $nick $uhost [lindex [split $text] 1] [lindex [split $text] 2] \"$rsn\"
				puthelp "NOTICE $nick :Vote accepted. You voted [lindex [split $text] 2] for [lindex [split $text] 1]. The reason given was \"$rsn\""
				putlog "[htmlopvote_dopre]$nick \($uhost\) voted [lindex [split $text] 2] for [lindex [split $text] 1]. The given reason was \"$rsn\"."
			}
		} else {
			if {([lindex [split $text] 2] == "") || (![regexp -all yes|no [lindex [split $text] 1]])} {
				puthelp "NOTICE $nick :Usage: \"/msg $botnick vote <nick> <yes/no> <reason>\"."
			} else {
				regsub -all -- "\\&" "[lrange [split $text] 2 end]" " and " rsn  
				htmlopvote_putlog $nick $uhost [lindex [split $text] 1] [lindex [split $text] 2] \"$rsn\"
				puthelp "NOTICE $nick :Vote accepted. You voted [lindex [split $text] 1] for [lindex [split $text] 0]. The reason given was \"$rsn\""
				putlog "[htmlopvote_dopre]$nick \($uhost\) voted [lindex [split $text] 1] for [lindex [split $text] 0]. The given reason was \"$rsn\"."
			}
		}	
	} else { 
		putserv "NOTICE $nick :Only ops in $htmlopvote_setting(votechan) may vote."
	}
}
proc htmlopvote_putlog {nick host for vote rsn} {
	global htmlopvote_setting
	regsub -all -- " " "[clock format [clock seconds] -format "%I:%M:%S %p %x"]" "\\&" time  
	regsub -all -- " " "$rsn" "\\&" rsn
	set databaseid [open $htmlopvote_setting(db) a+]
	puts $databaseid "$time $nick $host $vote $for $rsn"
	close $databaseid
	set votepageid [open $htmlopvote_setting(page) w+]	
	puts $votepageid "<html>"
	puts $votepageid "<head>"
	puts $votepageid "<title>Vote Results</title>"
	puts $votepageid "</head>"
	puts $votepageid "<body bgcolor=$htmlopvote_setting(bgcolor)>"
	puts $votepageid "<font size=\"6\" color=$htmlopvote_setting(textcolor)><center>Vote Results</center></font>"
	puts $votepageid "<br>"
	puts $votepageid "<table align=\"center\">"
	puts $votepageid "<tr>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_1)><font size=\"5\"><center>Time</center></font></td>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_2)><font size=\"5\"><center>Nick</center></font></td>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_1)><font size=\"5\"><center>Host</center></font></td>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_2)><font size=\"5\"><center>Vote</center></font></td>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_1)><font size=\"5\"><center>For</center></font></td>"
	puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tableheader_color_2)><font size=\"5\"><center>Reason</center></font></td>"
	puts $votepageid "</tr>"
	set databaseid [open $htmlopvote_setting(db) r]
	set filedata [split [read $databaseid] \n]
	close $databaseid
	foreach line $filedata {
		if {$line != ""} {
			regsub -all -- "\\&" "[lindex [split $line] 0]" " " arg1 
			regsub -all -- "\\&" "[lindex [split $line] 5]" " " arg6
			puts $votepageid "<tr><td align=left bgcolor=$htmlopvote_setting(tablecell_color_1)><font size=\"4\">$arg1</font></td>"
			puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tablecell_color_2)><font size=\"4\"><center>[lindex [split $line] 1]</center></font></td>"
			puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tablecell_color_1)><font size=\"4\"><center>[lindex [split $line] 2]</center></font></td>"
			puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tablecell_color_2)><font size=\"4\"><center>[lindex [split $line] 3]</center></font></td>"
			puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tablecell_color_1)><font size=\"4\"><center>[lindex [split $line] 4]</center></font></td>"
			puts $votepageid "<td align=left bgcolor=$htmlopvote_setting(tablecell_color_2)><font size=\"4\"><center>$arg6</center></font></td></tr>"
		}
	}
	puts $votepageid "</table>"
	puts $votepageid "<br>"
	puts $votepageid "<br>"
	puts $votepageid "<center><font color=$htmlopvote_setting(textcolor)>Last Updated: [clock format [clock seconds] -format %D] [clock format [clock seconds] -format "%I:%M %p"]</font></center>"
	puts $votepageid "</body>"
	puts $votepageid "</html>"
	puts $votepageid "<!--<DAWGID>Split $htmlopvote_setting(version)</DAWGID>-->"
	close $votepageid
}
putlog "\002HTMLOPVOTE:\002 HTMLOpVote.tcl Version $htmlopvote_setting(version) by Wcc is loaded."
putlog "\002HTMLOPVOTE:\002 Voting Channel Is $htmlopvote_setting(votechan)."
putlog "\002HTMLOPVOTE:\002 Voting log page is $htmlopvote_setting(page)."