annotate jabberbot/xmppbot.py @ 2949:2def79ea092e

Pass the unimplemented action before a fix is ready.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Thu, 29 Nov 2007 18:06:56 +0100
parents 80cf17344f98
children bc85e5200dd3
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")
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
388 help_form = _("Submit this form to perform a wiki search").encode("utf-8")
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
389 form = forms.Form(xmlnode_or_type="result", title=form_title, instructions=help_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
390
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
391 action_label = _("What to do next")
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
392 do_nothing = forms.Option("n", _("Do nothing"))
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
393 search_again = forms.Option("s", _("Search again"))
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
394
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
395 for no, warning in enumerate(warnings):
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
396 form.add_field(name="warning", field_type="fixed", value=warning)
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
397
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
398 for no, result in enumerate(results):
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
399 field_name = "url%d" % (no, )
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
400 form.add_field(name=field_name, value=unicode(result["url"]), label=result["description"].encode("utf-8"), field_type="text-single")
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
401
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
402 # Selection of a following action
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
403 form.add_field(name="options", field_type="list-single", options=[do_nothing, search_again], label=action_label)
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
404
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 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
406
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
407 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
408 """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
409
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 @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
411 @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
412 @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
413 @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
414 @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
415 @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
416
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 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
419 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
420 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
421 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
422 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
423
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 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
425 """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
426
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 @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
428 @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
429 @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
430 @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
431 @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
432 @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
433
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 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
436 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
437 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
438 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
439 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
440
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 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
443 """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
444
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 @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
446 @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
447 @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
448 @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
449 @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
450 @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
451
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 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
454 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
455 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
456 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
457 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
458
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 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
460 """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
461
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 @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
463 @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
464 @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
465 @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
466 @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
467 @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
468
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 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
471 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
472 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
473 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
474 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
475
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 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
477 """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
478
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 @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
480 @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
481 @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
482 @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
483 @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
484 @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
485
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 """
2949
2def79ea092e Pass the unimplemented action before a fix is ready.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
487 pass
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
488
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
489 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
490 """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
491
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
492 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
493 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
494
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
495 @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
496 @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
497
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
498 """
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 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
500 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
501
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
502 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
503 """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
504
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
505 @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
506 @type jid: JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
507
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
508 """
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 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
510 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
511
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
512 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
513 """Sends a message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
514
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
515 @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
516 @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
517 @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
518 @type jid_text: unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
519
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
520 """
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
521 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
522 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
523 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
524
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 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
526 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
527 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
528 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
529 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
530 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
531 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
532
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
533 # 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
534 # 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
535 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
536 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
537
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
538 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
539
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
540 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
541 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
542
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
543 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
544
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
545 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
546 """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
547
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 @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
549 @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
550 @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
551 @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
552 @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
553 @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
554 @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
555 @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
556
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 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
559 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
560
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 _ = 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
562
2696
89c40244fa6a Don't add the warning to messages with data forms. It can be shown with the form.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2695
diff changeset
563 message = Message(to_jid=jid, subject=subject)
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
564 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
565
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
566 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
567 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
568
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
569 self.get_stream().send(message)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
570
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
571 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
572 _ = self.get_text(jid)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
573
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
574 # 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
575 # 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
576 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
577 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
578 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
579 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
580 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
581 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
582 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
583 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
584 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
585
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
586 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
587 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
588
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
589 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
590 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
591 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
592 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
593 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
594 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
595
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
596 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
597
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 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
599 """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
600
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 @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
602 @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
603 @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
604 @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
605
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 """
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
607 _ = 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
608
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 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
610 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
611 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
612
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 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
614 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
615 action3 = _("View page info")
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
616 action4 = _("Perform a search")
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
617
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 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
619 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
620 view_info = forms.Option("v", action3)
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
621 search = forms.Option("s", action4)
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
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)
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
624 form.add_field(name='revision', field_type='hidden', value=msg_data['revision'])
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
625 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
626 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
627 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
628
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 # 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
630 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
631 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
632
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 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
634 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
635 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
636
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 # 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
638 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
639 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
640 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
641 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
642
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 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
644 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
645 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
646
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
647 # Selection of a following action
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
648 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info, search], label=action_label)
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
649
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
650 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
651
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 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
653 """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
654
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 @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
656 @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
657 @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
658 @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
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 """
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 _ = 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
662 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
663 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
664 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
665 '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
666 '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
667 '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
668 '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
669 '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
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
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 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
673 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
674
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 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
676 """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
677
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 @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
679 @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
680 @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
681 @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
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 """
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 _ = 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
685
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 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
687 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
688 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
689
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 action1 = _("Do nothing")
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
691 action2 = _("Perform a search")
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
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)
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
694 search = forms.Option("s", action2)
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
695
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 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
697 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
698 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
699
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 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
701 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
702 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
703
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 # 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
705 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
706 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
707 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
708 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
709
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 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
711 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
712 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
713
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 # Selection of a following action
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
715 form.add_field(name="options", field_type="list-single", options=[do_nothing, search], label=action_label)
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
716
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 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
718
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 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
720 """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
721
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 @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
723 @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
724 @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
725 @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
726
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 _ = 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
729 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
730 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
731 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
732 '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
733 '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
734 '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
735 '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
736 }
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 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
739 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
740
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 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
742 """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
743
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 @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
745 @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
746 @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
747 @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
748
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 _ = 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
751
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 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
753 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
754 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
755
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 action1 = _("Do nothing")
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
757 action2 = _("View page info")
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
758 action3 = _("Perform a search")
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
759
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 do_nothing = forms.Option("n", action1)
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
761 view_info = forms.Option("v", action2)
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
762 search = forms.Option("s", action3)
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
763
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 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
765 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
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
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
786 form.add_field(name="options", field_type="list-single", options=[do_nothing, view_info, search], label=action_label)
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
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")
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
829 action4 = _("Perform a search")
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
830
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 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
832 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
833 view_info = forms.Option("v", action3)
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
834 search = forms.Option("s", action4)
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
835
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 = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
837 form.add_field(name='revision', field_type='hidden', value=msg_data['revision'])
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
838 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
839 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
840 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
841 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
842 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
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 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
845 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
846 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
847
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 # 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
849 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
850 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
851 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
852 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
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 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
855 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
856 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
857
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 # Selection of a following action
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
859 form.add_field(name="options", field_type="list-single", options=[do_nothing, revert, view_info, search], label=action_label)
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
860
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 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
862
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 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
864 """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
865
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 @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
867 @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
868 @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
869 @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
870
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 """
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 _ = 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
873 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
874 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
875 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
876 '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
877 '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
878 '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
879 '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
880 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
881
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
882 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
883 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
884
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
885 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
886 """Handles GetPageInfo commands
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 @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
889 @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
890
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 # 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
893 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
894 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
895
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
896 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
897 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
898 'year': datestr[:4],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
899 'month': datestr[4:6],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
900 'day': datestr[6:8],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
901 'time': datestr[9:17],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
902 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
903
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
904 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
905 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
906 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
907 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
908
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
909 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
910 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
911
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
912 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
913 """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
914
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
915 @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
916 @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
917
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
918 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
919 _ = 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
920
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
921 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
922 is available:""")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
923
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
924 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
925 Last modification: %(modification)s
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
926 Current version: %(version)s""") % {
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
927 'author': command.data['author'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
928 'modification': command.data['lastModified'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
929 'version': command.data['version'],
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
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
932 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
933 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
934
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
935 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
936 """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
937
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 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
940 _ = 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
941 data = command.data
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 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
944 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
945 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
946
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
947 action1 = _("Do nothing")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
948 action2 = _("Get page contents")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
949 action3 = _("Get page contents (HTML)")
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
950 action4 = _("Perform a search")
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
951
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
952 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
953 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
954 get_content_html = forms.Option("h", action3)
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
955 search = forms.Option("s", action4)
2694
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 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
958 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
959 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
960 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
961 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
962
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
963 # full_jid = JID(jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
964 # 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
965 # resource = full_jid.resource
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 # 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
968 # 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
969 # 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
970 # else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
971 # url_list = []
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
972 #
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
973 # 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
974 # 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
975 # 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
976
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
977 # Selection of a following action
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
978 form.add_field(name="options", field_type="list-single", options=[do_nothing, get_content, get_content_html, search], label=action_label)
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
979
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
980 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
981
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
982 def is_internal(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
983 """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
984
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
985 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
986
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
987 """
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
988 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
989 if internal_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
990 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
991
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
992 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
993
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
994 def is_xmlrpc(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
995 """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
996
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
997 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
998
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
999 """
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
1000 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
1001 if xmlrpc_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1002 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1003
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1004 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
1005
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1006 def contains_form(self, message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1007 """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
1008
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1009 @param message: message stanza
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1010 @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
1011 @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
1012
2662
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 not isinstance(message, Message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1015 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
1016
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1017 payload = message.get_node()
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1018 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
1019
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1020 if form:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1021 return form[0]
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1022 else:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1023 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
1024
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
1025 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
1026 """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
1027
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
1028 @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
1029 @type jid: pyxmpp.jid.JID
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1030 @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
1031 @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
1032
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1033 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1034 if not isinstance(form_node, libxml2.xmlNode):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1035 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
1036
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
1037 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
1038 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
1039
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 _ = 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
1041
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1042 form = forms.Form(form_node)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1043
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
1044 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
1045 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
1046
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1047 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
1048 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
1049 if action == u"search":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1050 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
1051 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1052 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
1053 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
1054 elif "options" in form:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1055 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
1056
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1057 # View page info
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1058 if option == "v":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1059 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
1060 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
1061
2695
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
1062 # Perform an another search
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
1063 elif option == "s":
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
1064 self.handle_internal_command(jid, ["searchform"])
724b6b93f2f9 Show search results with a normal form. Make search available from forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2694
diff changeset
1065
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1066 # Revert a change
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1067 elif option == "r":
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1068 revision = int(form["revision"].value)
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1069
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1070 # We can't really revert creation of a page, right?
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1071 if revision == 1:
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1072 return
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1073
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1074 self.handle_xmlrpc_command(jid, ["revertpage", form["page_name"].value, "%d" % (revision - 1, )])
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1075
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
1076
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 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
1078 """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
1079
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 @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
1081 @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
1082 @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
1083 @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
1084
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 """
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
1086 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
1087 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
1088 _ = 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
1089
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
1090 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
1091 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
1092 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
1093 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
1094
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
1095 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
1096 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
1097 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
1098 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
1099 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
1100 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
1101
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
1102 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
1103 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
1104 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
1105
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1106 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
1107 """Handles incoming messages
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1108
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
1109 @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
1110 @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
1111
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1112 """
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
1113 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
1114 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
1115 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
1116
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1117 form = self.contains_form(message)
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1118 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
1119 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
1120 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1121
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1122 text = message.get_body()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1123 sender = message.get_from_jid()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1124 if text:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1125 command = text.split()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1126 command[0] = command[0].lower()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1127 else:
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1128 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1129
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1130 if self.is_internal(command[0]):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1131 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
1132 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
1133 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
1134 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1135 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
1136
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1137 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
1138 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
1139
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1140 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
1141 """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
1142
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
1143 @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
1144 @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
1145 @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
1146
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
1147 """
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
1148 _ = 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
1149
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1150 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
1151 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
1152 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
1153 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
1154 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
1155 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1156 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
1157 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
1158 jid = sender.bare().as_unicode()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1159 resource = sender.resource
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1160
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1161 # 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
1162 # 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
1163 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
1164 self.send_search_form(sender)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1165 else:
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1166 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
1167 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
1168 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
1169 # 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
1170 return self.reply_help(sender)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1171
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
1172 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
1173 """Performs a Wiki search of term
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1174
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1175 @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
1176 @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
1177 @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
1178 @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
1179 @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
1180 @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
1181 @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
1182 @type presentation: unicode
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1183
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1184 """
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
1185 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
1186 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
1187
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1188 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
1189 """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
1190
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
1191 @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
1192 @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
1193 @return: a help message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1194
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
1195 """
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
1196 _ = 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
1197
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1198 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
1199 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
1200 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
1201
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1202 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
1203 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
1204 as it's received.""")
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1205
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1206 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
1207 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
1208
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 # 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
1210 else:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1211 if command in self.xmlrpc_commands:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1212 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
1213 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
1214 return help_str % {'command': command,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1215 'description': classobj.description,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1216 '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
1217 }
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@