Comparison

main.lua @ 7:dcc5ac721c20

- Remove some debugging code - Don't allow stanzas to be sent before successful auth (needs further review) - Make init_xmlhandlers local - Fix indentation in main.lua - Fix disconnect logic (it broke if client disconnected before auth, etc.)
author Matthew Wild <mwild1@gmail.com>
date Sun, 24 Aug 2008 22:48:27 +0100
parent 6:7ad47ce20394
child 11:67460500abdd
comparison
equal deleted inserted replaced
6:7ad47ce20394 7:dcc5ac721c20
8 function log(type, area, message) 8 function log(type, area, message)
9 print(type, area, message); 9 print(type, area, message);
10 end 10 end
11 11
12 require "core.stanza_dispatch" 12 require "core.stanza_dispatch"
13 init_xmlhandlers = require "core.xmlhandlers" 13 local init_xmlhandlers = require "core.xmlhandlers"
14 require "core.rostermanager" 14 require "core.rostermanager"
15 require "core.offlinemessage" 15 require "core.offlinemessage"
16 require "core.usermanager" 16 require "core.usermanager"
17 require "util.stanza" 17 require "util.stanza"
18 require "util.jid" 18 require "util.jid"
101 end 101 end
102 end 102 end
103 103
104 function handler(conn, data, err) 104 function handler(conn, data, err)
105 local session = sessions[conn]; 105 local session = sessions[conn];
106 106
107 if not session then 107 if not session then
108 sessions[conn] = { conn = conn, notopen = true, priority = 0 }; 108 sessions[conn] = { conn = conn, notopen = true, priority = 0 };
109 session = sessions[conn]; 109 session = sessions[conn];
110 110
111 -- Logging functions -- 111 -- Logging functions --
112 112
113 local mainlog, log = log; 113 local mainlog, log = log;
114 do 114 do
115 local conn_name = tostring(conn):match("%w+$"); 115 local conn_name = tostring(conn):match("%w+$");
116 log = function (type, area, message) mainlog(type, conn_name, message); end 116 log = function (type, area, message) mainlog(type, conn_name, message); end
117 --log = function () end
117 end 118 end
118 local print = function (...) log("info", "core", t_concatall({...}, "\t")); end 119 local print = function (...) log("info", "core", t_concatall({...}, "\t")); end
119 session.log = log; 120 session.log = log;
120 121
121 -- -- -- 122 -- -- --
123 -- Send buffers -- 124 -- Send buffers --
124 125
125 local send = function (data) print("Sending...", tostring(data)); conn.write(tostring(data)); end; 126 local send = function (data) print("Sending...", tostring(data)); conn.write(tostring(data)); end;
126 session.send, session.send_to = send, send_to; 127 session.send, session.send_to = send, send_to;
127 128
128 print("Client connected"); 129 print("Client connected");
129 130
130 session.stanza_dispatch = init_stanza_dispatcher(session); 131 session.stanza_dispatch = init_stanza_dispatcher(session);
131 session.xml_handlers = init_xmlhandlers(session); 132 session.xml_handlers = init_xmlhandlers(session);
132 session.parser = lxp.new(session.xml_handlers, ":"); 133 session.parser = lxp.new(session.xml_handlers, ":");
133 134
134 function session.disconnect(err) 135 function session.disconnect(err)
135 if session.last_presence.attr.type ~= "unavailable" then 136 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then
136 local pres = st.presence{ type = "unavailable" }; 137 local pres = st.presence{ type = "unavailable" };
137 if err == "closed" then err = "connection closed"; end 138 if err == "closed" then err = "connection closed"; end
138 pres:tag("status"):text("Disconnected: "..err); 139 pres:tag("status"):text("Disconnected: "..err);
139 session.stanza_dispatch(pres); 140 session.stanza_dispatch(pres);
140 end 141 end
142 if session.username then
141 hosts[session.host].sessions[session.username] = nil; 143 hosts[session.host].sessions[session.username] = nil;
142 session = nil;
143 print("Disconnected: "..err);
144 end 144 end
145 session = nil;
146 print("Disconnected: "..err);
147 collectgarbage("collect");
145 end 148 end
149 end
146 if data then 150 if data then
147 session.parser:parse(data); 151 session.parser:parse(data);
148 end 152 end
149 153
150 --log("info", "core", "Client disconnected, connection closed"); 154 --log("info", "core", "Client disconnected, connection closed");