Index: openacs-4/packages/xotcl-core/tcl/chat-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xotcl-core/tcl/Attic/chat-procs.tcl,v diff -u -N -r1.27 -r1.28 --- openacs-4/packages/xotcl-core/tcl/chat-procs.tcl 17 Oct 2017 13:46:30 -0000 1.27 +++ openacs-4/packages/xotcl-core/tcl/chat-procs.tcl 21 Oct 2017 13:07:27 -0000 1.28 @@ -22,226 +22,214 @@ } Chat instproc init {} { - my instvar array - # my log "-- " - my set now [clock clicks -milliseconds] - if {![my exists user_id]} { - my set user_id [ad_conn user_id] + # :log "-- " + set :now [clock clicks -milliseconds] + if {![info exists :user_id]} { + set :user_id [ad_conn user_id] } - if {![my exists session_id]} { - my set session_id [ad_conn session_id] + if {![info exists :session_id]} { + set :session_id [ad_conn session_id] } - set cls [my info class] - set array $cls-[my set chat_id] + set cls [:info class] + set :array $cls-${:chat_id} if {![nsv_exists $cls initialized]} { - my log "-- initialize $cls" + :log "-- initialize $cls" $cls initialize_nsvs ::xo::clusterwide nsv_set $cls initialized \ [ad_schedule_proc \ - -thread "t" [my sweepinterval] $cls sweep_all_chats] + -thread "t" [:sweepinterval] $cls sweep_all_chats] } - if {![nsv_exists $array-seen newest]} { - ::xo::clusterwide nsv_set $array-seen newest 0 + if {![nsv_exists ${:array}-seen newest]} { + ::xo::clusterwide nsv_set ${:array}-seen newest 0 } - if {![nsv_exists $array-color idx]} { - ::xo::clusterwide nsv_set $array-color idx 0 + if {![nsv_exists ${:array}-color idx]} { + ::xo::clusterwide nsv_set ${:array}-color idx 0 } - if {[my user_id] != 0 || [my session_id] != 0} { - my init_user_color + if {[:user_id] != 0 || [:session_id] != 0} { + :init_user_color } } Chat instproc register_nsvs {msg_id user_id msg color secs} { - my instvar array now # Tell the system we are back again, in case we were auto logged out - if { ![nsv_exists $array-login $user_id] } { - ::xo::clusterwide nsv_set $array-login $user_id [clock seconds] + if { ![nsv_exists ${:array}-login $user_id] } { + ::xo::clusterwide nsv_set ${:array}-login $user_id [clock seconds] } - ::xo::clusterwide nsv_set $array $msg_id [list $now $secs $user_id $msg $color] - ::xo::clusterwide nsv_set $array-seen newest $now - ::xo::clusterwide nsv_set $array-seen last $secs - ::xo::clusterwide nsv_set $array-last-activity $user_id $now + ::xo::clusterwide nsv_set ${:array} $msg_id [list ${:now} $secs $user_id $msg $color] + ::xo::clusterwide nsv_set ${:array}-seen newest ${:now} + ::xo::clusterwide nsv_set ${:array}-seen last $secs + ::xo::clusterwide nsv_set ${:array}-last-activity $user_id ${:now} } Chat instproc add_msg {{-get_new:boolean true} {-uid ""} msg} { - # my log "--chat adding $msg" - my instvar array now - set user_id [expr {$uid ne "" ? $uid : [my set user_id]}] - set color [my user_color $user_id] + # :log "--chat adding $msg" + set user_id [expr {$uid ne "" ? $uid : ${:user_id}}] + set color [:user_color $user_id] set msg [ns_quotehtml $msg] - # my log "-- msg=$msg" + # :log "-- msg=$msg" if {$get_new && [info commands ::thread::mutex] ne "" && [info commands ::bgdelivery] ne ""} { # we could use the streaming interface - my broadcast_msg [Message new -volatile -time [clock seconds] \ + :broadcast_msg [Message new -volatile -time [clock seconds] \ -user_id $user_id -color $color [list -msg $msg]] } - my register_nsvs $now.$user_id $user_id $msg $color [clock seconds] + :register_nsvs ${:now}.$user_id $user_id $msg $color [clock seconds] # this in any case a valid result, but only needed for the polling interface if {$get_new} {my get_new} } Chat instproc current_message_valid {} { - expr { [my exists user_id] && [my set user_id] != -1 } + expr { [info exists :user_id] && ${:user_id} != -1 } } Chat instproc active_user_list {} { - nsv_array get [my set array]-login + nsv_array get ${:array}-login } Chat instproc nr_active_users {} { - expr { [llength [nsv_array get [my set array]-login]] / 2 } + expr { [llength [nsv_array get ${:array}-login]] / 2 } } Chat instproc last_activity {} { - if { ![nsv_exists [my set array]-seen last] } { return "-" } - return [clock format [nsv_get [my set array]-seen last] -format "%d.%m.%y %H:%M:%S"] + if { ![nsv_exists ${:array}-seen last] } { return "-" } + return [clock format [nsv_get ${:array}-seen last] -format "%d.%m.%y %H:%M:%S"] } Chat instproc check_age {key ago} { - my instvar array timewindow - if {$ago > $timewindow} { - ::xo::clusterwide nsv_unset $array $key + if {$ago > ${:timewindow}} { + ::xo::clusterwide nsv_unset ${:array} $key #my log "--c unsetting $key" return 0 } return 1 } Chat instproc get_new {} { - my instvar array now session_id - set last [expr {[nsv_exists $array-seen $session_id] ? [nsv_get $array-seen $session_id] : 0}] - if {[nsv_get $array-seen newest]>$last} { - #my log "--c must check $session_id: [nsv_get $array-seen newest] > $last" - foreach {key value} [nsv_array get $array] { + set last [expr {[nsv_exists ${:array}-seen ${:session_id}] ? [nsv_get ${:array}-seen ${:session_id}] : 0}] + if {[nsv_get ${:array}-seen newest]>$last} { + #my log "--c must check ${:session_id}: [nsv_get ${:array}-seen newest] > $last" + foreach {key value} [nsv_array get ${:array}] { lassign $value timestamp secs user msg color if {$timestamp > $last} { # # add the message to the ordered composite. # - my add [Message new -time $secs -user_id $user -msg $msg -color $color] + :add [Message new -time $secs -user_id $user -msg $msg -color $color] } else { - my check_age $key [expr {($now - $timestamp) / 1000}] + :check_age $key [expr {(${:now} - $timestamp) / 1000}] } } - ::xo::clusterwide nsv_set $array-seen $session_id $now - #my log "--c setting session_id $session_id: $now" + ::xo::clusterwide nsv_set ${:array}-seen ${:session_id} ${:now} + #my log "--c setting session_id ${:session_id}: ${:now}" } else { - #my log "--c nothing new for $session_id" + #my log "--c nothing new for ${:session_id}" } - my render + :render } Chat instproc get_all {} { - my instvar array now session_id - foreach {key value} [nsv_array get $array] { + foreach {key value} [nsv_array get ${:array}] { lassign $value timestamp secs user msg color - if {[my check_age $key [expr {($now - $timestamp) / 1000}]]} { - my add [Message new -time $secs -user_id $user -msg $msg -color $color] + if {[:check_age $key [expr {(${:now} - $timestamp) / 1000}]]} { + :add [Message new -time $secs -user_id $user -msg $msg -color $color] } } - #my log "--c setting session_id $session_id: $now" - ::xo::clusterwide nsv_set $array-seen $session_id $now - my render + #my log "--c setting session_id ${:session_id}: ${:now}" + ::xo::clusterwide nsv_set ${:array}-seen ${:session_id} ${:now} + :render } Chat instproc sweeper {} { - my instvar array now logout_messages_p - my log "--core-chat starting" - foreach {user timestamp} [nsv_array get $array-last-activity] { + :log "--core-chat starting" + foreach {user timestamp} [nsv_array get ${:array}-last-activity] { ns_log Notice "--core-chat at user $user with $timestamp" - set ago [expr {($now - $timestamp) / 1000}] - ns_log Notice "--core-chat Checking: now=$now, timestamp=$timestamp, ago=$ago" + set ago [expr {(${:now} - $timestamp) / 1000}] + ns_log Notice "--core-chat Checking: now=${:now}, timestamp=$timestamp, ago=$ago" # was 1200 if {$ago > 300} { - my logout -user_id $user -msg "auto logout" - catch {::bgdelivery do ::Subscriber sweep chat-[my chat_id]} + :logout -user_id $user -msg "auto logout" + catch {::bgdelivery do ::Subscriber sweep chat-[:chat_id]} } } - my log "-- ending" + :log "-- ending" } Chat instproc logout {{-user_id ""} {-msg ""}} { - set user_id [expr {$user_id ne "" ? $user_id : [my set user_id]}] + set user_id [expr {$user_id ne "" ? $user_id : ${:user_id}}] ns_log Notice "--core-chat User $user_id logging out of chat" - if {[my set logout_messages_p]} { + if {${:logout_messages_p}} { if {$msg eq ""} {set msg [_ chat.has_left_the_room].} - my add_msg -get_new false $msg + :add_msg -get_new false $msg } - my instvar array # This values could already not be here. Just ignore when we don't # find them catch { - ::xo::clusterwide nsv_unset -nocomplain $array-login $user_id + ::xo::clusterwide nsv_unset -nocomplain ${:array}-login $user_id } catch { - ::xo::clusterwide nsv_unset -nocomplain $array-color $user_id + ::xo::clusterwide nsv_unset -nocomplain ${:array}-color $user_id } catch { - ::xo::clusterwide nsv_unset -nocomplain $array-last-activity $user_id + ::xo::clusterwide nsv_unset -nocomplain ${:array}-last-activity $user_id } } Chat instproc init_user_color {} { - my instvar array user_id - if { [nsv_exists $array-color $user_id] } { + if { [nsv_exists ${:array}-color ${:user_id}] } { return } else { - set colors [::xo::parameter get -parameter UserColors -default [[my info class] set colors]] - # ns_log notice "getting colors of [my info class] = [info exists colors]" - set color [lindex $colors [expr { [nsv_get $array-color idx] % [llength $colors] }]] - ::xo::clusterwide nsv_set $array-color $user_id $color - ::xo::clusterwide nsv_incr $array-color idx + set colors [::xo::parameter get -parameter UserColors -default [[:info class] set colors]] + # ns_log notice "getting colors of [:info class] = [info exists colors]" + set color [lindex $colors [expr { [nsv_get ${:array}-color idx] % [llength $colors] }]] + ::xo::clusterwide nsv_set ${:array}-color ${:user_id} $color + ::xo::clusterwide nsv_incr ${:array}-color idx } } Chat instproc get_users {} { set output "" - foreach {user_id timestamp} [my active_user_list] { + foreach {user_id timestamp} [:active_user_list] { if {$user_id > 0} { set diff [clock format [expr {[clock seconds] - $timestamp}] -format "%H:%M:%S" -gmt 1] - set userlink [my user_link -user_id $user_id] + set userlink [:user_link -user_id $user_id] append output "$userlink$diff\n" } } return $output } Chat instproc user_active {user_id} { - my instvar array # was the user already active? - my log "--chat login already avtive? [nsv_exists $array-last-activity $user_id]" - return [nsv_exists $array-last-activity $user_id] + :log "--chat login already avtive? [nsv_exists ${:array}-last-activity $user_id]" + return [nsv_exists ${:array}-last-activity $user_id] } Chat instproc login {} { - my log "--chat login" - my instvar array user_id now - if {[my set login_messages_p] && ![my user_active $user_id]} { - my add_msg -get_new false [_ xotcl-core.has_entered_the_room] - } elseif {![nsv_exists $array-login $user_id]} { + :log "--chat login" + if {${:login_messages_p} && ![:user_active ${:user_id}]} { + :add_msg -get_new false [_ xotcl-core.has_entered_the_room] + } elseif {![nsv_exists ${:array}-login ${:user_id}]} { # give some proof of our presence to the chat system when we # don't issue the login message - ::xo::clusterwide nsv_set $array-login $user_id [clock seconds] - ::xo::clusterwide nsv_set $array-last-activity $user_id $now + ::xo::clusterwide nsv_set ${:array}-login ${:user_id} [clock seconds] + ::xo::clusterwide nsv_set ${:array}-last-activity ${:user_id} ${:now} } - my encoder noencode - my log "--c setting session_id [my set session_id]: $now" - return [my get_all] + :encoder noencode + :log "--c setting session_id ${:session_id}: ${:now}" + return [:get_all] } Chat instproc user_color { user_id } { - my instvar array - if { ![nsv_exists $array-color $user_id] } { - my log "warning: Cannot find user color for chat ($array-color $user_id)!" - return [lindex [[my info class] set colors] 0] + if { ![nsv_exists ${:array}-color $user_id] } { + :log "warning: Cannot find user color for chat (${:array}-color $user_id)!" + return [lindex [[:info class] set colors] 0] } - return [nsv_get $array-color $user_id] + return [nsv_get ${:array}-color $user_id] } Chat instproc user_name { user_id } { @@ -251,81 +239,81 @@ Chat instproc user_link { -user_id -color } { if {$user_id > 0} { - set name [my user_name $user_id] + set name [:user_name $user_id] set url "/shared/community-member?user%5fid=$user_id" if {![info exists color]} { - set color [my user_color $user_id] + set color [:user_color $user_id] } set creator "$name" } elseif { $user_id == 0 } { set creator "Nobody" } else { set creator "System" } - return [my encode $creator] + return [:encode $creator] } Chat instproc urlencode {string} {ns_urlencode $string} Chat instproc noencode {string} {set string} - Chat instproc encode {string} {my [my encoder] $string} + Chat instproc encode {string} {my [:encoder] $string} Chat instproc json_encode {string} { string map [list \n \\n \" \\\" ' {\'}] $string } Chat instproc json_encode_msg {msg} { - set old [my encoder] - my encoder noencode ;# just for user_link - set userlink [my user_link -user_id [$msg user_id] -color [$msg color]] - my encoder $old + set old [:encoder] + :encoder noencode ;# just for user_link + set userlink [:user_link -user_id [$msg user_id] -color [$msg color]] + :encoder $old set timeshort [clock format [$msg time] -format {[%H:%M:%S]}] - set text [my json_encode [$msg msg]] - foreach var {userlink timeshort} {set $var [my json_encode [set $var]]} + set text [:json_encode [$msg msg]] + foreach var {userlink timeshort} {set $var [:json_encode [set $var]]} return [subst -nocommands {{'messages': [ {'user':'$userlink', 'time': '$timeshort', 'msg':'$text'} ]\n} }] } Chat instproc js_encode_msg {msg} { - set json [my json_encode_msg $msg] + set json [:json_encode_msg $msg] return "\n" } Chat instproc broadcast_msg {msg} { - my log "--chat broadcast_msg" + :log "--chat broadcast_msg" ::xo::clusterwide \ - bgdelivery send_to_subscriber chat-[my chat_id] [my json_encode_msg $msg] + bgdelivery send_to_subscriber chat-[:chat_id] [:json_encode_msg $msg] } Chat instproc subscribe {-uid} { - set user_id [expr {[info exists uid] ? $uid : [my set user_id]}] - set color [my user_color $user_id] - bgdelivery subscribe chat-[my chat_id] "" [my mode] - if {[my set login_messages_p] && ![my user_active $user_id]} { - my broadcast_msg [Message new -volatile -time [clock seconds] \ + set user_id [expr {[info exists uid] ? $uid : ${:user_id}}] + set color [:user_color $user_id] + bgdelivery subscribe chat-[:chat_id] "" [:mode] + if {${:login_messages_p} && ![:user_active $user_id]} { + :broadcast_msg [Message new -volatile -time [clock seconds] \ -user_id $user_id -color $color \ -msg [_ xotcl-core.has_entered_the_room] ] } #my get_all } Chat instproc render {} { - my orderby time + :orderby time set result "
\n" - foreach child [my children] { + foreach child [:children] { set msg [$child msg] set user_id [$child user_id] set color [$child color] set timelong [clock format [$child time]] set timeshort [clock format [$child time] -format {[%H:%M:%S]}] - set userlink [my user_link -user_id $user_id -color $color] - ns_log notice "encode <$msg> using encoder [my encoder] gives <[my encode $msg]>" + set userlink [:user_link -user_id $user_id -color $color] + ns_log notice "encode <$msg> using encoder [:encoder] gives <[:encode $msg]>" append result "

$timeshort " \ "$userlink " \ - "[my encode $msg]

\n" + "[:encode $msg]

\n" } append result "
" return $result @@ -336,14 +324,14 @@ ############################################################################ Class create ChatClass -superclass ::xotcl::Class ChatClass method sweep_all_chats {} { - my log "-- starting" + :log "-- starting" foreach nsv [nsv_names "[self]-*-seen"] { if { [regexp "[self]-(\[0-9\]+)-seen" $nsv _ chat_id] } { - my log "--Chat_id $chat_id" - my new -volatile -chat_id $chat_id -user_id 0 -session_id 0 -init -sweeper + :log "--Chat_id $chat_id" + :new -volatile -chat_id $chat_id -user_id 0 -session_id 0 -init -sweeper } } - my log "-- ending" + :log "-- ending" } ChatClass method initialize_nsvs {} { @@ -366,7 +354,7 @@ ChatClass method init {} { # default setting is set19 from http://www.graphviz.org/doc/info/colors.html # per parameter settings in the chat package are available (param UserColors) - my set colors [list #1b9e77 #d95f02 #7570b3 #e7298a #66a61e #e6ab02 #a6761d #666666] + set :colors [list #1b9e77 #d95f02 #7570b3 #e7298a #66a61e #e6ab02 #a6761d #666666] } }