Software /
code /
prosody
Annotate
net/xmppclient_listener.lua @ 232:20745f8f4cf1
Actually show error and position when we show a traceback :)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 09 Nov 2008 20:15:31 +0000 |
parent | 226:ba4711c4e8d2 |
child | 267:6303b7454ad0 |
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) |
226
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
63 |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
64 -- Debug version -- |
232
20745f8f4cf1
Actually show error and position when we show a traceback :)
Matthew Wild <mwild1@gmail.com>
parents:
226
diff
changeset
|
65 local function handleerr(err) print("Traceback:", err, debug.traceback()); end |
226
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
66 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
67 |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
68 -- 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
|
69 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if data then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 session.data(conn, data); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 function xmppclient.disconnect(conn) |
123
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
77 local session = sessions[conn]; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
78 if session then |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
79 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
|
80 local pres = st.presence{ type = "unavailable" }; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
81 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
|
82 pres:tag("status"):text("Disconnected: "..err); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
83 session.stanza_dispatch(pres); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
84 end |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
85 sm_destroy_session(session); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
86 sessions[conn] = nil; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
87 session = nil; |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
88 collectgarbage("collect"); |
ebd65feb188c
Fix for not destroying sessions when connection closed.
Matthew Wild <mwild1@gmail.com>
parents:
99
diff
changeset
|
89 end |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 connlisteners_register("xmppclient", xmppclient); |