2 # -*- coding: utf-8 -*-
5 Slixmpp: The Slick XMPP Library
6 Copyright (C) 2012 Nathanael C. Fritz
7 This file is part of Slixmpp.
9 See the file LICENSE for copying permission.
18 from optparse
import OptionParser
21 from slixmpp
.exceptions
import XMPPError
24 # Python versions before 3.0 do not use UTF-8 encoding
25 # by default. To ensure that Unicode is handled properly
26 # throughout Slixmpp, we will set the default encoding
28 if sys
.version_info
< (3, 0):
29 from slixmpp
.util
.misc_ops
import setdefaultencoding
30 setdefaultencoding('utf8')
35 class AvatarSetter(slixmpp
.ClientXMPP
):
38 A basic script for downloading the avatars for a user's contacts.
41 def __init__(self
, jid
, password
, filepath
):
42 slixmpp
.ClientXMPP
.__init
__(self
, jid
, password
)
44 self
.add_event_handler("session_start", self
.start
, threaded
=True)
46 self
.filepath
= filepath
48 def start(self
, event
):
50 Process the session_start event.
52 Typical actions for the session_start event are
53 requesting the roster and broadcasting an initial
57 event -- An empty dictionary. The session_start
58 event does not provide any additional
66 avatar_file
= open(os
.path
.expanduser(self
.filepath
))
68 print('Could not find file: %s' % self
.filepath
)
69 return self
.disconnect()
71 avatar
= avatar_file
.read()
73 avatar_type
= 'image/%s' % imghdr
.what('', avatar
)
74 avatar_id
= self
['xep_0084'].generate_id(avatar
)
75 avatar_bytes
= len(avatar
)
81 print('Publish XEP-0084 avatar data')
82 self
['xep_0084'].publish_avatar(avatar
)
85 print('Could not publish XEP-0084 avatar')
88 print('Update vCard with avatar')
89 self
['xep_0153'].set_avatar(avatar
=avatar
, mtype
=avatar_type
)
91 print('Could not set vCard avatar')
95 print('Advertise XEP-0084 avatar metadata')
96 self
['xep_0084'].publish_avatar_metadata([
99 'bytes': avatar_bytes
}
100 # We could advertise multiple avatars to provide
101 # options in image type, source (HTTP vs pubsub),
106 print('Could not publish XEP-0084 metadata')
108 print('Wait for presence updates to propagate...')
109 self
.schedule('end', 5, self
.disconnect
, kwargs
={'wait': True})
112 if __name__
== '__main__':
113 # Setup the command line arguments.
114 optp
= OptionParser()
115 optp
.add_option('-q','--quiet', help='set logging to ERROR',
116 action
='store_const',
119 default
=logging
.ERROR
)
120 optp
.add_option('-d','--debug', help='set logging to DEBUG',
121 action
='store_const',
124 default
=logging
.ERROR
)
125 optp
.add_option('-v','--verbose', help='set logging to COMM',
126 action
='store_const',
129 default
=logging
.ERROR
)
131 # JID and password options.
132 optp
.add_option("-j", "--jid", dest
="jid",
134 optp
.add_option("-p", "--password", dest
="password",
135 help="password to use")
136 optp
.add_option("-f", "--file", dest
="filepath",
137 help="path to the avatar file")
138 opts
,args
= optp
.parse_args()
141 logging
.basicConfig(level
=opts
.loglevel
,
142 format
='%(levelname)-8s %(message)s')
145 opts
.jid
= raw_input("Username: ")
146 if opts
.password
is None:
147 opts
.password
= getpass
.getpass("Password: ")
148 if opts
.filepath
is None:
149 opts
.filepath
= raw_input("Avatar file location: ")
151 xmpp
= AvatarSetter(opts
.jid
, opts
.password
, opts
.filepath
)
152 xmpp
.register_plugin('xep_0054')
153 xmpp
.register_plugin('xep_0153')
154 xmpp
.register_plugin('xep_0084')
156 # If you are working with an OpenFire server, you may need
157 # to adjust the SSL version used:
158 # xmpp.ssl_version = ssl.PROTOCOL_SSLv3
160 # If you want to verify the SSL certificates offered by a server:
161 # xmpp.ca_certs = "path/to/ca/cert"
163 # Connect to the XMPP server and start processing XMPP stanzas.
165 # If you do not have the dnspython library installed, you will need
166 # to manually specify the name of the server if it does not match
167 # the one in the JID. For example, to use Google Talk you would
170 # if xmpp.connect(('talk.google.com', 5222)):
172 xmpp
.process(block
=True)
174 print("Unable to connect.")