BT.Notes.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 ##
  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 {[regexp {:} $sender]} {
  171. set sender [lindex [split $sender ":"] 0]
  172. }
  173. if {$readtype == "LIST"} {
  174. set readnote [lrange [split $line] 6 end]
  175. } else {
  176. set readnote [lrange [split $line] 7 end]
  177. }
  178. continue
  179. }
  180. }
  181. close $file
  182. if {$found_note == 0} {
  183. blacktools:tell $nick $host $hand $chan $chan1 notes.4 $number
  184. return
  185. }
  186. set split_note [split $readnote "~"]
  187. if {$readtype == "LIST"} {
  188. blacktools:tell $nick $host $hand $chan $chan1 notes.5 "$number $readtype $time"
  189. foreach line $split_note {
  190. set encoded [encoding convertfrom utf-8 $line]
  191. set text [black:color:set $botnick $encoded]
  192. blacktools:tell $nick $host $hand $chan $chan1 notes.6 "[join $text]"
  193. }
  194. } else {
  195. blacktools:tell $nick $host $hand $chan $chan1 notes.7 "$number $readtype $sender $time"
  196. foreach line $split_note {
  197. set encoded [encoding convertfrom utf-8 $line]
  198. set text [black:color:set $botnick $encoded]
  199. blacktools:tell $nick $host $hand $chan $chan1 notes.6 "[join $text]"
  200. }
  201. }
  202. notes:read $number $hand
  203. }
  204. send {
  205. set num_user 0
  206. set sent_user 0
  207. set invalid_user ""
  208. if {$user_send == ""} {
  209. switch $type {
  210. 0 {
  211. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "note"
  212. }
  213. 1 {
  214. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "note"
  215. }
  216. 2 {
  217. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "note"
  218. }
  219. }
  220. return
  221. }
  222. if {[llength [split $note_send]] == "0"} {
  223. switch $type {
  224. 0 {
  225. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "note"
  226. }
  227. 1 {
  228. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "note"
  229. }
  230. 2 {
  231. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "note"
  232. }
  233. }
  234. return
  235. }
  236. set split_usersend [split $user_send ","]
  237. foreach user $split_usersend {
  238. if {[onchan $user $chan] && ![validuser $user]} {
  239. set user [nick2hand $user]
  240. }
  241. set num_user [expr $num_user + 1]
  242. if {[validuser $user]} {
  243. set nonotes [getuser $user XTRA NO_NOTES]
  244. if {$nonotes == ""} {
  245. set sent_user [expr $sent_user + 1]
  246. set black(notes:announce:$user) 1
  247. set return [notes:add $nick $host $user "DB" "INBOX" $note_send $hand 0]
  248. } else {
  249. lappend invalid_user $user\($black(say.$getlang.notes.17)\)
  250. }
  251. } else {
  252. lappend invalid_user $user
  253. }
  254. }
  255. if {$invalid_user == ""} {
  256. blacktools:tell $nick $host $hand $chan $chan1 notes.2 "$sent_user $num_user"
  257. } else {
  258. blacktools:tell $nick $host $hand $chan $chan1 notes.3 "$sent_user $num_user $invalid_user"
  259. }
  260. }
  261. default {
  262. if {$who == ""} {
  263. set notes_read 0
  264. set notes_total 0
  265. set notes_unread ""
  266. array set inboxlist [list]
  267. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  268. set temp "$black(tempdir)/inbox_temp.$timestamp"
  269. set file [open $black(notes_file) "r"]
  270. while {[gets $file line] != -1} {
  271. set handle [lindex [split $line] 4]
  272. set readtype [lindex [split $line] 0]
  273. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  274. set notes_total [expr $notes_total + 1]
  275. set num [lindex [split $line] 2]
  276. set readnote [lrange [split $line] 7 end]
  277. set readit [lindex [split $line] 3]
  278. if {$readit == "0"} {
  279. set notes_read [expr $notes_read + 1]
  280. lappend notes_unread $num
  281. }
  282. }
  283. }
  284. close $file
  285. if {$notes_unread != ""} {
  286. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  287. blacktools:tell $nick $host $hand $chan $chan1 notesinbox.13 "none"
  288. } else {
  289. blacktools:tell $nick $host $hand $chan $chan1 notes.12 "$notes_read $notes_total"
  290. }
  291. return
  292. }
  293. set return [notes:add $nick $host $hand "DB" "LIST" $note "" 0]
  294. blacktools:tell $nick $host $hand $chan $chan1 notes.1 $return
  295. }
  296. }
  297. }
  298. proc notes:del {num hand} {
  299. global black
  300. set found_it 0
  301. set file [open $black(notes_file) "r"]
  302. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  303. set temp "$black(tempdir)/notes_temp.$timestamp"
  304. set tempwrite [open $temp w]
  305. while {[gets $file line] != -1} {
  306. set read_num [lindex [split $line] 2]
  307. set handle [lindex [split $line] 4]
  308. if {[string equal $num $read_num] && [string equal $handle $hand]} {
  309. set found_it 1
  310. continue
  311. } else {
  312. puts $tempwrite $line
  313. }
  314. }
  315. close $tempwrite
  316. close $file
  317. file rename -force $temp $black(notes_file)
  318. return $found_it
  319. }
  320. proc notes:delall {hand} {
  321. global black
  322. set found_it 0
  323. set file [open $black(notes_file) "r"]
  324. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  325. set temp "$black(tempdir)/notes_temp.$timestamp"
  326. set tempwrite [open $temp w]
  327. while {[gets $file line] != -1} {
  328. set type [lindex [split $line] 0]
  329. set handle [lindex [split $line] 4]
  330. if {[string equal $handle $hand] && [string equal $type "INBOX"]} {
  331. set found_it [expr $found_it + 1]
  332. continue
  333. } else {
  334. puts $tempwrite $line
  335. }
  336. }
  337. close $tempwrite
  338. close $file
  339. file rename -force $temp $black(notes_file)
  340. return $found_it
  341. }
  342. proc notes:read {num hand} {
  343. global black
  344. set read_it 0
  345. set file [open $black(notes_file) "r"]
  346. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  347. set temp "$black(tempdir)/notes_temp.$timestamp"
  348. set tempwrite [open $temp w]
  349. while {[gets $file line] != -1} {
  350. set read_num [lindex [split $line] 2]
  351. set handle [lindex [split $line] 4]
  352. set read_type [lindex [split $line] 0]
  353. set readit [lindex [split $line] 3]
  354. if {[string equal $num $read_num] && [string equal $handle $hand] && [string equal -nocase $read_type "INBOX"] && ($readit == "0")} {
  355. set db [lindex [split $line] 1]
  356. set readtype [lindex [split $line] 0]
  357. set readnum [lindex [split $line] 2]
  358. set read_it 1
  359. set time [lindex [split $line] 5]
  360. set readhandle [lindex [split $line] 4]
  361. set readsender [lindex [split $line] 6]
  362. set readnote [join [lrange [split $line] 7 end]]
  363. continue
  364. } else {
  365. puts $tempwrite $line
  366. }
  367. }
  368. close $tempwrite
  369. close $file
  370. file rename -force $temp $black(notes_file)
  371. if {$read_it == "1"} {
  372. set file [open $black(notes_file) "a"]
  373. puts $file "$readtype $db $readnum $read_it $readhandle $time $readsender $readnote"
  374. close $file
  375. }
  376. return $read_it
  377. }
  378. proc notes:add {nick host handle chan type note sender read} {
  379. global black
  380. set type [string toupper $type]
  381. set temp_num 0
  382. set num 0
  383. set time [unixtime]
  384. while {$temp_num == 0} {
  385. set get [find:note:num $num]
  386. if {$get == "$num"} {
  387. set num [expr $num + 1]
  388. } else { set temp_num 1 }
  389. }
  390. set file [open $black(notes_file) a]
  391. set encoded [encoding convertto utf-8 $note]
  392. if {$type == "LIST"} {
  393. puts $file "$type $chan $num $read $handle $time $encoded"
  394. }
  395. if {$type == "INBOX"} {
  396. puts $file "$type $chan $num $read $handle $time $sender $encoded"
  397. }
  398. close $file
  399. return $num
  400. }
  401. proc notes:announce:pub {nick host hand chan arg} {
  402. global black
  403. set found_access 0
  404. set hand [nick2hand $nick]
  405. foreach c [channels] {
  406. if {[matchattr $hand nmo|MAOV $c]} {
  407. set found_access 1
  408. }
  409. }
  410. if {$found_access == "1"} {
  411. if {[info exists black(notes:announce:$hand)]} {
  412. if {$black(notes:announce:$hand) == "0"} {
  413. return
  414. }
  415. } else { return }
  416. set chan1 $chan
  417. set notes_read 0
  418. set notes_total 0
  419. set notes_unread ""
  420. array set inboxlist [list]
  421. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  422. set temp "$black(tempdir)/inbox_temp.$timestamp"
  423. set file [open $black(notes_file) "r"]
  424. while {[gets $file line] != -1} {
  425. set handle [lindex [split $line] 4]
  426. set readtype [lindex [split $line] 0]
  427. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  428. set notes_total [expr $notes_total + 1]
  429. set num [lindex [split $line] 2]
  430. set readnote [join [lrange [split $line] 7 end]]
  431. set readit [lindex [split $line] 3]
  432. if {$readit == "0"} {
  433. set notes_read [expr $notes_read + 1]
  434. lappend notes_unread $num
  435. }
  436. }
  437. }
  438. close $file
  439. if {$notes_unread != ""} {
  440. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  441. blacktools:tell $nick $host $hand $chan $chan1 notes.16 "none"
  442. set black(notes:announce:$hand) 0
  443. }
  444. }
  445. }
  446. proc notes:announce {nick host hand chan} {
  447. global black
  448. set hand [nick2hand $nick]
  449. if {[matchattr $hand nmo|AOVM $chan]} {
  450. if {[info exists black(notes:announce:$hand)]} {
  451. if {$black(notes:announce:$hand) == "0"} {
  452. return
  453. }
  454. } else { return }
  455. set chan1 $chan
  456. set notes_read 0
  457. set notes_total 0
  458. set notes_unread ""
  459. array set inboxlist [list]
  460. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  461. set temp "$black(tempdir)/inbox_temp.$timestamp"
  462. set file [open $black(notes_file) "r"]
  463. while {[gets $file line] != -1} {
  464. set handle [lindex [split $line] 4]
  465. set readtype [lindex [split $line] 0]
  466. if {[string equal -nocase $handle $hand] && [string equal -nocase $readtype "INBOX"]} {
  467. set notes_total [expr $notes_total + 1]
  468. set num [lindex [split $line] 2]
  469. set readnote [lrange [split $line] 7 end]
  470. set readit [lindex [split $line] 3]
  471. if {$readit == "0"} {
  472. set notes_read [expr $notes_read + 1]
  473. lappend notes_unread $num
  474. }
  475. }
  476. }
  477. close $file
  478. if {$notes_unread != ""} {
  479. blacktools:tell $nick $host $hand $chan $chan1 notes.11 "$notes_read $notes_total $notes_unread"
  480. blacktools:tell $nick $host $hand $chan $chan1 notes.16 "none"
  481. set black(notes:announce:$hand) 0
  482. }
  483. }
  484. }
  485. ##############
  486. #########################################################################
  487. ## END ##
  488. #########################################################################