summaryrefslogtreecommitdiff
path: root/plugins/vcard.py
blob: e3a776e353ea5413ee29979fa3cc3579c5b0aa11 (plain)
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
"""
This plugin adds a :term:`/vcard` command to all tabs, allowing you to request
and display the vcard-temp of any given entity.

Command
-------

.. glossary::

    /vcard

        **Usage (globally):** ``/vcard <jid>``

        **Usage (in a chatroom tab):** ``/vcard <jid or nick>``

        **Usage (in a conversation or contact list tab):** ``/vcard [jid]``

        Globally, you can do ``/vcard user@server.example`` to get a vcard.

        In a chatroom , you can either do it on a JID or a nick (``/vcard nick``,
        ``/vcard room@muc.server.example/nick`` or ``/vcard
        user@server.example``).

        In a private or a direct conversation, you can do ``/vcard`` to request
        vcard from the current interlocutor, and in the contact list to do it
        on the currently selected contact.
"""

from poezio.decorators import command_args_parser
from poezio.plugin import BasePlugin
from poezio.roster import roster
from poezio.common import safeJID
from poezio.contact import Contact, Resource
from poezio.core.structs import Completion
from poezio import tabs
from slixmpp.jid import JID, InvalidJID


class Plugin(BasePlugin):
    def init(self):
        self.api.add_command(
            'vcard',
            self.command_vcard,
            usage='<jid>',
            help='Send an XMPP vcard request to jid (see XEP-0054).',
            short='Send a vcard request',
            completion=self.completion_vcard)
        self.api.add_tab_command(
            tabs.MucTab,
            'vcard',
            self.command_muc_vcard,
            usage='<jid|nick>',
            help='Send an XMPP vcard request to jid or nick (see XEP-0054).',
            short='Send a vcard request.',
            completion=self.completion_muc_vcard)
        self.api.add_tab_command(
            tabs.RosterInfoTab,
            'vcard',
            self.command_roster_vcard,
            usage='<jid>',
            help='Send an XMPP vcard request to jid (see XEP-0054).',
            short='Send a vcard request.',
            completion=self.completion_vcard)
        for _class in (tabs.PrivateTab, tabs.DynamicConversationTab, tabs.StaticConversationTab):
            self.api.add_tab_command(
                _class,
                'vcard',
                self.command_private_vcard,
                usage='[jid]',
                help=
                'Send an XMPP vcard request to the current interlocutor or the given JID.',
                short='Send a vcard request',
                completion=self.completion_vcard)

    def _handle_vcard(self, iq):
        '''Retrieves a vCard from vcard-temp and present it as a DataFormsTab.
        '''
        jid = iq['from']

        if iq['type'] == 'error':
            self.api.information(
                'Error retrieving vCard for %s: %s: %s' %
                (jid, iq['error']['type'], iq['error']['condition']), 'Error')
            return

        vcard = iq['vcard_temp']

        form = self.core.xmpp['xep_0004'].make_form(
            ftype='result', title='vCard of %s' % jid)

        # TODO: implement the other fields.

        form.add_field(
            var='FN', ftype='text-single', label='Name', value=vcard['FN'])
        form.add_field(
            var='NICKNAME',
            ftype='text-multi',
            label='Nicknames',
            value=vcard['NICKNAME'])

        # TODO: find a way to detect whether this is present or not.
        form.add_field(ftype='fixed', value='Full Name')
        form.add_field(
            var='N/GIVEN',
            ftype='text-single',
            label='Given',
            value=vcard['N']['GIVEN'])
        form.add_field(
            var='N/MIDDLE',
            ftype='text-single',
            label='Middle',
            value=vcard['N']['MIDDLE'])
        form.add_field(
            var='N/FAMILY',
            ftype='text-single',
            label='Family',
            value=vcard['N']['FAMILY'])
        form.add_field(
            var='N/PREFIX',
            ftype='text-single',
            label='Prefix',
            value=vcard['N']['PREFIX'])
        form.add_field(
            var='N/SUFFIX',
            ftype='text-single',
            label='Suffix',
            value=vcard['N']['SUFFIX'])

        for i, addr in enumerate(vcard['addresses']):
            form.add_field(ftype='fixed', value='Address')
            values = [type_ for type_ in addr.bool_interfaces if addr[type_]]
            addr_type = form.add_field(
                var='ADR %d/TYPE' % i,
                ftype='list-multi',
                label='Type',
                value=values)
            addr_type.add_option(label='Home', value='HOME')
            for type_ in addr.bool_interfaces:
                addr_type.add_option(label=type_, value=type_)
            form.add_field(
                var='ADR %d/POBOX' % i,
                ftype='text-single',
                label='Pobox',
                value=addr['POBOX'])
            form.add_field(
                var='ADR %d/EXTADD' % i,
                ftype='text-single',
                label='Extended Address',
                value=addr['EXTADD'])
            form.add_field(
                var='ADR %d/STREET' % i,
                ftype='text-single',
                label='Street',
                value=addr['STREET'])
            form.add_field(
                var='ADR %d/LOCALITY' % i,
                ftype='text-single',
                label='Locality',
                value=addr['LOCALITY'])
            form.add_field(
                var='ADR %d/REGION' % i,
                ftype='text-single',
                label='Region',
                value=addr['REGION'])
            form.add_field(
                var='ADR %d/PCODE' % i,
                ftype='text-single',
                label='Post Code',
                value=addr['PCODE'])
            form.add_field(
                var='ADR %d/CTRY' % i,
                ftype='text-single',
                label='Country',
                value=addr['CTRY'])

        for i, tel in enumerate(vcard['telephone_numbers']):
            form.add_field(ftype='fixed', value='Telephone')
            values = [type_ for type_ in tel.bool_interfaces if tel[type_]]
            tel_type = form.add_field(
                var='TEL %d/TYPE' % i,
                ftype='list-multi',
                label='Type',
                value=values)
            for type_ in tel.bool_interfaces:
                tel_type.add_option(label=type_, value=type_)
            form.add_field(
                var='TEL %d/NUMBER' % i,
                ftype='text-single',
                label='Number',
                value=tel['NUMBER'])

        for i, email in enumerate(vcard['emails']):
            form.add_field(ftype='fixed', value='Email address')
            values = [type_ for type_ in email.bool_interfaces if email[type_]]
            email_type = form.add_field(
                var='EMAIL %d/TYPE' % i,
                ftype='list-multi',
                label='Type',
                value=values)
            for type_ in email.bool_interfaces:
                email_type.add_option(label=type_, value=type_)
            form.add_field(
                var='EMAIL %d/USERID' % i,
                ftype='text-single',
                label='Email Address',
                value=email['USERID'])

        form.add_field(ftype='fixed', value='Misc')
        form.add_field(
            var='BDAY',
            ftype='text-single',
            label='Birthday',
            value=str(vcard['BDAY']))

        for i, jabberid in enumerate(vcard['jids']):
            form.add_field(ftype='fixed', value='URL')
            form.add_field(
                var='JABBERID %d' % i,
                ftype='jid-single',
                label='URL',
                value=jabberid['JABBERID'])

        for i, url in enumerate(vcard['urls']):
            form.add_field(ftype='fixed', value='URL')
            form.add_field(
                var='URL %d' % i,
                ftype='text-single',
                label='URL',
                value=url['URL'])

        for i, desc in enumerate(vcard['descriptions']):
            form.add_field(ftype='fixed', value='Description')
            form.add_field(
                var='DESC %d' % i,
                ftype='text-multi',
                label='Description',
                value=desc['DESC'])

        on_validate = lambda form: self.core.close_tab()
        on_cancel = lambda form: self.core.close_tab()
        self.core.open_new_form(form, on_cancel, on_validate)

    def _get_vcard(self, jid):
        '''Send an iq to ask the vCard.'''

        def timeout_cb(iq):
            self.api.information('Timeout while retrieving vCard for %s' % jid,
                                 'Error')
            return

        self.core.xmpp.plugin['xep_0054'].get_vcard(
            jid=jid,
            timeout=30,
            callback=self._handle_vcard,
            timeout_callback=timeout_cb)

    @command_args_parser.raw
    def command_vcard(self, arg):
        if not arg:
            self.core.command.help('vcard')
            return

        try:
            jid = JID(arg)
        except InvalidJID:
            self.api.information('Invalid JID: %s' % arg, 'Error')
            return

        self._get_vcard(jid)

    @command_args_parser.raw
    def command_private_vcard(self, arg):
        if arg:
            self.command_vcard(arg)
            return
        self.command_vcard(self.api.current_tab().jid.full)

    @command_args_parser.raw
    def command_muc_vcard(self, arg):
        if not arg:
            self.core.command.help('vcard')
            return
        user = self.api.current_tab().get_user_by_name(arg)
        if user:
            jid = self.api.current_tab().jid.bare + '/' + user.nick
        else:
            try:
                jid = safeJID(arg)
            except InvalidJID:
                return self.api.information('Invalid JID: %s' % arg, 'Error')
        self._get_vcard(jid)

    @command_args_parser.raw
    def command_roster_vcard(self, arg):
        if arg:
            self.command_vcard(arg)
            return
        current = self.api.current_tab().selected_row
        if isinstance(current, Resource):
            self._get_vcard(JID(current.jid).bare)
        elif isinstance(current, Contact):
            self._get_vcard(current.bare_jid)

    def completion_vcard(self, the_input):
        contacts = [contact.bare_jid for contact in roster.get_contacts()]
        return Completion(
            the_input.auto_completion, contacts, '', quotify=False)

    def completion_muc_vcard(self, the_input):
        users = [user.nick for user in self.api.current_tab().users]
        users.extend([
            resource.jid for contact in roster.get_contacts()
            for resource in contact.resources
        ])
        return Completion(the_input.auto_completion, users, '', quotify=False)