# Copyright 2005 James Bunton <james@delx.cjb.net>
# Licensed for distribution under the GPL version 2, check COPYING for details
-import utils
-import config
-from twisted.internet import reactor
-if(utils.checkTwisted()):
- from twisted.xish.domish import Element
-else:
- from tlib.domish import Element
from debug import LogEvent, INFO, WARN, ERROR
-import jabw
+
+from twisted.internet import reactor
+from tlib.xmlw import Element
+
+import sha, base64, os, os.path
+
+import utils
import config
-import lang
-import sha
-import base64
-import os
-import os.path
+
SPOOL_UMASK = 0077
def makePhotoElement(self):
""" Returns an XML Element that can be put into the vCard. """
photo = Element((None, "PHOTO"))
- type = photo.addElement("TYPE")
- type.addContent("image/png")
+ cType = photo.addElement("TYPE")
+ cType.addContent("image/png")
binval = photo.addElement("BINVAL")
binval.addContent(base64.encodestring(self.getImageData()))
return photo
def dir(self, key):
""" Returns the full path to the directory that a
particular key is in. Creates that directory if it doesn't already exist. """
- d = os.path.abspath(config.spooldir) + "/" + config.jid + "/avatars/" + key[0:3] + "/"
+ X = os.path.sep
+ d = os.path.os.path.abspath(config.spooldir) + X + config.jid + X + "avatars" + X + key[0:3] + X
if not os.path.exists(d):
os.makedirs(d)
return d
LogEvent(INFO, "", "Setting avatar %s" % (key))
prev_umask = os.umask(SPOOL_UMASK)
try:
- f = open(self.dir(key) + key, 'w')
+ f = open(self.dir(key) + key, 'wb')
f.write(imageData)
f.close()
except IOError, e:
imageData = self.getAvatarData(key)
if imageData:
return Avatar(imageData, self)
+ else:
+ return None
def getAvatarData(self, key):
""" Loads the avatar with SHA1 hash of 'key' from disk and returns the data """
filename = self.dir(key) + key
if os.path.isfile(filename):
LogEvent(INFO, "Getting avatar.")
- f = open(filename)
+ f = open(filename, "rb")
data = f.read()
f.close()
return data
else:
LogEvent(INFO, "", "Avatar not found.")
- except IOError, e:
+ except IOError:
LogEvent(WARN, "", "IOError reading avatar.")
+ else:
+ return None