Software /
code /
prosody
Annotate
plugins/mod_muc.lua @ 729:f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 16 Jan 2009 05:20:04 +0000 |
parent | 711:7653519ec4f2 |
child | 752:a9642c1d5827 |
rev | line source |
---|---|
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local register_component = require "core.componentmanager".register_component; |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
4 local deregister_component = require "core.componentmanager".deregister_component; |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local jid_split = require "util.jid".split; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local jid_bare = require "util.jid".bare; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local st = require "util.stanza"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local log = require "util.logger".init("mod_muc"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local multitable_new = require "util.multitable".new; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local muc_domain = "conference."..module:get_host(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local muc_name = "MUCMUCMUC!!!"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
14 -- room_name -> room |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
15 -- occupant_room_nick -> data |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
16 -- affiliation = ... |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
17 -- role |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
18 -- jid = occupant's real jid |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 local rooms = multitable_new(); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
20 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
21 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
22 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
23 -- room_name -> info |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
24 -- name - the room's friendly name |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
25 -- subject - the room's subject |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
26 -- non-anonymous = true|nil |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
27 -- persistent = true|nil |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local rooms_info = multitable_new(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
30 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
31 for room in pairs(persist_list) do |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
32 rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
33 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
34 |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local component; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 function getUsingPath(stanza, path, getText) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local tag = stanza; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 for _, name in ipairs(path) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 if type(tag) ~= 'table' then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 tag = tag:child_with_name(name); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 if tag and getText then tag = table.concat(tag); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 return tag; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 function getTag(stanza, path) return getUsingPath(stanza, path); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 function getText(stanza, path) return getUsingPath(stanza, path, true); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 function get_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 :tag("identity", {category='conference', type='text', name=muc_name}):up() |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 function get_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 for room in pairs(rooms_info:get()) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 return reply; -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 function get_room_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up() |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 function get_room_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 end -- TODO allow non-private rooms |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
70 function save_room(room) |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
71 local persistent = rooms_info:get(room, 'persistent'); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
72 if persistent then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
73 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room)); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
74 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
75 if persistent ~= persist_list[room] then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
76 if not persistent then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
77 datamanager.store(room, muc_domain, 'rooms', nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
78 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
79 persist_list[room] = persistent; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
80 datamanager.store(nil, muc_domain, 'room_list', persist_list); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
81 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
82 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
83 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
84 function set_subject(current_nick, room, subject) |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
85 -- TODO check nick's authority |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
86 if subject == "" then subject = nil; end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
87 rooms_info:set(room, 'subject', subject); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
88 save_room(); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
89 broadcast_message(current_nick, room, subject or "", nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
90 return true; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
91 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
92 |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 function broadcast_presence(type, from, room, code) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local data = rooms:get(room, from); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 local stanza = st.presence({type=type, from=from}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 :tag("item", {affiliation=data.affiliation, role=data.role}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 if code then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 stanza:tag("status", {code=code}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 local me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 me = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 if me then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 stanza:tag("status", {code='110'}); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 stanza.attr.to = me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 function broadcast_message(from, room, subject, body) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 local stanza = st.message({type='groupchat', from=from}); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 if subject then stanza:tag('subject'):text(subject):up(); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 if body then stanza:tag('body'):text(body):up(); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 function handle_to_occupant(origin, stanza) -- PM, vCards, etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 local from, to = stanza.attr.from, stanza.attr.to; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 local current_nick = jid_nick:get(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 if stanza.name == "presence" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 if type == "error" then -- error, kick em out! |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 broadcast_presence('unavailable', to, room); -- TODO also add <status>This participant is kicked from the room because he sent an error presence: badformed error stanza</status> |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 elseif type == "unavailable" then -- unavailable |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 if current_nick == to then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 broadcast_presence('unavailable', to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 end -- TODO else do nothing? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 elseif not type then -- available |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 if current_nick then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 if current_nick == to then -- simple presence |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 -- TODO broadcast |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 else -- change nick |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 origin.send(st.error_reply(stanza, "cancel", "conflict")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 local data = rooms:get(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 broadcast_presence('unavailable', current_nick, room, '303'); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 rooms:remove(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 broadcast_presence(nil, to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 else -- enter room |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 origin.send(st.error_reply(stanza, "cancel", "conflict")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 else |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
172 local data; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
173 if not rooms:get(room) and not rooms_info:get(room) then -- new room |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
174 data = {affiliation='owner', role='moderator', jid=from}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
175 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
176 if not data then -- new occupant |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
177 data = {affiliation='none', role='participant', jid=from}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
178 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 local pres = st.presence({to=from, from=occupant}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 core_route_stanza(component, pres); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 broadcast_presence(nil, to, room); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
193 -- TODO send discussion history |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
194 if rooms_info:get(room, 'subject') then |
671
c7519f0c9a2c
mod_muc: Room subject should be sent only the newly joined occupant
Waqas Hussain <waqas20@gmail.com>
parents:
668
diff
changeset
|
195 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject'))); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
196 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 elseif type ~= 'result' then -- bad type |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 elseif stanza.name == "message" and type == "groupchat" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 -- groupchat messages not allowed in PM |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 origin.send(st.error_reply(stanza, "modify", "bad-request")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 if stanza.name == "iq" and type == "get" then -- disco requests |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 if xmlns == "http://jabber.org/protocol/disco#info" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 origin.send(get_room_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 origin.send(get_room_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 elseif stanza.name == "message" and type == "groupchat" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 local from, to = stanza.attr.from, stanza.attr.to; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 local current_nick = jid_nick:get(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 if not current_nick then -- not in room |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 else |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
228 local subject = getText(stanza, {"subject"}); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
229 if subject then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
230 set_subject(current_nick, room, subject); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
231 else |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
232 broadcast_message(current_nick, room, nil, getText(stanza, {"body"})); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
233 -- TODO add to discussion history |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
234 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
237 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 function handle_to_domain(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 if stanza.name == "iq" and type == "get" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 if xmlns == "http://jabber.org/protocol/disco#info" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 origin.send(get_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 origin.send(get_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
252 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
253 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
255 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
256 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
257 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
258 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
259 component = register_component(muc_domain, function(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
260 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
261 if stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
262 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
263 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME what's appropriate? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
264 elseif to_resource and not to_node then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
265 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
266 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
267 elseif to_resource then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
268 handle_to_occupant(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
269 elseif to_node then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
270 handle_to_room(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
271 else -- to the main muc domain |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
272 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
273 handle_to_domain(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
274 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
275 end); |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
276 |
711
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
277 module.unload = function() |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
278 deregister_component(muc_domain); |
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
279 end |
711
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
280 module.save = function() |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
281 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list}; |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
282 end |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
283 module.restore = function(data) |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
284 rooms.data, jid_nick.data, rooms_info.data, persist_list = |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
285 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {}; |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
286 end |