BT.Notes.tcl 15 KB

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