watch.tcl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/tclsh
  2. proc watch_nick {nick host hand chan argv} {
  3. global watch_nick_user
  4. set watch_nick_user $nick
  5. # check if there are enough parameters
  6. if {[llength $argv] < 2} {
  7. puthelp "PRIVMSG $nick :Usage: !watch <add|del|chk> <nick>"
  8. #puts "PRIVMSG $nick :Usage: !watch <add|del> <nick>"
  9. return
  10. }
  11. # set some variables
  12. set command [lindex $argv 0]
  13. set nick2watch [lindex $argv 1]
  14. if {$command == "add"} {
  15. watch_add $nick2watch
  16. } elseif {$command == "del" } {
  17. watch_del $nick2watch
  18. } elseif {$command == "chk" } {
  19. watch_chk $nick2watch
  20. }
  21. }
  22. proc watch_add {nick} {
  23. # file where to save nicks
  24. set nickdb "watch_nick.db"
  25. # helper var to check if nick is already saved
  26. set found 0
  27. # open file and start reading and comparing saved nicks with submitted one
  28. if ![catch {open $nickdb r} input] {
  29. while {[gets $input line] >= 0} {
  30. if {[string compare -nocase $line $nick] == 0} {
  31. set found 1
  32. }
  33. }
  34. close $input
  35. }
  36. # insert nick into nickdb if it´s not already in it
  37. if { $found == 0} {
  38. if ![catch {open $nickdb a} output] {
  39. puts $output $nick
  40. close $output
  41. }
  42. }
  43. }
  44. proc watch_del {nick} {
  45. # file where to save nicks
  46. set nickdb "watch_nick.db"
  47. # open file and read nicks from it (omit nick that´s about to get deleted)
  48. if ![catch {open $nickdb r} input] {
  49. while {[gets $input line] >= 0} {
  50. if {$line != $nick} {
  51. lappend nicklist $line
  52. }
  53. }
  54. close $input
  55. }
  56. # write nicks to the file again
  57. if ![catch {open $nickdb w} output] {
  58. foreach entry $nicklist {
  59. puts $output $entry
  60. }
  61. close $output
  62. }
  63. }
  64. proc watch_chk {nick2watch} {
  65. bind RAW - 401 watch_chk_nosuch
  66. bind RAW - 311 watch_chk_info
  67. putserv "WHOIS $nick2watch"
  68. }
  69. proc watch_chk_nosuch {var1 var2 var3} {
  70. global watch_nick_user
  71. puthelp "PRIVMSG $watch_nick_user :offline $var1 || $var2 || $var3"
  72. unbind RAW - 401 watch_chk_nosuch
  73. unbind RAW - 311 watch_chk_info
  74. }
  75. proc watch_chk_info {var1 var2 var3} {
  76. global watch_nick_user
  77. puthelp "PRIVMSG $watch_nick_user :online $var1 || $var2 || $var3"
  78. unbind RAW - 401 watch_chk_nosuch
  79. unbind RAW - 311 watch_chk_info
  80. }
  81. bind pub - !watch watch_nick