All pastes #995410 Raw Edit

Untitled

public text v1 · immutable
#995410 ·published 2008-04-24 03:42 UTC
rendered paste body
#Pour faire marcher le TCL : 
# !apprends <mot> <definiton> -> Apprend un mot
#  kc <mot> -> Vous donne la definition de <mot>
#  oubli <mot> -> efface la defintion d'un mot

putlog "Le bot intelligent - par tarball"


set learnfile "bot.learn"
bind pub * !apprends pub:learn
bind pub * "?" pub:learned
bind pub * "??" pub:learned
bind pub * "!" pub:learned
bind pub * !oubli  pub:forget

#######################################################
# Ne changez rien a partir d'ici..ou a vos risques :) #
#######################################################

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 msg:learn {nick host hand args} {
	pub:learn {$nick $host $hand "" $args}
}

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 :Comment veux tu que j'apprenne ca $nick ? "
		return
	}	

	set space [string first " " $args]
	if {$space == -1} {
		putserv "PRIVMSG $chan :Uh? Tu fais pas correctement $nick ! r'tourne a l'école"
		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... oki $nick :)"
}



proc pub:learned {nick host hand chan args} {
  global learnfile

  set found 0
	
  set args [string trim $args "{} "]
  if {[string length $args] == 0} {
	putserv "PRIVMSG $chan :$nick Eh? "
	return 
  }

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

      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]

      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]
        
      if {[string compare $args $word] == 0} {
        putserv "PRIVMSG $chan :La définition de $word est: $line"
        incr found
      }
    }
    close $file
  } else {
    putserv "PRIVMSG $chan :Aucune idée!"
    return
  }
  if {$found == 0} {
	putserv "PRIVMSG $chan :On m'a pas appris ce qu'est $args"
  }

}



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 :Je me souviens meme pas la définition de ce que tu veux me faire effacer $nick .."
        return
  }
  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]

      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 :Jsais pas.. :/"
    return
  }

  if {$found == 0} {
    putserv "PRIVMSG $chan :J'ai aucune idée ce qu'est \"$args\" "
    return
  }
  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 :J'viens d'oublier ce qu'est \"$args\" "
}