25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Patch ODBC modules for MH (thanks to Alexey Shchepin and Geoff Cant)

This commit is contained in:
Badlop 2010-07-22 18:42:41 +02:00
parent a1a6bd79ac
commit bb77c39553
6 changed files with 86 additions and 50 deletions

View File

@ -49,7 +49,7 @@
-include("web/ejabberd_http.hrl"). -include("web/ejabberd_http.hrl").
-include("web/ejabberd_web_admin.hrl"). -include("web/ejabberd_web_admin.hrl").
-record(offline_msg, {user, timestamp, expire, from, to, packet}). -record(offline_msg, {user, server, timestamp, expire, from, to, packet}).
-define(PROCNAME, ejabberd_offline). -define(PROCNAME, ejabberd_offline).
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000). -define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
@ -88,15 +88,15 @@ start(Host, Opts) ->
loop(Host, AccessMaxOfflineMsgs) -> loop(Host, AccessMaxOfflineMsgs) ->
receive receive
#offline_msg{user = User} = Msg -> #offline_msg{user = User, server = Server} = Msg ->
Msgs = receive_all(User, [Msg]), Msgs = receive_all(User, Server, [Msg]),
Len = length(Msgs), Len = length(Msgs),
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs, MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
User, Host), User, Host),
%% Only count existing messages if needed: %% Only count existing messages if needed:
Count = if MaxOfflineMsgs =/= infinity -> Count = if MaxOfflineMsgs =/= infinity ->
Len + count_offline_messages(User, Host); Len + count_offline_messages(User, Server);
true -> 0 true -> 0
end, end,
if if
@ -129,9 +129,9 @@ loop(Host, AccessMaxOfflineMsgs) ->
XML = XML =
ejabberd_odbc:escape( ejabberd_odbc:escape(
exmpp_xml:document_to_list(Packet1)), exmpp_xml:document_to_list(Packet1)),
odbc_queries:add_spool_sql(Username, XML) odbc_queries:add_spool_sql(Server, Username, XML)
end, Msgs), end, Msgs),
case catch odbc_queries:add_spool(Host, Query) of case catch odbc_queries:add_spool(Server, Query) of
{'EXIT', Reason} -> {'EXIT', Reason} ->
?ERROR_MSG("~p~n", [Reason]); ?ERROR_MSG("~p~n", [Reason]);
{error, Reason} -> {error, Reason} ->
@ -154,10 +154,10 @@ get_max_user_messages(AccessRule, LUser, Host) ->
_ -> ?MAX_USER_MESSAGES _ -> ?MAX_USER_MESSAGES
end. end.
receive_all(Username, Msgs) -> receive_all(Username, Server, Msgs) ->
receive receive
#offline_msg{user=Username} = Msg -> #offline_msg{user = Username, server = Server} = Msg ->
receive_all(Username, [Msg | Msgs]) receive_all(Username, Server, [Msg | Msgs])
after 0 -> after 0 ->
lists:reverse(Msgs) lists:reverse(Msgs)
end. end.
@ -208,10 +208,19 @@ store_packet(From, To, Packet) ->
case check_event_chatstates(From, To, Packet) of case check_event_chatstates(From, To, Packet) of
true -> true ->
LUser = exmpp_jid:prep_node_as_list(To), LUser = exmpp_jid:prep_node_as_list(To),
LServer = exmpp_jid:prep_domain_as_list(To),
TimeStamp = now(), TimeStamp = now(),
Expire = find_x_expire(TimeStamp, Packet#xmlel.children), Expire = find_x_expire(TimeStamp, Packet#xmlel.children),
gen_mod:get_module_proc(exmpp_jid:prep_domain_as_list(To), ?PROCNAME) ! Proc1 = gen_mod:get_module_proc(LServer, ?PROCNAME),
Proc = case whereis(Proc1) of
undefined ->
gen_mod:get_module_proc(global, ?PROCNAME);
_ ->
Proc1
end,
Proc !
#offline_msg{user = LUser, #offline_msg{user = LUser,
server = LServer,
timestamp = TimeStamp, timestamp = TimeStamp,
expire = Expire, expire = Expire,
from = From, from = From,
@ -377,12 +386,14 @@ user_queue(User, Server, Query, Lang) ->
LUser = exmpp_stringprep:nodeprep(User), LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server), LServer = exmpp_stringprep:nameprep(Server),
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
US0 = {LUser, LServer}, US0 = {LUser, LServer},
R = user_queue_parse_query(Username, LServer, Query), R = user_queue_parse_query(Username, LServer, Query),
M = case catch ejabberd_odbc:sql_query( M = case catch ejabberd_odbc:sql_query(
LServer, LServer,
["select username, xml from spool" ["select username, xml from spool"
" where username='", Username, "'" " where username='", Username, "'"
" and host='", Host, "'"
" order by seq;"]) of " order by seq;"]) of
{selected, ["username", "xml"], Rs} -> {selected, ["username", "xml"], Rs} ->
lists:flatmap( lists:flatmap(
@ -449,10 +460,12 @@ user_queue(User, Server, Query, Lang) ->
user_queue_parse_query(Username, LServer, Query) -> user_queue_parse_query(Username, LServer, Query) ->
case lists:keysearch("delete", 1, Query) of case lists:keysearch("delete", 1, Query) of
{value, _} -> {value, _} ->
Host = ejabberd_odbc:escape(LServer),
Msgs = case catch ejabberd_odbc:sql_query( Msgs = case catch ejabberd_odbc:sql_query(
LServer, LServer,
["select xml, seq from spool" ["select xml, seq from spool"
" where username='", Username, "'" " where username='", Username, "' and"
" host='", Host, "'"
" order by seq;"]) of " order by seq;"]) of
{selected, ["xml", "seq"], Rs} -> {selected, ["xml", "seq"], Rs} ->
lists:flatmap( lists:flatmap(
@ -481,7 +494,7 @@ user_queue_parse_query(Username, LServer, Query) ->
catch ejabberd_odbc:sql_query( catch ejabberd_odbc:sql_query(
LServer, LServer,
["delete from spool" ["delete from spool"
" where username='", Username, "'" " where username='", Username, "' and host='", Host, "'"
" and seq='", SSeq, "';"]); " and seq='", SSeq, "';"]);
false -> false ->
ok ok
@ -498,10 +511,12 @@ us_to_list({User, Server}) ->
exmpp_jid:to_list(User, Server). exmpp_jid:to_list(User, Server).
get_queue_length(Username, LServer) -> get_queue_length(Username, LServer) ->
Host = ejabberd_odbc:escape(LServer),
case catch ejabberd_odbc:sql_query( case catch ejabberd_odbc:sql_query(
LServer, LServer,
["select count(*) from spool" ["select count(*) from spool"
" where username='", Username, "';"]) of " where username='", Username, "'"
" and host='", Host, "';"]) of
{selected, [_], [{SCount}]} -> {selected, [_], [{SCount}]} ->
SCount; SCount;
_ -> _ ->
@ -556,8 +571,10 @@ webadmin_user_parse_query(Acc, _Action, _User, _Server, _Query) ->
%% Returns as integer the number of offline messages for a given user %% Returns as integer the number of offline messages for a given user
count_offline_messages(LUser, LServer) -> count_offline_messages(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
case catch odbc_queries:count_records_where( case catch odbc_queries:count_records_where(
LServer, "spool", "where username='" ++ Username ++ "'") of LServer, "spool", "where username='" ++ Username ++
"' and host='" ++ Host ++ "'") of
{selected, [_], [{Res}]} -> {selected, [_], [{Res}]} ->
list_to_integer(Res); list_to_integer(Res);
_ -> _ ->

View File

@ -285,13 +285,13 @@ process_default_set(LUser, LServer, false) ->
process_default_set(LUser, LServer, Name) -> process_default_set(LUser, LServer, Name) ->
F = fun() -> F = fun() ->
case sql_get_privacy_list_names_t(LUser) of case sql_get_privacy_list_names_t(LUser, LServer) of
{selected, ["name"], []} -> {selected, ["name"], []} ->
{error, 'item-not-found'}; {error, 'item-not-found'};
{selected, ["name"], Names} -> {selected, ["name"], Names} ->
case lists:member({Name}, Names) of case lists:member({Name}, Names) of
true -> true ->
sql_set_default_privacy_list(LUser, Name), sql_set_default_privacy_list(LUser, LServer, Name),
{result, []}; {result, []};
false -> false ->
{error, 'item-not-found'} {error, 'item-not-found'}
@ -337,15 +337,17 @@ process_list_set(_LUser, _LServer, false, _Els) ->
{error, 'bad-request'}; {error, 'bad-request'};
process_list_set(LUser, LServer, Name, Els) -> process_list_set(LUser, LServer, Name, Els) ->
?DEBUG("~p@~p: name ~p, els: ~p", [LUser, LServer, Name, Els]),
case parse_items(Els) of case parse_items(Els) of
false -> false ->
{error, 'bad-request'}; {error, 'bad-request'};
remove -> remove ->
F = F =
fun() -> fun() ->
case sql_get_default_privacy_list_t(LUser) of case sql_get_default_privacy_list_t(LUser, LServer) of
{selected, ["name"], []} -> {selected, ["name"], []} ->
sql_remove_privacy_list(LUser, Name), ?DEBUG("Going to delete privacy list ~p", [Name]),
sql_remove_privacy_list(LUser, LServer, Name),
{result, []}; {result, []};
{selected, ["name"], [{Default}]} -> {selected, ["name"], [{Default}]} ->
% TODO: check active % TODO: check active
@ -353,7 +355,8 @@ process_list_set(LUser, LServer, Name, Els) ->
Name == Default -> Name == Default ->
{error, 'conflict'}; {error, 'conflict'};
true -> true ->
sql_remove_privacy_list(LUser, Name), ?DEBUG("Going to delete default list named ~p", [Name]),
sql_remove_privacy_list(LUser, LServer, Name),
{result, []} {result, []}
end end
end end
@ -375,14 +378,19 @@ process_list_set(LUser, LServer, Name, Els) ->
end; end;
List -> List ->
RItems = lists:map(fun item_to_raw/1, List), RItems = lists:map(fun item_to_raw/1, List),
?DEBUG("Got a list of items: ~p", [RItems]),
F = F =
fun() -> fun() ->
?DEBUG("About to grab the id...", []),
ID = ID =
case sql_get_privacy_list_id_t(LUser, Name) of case sql_get_privacy_list_id_t(LUser, LServer, Name) of
{selected, ["id"], []} -> {selected, ["id"], []} ->
sql_add_privacy_list(LUser, Name), ?DEBUG("No privacy list called ~p",[Name]),
sql_add_privacy_list(LUser, LServer, Name),
?DEBUG("Added the privacy list called ~p",[Name]),
{selected, ["id"], [{I}]} = {selected, ["id"], [{I}]} =
sql_get_privacy_list_id_t(LUser, Name), sql_get_privacy_list_id_t(LUser, LServer, Name),
?DEBUG("Privacy list called ~p added, now id ~p",[Name, I]),
I; I;
{selected, ["id"], [{I}]} -> {selected, ["id"], [{I}]} ->
I I
@ -778,54 +786,60 @@ sql_get_default_privacy_list(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
odbc_queries:get_default_privacy_list(LServer, Username). odbc_queries:get_default_privacy_list(LServer, Username).
sql_get_default_privacy_list_t(LUser) -> sql_get_default_privacy_list_t(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
odbc_queries:get_default_privacy_list_t(Username). odbc_queries:get_default_privacy_list_t(LServer, Username).
sql_get_privacy_list_names(LUser, LServer) -> sql_get_privacy_list_names(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
odbc_queries:get_privacy_list_names(LServer, Username). odbc_queries:get_privacy_list_names(LServer, Username).
sql_get_privacy_list_names_t(LUser) -> sql_get_privacy_list_names_t(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
odbc_queries:get_privacy_list_names_t(Username). odbc_queries:get_privacy_list_names_t(LServer, Username).
sql_get_privacy_list_id(LUser, LServer, Name) -> sql_get_privacy_list_id(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:get_privacy_list_id(LServer, Username, SName). odbc_queries:get_privacy_list_id(Host, Username, SName).
sql_get_privacy_list_id_t(LUser, Name) -> sql_get_privacy_list_id_t(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:get_privacy_list_id_t(Username, SName). odbc_queries:get_privacy_list_id_t(Host, Username, SName).
sql_get_privacy_list_data(LUser, LServer, Name) -> sql_get_privacy_list_data(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:get_privacy_list_data(LServer, Username, SName). odbc_queries:get_privacy_list_data(Host, Username, SName).
sql_get_privacy_list_data_by_id(ID, LServer) -> sql_get_privacy_list_data_by_id(ID, LServer) ->
odbc_queries:get_privacy_list_data_by_id(LServer, ID). odbc_queries:get_privacy_list_data_by_id(LServer, ID).
sql_set_default_privacy_list(LUser, Name) -> sql_set_default_privacy_list(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:set_default_privacy_list(Username, SName). odbc_queries:set_default_privacy_list(Host, Username, SName).
sql_unset_default_privacy_list(LUser, LServer) -> sql_unset_default_privacy_list(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
odbc_queries:unset_default_privacy_list(LServer, Username). odbc_queries:unset_default_privacy_list(LServer, Username).
sql_remove_privacy_list(LUser, Name) -> sql_remove_privacy_list(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:remove_privacy_list(Username, SName). odbc_queries:remove_privacy_list(Host, Username, SName).
sql_add_privacy_list(LUser, Name) -> sql_add_privacy_list(LUser, LServer, Name) ->
Username = ejabberd_odbc:escape(LUser), Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
SName = ejabberd_odbc:escape(Name), SName = ejabberd_odbc:escape(Name),
odbc_queries:add_privacy_list(Username, SName). odbc_queries:add_privacy_list(Host, Username, SName).
sql_set_privacy_list(ID, RItems) -> sql_set_privacy_list(ID, RItems) ->
odbc_queries:set_privacy_list(ID, RItems). odbc_queries:set_privacy_list(ID, RItems).

View File

@ -108,7 +108,7 @@ check_packet(From, To, IQ_Rec, [F | R]) ->
check_domain(From, _To, _IQ_Rec) -> check_domain(From, _To, _IQ_Rec) ->
LServer = exmpp_jid:prep_domain_as_list(From), LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of case ?IS_MY_HOST(LServer) of
true -> ok; true -> ok;
false -> {error, 'not-allowed'} false -> {error, 'not-allowed'}
end. end.

View File

@ -176,7 +176,7 @@ stop(Host) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([ServerHost, Opts]) -> init([ServerHost, Opts]) ->
?DEBUG("pubsub init ~p ~p",[ServerHost,Opts]), ?DEBUG("pubsub init ~p ~p",[ServerHost,Opts]),
Host = gen_mod:get_opt_host(ServerHost, Opts, "pubsub.@HOST@"), Host = gen_mod:expand_host_name(ServerHost, Opts, "pubsub"),
Access = gen_mod:get_opt(access_createnode, Opts, all), Access = gen_mod:get_opt(access_createnode, Opts, all),
PepOffline = gen_mod:get_opt(ignore_pep_from_offline, Opts, true), PepOffline = gen_mod:get_opt(ignore_pep_from_offline, Opts, true),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue), IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
@ -3105,7 +3105,7 @@ broadcast_stanza({LUser, LServer, LResource}, Publisher, Node, NodeId, Type, Nod
Contacts when is_list(Contacts) -> Contacts when is_list(Contacts) ->
lists:foreach(fun({U, S, _}) -> lists:foreach(fun({U, S, _}) ->
spawn(fun() -> spawn(fun() ->
case lists:member(S, ?MYHOSTS) of case ?IS_MY_HOST(S) of
true -> true ->
lists:foreach(fun(R) -> lists:foreach(fun(R) ->
ejabberd_router:route(Sender, exmpp_jid:make(U, S, R), StanzaToSend) ejabberd_router:route(Sender, exmpp_jid:make(U, S, R), StanzaToSend)

View File

@ -118,7 +118,7 @@ stop(Host) ->
process_iq(From, To, IQ_Rec) -> process_iq(From, To, IQ_Rec) ->
LServer = exmpp_jid:prep_domain_as_list(From), LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of case ?IS_MY_HOST(LServer) of
true -> true ->
process_local_iq(From, To, IQ_Rec); process_local_iq(From, To, IQ_Rec);
_ -> _ ->
@ -838,11 +838,11 @@ process_item_set_t(LUser, LServer, #xmlel{} = El) ->
Item2 = process_item_els(Item1, El#xmlel.children), Item2 = process_item_els(Item1, El#xmlel.children),
case Item2#roster.subscription of case Item2#roster.subscription of
remove -> remove ->
odbc_queries:del_roster_sql(Username, SJID); odbc_queries:del_roster_sql(LServer, Username, SJID);
_ -> _ ->
ItemVals = record_to_string(Item1), ItemVals = record_to_string(Item1),
ItemGroups = groups_to_string(Item2), ItemGroups = groups_to_string(Item2),
odbc_queries:update_roster_sql(Username, SJID, ItemVals, ItemGroups) odbc_queries:update_roster_sql(LServer, Username, SJID, ItemVals, ItemGroups)
end end
catch catch
_ -> _ ->
@ -1158,7 +1158,7 @@ build_contact_jid_td({U, S, R}) ->
{CUser, CServer} -> {CUser, CServer} ->
CUser_S = binary_to_list(CUser), CUser_S = binary_to_list(CUser),
CServer_S = binary_to_list(CServer), CServer_S = binary_to_list(CServer),
case lists:member(CServer_S, ?MYHOSTS) of case ?IS_MY_HOST(CServer_S) of
false -> ""; false -> "";
true -> "/admin/server/" ++ CServer_S ++ "/user/" ++ CUser_S ++ "/" true -> "/admin/server/" ++ CServer_S ++ "/user/" ++ CUser_S ++ "/"
end end

View File

@ -54,7 +54,7 @@ start(Host, Opts) ->
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_VCARD, gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_VCARD,
?MODULE, process_sm_iq, IQDisc), ?MODULE, process_sm_iq, IQDisc),
ejabberd_hooks:add(disco_sm_features, HostB, ?MODULE, get_sm_features, 50), ejabberd_hooks:add(disco_sm_features, HostB, ?MODULE, get_sm_features, 50),
MyHost = gen_mod:get_opt_host(Host, Opts, "vjud.@HOST@"), MyHost = gen_mod:expand_host_name(Host, Opts, "vjud"),
Search = gen_mod:get_opt(search, Opts, true), Search = gen_mod:get_opt(search, Opts, true),
register(gen_mod:get_module_proc(Host, ?PROCNAME), register(gen_mod:get_module_proc(Host, ?PROCNAME),
spawn(?MODULE, init, [MyHost, Host, Search])). spawn(?MODULE, init, [MyHost, Host, Search])).
@ -154,7 +154,7 @@ process_sm_iq(_From, To, #iq{type = get} = IQ_Rec) ->
process_sm_iq(From, _To, #iq{type = set, payload = Request} = IQ_Rec) -> process_sm_iq(From, _To, #iq{type = set, payload = Request} = IQ_Rec) ->
User = exmpp_jid:node_as_list(From), User = exmpp_jid:node_as_list(From),
LServer = exmpp_jid:prep_domain_as_list(From), LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of case ?IS_MY_HOST(LServer) of
true -> true ->
set_vcard(User, LServer, Request), set_vcard(User, LServer, Request),
exmpp_iq:result(IQ_Rec); exmpp_iq:result(IQ_Rec);
@ -285,6 +285,10 @@ set_vcard(User, LServer, VCARD) ->
?TLFIELD(<<"text-single">>, "Organization Unit", <<"orgunit">>) ?TLFIELD(<<"text-single">>, "Organization Unit", <<"orgunit">>)
]}]). ]}]).
do_route(global, From, To, Packet) ->
Host = exmpp_jid:prep_domain_as_list(To),
ServerHost = ejabberd_global_router:server_host(Host, self()),
do_route(ServerHost, From, To, Packet);
do_route(ServerHost, From, To, Packet) -> do_route(ServerHost, From, To, Packet) ->
User = exmpp_jid:node(To), User = exmpp_jid:node(To),
Resource = exmpp_jid:resource(To), Resource = exmpp_jid:resource(To),
@ -492,7 +496,8 @@ search(LServer, Data) ->
make_matchspec(LServer, Data) -> make_matchspec(LServer, Data) ->
filter_fields(Data, "", LServer). Host = ejabberd_odbc:escape(LServer),
filter_fields(Data, ["host = '", Host, "'"], LServer).
filter_fields([], Match, _LServer) -> filter_fields([], Match, _LServer) ->
case Match of case Match of