# $Id: jitworkaround.tcl 1520 2008-10-17 17:16:09Z sergei $

###############################################################################
# JIT workaround: when receving <presence/> stanza with name attribute and
# roster item doesn't have name, fill the name
#

namespace eval jitworkaround {}

proc jitworkaround::process_subscribed {xlib from type x args} {
    variable names

    if {$type != "subscribed"} return

    set newname ""
    foreach {opt val} $args {
	switch -- $opt {
	    -name { set newname $val }
	}
    }

    if {$newname == ""} return

    set jid [roster::find_jid $xlib $from]

    if {$jid == ""} {
	set names($xlib,$from) $newname
    } elseif {[roster::itemconfig $xlib $jid -name] == ""} {
	roster::itemconfig $xlib $jid -name $newname
	roster::send_item $xlib $jid
	::redraw_roster
    }
}

hook::add client_presence_hook \
	  [namespace current]::jitworkaround::process_subscribed

###############################################################################

proc jitworkaround::set_received_name {xlib jid name groups subsc ask} {
    variable names

    if {$subsc == "remove"} return

    if {[info exists names($xlib,$jid)]} {
	if {[roster::itemconfig $xlib $jid -name] == ""} {
	    roster::itemconfig $xlib $jid -name $names($xlib,$jid)
	    roster::send_item $xlib $jid
	}
	unset names($xlib,$jid)
    }
}

hook::add roster_push_hook \
	  [namespace current]::jitworkaround::set_received_name 60

###############################################################################

