2006-11-02 20:18:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_private_odbc.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2006-11-02 20:18:48 +01:00
|
|
|
%%% Purpose : Private storage support
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 5 Oct 2006 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2006-11-02 20:18:48 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_private_odbc).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2006-11-02 20:18:48 +01:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
|
|
|
process_sm_iq/3,
|
|
|
|
remove_user/2]).
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2006-11-02 20:18:48 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2006-11-02 20:18:48 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2006-11-02 20:18:48 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE,
|
2006-11-02 20:18:48 +01:00
|
|
|
?MODULE, process_sm_iq, IQDisc).
|
|
|
|
|
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2006-11-02 20:18:48 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE).
|
2006-11-02 20:18:48 +01:00
|
|
|
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_sm_iq(From, To, #iq{type = Type} = IQ_Rec) ->
|
|
|
|
case check_packet(From, To, IQ_Rec) of
|
|
|
|
ok ->
|
2006-11-02 20:18:48 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_set(From, To, IQ_Rec);
|
2006-11-02 20:18:48 +01:00
|
|
|
get ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, To, IQ_Rec)
|
2006-11-02 20:18:48 +01:00
|
|
|
end;
|
2008-10-07 14:20:09 +02:00
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
2006-11-02 20:18:48 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
LUser = exmpp_jid:lnode_as_list(From),
|
|
|
|
LServer = exmpp_jid:ldomain_as_list(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
case catch get_data(LUser,
|
|
|
|
LServer,
|
|
|
|
exmpp_xml:get_child_elements(SubEl)) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
{error, 'internal-server-error'};
|
|
|
|
Res ->
|
|
|
|
exmpp_iq:result(IQ_Rec, #xmlel{ns = ?NS_PRIVATE,
|
|
|
|
name = 'query',
|
|
|
|
children = Res})
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
process_iq_set(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
LUser = exmpp_jid:lnode_as_list(From),
|
|
|
|
LServer = exmpp_jid:ldomain_as_list(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
set_data(LUser, LServer, El)
|
|
|
|
end, exmpp_xml:get_child_elements(SubEl))
|
|
|
|
end,
|
|
|
|
odbc_queries:sql_transaction(LServer, F),
|
|
|
|
exmpp_iq:result(IQ_Rec).
|
|
|
|
|
|
|
|
|
|
|
|
check_packet(From, To, IQ_Rec) ->
|
|
|
|
check_packet(From, To, IQ_Rec, [ fun check_domain/3,
|
|
|
|
fun check_user/3,
|
|
|
|
fun check_ns/3]).
|
|
|
|
check_packet(_From, _To, _IQ_Rec, []) ->
|
|
|
|
ok;
|
|
|
|
check_packet(From, To, IQ_Rec, [F | R]) ->
|
|
|
|
case F(From, To, IQ_Rec) of
|
|
|
|
{error, _} = Error -> Error;
|
|
|
|
ok -> check_packet(From, To, IQ_Rec, R)
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_domain(From, _To, _IQ_Rec) ->
|
|
|
|
LServer = exmpp_jid:ldomain_as_list(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'not-allowed'}
|
|
|
|
end.
|
|
|
|
|
|
|
|
% the iq can't be directed to another jid
|
|
|
|
check_user(From, To, _IQ_Rec) ->
|
|
|
|
case exmpp_jid:compare_bare_jids(From, To) of
|
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'forbidden'}
|
2006-11-02 20:18:48 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
%there must be at least one child, and every child should have
|
|
|
|
%a namespace specified (reject if the namespace is jabber:iq:private,
|
|
|
|
%the same than the parent element).
|
|
|
|
check_ns(_From, _To, #iq{payload = SubEl}) ->
|
|
|
|
case exmpp_xml:get_child_elements(SubEl) of
|
|
|
|
[] ->
|
|
|
|
{error, 'not-acceptable'};
|
|
|
|
Children ->
|
|
|
|
case lists:any(fun(Child) ->
|
|
|
|
exmpp_xml:get_ns_as_atom(Child) =:= ?NS_PRIVATE
|
|
|
|
end, Children) of
|
|
|
|
true -> {error, 'not-acceptable'};
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set_data(LUser, LServer, El) ->
|
|
|
|
XMLNS = exmpp_xml:get_ns_as_list(El),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
|
|
|
SData = ejabberd_odbc:escape(exmpp_xml:document_to_list(El)),
|
|
|
|
odbc_queries:set_private_data(LServer, Username, LXMLNS, SData).
|
|
|
|
|
2006-11-02 20:18:48 +01:00
|
|
|
get_data(LUser, LServer, Els) ->
|
|
|
|
get_data(LUser, LServer, Els, []).
|
|
|
|
|
|
|
|
get_data(_LUser, _LServer, [], Res) ->
|
|
|
|
lists:reverse(Res);
|
|
|
|
get_data(LUser, LServer, [El | Els], Res) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
XMLNS = exmpp_xml:get_ns_as_list(El),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
|
|
|
case catch odbc_queries:get_private_data(LServer, Username, LXMLNS) of
|
|
|
|
{selected, ["data"], [{SData}]} ->
|
2009-01-03 16:15:38 +01:00
|
|
|
[Data] = exmpp_xml:parse_document(SData,
|
|
|
|
[names_as_atom, {check_elems, xmpp},
|
|
|
|
{check_nss,xmpp}, {check_attrs,xmpp}]),
|
2008-10-07 14:20:09 +02:00
|
|
|
get_data(LUser, LServer, Els, [Data | Res]);
|
|
|
|
%% MREMOND: I wonder when the query could return a vcard ?
|
|
|
|
{selected, ["vcard"], []} ->
|
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[El | Res]);
|
|
|
|
_ ->
|
|
|
|
get_data(LUser, LServer, Els, [El | Res])
|
|
|
|
end.
|
2006-11-02 20:18:48 +01:00
|
|
|
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server) when is_binary(User), is_binary(Server) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_user_private_storage(LServer, Username)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|