BT.Notes.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ############################# NOTES 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-script ##
  11. ## Online Help : irc://irc.undernet.org/tcl-help ##
  12. ## #TCL-HELP / UnderNet ##
  13. ## You can ask in english or romanian ##
  14. ## ##
  15. #########################################################################
  16. proc note:process {nick host hand chan chan1 who note user_send note_send number type} {
  17. global black botnick
  18. set cmd_status [btcmd:status $chan $hand "note" 0]
  19. if {$cmd_status == "1"} {
  20. return
  21. }
  22. if {![setting:get $chan note] && ![matchattr $hand nmo]} {
  23. return
  24. }
  25. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  26. return
  27. }
  28. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  29. return
  30. }
  31. set getlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  32. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  33. switch $who {
  34. list {
  35. set notes_total 0
  36. array set listlist [list]
  37. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  38. set temp "$black(tempdir)/list_temp.$timestamp"
  39. set file [open $black(notes_file) "r"]
  40. while {[gets $file line] != -1} {
  41. set handle [lindex [split $line] 4]
  42. set readtype [lindex [split $line] 0]
  43. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "LIST"]} {
  44. set notes_total [expr $notes_total + 1]
  45. set num [lindex [split $line] 2]
  46. set readnote [lrange [split $line] 6 end]
  47. if {[llength [lrange [split $line] 6 end]] > 7 } {
  48. set readnote [lrange [split $line] 6 13]
  49. set readnote "$readnote .."
  50. }
  51. set encoded [encoding convertfrom utf-8 $readnote]
  52. lappend listlist($num) [join "$encoded"]
  53. }
  54. }
  55. close $file
  56. set tempwrite [open $temp w]
  57. foreach note [lsort -integer -increasing [array names listlist]] {
  58. puts $tempwrite [join "$note $listlist($note)"]
  59. }
  60. close $tempwrite
  61. set file [open $temp "r"]
  62. set w [read -nonewline $file]
  63. close $file
  64. set data [split $w "\n"]
  65. file delete $temp
  66. if {$notes_total == "0"} {
  67. blacktools:tell $nick $host $hand $chan $chan1 notes.13 none
  68. return
  69. }
  70. blacktools:tell $nick $host $hand $chan $chan1 notes.14 "$notes_total"
  71. module:getinfo $nick $host $hand $chan $chan1 $type $data "noteslist" "0" $number
  72. }
  73. inbox {
  74. set notes_read 0
  75. set notes_total 0
  76. set notes_unread ""
  77. array set inboxlist [list]
  78. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  79. set temp "$black(tempdir)/inbox_temp.$timestamp"
  80. set file [open $black(notes_file) "r"]
  81. while {[gets $file line] != -1} {
  82. set handle [lindex [split $line] 4]
  83. set readtype [lindex [split $line] 0]
  84. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  85. set notes_total [expr $notes_total + 1]
  86. set num [lindex [split $line] 2]
  87. set readnote [lrange [split $line] 7 end]
  88. set readit [lindex [split $line] 3]
  89. if {$readit == "0"} {
  90. set notes_read [expr $notes_read + 1]
  91. lappend notes_unread $num
  92. }
  93. if {[llength $readnote] > 7 } {
  94. set readnote [lrange [split $line] 7 14]
  95. set readnote "$readnote .."
  96. }
  97. set encoded [encoding convertfrom utf-8 $readnote]
  98. if {$readit == "0"} {
  99. lappend inboxlist($num) "$black(say.$getlang.notes.10) $encoded"
  100. } else {
  101. lappend inboxlist($num) "$black(say.$getlang.notes.9) $encoded"
  102. }
  103. }
  104. }
  105. close $file
  106. set tempwrite [open $temp w]
  107. foreach note [lsort -integer -increasing [array names inboxlist]] {
  108. puts $tempwrite [join "$note $inboxlist($note)"]
  109. }
  110. close $tempwrite
  111. set file [open $temp "r"]
  112. set w [read -nonewline $file]
  113. close $file
  114. set data [split $w "\n"]
  115. file delete $temp
  116. if {$notes_unread != ""} {
  117. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  118. } else {
  119. blacktools:tell $nick $host $hand $chan $chan1 notes.12 "$notes_read $notes_total"
  120. }
  121. if {$notes_total == "0"} {
  122. return
  123. }
  124. module:getinfo $nick $host $hand $chan $chan1 $type $data "notesinbox" "0" $number
  125. }
  126. del {
  127. if {[string equal -nocase $number "all"]} {
  128. set delall [notes:delall $hand]
  129. blacktools:tell $nick $host $hand $chan $chan1 notes.15 none
  130. return
  131. }
  132. set status [notes:del $number $hand]
  133. if {$status == "1"} {
  134. blacktools:tell $nick $host $hand $chan $chan1 notes.8 $number
  135. } else { blacktools:tell $nick $host $hand $chan $chan1 notes.4 $number
  136. return
  137. }
  138. }
  139. read {
  140. if {![regexp {^[0-9]} $number]} {
  141. switch $type {
  142. 0 {
  143. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "note"
  144. }
  145. 1 {
  146. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "note"
  147. }
  148. 2 {
  149. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "note"
  150. }
  151. }
  152. return
  153. }
  154. set ret [find:note:num $number]
  155. set found_note 0
  156. if {$ret == 0} {
  157. blacktools:tell $nick $host $hand $chan $chan1 notes.4 $number
  158. return
  159. }
  160. set file [open $black(notes_file) "r"]
  161. while {[gets $file line] != -1} {
  162. set num [lindex [split $line] 2]
  163. set handle [lindex [split $line] 4]
  164. if {[string equal -nocase $handle $hand] && [string equal -nocase $number $num]} {
  165. set found_note 1
  166. set readtype [lindex [split $line] 0]
  167. set time [lindex [split $line] 5]
  168. set time [clock format $time -format %D]
  169. set sender [lindex [split $line] 6]
  170. if {$readtype == "LIST"} {
  171. set readnote [lrange [split $line] 6 end]
  172. } else {
  173. set readnote [lrange [split $line] 7 end]
  174. }
  175. continue
  176. }
  177. }
  178. close $file
  179. if {$found_note == 0} {
  180. blacktools:tell $nick $host $hand $chan $chan1 notes.4 $number
  181. return
  182. }
  183. set split_note [split $readnote "~"]
  184. if {$readtype == "LIST"} {
  185. blacktools:tell $nick $host $hand $chan $chan1 notes.5 "$number $readtype $time"
  186. foreach line $split_note {
  187. set encoded [encoding convertfrom utf-8 $line]
  188. set text [black:color:set $botnick $encoded]
  189. blacktools:tell $nick $host $hand $chan $chan1 notes.6 "[join $text]"
  190. }
  191. } else {
  192. blacktools:tell $nick $host $hand $chan $chan1 notes.7 "$number $readtype $sender $time"
  193. foreach line $split_note {
  194. set encoded [encoding convertfrom utf-8 $line]
  195. set text [black:color:set $botnick $encoded]
  196. blacktools:tell $nick $host $hand $chan $chan1 notes.6 "[join $text]"
  197. }
  198. }
  199. notes:read $number $hand
  200. }
  201. send {
  202. set num_user 0
  203. set sent_user 0
  204. set invalid_user ""
  205. if {$user_send == ""} {
  206. switch $type {
  207. 0 {
  208. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "note"
  209. }
  210. 1 {
  211. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "note"
  212. }
  213. 2 {
  214. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "note"
  215. }
  216. }
  217. return
  218. }
  219. if {[llength [split $note_send]] == "0"} {
  220. switch $type {
  221. 0 {
  222. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "note"
  223. }
  224. 1 {
  225. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "note"
  226. }
  227. 2 {
  228. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "note"
  229. }
  230. }
  231. return
  232. }
  233. set split_usersend [split $user_send ","]
  234. foreach user $split_usersend {
  235. if {[onchan $user $chan] && ![validuser $user]} {
  236. set user [nick2hand $user]
  237. }
  238. set num_user [expr $num_user + 1]
  239. if {[validuser $user]} {
  240. set nonotes [getuser $user XTRA NO_NOTES]
  241. if {$nonotes == ""} {
  242. set sent_user [expr $sent_user + 1]
  243. set black(notes:announce:$user) 1
  244. set return [notes:add $nick $host $user "DB" "INBOX" $note_send $hand 0]
  245. } else {
  246. lappend invalid_user $user\($black(say.$getlang.notes.17)\)
  247. }
  248. } else {
  249. lappend invalid_user $user
  250. }
  251. }
  252. if {$invalid_user == ""} {
  253. blacktools:tell $nick $host $hand $chan $chan1 notes.2 "$sent_user $num_user"
  254. } else {
  255. blacktools:tell $nick $host $hand $chan $chan1 notes.3 "$sent_user $num_user $invalid_user"
  256. }
  257. }
  258. default {
  259. if {$who == ""} {
  260. set notes_read 0
  261. set notes_total 0
  262. set notes_unread ""
  263. array set inboxlist [list]
  264. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  265. set temp "$black(tempdir)/inbox_temp.$timestamp"
  266. set file [open $black(notes_file) "r"]
  267. while {[gets $file line] != -1} {
  268. set handle [lindex [split $line] 4]
  269. set readtype [lindex [split $line] 0]
  270. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  271. set notes_total [expr $notes_total + 1]
  272. set num [lindex [split $line] 2]
  273. set readnote [lrange [split $line] 7 end]
  274. set readit [lindex [split $line] 3]
  275. if {$readit == "0"} {
  276. set notes_read [expr $notes_read + 1]
  277. lappend notes_unread $num
  278. }
  279. }
  280. }
  281. close $file
  282. if {$notes_unread != ""} {
  283. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  284. blacktools:tell $nick $host $hand $chan $chan1 notesinbox.13 "none"
  285. } else {
  286. blacktools:tell $nick $host $hand $chan $chan1 notes.12 "$notes_read $notes_total"
  287. }
  288. return
  289. }
  290. set return [notes:add $nick $host $hand "DB" "LIST" $note "" 0]
  291. blacktools:tell $nick $host $hand $chan $chan1 notes.1 $return
  292. }
  293. }
  294. }
  295. proc notes:del {num hand} {
  296. global black
  297. set found_it 0
  298. set file [open $black(notes_file) "r"]
  299. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  300. set temp "$black(tempdir)/notes_temp.$timestamp"
  301. set tempwrite [open $temp w]
  302. while {[gets $file line] != -1} {
  303. set read_num [lindex [split $line] 2]
  304. set handle [lindex [split $line] 4]
  305. if {[string equal $num $read_num] && [string equal $handle $hand]} {
  306. set found_it 1
  307. continue
  308. } else {
  309. puts $tempwrite $line
  310. }
  311. }
  312. close $tempwrite
  313. close $file
  314. file rename -force $temp $black(notes_file)
  315. return $found_it
  316. }
  317. proc notes:delall {hand} {
  318. global black
  319. set found_it 0
  320. set file [open $black(notes_file) "r"]
  321. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  322. set temp "$black(tempdir)/notes_temp.$timestamp"
  323. set tempwrite [open $temp w]
  324. while {[gets $file line] != -1} {
  325. set type [lindex [split $line] 0]
  326. set handle [lindex [split $line] 4]
  327. if {[string equal $handle $hand] && [string equal $type "INBOX"]} {
  328. set found_it [expr $found_it + 1]
  329. continue
  330. } else {
  331. puts $tempwrite $line
  332. }
  333. }
  334. close $tempwrite
  335. close $file
  336. file rename -force $temp $black(notes_file)
  337. return $found_it
  338. }
  339. proc notes:read {num hand} {
  340. global black
  341. set read_it 0
  342. set file [open $black(notes_file) "r"]
  343. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  344. set temp "$black(tempdir)/notes_temp.$timestamp"
  345. set tempwrite [open $temp w]
  346. while {[gets $file line] != -1} {
  347. set read_num [lindex [split $line] 2]
  348. set handle [lindex [split $line] 4]
  349. set read_type [lindex [split $line] 0]
  350. set readit [lindex [split $line] 3]
  351. if {[string equal $num $read_num] && [string equal $handle $hand] && [string equal -nocase $read_type "INBOX"] && ($readit == "0")} {
  352. set db [lindex [split $line] 1]
  353. set readtype [lindex [split $line] 0]
  354. set readnum [lindex [split $line] 2]
  355. set read_it 1
  356. set time [lindex [split $line] 5]
  357. set readhandle [lindex [split $line] 4]
  358. set readsender [lindex [split $line] 6]
  359. set readnote [join [lrange [split $line] 7 end]]
  360. continue
  361. } else {
  362. puts $tempwrite $line
  363. }
  364. }
  365. close $tempwrite
  366. close $file
  367. file rename -force $temp $black(notes_file)
  368. if {$read_it == "1"} {
  369. set file [open $black(notes_file) "a"]
  370. puts $file "$readtype $db $readnum $read_it $readhandle $time $readsender $readnote"
  371. close $file
  372. }
  373. return $read_it
  374. }
  375. proc notes:add {nick host handle chan type note sender read} {
  376. global black
  377. set type [string toupper $type]
  378. set temp_num 0
  379. set num 0
  380. set time [unixtime]
  381. while {$temp_num == 0} {
  382. set get [find:note:num $num]
  383. if {$get == "$num"} {
  384. set num [expr $num + 1]
  385. } else { set temp_num 1 }
  386. }
  387. set file [open $black(notes_file) a]
  388. set encoded [encoding convertto utf-8 $note]
  389. if {$type == "LIST"} {
  390. puts $file "$type $chan $num $read $handle $time $encoded"
  391. }
  392. if {$type == "INBOX"} {
  393. puts $file "$type $chan $num $read $handle $time $sender $encoded"
  394. }
  395. close $file
  396. return $num
  397. }
  398. proc notes:announce:pub {nick host hand chan arg} {
  399. global black
  400. set found_access 0
  401. set hand [nick2hand $nick]
  402. foreach c [channels] {
  403. if {[matchattr $hand nmo|MAOV $c]} {
  404. set found_access 1
  405. }
  406. }
  407. if {$found_access == "1"} {
  408. if {[info exists black(notes:announce:$hand)]} {
  409. if {$black(notes:announce:$hand) == "0"} {
  410. return
  411. }
  412. } else { return }
  413. set chan1 $chan
  414. set notes_read 0
  415. set notes_total 0
  416. set notes_unread ""
  417. array set inboxlist [list]
  418. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  419. set temp "$black(tempdir)/inbox_temp.$timestamp"
  420. set file [open $black(notes_file) "r"]
  421. while {[gets $file line] != -1} {
  422. set handle [lindex [split $line] 4]
  423. set readtype [lindex [split $line] 0]
  424. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  425. set notes_total [expr $notes_total + 1]
  426. set num [lindex [split $line] 2]
  427. set readnote [join [lrange [split $line] 7 end]]
  428. set readit [lindex [split $line] 3]
  429. if {$readit == "0"} {
  430. set notes_read [expr $notes_read + 1]
  431. lappend notes_unread $num
  432. }
  433. }
  434. }
  435. close $file
  436. if {$notes_unread != ""} {
  437. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  438. blacktools:tell $nick $host $hand $chan $chan1 notes.16 "none"
  439. set black(notes:announce:$hand) 0
  440. }
  441. }
  442. }
  443. proc notes:announce {nick host hand chan} {
  444. global black
  445. set hand [nick2hand $nick]
  446. if {[matchattr $hand nmo|AOVM $chan]} {
  447. if {[info exists black(notes:announce:$hand)]} {
  448. if {$black(notes:announce:$hand) == "0"} {
  449. return
  450. }
  451. } else { return }
  452. set chan1 $chan
  453. set notes_read 0
  454. set notes_total 0
  455. set notes_unread ""
  456. array set inboxlist [list]
  457. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  458. set temp "$black(tempdir)/inbox_temp.$timestamp"
  459. set file [open $black(notes_file) "r"]
  460. while {[gets $file line] != -1} {
  461. set handle [lindex [split $line] 4]
  462. set readtype [lindex [split $line] 0]
  463. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  464. set notes_total [expr $notes_total + 1]
  465. set num [lindex [split $line] 2]
  466. set readnote [lrange [split $line] 7 end]
  467. set readit [lindex [split $line] 3]
  468. if {$readit == "0"} {
  469. set notes_read [expr $notes_read + 1]
  470. lappend notes_unread $num
  471. }
  472. }
  473. }
  474. close $file
  475. if {$notes_unread != ""} {
  476. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  477. blacktools:tell $nick $host $hand $chan $chan1 notes.16 "none"
  478. set black(notes:announce:$hand) 0
  479. }
  480. }
  481. }
  482. ##############
  483. #########################################################################
  484. ## END ##
  485. #########################################################################