4
0

match.tcl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #
  21. # See https://github.com/hwipl/eggdrop-scripts for the latest version and
  22. # additional information including the license (MIT).
  23. # tested versions, might run on earlier versions
  24. package require Tcl 8.6
  25. package require eggdrop 1.8.4
  26. # Config:
  27. namespace eval ::match {
  28. # channel flag for enabling/disabling
  29. setudef flag match
  30. # Name and/or path of the file you want to store the matches in and its
  31. # backup file. Channel name will be prepended to file name
  32. variable filename "matches.lst"
  33. variable filenamebak "matches.lst.bak"
  34. # Names of the commands for adding, deleting and showing
  35. variable addcommand "!addmatch"
  36. variable delcommand "!delmatch"
  37. variable showcommand "!showmatch"
  38. }
  39. # End of Config
  40. # this procedure shows the saved matches:
  41. proc ::match::show {nick host hand chan arg} {
  42. variable filename
  43. # check channel flag if enabled in this channel
  44. if {![channel get $chan match]} {
  45. return 0
  46. }
  47. # check if file exists and contains matches
  48. set mfile ${chan}.${filename}
  49. set nomatches "No matches found."
  50. if {![file exists $mfile] || [file size $mfile] == 0} {
  51. puthelp "PRIVMSG $chan :$nomatches"
  52. return 0
  53. }
  54. # read all matches from file
  55. if {[catch {open $mfile r} input]} {
  56. puthelp "PRIVMSG $nick :Error opening file: $input"
  57. return 0
  58. }
  59. while {[gets $input line] >= 0} {
  60. lappend matches $line
  61. }
  62. close $input
  63. # show each match as a message in the channel
  64. puthelp "PRIVMSG $chan :*** Match List ***"
  65. for { set i 0 } { $i < [llength $matches] } { incr i } {
  66. puthelp "PRIVMSG $chan :([expr $i +1]) [lindex $matches $i]"
  67. }
  68. puthelp "PRIVMSG $chan :*** End of Match List ***"
  69. return 1
  70. }
  71. # this procedure deletes saved matches:
  72. proc ::match::del {nick host hand chan arg} {
  73. variable filename
  74. variable filenamebak
  75. # check channel flag if enabled in this channel
  76. if {![channel get $chan match]} {
  77. return 0
  78. }
  79. # arg containing the match id must be present
  80. if {$arg == ""} {
  81. return 0
  82. }
  83. # check if file exists and contains matches
  84. set mfile ${chan}.${filename}
  85. set mfilebak ${chan}.${filenamebak}
  86. set noexist "Match does not exist."
  87. if {![file exists $mfile] || [file size $mfile] == 0} {
  88. puthelp "PRIVMSG $nick :$noexist"
  89. return 0
  90. }
  91. # read all matches from file
  92. if {[catch {open $mfile r} input]} {
  93. puthelp "PRIVMSG $nick :Error opening file: $input"
  94. return 0
  95. }
  96. while {[gets $input line] >= 0} {
  97. lappend matches $line
  98. }
  99. close $input
  100. # backup matches file
  101. file copy -force $mfile $mfilebak
  102. # write matches back to file
  103. if {[catch {open $mfile w} output]} {
  104. putshelp "PRIVMSG $nick :Error opening file: $output"
  105. return 0
  106. }
  107. set deleted 0
  108. for { set i 0 } { $i < [llength $matches] } { incr i } {
  109. # omit the match that should be deleted
  110. if {[expr $i +1] == $arg} {
  111. set deleted 1
  112. continue
  113. }
  114. puts $output "[lindex $matches $i]"
  115. }
  116. close $output
  117. # send result back to caller
  118. if {$deleted == 0} {
  119. puthelp "PRIVMSG $nick :$noexist"
  120. return 0
  121. }
  122. puthelp "NOTICE $nick :Match number $arg deleted."
  123. return 1
  124. }
  125. # this procedure adds matches to the list:
  126. proc ::match::add {nick host hand chan arg} {
  127. variable filename
  128. # check channel flag if enabled in this channel
  129. if {![channel get $chan match]} {
  130. return 0
  131. }
  132. # arg containing the match must be present
  133. if {$arg == ""} {
  134. return 0
  135. }
  136. # write match to file
  137. set mfile ${chan}.${filename}
  138. if {[catch {open $mfile a} output]} {
  139. puthelp "PRIVMSG $nick :Error opening file: $output"
  140. return 0
  141. }
  142. puts $output "$nick: $arg"
  143. close $output
  144. puthelp "NOTICE $nick :Match added."
  145. return 1
  146. }
  147. namespace eval ::match {
  148. bind pub - $showcommand ::match::show
  149. bind pub - $addcommand ::match::add
  150. # bind pub o|o $delcommand ::match::del
  151. bind pub - $delcommand ::match::del
  152. putlog "Loaded match.tcl"
  153. }