BT.Vote.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. } else {
  265. set list [blacktools:voting_list $chan $userlang $id 0]
  266. }
  267. if {$list == 0} {
  268. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.33 $id
  269. return
  270. }
  271. module:getinfo $nick $host $hand $chan $chan1 $type $list "vote" "0" [list $opt $id $vote_next]
  272. }
  273. }
  274. del {
  275. if {[matchattr $hand nmo|MA $chan]} {
  276. if {![regexp {^[0-9]+$} $id]} {
  277. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.7 none
  278. return
  279. }
  280. set voting_exists [blacktools:voting_exists $chan $id 1]
  281. if {$voting_exists == -1} {
  282. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $id]
  283. return
  284. }
  285. blacktools:voting_delete $chan $id
  286. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.9 [list $id]
  287. blacktools:vote_new_first
  288. }
  289. }
  290. end {
  291. if {[matchattr $hand nmo|MA $chan]} {
  292. if {![regexp {^[0-9]+$} $id]} {
  293. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.10 none
  294. return
  295. }
  296. set voting_exists [blacktools:voting_exists $chan $id 1]
  297. if {$voting_exists == -1} {
  298. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $id]
  299. return
  300. }
  301. set status [lindex $voting_exists 0]
  302. if {$status == 0} {
  303. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.11 [list $id]
  304. return
  305. }
  306. blacktools:voting_end $chan $id
  307. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.11 [list $id]
  308. blacktools:vote_new_first
  309. }
  310. }
  311. default {
  312. set get_letter [string toupper $id]
  313. if {![regexp {^[0-9]+$} $word]} {
  314. if {$type == "0"} {
  315. blacktools:tell_v2 $nick $host $hand $chan $chan1 gl.instr [list "vote"]
  316. }
  317. if {$type == "1"} {
  318. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick [list "vote"]
  319. }
  320. if {$type == "2"} {
  321. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv [list "vote"]
  322. }
  323. } else {
  324. set voting_exists [blacktools:voting_exists $chan $word 1]
  325. if {$voting_exists == -1} {
  326. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.8 [list $word]
  327. return
  328. }
  329. set status [lindex $voting_exists 0]
  330. set access [lindex $voting_exists 1]
  331. set voting_name [lindex $voting_exists 2]
  332. set options [lindex $voting_exists 3]
  333. set expires [lindex $voting_exists 4]
  334. set handle [lindex $voting_exists 5]
  335. if {$access == 1} {
  336. if {![matchattr $hand $black(localflags) $chan] && ![matchattr $hand $black(glflags)]} {
  337. return
  338. }
  339. }
  340. set letter_num 0
  341. array set op [list]
  342. set show_list ""
  343. foreach option $options {
  344. set letter [blacktools:return_letter $letter_num]
  345. set op($letter) [concat [join [split $option]]]
  346. lappend show_list [concat [join "\002$letter\002: [split $option]"]]
  347. incr letter_num
  348. }
  349. if {$get_letter == ""} {
  350. set votes [blacktools:get_votes $chan $word]
  351. set sum [lindex $votes 1]
  352. set votes [lindex $votes 0]
  353. if {$status == 1} {set read_status $black(say.$userlang.vote.13)} else {set read_status $black(say.$userlang.vote.14)}
  354. if {$access == 1} {set access $black(say.$userlang.vote.15)} else {set access $black(say.$userlang.vote.16)}
  355. if {$expires == 0} {
  356. set expire_time "N/A"
  357. } else {
  358. set expire_time [return_time_2 $userlang [expr $expires - [clock seconds]]]
  359. }
  360. if {$status == 0} {
  361. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.38 [list $word $voting_name "" $read_status $access $expire_time $handle]
  362. } else {
  363. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.17 [list $word $voting_name "" $read_status $access $expire_time $handle]
  364. }
  365. foreach w [wordwrap [join $show_list ", "] 440] {
  366. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.41 [list $w]
  367. }
  368. if {$votes == 0} {
  369. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.18 none
  370. } else {
  371. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.19 [list [join $votes ", "] $sum]
  372. }
  373. return
  374. }
  375. if {$status == 0} {
  376. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.20 [list $id]
  377. return
  378. }
  379. if {![regexp {^[A-Z]$} $get_letter]} {
  380. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.21 none
  381. return
  382. }
  383. if {![info exists op($get_letter)]} {
  384. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.22 [list $get_letter [join $show_list ", "]]
  385. return
  386. }
  387. set mask_num [get:mask "vote" $chan]
  388. set get_host [return_mask $mask_num $uhost $nick]
  389. if {$access == 1} {
  390. set vote [blacktools:vote_now $hand $get_host $chan $word 0 $get_letter]
  391. } else {
  392. set vote [blacktools:vote_now $nick $get_host $chan $word 1 $get_letter]
  393. }
  394. if {$vote == 0} {
  395. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.23 none
  396. } else {
  397. blacktools:tell_v2 $nick $host $hand $chan $chan1 vote.24 [list $get_letter $word $voting_name]
  398. }
  399. }
  400. }
  401. }
  402. }
  403. ###
  404. proc blacktools:return_letter {num} {
  405. global black
  406. 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"
  407. return [lindex $letters $num]
  408. }
  409. ###
  410. proc blacktools:get_votes {chan id} {
  411. global black
  412. set file [open $black(voting_file) r]
  413. set found_vote 0
  414. set output ""
  415. array set votes [list]
  416. while {[gets $file line] != -1} {
  417. set read_chan [lindex $line 1]
  418. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTE"] && [string equal -nocase [lindex $line 2] $id]} {
  419. lappend votes([lindex $line 5]) 1
  420. }
  421. }
  422. close $file
  423. if {[array size votes] == 0} {
  424. return 0
  425. }
  426. set sum 0
  427. foreach v [lsort -decreasing -increasing [array names votes]] {
  428. set sum [expr $sum + [llength $votes($v)]]
  429. }
  430. foreach v [lsort -decreasing -increasing [array names votes]] {
  431. lappend output "\002$v\002 [expr [expr [llength $votes($v)] * 100.0 ] / $sum] %"
  432. }
  433. return [list $output $sum]
  434. }
  435. ###
  436. proc blacktools:vote:already_vote {chan id hand host type} {
  437. global black
  438. set file [open $black(voting_file) r]
  439. set found_vote 0
  440. while {[gets $file line] != -1} {
  441. set read_chan [lindex $line 1]
  442. 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")} {
  443. set found_vote 1
  444. break
  445. }
  446. }
  447. close $file
  448. return $found_vote
  449. }
  450. ###
  451. proc blacktools:vote_now {hand host chan id type vote} {
  452. global black
  453. set file [open $black(voting_file) r]
  454. set found_vote 0
  455. while {[gets $file line] != -1} {
  456. set read_chan [lindex $line 1]
  457. 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")} {
  458. set found_vote [list [lindex $line 4] [lindex $line 5]]
  459. break
  460. }
  461. }
  462. close $file
  463. if {$found_vote == 0} {
  464. set file [open $black(voting_file) a]
  465. puts $file "VOTE $chan $id $hand $host $vote [unixtime]"
  466. close $file
  467. return 1
  468. } else {return 0}
  469. }
  470. ###
  471. proc blacktools:voting_delete {chan id} {
  472. global black
  473. set file [open $black(voting_file) r]
  474. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  475. set temp "$black(tempdir)/vote_temp.$timestamp"
  476. set tempwrite [open $temp w]
  477. while {[gets $file line] != -1} {
  478. set read_chan [lindex $line 2]
  479. set read_chan_vote [lindex $line 1]
  480. set read_id [lindex $line 1]
  481. set read_id_vote [lindex $line 2]
  482. set read_type [lindex $line 0]
  483. 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"])} {
  484. continue
  485. } else {
  486. puts $tempwrite $line
  487. }
  488. }
  489. close $file
  490. close $tempwrite
  491. file rename -force $temp $black(voting_file)
  492. }
  493. ###
  494. proc blacktools:voting_extend {chan id time} {
  495. global black
  496. set file [open $black(voting_file) r]
  497. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  498. set temp "$black(tempdir)/vote_temp.$timestamp"
  499. set tempwrite [open $temp w]
  500. while {[gets $file line] != -1} {
  501. set read_chan [lindex $line 2]
  502. set read_id [lindex $line 1]
  503. if {[string equal -nocase $read_chan $chan] && [string equal -nocase $read_id $id]} {
  504. set replace_line [lreplace $line 7 7 "$time"]
  505. continue
  506. } else {
  507. puts $tempwrite $line
  508. }
  509. }
  510. close $file
  511. if {[info exists replace_line]} {
  512. puts $tempwrite $replace_line
  513. }
  514. close $tempwrite
  515. file rename -force $temp $black(voting_file)
  516. }
  517. ###
  518. proc blacktools:voting_end {chan id} {
  519. global black
  520. set file [open $black(voting_file) r]
  521. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  522. set temp "$black(tempdir)/vote_temp.$timestamp"
  523. set tempwrite [open $temp w]
  524. while {[gets $file line] != -1} {
  525. set read_chan [lindex $line 2]
  526. set read_id [lindex $line 1]
  527. if {[string equal -nocase $read_chan $chan] && [string equal -nocase $read_id $id]} {
  528. set replace_line [lreplace $line 8 8 "0"]
  529. continue
  530. } else {
  531. puts $tempwrite $line
  532. }
  533. }
  534. close $file
  535. if {[info exists replace_line]} {
  536. puts $tempwrite $replace_line
  537. }
  538. close $tempwrite
  539. file rename -force $temp $black(voting_file)
  540. }
  541. ###
  542. proc blacktools:voting_list {chan lang text who} {
  543. global black
  544. set counter 0
  545. set file [open $black(voting_file) r]
  546. while {[gets $file line] != -1} {
  547. set read_chan [lindex $line 2]
  548. set read_type [lindex $line 0]
  549. set access [lindex $line 9]
  550. set read_chan_vote [lindex $line 1]
  551. set show_list ""
  552. if {$text == ""} {
  553. if {[string equal -nocase $chan $read_chan] && [string equal -nocase $read_type "VOTING"]} {
  554. set status [lindex $line 8]
  555. if {$who == 0 && $access == 1} {continue}
  556. set voting_name [lindex $line 4]
  557. set options [lindex $line 5]
  558. set handle [lindex $line 3]
  559. set letter_num 0
  560. foreach option $options {
  561. set letter [blacktools:return_letter $letter_num]
  562. set op($letter) [concat [join [split $option]]]
  563. lappend show_list [concat [join "\002$letter\002: [split $option]"]]
  564. incr letter_num
  565. }
  566. set expires [lindex $line 7]
  567. set id [lindex $line 1]
  568. if {$access == 1} {set access $black(say.$lang.vote.15)} else {set access $black(say.$lang.vote.16)}
  569. if {$expires == 0} {
  570. set expire_time "N/A"
  571. } else {
  572. set expire_time [return_time_2 $lang [expr $expires - [unixtime]]]
  573. }
  574. lappend output [list $id $voting_name [join $show_list ", "] $status $access $expire_time $handle]
  575. }
  576. } else {
  577. if {[string equal -nocase $chan $read_chan_vote] && [string equal -nocase $read_type "VOTE"]} {
  578. incr counter
  579. set hand [lindex $line 3]
  580. set host [lindex $line 4]
  581. set option [lindex $line 5]
  582. set time [lindex $line 6]
  583. if {$who == 0 && $access == 1} {continue}
  584. lappend output [list $counter $hand $host $option $time]
  585. }
  586. }
  587. }
  588. close $file
  589. if {![info exists output]} {return 0} else {
  590. return $output
  591. }
  592. }
  593. ###
  594. proc blacktools:voting_exists {chan get type} {
  595. global black
  596. set file [open $black(voting_file) r]
  597. set found_it -1
  598. while {[gets $file line] != -1} {
  599. set read_chan [lindex $line 2]
  600. set read_id [lindex $line 1]
  601. set read_name [lindex $line 4]
  602. 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")} {
  603. set found_it [list [lindex $line 8] [lindex $line 9] [lindex $line 4] [lindex $line 5] [lindex $line 7] [lindex $line 3]]
  604. break
  605. }
  606. }
  607. close $file
  608. return $found_it
  609. }
  610. ###
  611. proc blacktools:voting_id {chan} {
  612. global black
  613. set file [open $black(voting_file) r]
  614. set ids ""
  615. while {[gets $file line] != -1} {
  616. set read_chan [lindex $line 2]
  617. if {[string equal -nocase $chan $read_chan] && [string equal -nocase [lindex $line 0] "VOTING"]} {
  618. lappend ids [lindex $line 1]
  619. }
  620. }
  621. close $file
  622. if {$ids == ""} {
  623. return 0
  624. } else {
  625. set sort [blacktools:quicksort [join $ids]]
  626. return [lindex $sort end]
  627. }
  628. }
  629. ###
  630. proc blacktools:first_expire {} {
  631. global black
  632. if {![file exists $black(voting_file)]} {return 0}
  633. set file [open $black(voting_file) r]
  634. set times ""
  635. while {[gets $file line] != -1} {
  636. if {[string equal -nocase [lindex $line 0] "VOTING"]} {
  637. set expire [lindex $line 7]
  638. set status [lindex $line 8]
  639. if {$expire == 0} {continue}
  640. if {$status == 0} {continue}
  641. lappend times $expire
  642. }
  643. }
  644. close $file
  645. if {$times != ""} {
  646. set sort [blacktools:quicksort [join $times]]
  647. return [lindex $sort 0]
  648. } else {
  649. return 0
  650. }
  651. }