view mndb.tcl @ 0:1c4e2814cd41

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Sep 2010 13:12:49 +0300
parents
children
line wrap: on
line source

##########################################################################
#
# MattiNykanenDB v0.7 by ccr/TNSP <ccr@tnsp.org>
#
# Not for public use or distribution. If you happen to find this,
# send your questions and/or problems to /dev/null, thank you.
#
##########################################################################

###
### General options
###
# Filename where the logged quote data goes
set mndb_file "data.mndb"

# 1 = Verbose: Say messages PUBLIC when quote is OK, bad, etc.
# 0 = Quiet  : Say privately
set mndb_verbose 0

# 1 = Put some info to bot's Logfile when doing stuff...
# 0 = Don't.
set mndb_logmsg 1

# What IRC "command" should we use to send messages:
# (Valid alternatives are "PRIVMSG" and "NOTICE")
set mndb_preferredmsg "PRIVMSG"


###
### Search related settings
###

# How many quote's should the !mnfind command show (maximum limit)
set mndb_showmax_pub 3

# For private-search, this is the default limit (user can change it)
set mndb_showmax_priv 5


##########################################################################
# No need to look below this line
##########################################################################
#-------------------------------------------------------------------------
set mndb_name "MattiNykanenDB"
set mndb_version "0.7"


#-------------------------------------------------------------------------
### Binding initializations
bind pub - !mnfind mndb_pub_find
bind pub - !mnadd mndb_pub_add
bind pub - !mn mndb_pub_mn
bind msg - mnfind mndb_msg_find
bind msg - mn mndb_msg_mn


### Initialization messages
set mndb_message "$mndb_name v$mndb_version by ccr/TNSP"
putlog "$mndb_message"


#-------------------------------------------------------------------------
### Utility functions
proc mndb_log { jarg } {
  global mndb_logmsg mndb_name

  if {$mndb_logmsg != 0} {
	putlog "$mndb_name: $jarg"
	}
}


proc mndb_ctime { utime } {

  if {$utime == "" || $utime == "*"} {
	set utime 0
	}

  return [clock format $utime -format "%d.%m.%Y %H:%M"]
}


proc mndb_isnumber { uarg } {
  set ufoo 1

  foreach i [split $uarg {}] {
	if ![string match \[0-9\] $i] {set ufoo 0}
	}

  return $ufoo
}


proc mndb_msg {apublic anick achan amsg} {
global mndb_preferredmsg

if {$apublic == 0} {
	putserv "$mndb_preferredmsg $anick :$amsg" 
	} else {
	putserv "$mndb_preferredmsg $achan :$anick, $amsg"
	}
}


#-------------------------------------------------------------------------
proc mndb_add {utext unick uhost uchan upublic} {
global mndb_file

	if {[string length $utext] < 10} {
		mndb_msg $upublic $unick $uchan "pyh."
		return 0
	}

### Create the database file if it does not exist yet
	set fd [open $mndb_file a+]
	close $fd

### OK. The quote is valid, but let's check if we already know it.
	set fd [open $mndb_file r]
	set sindex 0
	set smax -1

	while {![eof $fd]} {
		gets $fd foo
		incr sindex

		set foo [split $foo "|"]
		set qindex [lindex $foo 0]
		if {$qindex > $smax} { set smax $qindex }
		}

	close $fd

### OK, the quote was not already known and IS valid. Add it.

        incr smax

	set fd [open $mndb_file a+]
	puts $fd "$smax|$utext|[unixtime]|$unick|$uhost|$uchan"
	close $fd

### Log some data
	mndb_log "Added quote #$smax ($unick @ $uchan): $utext"

### Let's report success to user
	mndb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos."

	return 1
}


#-------------------------------------------------------------------------
proc mndb_find {ipatterns imax} {
global mndb_file

### Search the database for pattern
### Clear the count, open the quote logfile
set iresults {}
set nresults 0
set fd [open $mndb_file r]

### Start searching...
while {![eof $fd]} {

# Get one quote for inspection
	gets $fd foo
	set irecord [split [string tolower $foo] "|"]
	set itext [lindex $irecord 1]
	set iname [lindex $irecord 3]

# Match with all given patterns and rules
	set imatched 1

	foreach ipattern $ipatterns {

		set foob [split [string tolower $ipattern] " "]
		set ftoken [lindex $foob 0]
		set fparam [lindex $foob 1]
		set fmatch [string match $fparam $itext]

		if {($ftoken == "+") && ($fmatch == 0)} { set imatched 0 }

		if {($ftoken == "-") && ($fmatch == 1)} { set imatched 0 }

		if {($ftoken == "%") && ([string match $fparam $iname] == 0)} { set imatched 0 }

		}

# If the all patterns matched, add to the list...
	if {($imatched == 1) && ($foo != "")} {
		incr nresults
		lappend iresults $foo
		}

	}

# Close file
close $fd

# Take only last imax results
return [lrange $iresults [expr $nresults-$imax] $nresults]
}



#-------------------------------------------------------------------------
proc mndb_get { unick uhand uindex } {
global mndb_file

set ifound 0
set iindex 0
set iresults {}
set nresults 0

### Create the database file if it does not exist yet
set fd [open $mndb_file a+]
close $fd

### OK. The quote is valid, but let's check if we already know it.
set fd [open $mndb_file r]

if {$uindex == ""} {
### Log search
	mndb_log "$unick/$uhand get random quote"

### Do search
	while {![eof $fd]} {
		gets $fd foo
		incr nresults
		lappend iresults $foo
		}

	set foo [split [lindex $iresults [rand $nresults]] "|"]
	set ifound 1

} else {
### Log search
	mndb_log "$unick/$uhand searched quote #$uindex"

### Do search
	while {![eof $fd] && !$ifound} {
		gets $fd foo
		set foo [split $foo "|"]

		if {[lindex $foo 0] == $uindex} {
			set ifound 1
			}
		}
}

### Close file
close $fd

### Return result
if {$ifound} {
	return "#[lindex $foo 0]: [lindex $foo 1]"
	} else {
	return "ei löydy."
	}
}


#-------------------------------------------------------------------------
proc mndb_search {unick uhand uchan utext upublic} {
global mndb_showmax_pub spmsg_nomatch

### Log search
	mndb_log "$unick/$uhand searched quote: $utext"

### Parse the given command
	set footokens [split $utext " "]
	foreach ftoken $footokens {
		set foomark [string range $ftoken 0 0]
		set foopat  [string range $ftoken 1 end]

		if {$foomark == "-" || $foomark == "+" || $foomark == "%"} {
			lappend ipatlist "$foomark *$foopat*"
			} else {
			lappend ipatlist "+ *$ftoken*"
			}
	}
	

### Get the matches

	set iresults [mndb_find $ipatlist $mndb_showmax_pub]

### Show the results
	if {$iresults != ""} {
	foreach i $iresults {
		set foo [split $i "|"]
		mndb_msg $upublic $unick $uchan "#[lindex $foo 0]: [lindex $foo 1]"
		}

# If no quotes were found
	} else {
		mndb_msg $upublic $unick $uchan "ei löydy."
	}
}


#-------------------------------------------------------------------------
proc mndb_pub_mn {unick uhost uhand uchan utext} {

mndb_msg 0 $uchan "" [mndb_get $unick $uhand $utext]
}


#-------------------------------------------------------------------------
proc mndb_msg_mn {unick uhost uhand utext} {

mndb_msg 0 $unick "" [mndb_get $unick $uhand $utext]
}


#-------------------------------------------------------------------------
proc mndb_pub_add {unick uhost uhand uchan utext} {

 mndb_add $utext $unick $uhost $uchan 1
}


#-------------------------------------------------------------------------
proc mndb_pub_rm {unick uhost uhand uchan utext} {

 mndb_rm $utext $unick $uhost $uchan 1
}


#-------------------------------------------------------------------------
proc mndb_pub_find {unick uhost uhand uchan utext} {

 mndb_search $unick $uhand $uchan $utext 1
}


#-------------------------------------------------------------------------
proc mndb_msg_find {unick uhost uhand utext} {

 mndb_search $unick $uhand "" $utext 0
}


#-------------------------------------------------------------------------

# end of script