BT.Quote.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ############################# QUOTE TCL #############################
  6. #########################################################################
  7. ## ##
  8. ## BlackTools : http://blacktools.tclscripts.net ##
  9. ## Bugs report : http://www.tclscripts.net/ ##
  10. ## Online Help : irc://irc.undernet.org/tcl-help ##
  11. ## #TCL-HELP / UnderNet ##
  12. ## You can ask in english or romanian ##
  13. ## ##
  14. #########################################################################
  15. proc quote:announce {} {
  16. global black
  17. set return_time [time_return_minute $black(quote:msgtime)]
  18. set channels ""
  19. foreach chan [channels] {
  20. if {[validchan $chan]} {
  21. if {[setting:get $chan quoteofday]} {
  22. lappend channels $chan
  23. }
  24. }
  25. }
  26. if {$channels != ""} {
  27. quote:time $channels 0
  28. timer $return_time quote:announce
  29. }
  30. }
  31. proc quote:time {channels counter} {
  32. global black
  33. set chan [lindex $channels $counter]
  34. set cc [expr $counter + 1]
  35. if {$chan != ""} {
  36. quoteofday:show $chan
  37. }
  38. if {[lindex $channels $cc] == ""} {
  39. return
  40. } else {
  41. quote:time $channels $cc
  42. }
  43. }
  44. proc quoteofday:show {chan} {
  45. global black botnick
  46. set getlang [string tolower [setting:get $chan lang]]
  47. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  48. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  49. set temp_dir "$black(tempdir)/quote_temp.$timestamp"
  50. set day [clock format [clock seconds] -format {%d}]
  51. set file [open $black(quote_file) "r"]
  52. set temp [open $temp_dir "a"]
  53. while {[gets $file line] != -1} {
  54. set read_chan [lindex [split $line] 0]
  55. set enc_chan [encoding convertfrom utf-8 $read_chan]
  56. if {[string match -nocase $enc_chan $chan]} {
  57. set quote [join [lrange [split $line] 6 end]]
  58. set gettime [lindex [split $line] 4]
  59. set getday [clock format $gettime -format %d]
  60. if {$getday == $day} {
  61. puts $temp $quote
  62. }
  63. }
  64. }
  65. close $temp
  66. close $file
  67. set file [open $temp_dir "r"]
  68. set data [read -nonewline $file]
  69. close $file
  70. if {$data != ""} {
  71. if {![info exists black(quoteshow:$chan)]} {
  72. set black(quoteshow:$chan) 0
  73. }
  74. set lines [split $data "\n"]
  75. set line [lindex $lines $black(quoteshow:$chan)]
  76. if {$line == ""} {
  77. set black(quoteshow:$chan) 0
  78. set line [lindex $lines $black(quoteshow:$chan)]
  79. }
  80. set black(quoteshow:$chan) [expr $black(quoteshow:$chan) + 1]
  81. set encoded [encoding convertfrom utf-8 $line]
  82. set replace(%msg%) $encoded
  83. set message [join [string map [array get replace] $black(say.$getlang.quote.16)]]
  84. set split_message [split $message "~"]
  85. if {[onchan $botnick $chan]} {
  86. if {$black(quote:outtype) == "1"} {
  87. putserv "PRIVMSG $chan :\001ACTION $black(say.$getlang.quote.12)\001"
  88. } else {
  89. putserv "PRIVMSG $chan :$black(say.$getlang.quote.12)"
  90. }
  91. foreach mes $split_message {
  92. if {$black(quote:outtype) == "1"} {
  93. putserv "PRIVMSG $chan :\001ACTION \"$mes\"\001"
  94. } else {
  95. putserv "PRIVMSG $chan :\"$mes\""
  96. }
  97. }
  98. }
  99. }
  100. file delete $temp_dir
  101. }
  102. proc quotepublic {nick host hand chan arg} {
  103. global black lastbind
  104. set otherchan 0
  105. set return [blacktools:mychar $lastbind $hand]
  106. if {$return == "0"} {
  107. return
  108. }
  109. set who [lindex [split $arg] 0]
  110. set next [lindex [split $arg] 1]
  111. set quote [join [lrange [split $arg] 0 end]]
  112. if {$who == "*"} {
  113. set who [lindex [split $arg] 1]
  114. set quote [join [lrange [split $arg] 0 end]]
  115. }
  116. if {[regexp {^[\[]} $who]} {
  117. set who [lindex [split $arg] 1]
  118. set quote [join [lrange [split $arg] 0 end]]
  119. if {$who == "*"} {
  120. set who [lindex [split $arg] 2]
  121. set quote [join [lrange [split $arg] 0 end]]
  122. }
  123. }
  124. set type 0
  125. set chan1 $chan
  126. if {[regexp {^[&#]} $who] && [matchattr $hand nmo|AOMV $who]} {
  127. set otherchan 1
  128. set chan "$who"
  129. set who [lindex [split $arg] 1]
  130. set next [lindex [split $arg] 2]
  131. set quote [join [lrange [split $arg] 1 end]]
  132. if {$who == "*"} {
  133. set who [lindex [split $arg] 2]
  134. set quote [join [lrange [split $arg] 1 end]]
  135. }
  136. if {[regexp {^[\[]} $who]} {
  137. set who [lindex [split $arg] 2]
  138. set quote [join [lrange [split $arg] 1 end]]
  139. if {$who == "*"} {
  140. set who [lindex [split $arg] 3]
  141. set quote [join [lrange [split $arg] 1 end]]
  142. }
  143. }
  144. }
  145. if {$otherchan == "1"} {
  146. quote:process $nick $host $hand $chan $chan1 $type $who $quote [lrange [split $arg] 1 end] $next
  147. } else {
  148. quote:process $nick $host $hand $chan $chan1 $type $who $quote [lrange [split $arg] 0 end] $next
  149. }
  150. }
  151. proc quote:process {nick host hand chan chan1 type who quote arg next} {
  152. global black
  153. set cmd_status [btcmd:status $chan $hand "q" 0]
  154. if {$cmd_status == "1"} {
  155. return
  156. }
  157. if {[setting:get $chan quote]} {
  158. set show_who $who
  159. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  160. return
  161. }
  162. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  163. return
  164. }
  165. switch $who {
  166. list {
  167. if {[matchattr $hand nmo|M $chan]} {
  168. array set nicklist [list]
  169. set getlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  170. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  171. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  172. set temp "$black(tempdir)/quote_temp.$timestamp"
  173. set file [open $black(quote_file) "r"]
  174. while {[gets $file line] != -1} {
  175. set read_chan [lindex [split $line] 0]
  176. set enc_chan [encoding convertfrom utf-8 $read_chan]
  177. set read_type [lindex [split $line] 1]
  178. if {[string match -nocase $enc_chan $chan]} {
  179. set msg_num [lindex [split $line] 2]
  180. set by_who [lindex [split $line] 3]
  181. set quote [join [lrange [split $line] 6 end]]
  182. lappend nicklist($msg_num) "$quote :: $black(say.$getlang.quote.15) $by_who"
  183. }
  184. }
  185. close $file
  186. set tempwrite [open $temp w]
  187. foreach msg [lsort -integer -increasing [array names nicklist]] {
  188. puts $tempwrite "$msg [join $nicklist($msg)]"
  189. }
  190. close $tempwrite
  191. set file [open $temp "r"]
  192. set w [read -nonewline $file]
  193. close $file
  194. set data [split $w "\n"]
  195. file delete $temp
  196. module:getinfo $nick $host $hand $chan $chan1 $type $data "quote" "0" $next
  197. }
  198. }
  199. del {
  200. if {[matchattr $hand nmo|M $chan]} {
  201. if {![regexp {^[0-9]} $next]} {
  202. switch $type {
  203. 0 {
  204. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "quote"
  205. }
  206. 1 {
  207. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "quote"
  208. }
  209. 2 {
  210. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "quote"
  211. }
  212. }
  213. return
  214. }
  215. set ret [find:q:num $next $chan]
  216. if {$ret == 0} {
  217. blacktools:tell $nick $host $hand $chan $chan1 quote.13 $next
  218. return
  219. }
  220. set file [open $black(quote_file) "r"]
  221. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  222. set temp "$black(tempdir)/quote_temp.$timestamp"
  223. set tempwrite [open $temp w]
  224. while {[gets $file line] != -1} {
  225. set read_num [lindex [split $line] 2]
  226. set read_chan [lindex [split $line] 0]
  227. set enc_chan [encoding convertfrom utf-8 $read_chan]
  228. if {[string equal $next $read_num] && [string equal -nocase $enc_chan $chan]} {
  229. continue
  230. } else {
  231. puts $tempwrite $line
  232. }
  233. }
  234. close $tempwrite
  235. close $file
  236. file rename -force $temp $black(quote_file)
  237. blacktools:tell $nick $host $hand $chan $chan1 quote.10 $next
  238. }
  239. }
  240. default {
  241. if {[llength [split $arg]] > 1} {
  242. set ret_num [quote:add $nick $host $chan $who $quote]
  243. blacktools:tell $nick $host $hand $chan $chan1 quote.1 $ret_num
  244. }
  245. if {[llength [split $arg]] == 1} {
  246. quote:show $nick $host $hand $chan $chan1 $type $show_who
  247. }
  248. if {[llength [split $arg]] == 0} {
  249. quote:rand $nick $host $hand $chan $chan1 $type $show_who
  250. }
  251. }
  252. }
  253. }
  254. }
  255. proc quote:rand {nick host hand chan chan1 type who} {
  256. global black
  257. set total [quote:total $chan]
  258. set length [llength $total]
  259. if {$length > 0} {
  260. set random [expr int(rand()*$length)]
  261. set quote [lindex $total $random]
  262. set file [open $black(quote_file) "r"]
  263. while {[gets $file line] != -1} {
  264. set read_chan [lindex [split $line] 0]
  265. set num [lindex [split $line] 2]
  266. set enc_chan [encoding convertfrom utf-8 $read_chan]
  267. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote $num]} {
  268. set time [lindex [split $line] 4]
  269. set time [clock format $time -format %D]
  270. set readquote [join [lrange [split $line] 6 end]]
  271. set encoded [encoding convertfrom utf-8 $readquote]
  272. continue
  273. }
  274. }
  275. }
  276. close $file
  277. blacktools:tell $nick $host $hand $chan $chan1 quote.3 "$quote $time"
  278. set split_encoded [split $encoded "~"]
  279. foreach enc $split_encoded {
  280. blacktools:tell $nick $host $hand $chan $chan1 quote.19 $enc
  281. }
  282. }
  283. proc quote:show {nick host hand chan chan1 type who} {
  284. global black
  285. set quote_numbers ""
  286. set find_num 0
  287. set found_quote 0
  288. set show_who $who
  289. if {[regexp {^[0-9]} $who]} {
  290. set quote_num $who
  291. set find_num 1
  292. } else {
  293. set quote_list [quote:count $who $chan]
  294. set length [llength $quote_list]
  295. if {$length > 0} {
  296. set found_quote 1
  297. }
  298. set random [expr int(rand()*$length)]
  299. set quote [lindex $quote_list $random]
  300. set split_quote [split $quote ":"]
  301. set quote_num [lindex $split_quote 0]
  302. set quote_show [lindex $split_quote 1]
  303. foreach q $quote_list {
  304. set q_split [split $q ":"]
  305. set q_num [lindex $q_split 0]
  306. if {$quote_num != $q_num} {
  307. lappend quote_numbers \#$q_num
  308. }
  309. }
  310. if {$found_quote == "0"} {
  311. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  312. return
  313. }
  314. }
  315. set file [open $black(quote_file) "r"]
  316. while {[gets $file line] != -1} {
  317. set read_chan [lindex [split $line] 0]
  318. set num [lindex [split $line] 2]
  319. set enc_chan [encoding convertfrom utf-8 $read_chan]
  320. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote_num $num]} {
  321. set found_quote 1
  322. set time [lindex [split $line] 4]
  323. set time [clock format $time -format %D]
  324. set readquote [join [lrange [split $line] 6 end]]
  325. set encoded [encoding convertfrom utf-8 $readquote]
  326. continue
  327. }
  328. }
  329. close $file
  330. if {$found_quote == "0"} {
  331. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  332. return
  333. }
  334. if {$find_num == "0"} {
  335. blacktools:tell:h $nick $host $hand $chan $chan1 quote.4 "$quote_show/$length $show_who $time"
  336. }
  337. set split_encoded [split $encoded "~"]
  338. foreach enc $split_encoded {
  339. blacktools:tell $nick $host $hand $chan $chan1 quote.17 "$quote_num $enc"
  340. }
  341. if {$quote_numbers != ""} {
  342. blacktools:tell $nick $host $hand $chan $chan1 quote.18 $quote_numbers
  343. }
  344. }
  345. proc quote:total {chan} {
  346. global black
  347. set nums ""
  348. set file [open $black(quote_file) "r"]
  349. while {[gets $file line] != -1} {
  350. set read_chan [lindex [split $line] 0]
  351. set enc_chan [encoding convertfrom utf-8 $read_chan]
  352. if {[string equal -nocase $chan $enc_chan]} {
  353. set read_num [lindex [split $line] 2]
  354. lappend nums $read_num
  355. }
  356. }
  357. close $file
  358. return $nums
  359. }
  360. proc quote:count {who chan} {
  361. global black
  362. set q_count 0
  363. set return ""
  364. set found_nick 0
  365. set found_quote 0
  366. set file [open $black(quote_file) "r"]
  367. while {[gets $file line] != -1} {
  368. set read_chan [lindex [split $line] 0]
  369. set read_who [lindex [split $line] 5]
  370. set read_who [string map { "<" ""
  371. ">" ""
  372. "@" ""
  373. "+" ""
  374. } $read_who]
  375. set enc_chan [encoding convertfrom utf-8 $read_chan]
  376. set read_who [string map [list \[ {\[} \] {\]} \? {\?} \\ {\\}] $read_who]
  377. if {[string equal -nocase $chan $enc_chan] && [string match -nocase $read_who $who]} {
  378. set found_nick 1
  379. set q_count [expr $q_count + 1]
  380. set num [lindex [split $line] 2]
  381. set return "$return $num:$q_count"
  382. }
  383. }
  384. close $file
  385. if {$found_nick == "0"} {
  386. set file [open $black(quote_file) "r"]
  387. while {[gets $file line] != -1} {
  388. set read_chan [lindex [split $line] 0]
  389. set readquote [lrange [split $line] 6 end]
  390. set enc_chan [encoding convertfrom utf-8 $read_chan]
  391. if {[string equal -nocase $chan $enc_chan] && ([lsearch -exact $readquote $who] > -1)} {
  392. set found_quote 1
  393. set q_count [expr $q_count + 1]
  394. set num [lindex [split $line] 2]
  395. set return "$return $num:$q_count"
  396. }
  397. }
  398. close $file
  399. }
  400. return $return
  401. }
  402. proc quote:add {nick host chan who quote} {
  403. global black
  404. set temp_num 0
  405. set num 0
  406. set time [unixtime]
  407. while {$temp_num == 0} {
  408. set get [find:q:num $num $chan]
  409. if {$get == "$num"} {
  410. set num [expr $num + 1]
  411. } else { set temp_num 1 }
  412. }
  413. set file [open $black(quote_file) a]
  414. set enc_chan [encoding convertto utf-8 $chan]
  415. set encoded [encoding convertto utf-8 $quote]
  416. puts $file "$enc_chan QUOTE $num $nick $time $who $encoded"
  417. close $file
  418. return $num
  419. }
  420. ##############
  421. #########################################################################
  422. ## END ##
  423. #########################################################################