annotate jabberbot/xmppbot.py @ 2661:9f6e830c82e0

Add more fields to search form, fix bug left from changes in service discovery.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Wed, 08 Aug 2007 21:44:41 +0200
parents 892055d122fb
children 9c71c7c7f5f6
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
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
19
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
20 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
21 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
22 import jabberbot.oob as oob
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2394
diff changeset
23
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
24
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
25 class Contact:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
26 """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
27
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
28 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
29 contact availability, status, etc."""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
30
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
31 # 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
32 # 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
33 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
34
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
35 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
36 self.jid = jid
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
37 self.resources = {resource: {'show': show, 'priority': priority}}
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
38 self.language = language
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
39
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
40 # 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
41 # 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
42 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
43
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
44 # 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
45 # 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
46 # 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
47 # "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
48 # 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
49 self.messages = []
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
50
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
51 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
52 """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
53
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
54 @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
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 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
57 # 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
58 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
59
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
60 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
61 """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
62
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 @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
64 @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
65 @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
66
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
67 """
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
68 self.resources[resource] = {'show': show, 'priority': priority, 'forms': 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
69 self.last_online = None
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
70
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
71 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
72 """Flag a given resource as supporting a particular extension"""
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
73 if resource in self.resources:
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
74 self.resources[resource][extension] = True
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
75
2654
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
76 def supports(self, resource, extension):
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 """Check if a given resource supports a particular extension"""
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
78 if resource in self.resources:
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
79 return extension in self.resources[resource]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
80
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
81 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
82 """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
83
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
84 @param resource: resource name
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
85
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
86 """
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
87 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
88 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
89 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
90 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
91
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
92 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
93 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
94
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
95 def is_dnd(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
96 """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
97
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
98 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
99
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
100 """
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
101 # Priority can't be lower than -128
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
102 max_prio = -129
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
103 max_prio_show = u"dnd"
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
104
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
105 for resource in self.resources.itervalues():
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
106 # TODO: check RFC for behaviour of 2 resources with the same priority
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
107 if resource['priority'] > max_prio:
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
108 max_prio = resource['priority']
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
109 max_prio_show = resource['show']
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
110
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
111 # If there are no resources the contact is offline, not dnd
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
112 return self.resources and max_prio_show == u'dnd'
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
113
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
114 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
115 """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
116
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
117 @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
118 @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
119 @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
120
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
121 """
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
122 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
123 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
124 else:
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
125 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
126
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
127 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
128 """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
129 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
130
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
131 def __str__(self):
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
132 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
133 res = ", ".join([name + " is " + res['show'] for name, res in self.resources.items()])
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
134 return retval % (self.jid.as_utf8(), 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
135
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
136 class XMPPBot(Client, Thread):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
137 """A simple XMPP bot"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
138
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
139 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
140 """A constructor
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
141
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
142 @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
143 @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
144
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
145 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
146 Thread.__init__(self)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
147
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
148 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
149 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
150 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
151
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
152 self.config = config
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
153 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
154 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
155 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
156
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
157 # 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
158 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
159
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
160 # 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
161 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
162
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
163 # 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
164 self.contact_check = 600
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
165 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
166
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
167 self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML, cmd.GetPageList, cmd.GetPageInfo, cmd.Search]
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
168 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
169
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
170 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
171 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
172 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
173
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
174 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
175
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
176 def run(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
177 """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
178
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
179 self.log.info("Starting the jabber bot.")
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
180 self.connect()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
181 self.loop()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
182
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
183 def stop(self):
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
184 """Stop the thread"""
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
185 self.stopping = True
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
186
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
187 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
188 """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
189
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
190 while True:
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
191 if self.stopping:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
192 break
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
193
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
194 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
195 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
196 break
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
197
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
198 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
199 if not act:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
200 # Process all available commands
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
201 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
202 self.idle()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
203
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
204 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
205 """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
206
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
207 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
208
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
209 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
210 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
211 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
212 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
213
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
214 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
215 """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
216
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
217 @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
218
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
219 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
220 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
221 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
222 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
223
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
224 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
225 """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
226
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
227 @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
228 @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
229
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
230 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
231 language = "en"
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
232 if isinstance(jid, str) or isinstance(jid, unicode):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
233 jid = JID(jid).bare().as_utf8()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
234 else:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
235 jid = jid.bare().as_utf8()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
236
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
237 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
238 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
239
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
240 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
241
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 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
243 """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
244
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
245 @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
246
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
247 """
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
248 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
249 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
250 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
251 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
252 except Queue.Empty:
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
253 return False
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
254
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
255 # XXX: refactor this, if-elif sequence is already too long
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
256 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
257 """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
258
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
259 @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
260 @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
261 @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
262
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
263 """
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
264 # Handle normal notifications
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
265 if isinstance(command, cmd.NotificationCommand):
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
266 cmd_data = command.notification
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
267
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
268 for recipient in command.jids:
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
269 jid = JID(recipient)
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
270 jid_text = jid.bare().as_utf8()
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
271
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
272 text = cmd_data['text']
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
273 subject = cmd_data.get('subject', '')
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
274 msg_data = command.notification
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
275
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
276 if isinstance(command, cmd.NotificationCommandI18n):
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
277 # Translate&interpolate the message with data
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
278 gettext_func = self.get_text(jid_text)
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
279 text, subject = command.translate(gettext_func)
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
280 msg_data = {'text': text, 'subject': subject,
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
281 'url_list': cmd_data.get('url_list', [])}
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
282
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
283 # Check if contact is DoNotDisturb.
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
284 # If so, queue the message for delayed delivery.
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
285 contact = self.contacts.get(jid_text, '')
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
286 if contact:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
287 if command.async and contact.is_dnd() and not ignore_dnd:
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
288 contact.messages.append(command)
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
289 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
290
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
291 self.send_message(jid, msg_data, command.msg_type)
2483
41e79a4df6b6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2482
diff changeset
292
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
293 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
294
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
295 _ = self.get_text(command.jid)
2477
76f87ae87759 Initialize _ later, as NotificationCommand doesn't have `jid` attr
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2474
diff changeset
296
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
297 # Handle subscribtion management commands
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
298 if isinstance(command, cmd.AddJIDToRosterCommand):
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
299 jid = JID(node_or_jid=command.jid)
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
300 self.ask_for_subscription(jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
301
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
302 elif isinstance(command, cmd.RemoveJIDFromRosterCommand):
2101
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
303 jid = JID(node_or_jid=command.jid)
3b0fca14c14c Add basic handling of new presence stanzas, type=subscribe and type=unsubscribe
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2100
diff changeset
304 self.remove_subscription(jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
305
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
306 elif isinstance(command, cmd.GetPage) or isinstance(command, cmd.GetPageHTML):
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
307 msg = _(u"""Here's the page "%(pagename)s" that you've requested:\n\n%(data)s""")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
308
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
309 cmd_data = {'text': msg % {'pagename': command.pagename, 'data': command.data}}
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
310 self.send_message(command.jid, cmd_data)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
311
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
312 elif isinstance(command, cmd.GetPageList):
2500
7203f7cc4201 Fix a typo.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2488
diff changeset
313 msg = _("That's the list of pages accesible to you:\n\n%s")
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
314 pagelist = u"\n".join(command.data)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
315
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
316 self.send_message(command.jid, {'text': msg % (pagelist, )})
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
317
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
318 elif isinstance(command, cmd.GetPageInfo):
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
319 intro = _("""Following detailed information on page "%(pagename)s" \
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
320 is available:""")
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
321
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
322 if command.data['author'].startswith("Self:"):
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
323 author = command.data['author'][5:]
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
324 else:
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
325 author = command.data['author']
2415
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
326
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
327 datestr = str(command.data['lastModified'])
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
328 date = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
329 'year': datestr[:4],
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
330 'month': datestr[4:6],
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
331 'day': datestr[6:8],
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
332 'time': datestr[9:17],
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
333 }
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
334
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
335 msg = _("""Last author: %(author)s
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
336 Last modification: %(modification)s
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
337 Current version: %(version)s""") % {
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
338 'author': author,
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
339 'modification': date,
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
340 'version': command.data['version'],
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
341 }
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
342
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
343 self.send_message(command.jid, {'text': intro % {'pagename': command.pagename}})
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
344 self.send_message(command.jid, {'text': msg})
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
345
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
346 elif isinstance(command, cmd.GetUserLanguage):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
347 if command.jid in self.contacts:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
348 self.contacts[command.jid].language = command.language
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
349
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
350 elif isinstance(command, cmd.Search):
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
351 if command.presentation == u"text":
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
352 if not command.data:
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
353 msg = _("There are no pages matching your search criteria!")
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
354 self.send_message(command.jid, {'text': msg})
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
355 return
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
356
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
357 # This hardcoded limitation relies on (mostly correct) assumption that Jabber
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
358 # servers have rather tight traffic limits. Sending more than 25 results is likely
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
359 # to take a second or two - users should not have to wait longer (+search time!).
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
360 elif len(command.data) > 25:
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
361 msg = _("There are too many results (%(number)s). Limiting to first 25 entries.") % {'number': str(len(command.data))}
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
362 self.send_message(command.jid, {'text': msg})
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
363 command.data = command.data[:25]
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
364
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
365 #intro = _("Following pages match your search:\n%(results)s")
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
366
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
367 results = [{'description': result[0], 'url': result[2]} for result in command.data]
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
368
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
369 data = {'text': _('Following pages match your search criteria:'), 'url_list': results}
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
370 self.send_message(command.jid, data, u"chat")
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
371 else:
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
372 pass
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
373 # TODO: implement data forms here
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
374
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
375 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
376 """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
377
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
378 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
379 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
380
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
381 @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
382 @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
383
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
384 """
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
385 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
386 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
387
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
388 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
389 """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
390
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
391 @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
392 @type jid: JID
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
393
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
394 """
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
395 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
396 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
397
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
398 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
399 """Sends a message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
400
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
401 @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
402 @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
403 @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
404 @type jid_text: unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
405
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
406 """
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
407 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
408 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
409 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
410
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
411 if data.has_key('url_list') and data['url_list']:
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
412 jid_bare = jid.bare().as_utf8()
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
413 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
414 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
415 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
416 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
417 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
418
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
419 # 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
420 # 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
421 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
422 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
423
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
424 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
425
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
426 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
427 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
428
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
429 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
430
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
431 def send_form(self, jid, form):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
432 pass
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
433
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
434 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
435 _ = self.get_text(jid)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
436
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
437 # 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
438 # 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
439 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
440 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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
449 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
450 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
451
2482
33eb7652cc0a Actually fix what 76f87ae87759 tried. Finally work around pyxmpp problems (?).
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2477
diff changeset
452 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=help_form)
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
453 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
454 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
455 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
456 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
457
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
458 message = Message(to_jid=jid, body=forms_warn, subject=_("Wiki search"))
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
459 message.add_content(form)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
460 self.get_stream().send(message)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
461
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
462 def is_internal(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
463 """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
464
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
465 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
466
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
467 """
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
468 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
469 if internal_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
470 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
471
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
472 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
473
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
474 def is_xmlrpc(self, command):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
475 """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
476
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
477 @type command: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
478
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
479 """
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
480 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
481 if xmlrpc_cmd.lower() == command:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
482 return True
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
483
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
484 return False
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
485
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
486 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
487 """Handles incoming messages
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
488
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
489 @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
490 @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
491
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
492 """
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
493 if self.config.verbose:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
494 msg = "Message from %s." % (message.get_from_jid().as_utf8(), )
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
495 self.log.debug(msg)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
496
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
497 text = message.get_body()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
498 sender = message.get_from_jid()
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
499 if text:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
500 command = text.split()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
501 command[0] = command[0].lower()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
502 else:
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
503 return
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
504
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
505 if self.is_internal(command[0]):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
506 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
507 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
508 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
509 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
510 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
511
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
512 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
513 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
514
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
515 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
516 """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
517
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
518 @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
519 @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
520 @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
521
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
522 """
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
523 _ = 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
524
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
525 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
526 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
527 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
528 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
529 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
530 else:
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
531 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
532 elif command[0] == "searchform":
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
533 jid = sender.bare().as_utf8()
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
534 resource = sender.resource
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
535
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
536 # 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
537 # 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
538 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
539 self.send_search_form(sender)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
540 else:
2648
596516ae09dd Fix two forms-related bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
541 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
542 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
543 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
544 # 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
545 return self.reply_help(sender)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
546
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
547 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
548 """Performs a Wiki search of term
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
549
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
550 @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
551 @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
552 @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
553 @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
554 @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
555 @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
556 @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
557 @type presentation: unicode
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
558
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
559 """
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
560 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
561 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
562
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
563 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
564 """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
565
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
566 @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
567 @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
568 @return: a help message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
569
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
570 """
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
571 _ = 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
572
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
573 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
574 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
575 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
576
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
577 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
578 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
579 as it's received.""")
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
580
2393
ae44e3266486 s/search/searchform/, add a Search command (does nothing)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
581 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
582 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
583
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
584 # 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
585 else:
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
586 if command in self.xmlrpc_commands:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
587 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
588 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
589 return help_str % {'command': command,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
590 'description': classobj.description,
1007128cb089 Whitespace/pep8 fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
591 '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
592 }
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
593 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
594 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
595
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
596 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
597 """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
598
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
599 @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
600 @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
601
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
602 """
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
603 _ = 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
604 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
605
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
606 # Add sender's JID to the argument list
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
607 command.insert(1, sender.as_utf8())
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
608
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
609 try:
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
610 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
611 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
612 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
613
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
614 # 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
615 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
616 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
617 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
618
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
619 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
620
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
621 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
622 """Handles unsubscribed presence stanzas"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
623
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
624 # 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
625 pass
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
626
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
627 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
628 """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
629
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
630 # 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
631 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
632 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
633
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
634 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
635 """Handles unavailable presence stanzas
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
636
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
637 @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
638
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
639 """
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
640 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
641
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
642 jid = stanza.get_from_jid()
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
643 bare_jid = jid.bare().as_utf8()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
644
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
645 # 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
646 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
647 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
648
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
649 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
650 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
651
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
652 try:
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
653 # 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
654 # 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
655 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
656 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
657 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
658 else:
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
659 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
660
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
661 # 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
662 # 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
663 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
664 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
665
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
666 except ValueError:
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
667 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
668
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
669 else:
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
670 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
671
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
672 # 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
673 return True
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
674
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
675 def handle_available_presence(self, presence):
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
676 """Handles available presence stanzas
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
677
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
678 @type presence: pyxmpp.presence.Presence
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
679
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
680 """
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
681 self.log.debug("Handling available presence.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
682
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
683 show = presence.get_show()
2104
c2114faf540e Fix the case when pyxmpp returns None instead of "available" as presence type.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2101
diff changeset
684 if show is None:
c2114faf540e Fix the case when pyxmpp returns None instead of "available" as presence type.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2101
diff changeset
685 show = u'available'
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
686
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
687 priority = presence.get_priority()
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
688 jid = presence.get_from_jid()
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
689 bare_jid = jid.bare().as_utf8()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
690
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
691 if bare_jid in self.contacts:
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
692 contact = self.contacts[bare_jid]
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
693
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
694 # The resource is already known, so update it
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
695 if contact.uses_resource(jid.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
696 contact.set_show(jid.resource, show)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
697
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
698 # Unknown resource, add it to the list
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
699 else:
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
700 contact.add_resource(jid.resource, show, priority)
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
701 self.supports(jid, u"jabber:x:data")
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
702
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
703 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
704 self.log.debug(contact)
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
705
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
706 # Either way check, if we can deliver queued messages now
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
707 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
708 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
709
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
710 else:
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
711 self.contacts[bare_jid] = Contact(jid, jid.resource, priority, show)
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
712 self.service_discovery(jid)
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
713 self.get_user_language(bare_jid)
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
714 self.log.debug(self.contacts[bare_jid])
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
715
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
716 # Confirm that we've handled this stanza
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
717 return True
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
718
2488
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
719 def get_user_language(self, jid):
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
720 """Request user's language setting from the wiki
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
721
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
722 @param jid: bare Jabber ID of the user to query for
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
723 @type jid: unicode
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
724 """
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
725 request = cmd.GetUserLanguage(jid)
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
726 self.from_commands.put_nowait(request)
c707250dca17 Keep contact entries longer (with ttl) to keep lang settings.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
727
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
728 def service_discovery(self, jid):
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
729 """Ask a client about supported features
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
730
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
731 This is not the recommended way of discovering support
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
732 for data forms, but it's easy to implement, so it'll be
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
733 like that for now. The proper way to do this is described
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
734 in XEP-0115 (Entity Capabilities)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
735
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
736 @param jid: FULL (user@host/resource) jabber id to query
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
737 @type jid: unicode
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
738
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
739 """
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
740 query = Iq(to_jid=jid, stanza_type="get")
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
741 query.new_query("http://jabber.org/protocol/disco#info")
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
742 self.get_stream().set_response_handlers(query, self.handle_disco_result, None)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
743 self.get_stream().send(query)
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
744
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
745
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
746 def handle_disco_result(self, stanza):
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
747 """Handler for <iq> service discovery results
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
748
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
749 Works with elements qualified by http://jabber.org/protocol/disco#info ns
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
750
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
751 @param stanza: a received result stanza
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
752 """
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
753 payload = stanza.get_query()
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
754
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
755 supports = payload.xpathEval('//*[@var="jabber:x:data"]')
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
756 if supports:
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
757 jid = stanza.get_from_jid()
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
758 self.contacts[jid.bare().as_utf8()].set_supports(jid.resource, u"jabber:x:data")
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2658
diff changeset
759
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
760 supports = payload.xpathEval('//*[@var="jabber:x:oob"]')
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
761 if supports:
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
762 jid = stanza.get_from_jid()
0608417246ee Use a more generic way to store information about supported extensions.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
763 self.contacts[jid.bare().as_utf8()].set_supports(jid.resource, u"jabber:x:oob")
2394
0f9b7b20fb9c Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2393
diff changeset
764
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
765
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
766 def send_queued_messages(self, contact, 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
767 """Sends messages queued for the contact
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
768
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
769 @param contact: a contact whose queued messages are to be sent
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
770 @type contact: jabberbot.xmppbot.Contact
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2115
diff changeset
771 @param ignore_dnd: should contact's DnD status be ignored?
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
772
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
773 """
2077
636e646f909b Make jabber bot aware of contacts' presence.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
774 for command in contact.messages:
2081
6c3ae1a407a0 Fix message delivery to contacts that become unavailable directly from dnd.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2077
diff changeset
775 self.handle_command(command, ignore_dnd)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
776
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
777 def reply_help(self, jid):
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
778 """Constructs a generic help message
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
779
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
780 It's sent in response to an uknown message or the "help" command.
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
781
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
782 """
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
783 _ = 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
784
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
785 msg = _("Hello there! I'm a MoinMoin Notification Bot. Available commands:\
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
786 \n\n%(internal)s\n%(xmlrpc)s")
2115
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
787 internal = ", ".join(self.internal_commands)
260d0dbded44 Checkpoint commit, to record progress on implementation of user commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
788 xmlrpc = ", ".join(self.xmlrpc_commands.keys())
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
789
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
790 return msg % {'internal': internal, 'xmlrpc': xmlrpc}
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
791
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
792 def authenticated(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
793 """Called when authentication succeedes"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
794 self.log.info("Authenticated.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
795
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
796 def authorized(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
797 """Called when authorization succeedes"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
798
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
799 self.log.info("Authorized.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
800
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
801 stream = self.get_stream()
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
802 stream.set_message_handler("normal", self.handle_message)
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
803 stream.set_presence_handler("available", self.handle_available_presence)
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2082
diff changeset
804 stream.set_presence_handler("unavailable", self.handle_unavailable_presence)
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
805 stream.set_presence_handler("unsubscribed", self.handle_unsubscribed_presence)
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
806 stream.set_presence_handler("subscribe", self.handle_subscribe_presence)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
807
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
808 self.request_session()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
809
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
810 def connected(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
811 """Called when connections has been established"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
812 self.log.info("Connected.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
813
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
814 def disconnected(self):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
815 """Called when disconnection occurs"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
816 self.log.info("Disconnected.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
817
2089
4ec961fd40d9 Even more stylistic fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2088
diff changeset
818 def roster_updated(self, item=None):
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
819 """Called when roster gets updated"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
820 self.log.debug("Updating roster.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
821
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
822 def stream_closed(self, stream):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
823 """Called when stream closes"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
824 self.log.debug("Stream closed.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
825
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
826 def stream_created(self, stream):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
827 """Called when stream gets created"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
828 self.log.debug("Stream created.")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
829
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
830 def stream_error(self, error):
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
831 """Called when stream error gets received"""
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
832 self.log.error("Received a stream error.")