BT.Vote.tcl 21 KB

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