[Tkabber-dev] r315 - trunk/plugins/avatarcache

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Mon Mar 8 11:49:46 MSK 2010


Author: Hermitifier
Date: 2010-03-08 11:49:46 +0300 (Mon, 08 Mar 2010)
New Revision: 315

Modified:
   trunk/plugins/avatarcache/avatarcache.tcl
Log:
remove excessive debug level
fix restoring empty avatars
style tweaks
relax custom avatar file extensions

Modified: trunk/plugins/avatarcache/avatarcache.tcl
===================================================================
--- trunk/plugins/avatarcache/avatarcache.tcl	2010-03-05 18:55:49 UTC (rev 314)
+++ trunk/plugins/avatarcache/avatarcache.tcl	2010-03-08 08:49:46 UTC (rev 315)
@@ -2,6 +2,7 @@
 # Automatically downloads avatars announced by PEP or vcard-temp update
 # Written by Jan Zachorowski <quantifier666 at gmail.com>
 
+package require base64
 package require sha1
 
 namespace eval avatarcache {
@@ -16,7 +17,6 @@
 	    [list [namespace current]::process_avatar_metadata_notification "urn:xmpp:avatar:metadata"]
     pubsub::register_event_notification_handler "http://www.xmpp.org/extensions/xep-0084.html#ns-metadata" \
 	    [list [namespace current]::process_avatar_metadata_notification "http://www.xmpp.org/extensions/xep-0084.html#ns-metadata"]
-    lappend ::debug_lvls avatarcache
     
     file mkdir [file join $::configdir avatars]
 
@@ -37,8 +37,8 @@
 proc avatarcache::process_presence {xlib from type x args} {
     variable vcard_photo_hashes
     variable assigned_hashes
-    set sjid [::xmpp::jid::stripResource $from]
-    if {[chat::is_groupchat [chat::chatid $xlib $sjid]]} {
+    set bjid [::xmpp::jid::stripResource $from]
+    if {[chat::is_groupchat [chat::chatid $xlib $bjid]]} {
 	return
     }
     switch -- $type {
@@ -52,31 +52,31 @@
 			::xmpp::xml::split $subel stag sxmlns sattrs scdata ssubels
 			if {$stag == "photo"} {
 			    debugmsg avatarcache "photo $from: '$scdata'"
-			    if {![info exists vcard_photo_hashes($sjid)] \
-				    || $vcard_photo_hashes($sjid) != $scdata} {
+			    if {![info exists vcard_photo_hashes($bjid)] \
+				    || $vcard_photo_hashes($bjid) != $scdata} {
 				debugmsg avatarcache "saving unknown photo hash $from $scdata"
-				#very dirty hack ahead:
-				#if {$scdata == "9a9daf48824e2da088a085ae512a2e0377367e18"} return
 				if {$scdata != ""} {
-				    set vcard_photo_hashes($sjid) $scdata
+				    set vcard_photo_hashes($bjid) $scdata
 				    store_cache
 				    set filename [file join $::configdir avatars $scdata]
-				    if {![file exists $filename] && [roster::is_trusted $xlib $sjid]} {
-					after_vCard_photo "" $xlib $from 0
-					::xmpp::sendIQ $xlib get \
-					    -query [::xmpp::xml::create vCard -xmlns vcard-temp] \
-					    -to $sjid \
-					    -command [list userinfo::parse_vcard $from]
+				    if {![file exists $filename]} {
+				        if {[roster::is_trusted $xlib $bjid]} {
+					    after_vCard_photo "" $xlib $from 0
+					    ::xmpp::sendIQ $xlib get \
+						-query [::xmpp::xml::create vCard -xmlns vcard-temp] \
+						-to $bjid \
+						-command [list userinfo::parse_vcard $from]
+					}
 				    } {
-					if {![info exists assigned_hashes($sjid)]} {
-					    hook::run user_avatar_notification_hook $sjid $filename
+					if {![info exists assigned_hashes($bjid)]} {
+					    hook::run user_avatar_notification_hook $bjid $filename
 					}
 				    }
 				} {
-				    catch {unset vcard_photo_hashes($sjid)}
+				    catch {unset vcard_photo_hashes($bjid)}
 				    store_cache
-				    if {![info exists assigned_hashes($sjid)]} {
-					hook::run user_avatar_notification_hook $sjid ""
+				    if {![info exists assigned_hashes($bjid)]} {
+					hook::run user_avatar_notification_hook $bjid ""
 				    }
 				}
 			    }
@@ -185,30 +185,28 @@
 proc avatarcache::after_vCard_photo {tab xlib jid editable} {
     debugmsg avatarcache [info level 0]
     if {[roster::is_trusted $xlib $jid] && !$editable} {
-	set xjid [::xmpp::jid::stripResource $jid]
-	trace vdelete ::userinfo::userinfo(photo_binval,$jid) w [list [namespace current]::save_vCard_photo $jid $xjid]
-	trace variable ::userinfo::userinfo(photo_binval,$jid) w [list [namespace current]::save_vCard_photo $jid $xjid]
+	set bjid [::xmpp::jid::stripResource $jid]
+	trace vdelete ::userinfo::userinfo(photo_binval,$jid) w [list [namespace current]::save_vCard_photo $jid $bjid]
+	trace variable ::userinfo::userinfo(photo_binval,$jid) w [list [namespace current]::save_vCard_photo $jid $bjid]
     }
 }
 
-proc avatarcache::save_vCard_photo {jid xjid a b c} {
+proc avatarcache::save_vCard_photo {jid bjid a b c} {
     variable assigned_hashes
     debugmsg avatarcache [info level 0]
-    trace vdelete "userinfo::$a\($b\)" $c [list [namespace current]::save_vCard_photo $jid $xjid]
+    trace vdelete "userinfo::$a\($b\)" $c [list [namespace current]::save_vCard_photo $jid $bjid]
     variable vcard_photo_hashes
-    set bjid [::xmpp::jid::stripResource $jid]
     set hash [sha1::sha1 $::userinfo::userinfo(photo_binval,$jid)]
     if {[info exists vcard_photo_hashes($bjid)] && $vcard_photo_hashes($bjid) != ""} {
-	#TODO: check if hashes match ...
-	set filename [file join $::configdir avatars $vcard_photo_hashes($bjid)]
+	#TODO: check if hashes match ... misbehaving clients might be announcing wrong hash.
+	# what should happen then ?
+	#set filename [file join $::configdir avatars $vcard_photo_hashes($xjid)]
     } {
-	#FIXME: calculate hash and add jid->hash mapping instead of this
-	#set filename [file join $::configdir avatars [::logger::jid_to_filename $xjid]]
+	#we didn't see avatar announce yet, but we deduce it anyway
 	set vcard_photo_hashes($bjid) $hash
-	set filename [file join $::configdir avatars $hash]
+	store_cache
     }
-    #very dirty hack ahead:
-    #if {$hash == "3afe88c89837563780697d2d30d5e9f47a7325dc" || $hash == "9a9daf48824e2da088a085ae512a2e0377367e18"} return
+    set filename [file join $::configdir avatars $hash]
 	
     if {!([file exists $filename] && [file attributes $filename -readonly])} {
 	set fileid [open $filename w]
@@ -216,8 +214,8 @@
 	puts -nonewline $fileid $::userinfo::userinfo(photo_binval,$jid)
 	close $fileid
     }
-    if {![info exists assigned_hashes($jid)]} {
-	hook::run user_avatar_notification_hook $xjid $filename
+    if {![info exists assigned_hashes($bjid)]} {
+	hook::run user_avatar_notification_hook $bjid $filename
     }
     return
 }
@@ -225,38 +223,49 @@
 proc avatarcache::store_cache {} {
     variable pep_avatar_hashes
     variable vcard_photo_hashes
-    #variable assigned_hashes
     variable avatarcache
     array set tmp $avatarcache
     array set tmp [array get vcard_photo_hashes]
     array set tmp [array get pep_avatar_hashes]
-    #array set tmp [array get assigned_hashes]
     set avatarcache [array get tmp]
 }
 
 proc avatarcache::restore_cache {} {
     variable avatarcache
     variable avatarcache_custom
+    variable assigned_hashes
     
-    foreach filename [glob -nocomplain [file join $::configdir avatars *@*.*]] {
-	set jid_lookalike [file tail $filename]
-	if {[info exists avatarcache($jid_lookalike)]} continue
-
-	hook::run user_avatar_notification_hook $jid_lookalike $filename
-    }
+#    foreach filename [glob -nocomplain [file join $::configdir avatars *@*.*]] {
+#	set jid_lookalike [file tail $filename]
+#	if {[info exists avatarcache($jid_lookalike)]} continue
+#
+#	hook::run user_avatar_notification_hook $jid_lookalike $filename
+#    }
     
     array set assigned_hashes $avatarcache_custom
     foreach {jid hash} $avatarcache {
-	set filename [file join $::configdir avatars $hash]
-	if {[file exists $filename] && ![info exists assigned_hashes($jid)]} {
-	    hook::run user_avatar_notification_hook $jid $filename
+	if {$hash != ""} {
+	    set filename [file join $::configdir avatars $hash]
+	    if {[file exists $filename] && ![info exists assigned_hashes($jid)]} {
+		debugmsg avatarcache "restoring '$jid' -> '$hash'"
+		hook::run user_avatar_notification_hook $jid $filename
+	    }
+	} {
+	    debugmsg avatarcache "restoring '$jid' -> '$hash'"
+	    hook::run user_avatar_notification_hook $jid ""
 	}
     }
     
     foreach {jid hash} $avatarcache_custom {
-	set filename [file join $::configdir avatars $hash]
-	if {[file exists $filename]} {
-	    hook::run user_avatar_notification_hook $jid $filename
+	if {$hash != ""} {
+	    set filename [file join $::configdir avatars $hash]
+	    if {[file exists $filename]} {
+		debugmsg avatarcache "restoring '$jid' -> '$hash'"
+		hook::run user_avatar_notification_hook $jid $filename
+	    }
+	} {
+	    debugmsg avatarcache "restoring '$jid' -> '$hash'"
+	    hook::run user_avatar_notification_hook $jid ""
 	}
     }
 }
@@ -265,7 +274,8 @@
     variable assigned_hashes
     variable avatarcache_custom
     set custom_filename [tk_getOpenFile -defaultextension .png \
-					-filetypes [list [list "Images" [list ".png" ".jpg" ".jpeg" ".gif" ".bmp"]]] \
+					-filetypes [list [list "Images" [list ".png" ".jpg" ".jpeg" ".gif" ".bmp"]]\
+							 [list "All files" [list "*.*"]]] \
 					-title "Select custom avatar"]
     if {$custom_filename == ""} return
     



More information about the Tkabber-dev mailing list