#
# URL storing and retrieving tcl script v1.0
# by Ernst <ernst@studbox.uni-stuttgart.de>
# Ernst's eggdrop page:  http://www.sodre.net/ernst/eggdrop/
#
# This script will allow your bot's users to store their homepage address
# (url) which may then be requested by anyone in public or private.
#
# This "url" field will also show up in the .whois, if you have "url" in the 
# 'set whois-fields' config of your bot (eggdrop 1.1 only).
#
# The script needs "toolkit.tcl" to be loaded and that the xtra field is not
# raped by other stupid scripts which does not know how to properly access the
# xtra field. :)
#
# I have used "flagnote5.tcl" to send a note to every +p user in my bot,
# asking them to set their url field (and explaining *how* cause we know,
# users are dumb), you might want to do the same (just an idea).
#
# -----------------------------------------------------------------------------
# Dcc command (for Masters only):
#   .allwww     - gives a list of already registered url's
#   .score url  - shows which url has been requested the most
#   .score mail - shows which email address has been requested the most
#
# Public commands for everyone:
#   !www <nick>  - shows homepage of this user, if it is known
#   !www         - shows a list of users for which bot knows the homepage
#   !mail <nick> - shows email of this user, if it is known
#   !mail        - shows a list of users for which bot knows the email
#
# Message commands for everyone:
#   /msg bot www <nick>         - same as public the command
#   /msg bot www                - same as public the command
#   /msg bot mail <nick>        - same as public the command
#   /msg bot mail               - same as public the command
#   /msg bot url http://address - stores homepage address for oneself
#
# -----------------------------------------------------------------------------
# This script can make use of 'altnick.tcl', which enables you to have altnicks
# stored with each user, so asking !www one_altnick gives you the proper
# response.  This is OPTIONAL and will only work if you have and load
# 'altnick.tcl' *before* this url.tcl in your config.  It is avaliable at
# ftp://ftp.sodre.net/pub/eggdrop/scripts1.0/altnick.tcl.gz, and it will
# probably not hurt you installing it.
# -----------------------------------------------------------------------------

# If altnick.tcl is loaded, use it
if {[info commands findnick] == ""} {
	proc findnick {nick} {
		if {[validuser $nick]} { return $nick } { return "" }
	}
}

# Just in case toolkit is not loaded (shame on you)...
if {!$toolkit_loaded} {
	proc user-get {handle key} {
		set xtra [getxtra $handle]
		for {set i 0} {$i < [llength $xtra]} {incr i} {
			set this [lindex $xtra $i]
			if {[string compare [lindex $this 0] $key] == 0} {
				return [lindex $this 1]
			}
		}
		return ""
	}
	proc user-set {handle key data} {
		set xtra [getxtra $handle]
		# is key already there?
		for {set i 0} {$i < [llength $xtra]} {incr i} {
			set this [lindex $xtra $i]
			if {[string compare [lindex $this 0] $key] == 0} {
				set this [list $key $data]
				setxtra $handle [lreplace $xtra $i $i $this]
				return
			}
		}
		lappend xtra [list $key $data]
		setxtra $handle $xtra
	}
}

bind msg - url url_set
proc url_set {nick uhost hand arg} {
	global botnick
	if { $hand == "*" } {
		putnotc $nick "Sorry, but I don't know you, $nick."
	} {
		if {$arg == ""} {
			if {[user-get $hand url] == ""} {
				putnotc $nick "You still haven't got an homepage registered!"
				putnotc $nick "To register, use: \002/msg $botnick url http://address\002"
			} {
				putnotc $nick "Your current url: [user-get $hand url]"
				putnotc $nick "To remove: \002/msg $botnick url none\002"
				putnotc $nick "To change: \002/msg $botnick url http://address\002"
			}
		} elseif {[string tolower $arg] == "none"} {
			user-set $hand "url" ""
			putnotc $nick "Your homepage was removed!"
		} {
			user-set $hand "url" $arg
			putnotc $nick "Your homepage is now $arg!"
			putnotc $nick "To remove: \002/msg $botnick url none\002"
			putnotc $nick "To change: \002/msg $botnick url http://endereco\002"
		}
	}
	return 1
}

bind pub - !www pub_msg_www
bind msg - www pub_msg_www

# PUB: procname <nick> <user@host> <handle> <channel> <args>
# MSG: procname <nick> <user@host> <handle> <args>

# See how you can use "args" to have just one proc for both "pub" and "msg"
# bindings?  "$args" is not that bad after all! :)

proc pub_msg_www {nick uhost hand args} {
	global botnick
	putlog "args: $args"
	if {[string index [lindex $args 0] 0] == "#"} {
		set dest "[lindex $args 0]"
		set arg "[lindex $args 1]"
		set log "<$nick@$dest> !$hand!"
	} {
		set dest "$nick"
		set arg "[lindex $args 0]"
		set log "($nick!$uhost) !hand!"
	}
	if {$arg == ""} {
		foreach one [userlist] {
			if {[user-get $one url] != ""} { lappend urluser $one }
		}
		if {[info exist urluser]} {
			set urluser [lsort -command icompare $urluser]
			regsub -all " " $urluser ", " urluser
			putnotc $nick "$nick, I know the homepages from $urluser."
		}
		putcmdlog "$log www"
		return 0
	}
	# User is just an altnick?
	set validnick [findnick $arg]
	if {$validnick != $arg} {
		set realuser "$arg (aka $validnick)"
	} {
		set realuser "$arg"
	}
	if {$validnick == ""} {
		puthelp "NOTICE $nick :$nick, I don't know $arg."
		return 0
	}
	if {[user-get $validnick url] != ""} {
		puthelp "PRIVMSG $dest :$nick, the homepage of $realuser is [user-get $validnick url]"
		if {$validnick != $hand} {
			user-set $validnick url-score [expr [user-get $validnick url-score] + 1]
		}
	} {
		if {[getemail $validnick] != ""} {
			puthelp "PRIVMSG $dest :$nick, I don't know the homepage of $realuser, but here is the email: [getemail $validnick]."
		} {
			puthelp "PRIVMSG $dest :$nick, $realuser does not have an homepage."
		}
		if {[string index $dest 0] == "#"} {
			set wwwnick [hand2nick $validnick $dest]
			if {$wwwnick != ""} {
				putnotc $wwwnick "$validnick, if you want to register your homepage with me, use \002/msg $botnick URL http://address\002"
			}
		}
	}
	putcmdlog "$log www $realuser"
	return 0
}

bind msg - mail pub_msg_email
bind pub - !mail pub_msg_email

proc pub_msg_email {nick uhost hand args} {
	global botnick
	if {[string index [lindex $args 0] 0] == "#"} {
		set dest "[lindex $args 0]"
		set arg "[lindex $args 1]"
		set log "<$nick@$dest> !$hand!"
	} {
		set dest "$nick"
		set arg "[lindex $args 0]"
		set log "($nick!$uhost) !hand!"
	}
	if {$arg == ""} {
		foreach one [userlist] {
			if {[getemail $one] != ""} { lappend emailuser $one }
		}
		if {[info exist emailuser]} {
			set emailuser [lsort -command icompare $emailuser]
			regsub -all " " $emailuser ", " emailuser
			putnotc $nick "$nick, I know the emails from $emailuser."
		}
		putcmdlog "$log email"
		return 0
	}
	set validnick [findnick $arg]
	if {$validnick != $arg} {
		set realuser "$arg (aka $validnick)"
	} {
		set realuser "$arg"
	}
	if {$validnick == ""} {
		puthelp "NOTICE $nick :$nick, I don't know $arg."
		return 0
	}
	if {[getemail $validnick] != ""} {
		puthelp "PRIVMSG $dest :$nick, the email address of $realuser is: [getemail $validnick]"
		if {$validnick != $hand && $nick != $validnick} {
			user-set $validnick mail-score [expr [user-get $validnick mail-score] + 1]
		}
	} {
		if {[user-get url $validnick] != ""} {
			puthelp "PRIVMSG $dest :$nick, I don't know the email of $realuser, but here is the homepage: [user-get url $validnick]"
		} {
			puthelp "PRIVMSG $dest :$nick, $realuser does not have an email registered."
		}
		if {[string index $dest 0] == "#"} {
			set emailnick [hand2nick $validnick $chan]
			if {$emailnick != ""} {
				putnotc $emailnick "$validnick, if you want to register your email, use \002/msg $botnick EMAIL your@email.com\002"
			}
		}
	}
	putcmdlog "$log email $realuser"
}

bind dcc M allwww dcc_www

proc dcc_www { hand idx arg } {
	foreach one [userlist] { if {[user-get $one url] != ""} { putdcc $idx "$one: [user-get $one url]" } }
}

bind dcc M score dcc_score

proc dcc_score { hand idx arg } {
	switch [lindex $arg 0] {
		"www" { set search "url-score" }
		"url" { set search "url-score" }
		"mail" { set search "mail-score" }
		"email" { set search "mail-score" }
		default { putdcc $idx "Usage: score url | mail"; return 0 }
	}
	foreach one [userlist] {
		set score [user-get $one $search]
		if {$score != ""} {
			lappend data($score) $one
		}
	}
	set i 0
	putdcc $idx "Top [lindex $arg 0] hits:"
	foreach scores [lsort -decreasing -integer [array names data]] {
		incr i
		if {$i > 10} { break }
		regsub -all " " $data($scores) ", " data($scores)
		putdcc $idx "$i. $data($scores) \($scores hits\)"
	}
	if {![info exist data]} {
		putdcc $idx "- nothing avaliable"
	}
}

