annotate jabberbot/xmppbot.py @ 2999:e94b940f3a84

add a comment about listen backlog to moin.fcg (port from 1.6)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 06 Jan 2008 17:32:33 +0100
parents 2a5fa7d79d86
children d860ab45d438
rev   line source
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
2110
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
3 MoinMoin - jabber bot
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
5 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
6 @license: GNU GPL, see COPYING for details.
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
8
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
9 import logging, time, Queue
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10 from threading import Thread
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
11
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
12 from pyxmpp.client import Client
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
13 from pyxmpp.jid import JID
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
14 from pyxmpp.streamtls import TLSSettings
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
15 from pyxmpp.message import Message
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
16 from pyxmpp.presence import Presence
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
17 from pyxmpp.iq import Iq
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
18 import pyxmpp.jabber.dataforms as forms
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
19 import libxml2
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
20
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
21 import jabberbot.commands as cmd
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
22 import jabberbot.i18n as i18n
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
23 import jabberbot.oob as oob
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
24 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
25
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
26
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
27 class Contact:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
28 """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
29
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
30 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
31 contact availability, status, etc."""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
32
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
33 # 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
34 # 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
35 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
36
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
37 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
38 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
39 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
40 self.language = language
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
41
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
42 # 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
43 # 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
44 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
45
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
46 # 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
47 # 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
48 # 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
49 # "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
50 # 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
51 self.messages = []
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
52
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
53 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
54 """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
55
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
56 @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
57
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
58 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
59 # 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
60 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
61
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
62 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
63 """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
64
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
65 @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
66 @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
67 @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
68
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
69 """
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
70 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
71 self.last_online = None
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
72
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
73 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
74 """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
75 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
76
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
77 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
78 """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
79
b0a44fc4d87d Towards more applications of data forms, with many bugs fixed along the way.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
80 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
81 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
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 """
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
84 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
85 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
86 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
87 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
88 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
89
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 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
91 """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
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 @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
94 @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
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 """
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 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
98 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
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 # 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
101 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
102 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
103
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 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
105 # 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
106 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
107 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
108 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
109
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 return selected
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
111
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
112 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
113 """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
114
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
115 @param resource: resource name
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
116
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
117 """
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
118 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
119 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
120 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
121 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
122
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
123 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
124 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
125
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
126 def is_dnd(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
127 """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
128
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
129 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
130
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
131 """
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
132 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
133
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
134 # 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
135 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
136 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
137 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
138 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
139
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
140 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
141 """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
142
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
143 @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
144 @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
145 @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
146
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
147 """
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
148 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
149 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
150 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
151 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
152
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
153 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
154 """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
155 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
156
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
157 def __str__(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
158 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
159 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
160 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
161
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
162
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
163 class XMPPBot(Client, Thread):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
164 """A simple XMPP bot"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
165
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
166 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
167 """A constructor
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 @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
170 @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
171
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
172 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
173 Thread.__init__(self)
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 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
176 self.to_commands = to_commands
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
177 jid = u"%s@%s/%s" % (config.xmpp_node, config.xmpp_server, config.xmpp_resource)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
178
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
179 self.config = config
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
180 self.log = logging.getLogger("log")
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
181 self.jid = JID(node_or_jid=jid, domain=config.xmpp_server, resource=config.xmpp_resource)
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
182 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
183
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
184 # 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
185 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
186
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
187 # 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
188 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
189
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
190 # 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
191 self.contact_check = 600
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
192 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
193
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2668
diff changeset
194 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
195 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
196
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
197 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
198 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
199 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
200
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
201 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
202
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
203 # 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
204
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
216 def run(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
217 """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
218
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
219 self.log.info("Starting the jabber bot.")
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
220 self.connect()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
221 self.loop()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
222
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
223 def stop(self):
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
224 """Stop the thread"""
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
225 self.stopping = True
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
226
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
227 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
228 """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
229
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
230 while True:
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
231 if self.stopping:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
232 break
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
233
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
234 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
235 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
236 break
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
237
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
238 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
239 if not act:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
240 # Process all available commands
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
241 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
242 self.idle()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
243
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
244 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
245 """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
246
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
247 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
248
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
249 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
250 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
251 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
252 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
253
2968
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
254 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
255 """Handle session started event.
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
256 Requests the user's roster and sends the initial presence with
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
257 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
258
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
259 """
2a5fa7d79d86 GHOP: 2/3 of XEP-0115 implementation by bolekk
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2957
diff changeset
260 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
261 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
262 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
263 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
264
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
265 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
266 """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
267
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
268 @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
269
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
270 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
271 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
272 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
273 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
274
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
275 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
276 """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
277
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
278 @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
279 @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
280
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
281 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
282 language = "en"
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
283 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
284 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
285 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
286 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
287
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
288 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
289 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
290
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
291 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
292
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
293 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
294 """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
295
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
296 @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
297
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
298 """
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
299 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
300 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
301 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
302 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
303 except Queue.Empty:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
304 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
305
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
306 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
307 """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
308
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
309 @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
310 @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
311 @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
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 """
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
314
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
315 cmd_cls = command.__class__
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
316
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
317 try:
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
318 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
319 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
320 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
321 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
322
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2949
diff changeset
323 # 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
324 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
325
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
326 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
327 """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
328
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
329 @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
330 @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
331 @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
332 @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
333 @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
334 @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
335
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
336 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
337 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
338 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
339 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
340 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
341 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
342
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 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
344 """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
345
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 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
347 @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
348 @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
349 @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
350 @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
351 @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
352
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 """
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 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
355 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
356 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
357 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
358 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
359
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 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
361 """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
362
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 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
364 @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
365 @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
366 @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
367 @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
368 @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
369
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 """
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 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
372 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
373 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
374 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
375 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
376
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 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
378 """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
379
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 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
381 @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
382 @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
383 @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
384 @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
385 @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
386
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 """
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 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
389 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
390 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
391 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
392 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
393
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 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
395 """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
396
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 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
398 @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
399 @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
400 @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
401 @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
402 @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
403
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 """
2949
2def79ea092e Pass the unimplemented action before a fix is ready.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
405 pass
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
406
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
407 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
408 """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
409
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
410 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
411 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
412
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
413 @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
414 @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
415
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
416 """
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
417 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
418 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
419
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
420 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
421 """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
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 @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
424 @type jid: JID
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 """
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 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
428 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
429
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
430 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
431 """Sends a message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
432
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
433 @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
434 @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
435 @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
436 @type jid_text: unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
437
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
438 """
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
439 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
440 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
441 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
442
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
443 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
444 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
445 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
446 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
447 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
448 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
449 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
450
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
451 # 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
452 # 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
453 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
454 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
455
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
456 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
457
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
458 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
459 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
460
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
461 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
462
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
463 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
464 """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
465
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
466 @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
467 @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
468 @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
469 @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
470 @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
471 @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
472 @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
473 @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
474
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
475 """
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
476 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
477 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
478
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
479 _ = 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
480
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
481 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
482 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
483
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
484 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
485 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
486
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
487 self.get_stream().send(message)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
488
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
489 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
490 _ = self.get_text(jid)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
491
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
492 # 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
493 # 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
494 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
495 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
496 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
497 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
498 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
499 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
500 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
501 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
502 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
503
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
504 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
505 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
506
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
507 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
508 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
509 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
510 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
511 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
512 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
513
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
514 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
515
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
516 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
517 """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
518
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
519 @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
520 @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
521 @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
522 @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
523
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
524 """
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
525 _ = 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
526
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
527 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
528 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
529 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
530
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 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
532 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
533 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
534 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
535
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
536 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
537 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
538 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
539 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
540
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 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
542 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
543 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
544 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
545 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
546
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
547 # 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
548 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
549 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
550
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
551 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
552 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
553 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
554
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
555 # 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
556 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
557 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
558 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
559 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
560
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 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
562 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
563 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
564
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 # 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
566 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
567
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
568 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
569
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 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
571 """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
572
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 @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
574 @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
575 @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
576 @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
577
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 """
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 _ = 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
580 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
581 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
582 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
583 '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
584 '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
585 '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
586 '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
587 '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
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
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 data = {'text': message, 'subject': msg_data.get('subject', '')}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
591 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
592
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 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
594 """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
595
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 @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
597 @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
598 @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
599 @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
600
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
601 """
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 _ = 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
603
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 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
605 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
606 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
607
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
608 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
609 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
610
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 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
612 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
613
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 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
615 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
616 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
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 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
619 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
620 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
621
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 # 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
623 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
624 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
625 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
626 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
627
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
628 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
629 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
630 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
631
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
632 # 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
633 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
634
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 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
636
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
637 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
638 """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
639
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 @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
641 @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
642 @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
643 @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
644
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
645 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
646 _ = 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
647 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
648 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
649 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
650 '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
651 '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
652 '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
653 '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
654 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
655
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 data = {'text': message, 'subject': msg_data.get('subject', '')}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
657 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
658
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
659 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
660 """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
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 @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
663 @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
664 @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
665 @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
666
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 """
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 _ = 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
669
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
670 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
671 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
672 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
673
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
674 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
675 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
676 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
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 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
679 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
680 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
681
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
682 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
683 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
684 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
685 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
686 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
687 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
688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
689 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
690 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
691 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
692
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
693 # 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
694 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
695 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
696 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
697 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
698
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
699 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
700 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
701 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
702
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 # 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
704 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
705
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 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
707
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 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
709 """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
710
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
711 @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
712 @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
713 @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
714 @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
715
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
716 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
717 _ = self.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
718 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
719 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
720 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
721 '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
722 '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
723 '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
724 }
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
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 data = {'text': message, 'subject': msg_data['subject']}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
727 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
728
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
729 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
730 """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
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 @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
733 @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
734 @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
735 @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
736
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
737 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
738 _ = 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
739
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 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
741 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
742 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
743
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
744 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
745 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
746 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
747 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
748
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
749 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
750 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
751 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
752 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
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 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
755 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
756 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
757 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
758 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
759 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
760 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
761
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 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
763 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
764 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
765
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 # 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
767 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
768 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
769 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
770 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
771
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
772 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
773 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
774 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
775
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 # 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
777 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
778
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 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
780
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
781 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
782 """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
783
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 @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
785 @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
786 @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
787 @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
788
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
789 """
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
790 _ = 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
791 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
792 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
793 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
794 '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
795 '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
796 '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
797 '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
798 }
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
799
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 data = {'text': message, 'subject': msg_data['subject']}
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
801 self.send_message(jid, data, u"message")
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2686
diff changeset
802
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
803 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
804 """Handles GetPageInfo commands
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
805
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
806 @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
807 @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
808
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
809 """
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
810 # 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
811 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
812 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
813
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
814 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
815 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
816 'year': datestr[:4],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
817 'month': datestr[4:6],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
818 'day': datestr[6:8],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
819 'time': datestr[9:17],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
820 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
821
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
822 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
823 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
824 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
825 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
826
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
827 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
828 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
829
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
830 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
831 """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
832
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
833 @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
834 @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
835
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 _ = 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
838
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
839 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
840 is available:""")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
841
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
842 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
843 Last modification: %(modification)s
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
844 Current version: %(version)s""") % {
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
845 'author': command.data['author'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
846 'modification': command.data['lastModified'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
847 'version': command.data['version'],
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
848 }
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
849
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
850 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
851 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
852
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
853 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
854 """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
855
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
856
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 _ = 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
859 data = command.data
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 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
862 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
863 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
864
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
865 action1 = _("Do nothing")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
866 action2 = _("Get page contents")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
867 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
868 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
869
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
870 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
871 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
872 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
873 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
874
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
875 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
876 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
877 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
878 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
879 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
880
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
881 # full_jid = JID(jid)
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
882 # 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
883 # resource = full_jid.resource
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
884
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
885 # 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
886 # 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
887 # 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
888 # else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
889 # url_list = []
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
890 #
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
891 # 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
892 # 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
893 # 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
894
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
895 # 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
896 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
897
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
898 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
899
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
900 def is_internal(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
901 """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
902
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
903 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
904
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
905 """
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
906 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
907 if internal_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
908 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
909
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
910 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
911
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
912 def is_xmlrpc(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
913 """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
914
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
915 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
916
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
917 """
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
918 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
919 if xmlrpc_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
920 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
921
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
922 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
923
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
924 def contains_form(self, message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
925 """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
926
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
927 @param message: message stanza
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
928 @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
929 @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
930
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
931 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
932 if not isinstance(message, Message):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
933 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
934
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
935 payload = message.get_node()
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
936 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
937
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
938 if form:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
939 return form[0]
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
940 else:
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
941 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
942
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
943 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
944 """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
945
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
946 @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
947 @type jid: pyxmpp.jid.JID
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
948 @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
949 @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
950
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
951 """
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
952 if not isinstance(form_node, libxml2.xmlNode):
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
953 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
954
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
955 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
956 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
957
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
958 _ = 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
959
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
960 form = forms.Form(form_node)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
961
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
962 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
963 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
964
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
965 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
966 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
967 if action == u"search":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
968 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
969 else:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
970 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
971 self.send_message(jid.as_unicode(), data, u"message")
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
972 elif "options" in form:
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
973 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
974
2694
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
975 # View page info
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
976 if option == "v":
6500951853e2 Make "get page info" work with data forms.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
977 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
978 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
979
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
980 # 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
981 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
982 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
983
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
984 # 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
985 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
986 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
987
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
988 # 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
989 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
990 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
991
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2696
diff changeset
992 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
993
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
994 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
995 """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
996
4d4a1a5c0c42 Data forms 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 @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
998 @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
999 @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
1000 @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
1001
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1002 """
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1003 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
1004 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
1005 _ = 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
1006
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1007 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
1008 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
1009 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1010 self.send_message(jid.as_unicode(), data, u"message")
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1011
4d4a1a5c0c42 Data forms search now works. Display of results... almost (no idea why not atm).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2663
diff changeset
1012 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
1013 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
1014 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
1015 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
1016 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
1017 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
1018
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
1019 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
1020 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
1021 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
1022
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1023 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
1024 """Handles incoming messages
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1025
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
1026 @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
1027 @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
1028
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1029 """
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
1030 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
1031 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
1032 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
1033
2662
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1034 form = self.contains_form(message)
9c71c7c7f5f6 Add basic form handling.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2661
diff changeset
1035 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
1036 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
1037 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1038
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1039 text = message.get_body()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1040 sender = message.get_from_jid()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1041 if text:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1042 command = text.split()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1043 command[0] = command[0].lower()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1044 else:
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1045 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1046
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1047 if self.is_internal(command[0]):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1048 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
1049 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
1050 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
1051 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1052 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
1053
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1054 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
1055 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
1056
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1057 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
1058 """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
1059
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
1060 @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
1061 @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
1062 @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
1063
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
1064 """
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
1065 _ = 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
1066
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1067 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
1068 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
1069 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
1070 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
1071 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
1072 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1073 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
1074 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
1075 jid = sender.bare().as_unicode()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1076 resource = sender.resource
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1077
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1078 # 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
1079 # 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
1080 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
1081 self.send_search_form(sender)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1082 else:
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
1083 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
1084 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
1085 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
1086 # 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
1087 return self.reply_help(sender)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1088
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
1089 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
1090 """Performs a Wiki search of term
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1091
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 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
1093 @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
1094 @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
1095 @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
1096 @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
1097 @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
1098 @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
1099 @type presentation: unicode
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
1100
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
1101 """
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
1102 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
1103 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
1104
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
1105 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
1106 """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
1107
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
1108 @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
1109 @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
1110 @return: a help message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1111
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
1112 """
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
1113 _ = 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
1114
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1115 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
1116 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
1117 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
1118
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1119 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
1120 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
1121 as it's received.""")
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 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
1124 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
1125
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1126 # 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
1127 else:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1128 if command in self.xmlrpc_commands:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1129 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
1130 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
1131 return help_str % {'command': command,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1132 'description': classobj.description,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
1133 '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
1134 }
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
1135 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
1136 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
1137
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1138 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
1139 """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
1140
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
1141 @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
1142 @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
1143
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
1144 """
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
1145 _ = 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
1146 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
1147
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
1148 # 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
1149 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
1150
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1151 try:
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1152 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
1153 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
1154 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
1155
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
1156 # 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
1157 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
1158 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
1159 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
1160
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
1161 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
1162
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
1163 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
1164 """Handles unsubscribed presence stanzas"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1165
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
1166 # 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
1167 pass
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1168
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
1169 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
1170 """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
1171
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
1172 # 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
1173 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
1174 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
1175
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1176 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
1177 """Handles unavailable presence stanzas
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1178
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
1179 @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
1180
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
1181 """
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1182 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
1183
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1184 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
1185 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
1186
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1187 # If we get presence, this contact should already be known
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1188 if bare_jid in self.contacts:
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1189 contact = self.contacts[bare_jid]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1190
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1191 if self.config.verbose:
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1192 self.log.debug("%s, going OFFLINE." % contact)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1193
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1194 try:
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1195 # Send queued messages now, as we can't guarantee to be
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1196 # alive the next time this contact becomes available.
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1197 if len(contact.resources) == 1:
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1198 self.send_queued_messages(contact, ignore_dnd=True)
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
1199 contact.remove_resource(jid.resource)
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1200 else:
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1201 contact.remove_resource(jid.resource)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1202
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1203 # The highest-priority resource, which used to be DnD might
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1204 # have gone offline. If so, try to deliver messages now.
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1205 if not contact.is_dnd():
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1206 self.send_queued_messages(contact)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1207
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1208 except ValueError:
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1209 self.log.error("Unknown contact (resource) going offline...")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1210
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1211 else:
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
1212 self.log.error("Unavailable presence from unknown contact.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1213
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1214 # Confirm that we've handled this stanza
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
1215 return True
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
1216
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.un