1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
# presence.tcl --
#
# This file is a part of the Tkabber XMPP client. It implements
# general routines for working with presence (receiving presence,
# sending presence).
array set long_statusdesc [list \
available [::msgcat::mc "is available"] \
chat [::msgcat::mc "is free to chat"] \
away [::msgcat::mc "is away"] \
xa [::msgcat::mc "is extended away"] \
dnd [::msgcat::mc "doesn't want to be disturbed"] \
invisible [::msgcat::mc "is invisible"] \
unavailable [::msgcat::mc "is unavailable"]]
proc get_long_status_desc {status} {
set ::long_statusdesc($status)
}
###############################################################################
proc client:presence {xlib from type x args} {
global presence
global processed_presence
debugmsg presence "PRESENCE: $from; $type; $x; $args"
set from [::xmpp::jid::normalize $from]
switch -- $type {
error -
unavailable {
catch { unset presence(type,$xlib,$from) }
catch { unset presence(status,$xlib,$from) }
catch { unset presence(priority,$xlib,$from) }
catch { unset presence(show,$xlib,$from) }
catch { unset presence(x,$xlib,$from) }
catch { unset presence(error,$xlib,$from) }
set user [::xmpp::jid::stripResource $from]
if {[info exists presence(user_jids,$xlib,$user)]} {
set idx [lsearch -exact $presence(user_jids,$xlib,$user) $from]
set presence(user_jids,$xlib,$user) \
[lreplace $presence(user_jids,$xlib,$user) $idx $idx]
}
cache_preferred_jid_on_unavailable $xlib $from $user
cache_user_status $xlib $user
foreach {attr val} $args {
switch -- $attr {
-status {
set presence(status,$xlib,$from) $val
if {[get_user_status $xlib $user] == "unavailable"} {
set presence(status,$xlib,$user) $val
}
}
-error {
set presence(error,$xlib,$from) $val
}
-lang {
set presence(lang,$xlib,$from) $val
}
}
}
debugmsg presence "$xlib $from unavailable"
}
subscribe {}
subscribed {}
unsubscribe {}
unsubscribed {}
probe {}
default {
set type available
set presence(type,$xlib,$from) available
set presence(status,$xlib,$from) ""
set presence(priority,$xlib,$from) 0
set presence(show,$xlib,$from) available
set presence(x,$xlib,$from) $x
catch { unset presence(error,$xlib,$from) }
foreach {attr val} $args {
switch -- $attr {
-status {set presence(status,$xlib,$from) $val}
-priority {set presence(priority,$xlib,$from) $val}
-show {set presence(show,$xlib,$from) $val}
-lang {set presence(lang,$xlib,$from) $val}
}
}
set presence(show,$xlib,$from) \
[normalize_show $presence(show,$xlib,$from)]
set user [::xmpp::jid::stripResource $from]
if {![info exists presence(user_jids,$xlib,$user)] || \
$from ni $presence(user_jids,$xlib,$user)} {
lappend presence(user_jids,$xlib,$user) $from
}
cache_preferred_jid_on_available $xlib $from $user
cache_user_status $xlib $user
}
}
eval {hook::run client_presence_hook $xlib $from $type $x} $args
}
###############################################################################
proc get_jids_of_user {xlib user} {
global presence
if {[info exists presence(user_jids,$xlib,$user)]} {
return $presence(user_jids,$xlib,$user)
} elseif {[::xmpp::jid::resource $user] ne ""} {
if {[info exists presence(type,$xlib,$user)]} {
return [list $user]
}
}
return {}
}
proc get_jid_of_user {xlib user} {
global presence
if {[info exists presence(preferred_jid,$xlib,$user)]} {
return $presence(preferred_jid,$xlib,$user)
} else {
return $user
}
}
proc cache_preferred_jid_on_available {xlib jid user} {
global presence
set pri $presence(priority,$xlib,$jid)
if {![info exists presence(maxpriority,$xlib,$user)]} {
cache_preferred_jid $xlib $user
return
} elseif {$presence(preferred_jid,$xlib,$user) == $jid && \
$pri < $presence(maxpriority,$xlib,$user)} {
unset presence(preferred_jid,$xlib,$user)
unset presence(maxpriority,$xlib,$user)
cache_preferred_jid $xlib $user
return
} else {
set maxpri $presence(maxpriority,$xlib,$user)
if {$pri > $maxpri} {
set presence(maxpriority,$xlib,$user) $pri
set presence(preferred_jid,$xlib,$user) $jid
}
}
}
proc cache_preferred_jid_on_unavailable {xlib jid user} {
global presence
if {![info exists presence(maxpriority,$xlib,$user)]} {
cache_preferred_jid $xlib $user
return
}
if {$presence(preferred_jid,$xlib,$user) == $jid} {
unset presence(preferred_jid,$xlib,$user)
unset presence(maxpriority,$xlib,$user)
cache_preferred_jid $xlib $user
}
}
proc cache_preferred_jid {xlib user} {
global presence
set jids [get_jids_of_user $xlib $user]
if {$jids != {}} {
set rjid [lindex $jids 0]
set pri $presence(priority,$xlib,$rjid)
foreach jid $jids {
if {$presence(priority,$xlib,$jid) > $pri} {
set pri $presence(priority,$xlib,$jid)
set rjid $jid
}
}
set presence(maxpriority,$xlib,$user) $pri
set presence(preferred_jid,$xlib,$user) $rjid
}
}
proc get_jid_status {xlib jid} {
global presence
set j $jid
if {[info exists presence(show,$xlib,$j)]} {
return $presence(show,$xlib,$j)
} else {
return unavailable
}
}
proc get_jid_presence_info {param xlib jid} {
global presence
if {[info exists presence($param,$xlib,$jid)]} {
return $presence($param,$xlib,$jid)
} else {
return ""
}
}
proc get_user_status {xlib user} {
global presence
if {[info exists presence(cachedstatus,$xlib,$user)]} {
return $presence(cachedstatus,$xlib,$user)
} elseif {[info exists presence(show,$xlib,$user)]} {
return $presence(show,$xlib,$user)
} else {
return unavailable
}
}
proc cache_user_status {xlib user} {
global presence
set jid [get_jid_of_user $xlib $user]
if {[info exists presence(show,$xlib,$jid)]} {
set presence(cachedstatus,$xlib,$user) $presence(show,$xlib,$jid)
} else {
set presence(cachedstatus,$xlib,$user) unavailable
}
}
proc get_user_status_desc {xlib user} {
global presence
set jid [get_jid_of_user $xlib $user]
if {[info exists presence(error,$xlib,$jid)]} {
return [::xmpp::stanzaerror::message $presence(error,$xlib,$jid)]
} elseif {[info exists presence(status,$xlib,$jid)]} {
return $presence(status,$xlib,$jid)
} else {
return ""
}
}
array set status_priority {
unavailable 1
xa 2
away 3
dnd 4
available 5
chat 6
}
proc compare_status {s1 s2} {
global status_priority
set p1 $status_priority($s1)
set p2 $status_priority($s2)
if {$p1 > $p2} {
return 1
} elseif {$p1 == $p2} {
return 0
} else {
return -1
}
}
proc max_status {s1 s2} {
global status_priority
set p1 $status_priority($s1)
set p2 $status_priority($s2)
if {$p1 >= $p2} {
return $s1
} else {
return $s2
}
}
###############################################################################
set curpriority 0
set curuserstatus unavailable
set curtextstatus ""
custom::defvar userpriority 0 [::msgcat::mc "Stored user priority."] \
-type integer -group Hidden
custom::defvar userstatus available [::msgcat::mc "Stored user status."] \
-type string -group Hidden
custom::defvar textstatus "" [::msgcat::mc "Stored user text status."] \
-type string -group Hidden
set userstatusdesc [::msgcat::mc "Not logged in"]
set statusdesc(available) [::msgcat::mc "Available"]
set statusdesc(chat) [::msgcat::mc "Free to chat"]
set statusdesc(away) [::msgcat::mc "Away"]
set statusdesc(xa) [::msgcat::mc "Extended away"]
set statusdesc(dnd) [::msgcat::mc "Do not disturb"]
set statusdesc(invisible) [::msgcat::mc "Invisible"]
set statusdesc(unavailable) [::msgcat::mc "Unavailable"]
###############################################################################
proc change_priority_dialog {} {
global tmppriority
global userpriority
set tmppriority $userpriority
set w .change_priority
if {[winfo exists $w]} {
focus -force $w
return
}
Dialog $w -title [::msgcat::mc "Change Presence Priority"] \
-modal none \
-anchor e \
-default 0 \
-cancel 1
$w add -text [::msgcat::mc "OK"] \
-command [list do_change_priority $w]
$w add -text [::msgcat::mc "Cancel"] -command [list destroy $w]
set f [$w getframe]
Label $f.lpriority -text [::msgcat::mc "Priority:"]
Spinbox $f.priority -1000 1000 1 tmppriority
grid $f.lpriority -row 0 -column 0 -sticky e
grid $f.priority -row 0 -column 1 -sticky ew
grid columnconfigure $f 0 -weight 1
grid columnconfigure $f 1 -weight 1
$w draw
}
###############################################################################
proc do_change_priority {w} {
global userstatus
global tmppriority
global userpriority
destroy $w
if {$userpriority ne $tmppriority} {
set userpriority $tmppriority
set userstatus $userstatus
}
}
###############################################################################
trace variable userstatus w change_our_presence
trace variable logoutuserstatus w change_our_presence
###############################################################################
proc change_our_presence {name1 name2 op} {
global userstatus logoutuserstatus curuserstatus
global textstatus logouttextstatus curtextstatus
global userpriority logoutpriority curpriority
global statusdesc userstatusdesc
switch -- $name1 {
logoutuserstatus {
set newstatus $logoutuserstatus
set newtextstatus $logouttextstatus
set newpriority $logoutpriority
}
default {
if {[llength [connections]] == 0} return
set newstatus $userstatus
set newtextstatus $textstatus
set newpriority $userpriority
}
}
if {$newstatus eq $curuserstatus && \
$newtextstatus eq $curtextstatus && \
$newpriority eq $curpriority} {
return
}
if {$newstatus ni [array names statusdesc]} {
error [::msgcat::mc "Invalid userstatus value %s" $newstatus]
}
set userstatusdesc $statusdesc($newstatus)
set status $newtextstatus
foreach xlib [connections] {
send_presence $xlib $newstatus \
-status $status \
-priority $newpriority
}
foreach chatid [lfilter chat::is_groupchat [chat::opened]] {
set xlib [chat::get_xlib $chatid]
set group [chat::get_jid $chatid]
set nick [get_our_groupchat_nick $chatid]
if {$newstatus == "invisible"} {
set newst available
} else {
set newst $newstatus
}
send_presence $xlib $newst \
-to $group/$nick \
-status $status \
-priority $userpriority
}
set curuserstatus $newstatus
set curtextstatus $newtextstatus
set curpriority $newpriority
hook::run change_our_presence_post_hook $newstatus
}
###############################################################################
proc send_first_presence {xlib} {
global userstatus curuserstatus statusdesc userstatusdesc
global textstatus curtextstatus
global userpriority curpriority
global loginconf
if {$userstatus ni [array names statusdesc]} {
error [::msgcat::mc "Invalid userstatus value %s" $userstatus]
}
set userstatusdesc $statusdesc($userstatus)
set status $textstatus
set curuserstatus $userstatus
set curtextstatus $textstatus
set curpriority [set userpriority $loginconf(priority)]
send_presence $xlib $userstatus \
-status $status \
-priority $userpriority
hook::run change_our_presence_post_hook $userstatus
}
hook::add connected_hook [namespace current]::send_first_presence 15
###############################################################################
proc send_custom_presence {xlib jid status args} {
global userpriority
global statusdesc
set type jid
set stat ""
foreach {key val} $args {
switch -- $key {
-type { set type $val }
-status { set stat $val }
}
}
switch -- $type {
group {
set to $jid/[get_our_groupchat_nick [chat::chatid $xlib $jid]]
}
default {
set to $jid
}
}
eval {send_presence $xlib $status} $args \
{-to $to -status $stat -priority $userpriority}
}
###############################################################################
proc send_presence {xlib status args} {
set newargs [eval [list presence_args $xlib $status] $args]
eval [list ::xmpp::sendPresence $xlib] $newargs
}
###############################################################################
proc presence_args {xlib status args} {
switch -- $status {
available {
set newargs {}
}
unavailable {
set newargs [list -type $status]
}
default {
set newargs [list -show $status]
}
}
set xlist {}
set to ""
set stat ""
foreach {opt val} $args {
switch -- $opt {
-id { lappend newargs -id $val }
-to {
set to $val
lappend newargs -to $val
}
-priority { lappend newargs -priority $val }
-xlist { set xlist $val }
-status { set stat $val }
}
}
hook::run rewrite_presence_status_hook stat $xlib
if {$stat != ""} {
lappend newargs -status $stat
}
hook::run presence_xlist_hook xlist $xlib $stat
lappend newargs -xlist $xlist
debugmsg presence "$newargs"
return $newargs
}
###############################################################################
proc normalize_show {show} {
set res $show
switch -- $show {
away {}
chat {}
dnd {}
xa {}
unavailable {}
default {set res available}
}
return $res
}
###############################################################################
proc add_presence_to_popup_info {infovar xlib jid} {
upvar 0 $infovar info
set bjid [::xmpp::jid::stripResource $jid]
if {[chat::is_groupchat [chat::chatid $xlib $bjid]]} return
set priority [get_jid_presence_info priority $xlib $jid]
if {$priority != ""} {
append info "\n\t[::msgcat::mc {Priority:}] $priority"
}
}
hook::add roster_user_popup_info_hook add_presence_to_popup_info 20
###############################################################################
proc clear_presence_info {xlib} {
global curuserstatus
global userstatusdesc
global presence
array unset presence type,$xlib,*
array unset presence status,$xlib,*
array unset presence priority,$xlib,*
array unset presence show,$xlib,*
array unset presence error,$xlib,*
array unset presence x,$xlib,*
array unset presence user_jids,$xlib,*
array unset presence preferred_jid,$xlib,*
array unset presence cachedstatus,$xlib,*
array unset presence maxpriority,$xlib,*
if {[connections] == {}} {
set_status "Disconnected"
set curuserstatus unavailable
set userstatusdesc [::msgcat::mc "Not logged in"]
hook::run change_our_presence_post_hook unavailable
}
}
hook::add disconnected_hook clear_presence_info
###############################################################################
proc custom_presence_menu {m xlib jid} {
set chatid [chat::chatid $xlib $jid]
set chatid1 [chat::chatid $xlib [::xmpp::jid::removeResource $jid]]
if {![chat::is_groupchat $chatid] && [chat::is_groupchat $chatid1]} {
return
}
if {[chat::is_groupchat $chatid]} {
set jid [::xmpp::jid::replaceResource $jid \
[get_our_groupchat_nick $chatid]]
}
set mm [menu $m.custom_presence -tearoff 0]
$mm add command -label [::msgcat::mc "Available"] \
-command [list send_custom_presence $xlib $jid available]
$mm add command -label [::msgcat::mc "Free to chat"] \
-command [list send_custom_presence $xlib $jid chat]
$mm add command -label [::msgcat::mc "Away"] \
-command [list send_custom_presence $xlib $jid away]
$mm add command -label [::msgcat::mc "Extended away"] \
-command [list send_custom_presence $xlib $jid xa]
$mm add command -label [::msgcat::mc "Do not disturb"] \
-command [list send_custom_presence $xlib $jid dnd]
$mm add command -label [::msgcat::mc "Unavailable"] \
-command [list send_custom_presence $xlib $jid unavailable]
$m add cascade -label [::msgcat::mc "Send custom presence"] \
-menu $mm
}
hook::add chat_create_user_menu_hook custom_presence_menu 43
hook::add roster_jid_popup_menu_hook custom_presence_menu 43
hook::add roster_service_popup_menu_hook custom_presence_menu 43
hook::add chat_create_conference_menu_hook custom_presence_menu 43
###############################################################################
proc service_login {xlib jid} {
global userstatus curtextstatus
set newargs {}
if {$curtextstatus != ""} {
lappend newargs -status $curtextstatus
}
switch -- $userstatus {
available {
set command [list ::xmpp::sendPresence $xlib -to $jid]
}
invisible {
set command [list ::xmpp::sendPresence $xlib -to $jid \
-type $userstatus]
}
default {
set command [list ::xmpp::sendPresence $xlib -to $jid \
-show $userstatus]
}
}
eval $command $newargs
}
proc service_logout {xlib jid} {
global curtextstatus
set newargs {}
if {$curtextstatus != ""} {
lappend newargs -status $curtextstatus
}
set command [list ::xmpp::sendPresence $xlib -to $jid -type unavailable]
eval $command $newargs
}
proc service_login_logout_menu_item {m xlib jid} {
# TODO
$m add command -label [::msgcat::mc "Log in"] \
-command [list service_login $xlib $jid]
$m add command -label [::msgcat::mc "Log out"] \
-command [list service_logout $xlib $jid]
}
hook::add roster_service_popup_menu_hook service_login_logout_menu_item 20
###############################################################################
proc systray_presence_menu_item {m} {
set mp [menu $m.presence -title [::msgcat::mc "Presence"] \
-tearoff $ifacetk::options(show_tearoffs)]
$mp add command -label [::msgcat::mc "Available"] \
-command {set userstatus available}
$mp add command -label [::msgcat::mc "Free to chat"] \
-command {set userstatus chat}
$mp add command -label [::msgcat::mc "Away"] \
-command {set userstatus away}
$mp add command -label [::msgcat::mc "Extended away"] \
-command {set userstatus xa}
$mp add command -label [::msgcat::mc "Do not disturb"] \
-command {set userstatus dnd}
$mp add separator
$mp add command -label [::msgcat::mc "Change priority..."] \
-command change_priority_dialog
$m add cascade -label [::msgcat::mc "Presence"] -menu $mp
}
hook::add systray_menu_hook systray_presence_menu_item 40
# vim:ft=tcl:ts=8:sw=4:sts=4:et
|