annotate src/plugins/plugin_xep_0085.py @ 1252:5ff9f9af9d1f

plugin XEP-0085: use the full JID + fixes bad entity data "type" value
author souliane <souliane@mailoo.org>
date Mon, 20 Oct 2014 12:57:55 +0200
parents 301b342c697a
children c13a46207410
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
3
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Chat State Notifications Protocol (xep-0085)
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 771
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Adrien Cossa (souliane@mailoo.org)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
6
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
11
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
16
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 724
diff changeset
20 from sat.core.i18n import _
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 908
diff changeset
21 from sat.core.constants import Const as C
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
23 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
24 log = getLogger(__name__)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from zope.interface import implements
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.jid import JID
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
28 try:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
29 from twisted.words.protocols.xmlstream import XMPPHandler
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
30 except ImportError:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
31 from wokkel.subprotocols import XMPPHandler
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from threading import Timer
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 659
diff changeset
33 from twisted.words.xish import domish
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
34
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
35 NS_XMPP_CLIENT = "jabber:client"
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
36 NS_CHAT_STATES = "http://jabber.org/protocol/chatstates"
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
37 CHAT_STATES = ["active", "inactive", "gone", "composing", "paused"]
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
38 MESSAGE_TYPES = ["chat", "groupchat"]
643
262d9d9ad27a plugin XEP-0085: renamed category and parameter
souliane <souliane@mailoo.org>
parents: 636
diff changeset
39 PARAM_KEY = "Notifications"
262d9d9ad27a plugin XEP-0085: renamed category and parameter
souliane <souliane@mailoo.org>
parents: 636
diff changeset
40 PARAM_NAME = "Enable chat state notifications"
262d9d9ad27a plugin XEP-0085: renamed category and parameter
souliane <souliane@mailoo.org>
parents: 636
diff changeset
41 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
42
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
43 PLUGIN_INFO = {
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
44 "name": "Chat State Notifications Protocol Plugin",
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
45 "import_name": "XEP-0085",
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
46 "type": "XEP",
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
47 "protocols": ["XEP-0085"],
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
48 "dependencies": [],
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
49 "main": "XEP_0085",
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
50 "handler": "yes",
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
51 "description": _("""Implementation of Chat State Notifications Protocol""")
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
52 }
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
53
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
54
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
55 # Describe the internal transitions that are triggered
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
56 # by a timer. Beside that, external transitions can be
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
57 # runned to target the states "active" or "composing".
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
58 # Delay is specified here in seconds.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
59 TRANSITIONS = {
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
60 "active": {"next_state": "inactive", "delay": 120},
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
61 "inactive": {"next_state": "gone", "delay": 480},
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
62 "gone": {"next_state": "", "delay": 0},
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
63 "composing": {"next_state": "paused", "delay": 30},
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
64 "paused": {"next_state": "inactive", "delay": 450}
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
65 }
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
66
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
67
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
68 class UnknownChatStateException(Exception):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
69 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
70 This error is raised when an unknown chat state is used.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
71 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
72 pass
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
73
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
74
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
75 class XEP_0085(object):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
76 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
77 Implementation for XEP 0085
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
78 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
79 params = """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
80 <params>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
81 <individual>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
82 <category name="%(category_name)s" label="%(category_label)s">
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
83 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" security="0"/>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
84 </category>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
85 </individual>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
86 </params>
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
87 """ % {
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
88 'category_name': PARAM_KEY,
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
89 'category_label': _(PARAM_KEY),
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
90 'param_name': PARAM_NAME,
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
91 'param_label': _('Enable chat state notifications')
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
92 }
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
93
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
94 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
95 log.info(_("Chat State Notifications plugin initialization"))
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
96 self.host = host
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
97
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
98 # parameter value is retrieved before each use
662
4f747d7fde8c core: importParams renamed to updateParams: it now updates the parameter instead of appending children if it find an existing one.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
99 host.memory.updateParams(self.params)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
100
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
101 # triggers from core
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
102 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
697
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
103 host.trigger.add("sendMessage", self.sendMessageTrigger)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
104 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
105 # TODO: handle profile disconnection (free memory in entity data)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
106
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
107 # args: to_s (jid as string), profile
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
108 host.bridge.addMethod("chatStateComposing", ".plugin", in_sign='ss',
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
109 out_sign='', method=self.chatStateComposing)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
110
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
111 # args: from (jid as string), state in CHAT_STATES, profile
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
112 host.bridge.addSignal("chatStateReceived", ".plugin", signature='sss')
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
113
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
114 def getHandler(self, profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
115 return XEP_0085_handler(self, profile)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
116
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
117 def updateEntityData(self, entity_jid, value, profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
118 """
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
119 Update the entity data of the given profile for one or all contacts.
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
120 Reset the chat state(s) display if the notification has been disabled.
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
121 @param entity_jid: contact's JID, or '@ALL@' to update all contacts.
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 908
diff changeset
122 @param value: True, False or C.PROF_KEY_NONE to delete the entity data
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
123 @param profile: current profile
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
124 """
643
262d9d9ad27a plugin XEP-0085: renamed category and parameter
souliane <souliane@mailoo.org>
parents: 636
diff changeset
125 self.host.memory.updateEntityData(entity_jid, ENTITY_KEY, value, profile)
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 908
diff changeset
126 if not value or value == C.PROF_KEY_NONE:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
127 # disable chat state for this or these contact(s)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
128 self.host.bridge.chatStateReceived(unicode(entity_jid), "", profile)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
129
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
130 def paramUpdateTrigger(self, name, value, category, type_, profile):
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
131 """
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
132 Reset all the existing chat state entity data associated with this profile after a parameter modification.
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
133 @param name: parameter name
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
134 @param value: "true" to activate the notifications, or any other value to delete it
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
135 @param category: parameter category
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
136 @param type_: parameter type
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
137 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
138 if (category, name) == (PARAM_KEY, PARAM_NAME):
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 908
diff changeset
139 self.updateEntityData("@ALL@", True if value == "true" else C.PROF_KEY_NONE, profile)
868
967b94ef821e plugin XEP-0085: fixed bad trigger return value
Goffi <goffi@goffi.org>
parents: 811
diff changeset
140 return False
967b94ef821e plugin XEP-0085: fixed bad trigger return value
Goffi <goffi@goffi.org>
parents: 811
diff changeset
141 return True
967b94ef821e plugin XEP-0085: fixed bad trigger return value
Goffi <goffi@goffi.org>
parents: 811
diff changeset
142
663
8004c7d4aba7 core: Deferred in onMessage.
Goffi <goffi@goffi.org>
parents: 662
diff changeset
143 def messageReceivedTrigger(self, message, post_treat, profile):
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
144 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
145 Update the entity cache when we receive a message with body.
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
146 Check for a chat state in the message and signal frontends.
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
147 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
148 if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
149 return True
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
150
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
151 from_jid = JID(message.getAttribute("from"))
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
152 if self.__isMUC(from_jid, profile):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
153 from_jid = from_jid.userhostJID()
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
154 else: # update entity data for one2one chat
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
155 assert(from_jid.resource)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
156 try:
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
157 domish.generateElementsNamed(message.elements(), name="body").next()
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
158 try:
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
159 domish.generateElementsNamed(message.elements(), name="active").next()
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
160 # contact enabled Chat State Notifications
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
161 self.updateEntityData(from_jid, True, profile)
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
162 except StopIteration:
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
163 if message.getAttribute('type') == 'chat':
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
164 # contact didn't enable Chat State Notifications
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
165 self.updateEntityData(from_jid, False, profile)
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
166 return True
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
167 except StopIteration:
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
168 pass
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
169
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
170 # send our next "composing" states to any MUC and to the contacts who enabled the feature
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
171 self.__chatStateInit(from_jid, message.getAttribute("type"), profile)
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
172
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 659
diff changeset
173 state_list = [child.name for child in message.elements() if
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
174 message.getAttribute("type") in MESSAGE_TYPES
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
175 and child.name in CHAT_STATES
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
176 and child.defaultUri == NS_CHAT_STATES]
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
177 for state in state_list:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
178 # there must be only one state according to the XEP
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
179 if state != 'gone' or message.getAttribute('type') != 'groupchat':
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
180 self.host.bridge.chatStateReceived(message.getAttribute("from"), state, profile)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
181 break
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
182 return True
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
183
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 916
diff changeset
184 def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
185 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
186 Eventually add the chat state to the message and initiate
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
187 the state machine when sending an "active" state.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
188 """
697
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
189 def treatment(mess_data):
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
190 message = mess_data['xml']
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
191 to_jid = JID(message.getAttribute("to"))
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
192 if not self.__checkActivation(to_jid, forceEntityData=True, profile=profile):
724
6edd9bda4e3f plugin XEP-0085: fixed bad return value for sendMessage trigger's post treatment
Goffi <goffi@goffi.org>
parents: 698
diff changeset
193 return mess_data
697
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
194 try:
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
195 # message with a body always mean active state
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
196 domish.generateElementsNamed(message.elements(), name="body").next()
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
197 message.addElement('active', NS_CHAT_STATES)
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
198 # launch the chat state machine (init the timer)
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
199 if self.__isMUC(to_jid, profile):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
200 to_jid = to_jid.userhostJID()
697
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
201 self.__chatStateActive(to_jid, mess_data["type"], profile)
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
202 except StopIteration:
698
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
203 if "chat_state" in mess_data["extra"]:
d731ae066158 core: sendMessage's options parameter has been renamed to extra to be consistent with newMessage
Goffi <goffi@goffi.org>
parents: 697
diff changeset
204 state = mess_data["extra"].pop("chat_state")
697
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
205 assert(state in CHAT_STATES)
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
206 message.addElement(state, NS_CHAT_STATES)
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
207 return mess_data
0c84fb112d70 core: sendMessage triggers now use a treatments deferred;
Goffi <goffi@goffi.org>
parents: 663
diff changeset
208
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 916
diff changeset
209 post_xml_treatments.addCallback(treatment)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
210 return True
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
211
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
212 def __isMUC(self, to_jid, profile):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
213 """Tell if that JID is a MUC or not
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
214
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
215 @param to_jid (JID): full or bare JID to check
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
216 @param profile (str): %(doc_profile)s
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
217 @return: bool
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
218 """
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
219 try:
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
220 type_ = self.host.memory.getEntityDatum(to_jid.userhostJID(), 'type', profile)
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
221 if type_ == 'chatroom':
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
222 return True
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
223 except (exceptions.UnknownEntityError, KeyError):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
224 pass
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
225 return False
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
226
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
227 def __checkActivation(self, to_jid, forceEntityData, profile):
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
228 """
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
229 @param to_jid: the contact's full JID (or bare if you know it's a MUC)
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
230 @param forceEntityData: if set to True, a non-existing
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
231 entity data will be considered to be True (and initialized)
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
232 @param: current profile
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
233 @return: True if the notifications should be sent to this JID.
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
234 """
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
235 # check if the parameter is active
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
236 if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile):
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
237 return False
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
238 # check if notifications should be sent to this contact
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
239 if self.__isMUC(to_jid, profile):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
240 return True
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
241 assert(to_jid.resource or not self.host.memory.isContactConnected(to_jid, profile))
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
242 try:
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
243 return self.host.memory.getEntityDatum(to_jid, ENTITY_KEY, profile)
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
244 except (exceptions.UnknownEntityError, KeyError):
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
245 if forceEntityData:
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
246 # enable it for the first time
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
247 self.updateEntityData(to_jid, True, profile)
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
248 return True
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
249 # wait for the first message before sending states
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
250 return False
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
251
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
252 def __chatStateInit(self, to_jid, mess_type, profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
253 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
254 Data initialization for the chat state machine.
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
255
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
256 @param to_jid (JID): full JID for one2one, bare JID for MUC
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
257 @param mess_type (str): "one2one" or "groupchat"
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
258 @param profile (str): %(doc_profile)s
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
259 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
260 if mess_type is None:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
261 return
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
262 if not hasattr(self, "map"):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
263 self.map = {}
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
264 profile_map = self.map.setdefault(profile, {})
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
265 if to_jid not in profile_map:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
266 machine = ChatStateMachine(self.host, to_jid,
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
267 mess_type, profile)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
268 self.map[profile][to_jid] = machine
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
269
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
270 def __chatStateActive(self, to_jid, mess_type, profile_key):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
271 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
272 Launch the chat state machine on "active" state.
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
273
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
274 @param to_jid (JID): full JID for one2one, bare JID for MUC
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
275 @param mess_type (str): "one2one" or "groupchat"
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
276 @param profile (str): %(doc_profile)s
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
277 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
278 profile = self.host.memory.getProfileName(profile_key)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
279 if profile is None:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
280 raise exceptions.ProfileUnknownError
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
281 self.__chatStateInit(to_jid, mess_type, profile)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
282 self.map[profile][to_jid]._onEvent("active")
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
283
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
284 def chatStateComposing(self, to_jid_s, profile_key):
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
285 """Move to the "composing" state when required.
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
286
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
287 Since this method is called from the front-end, it needs to check the
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
288 values of the parameter "Send chat state notifications" and the entity
654
5c5cf5bca240 plugin XEP-0085: improvement for sending "composing" state
souliane <souliane@mailoo.org>
parents: 643
diff changeset
289 data associated to the target JID.
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
290
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
291 @param to_jid_s (str): contact full JID as a string
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
292 @param profile_key (str): %(doc_profile_key)s
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
293 """
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
294 # TODO: try to optimize this method which is called often
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
295 profile = self.host.memory.getProfileName(profile_key)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
296 if profile is None:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
297 raise exceptions.ProfileUnknownError
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
298 to_jid = JID(to_jid_s)
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
299 if self.__isMUC(to_jid, profile):
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
300 to_jid = to_jid.userhostJID()
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
301 elif not to_jid.resource:
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
302 to_jid.resource = self.host.memory.getLastResource(to_jid, profile)
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
303 if not self.__checkActivation(to_jid, forceEntityData=False, profile=profile):
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
304 return
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
305 try:
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
306 self.map[profile][to_jid]._onEvent("composing")
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
307 except (KeyError, AttributeError):
659
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
308 # no message has been sent/received since the notifications
b6c22d9f593a plugin xep-0085: bug fix + improvement
souliane <souliane@mailoo.org>
parents: 654
diff changeset
309 # have been enabled, it's better to wait for a first one
654
5c5cf5bca240 plugin XEP-0085: improvement for sending "composing" state
souliane <souliane@mailoo.org>
parents: 643
diff changeset
310 pass
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
311
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
312
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
313 class ChatStateMachine:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
314 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
315 This class represents a chat state, between one profile and
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
316 one target contact. A timer is used to move from one state
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
317 to the other. The initialization is done through the "active"
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
318 state which is internally set when a message is sent. The state
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
319 "composing" can be set externally (through the bridge by a
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
320 frontend). Other states are automatically set with the timer.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
321 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
322
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
323 def __init__(self, host, to_jid, mess_type, profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
324 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
325 Initialization need to store the target, message type
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
326 and a profile for sending later messages.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
327 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
328 self.host = host
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
329 self.to_jid = to_jid
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
330 self.mess_type = mess_type
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
331 self.profile = profile
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
332 self.state = None
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
333 self.timer = None
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
334
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
335 def _onEvent(self, state):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
336 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
337 Move to the specified state, eventually send the
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
338 notification to the contact (the "active" state is
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
339 automatically sent with each message) and set the timer.
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
340 """
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
341 if state != self.state and state != "active":
908
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
342 if state != 'gone' or self.mess_type != 'groupchat':
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
343 # send a new message without body
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
344 self.host.sendMessage(self.to_jid, '', '', self.mess_type,
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
345 extra={"chat_state": state},
2ef523f0b5c3 plugin XEP-0085: bug fixes, especially for groupchat messages
souliane <souliane@mailoo.org>
parents: 868
diff changeset
346 profile_key=self.profile)
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
347 self.state = state
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
348 if self.timer is not None:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
349 self.timer.cancel()
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
350
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
351 if state not in TRANSITIONS:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
352 return
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
353 if "next_state" not in TRANSITIONS[state]:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
354 return
1252
5ff9f9af9d1f plugin XEP-0085: use the full JID + fixes bad entity data "type" value
souliane <souliane@mailoo.org>
parents: 993
diff changeset
355 if "delay" not in TRANSITIONS[state]:
636
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
356 return
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
357 next_state = TRANSITIONS[state]["next_state"]
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
358 delay = TRANSITIONS[state]["delay"]
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
359 if next_state == "" or delay < 0:
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
360 return
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
361 self.timer = Timer(delay, self._onEvent, [next_state])
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
362 self.timer.start()
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
363
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
364
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
365 class XEP_0085_handler(XMPPHandler):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
366 implements(iwokkel.IDisco)
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
367
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
368 def __init__(self, plugin_parent, profile):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
369 self.plugin_parent = plugin_parent
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
370 self.host = plugin_parent.host
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
371 self.profile = profile
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
372
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
373 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
374 return [disco.DiscoFeature(NS_CHAT_STATES)]
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
375
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
376 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
7ea6d5a86e58 plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
diff changeset
377 return []