BT.Vote.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ############################# VOTE TCL ##############################
  6. #########################################################################
  7. ## ##
  8. ## BlackTools : http://blacktools.tclscripts.net ##
  9. ## Bugs report : http://www.tclscripts.net/ ##
  10. ## GitHub page : https://github.com/tclscripts/BlackToolS-TCL ##
  11. ## Online Help : irc://irc.undernet.org/tcl-help ##
  12. ## #TCL-HELP / UnderNet ##
  13. ## You can ask in english or romanian ##
  14. ## ##
  15. #########################################################################
  16. ###
  17. proc blacktools:vote:greetshow {nick host hand chan} {
  18. global black
  19. if {[setting:get $chan votegreet]} {
  20. if {[isbotnick $nick]} { return }
  21. if {[info exists black(turnOnFlood:$chan)]} {
  22. return
  23. }
  24. if {[matchattr $hand nmo|MA $chan]} {
  25. set access 1
  26. } else {
  27. set access 0
  28. }
  29. set mask_num [get:mask "vote" $chan]
  30. set get_host [return_mask $mask_num $host $nick]
  31. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  32. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  33. if {$access == 1} {
  34. set list [blacktools:voting_list $chan $userlang "" 1]
  35. } else {
  36. set list [blacktools:voting_list $chan $userlang "" 0]
  37. }
  38. if {$list == 0} {return}
  39. set ids ""
  40. foreach entry $list {
  41. set status [lindex $entry 3]
  42. if {$status == 0} {continue}
  43. set id [lindex $entry 0]
  44. if {$access == 1} {
  45. set vote [blacktools:vote:already_vote $chan $id $hand $get_host 0]
  46. } else {
  47. set vote [blacktools:vote:already_vote $chan $id $hand $get_host 1]
  48. }
  49. if {$vote == 0} {
  50. lappend ids $id
  51. }
  52. }
  53. if {$ids != ""} {
  54. blacktools:tell_v2 $nick $host $hand $chan $chan vote.42 [list [join $ids ", "]]
  55. }
  56. }
  57. }
  58. ###
  59. proc blacktools:vote_new_first {} {
  60. global black
  61. set expire [blacktools:first_expire]
  62. foreach tmr [utimers] {
  63. if {[string match "*blacktools:vote:expire*" [join [lindex $tmr 1]]]} {
  64. killutimer [lindex $tmr 2]
  65. }
  66. }
  67. if {$expire != 0} {
  68. if {[info exists black(vote_expire)]} {
  69. if {$expire <= $black(vote_expire)} {
  70. set black(vote_expire) $expire
  71. utimer [expr $expire - [clock seconds]] [list blacktools:vote:expire]
  72. }
  73. } else {
  74. set black(vote_expire) $expire
  75. utimer [expr $expire - [clock seconds]] [list blacktools:vote:expire]
  76. }
  77. } else {
  78. if {[info exists black(vote_expire)]} {
  79. unset black(vote_expire)
  80. }
  81. }
  82. }
  83. ###
  84. proc blacktools:vote:expire {} {
  85. global black
  86. set file [open $black(voting_file) r]
  87. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  88. set temp "$black(tempdir)/vote_temp.$timestamp"
  89. set tempwrite [open $temp w]
  90. while {[gets $file line] != -1} {
  91. set expire [lindex $line 7]
  92. if {[string equal -nocase [lindex $line 0] "VOTE"]} {
  93. puts $tempwrite $line
  94. continue
  95. }
  96. if {[info exists black(vote_expire)]} {
  97. if {$expire <= $black(vote_expire)} {
  98. set replace_line [lreplace $line 8 8 "0"]
  99. putlog "1 $replace_line"
  100. puts $tempwrite $replace_line
  101. } else {
  102. puts $tempwrite $line
  103. }
  104. } elseif {$expire <= [clock seconds]} {
  105. set replace_line [lreplace $line 8 8 "0"]
  106. puts $tempwrite $replace_line
  107. putlog "2 $replace_line"
  108. } else {
  109. puts $tempwrite $line
  110. }
  111. }
  112. close $file
  113. close $tempwrite
  114. file rename -force $temp $black(voting_file)
  115. set read_expire [blacktools:first_expire]
  116. if {$read_expire == 0} {
  117. if {[info exists black(vote_expire)]} {
  118. unset black(vote_expire)
  119. }
  120. return
  121. } else {
  122. utimer [expr $read_expire - [clock seconds]] [list blacktools:vote:expire]
  123. set black(vote_expire) $read_expire
  124. }
  125. }
  126. ###
  127. proc vote:process {nick host uhost hand chan chan1 type arg} {
  128. global botnick black username
  129. if {![setting:get $chan vote]} {
  130. return
  131. }
  132. set cmd_status [btcmd:status $chan $hand "vote" 0]
  133. if {$cmd_status == "1"} {
  134. return
  135. }
  136. if {[matchattr $hand q]} { blacktools:tell_v2 $nick $host $hand $chan $chan1 gl.glsuspend none
  137. return
  138. }
  139. if {[matchattr $hand -|q $chan]} { blacktools:tell_v2 $nick $host $hand $chan $chan1 gl.suspend none
  140. return
  141. }
  142. set word [lindex $arg 0]
  143. set text [lindex $arg 1]
  144. set id [lindex $arg 2]
  145. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  146. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  147. switch [string tolower $word] {
  148. add {
  149. if {[matchattr $hand nmo|MA $chan]} {
  150. if {[lindex [split $arg] 1] == ""} {
  151. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.1 none
  152. return
  153. }
  154. set options [regexp -nocase -all -inline {\-option\s.+?(?=(-|$))} $text]
  155. set voting_name [regexp -nocase -inline {.+?(?=(-))} $text]
  156. set time [regexp -nocase -inline {\-time\s.+?(?=(-|$))} $text]
  157. set type [regexp -nocase -inline {\-type\s.+?(?=(-|$))} $text]
  158. set option_list ""
  159. set show_list ""
  160. set letter_num 0
  161. foreach option $options {
  162. set letter [blacktools:return_letter $letter_num]
  163. set option [lrange [split $option] 1 end]
  164. lappend option_list [concat [join $option]]
  165. lappend show_list [concat [join "\002$letter\002: $option"]]
  166. incr letter_num
  167. }
  168. if {$option_list == ""} {
  169. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.1 none
  170. return
  171. }
  172. if {$time != ""} {
  173. set time [concat [lrange [join $time] 1 end]]
  174. set check_time [catch {clock scan $time -format "%d/%m/%y %H:%M"} time_seconds]
  175. if {$check_time == 1} {
  176. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.2 none
  177. return
  178. }
  179. set valid_time [expr $time_seconds - [clock seconds]]
  180. if {$valid_time < 0} {
  181. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.2 none
  182. return
  183. }
  184. }
  185. if {![info exists time_seconds]} {
  186. set time_seconds 0
  187. }
  188. if {$type != ""} {
  189. set type [concat [lrange [join $type] 1 end]]
  190. if {![regexp {[01]} $type]} {
  191. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.3 none
  192. return
  193. }
  194. } else {set type $black(vote:type)}
  195. set voting_exists [blacktools:voting_exists $chan [join $voting_name] 0]
  196. if {$voting_exists != -1} {
  197. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.4 none
  198. return
  199. }
  200. set addtime [clock seconds]
  201. set id [expr [blacktools:voting_id $chan] + 1]
  202. set line "VOTING $id $chan $hand $voting_name [list $option_list] $addtime $time_seconds 1 $type"
  203. set file [open $black(voting_file) a]
  204. puts $file $line
  205. close $file
  206. if {$time_seconds == 0} {
  207. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.5 [list [concat [join $voting_name]] [join $show_list ", "] $id]
  208. } else {
  209. blacktools:vote_new_first
  210. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.6 [list [concat [join $voting_name]] [join $show_list ", "] [return_time_2 $userlang [expr $time_seconds - $addtime]] $id]
  211. }
  212. }
  213. }
  214. time {
  215. if {[matchattr $hand nmo|MA $chan]} {
  216. if {![regexp {^[0-9]+$} $id]} {
  217. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.39 none
  218. return
  219. }
  220. set time [lrange $text 1 end]
  221. set check_time [catch {clock scan $time -format "%d/%m/%y %H:%M"} time_seconds]
  222. if {$check_time == 1} {
  223. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.2 none
  224. return
  225. }
  226. set valid_time [expr $time_seconds - [clock seconds]]
  227. if {$valid_time < 0} {
  228. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.2 none
  229. return
  230. }
  231. set voting_exists [blacktools:voting_exists $chan $id 1]
  232. if {$voting_exists == -1} {
  233. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 $id
  234. return
  235. }
  236. set status [lindex $voting_exists 0]
  237. if {$status == 0} {
  238. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.11 [list $id]
  239. return
  240. }
  241. blacktools:voting_extend $chan $id $time_seconds
  242. blacktools:vote_new_first
  243. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.40 [list $id [return_time_2 $userlang [expr $time_seconds - [clock seconds]]]]
  244. }
  245. }
  246. list {
  247. set opt [lindex $arg 3]
  248. set vote_next [lindex $arg 4]
  249. if {![regexp {^[0-9]+$} $id]} {
  250. set vote_list 1
  251. if {[matchattr $hand nmo|MA $chan]} {
  252. set list [blacktools:voting_list $chan $userlang "" 1]
  253. } else {
  254. set list [blacktools:voting_list $chan $userlang "" 0]
  255. }
  256. if {$list == 0} {
  257. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.30 none
  258. return
  259. }
  260. module:getinfo $nick $host $hand $chan $chan1 $type $list "vote" "0" [list $id]
  261. } else {
  262. if {[matchattr $hand nmo|MA $chan]} {
  263. set list [blacktools:voting_list $chan $userlang $id 1]
  264. if {$list == 0} {
  265. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.33 $id
  266. return
  267. }
  268. module:getinfo $nick $host $hand $chan $chan1 $type $list "vote" "0" [list $opt $id $vote_next]
  269. }
  270. }
  271. }
  272. del {
  273. if {[matchattr $hand nmo|MA $chan]} {
  274. if {![regexp {^[0-9]+$} $id]} {
  275. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.7 none
  276. return
  277. }
  278. set voting_exists [blacktools:voting_exists $chan $id 1]
  279. if {$voting_exists == -1} {
  280. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $id]
  281. return
  282. }
  283. blacktools:voting_delete $chan $id
  284. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.9 [list $id]
  285. blacktools:vote_new_first
  286. }
  287. }
  288. end {
  289. if {[matchattr $hand nmo|MA $chan]} {
  290. if {![regexp {^[0-9]+$} $id]} {
  291. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.10 none
  292. return
  293. }
  294. set voting_exists [blacktools:voting_exists $chan $id 1]
  295. if {$voting_exists == -1} {
  296. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $id]
  297. return
  298. }
  299. set status [lindex $voting_exists 0]
  300. if {$status == 0} {
  301. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.11 [list $id]
  302. return
  303. }
  304. blacktools:voting_end $chan $id
  305. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.11 [list $id]
  306. blacktools:vote_new_first
  307. }
  308. }
  309. default {
  310. set get_letter [string toupper $id]
  311. if {![regexp {^[0-9]+$} $word]} {
  312. if {$type == "0"} {
  313. blacktools:tell_v2 $nick $host $hand $chan $chan1 gl.instr [list "vote"]
  314. }
  315. if {$type == "1"} {
  316. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick [list "vote"]
  317. }
  318. if {$type == "2"} {
  319. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv [list "vote"]
  320. }
  321. } else {
  322. set voting_exists [blacktools:voting_exists $chan $word 1]
  323. if {$voting_exists == -1} {
  324. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $word]
  325. return
  326. }
  327. set status [lindex $voting_exists 0]
  328. set access [lindex $voting_exists 1]
  329. set voting_name [lindex $voting_exists 2]
  330. set options [lindex $voting_exists 3]
  331. set expires [lindex $voting_exists 4]
  332. set handle [lindex $voting_exists 5]
  333. if {$access == 1} {
  334. if {![matchattr $hand $black(localflags) $chan] && ![matchattr $hand $black(glflags)]} {
  335. return
  336. }
  337. }
  338. set letter_num 0
  339. array set op [list]
  340. set show_list ""
  341. foreach option $options {
  342. set letter [blacktools:return_letter $letter_num]
  343. set op($letter) [concat [join [split $option]]]
  344. lappend show_list [concat [join "\002$letter\002: [split $option]"]]
  345. incr letter_num
  346. }
  347. if {$get_letter == ""} {
  348. set votes [blacktools:get_votes $chan $word]
  349. set sum [lindex $votes 1]
  350. set votes [lindex $votes 0]
  351. if {$status == 1} {set read_status $black(say.$userlang.vote.13)} else {set read_status $black(say.$userlang.vote.14)}
  352. if {$access == 1} {set access $black(say.$userlang.vote.15)} else {set access $black(say.$userlang.vote.16)}
  353. if {$expires == 0} {
  354. set expire_time "N/A"
  355. } else {
  356. set expire_time [return_time_2 $userlang [expr $expires - [clock seconds]]]
  357. }
  358. if {$status == 0} {
  359. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.38 [list $word $voting_name "" $read_status $access $expire_time $handle]
  360. } else {
  361. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.17 [list $word $voting_name "" $read_status $access $expire_time $handle]
  362. }
  363. foreach w [wordwrap [join $show_list ", "] 440] {
  364. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.41 [list $w]
  365. }
  366. if {$votes == 0} {
  367. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.18 none
  368. } else {
  369. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.19 [list [join $votes ", "] $sum]
  370. }
  371. return
  372. }
  373. if {$status == 0} {
  374. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.20 [list $id]
  375. return
  376. }
  377. if {![regexp {^[A-Z]$} $get_letter]} {
  378. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.21 none
  379. return
  380. }
  381. if {![info exists op($get_letter)]} {
  382. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.22 [list $get_letter [join $show_list ", "]]
  383. return
  384. }
  385. set mask_num [get:mask "vote" $chan]
  386. set get_host [return_mask $mask_num $uhost $nick]
  387. if {$access == 1} {
  388. set vote [blacktools:vote_now $hand $get_host $chan $word 0 $get_letter]
  389. } else {
  390. set vote [blacktools:vote_now $nick $get_host $chan $word 1 $get_letter]
  391. }
  392. if {$vote == 0} {
  393. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.23 none
  394. } else {
  395. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.24 [list $get_letter $word $voting_name]
  396. }
  397. }
  398. }
  399. }
  400. }
  401. ###
  402. proc blacktools:return_letter {num} {
  403. global black
  404. set letters "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
  405. return [lindex $letters $num]
  406. }
  407. ###
  408. proc blacktools:get_votes {chan id} {
  409. global black
  410. set file [open $black(voting_file) r]
  411. set found_vote 0
  412. set output ""
  413. array set votes [list]
  414. while {[gets $file line] != -1} {
  415. set read_chan [lindex $line 1]
  416. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTE"] && [string equal -nocase [lindex $line 2] $id]} {
  417. lappend votes([lindex $line 5]) 1
  418. }
  419. }
  420. close $file
  421. if {[array size votes] == 0} {
  422. return 0
  423. }
  424. set sum 0
  425. foreach v [lsort -decreasing -increasing [array names votes]] {
  426. set sum [expr $sum + [llength $votes($v)]]
  427. }
  428. foreach v [lsort -decreasing -increasing [array names votes]] {
  429. lappend output "\002$v\002 [expr [expr [llength $votes($v)] * 100.0 ] / $sum] %"
  430. }
  431. return [list $output $sum]
  432. }
  433. ###
  434. proc blacktools:vote:already_vote {chan id hand host type} {
  435. global black
  436. set file [open $black(voting_file) r]
  437. set found_vote 0
  438. while {[gets $file line] != -1} {
  439. set read_chan [lindex $line 1]
  440. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTE"] && [string equal -nocase [lindex $line 2] $id] && ([string equal -nocase [lindex $line 3] $hand] && $type == "0" || [string equal -nocase [lindex $line 4] $host] && $type == "1")} {
  441. set found_vote 1
  442. break
  443. }
  444. }
  445. close $file
  446. return $found_vote
  447. }
  448. ###
  449. proc blacktools:vote_now {hand host chan id type vote} {
  450. global black
  451. set file [open $black(voting_file) r]
  452. set found_vote 0
  453. while {[gets $file line] != -1} {
  454. set read_chan [lindex $line 1]
  455. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTE"] && [string equal -nocase [lindex $line 2] $id] && ([string equal -nocase [lindex $line 3] $hand] && $type == "0" || [string equal -nocase [lindex $line 4] $host] && $type == "1")} {
  456. set found_vote [list [lindex $line 4] [lindex $line 5]]
  457. break
  458. }
  459. }
  460. close $file
  461. if {$found_vote == 0} {
  462. set file [open $black(voting_file) a]
  463. puts $file "VOTE $chan $id $hand $host $vote [unixtime]"
  464. close $file
  465. return 1
  466. } else {return 0}
  467. }
  468. ###
  469. proc blacktools:voting_delete {chan id} {
  470. global black
  471. set file [open $black(voting_file) r]
  472. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  473. set temp "$black(tempdir)/vote_temp.$timestamp"
  474. set tempwrite [open $temp w]
  475. while {[gets $file line] != -1} {
  476. set read_chan [lindex $line 2]
  477. set read_chan_vote [lindex $line 1]
  478. set read_id [lindex $line 1]
  479. set read_id_vote [lindex $line 2]
  480. set read_type [lindex $line 0]
  481. if {([string equal -nocase $read_chan $chan] && [string equal -nocase $read_id $id] && [string equal -nocase $read_type "VOTING"]) || ([string equal -nocase $read_chan_vote $chan] && [string equal -nocase $read_id_vote $id] && [string equal -nocase $read_type "VOTE"])} {
  482. continue
  483. } else {
  484. puts $tempwrite $line
  485. }
  486. }
  487. close $file
  488. close $tempwrite
  489. file rename -force $temp $black(voting_file)
  490. }
  491. ###
  492. proc blacktools:voting_extend {chan id time} {
  493. global black
  494. set file [open $black(voting_file) r]
  495. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  496. set temp "$black(tempdir)/vote_temp.$timestamp"
  497. set tempwrite [open $temp w]
  498. while {[gets $file line] != -1} {
  499. set read_chan [lindex $line 2]
  500. set read_id [lindex $line 1]
  501. if {[string equal -nocase $read_chan $chan] && [string equal -nocase $read_id $id]} {
  502. set replace_line [lreplace $line 7 7 "$time"]
  503. continue
  504. } else {
  505. puts $tempwrite $line
  506. }
  507. }
  508. close $file
  509. if {[info exists replace_line]} {
  510. puts $tempwrite $replace_line
  511. }
  512. close $tempwrite
  513. file rename -force $temp $black(voting_file)
  514. }
  515. ###
  516. proc blacktools:voting_end {chan id} {
  517. global black
  518. set file [open $black(voting_file) r]
  519. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  520. set temp "$black(tempdir)/vote_temp.$timestamp"
  521. set tempwrite [open $temp w]
  522. while {[gets $file line] != -1} {
  523. set read_chan [lindex $line 2]
  524. set read_id [lindex $line 1]
  525. if {[string equal -nocase $read_chan $chan] && [string equal -nocase $read_id $id]} {
  526. set replace_line [lreplace $line 8 8 "0"]
  527. continue
  528. } else {
  529. puts $tempwrite $line
  530. }
  531. }
  532. close $file
  533. if {[info exists replace_line]} {
  534. puts $tempwrite $replace_line
  535. }
  536. close $tempwrite
  537. file rename -force $temp $black(voting_file)
  538. }
  539. ###
  540. proc blacktools:voting_list {chan lang text who} {
  541. global black
  542. set counter 0
  543. set file [open $black(voting_file) r]
  544. while {[gets $file line] != -1} {
  545. set read_chan [lindex $line 2]
  546. set read_type [lindex $line 0]
  547. set access [lindex $line 9]
  548. set read_chan_vote [lindex $line 1]
  549. set show_list ""
  550. if {$text == ""} {
  551. if {[string equal -nocase $chan $read_chan] && [string equal -nocase $read_type "VOTING"]} {
  552. set status [lindex $line 8]
  553. if {$who == 0 && $access == 1} {continue}
  554. set voting_name [lindex $line 4]
  555. set options [lindex $line 5]
  556. set handle [lindex $line 3]
  557. set letter_num 0
  558. foreach option $options {
  559. set letter [blacktools:return_letter $letter_num]
  560. set op($letter) [concat [join [split $option]]]
  561. lappend show_list [concat [join "\002$letter\002: [split $option]"]]
  562. incr letter_num
  563. }
  564. set expires [lindex $line 7]
  565. set id [lindex $line 1]
  566. if {$access == 1} {set access $black(say.$lang.vote.15)} else {set access $black(say.$lang.vote.16)}
  567. if {$expires == 0} {
  568. set expire_time "N/A"
  569. } else {
  570. set expire_time [return_time_2 $lang [expr $expires - [unixtime]]]
  571. }
  572. lappend output [list $id $voting_name [join $show_list ", "] $status $access $expire_time $handle]
  573. }
  574. } else {
  575. if {[string equal -nocase $chan $read_chan_vote] && [string equal -nocase $read_type "VOTE"]} {
  576. incr counter
  577. set hand [lindex $line 3]
  578. set host [lindex $line 4]
  579. set option [lindex $line 5]
  580. set time [lindex $line 6]
  581. if {$who == 0 && $access == 1} {continue}
  582. lappend output [list $counter $hand $host $option $time]
  583. }
  584. }
  585. }
  586. close $file
  587. if {![info exists output]} {return 0} else {
  588. return $output
  589. }
  590. }
  591. ###
  592. proc blacktools:voting_exists {chan get type} {
  593. global black
  594. set file [open $black(voting_file) r]
  595. set found_it -1
  596. while {[gets $file line] != -1} {
  597. set read_chan [lindex $line 2]
  598. set read_id [lindex $line 1]
  599. set read_name [lindex $line 4]
  600. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTING"] && ([string equal -nocase $read_name $get] && $type == "0" || [string equal -nocase $read_id $get] && $type == "1")} {
  601. set found_it [list [lindex $line 8] [lindex $line 9] [lindex $line 4] [lindex $line 5] [lindex $line 7] [lindex $line 3]]
  602. break
  603. }
  604. }
  605. close $file
  606. return $found_it
  607. }
  608. ###
  609. proc blacktools:voting_id {chan} {
  610. global black
  611. set file [open $black(voting_file) r]
  612. set ids ""
  613. while {[gets $file line] != -1} {
  614. set read_chan [lindex $line 2]
  615. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTING"]} {
  616. lappend ids [lindex $line 1]
  617. }
  618. }
  619. close $file
  620. if {$ids == ""} {
  621. return 0
  622. } else {
  623. set sort [blacktools:quicksort [join $ids]]
  624. return [lindex $sort end]
  625. }
  626. }
  627. ###
  628. proc blacktools:first_expire {} {
  629. global black
  630. if {![file exists $black(voting_file)]} {return 0}
  631. set file [open $black(voting_file) r]
  632. set times ""
  633. while {[gets $file line] != -1} {
  634. if {[string equal -nocase [lindex $line 0] "VOTING"]} {
  635. set expire [lindex $line 7]
  636. set status [lindex $line 8]
  637. if {$expire == 0} {continue}
  638. if {$status == 0} {continue}
  639. lappend times $expire
  640. }
  641. }
  642. close $file
  643. if {$times != ""} {
  644. set sort [blacktools:quicksort [join $times]]
  645. return [lindex $sort 0]
  646. } else {
  647. return 0
  648. }
  649. }