annotate jabberbot/xmppbot.py @ 5829:a744f573fe30

advancedsearch: fix problem with non-ascii chars breaking up words for OR and NOT terms
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 04 Feb 2012 16:52:24 +0100
parents d4635a121689
children
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
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
11 from datetime import timedelta
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
12
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
13 from pyxmpp.cache import Cache
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
14 from pyxmpp.cache import CacheItem
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
15 from pyxmpp.client import Client
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
16 from pyxmpp.jid import JID
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
17 from pyxmpp.streamtls import TLSSettings
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
18 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
19 from pyxmpp.presence import Presence
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
20 from pyxmpp.iq import Iq
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
21 import pyxmpp.jabber.dataforms as forms
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
22 import libxml2
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
23
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
24 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
25 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
26 import jabberbot.oob as oob
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
27 import jabberbot.capat as capat
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
28
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
29
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
30 class Contact:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
31 """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
32
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
33 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
34 contact availability, status, etc."""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
35
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
36 # 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
37 # 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
38 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
39
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
40 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
41 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
42 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
43 self.language = language
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
44
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
45 # 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
46 # 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
47 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
48
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
49 # 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
50 # 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
51 # 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
52 # "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
53 # 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
54 self.messages = []
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
55
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
56 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
57 """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
58
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
59 @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
60
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
61 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
62 # 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
63 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
64
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
65 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
66 """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
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 @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
69 @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
70 @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
71
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
72 """
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
73 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
74 self.last_online = None
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
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 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
77 """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
78 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
79
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
80 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
81 """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
82
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
83 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
84 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
85
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 """
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
87 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
88 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
89 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
90 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
91 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
92
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 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
94 """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
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 @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
97 @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
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 """
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 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
101 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
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 # 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
104 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
105 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
106
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 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
108 # 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
109 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
110 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
111 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
112
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
113 return selected
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
114
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
115 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
116 """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
117
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
118 @param resource: resource name
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
119
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
120 """
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
121 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
122 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
123 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
124 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
125
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
126 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
127 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
128
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
129 def is_dnd(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
130 """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
131
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
132 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
133
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
134 """
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
135 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
136
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
137 # 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
138 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
139 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
140 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
141 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
142
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
143 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
144 """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
145
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
146 @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
147 @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
148 @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
149
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
150 """
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
151 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
152 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
153 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
154 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
155
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
156 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
157 """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
158 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
159
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
160 def __str__(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
161 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
162 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
163 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
164
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
165
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
166 class XMPPBot(Client, Thread):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
167 """A simple XMPP bot"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
168
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
169 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
170 """A constructor
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
171
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
172 @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
173 @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
174
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
175 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
176 Thread.__init__(self)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
177
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
178 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
179 self.to_commands = to_commands
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
180
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
181 self.config = config
3612
6f06c7d051a4 Setting a more meaningful logger name.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 3211
diff changeset
182 self.log = logging.getLogger(__name__)
4132
99ed52b53e4e Added support for authentication realms other server hostname
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 3612
diff changeset
183 self.jid = JID(node_or_jid=config.xmpp_node, domain=config.xmpp_server)
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
184 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
185
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
186 # 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
187 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
188
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
189 # 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
190 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
191
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
192 # 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
193 self.contact_check = 600
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
194 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
195
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
196 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
197 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
198
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
199 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
200 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
201 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
202
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
203 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
204
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
205 # Setup message handlers
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
206
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
207 self._msg_handlers = {cmd.NotificationCommand: self._handle_notification,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
208 cmd.NotificationCommandI18n: self._handle_notification,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
209 cmd.AddJIDToRosterCommand: self._handle_add_contact,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
210 cmd.RemoveJIDFromRosterCommand: self._handle_remove_contact,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
211 cmd.GetPage: self._handle_get_page,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
212 cmd.GetPageHTML: self._handle_get_page,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
213 cmd.GetPageList: self._handle_get_page_list,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
214 cmd.GetPageInfo: self._handle_get_page_info,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
215 cmd.GetUserLanguage: self._handle_get_language,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
216 cmd.Search: self._handle_search}
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
217
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
218 # cache for service discovery results ( (ver, algo) : Capabilities = libxml2.xmlNode)
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
219 self.disco_cache = Cache(max_items=config.disco_cache_size, default_purge_period=0)
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
220
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
221 # dictionary of jids waiting for service discovery results
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
222 # ( (ver, algo) : (timeout=datetime.timedelta, [list_of_jids=pyxmpp.jid]) )
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
223 self.disco_wait = {}
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
224
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
225 # temporary dictionary ( pyxmpp.jid: (ver, algo) )
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
226 self.disco_temp = {}
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
227
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
228 def run(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
229 """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
230
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
231 self.log.info("Starting the jabber bot.")
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
232 self.connect()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
233 self.loop()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
234
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
235 def stop(self):
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
236 """Stop the thread"""
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
237 self.stopping = True
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
238
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
239 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
240 """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
241
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
242 while True:
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
243 if self.stopping:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
244 break
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
245
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
246 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
247 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
248 break
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
249
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
250 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
251 if not act:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
252 # Process all available commands
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
253 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
254 self.idle()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
255
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
256 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
257 """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
258
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
259 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
260
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
261 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
262 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
263 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
264 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
265
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
266 self.disco_cache.tick()
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
267 self.check_disco_delays()
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
268
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
269 def session_started(self):
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
270 """Handle session started event.
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
271 Requests the user's roster and sends the initial presence with
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
272 a <c> child as described in XEP-0115 (Entity Capabilities)
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
273
3019
d860ab45d438 GHOP: part 3/3 of XEP-115 implementation by bolekk.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2968
diff changeset
274 """
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
275 self.request_roster()
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
276 pres = capat.create_presence(self.jid)
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
277 self.stream.set_iq_get_handler("query", "http://jabber.org/protocol/disco#info", self.handle_disco_query)
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
278 self.stream.send(pres)
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
279
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
280 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
281 """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
282
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
283 @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
284
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
285 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
286 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
287 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
288 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
289
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
290 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
291 """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
292
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
293 @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
294 @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
295
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
296 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
297 language = "en"
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
298 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
299 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
300 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
301 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
302
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
303 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
304 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
305
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
306 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
307
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
308 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
309 """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
310
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
311 @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
312
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
313 """
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
314 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
315 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
316 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
317 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
318 except Queue.Empty:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
319 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
320
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
321 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
322 """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
323
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
324 @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
325 @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
326 @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
327
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
328 """
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
329
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
330 cmd_cls = command.__class__
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
331
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
332 try:
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
333 handler = self._msg_handlers[cmd_cls]
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
334 except KeyError:
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
335 self.log.debug("No such command: " + cmd_cls.__name__)
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
336 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
337
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
338 # NOTE: handler is a method, so it takes self as a hidden arg
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
339 handler(command, ignore_dnd)
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
340
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
341 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
342 """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
343
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
344 @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
345 @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
346 @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
347 @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
348 @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
349 @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
350
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
351 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
352 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
353 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
354 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
355 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
356 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
357
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
358 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
359 """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
360
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
361 @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
362 @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
363 @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
364 @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
365 @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
366 @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
367
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
368 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
369 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
370 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
371 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
372 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
373 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
374
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
375 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
376 """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
377
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
378 @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
379 @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
380 @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
381 @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
382 @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
383 @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
384
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
385 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
386 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
387 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
388 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
389 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
390 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
391
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
392 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
393 """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
394
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
395 @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
396 @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
397 @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
398 @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
399 @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
400 @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
401
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
402 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
403 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
404 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
405 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
406 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
407 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
408
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 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
410 """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
411
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 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
413 @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
414 @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
415 @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
416 @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
417 @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
418
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
419 """
4543
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
420 # TODO: send as form if user-client supports it
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
421 self.send_user_created_text(jid.as_unicode(), cmd_data)
4551
d4635a121689 PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4543
diff changeset
422
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
423 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
424 """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
425
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
426 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
427 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
428
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
429 @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
430 @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
431
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
432 """
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
433 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
434 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
435
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
436 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
437 """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
438
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
439 @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
440 @type jid: JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
441
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
442 """
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
443 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
444 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
445
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
446 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
447 """Sends a message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
448
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
449 @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
450 @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
451 @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
452 @type jid_text: unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
453
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
454 """
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
455 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
456 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
457 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
458
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
459 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
460 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
461 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
462 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
463 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
464 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
465 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
466
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
467 # 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
468 # 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
469 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
470 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
471
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
472 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
473
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
474 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
475 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
476
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
477 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
478
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
479 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
480 """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
481
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
482 @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
483 @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
484 @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
485 @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
486 @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
487 @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
488 @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
489 @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
490
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
491 """
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
492 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
493 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
494
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
495 _ = 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
496
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
497 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
498 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
499
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
500 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
501 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
502
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
503 self.get_stream().send(message)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
504
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
505 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
506 _ = self.get_text(jid)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
507
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
508 # 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
509 # 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
510 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
511 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
512 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
513 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
514 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
515 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
516 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
517 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
518 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
519
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
520 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
521 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
522
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
523 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
524 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
525 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
526 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
527 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
528 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
529
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
530 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
531
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
532 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
533 """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
534
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
535 @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
536 @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
537 @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
538 @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
539
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
540 """
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
541 _ = 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
542
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
543 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
544 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
545 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
546
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
547 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
548 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
549 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
550 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
551
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
552 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
553 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
554 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
555 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
556
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
557 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
558 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
559 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
560 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
561 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
562
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
563 # 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
564 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
565 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
566
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 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
568 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
569 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
570
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
571 # 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
572 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
573 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
574 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
575 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
576
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
577 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
578 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
579 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
580
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
581 # 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
582 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
583
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
584 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
585
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
586 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
587 """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
588
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
589 @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
590 @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
591 @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
592 @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
593
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
594 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
595 _ = 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
596 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
597 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
598 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
599 '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
600 '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
601 '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
602 '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
603 '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
604 }
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 data = {'text': message, 'subject': msg_data.get('subject', '')}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
607 self.send_message(jid, data, u"normal")
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
608
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
609 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
610 """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
611
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
612 @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
613 @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
614 @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
615 @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
616
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
617 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
618 _ = 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
619
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
620 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
621 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
622 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
623
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
624 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
625 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
626
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 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
628 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
629
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
630 form = 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
631 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
632 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
633
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 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
635 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
636 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
637
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 # 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
639 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
640 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
641 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
642 url_list = []
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
643
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 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
645 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
646 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
647
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
648 # 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
649 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
650
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
651 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
652
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 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
654 """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
655
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
656 @param 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
657 @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
658 @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
659 @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
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 """
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 _ = 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
663 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
664 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
665 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
666 '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
667 '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
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', '')}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
673 self.send_message(jid, data, u"normal")
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
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_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
676 """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
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 = _("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
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 = _("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
692 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
693
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
694 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
695 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
696 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
697
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 = 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
699 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
700 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
701 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
702 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
703 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
704
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
705 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
706 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
707 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
708
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 # 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
710 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
711 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
712 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
713 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
714
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
715 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
716 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
717 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
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 # 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
720 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
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 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
723
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 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
725 """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
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 @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
728 @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
729 @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
730 @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
731
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 """
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 _ = 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
734 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
735 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
736 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
737 '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
738 '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
739 '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
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
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 data = {'text': message, 'subject': msg_data['subject']}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
743 self.send_message(jid, data, u"normal")
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
744
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
745 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
746 """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
747
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 @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
749 @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
750 @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
751 @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
752
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
753 """
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 _ = 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
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 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
757 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
758 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
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 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
761 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
762 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
763 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
764
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
765 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
766 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
767 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
768 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
769
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 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
771 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
772 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
773 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
774 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
775 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
776 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
777
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 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
779 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
780 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
781
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 # 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
783 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
784 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
785 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
786 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
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 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
789 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
790 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
791
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 # 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
793 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
794
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 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
796
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 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
798 """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
799
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 @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
801 @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
802 @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
803 @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
804
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 """
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 _ = 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
807 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
808 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
809 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
810 '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
811 '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
812 '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
813 '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
814 }
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
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 data = {'text': message, 'subject': msg_data['subject']}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
817 self.send_message(jid, data, u"normal")
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
818
4543
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
819 def send_user_created_text(self, jid, msg_data):
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
820 """Sends a simple, text page user-created-notification
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
821
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
822 @param jid: a Jabber ID to send the notification to
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
823 @type jid: unicode
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
824 @param msg_data: dictionary with notification data
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
825 @type msg_data: dict
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
826
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
827 """
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
828 _ = self.get_text(jid)
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
829 message = _("%(text)s") % {'text': msg_data['text']}
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
830
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
831 data = {'text': message, 'subject': msg_data['subject']}
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
832 self.send_message(jid, data, u"normal")
544679a8a444 Commiting a patch from Noya to fix user creation notifications.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4542
diff changeset
833
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
834 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
835 """Handles GetPageInfo commands
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
836
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
837 @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
838 @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
839
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
840 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
841 # 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
842 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
843 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
844
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
845 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
846 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
847 'year': datestr[:4],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
848 'month': datestr[4:6],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
849 'day': datestr[6:8],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
850 'time': datestr[9:17],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
851 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
852
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
853 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
854 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
855 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
856 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
857
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
858 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
859 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
860
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
861 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
862 """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
863
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
864 @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
865 @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
866
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
867 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
868 _ = 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
869
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
870 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
871 is available:""")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
872
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
873 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
874 Last modification: %(modification)s
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
875 Current version: %(version)s""") % {
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
876 'author': command.data['author'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
877 'modification': command.data['lastModified'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
878 'version': command.data['version'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
879 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
880
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
881 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
882 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
883
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
884 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
885 """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
886
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
887
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
888 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
889 _ = 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
890 data = command.data
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 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
893 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
894 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
895
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
896 action1 = _("Do nothing")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
897 action2 = _("Get page contents")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
898 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
899 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
900
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
901 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
902 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
903 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
904 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
905
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
906 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
907 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
908 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
909 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
910 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
911
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
912 # full_jid = JID(jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
913 # 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
914 # resource = full_jid.resource
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
915
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
916 # 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
917 # 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
918 # 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
919 # else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
920 # url_list = []
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
921 #
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
922 # 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
923 # 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
924 # 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
925
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
926 # 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
927 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
928
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
929 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
930
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
931 def is_internal(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
932 """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
933
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
934 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
935
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
936 """
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
937 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
938 if internal_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
939 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
940
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
941 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
942
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
943 def is_xmlrpc(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
944 """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
945
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
946 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
947
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
948 """
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
949 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
950 if xmlrpc_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
951 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
952
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
953 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
954
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
955 def contains_form(self, message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
956 """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
957
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
958 @param message: message stanza
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
959 @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
960 @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
961
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
962 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
963 if not isinstance(message, Message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
964 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
965
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
966 payload = message.get_node()
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
967 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
968
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
969 if form:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
970 return form[0]
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
971 else:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
972 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
973
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
974 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
975 """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
976
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
977 @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
978 @type jid: pyxmpp.jid.JID
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
979 @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
980 @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
981
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
982 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
983 if not isinstance(form_node, libxml2.xmlNode):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
984 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
985
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
986 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
987 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
988
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
989 _ = 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
990
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
991 form = forms.Form(form_node)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
992
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
993 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
994 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
995
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
996 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
997 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
998 if action == u"search":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
999 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
1000 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1001 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
1002 self.send_message(jid.as_unicode(), data, u"normal")
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1003 elif "options" in form:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1004 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
1005
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1006 # View page info
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1007 if option == "v":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
1008 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
1009 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
1010
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
1011 # 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
1012 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
1013 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
1014
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
1015 # 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
1016 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
1017 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
1018
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1019 # 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
1020 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
1021 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
1022
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
1023 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
1024
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
1025 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
1026 """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
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
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
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1030 @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
1031 @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
1032
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1033 """
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1034 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
1035 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
1036 _ = 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
1037
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 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
1039 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
1040 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
4542
9a3e85b44c25 Applying a patch from Noya to use an RFC compliant message type.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 4342
diff changeset
1041 self.send_message(jid.as_unicode(), data, u"normal")
2664
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1042
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
1043 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
1044 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
1045 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
1046 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
1047 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
1048 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
1049
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
1050 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
1051 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
1052 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
1053
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1054 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
1055 """Handles incoming messages
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1056
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
1057 @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
1058 @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
1059
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1060 """
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
1061 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
1062 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
1063 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
1064
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1065 form = self.contains_form(message)
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1066 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
1067 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
1068 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1069
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1070 text = message.get_body()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1071 sender = message.get_from_jid()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1072 if text:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1073 command = text.split()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1074 command[0] = command[0].lower()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1075 else:
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1076 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1077
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1078 if self.is_internal(command[0]):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1079 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
1080 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
1081 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
1082 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1083 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
1084
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1085 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
1086 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
1087
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1088 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
1089 """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
1090
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
1091 @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
1092 @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
1093 @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
1094
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
1095 """
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
1096 _ = 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
1097
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1098 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
1099 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
1100 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
1101 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
1102 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
1103 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1104 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
1105 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
1106 jid = sender.bare().as_unicode()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1107 resource = sender.resource
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1108
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1109 # 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
1110 # 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
1111 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
1112 self.send_search_form(sender)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1113 else:
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1114 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
1115 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
1116 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
1117 # 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
1118 return self.reply_help(sender)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1119
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
1120 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
1121 """Performs a Wiki search of term
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1122
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1123 @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
1124 @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
1125 @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
1126 @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
1127 @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
1128 @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
1129 @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
1130 @type presentation: unicode
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1131
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1132 """
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
1133 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
1134 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
1135
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1136 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
1137 """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
1138
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
1139 @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
1140 @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
1141 @return: a help message
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 """
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
1144 _ = 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
1145
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1146 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
1147 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
1148 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
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 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
1151 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
1152 as it's received.""")
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1153
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1154 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
1155 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
1156
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1157 # 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
1158 else:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1159 if command in self.xmlrpc_commands:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1160 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
1161 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
1162 return help_str % {'command': command,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1163 'description': classobj.description,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1164 '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
1165 }
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1166 else:
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
1167 return _("""Unknown command "%s" """) % (command, )
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1168
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1169 def handle_xmlrpc_command(self, sender, command):
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1170 """Creates a command object, and puts it the command queue
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1171
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
1172 @param command: a valid name of available xmlrpc command
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
1173 @type command: list representing a command, name and parameters
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1174
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1175 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2500
diff changeset
1176 _ = self.get_text(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
1177 command_class = self.xmlrpc_commands[command[0]]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1178
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1179 # Add sender's JID to the argument list
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
1180 command.insert(1, sender.as_unicode())
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1181
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1182 try:
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1183 instance = command_class.__new__(command_class)
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1184 instance.__init__(*command[1:])
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1185 self.from_commands.put_nowait(instance)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1186
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1187 # This happens when user specifies wrong parameters
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1188 except TypeError:
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
1189 msg = _("You've specified a wrong parameter list. \
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
1190 The call should look like:\n\n%(command)s %(params)s")
2415
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1191
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
1192 return msg % {'command': command[0], 'params': command_class.parameter_list}
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1193
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
1194 def handle_unsubscribed_presence(self, stanza):
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
1195 """Handles unsubscribed presence stanzas"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1196
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
1197 # FiXME: what policy should we adopt in this case?
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
1198 pass
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1199
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
1200 def handle_subscribe_presence(self, stanza):
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
1201 """Handles subscribe presence stanzas (requests)"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1202
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
1203 # FIXME: Let's just accept all subscribtion requests for now
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
1204 response = stanza.make_accept_response()
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
1205 self.get_stream().send(response)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1206
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1207 def handle_unavailable_presence(self, stanza):
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
1208 """Handles unavailable presence stanzas
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1209
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
1210 @type stanza: pyxmpp.presence.Presence
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1211
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
1212 """
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1213 self.log.debug("Handling unavailable presence.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1214
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1215 jid = stanza.get_from_jid()
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
1216 bare_jid = jid.bare().as_unicode()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1217
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1218 # If we get presence, this contact should already be known