match.tcl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # match.tcl
  2. #
  3. # This script was originally created to save clan matches in a file,
  4. # to show all the saved matches and to be able to remove them from
  5. # the file again. But it could be used for storing, showing and
  6. # deleting arbitrary lines of text in a file.
  7. #
  8. # Usage:
  9. # !addmatch match add match to the file/list.
  10. # !showmatch show the saved matches.
  11. # !delmatch number remove match with number (as shown
  12. # by !showmatch) from the file/list.
  13. # The numbers of remaining matches
  14. # might change.
  15. #
  16. # The command names can be changed in the config section below.
  17. #
  18. # Enable for a channel with: .chanset #channel +match
  19. # Disable for a channel with: .chanset #channel -match
  20. # tested versions, might run on earlier versions
  21. package require Tcl 8.6
  22. package require eggdrop 1.8.4
  23. # Config:
  24. namespace eval ::match {
  25. # channel flag for enabling/disabling
  26. setudef flag match
  27. # Name and/or path of the file you want to store the matches in and its
  28. # backup file. Channel name will be prepended to file name
  29. variable filename "matches.lst"
  30. variable filenamebak "matches.lst.bak"
  31. # Names of the commands for adding, deleting and showing
  32. variable addcommand "!addmatch"
  33. variable delcommand "!delmatch"
  34. variable showcommand "!showmatch"
  35. }
  36. # End of Config
  37. # this procedure shows the saved matches:
  38. proc ::match::show {nick host hand chan arg} {
  39. variable filename
  40. # check channel flag if enabled in this channel
  41. if {![channel get $chan match]} {
  42. return 0
  43. }
  44. # check if file exists and contains matches
  45. set mfile ${chan}.${filename}
  46. set nomatches "No matches found."
  47. if {![file exists $mfile] || [file size $mfile] == 0} {
  48. puthelp "PRIVMSG $chan :$nomatches"
  49. return 0
  50. }
  51. # read all matches from file
  52. if {[catch {open $mfile r} input]} {
  53. puthelp "PRIVMSG $nick :Error opening file: $input"
  54. return 0
  55. }
  56. while {[gets $input line] >= 0} {
  57. lappend matches $line
  58. }
  59. close $input
  60. # show each match as a message in the channel
  61. puthelp "PRIVMSG $chan :*** Match List ***"
  62. for { set i 0 } { $i < [llength $matches] } { incr i } {
  63. puthelp "PRIVMSG $chan :([expr $i +1]) [lindex $matches $i]"
  64. }
  65. puthelp "PRIVMSG $chan :*** End of Match List ***"
  66. return 1
  67. }
  68. # this procedure deletes saved matches:
  69. proc ::match::del {nick host hand chan arg} {
  70. variable filename
  71. variable filenamebak
  72. # check channel flag if enabled in this channel
  73. if {![channel get $chan match]} {
  74. return 0
  75. }
  76. # arg containing the match id must be present
  77. if {$arg == ""} {
  78. return 0
  79. }
  80. # check if file exists and contains matches
  81. set mfile ${chan}.${filename}
  82. set mfilebak ${chan}.${filenamebak}
  83. set noexist "Match does not exist."
  84. if {![file exists $mfile] || [file size $mfile] == 0} {
  85. puthelp "PRIVMSG $nick :$noexist"
  86. return 0
  87. }
  88. # read all matches from file
  89. if {[catch {open $mfile r} input]} {
  90. puthelp "PRIVMSG $nick :Error opening file: $input"
  91. return 0
  92. }
  93. while {[gets $input line] >= 0} {
  94. lappend matches $line
  95. }
  96. close $input
  97. # backup matches file
  98. file copy -force $mfile $mfilebak
  99. # write matches back to file
  100. if {[catch {open $mfile w} output]} {
  101. putshelp "PRIVMSG $nick :Error opening file: $output"
  102. return 0
  103. }
  104. set deleted 0
  105. for { set i 0 } { $i < [llength $matches] } { incr i } {
  106. # omit the match that should be deleted
  107. if {[expr $i +1] == $arg} {
  108. set deleted 1
  109. continue
  110. }
  111. puts $output "[lindex $matches $i]"
  112. }
  113. close $output
  114. # send result back to caller
  115. if {$deleted == 0} {
  116. puthelp "PRIVMSG $nick :$noexist"
  117. return 0
  118. }
  119. puthelp "NOTICE $nick :Match number $arg deleted."
  120. return 1
  121. }
  122. # this procedure adds matches to the list:
  123. proc ::match::add {nick host hand chan arg} {
  124. variable filename
  125. # check channel flag if enabled in this channel
  126. if {![channel get $chan match]} {
  127. return 0
  128. }
  129. # arg containing the match must be present
  130. if {$arg == ""} {
  131. return 0
  132. }
  133. # write match to file
  134. set mfile ${chan}.${filename}
  135. if {[catch {open $mfile a} output]} {
  136. puthelp "PRIVMSG $nick :Error opening file: $output"
  137. return 0
  138. }
  139. puts $output "$nick: $arg"
  140. close $output
  141. puthelp "NOTICE $nick :Match added."
  142. return 1
  143. }
  144. namespace eval ::match {
  145. bind pub - $showcommand ::match::show
  146. bind pub - $addcommand ::match::add
  147. # bind pub o|o $delcommand ::match::del
  148. bind pub - $delcommand ::match::del
  149. putlog "Loaded match.tcl"
  150. }