Posted by electronerd Thu 8th Mar 2007 22:02 - Syntax is TCL/iTCL - 129 views
Download | New Post | Modify | Hide line numbers
  1. #name prefix hseen means "handle seen"
  2. package require mysqltcl 3.0
  3.  
  4. set hseen_db [::mysql::connect -host localhost -user $mysqluser -password $mysqlpass -db $mysqldb]
  5. set hseen_channel #nerdstar-test
  6.  
  7.  
  8. #a little test proc
  9. bind PUB - !hseen hseen
  10. proc hseen {nick uhost hand chan txt} {
  11.     global hseen_db
  12.     #make sure the connection is up
  13.     ::mysql::ping $hseen_db   
  14.  
  15.     #escape what's gonna be put into an SQL statement from the outside
  16.     set txt [::mysql::escape $hseen_db $txt]
  17.  
  18.     #fetch the data about the user
  19.     set result [::mysql::sel $hseen_db "SELECT jointime, quittime, quitmsg FROM ixc_users WHERE nickname = '$txt' LIMIT 1" -list]
  20.     ::mysql::endquery $hseen_db
  21.  
  22.     if {[llength $result] == 0} {
  23.         putmsg $chan "$txt: not found"
  24.         return
  25.     }
  26.  
  27.     set data [lindex $result 0 ]
  28.     set jointime [lindex $data 0]
  29.     set quittime [lindex $data 1]
  30.     set quitmsg [lindex $data 2]
  31.     putmsg $chan "$txt: Last join: $jointime, Last quit: $quittime, Last quit message: $quitmsg"
  32. }
  33.  
  34. bind JOIN - "$hseen_channel *" hseen:join
  35. proc hseen:join {nick uhost hand chan} {
  36.     #we've got a separate bind for the bot joining the channel
  37.     global botnick
  38.     if { $nick == $botnick} return;
  39.    
  40.    
  41.     global hseen_db
  42.     #make sure the server is alive
  43.     ::mysql::ping $hseen_db   
  44.  
  45.     #escape what we're shoving in the query
  46.     set hand [::mysql::escape $hseen_db $hand]
  47.  
  48.     #update the join time
  49.     ::mysql::exec $hseen_db "UPDATE ixc_users SET jointime = NOW() WHERE nickname = '$hand' LIMIT 1"
  50.  
  51. }
  52.  
  53. bind SIGN - "$hseen_channel *" hseen:sign
  54. bind PART - "$hseen_channel *" hseen:part
  55. bind KICK - "$hseen_channel *" hseen:kick
  56. proc hseen:sign {nick uhost hand chan reason} {
  57.     hseen:leave $hand "Signed off: $reason"
  58. }
  59. proc hseen:part {nick uhost hand chan reason} {
  60.     hseen:leave $hand "Parted: $reason"
  61. }
  62. proc hseen:kick {nick uhost hand chan target reason} {
  63.     set khand [nick2hand $target]
  64.     hseen:leave $khand "Kicked by $nick ($hand): $reason"
  65. }
  66.  
  67. proc hseen:leave {hand reason} {
  68.     #we don't care about non-registered users
  69.     if {$hand == "*"} {
  70.         return
  71.     }
  72.  
  73.     global hseen_db
  74.     #make sure the connection is alive
  75.     ::mysql::ping $hseen_db   
  76.  
  77.     #escape what's going into the SQL to avoid injection
  78.     set hand [::mysql::escape $hseen_db $hand]
  79.     set reason [::mysql::escape $hseen_db $reason]
  80.  
  81.     #update the quit time. I subtract a second from the current time so
  82.     #that if the user rejoins within the same second, they'll still appear online
  83.     ::mysql::exec $hseen_db "UPDATE ixc_users
  84.         SET quittime = NOW() - INTERVAL 1 second,
  85.             jointime = LEAST(jointime, NOW() - INTERVAL 1 second),
  86.             quitmsg = '$reason'
  87.         WHERE nickname = '$hand'"
  88.  
  89. }
  90.  
  91.    
  92. bind JOIN - "$hseen_channel $botnick!*" hseen:botjoin
  93. proc hseen:botjoin {nick uhost hand chan} {
  94.     #we have to put this on a timer because when this bind is called
  95.     #the bot hasn't seen the userlist yet
  96.     utimer 1 "hseen:finishbotjoin $chan"
  97. }
  98.  
  99. proc hseen:finishbotjoin {chan} {
  100.     set nicks [chanlist $chan]
  101.  
  102.     global hseen_db
  103.     ::mysql::ping $hseen_db
  104.  
  105.     #set everyone offline
  106.     ::mysql::exec $hseen_db "UPDATE ixc_users
  107.         SET quittime = NOW() - INTERVAL 1 second,
  108.             jointime = LEAST(jointime, NOW() - INTERVAL 1 second),
  109.             quitmsg = 'Bot restarted'
  110.         WHERE jointime > quittime"
  111.  
  112.     set hands {}
  113.     #see who's still online and logged in
  114.     foreach nick $nicks {
  115.         set hand [nick2hand $nick $chan]
  116.         if { $hand == "*" } {
  117.             #user is not registered/logged-in so we don't care
  118.             continue
  119.         }
  120.         set hand [::mysql::escape $hseen_db $hand]
  121.         lappend hands "'$hand'"
  122.     }
  123.     set hand_list [join $hands ", "]
  124.  
  125.     #set the jointime on everyone still online
  126.     ::mysql::exec $hseen_db "UPDATE ixc_users
  127.         SET jointime = NOW()
  128.         WHERE nickname IN ($hand_list)"
  129.  
  130. }

PermaLink to this entry https://pastebin.co.uk/11578
Posted by electronerd Thu 8th Mar 2007 22:02 - Syntax is TCL/iTCL - 129 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0