Software /
code /
prosody
Annotate
plugins/mod_message.lua @ 1288:d2dc0954ebfd
mod_iq: Limit sub-events to get and set IQs
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Tue, 02 Jun 2009 20:18:02 +0500 |
parent | 1275:850cf92b8ad4 |
child | 1289:d0c38cac1687 |
rev | line source |
---|---|
1232 | 1 |
2 local full_sessions = full_sessions; | |
3 local bare_sessions = bare_sessions; | |
4 | |
5 local jid_bare = require "util.jid".bare; | |
6 local user_exists = require "core.usermanager".user_exists; | |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
7 local offlinemanager = require "core.offlinemanager"; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
8 |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
9 local function select_top_resources(user) |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
10 local priority = 0; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
11 local recipients = {}; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
12 for _, session in pairs(user.sessions) do -- find resource with greatest priority |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
13 if session.presence then |
1275
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
14 -- TODO check active privacy list for session |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
15 local p = session.priority; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
16 if p > priority then |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
17 priority = p; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
18 recipients = {session}; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
19 elseif p == priority then |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
20 t_insert(recipients, session); |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
21 end |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
22 end |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
23 end |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
24 return recipients; |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
25 end |
1232 | 26 |
1271
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
27 local function process_to_bare(bare, origin, stanza) |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
28 local user = bare_sessions[bare]; |
1232 | 29 |
1272
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
30 local t = stanza.attr.type; |
1275
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
31 if t == "error" then |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
32 -- discard |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
33 elseif t == "groupchat" then |
1272
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
34 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
1275
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
35 elseif t == "headline" then |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
36 if user then |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
37 for _, session in pairs(user.sessions) do |
1272
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
38 if session.presence and session.priority >= 0 then |
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
39 session.send(stanza); |
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
40 end |
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
41 end |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
42 end -- current policy is to discard headlines if no recipient is available |
1275
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
43 else -- chat or normal message |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
44 if user then -- some resources are connected |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
45 local recipients = select_top_resources(user); |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
46 if #recipients > 0 then |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
47 for i=1,#recipients do |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
48 recipients[i].send(stanza); |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
49 end |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
50 return true; |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
51 end |
1272
28f9041d8c55
mod_message: Added code to handle error groupchat and headline messages to bare JID
Waqas Hussain <waqas20@gmail.com>
parents:
1271
diff
changeset
|
52 end |
1275
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
53 -- no resources are online |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
54 local node, host = jid_split(bare); |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
55 if user_exists(node, host) then |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
56 -- TODO apply the default privacy list |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
57 offlinemanager.store(node, host, stanza); |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
58 else |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
59 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
850cf92b8ad4
mod_message: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
1274
diff
changeset
|
60 end |
1274
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
61 end |
50babb72edac
mod_message: mod_message now handles all cases
Waqas Hussain <waqas20@gmail.com>
parents:
1272
diff
changeset
|
62 return true; |
1271
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
63 end |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
64 |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
65 module:hook("message/full", function(data) |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
66 -- message to full JID recieved |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
67 local origin, stanza = data.origin, data.stanza; |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
68 |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
69 local session = full_sessions[stanza.attr.to]; |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
70 if session then |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
71 -- TODO fire post processing event |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
72 session.send(stanza); |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
73 return true; |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
74 else -- resource not online |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
75 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza); |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
76 end |
1232 | 77 end); |
1271
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
78 |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
79 module:hook("message/bare", function(data) |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
80 -- message to bare JID recieved |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
81 local origin, stanza = data.origin, data.stanza; |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
82 |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
83 return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza); |
e78c161944ab
mod_message: Move bare JID processing to it's own function
Waqas Hussain <waqas20@gmail.com>
parents:
1234
diff
changeset
|
84 end); |