4
0

pisg-addchan.tcl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Original Author: paultwang - 2010-03-17
  2. # fixed and tweaked: Sebastien 2017-10-25
  3. # This is where pisg.cfg is stored
  4. set pp_confname "/home/jedi/pisg/pisg-0.73/pisg.cfg"
  5. # This is where your IRC logs are stored
  6. set pp_baselogdir "/home/jedi/eggdrop/logs"
  7. # This is the file extension of your logs
  8. set pp_logfilesuffix ".%Y%m%d"
  9. # This is where your output should be
  10. set pp_baseoutputdir "/var/www/html/chanstats"
  11. # This is the file extension of your output
  12. set pp_outputsuffix ".html"
  13. # Set IRC network name
  14. set pp_networkname "Undernet"
  15. # Set log file format
  16. set pp_format "eggdrop"
  17. # NOTE: When you add channel, it will make eggdrop start logging that channel
  18. bind pub m|m !pisgadd pub_pisgadd
  19. bind pub m|m !pisgdel pub_pisgdel
  20. bind pub m|m !pisgreaddall pub_pisgreaddall
  21. #STOP EDITING HERE
  22. #STOP EDITING HERE
  23. bind join - * join_autoaddbotselfjoin
  24. proc pub_pisgadd {n u h c a} {
  25. if {[string length $a] == 0} {
  26. set chan $c
  27. } else {
  28. set chan [lindex [split $a] 0]
  29. }
  30. pp_addchan $chan
  31. puthelp "privmsg $c :Added $chan"
  32. }
  33. proc pub_pisgdel {n u h c a} {
  34. if {[string length $a] == 0} {
  35. set chan $c
  36. } else {
  37. set chan [lindex [split $a] 0]
  38. }
  39. pp_delchan $chan
  40. puthelp "privmsg $c :Deleted $chan"
  41. }
  42. proc pub_pisgreaddall {n u h c a} {
  43. pp_readdallchans
  44. puthelp "privmsg $c :Re-added all channels"
  45. }
  46. proc pp_toASCII {char} {
  47. return [scan $char %c]
  48. }
  49. proc pp_toASCIIHEX {string} {
  50. set hexvalue [list]
  51. foreach char [split $string ""] {
  52. lappend hexvalue [format %x [scan $char %c]]
  53. }
  54. return $hexvalue
  55. }
  56. proc pp_sanitizechanname {chan} {
  57. # This makes it suitable for filename
  58. set chan [string tolower [string range $chan 1 end]]
  59. set newchan [list]
  60. foreach char [split $chan ""] {
  61. set ascii [pp_toASCII $char]
  62. if {$ascii >= 48 && $ascii <= 57} {lappend newchan $char; continue}
  63. if {$ascii >= 65 && $ascii <= 90} {lappend newchan $char; continue}
  64. if {$ascii >= 97 && $ascii <= 122} {lappend newchan $char; continue}
  65. lappend newchan "%[join [pp_toASCIIHEX $char]]"
  66. }
  67. set newchan [join $newchan ""]
  68. return $newchan
  69. }
  70. proc pp_addlog {chan} {
  71. global pp_baselogdir pp_logfilesuffix
  72. set logname [pp_sanitizechanname $chan]
  73. logfile "pjk" "$chan" "${pp_baselogdir}/${logname}${pp_logfilesuffix}"
  74. }
  75. proc pp_addlogfiles {} {
  76. foreach chan [channels] {
  77. pp_addlog $chan
  78. }
  79. }
  80. proc join_autoaddbotselfjoin {n u h chan} {
  81. if {[isbotnick $n]} {
  82. pp_addlog $chan
  83. }
  84. }
  85. proc pp_loadconf {} {
  86. global pp_confname
  87. set conflines [list]
  88. set fh [open $pp_confname "r"]
  89. for {set i 0} {![eof $fh]} {incr i} {
  90. lappend conflines [gets $fh]
  91. }
  92. close $fh
  93. return $conflines
  94. }
  95. proc pp_overwriteconf {conflines} {
  96. global pp_confname {temp-path}
  97. set fh [open "${pp_confname}.new" "w"]
  98. set lmax [llength $conflines]
  99. for {set i 0} {$i < $lmax} {incr i} {
  100. puts $fh [lindex $conflines $i]
  101. }
  102. close $fh
  103. file copy -force -- "${pp_confname}.new" $pp_confname
  104. file delete -- "${pp_confname}.new"
  105. return
  106. }
  107. proc pp_appendconf {addnewlines} {
  108. global pp_confname
  109. set fh [open $pp_confname "a"]
  110. set lmax [llength $addnewlines]
  111. for {set i 0} {$i < $lmax} {incr i} {
  112. puts $fh [lindex $addnewlines $i]
  113. }
  114. close $fh
  115. return
  116. }
  117. proc pp_findchaninconf {conflines} {
  118. set lmax [llength $conflines]
  119. set chanlines [list]
  120. set single [list]
  121. set begun 0
  122. for {set i 0} {$i < $lmax} {incr i} {
  123. set line [string trim [lindex $conflines $i]]
  124. if {!$begun && [string match "<channel=*>" $line]} {
  125. set channame [string tolower [join [lrange [split $line "\""] 1 end-1] "\""]]
  126. if {[string length $channame] == 0} {
  127. set channame "malform"
  128. }
  129. set single [list $channame]
  130. lappend single $i
  131. set begun 1
  132. } elseif {$begun && [string match "</channel>" $line]} {
  133. lappend single $i
  134. set begun 0
  135. lappend chanlines $single
  136. }
  137. }
  138. return $chanlines
  139. }
  140. proc pp_addchan {chan} {
  141. global pp_baselogdir pp_logfilesuffix pp_baseoutputdir pp_outputsuffix pp_networkname pp_format
  142. if {![string equal [string index $chan 0] "#"]} {
  143. putlog "autoadd err: Chan must begin with # sign"
  144. return
  145. }
  146. pp_addlog $chan
  147. set conflines [pp_loadconf]
  148. set chanlines [pp_findchaninconf $conflines]
  149. set lmax [llength $chanlines]
  150. for {set i 0} {$i < $lmax} {incr i} {
  151. if {[string equal -nocase $chan [lindex [lindex $chanlines $i] 0]]} {
  152. # Channel already exists. Ignore command.
  153. return
  154. }
  155. }
  156. set addnewlines [list]
  157. #puthelp "privmsg #ChanStats :debug 1"
  158. lappend addnewlines "<channel=\"${chan}\">"
  159. #lappend addnewlines " Logfile=\"${pp_baselogdir}/${chan}${pp_logfilesuffix}\""
  160. lappend addnewlines " LogDir=\"${pp_baselogdir}/\""
  161. lappend addnewlines " LogPrefix=\"[pp_sanitizechanname ${chan}].\""
  162. lappend addnewlines " Format=\"${pp_format}\""
  163. lappend addnewlines " Network=\"${pp_networkname}\""
  164. lappend addnewlines " OutputFile=\"${pp_baseoutputdir}/[pp_sanitizechanname ${chan}]${pp_outputsuffix}\""
  165. lappend addnewlines "</channel>"
  166. #puthelp "privmsg #ChanStats :debug 1.5"
  167. pp_appendconf $addnewlines
  168. #puthelp "privmsg #ChanStats :debug 2"
  169. }
  170. proc pp_delchan {chan} {
  171. global pp_logfilesuffix
  172. set conflines [pp_loadconf]
  173. set chanlines [pp_findchaninconf $conflines]
  174. set linestodelete [list]
  175. set lmax [llength $chanlines]
  176. for {set i 0} {$i < $lmax} {incr i} {
  177. if {[string equal -nocase $chan [lindex [lindex $chanlines $i] 0]]} {
  178. set linepair [lrange [lindex $chanlines $i] 1 2]
  179. lappend linestodelete $linepair
  180. }
  181. }
  182. set linestodelete [lsort -integer -index 0 -decreasing $linestodelete]
  183. set lmax [llength $linestodelete]
  184. for {set i 0} {$i < $lmax} {incr i} {
  185. set d [lindex $linestodelete $i]
  186. set conflines [lreplace $conflines [lindex $d 0] [lindex $d 1]]
  187. }
  188. pp_overwriteconf $conflines
  189. }
  190. proc pp_readdallchans {} {
  191. global pp_logfilesuffix
  192. set conflines [pp_loadconf]
  193. set chanlines [pp_findchaninconf $conflines]
  194. set linestodelete [list]
  195. set lmax [llength $chanlines]
  196. for {set i 0} {$i < $lmax} {incr i} {
  197. set linepair [lrange [lindex $chanlines $i] 1 2]
  198. lappend linestodelete $linepair
  199. }
  200. set linestodelete [lsort -integer -index 0 -decreasing $linestodelete]
  201. set lmax [llength $linestodelete]
  202. for {set i 0} {$i < $lmax} {incr i} {
  203. set d [lindex $linestodelete $i]
  204. set conflines [lreplace $conflines [lindex $d 0] [lindex $d 1]]
  205. }
  206. pp_overwriteconf $conflines
  207. foreach chan [channels] {
  208. pp_addchan $chan
  209. }
  210. }
  211. pp_addlogfiles