Software /
code /
prosody
Annotate
net/xmppclient_listener.lua @ 172:71247788c7c7
Inbound subscription cancellation
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 25 Oct 2008 02:29:58 +0500 |
parent | 166:d4ee015fcee4 |
child | 226:ba4711c4e8d2 |
rev | line source |
---|---|
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local logger = require "logger"; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local lxp = require "lxp" |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local init_xmlhandlers = require "core.xmlhandlers" |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local sm_new_session = require "core.sessionmanager".new_session; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local connlisteners_register = require "net.connlisteners".register; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local t_insert = table.insert; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local t_concat = table.concat; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local m_random = math.random; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local format = string.format; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local sm_streamopened = sessionmanager.streamopened; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local st = stanza; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local sessions = {}; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local xmppclient = { default_port = 5222 }; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 -- These are session methods -- |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local function session_reset_stream(session) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 -- Reset stream |
148 | 25 local parser = lxp.new(init_xmlhandlers(session, sm_streamopened), "|"); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 session.parser = parser; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 session.notopen = true; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 function session.data(conn, data) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 parser:parse(data); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 return true; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 -- End of session methods -- |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 function xmppclient.listener(conn, data) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local session = sessions[conn]; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if not session then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 session = sm_new_session(conn); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 sessions[conn] = session; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 -- Logging functions -- |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local mainlog, log = log; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 do |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local conn_name = tostring(conn):match("[a-f0-9]+$"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 log = logger.init(conn_name); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local print = function (...) log("info", t_concatall({...}, "\t")); end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 session.log = log; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 print("Client connected"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 session.reset_stream = session_reset_stream; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 session_reset_stream(session); -- Initialise, ready for use |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 -- TODO: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 -- this will avoid the useless indirection we have atm |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 -- (I'm on a mission, no time to fix now) |
166
d4ee015fcee4
Backed out changeset 4adc53e03b4d (garbage collection)
Matthew Wild <mwild1@gmail.com>
parents:
165
diff
changeset
|
63 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 if data then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 session.data(conn, data); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 function xmppclient.disconnect(conn) |
123
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
72 local session = sessions[conn]; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
73 if session then |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
74 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
75 local pres = st.presence{ type = "unavailable" }; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
76 if err == "closed" then err = "connection closed"; end |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
77 pres:tag("status"):text("Disconnected: "..err); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
78 session.stanza_dispatch(pres); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
79 end |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
80 sm_destroy_session(session); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
81 sessions[conn] = nil; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
82 session = nil; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
83 collectgarbage("collect"); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
84 end |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 connlisteners_register("xmppclient", xmppclient); |