BT.Notes.tcl 14 KB

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