################################################################################
#
#-----------============= Solid Services =============--------------------------
#                     www.solidservices.ro
#                   #solidservices @Undernet
##############################################################################
# Completely Useless Fun Script.tcl [TM]
# Version: 2.0
#
# Zoef (zoef@zoefdehaas.nl)
# developed on Plophoofd on #openbsd.nl@IRCNet
# Original way way back by ArcAngel (arcangel@xs26.net)
#
# - log lovely links with descriptive keywords
#   and search them later if you forgot them
#   .oO( this could be useful, actually ) 
#   for example
#  	!url http://www.google.com search google find 
#   will log the url and the keywords.
#   Look for the url with !surl
#   for example
#	!surl google	- return all urls containing 'google'
#			  in keywords OR url
#	!surl find	- return all urls containing 'find'
#			  in keywords OR url
#	!surl .com  	- return all .com urls
#  NOTE: !surl * and !surl http:// are broken. on purpose.
#
# - let your bot react to userdefined keywords
#   for example
# 	!remember cool Yeah, I like that too
#   will let your bot say "Yeah, I like that too" every time
#   the word 'cool' is spoken in the channel
#
# - let your bot forget words if he gets to nosy
#   for example
#	!forget cool
#   will make it forget the description of 'cool'
#   no... forget * does not work...
# 
# - let the bot reverse sentences
#   for example
#	!rev I want to lick y00
#   will make the bot say '00y kcil ot tnaw I'
#   ...pretty useless, eh? :)
#
# - make the bot say evil things through /msg
#   for example
#	/msg <botnick> speak <channel> Feed me!
#   will make the bot say 'Feed me!' on <channel>

putlog "Channel LSUfun v2.0 by Zoef - Loaded"

# NOTE: Make sure the files/ dir exists !!
set learnfile "files/ostasul.learn"
set urlfile "files/$botnick.url"

# remember and forget
bind pub - .adauga pub:learn
bind pub - .uita pub:forget

# ...
bind pub - .rev pub:reverse

# remember links & their keywords
bind pub - !url pub:url
bind pub - !surl pub:surl

# make bot speak through /msg
bind msg - speak msg:speak
bind pubm - * pub:learned

proc ctoken {str delim} {
	set index [string match [$delim] $str]
	set token [string range $str 0 $index]
	set strvar [string range $str $index [string length $str] ]
	return $token
}

proc pub:reverse {nick host hand args} {
	set args [string trim $args "{} "]
        set len [string length $args]
        set space [string first " " $args]
        set channel [string trim [string range $args 0 $space] ]
        set blah [string trim [string range $args $space $len] ]

	set halb [reverse $blah]
	set halb [string trim $halb "{} "]
	putserv "PRIVMSG $channel :$halb"
}

proc reverse { text } {
	for { set i [string length $text] } { $i >= 0 } { incr i -1 } {
	append reverse [string index $text $i]
	}
	return $reverse
}


# /msg speak <channel> <blaat> 
proc msg:speak {nick host hand args} {

	set args [string trim $args "{} "]
	set len [string length $args]
	set space [string first " " $args]
	set channel [string trim [string range $args 0 $space] ]
	set blah [string trim [string range $args $space $len] ]


	if {[string length $args] == 0} {
		putserv "PRIVMSG $nick :Nothing to say!"
		return
		}
	
	putserv "PRIVMSG $channel :$blah"
}

# !remember <keyword> <desciption>
proc pub:learn {nick host hand chan args} {
	global learnfile
	set args [string trim $args "{} "]
	set len [string length $args]
	if {$len == 0} {
		putserv "PRIVMSG $chan :*bzzzzzz* precizeaza un cuvant!"
		return
			}

	set space [string first " " $args]
	if {$space == -1} {
		putserv "PRIVMSG $chan :*bzzzzzz* geen koelkast voor jou!"
		return
		}

	set word [string trim [string range $args 0 $space] ]
	set learn [string trim [string range $args $space $len] ]

	set file [open $learnfile a+]
        puts $file "${word}::$learn"
	close $file

	putserv "PRIVMSG $chan :$word... a fost adaugat in baza de date"
}

# keep eye on chan and talk
proc pub:learned {nick host hand chan args} {
  	global learnfile

  	set found 0
  	set i ""
  	set foundstr(0) ""

  	set args [string trim $args "{} "]

  	if [file readable $learnfile] {
    		set file [open $learnfile RDONLY]
    		while {1} {
      			if [eof $file] break
      			set line [gets $file]

      			# Get the word.  Should be: set word [ctoken $line ":"]
      			set len [string length $line]
      			set colon [string first ":" $line]
      			set word [string range $line 0 [expr $colon -1] ]
     			set line [string range $line [expr $colon +1] $len]

      			# Get the flags.  Should be: set flags [ctoken $line ":"]
      			set len [string length $line]
      			set colon [string first ":" $line]
      			set flags [string range $line 0 [expr $colon -1] ]
      			set line [string range $line [expr $colon +1] $len]

      			foreach i [join $args] {
        			if {[string compare $i $word] == 0} {
          				set foundstr($found) $line;
          				##putserv "PRIVMSG $chan :$i == $line"
          				incr found
        				}
      				}
    			}
    close $file
    if { $found > 0 } {
      set show [expr [rand $found]]
      putserv "PRIVMSG $chan :$foundstr($show)"
      unset foundstr
      set found 0
      return
    }
  } else {
    	return
  	}

}

# !forget <keyword>
proc pub:forget {nick host hand chan args} {
        global learnfile
        set found 0

        set args [string trim $args "{} "]

        if {[string length $args] == 0} {
                putserv "PRIVMSG $chan :*bzzzzzzt*"
                return
                }

        # Step one: Create a new file without the $args keyword.
        if [file readable $learnfile] {
        set file [open $learnfile RDONLY]
        set newfile [open $learnfile.tmp w]

        while {1} {
                if [eof $file] break
                        set line [gets $file]

        # Get the word.  Should be: set word [ctoken $line ":"]
        set len [string length $line]
        set colon [string first ":" $line]
        set word [string range $line 0 [expr $colon -1] ]
        set line [string range $line [expr $colon] $len]

        if {[string compare $args $word]} {
                puts $newfile "$word$line"
                } else {
                incr found
                }
        }

        close $file
        close $newfile
        } else {
                putserv "PRIVMSG $chan :But I don't know anything!"
                return
                }

        if {$found == 0} {
                putserv "PRIVMSG $chan :*bzzt* Ohoo am si uitat :)  \"$args\"?"
                return
                }

        # Step two: Rewrite the $learnfile from the new file.
        set file [open $learnfile w]
        set newfile [open $learnfile.tmp r]
        while { 1 } {
                if [eof $newfile] break
                puts $file [gets $newfile]
                }
        close $file
        close $newfile
        putserv "PRIVMSG $chan :ce ? \"$args\"? da ce e i aia?"
}

# !url <url> <keywords>
proc pub:url {nick host hand chan args} {
   global urlfile
   set args [string trim $args "{} "]
   set len [string length $args]
   if {$len == 0} { 
   	putserv "PRIVMSG $chan :*bzzzzzz*!" 
	putserv "PRIVMSG $chan :!url <url> <keywords>"
   	return
   }

   set space [string first " " $args]
   if {$space == -1} {
	putserv "PRIVMSG $chan :*bzzzzzz*!"
	putserv "PRIVMSG $chan :!url <url> <keywords>"
     	return
   }

   set url [string trim [string range $args 0 $space] ]
   set descrip [string trim [string range $args $space $len] ]
   set file [open $urlfile a+]
   puts $file "${url}!!$descrip"
   close $file
   putserv "PRIVMSG $chan :Logged: $url - $descrip "
}

# !surl <keyword>
proc pub:surl {nick host hand chan args} {
	global urlfile
	set args [string trim $args "{} "]
	set len [string length $args]
	if {$len == 0} {
		putserv "PRIVMSG $chan :*bzzzzzz*"
		putserv "PRIVMSG $chan :!surl <keyword>"
		return
			}

	set space [string first " " $args]

	set match_string [string trim [string range $args 0 $space] ]

  	set found 0
	set matched 0
  
	if { [regexp {^\*} $args] || [regexp {www} $args] || [regexp {http} $args] } {
		putserv "PRIVMSG $chan :No way, $nick."
		return
	}
  
	if [file readable $urlfile] {
    		set file [open $urlfile RDONLY]
    		while {1} {
      			if [eof $file] break
      			set line [gets $file]

			set len [string length $line]
      			set colon [string first "!" $line]
      			set url [string range $line 0 [expr $colon -1] ]
     			set description [string range $line [expr $colon +1] $len]

			if {[string match *$args* $description]} {
          			set ary($found) "$url"	
				incr found
        			}
      			}

		close $file
    		}
    
    	if { $found > 0 } {
		foreach match [array names ary] {
      		putserv "PRIVMSG $chan :$ary($match)"
		set matched 1
		}
  	}

      	set found 0
	
	if [file readable $urlfile] {
    		set file [open $urlfile RDONLY]
    		while {1} {
      			if [eof $file] break
      			set line [gets $file]

			set len [string length $line]
      			set colon [string first "!" $line]
      			set url [string range $line 0 [expr $colon -1] ]

			if {[string match *$args* $url]} {
          			set ary2($found) "$url"	
          			incr found
        			}
      			}

		close $file
    		}
    
    	if { $found > 0 } {
		foreach match [array names ary2] {
      		putserv "PRIVMSG $chan :$ary2($match)"
		set matched 1
		}
      		set found 0
		return
  	}

	if { $found == 0 && $matched == 0 } {
		putserv "PRIVMSG $chan :$nick, niets gevonden voor je."
		return
		
	}
	return
}

# EOF
