core/stanza_dispatch.lua
author matthew
Fri, 22 Aug 2008 21:09:04 +0000
changeset 0 3e3171b59028
child 1 b8787e859fd2
permissions -rw-r--r--
First commit, where do you want to go tomorrow?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     1
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     2
require "util.stanza"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     3
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     4
local st = stanza;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     5
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     6
local t_concat = table.concat;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     7
local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     8
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     9
function init_stanza_dispatcher(session)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    10
	local iq_handlers = {};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    11
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    12
	local session_log = session.log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    13
	local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    14
	local send = session.send;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    15
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    16
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    17
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    18
	iq_handlers["jabber:iq:auth"] = 
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    19
		function (stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    20
			local username = stanza[1]:child_with_name("username");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    21
			local password = stanza[1]:child_with_name("password");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    22
			local resource = stanza[1]:child_with_name("resource");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    23
			if not (username and password and resource) then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    24
				local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    25
				send(reply:query("jabber:iq:auth")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    26
					:tag("username"):up()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    27
					:tag("password"):up()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    28
					:tag("resource"):up());
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    29
				return true;			
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    30
			else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    31
				username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    32
				print(username, password, resource)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    33
				local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    34
				require "core.usermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    35
				if usermanager.validate_credentials(session.host, username, password) then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    36
					-- Authentication successful!
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    37
					session.username = username;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    38
					session.resource = resource;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    39
					if not hosts[session.host].sessions[username] then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    40
						hosts[session.host].sessions[username] = { sessions = {} };
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    41
					end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    42
					hosts[session.host].sessions[username].sessions[resource] = session;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    43
					send(st.reply(stanza));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    44
					return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    45
				else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    46
					local reply = st.reply(stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    47
					reply.attr.type = "error";
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    48
					reply:tag("error", { code = "401", type = "auth" })
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    49
						:tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    50
					send(reply);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    51
					return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    52
				end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    53
			end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    54
			
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    55
		end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    56
		
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    57
	iq_handlers["jabber:iq:roster"] =
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    58
		function (stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    59
			if stanza.attr.type == "get" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    60
				session.roster = session.roster or rostermanager.getroster(session.username, session.host);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    61
				if session.roster == false then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    62
					send(st.reply(stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    63
						:tag("error", { type = "wait" })
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    64
						:tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    65
					return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    66
				else session.roster = session.roster or {};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    67
				end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    68
				local roster = st.reply(stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    69
							:query("jabber:iq:roster");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    70
				for jid in pairs(session.roster) do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    71
					roster:tag("item", { jid = jid, subscription = "none" }):up();
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    72
				end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    73
				send(roster);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    74
				return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    75
			end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    76
		end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    77
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    78
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    79
	return	function (stanza)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    80
			log("info", "--> "..tostring(stanza));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    81
			if stanza.name == "iq" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    82
				if not stanza[1] then log("warn", "<iq> without child is invalid"); return; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    83
				if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    84
				local xmlns = stanza[1].attr.xmlns;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    85
				if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    86
				if (((not stanza.attr.to) or stanza.attr.to == session.host or stanza.attr.to:match("@[^/]+$")) and (stanza.attr.type == "get" or stanza.attr.type == "set")) then -- Stanza sent to us
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    87
					if iq_handlers[xmlns] then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    88
						if iq_handlers[xmlns](stanza) then return; end;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    89
					end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    90
					log("warn", "Unhandled namespace: "..xmlns);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    91
					send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    92
				end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    93
			
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    94
			end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    95
							-- Need to route stanza
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    96
			if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    97
				stanza.attr.from = session.username.."@"..session.host;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    98
				session.send_to(stanza.attr.to, stanza);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    99
			end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
   100
		end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
   101
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
   102
end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
   103