Code: Select all
# Author: tomekk
# e-mail:  tomekk/@/oswiecim/./eu/./org
# home page: http://tomekk.oswiecim.eu.org/
#
# Version 0.1
#
# This file is Copyrighted under the GNU Public License.
# http://www.gnu.org/copyleft/gpl.html
#
# if you want to use this script on your chan, type in eggdrop console (via telnet or DCC chat)                 
# .chanset #channel_name +vote
# and later .save   
# ban after X yes votes
set vote_limit 5
# dir with users data
set users_file "users.db"
# kick messages
set kick_msgs {
	"message 1"
	"message 2"
	"message 3"
	"message N"
}
# punish method
# 0 - just kick
# 1 - kick & ban
set punish 0
###############################################################################################
bind pub -|- !vote vote_proc
setudef flag vote
if {![file exists $users_file]} {                                                                             
	set file_handle [open $users_file w]
	close $file_handle
}
proc get_hosts { } { 
	global users_file
	set get_hosts [open $users_file r] 
	set get_all [split [read $get_hosts] "\n"] 
	close $get_hosts 
	return $get_all 
}
proc put_host { user_host voter_host } {
	global users_file
	set put_host [open $users_file a] 
	puts $put_host "$user_host $voter_host" 
	close $put_host
}
proc del_host { user_host } {
	global users_file
	set old_hosts [get_hosts]
	set del_hosts [open $users_file w]
	foreach del_host $old_hosts {
		if {$del_host != ""} {
			set del_rows [split $del_host " "]
			set del_user_host [lindex $del_rows 0]
			set del_user_voters [lindex $del_rows 1]
			
			if {$del_user_host != $user_host} {
				puts $del_hosts "$del_user_host $del_user_voters"
			}
		}
	}
	close $del_hosts
}
proc add_voter { user_host voter_host } {
	global users_file
	set old_db [get_hosts]
	set add_voter [open $users_file w]
	foreach old_host $old_db {
		if {$old_host != ""} {
			set old_rows [split $old_host " "]
			set old_user_host [lindex $old_rows 0]
			set old_user_voters [lindex $old_rows 1]
			if {$old_user_host == $user_host} {
				puts $add_voter "$old_user_host $old_user_voters\,$voter_host"
			} {
				puts $add_voter "$old_user_host $old_user_voters"
			}
		}
	}
	close $add_voter
}
proc count_voters { user_host } {
	set voters_count 0
	foreach db_host [get_hosts] {
		if {$db_host != ""} {
			set db_rows [split $db_host " "]
			set db_user_host [lindex $db_host 0]
			set db_user_voters [lindex $db_host 1]
			if {$db_user_host == $user_host} {
				set voters_split [split $db_user_voters ","]
				set voters_count [llength $voters_split]
				break
			}
		}
	}
	return $voters_count
}
proc check_user_host { user_host } {
	set host_exists "no"
	foreach host [get_hosts] { 
		if {$host != ""} { 
			set rows [split $host " "]
			set file_user_host [lindex $rows 0]
			if {$file_user_host == $user_host} {
				set host_exists "yes"
				break
			}
		}
	}
	return $host_exists
}
proc check_voter_host { user_host voter_host } {
	set voter_exists "no"
	foreach voter [get_hosts] {
		if {$voter != ""} {
			set voter_rows [split $voter " "]
			set voter_user_host [lindex $voter_rows 0]
			set voter_voters [lindex $voter_rows 1]
			if {$voter_user_host == $user_host} {
				set host_votes [split $voter_voters ","]
				foreach vote $host_votes {
					if {$vote != ""} {
						if {$vote == $voter_host} {
							set voter_exists "yes"
							break
						}
					}
				}
			}
		}
	}
	
	return $voter_exists
}
proc vote_proc { nick uhost hand chan arg } {
	global users_file vote_limit kick_msgs punish
		
	set args [split $arg] 
	set user [lindex $args 0] 
	set yes_or_no [lindex $args 1]
	if {[isbotnick $user]} { 
		return 
	}
	if {$user != ""} {
		if {($yes_or_no == "yes") || ($yes_or_no == "no")} { 
			if {[onchan $user $chan]} { 
				set user_host [getchanhost $user $chan]
				if {[check_user_host $user_host] == "yes"} {
					if {[check_voter_host $user_host $uhost] == "no"} {
						set user_votes_counter [count_voters $user_host]
						if {[expr $user_votes_counter + 1] == $vote_limit} {
							putquick "PRIVMSG $chan :$nick voted $user Yes. ($vote_limit) Votes."
							if {$punish == 1} {
								putquick "MODE $chan +b *!*$user_host" 
							}
						
							putkick $chan $user [lindex $kick_msgs [expr {int(rand() * [llength $kick_msgs])}]]
							del_host $user_host
						} {
							putquick "PRIVMSG $chan :$nick voted $user Yes. ([expr $user_votes_counter + 1]) Votes."
							add_voter $user_host $uhost
						}
					}
				} {
					put_host $user_host $uhost
					putquick "PRIVMSG $chan :$nick voted $user Yes. (1) Votes."
				}
			}
		}
	}
}
putlog "simple-vote.tcl ver 0.1 by tomekk loaded"
you can vote only for YES atm, I just want to try the counting on your channel
cheers