annotate jabberbot/xmppbot.py @ 2694:6500951853e2

Make "get page info" work with data forms.
author Karol Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 17 Aug 2007 23:29:52 +0200
parents da94375a625c
children 724b6b93f2f9
rev   line source
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
2110
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
3 MoinMoin - jabber bot
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
5 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
6 @license: GNU GPL, see COPYING for details.
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
8
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
9 import logging, time, Queue
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10 from threading import Thread
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
11
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
12 from pyxmpp.client import Client
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
13 from pyxmpp.jid import JID
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
14 from pyxmpp.streamtls import TLSSettings
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
15 from pyxmpp.message import Message
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
16 from pyxmpp.presence import Presence
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
17 from pyxmpp.iq import Iq
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
18 import pyxmpp.jabber.dataforms as forms
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
19 import libxml2
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
20
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
21 import jabberbot.commands as cmd
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
22 import jabberbot.i18n as i18n
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
23 import jabberbot.oob as oob
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
24
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2085
diff changeset
25
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
26 class Contact:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
27 """Abstraction of a roster item / contact
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
28
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
29 This class handles some logic related to keeping track of
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
30 contact availability, status, etc."""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
31
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
32 # Default Time To Live of a contact. If there are no registered
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
33 # resources for that period of time, the contact should be removed
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
34 default_ttl = 3600 * 24 # default of one day
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
35
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
36 def __init__(self, jid, resource, priority, show, language=None):
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
37 self.jid = jid
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
38 self.resources = {resource: {'show': show, 'priority': priority, 'supports': []}}
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
39 self.language = language
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
40
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
41 # The last time when this contact was seen online.
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
42 # This value has meaning for offline contacts only.
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
43 self.last_online = None
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
44
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
45 # Queued messages, waiting for contact to change its "show"
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
46 # status to something different than "dnd". The messages should
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
47 # also be sent when contact becomes "unavailable" directly from
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
48 # "dnd", as we can't guarantee, that the bot will be up and running
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
49 # the next time she becomes "available".
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
50 self.messages = []
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
51
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
52 def is_valid(self, current_time):
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
53 """Check if this contact entry is still valid and should be kept
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
54
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
55 @param time: current time in seconds
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
56
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
57 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
58 # No resources == offline
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
59 return self.resources or current_time < self.last_online + self.default_ttl
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
60
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
61 def add_resource(self, resource, show, priority):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
62 """Adds information about a connected resource
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
63
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
64 @param resource: resource name
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
65 @param show: a show presence property, as defined in XMPP
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
66 @param priority: priority of the given resource
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
67
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
68 """
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
69 self.resources[resource] = {'show': show, 'priority': priority, 'supports': []}
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
70 self.last_online = None
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
71
2654
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
72 def set_supports(self, resource, extension):
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
73 """Flag a given resource as supporting a particular extension"""
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
74 self.resources[resource]['supports'].append(extension)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
75
2654
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
76 def supports(self, resource, extension):
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
77 """Check if a given resource supports a particular extension
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
78
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
79 If no resource is specified, check the resource with the highest
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
80 priority among currently connected.
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
81
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
82 """
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
83 if resource and resource in self.resources:
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
84 return extension in self.resources[resource]['supports']
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
85 else:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
86 resource = self.max_prio_resource()
2686
248489d28118 Make a conditional a little bit more pretty. ;)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2684
diff changeset
87 return resource and extension in resource['supports']
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
88
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
89 def max_prio_resource(self):
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
90 """Returns the resource (dict) with the highest priority
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
91
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
92 @return: highest priority resource or None if contacts is offline
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
93 @rtype: dict or None
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
94
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
95 """
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
96 if not self.resources:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
97 return None
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
98
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
99 # Priority can't be lower than -128
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
100 max_prio = -129
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
101 selected = None
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
102
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
103 for resource in self.resources.itervalues():
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
104 # TODO: check RFC for behaviour of 2 resources with the same priority
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
105 if resource['priority'] > max_prio:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
106 max_prio = resource['priority']
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
107 selected = resource
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
108
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
109 return selected
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
110
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
111 def remove_resource(self, resource):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
112 """Removes information about a connected resource
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
113
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
114 @param resource: resource name
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
115
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
116 """
2085
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
117 if self.resources.has_key(resource):
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
118 del self.resources[resource]
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
119 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
120 raise ValueError("No such resource!")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
121
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
122 if not self.resources:
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
123 self.last_online = time.time()
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
124
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
125 def is_dnd(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
126 """Checks if contact is DoNotDisturb
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
127
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
128 The contact is DND if its resource with the highest priority is DND
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
129
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
130 """
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
131 max_prio_res = self.max_prio_resource()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
132
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
133 # If there are no resources the contact is offline, not dnd
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
134 if max_prio_res:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
135 return max_prio_res['show'] == u"dnd"
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
136 else:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
137 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
138
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
139 def set_show(self, resource, show):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
140 """Sets show property for a given resource
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
141
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
142 @param resource: resource to alter
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
143 @param show: new value of the show property
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
144 @raise ValueError: no resource with given name has been found
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
145
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
146 """
2085
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
147 if self.resources.has_key(resource):
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
148 self.resources[resource]['show'] = show
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
149 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
150 raise ValueError("There's no such resource")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
151
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
152 def uses_resource(self, resource):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
153 """Checks if contact uses a given resource"""
2085
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
154 return self.resources.has_key(resource)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
155
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
156 def __str__(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
157 retval = "%s (%s) has %d queued messages"
2085
d72f126157f7 Make Contact.resources a dictionary, fix a bug in updating of show property
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
158 res = ", ".join([name + " is " + res['show'] for name, res in self.resources.items()])
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
159 return retval % (self.jid.as_unicode(), res, len(self.messages))
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
160
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
161 class XMPPBot(Client, Thread):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
162 """A simple XMPP bot"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
163
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
164 def __init__(self, config, from_commands, to_commands):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
165 """A constructor
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
166
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
167 @param from_commands: a Queue object used to send commands to other (xmlrpc) threads
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
168 @param to_commands: a Queue object used to receive commands from other threads
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
169
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
170 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
171 Thread.__init__(self)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
172
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
173 self.from_commands = from_commands
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
174 self.to_commands = to_commands
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
175 jid = u"%s@%s/%s" % (config.xmpp_node, config.xmpp_server, config.xmpp_resource)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
176
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
177 self.config = config
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
178 self.log = logging.getLogger("log")
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
179 self.jid = JID(node_or_jid=jid, domain=config.xmpp_server, resource=config.xmpp_resource)
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
180 self.tlsconfig = TLSSettings(require = True, verify_peer=False)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
181
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
182 # A dictionary of contact objects, ordered by bare JID
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
183 self.contacts = {}
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
184
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
185 # The last time when contacts were checked for expiration, in seconds
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
186 self.last_expiration = time.time()
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
187
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
188 # How often should the contacts be checked for expiration, in seconds
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
189 self.contact_check = 600
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
190 self.stopping = False
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
191
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
192 self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML, cmd.GetPageList, cmd.GetPageInfo, cmd.Search, cmd.RevertPage]
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
193 self.internal_commands = ["ping", "help", "searchform"]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
194
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
195 self.xmlrpc_commands = {}
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
196 for command, name in [(command, command.__name__) for command in self.known_xmlrpc_cmds]:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
197 self.xmlrpc_commands[name.lower()] = command
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
198
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
199 Client.__init__(self, self.jid, config.xmpp_password, config.xmpp_server, tls_settings=self.tlsconfig)
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
200
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
201 def run(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
202 """Start the bot - enter the event loop"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
203
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
204 self.log.info("Starting the jabber bot.")
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
205 self.connect()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
206 self.loop()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
207
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
208 def stop(self):
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
209 """Stop the thread"""
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
210 self.stopping = True
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
211
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
212 def loop(self, timeout=1):
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
213 """Main event loop - stream and command handling"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
214
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
215 while True:
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
216 if self.stopping:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
217 break
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
218
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
219 stream = self.get_stream()
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
220 if not stream:
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
221 break
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
222
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
223 act = stream.loop_iter(timeout)
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
224 if not act:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
225 # Process all available commands
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
226 while self.poll_commands(): pass
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
227 self.idle()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
228
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
229 def idle(self):
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
230 """Do some maintenance"""
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
231
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
232 Client.idle(self)
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
233
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
234 current_time = time.time()
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
235 if self.last_expiration + self.contact_check < current_time:
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
236 self.expire_contacts(current_time)
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
237 self.last_expiration = current_time
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
238
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
239 def expire_contacts(self, current_time):
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
240 """Check which contats have been offline for too long and should be removed
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
241
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
242 @param current_time: current time in seconds
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
243
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
244 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
245 for jid, contact in self.contacts.items():
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
246 if not contact.is_valid(current_time):
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
247 del self.contacts[jid]
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
248
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
249 def get_text(self, jid):
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
250 """Returns a gettext function (_) for the given JID
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
251
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
252 @param jid: bare Jabber ID of the user we're going to communicate with
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
253 @type jid: str or pyxmpp.jid.JID
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
254
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
255 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
256 language = "en"
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
257 if isinstance(jid, str) or isinstance(jid, unicode):
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
258 jid = JID(jid).bare().as_unicode()
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
259 else:
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
260 jid = jid.bare().as_unicode()
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
261
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
262 if jid in self.contacts:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
263 language = self.contacts[jid].language
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
264
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
265 return lambda text: i18n.get_text(text, lang=language)
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
266
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
267 def poll_commands(self):
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
268 """Checks for new commands in the input queue and executes them
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
269
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
270 @return: True if any command has been executed, False otherwise.
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
271
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
272 """
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
273 try:
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
274 command = self.to_commands.get_nowait()
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
275 self.handle_command(command)
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
276 return True
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
277 except Queue.Empty:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
278 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
279
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
280 # XXX: refactor this, if-elif sequence is already too long
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
281 def handle_command(self, command, ignore_dnd=False):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
282 """Excecutes commands from other components
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
283
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
284 @param command: a command to execute
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
285 @type command: any class defined in commands.py (FIXME?)
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
286 @param ignore_dnd: if command results in user interaction, should DnD be ignored?
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
287
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
288 """
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
289 # Handle normal notifications
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
290 if isinstance(command, cmd.NotificationCommand):
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
291 cmd_data = command.notification
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
292 original_text = cmd_data.get('text', '')
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
293 original_subject = cmd_data.get('subject', '')
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
294
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
295 for recipient in command.jids:
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
296 jid = JID(recipient)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
297 jid_text = jid.bare().as_unicode()
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
298
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
299 if isinstance(command, cmd.NotificationCommandI18n):
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
300 # Translate&interpolate the message with data
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
301 gettext_func = self.get_text(jid_text)
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
302 text, subject = command.translate(gettext_func)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
303 cmd_data['text'] = text
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
304 cmd_data['subject'] = subject
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
305 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
306 cmd_data['text'] = original_text
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
307 cmd_data['subject'] = original_subject
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
308
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
309 # Check if contact is DoNotDisturb.
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
310 # If so, queue the message for delayed delivery.
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
311 contact = self.contacts.get(jid_text, '')
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
312 if contact:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
313 if command.async and contact.is_dnd() and not ignore_dnd:
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
314 contact.messages.append(command)
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
315 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
316
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
317 action = cmd_data.get('action', '')
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
318 if action == u'page_changed':
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
319 self.handle_changed_action(cmd_data, jid, contact)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
320 elif action == u'page_deleted':
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
321 self.handle_deleted_action(cmd_data, jid, contact)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
322 elif action == u'file_attached':
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
323 self.handle_attached_action(cmd_data, jid, contact)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
324 elif action == u'page_renamed':
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
325 self.handle_renamed_action(cmd_data, jid, contact)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
326 elif action == u'user_created':
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
327 self.handle_user_created_action(cmd_data, jid, contact)
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
328 else:
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
329 self.send_message(jid, cmd_data, command.msg_type)
2483
41e79a4df6b6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2482
diff changeset
330
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
331 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
332
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
333 _ = self.get_text(command.jid)
2477
76f87ae87759 Initialize _ later, as NotificationCommand doesn't have `jid` attr
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2474
diff changeset
334
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
335 # Handle subscribtion management commands
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
336 if isinstance(command, cmd.AddJIDToRosterCommand):
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
337 jid = JID(node_or_jid=command.jid)
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
338 self.ask_for_subscription(jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
339
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
340 elif isinstance(command, cmd.RemoveJIDFromRosterCommand):
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
341 jid = JID(node_or_jid=command.jid)
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
342 self.remove_subscription(jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
343
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
344 elif isinstance(command, cmd.GetPage) or isinstance(command, cmd.GetPageHTML):
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
345 msg = _(u"""Here's the page "%(pagename)s" that you've requested:\n\n%(data)s""")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
346
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
347 cmd_data = {'text': msg % {'pagename': command.pagename, 'data': command.data}}
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
348 self.send_message(command.jid, cmd_data)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
349
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
350 elif isinstance(command, cmd.GetPageList):
2500
7203f7cc4201 Fix a typo.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2488
diff changeset
351 msg = _("That's the list of pages accesible to you:\n\n%s")
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
352 pagelist = u"\n".join(command.data)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
353
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
354 self.send_message(command.jid, {'text': msg % (pagelist, )})
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
355
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
356 elif isinstance(command, cmd.GetPageInfo):
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
357 self.handle_page_info(command)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
358
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
359 elif isinstance(command, cmd.GetUserLanguage):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
360 if command.jid in self.contacts:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
361 self.contacts[command.jid].language = command.language
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
362
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
363 elif isinstance(command, cmd.Search):
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
364 warnings = []
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
365 if not command.data:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
366 warnings.append(_("There are no pages matching your search criteria!"))
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
367
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
368 # This hardcoded limitation relies on (mostly correct) assumption that Jabber
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
369 # servers have rather tight traffic limits. Sending more than 25 results is likely
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
370 # to take a second or two - users should not have to wait longer (+search time!).
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
371 elif len(command.data) > 25:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
372 warnings.append(_("There are too many results (%(number)s). Limiting to first 25 entries.") % {'number': str(len(command.data))})
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
373 command.data = command.data[:25]
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
374
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
375 results = [{'description': result[0], 'url': result[2]} for result in command.data]
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
376
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
377 if command.presentation == u"text":
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
378 for warning in warnings:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
379 self.send_message(command.jid, {'text': warning})
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
380
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
381 if not results:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
382 return
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
383
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
384 data = {'text': _('Following pages match your search criteria:'), 'url_list': results}
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
385 self.send_message(command.jid, data, u"chat")
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
386 else:
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
387 form_title = _("Search results").encode("utf-8")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
388
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
389 warnings = []
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
390 for no, warning in enumerate(warnings):
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
391 field = forms.Field(name="warning", field_type="fixed", value=warning)
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
392 warnings.append(forms.Item([field]))
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
393
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
394 reported = [forms.Field(name="url", field_type="text-single"), forms.Field(name="description", field_type="text-single")]
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
395 if warnings:
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
396 reported.append(forms.Field(name="warning", field_type="fixed"))
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
397
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
398 form = forms.Form(xmlnode_or_type="result", title=form_title, reported_fields=reported)
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
399
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
400 for no, result in enumerate(results):
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
401 url = forms.Field(name="url", value=result["url"], field_type="text-single")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
402 description = forms.Field(name="description", value=result["description"], field_type="text-single")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
403 item = forms.Item([url, description])
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
404 form.add_item(item)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
405
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
406 self.send_form(command.jid, form, _("Search results"))
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
407
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
408 def handle_changed_action(self, cmd_data, jid, contact):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
409 """Handles a notification command with 'page_changed' action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
410
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
411 @param cmd_data: notification command data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
412 @param jid: jid to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
413 @param contact: a roster contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
414 @type cmd_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
415 @type jid: pyxmpp.jid.JID
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
416 @type contact: Contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
417
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
418 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
419 if contact and contact.supports(jid.resource, u"jabber:x:data"):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
420 self.send_change_form(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
421 return
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
422 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
423 self.send_change_text(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
424
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
425 def handle_deleted_action(self, cmd_data, jid, contact):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
426 """Handles a notification cmd_data with 'page_deleted' action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
427
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
428 @param cmd_data: notification cmd_data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
429 @param jid: jid to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
430 @param contact: a roster contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
431 @type cmd_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
432 @type jid: pyxmpp.jid.JID
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
433 @type contact: Contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
434
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
435 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
436 if contact and contact.supports(jid.resource, u"jabber:x:data"):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
437 self.send_deleted_form(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
438 return
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
439 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
440 self.send_deleted_text(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
441
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
442
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
443 def handle_attached_action(self, cmd_data, jid, contact):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
444 """Handles a notification cmd_data with 'file_attached' action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
445
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
446 @param cmd_data: notification cmd_data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
447 @param jid: jid to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
448 @param contact: a roster contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
449 @type cmd_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
450 @type jid: pyxmpp.jid.JID
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
451 @type contact: Contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
452
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
453 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
454 if contact and contact.supports(jid.resource, u"jabber:x:data"):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
455 self.send_attached_form(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
456 return
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
457 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
458 self.send_attached_text(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
459
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
460 def handle_renamed_action(self, cmd_data, jid, contact):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
461 """Handles a notification cmd_data with 'page_renamed' action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
462
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
463 @param cmd_data: notification cmd_data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
464 @param jid: jid to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
465 @param contact: a roster contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
466 @type cmd_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
467 @type jid: pyxmpp.jid.JID
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
468 @type contact: Contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
469
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
470 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
471 if contact and contact.supports(jid.resource, u"jabber:x:data"):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
472 self.send_renamed_form(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
473 return
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
474 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
475 self.send_renamed_text(jid.as_unicode(), cmd_data)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
476
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
477 def handle_user_created_action(self, cmd_data, jid, contact):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
478 """Handles a notification cmd_data with 'user_created' action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
479
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
480 @param cmd_data: notification cmd_data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
481 @param jid: jid to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
482 @param contact: a roster contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
483 @type cmd_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
484 @type jid: pyxmpp.jid.JID
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
485 @type contact: Contact
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
486
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
487 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
488 raise NotImplemented()
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
489
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
490 def ask_for_subscription(self, jid):
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
491 """Sends a <presence/> stanza with type="subscribe"
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
492
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
493 Bot tries to subscribe to every contact's presence, so that
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
494 it can honor special cases, like DoNotDisturb setting.
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
495
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
496 @param jid: Jabber ID of entity we're subscribing to
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
497 @type jid: pyxmpp.jid.JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
498
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
499 """
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
500 stanza = Presence(to_jid=jid, stanza_type="subscribe")
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
501 self.get_stream().send(stanza)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
502
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
503 def remove_subscription(self, jid):
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
504 """Sends a <presence/> stanza with type="unsubscribed
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
505
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
506 @param jid: Jabber ID of entity whose subscription we cancel
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
507 @type jid: JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
508
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
509 """
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
510 stanza = Presence(to_jid=jid, stanza_type="unsubscribed")
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
511 self.get_stream().send(stanza)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
512
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
513 def send_message(self, jid_text, data, msg_type=u"chat"):
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
514 """Sends a message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
515
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
516 @param jid_text: JID to send the message to
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
517 @param data: dictionary containing notification data
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
518 @param msg_type: message type, as defined in RFC
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
519 @type jid_text: unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
520
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
521 """
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
522 use_oob = False
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
523 subject = data.get('subject', '')
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
524 jid = JID(jid_text)
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
525
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
526 if data.has_key('url_list') and data['url_list']:
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
527 jid_bare = jid.bare().as_unicode()
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
528 contact = self.contacts.get(jid_bare, None)
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
529 if contact and contact.supports(jid.resource, u'jabber:x:oob'):
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
530 use_oob = True
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
531 else:
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
532 url_strings = ['%s - %s' % (entry['url'], entry['description']) for entry in data['url_list']]
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
533
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
534 # Insert a newline, so that the list of URLs doesn't start in the same
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
535 # line as the rest of message text
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
536 url_strings.insert(0, '\n')
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
537 data['text'] = data['text'] + '\n'.join(url_strings)
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
538
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
539 message = Message(to_jid=jid, body=data['text'], stanza_type=msg_type, subject=subject)
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
540
2655
9efa3194636d Pass URLs using OOB only if it's known to be supported by the client.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2654
diff changeset
541 if use_oob:
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
542 oob.add_urls(message, data['url_list'])
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
543
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
544 self.get_stream().send(message)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
545
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
546 def send_form(self, jid, form, subject, url_list=[]):
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
547 """Send a data form
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
548
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
549 @param jid: jid to send the form to (full)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
550 @param form: the form to send
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
551 @param subject: subject of the message
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
552 @param url_list: list of urls to use with OOB
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
553 @type jid: unicode
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
554 @type form: pyxmpp.jabber.dataforms.Form
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
555 @type subject: unicode
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
556 @type url_list: list
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
557
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
558 """
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
559 if not isinstance(form, forms.Form):
2668
165c3f2950dc Fix a typo and an error introduced in recent refactoring.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2664
diff changeset
560 raise ValueError("The 'form' argument must be of type pyxmpp.jabber.dataforms.Form!")
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
561
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
562 _ = self.get_text(JID(jid).bare().as_unicode())
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
563
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
564 warning = _("If you see this, your client probably doesn't support Data Forms.")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
565 message = Message(to_jid=jid, body=warning, subject=subject)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
566 message.add_content(form)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
567
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
568 if url_list:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
569 oob.add_urls(message, url_list)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
570
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
571 self.get_stream().send(message)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
572
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
573 def send_search_form(self, jid):
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
574 _ = self.get_text(jid)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
575
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
576 # These encode()s may look weird, but due to some pyxmpp oddness we have
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
577 # to provide an utf-8 string instead of unicode. Bug reported, patches submitted...
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
578 form_title = _("Wiki search").encode("utf-8")
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
579 help_form = _("Submit this form to perform a wiki search").encode("utf-8")
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
580 search_type1 = _("Title search")
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
581 search_type2 = _("Full-text search")
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
582 search_label = _("Search type")
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
583 search_label2 = _("Search text")
2661
9f6e830c82e0 Add more fields to search form, fix bug left from changes in service discovery.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
584 case_label = _("Case-sensitive search")
9f6e830c82e0 Add more fields to search form, fix bug left from changes in service discovery.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
585 regexp_label = _("Treat terms as regular expressions")
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
586 forms_warn = _("If you see this, your client probably doesn't support Data Forms.")
2483
41e79a4df6b6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2482
diff changeset
587
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
588 title_search = forms.Option("t", search_type1)
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
589 full_search = forms.Option("f", search_type2)
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
590
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
591 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=help_form)
2663
c1f0d6ec3b55 Add a hidden action field to the search form.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2662
diff changeset
592 form.add_field(name="action", field_type="hidden", value="search")
2661
9f6e830c82e0 Add more fields to search form, fix bug left from changes in service discovery.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
593 form.add_field(name="case", field_type="boolean", label=case_label)
9f6e830c82e0 Add more fields to search form, fix bug left from changes in service discovery.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
594 form.add_field(name="regexp", field_type="boolean", label=regexp_label)
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
595 form.add_field(name="search_type", options=[title_search, full_search], field_type="list-single", label=search_label)
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
596 form.add_field(name="search", field_type="text-single", label=search_label2)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
597
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
598 self.send_form(jid, form, _("Wiki search"))
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
599
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
600 def send_change_form(self, jid, msg_data):
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
601 """Sends a page change notification using Data Forms
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
602
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
603 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
604 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
605 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
606 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
607
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
608 """
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
609 _ = self.get_text(jid)
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
610
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
611 form_title = _("Page changed notification").encode("utf-8")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
612 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
613 action_label = _("What to do next")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
614
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
615 action1 = _("Do nothing")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
616 action2 = _("Revert change")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
617 action3 = _("View page info")
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
618
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
619 do_nothing = forms.Option("n", action1)
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
620 revert = forms.Option("r", action2)
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
621 view_info = forms.Option("v", action3)
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
622
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
623 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
624 form.add_field(name='page_name', field_type='hidden', value=msg_data['page_name'])
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
625 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
626 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
627
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
628 # Add lines of text as separate values, as recommended in XEP
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
629 diff_lines = msg_data['diff'].split('\n')
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
630 form.add_field(name="diff", field_type="text-multi", values=diff_lines, label=("Diff"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
631
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
632 full_jid = JID(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
633 bare_jid = full_jid.bare().as_unicode()
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
634 resource = full_jid.resource
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
635
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
636 # Add URLs as OOB data if it's supported and as separate fields otherwise
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
637 if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
638 url_list = msg_data['url_list']
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
639 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
640 url_list = []
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
641
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
642 for number, url in enumerate(msg_data['url_list']):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
643 field_name = "url%d" % (number, )
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
644 form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
645
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
646 # Selection of a following action
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
647 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info], label=action_label)
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
648
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
649 self.send_form(jid, form, _("Page change notification"), url_list)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
650
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
651 def send_change_text(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
652 """Sends a simple, text page change notification
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
653
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
654 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
655 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
656 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
657 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
658
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
659 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
660 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
661 separator = '-' * 78
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
662 urls_text = '\n'.join(["%s - %s" % (url["description"], url["url"]) for url in msg_data['url_list']])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
663 message = _("%(preamble)s\nComment: %(comment)s\n%(separator)s\n%(diff)s\n%(separator)s\n%(links)s") % {
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
664 'preamble': msg_data['text'],
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
665 'separator': separator,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
666 'diff': msg_data['diff'],
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
667 'comment': msg_data.get('comment', _('no comment')),
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
668 'links': urls_text,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
669 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
670
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
671 data = {'text': message, 'subject': msg_data.get('subject', '')}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
672 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
673
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
674 def send_deleted_form(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
675 """Sends a page deleted notification using Data Forms
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
676
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
677 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
678 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
679 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
680 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
681
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
682 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
683 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
684
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
685 form_title = _("Page deletion notification").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
686 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
687 action_label = _("What to do next")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
689 action1 = _("Do nothing")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
690 action2 = _("Revert change")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
691 action3 = _("View page info")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
692
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
693 do_nothing = forms.Option("n", action1)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
694 revert = forms.Option("r", action2)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
695 view_info = forms.Option("v", action3)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
696
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
697 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
698 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
699 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
700
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
701 full_jid = JID(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
702 bare_jid = full_jid.bare().as_unicode()
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
703 resource = full_jid.resource
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
704
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
705 # Add URLs as OOB data if it's supported and as separate fields otherwise
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
706 if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
707 url_list = msg_data['url_list']
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
708 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
709 url_list = []
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
710
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
711 for number, url in enumerate(msg_data['url_list']):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
712 field_name = "url%d" % (number, )
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
713 form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
714
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
715 # Selection of a following action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
716 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info], label=action_label)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
717
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
718 self.send_form(jid, form, _("Page deletion notification"), url_list)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
719
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
720 def send_deleted_text(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
721 """Sends a simple, text page deletion notification
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
722
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
723 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
724 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
725 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
726 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
727
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
728 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
729 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
730 separator = '-' * 78
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
731 urls_text = '\n'.join(["%s - %s" % (url["description"], url["url"]) for url in msg_data['url_list']])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
732 message = _("%(preamble)s\nComment: %(comment)s\n%(separator)s\n%(links)s") % {
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
733 'preamble': msg_data['text'],
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
734 'separator': separator,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
735 'comment': msg_data.get('comment', _('no comment')),
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
736 'links': urls_text,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
737 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
738
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
739 data = {'text': message, 'subject': msg_data.get('subject', '')}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
740 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
741
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
742 def send_attached_form(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
743 """Sends a new attachment notification using Data Forms
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
744
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
745 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
746 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
747 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
748 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
749
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
750 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
751 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
752
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
753 form_title = _("File attached notification").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
754 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
755 action_label = _("What to do next")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
756
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
757 action1 = _("Do nothing")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
758 action2 = _("Revert change")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
759 action3 = _("View page info")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
760
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
761 do_nothing = forms.Option("n", action1)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
762 revert = forms.Option("r", action2)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
763 view_info = forms.Option("v", action3)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
764
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
765 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
766 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
767 form.add_field(name='page', field_type='text-single', value=msg_data['page_name'], label=_("Page name"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
768 form.add_field(name='name', field_type='text-single', value=msg_data['attach_name'], label=_("File name"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
769 form.add_field(name='size', field_type='text-single', value=msg_data['attach_size'], label=_("File size"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
770
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
771 full_jid = JID(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
772 bare_jid = full_jid.bare().as_unicode()
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
773 resource = full_jid.resource
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
774
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
775 # Add URLs as OOB data if it's supported and as separate fields otherwise
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
776 if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
777 url_list = msg_data['url_list']
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
778 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
779 url_list = []
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
780
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
781 for number, url in enumerate(msg_data['url_list']):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
782 field_name = "url%d" % (number, )
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
783 form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
784
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
785 # Selection of a following action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
786 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info], label=action_label)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
787
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
788 self.send_form(jid, form, _("File attached notification"), url_list)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
789
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
790 def send_attached_text(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
791 """Sends a simple, text page deletion notification
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
792
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
793 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
794 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
795 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
796 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
797
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
798 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
799 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
800 separator = '-' * 78
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
801 urls_text = '\n'.join(["%s - %s" % (url["description"], url["url"]) for url in msg_data['url_list']])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
802 message = _("%(preamble)s\n%(separator)s\n%(links)s") % {
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
803 'preamble': msg_data['text'],
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
804 'separator': separator,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
805 'links': urls_text,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
806 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
807
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
808 data = {'text': message, 'subject': msg_data['subject']}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
809 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
810
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
811 def send_renamed_form(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
812 """Sends a page rename notification using Data Forms
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
813
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
814 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
815 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
816 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
817 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
818
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
819 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
820 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
821
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
822 form_title = _("Page rename notification").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
823 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
824 action_label = _("What to do next")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
825
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
826 action1 = _("Do nothing")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
827 action2 = _("Revert change")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
828 action3 = _("View page info")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
829
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
830 do_nothing = forms.Option("n", action1)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
831 revert = forms.Option("r", action2)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
832 view_info = forms.Option("v", action3)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
833
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
834 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
835 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
836 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
837 form.add_field(name='old', field_type='text-single', value=msg_data['old_name'], label=_("Old name"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
838 form.add_field(name='new', field_type='text-single', value=msg_data['page_name'], label=_("New name"))
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
839
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
840 full_jid = JID(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
841 bare_jid = full_jid.bare().as_unicode()
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
842 resource = full_jid.resource
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
843
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
844 # Add URLs as OOB data if it's supported and as separate fields otherwise
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
845 if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
846 url_list = msg_data['url_list']
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
847 else:
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
848 url_list = []
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
849
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
850 for number, url in enumerate(msg_data['url_list']):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
851 field_name = "url%d" % (number, )
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
852 form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
853
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
854 # Selection of a following action
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
855 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info], label=action_label)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
856
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
857 self.send_form(jid, form, _("Page rename notification"), url_list)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
858
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
859 def send_renamed_text(self, jid, msg_data):
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
860 """Sends a simple, text page rename notification
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
861
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
862 @param jid: a Jabber ID to send the notification to
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
863 @type jid: unicode
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
864 @param msg_data: dictionary with notification data
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
865 @type msg_data: dict
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
866
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
867 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
868 _ = self.get_text(jid)
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
869 separator = '-' * 78
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
870 urls_text = '\n'.join(["%s - %s" % (url["description"], url["url"]) for url in msg_data['url_list']])
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
871 message = _("%(preamble)s\nComment: %(comment)s\n%(separator)s\n%(links)s") % {
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
872 'preamble': msg_data['text'],
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
873 'separator': separator,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
874 'comment': msg_data.get('comment', _('no comment')),
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
875 'links': urls_text,
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
876 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
877
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
878 data = {'text': message, 'subject': msg_data['subject']}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
879 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
880
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
881 def handle_page_info(self, command):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
882 """Handles GetPageInfo commands
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
883
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
884 @param command: a command instance
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
885 @type command: jabberbot.commands.GetPageInfo
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
886
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
887 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
888 # Process command data first so it can be directly usable
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
889 if command.data['author'].startswith("Self:"):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
890 command.data['author'] = command.data['author'][5:]
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
891
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
892 datestr = str(command.data['lastModified'])
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
893 command.data['lastModified'] = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
894 'year': datestr[:4],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
895 'month': datestr[4:6],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
896 'day': datestr[6:8],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
897 'time': datestr[9:17],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
898 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
899
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
900 if command.presentation == u"text":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
901 self.send_pageinfo_text(command)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
902 elif command.presentation == u"dataforms":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
903 self.send_pageinfo_form(command)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
904
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
905 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
906 raise ValueError("presentation value '%s' is not supported!" % (command.presentation, ))
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
907
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
908 def send_pageinfo_text(self, command):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
909 """Sends detailed page info with plain text
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
910
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
911 @param command: command with detailed data
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
912 @type command: jabberbot.command.GetPageInfo
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
913
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
914 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
915 _ = self.get_text(command.jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
916
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
917 intro = _("""Following detailed information on page "%(pagename)s" \
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
918 is available:""")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
919
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
920 msg = _("""Last author: %(author)s
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
921 Last modification: %(modification)s
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
922 Current version: %(version)s""") % {
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
923 'author': command.data['author'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
924 'modification': command.data['lastModified'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
925 'version': command.data['version'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
926 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
927
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
928 self.send_message(command.jid, {'text': intro % {'pagename': command.pagename}})
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
929 self.send_message(command.jid, {'text': msg})
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
930
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
931 def send_pageinfo_form(self, command):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
932 """Sends page info using Data Forms
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
933
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
934
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
935 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
936 _ = self.get_text(command.jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
937 data = command.data
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
938
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
939 form_title = _("Detailed page information").encode("utf-8")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
940 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
941 action_label = _("What to do next")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
942
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
943 action1 = _("Do nothing")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
944 action2 = _("Get page contents")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
945 action3 = _("Get page contents (HTML)")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
946
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
947 do_nothing = forms.Option("n", action1)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
948 get_content = forms.Option("c", action2)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
949 get_content_html = forms.Option("h", action3)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
950
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
951 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
952 form.add_field(name='pagename', field_type='text-single', value=command.pagename, label=_("Page name"))
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
953 form.add_field(name="changed", field_type='text-single', value=data['lastModified'], label=_("Last changed"))
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
954 form.add_field(name='editor', field_type='text-single', value=data['author'], label=_("Last editor"))
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
955 form.add_field(name='version', field_type='text-single', value=data['version'], label=_("Current version"))
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
956
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
957 # full_jid = JID(jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
958 # bare_jid = full_jid.bare().as_unicode()
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
959 # resource = full_jid.resource
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
960
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
961 # Add URLs as OOB data if it's supported and as separate fields otherwise
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
962 # if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
963 # url_list = msg_data['url_list']
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
964 # else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
965 # url_list = []
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
966 #
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
967 # for number, url in enumerate(msg_data['url_list']):
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
968 # field_name = "url%d" % (number, )
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
969 # form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
970
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
971 # Selection of a following action
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
972 form.add_field(name="options", field_type="list-single", options=[do_nothing, get_content, get_content_html], label=action_label)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
973
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
974 self.send_form(command.jid, form, _("Detailed page information"))
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
975
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
976 def is_internal(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
977 """Check if a given command is internal
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
978
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
979 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
980
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
981 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
982 for internal_cmd in self.internal_commands:
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
983 if internal_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
984 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
985
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
986 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
987
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
988 def is_xmlrpc(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
989 """Checks if a given commands requires interaction via XMLRPC
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
990
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
991 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
992
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
993 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
994 for xmlrpc_cmd in self.xmlrpc_commands:
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
995 if xmlrpc_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
996 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
997
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
998 return False
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
999
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1000 def contains_form(self, message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1001 """Checks if passed message stanza contains a submitted form and parses it
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1002
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1003 @param message: message stanza
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1004 @type message: pyxmpp.message.Message
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1005 @return: xml node with form data if found, or None
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1006
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1007 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1008 if not isinstance(message, Message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1009 raise ValueError("The 'message' parameter must be of type pyxmpp.message.Message!")
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1010
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1011 payload = message.get_node()
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1012 form = message.xpath_eval('/ns:message/data:x', {'data': 'jabber:x:data'})
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1013
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1014 if form:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1015 return form[0]
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1016 else:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1017 return None
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1018
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1019 def handle_form(self, jid, form_node):
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1020 """Handles a submitted data form
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1021
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1022 @param jid: jid that submitted the form (full jid)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1023 @type jid: pyxmpp.jid.JID
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1024 @param form_node: a xml node with data form
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1025 @type form_node: libxml2.xmlNode
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1026
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1027 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1028 if not isinstance(form_node, libxml2.xmlNode):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1029 raise ValueError("The 'form' parameter must be of type libxml2.xmlNode!")
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1030
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1031 if not isinstance(jid, JID):
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1032 raise ValueError("The 'jid' parameter must be of type jid!")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1033
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1034 _ = self.get_text(jid.bare().as_unicode())
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1035
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1036 form = forms.Form(form_node)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1037
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1038 if form.type != u"submit":
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1039 return
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1040
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1041 if "action" in form:
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1042 action = form["action"].value
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1043 if action == u"search":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1044 self.handle_search_form(jid, form)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1045 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1046 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1047 self.send_message(jid.as_unicode(), data, u"message")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1048 elif "options" in form:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1049 option = form["options"].value
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1050
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1051 # View page info
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1052 if option == "v":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1053 command = cmd.GetPageInfo(jid.as_unicode(), form["page_name"].value, presentation="dataforms")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1054 self.from_commands.put_nowait(command)
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1055
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1056
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1057 def handle_search_form(self, jid, form):
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1058 """Handles a search form
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1059
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1060 @param jid: jid that submitted the form
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1061 @type jid: pyxmpp.jid.JID
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1062 @param form: a form object
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1063 @type form_node: pyxmpp.jabber.dataforms.Form
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1064
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1065 """
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1066 required_fields = ["case", "regexp", "search_type", "search"]
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1067 jid_text = jid.bare().as_unicode()
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1068 _ = self.get_text(jid_text)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1069
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1070 for field in required_fields:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1071 if field not in form:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1072 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1073 self.send_message(jid.as_unicode(), data, u"message")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1074
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1075 case_sensitive = form['case'].value
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1076 regexp_terms = form['regexp'].value
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1077 if form['search_type'].value == 't':
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1078 search_type = 'title'
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1079 else:
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1080 search_type = 'text'
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1081
2682
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
1082 command = cmd.Search(jid.as_unicode(), search_type, form["search"].value, case=form['case'].value,
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1083 regexp=form['regexp'].value, presentation='dataforms')
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1084 self.from_commands.put_nowait(command)
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1085
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1086 def handle_message(self, message):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1087 """Handles incoming messages
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1088
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1089 @param message: a message stanza to parse
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1090 @type message: pyxmpp.message.Message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1091
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1092 """
2076
0128bbaf0172 Add newlines at the end of files, fix some overzealous deletion.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2070
diff changeset
1093 if self.config.verbose:
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
1094 msg = "Message from %s." % (message.get_from_jid().as_unicode(), )
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1095 self.log.debug(msg)
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1096
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1097 form = self.contains_form(message)
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1098 if form:
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1099 self.handle_form(message.get_from_jid(), form)
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1100 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1101
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1102 text = message.get_body()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1103 sender = message.get_from_jid()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1104 if text:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1105 command = text.split()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1106 command[0] = command[0].lower()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1107 else:
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1108 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1109
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1110 if self.is_internal(command[0]):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1111 response = self.handle_internal_command(sender, command)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1112 elif self.is_xmlrpc(command[0]):
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1113 response = self.handle_xmlrpc_command(sender, command)
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1114 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1115 response = self.reply_help(sender)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1116
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1117 if response:
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
1118 self.send_message(sender, {'text': response})
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1119
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1120 def handle_internal_command(self, sender, command):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1121 """Handles internal commands, that can be completed by the XMPP bot itself
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1122
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1123 @param command: list representing a command
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1124 @param sender: JID of sender
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1125 @type sender: pyxmpp.jid.JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1126
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1127 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
1128 _ = self.get_text(sender)
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1129
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1130 if command[0] == "ping":
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1131 return "pong"
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1132 elif command[0] == "help":
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1133 if len(command) == 1:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1134 return self.reply_help(sender)
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1135 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1136 return self.help_on(sender, command[1])
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1137 elif command[0] == "searchform":
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
1138 jid = sender.bare().as_unicode()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1139 resource = sender.resource
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1140
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1141 # Assume that outsiders know what they are doing. Clients that don't support
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1142 # data forms should display a warning passed in message <body>.
2661
9f6e830c82e0 Add more fields to search form, fix bug left from changes in service discovery.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
1143 if jid not in self.contacts or self.contacts[jid].supports(resource, u"jabber:x:data"):
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1144 self.send_search_form(sender)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1145 else:
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1146 msg = {'text': _("This command requires a client supporting Data Forms.")}
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1147 self.send_message(sender, msg, u"")
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1148 else:
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1149 # For unknown command return a generic help message
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1150 return self.reply_help(sender)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1151
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1152 def do_search(self, jid, search_type, presentation, *args):
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1153 """Performs a Wiki search of term
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1154
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1155 @param jid: Jabber ID of user performing a search
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1156 @type jid: pyxmpp.jid.JID
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1157 @param term: term to search for
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1158 @type term: unicode
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1159 @param search_type: type of search; either "text" or "title"
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1160 @type search_type: unicode
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1161 @param presentation: how to present the results; "text" or "dataforms"
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1162 @type presentation: unicode
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1163
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1164 """
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1165 search = cmd.Search(jid, search_type, presentation=presentation, *args)
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1166 self.from_commands.put_nowait(search)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1167
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1168 def help_on(self, jid, command):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1169 """Returns a help message on a given topic
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1170
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1171 @param command: a command to describe in a help message
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1172 @type command: str or unicode
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1173 @return: a help message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1174
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1175 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
1176 _ = self.get_text(jid)
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1177
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1178 if command == "help":
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1179 return _("""The "help" command prints a short, helpful message \
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1180 about a given topic or function.\n\nUsage: help [topic_or_function]""")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1181
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1182 elif command == "ping":
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1183 return _("""The "ping" command returns a "pong" message as soon \
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1184 as it's received.""")
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1185
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1186 elif command == "searchform":
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1187 return _("""searchform - perform a wiki search using a form""")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1188
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1189 # Here we have to deal with help messages of external (xmlrpc) commands
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1190 else:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1191 if command in self.xmlrpc_commands:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1192 classobj = self.xmlrpc_commands[command]
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1193 help_str = _(u"%(command)s - %(description)s\n\nUsage: %(command)s %(params)s")
2415
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1194 return help_str % {'command': command,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1195 'description': classobj.description,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1196 'params': classobj.parameter_list,
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1197 }
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1198 else:
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1199 return _("""Unknown command "%s" """) % (command, )
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1200
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1201 def handle_xmlrpc_command(self, sender, command):
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1202 """Creates a command object, and puts it the command queue
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1203
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1204 @param command: a valid name of available xmlrpc command
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
1205 @type command: list representing a command, name and parameters
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1206
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1207 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
1208 _ = self.get_text(sender)
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1209 command_class = self.xmlrpc_commands[command[0]]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1210
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1211 # Add sender's JID to the argument list
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
1212 command.insert(1, sender.as_unicode())
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1213
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1214 try:
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1215 instance = command_class.__new__(command_class)
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1216 instance.__init__(*command[1:])
2115