package require mytcl

set sqlhost "localhost"
set sqluser "*****"
set sqlpassword "******"
set sqldbname "*******"
set sqltablename "*****"
set maxskaic 0
set kur(chans) "#caurums"

bind pubm - "*ornis*" runaajam
bind pub - !addbad addbad

proc addbad {nick uhost hand chan text} {
		if {$text == ""} {
		putserv "notice $nick :pieliec slikto vaardu"
		return 0
		}
		set fd2 [open scripts/badwords.txt a]
		puts $fd2 $text
		putserv "notice $nick :pieliku slikto vaardu -> $text"
		close $fd2
}
proc badcheck {nick uhost hand chan text} {
	global kur
	set wrong 0
	set out [open scripts/badwords.txt r]
	while {![eof $out]} {
	set badwords [gets $out]
	}
	close $input
	foreach t $text b $kur(badwords) { if {[string tolower $t] == [string tolower $b]} { set wrong 1 }}
	return $wrong
}

proc checktext {nick uhost hand chan text} {
	set wrong 0
	set users [chanlist $chan]
	foreach t $text u $users { if {[string tolower $t] == [string tolower $u]} { set wrong 1 }}
	return $wrong
}

proc validchan {chan} {
   global kur ; set valid 0
   foreach channel $kur(chans) { if {$channel == [string tolower $chan]} { set valid 1 }}
   return $valid
}

proc runaajam { nick uhost hand chan text } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick irjautziim
  if {![validchan $chan]} { return 0 }
  set connectsql [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsql $sqldbname
  set maxskaic 0
  set irjautziim 0
  resetdb
  if {[expr [string length $text]-[string length "bobis"]] > 3} {
    regsub -all -nocase $botnick $text \%niks text
    regsub -all -nocase [string range $botnick 0 [expr [string len $botnick] -2]] $text \%niks text
    sql query $connectsql "select * from $sqltablename where teksc = '[mysqlescape $text]'"
    set tt 0
    set tt [sql numrows $connectsql]
    if {$tt == 0} {
      piemekltxt $chan $nick $text
			if {![checktext $nick $uhost $hand $chan $text]} { return 0 }
			if {![badcheck $nick $uhost $hand $chan $text]} { return 0 }
			sql exec $connectsql "insert into $sqltablename (teksc) values('[mysqlescape $text]')"
    } else {
      piemekltxt $chan $nick $text
    }
  } else {
    randtext $chan $nick
  }
  sql disconnect $connectsql
}

proc randtext { chan nick } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick irjautaajumziime
  if {![validchan $chan]} { return 0 }
	set connectsqldivi [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsqldivi $sqldbname
  set co [sql query $connectsqldivi "select * from $sqltablename ORDER BY RAND() limit 1"]
  while { [set row [sql fetchrow $connectsqldivi $co]] != "" } {
    set atbilde [lindex $row 1]
    regsub -all \%niks $atbilde $nick atbilde
		set garums [string length $atbilde]
		set timeout [expr $garums / 4]
		utimer $timeout { putserv "PRIVMSG $chan : $timeout ... $atbilde" }
		break
  }
  sql disconnect $connectsqldivi
}

proc resetdb { } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick
  set connectsqltriis [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsqltriis $sqldbname
  set codivi [sql query $connectsqltriis "select * from $sqltablename where skaic <> '0'"]
  while { [set row [sql fetchrow $connectsqltriis $codivi]] != "" } {
    set idnum [lindex $row 0]
    sql exec $connectsqltriis "UPDATE $sqltablename SET skaic='0' WHERE id='$idnum'"
  }
  sql disconnect $connectsqltriis
}

proc piemekltxt { chan nick text } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick maxskaic
	set connectsqlchetri [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsqlchetri $sqldbname
  set izlasiitie ""
  for {set i 0} {$i < [llength $text]} {incr i} {
    if { [string length [lrange $text $i $i]] > 3 && [lrange $text $i $i] != "\%niks" } {
      lappend izlasiitie "[lrange $text $i $i]"
    }
  }
  if {$izlasiitie != ""} {
    for {set i 0} {$i < [llength $izlasiitie]} {incr i} {
      set cotriis [sql query $connectsqlchetri "select * from $sqltablename where teksc like '\%[lrange $izlasiitie $i $i]\%'"]
      if { [sql numrows $connectsqlchetri] != 0 } {
        while { [set row [sql fetchrow $connectsqlchetri $cotriis]] != "" } {
          set idnum [lindex $row 0]
          set konkskaic [expr [lindex $row 2]+1]
          sql exec $connectsqlchetri "UPDATE $sqltablename SET skaic='$konkskaic' WHERE id='$idnum'"
          if {$maxskaic < $konkskaic} { set maxskaic $konkskaic }
        }

      }
    sql endquery $connectsqlchetri $cotriis
    }
  atlasiitpeecskaic $chan $nick $text
  } else {
    randtext $chan $nick
  }
  sql disconnect $connectsqlchetri
}

proc atlasiitpeecskaic { chan nick text } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick maxskaic irjautaajumziime
  if {![validchan $chan]} { return 0 }
  set connectsqlpieci [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsqlpieci $sqldbname
  set skaitish $maxskaic
  set vajag 0
  set cochetri [sql query $connectsqlpieci "select * from $sqltablename where (teksc <> '$text') and (skaic = '$skaitish') ORDER BY RAND() LIMIT 5"]
  if { [sql numrows $connectsqlpieci] <= 1 && $skaitish >= 1 } {
    sql endquery $connectsqlpieci $cochetri
    set cochetri [sql query $connectsqlpieci "select * from $sqltablename where (teksc <> '$text') and (skaic = '[expr $skaitish - 1]') ORDER BY RAND() LIMIT 2"]
  }
  if { [sql numrows $connectsqlpieci] != 0 } {
    while { [set row [sql fetchrow $connectsqlpieci $cochetri]] != "" } {
      set atbilde [lindex $row 1]
      regsub -all \%niks $atbilde $nick atbilde
			set garums [string length $atbilde]
			set timeout [expr $garums / 4]
			utimer $timeout { putserv "PRIVMSG $chan : $timeout ... $atbilde" }
      break
    }
  } else { randtext $chan $nick }
  sql disconnect $connectsqlpieci
}
bind pub - !smadzene smadzene
proc smadzene { nick uhost hand chan text } {
  global sqlhost sqluser sqlpassword sqldbname sqltablename botnick
  if {![validchan $chan]} { return 0 }
  set connectsqlseshi [sql connect $sqlhost $sqluser $sqlpassword]
  sql selectdb $connectsqlseshi $sqldbname
  set lielumz 0
  set rindinjas 0
  set copieci [sql query $connectsqlseshi "SHOW TABLE STATUS"]
  while { [set row [sql fetchrow $connectsqlseshi $copieci]] != "" } {
    set rindinjas [lindex $row 3]
    set lielumz [expr [lindex $row 5] + [lindex $row 7]]
  }
  putserv "NOTICE $chan :Manaas smadzenees ir $rindinjas rindinjas un tas kopaa ir apmeeram [expr $lielumz/1024] KB :)p !!!"
  sql disconnect $connectsqlseshi
}
putlog "Loading bobis script"
