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_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(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
@ -88,15 +88,15 @@ start(Host, Opts) ->
loop(Host, AccessMaxOfflineMsgs) ->
receive
#offline_msg{user = User} = Msg ->
Msgs = receive_all(User, [Msg]),
#offline_msg{user = User, server = Server} = Msg ->
Msgs = receive_all(User, Server, [Msg]),
Len = length(Msgs),
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
User, Host),
%% Only count existing messages if needed:
Count = if MaxOfflineMsgs =/= infinity ->
Len + count_offline_messages(User, Host);
Len + count_offline_messages(User, Server);
true -> 0
end,
if
@ -129,9 +129,9 @@ loop(Host, AccessMaxOfflineMsgs) ->
XML =
ejabberd_odbc:escape(
exmpp_xml:document_to_list(Packet1)),
odbc_queries:add_spool_sql(Username, XML)
odbc_queries:add_spool_sql(Server, Username, XML)
end, Msgs),
case catch odbc_queries:add_spool(Host, Query) of
case catch odbc_queries:add_spool(Server, Query) of
{'EXIT', Reason} ->
?ERROR_MSG("~p~n", [Reason]);
{error, Reason} ->
@ -154,10 +154,10 @@ get_max_user_messages(AccessRule, LUser, Host) ->
_ -> ?MAX_USER_MESSAGES
end.
receive_all(Username, Msgs) ->
receive_all(Username, Server, Msgs) ->
receive
#offline_msg{user=Username} = Msg ->
receive_all(Username, [Msg | Msgs])
#offline_msg{user = Username, server = Server} = Msg ->
receive_all(Username, Server, [Msg | Msgs])
after 0 ->
lists:reverse(Msgs)
end.
@ -208,10 +208,19 @@ store_packet(From, To, Packet) ->
case check_event_chatstates(From, To, Packet) of
true ->
LUser = exmpp_jid:prep_node_as_list(To),
LServer = exmpp_jid:prep_domain_as_list(To),
TimeStamp = now(),
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,
server = LServer,
timestamp = TimeStamp,
expire = Expire,
from = From,
@ -377,12 +386,14 @@ user_queue(User, Server, Query, Lang) ->
LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
US0 = {LUser, LServer},
R = user_queue_parse_query(Username, LServer, Query),
M = case catch ejabberd_odbc:sql_query(
LServer,
["select username, xml from spool"
" where username='", Username, "'"
" and host='", Host, "'"
" order by seq;"]) of
{selected, ["username", "xml"], Rs} ->
lists:flatmap(
@ -449,10 +460,12 @@ user_queue(User, Server, Query, Lang) ->
user_queue_parse_query(Username, LServer, Query) ->
case lists:keysearch("delete", 1, Query) of
{value, _} ->
Host = ejabberd_odbc:escape(LServer),
Msgs = case catch ejabberd_odbc:sql_query(
LServer,
["select xml, seq from spool"
" where username='", Username, "'"
" where username='", Username, "' and"
" host='", Host, "'"
" order by seq;"]) of
{selected, ["xml", "seq"], Rs} ->
lists:flatmap(
@ -481,7 +494,7 @@ user_queue_parse_query(Username, LServer, Query) ->
catch ejabberd_odbc:sql_query(
LServer,
["delete from spool"
" where username='", Username, "'"
" where username='", Username, "' and host='", Host, "'"
" and seq='", SSeq, "';"]);
false ->
ok
@ -498,10 +511,12 @@ us_to_list({User, Server}) ->
exmpp_jid:to_list(User, Server).
get_queue_length(Username, LServer) ->
Host = ejabberd_odbc:escape(LServer),
case catch ejabberd_odbc:sql_query(
LServer,
["select count(*) from spool"
" where username='", Username, "';"]) of
" where username='", Username, "'"
" and host='", Host, "';"]) of
{selected, [_], [{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
count_offline_messages(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
case catch odbc_queries:count_records_where(
LServer, "spool", "where username='" ++ Username ++ "'") of
LServer, "spool", "where username='" ++ Username ++
"' and host='" ++ Host ++ "'") of
{selected, [_], [{Res}]} ->
list_to_integer(Res);
_ ->

View File

@ -285,13 +285,13 @@ process_default_set(LUser, LServer, false) ->
process_default_set(LUser, LServer, Name) ->
F = fun() ->
case sql_get_privacy_list_names_t(LUser) of
case sql_get_privacy_list_names_t(LUser, LServer) of
{selected, ["name"], []} ->
{error, 'item-not-found'};
{selected, ["name"], Names} ->
case lists:member({Name}, Names) of
true ->
sql_set_default_privacy_list(LUser, Name),
sql_set_default_privacy_list(LUser, LServer, Name),
{result, []};
false ->
{error, 'item-not-found'}
@ -337,15 +337,17 @@ process_list_set(_LUser, _LServer, false, _Els) ->
{error, 'bad-request'};
process_list_set(LUser, LServer, Name, Els) ->
?DEBUG("~p@~p: name ~p, els: ~p", [LUser, LServer, Name, Els]),
case parse_items(Els) of
false ->
{error, 'bad-request'};
remove ->
F =
fun() ->
case sql_get_default_privacy_list_t(LUser) of
case sql_get_default_privacy_list_t(LUser, LServer) of
{selected, ["name"], []} ->
sql_remove_privacy_list(LUser, Name),
?DEBUG("Going to delete privacy list ~p", [Name]),
sql_remove_privacy_list(LUser, LServer, Name),
{result, []};
{selected, ["name"], [{Default}]} ->
% TODO: check active
@ -353,7 +355,8 @@ process_list_set(LUser, LServer, Name, Els) ->
Name == Default ->
{error, 'conflict'};
true ->
sql_remove_privacy_list(LUser, Name),
?DEBUG("Going to delete default list named ~p", [Name]),
sql_remove_privacy_list(LUser, LServer, Name),
{result, []}
end
end
@ -375,14 +378,19 @@ process_list_set(LUser, LServer, Name, Els) ->
end;
List ->
RItems = lists:map(fun item_to_raw/1, List),
?DEBUG("Got a list of items: ~p", [RItems]),
F =
fun() ->
?DEBUG("About to grab the 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"], []} ->
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}]} =
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;
{selected, ["id"], [{I}]} ->
I
@ -778,54 +786,60 @@ sql_get_default_privacy_list(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser),
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),
odbc_queries:get_default_privacy_list_t(Username).
odbc_queries:get_default_privacy_list_t(LServer, Username).
sql_get_privacy_list_names(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser),
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),
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) ->
Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
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),
Host = ejabberd_odbc:escape(LServer),
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) ->
Username = ejabberd_odbc:escape(LUser),
Host = ejabberd_odbc:escape(LServer),
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) ->
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),
Host = ejabberd_odbc:escape(LServer),
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) ->
Username = ejabberd_odbc:escape(LUser),
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),
Host = ejabberd_odbc:escape(LServer),
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),
Host = ejabberd_odbc:escape(LServer),
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) ->
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) ->
LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of
case ?IS_MY_HOST(LServer) of
true -> ok;
false -> {error, 'not-allowed'}
end.

View File

@ -176,7 +176,7 @@ stop(Host) ->
%%--------------------------------------------------------------------
init([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),
PepOffline = gen_mod:get_opt(ignore_pep_from_offline, Opts, true),
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) ->
lists:foreach(fun({U, S, _}) ->
spawn(fun() ->
case lists:member(S, ?MYHOSTS) of
case ?IS_MY_HOST(S) of
true ->
lists:foreach(fun(R) ->
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) ->
LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of
case ?IS_MY_HOST(LServer) of
true ->
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),
case Item2#roster.subscription of
remove ->
odbc_queries:del_roster_sql(Username, SJID);
odbc_queries:del_roster_sql(LServer, Username, SJID);
_ ->
ItemVals = record_to_string(Item1),
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
catch
_ ->
@ -1158,7 +1158,7 @@ build_contact_jid_td({U, S, R}) ->
{CUser, CServer} ->
CUser_S = binary_to_list(CUser),
CServer_S = binary_to_list(CServer),
case lists:member(CServer_S, ?MYHOSTS) of
case ?IS_MY_HOST(CServer_S) of
false -> "";
true -> "/admin/server/" ++ CServer_S ++ "/user/" ++ CUser_S ++ "/"
end

View File

@ -54,7 +54,7 @@ start(Host, Opts) ->
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_VCARD,
?MODULE, process_sm_iq, IQDisc),
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),
register(gen_mod:get_module_proc(Host, ?PROCNAME),
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) ->
User = exmpp_jid:node_as_list(From),
LServer = exmpp_jid:prep_domain_as_list(From),
case lists:member(LServer, ?MYHOSTS) of
case ?IS_MY_HOST(LServer) of
true ->
set_vcard(User, LServer, Request),
exmpp_iq:result(IQ_Rec);
@ -285,6 +285,10 @@ set_vcard(User, LServer, VCARD) ->
?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) ->
User = exmpp_jid:node(To),
Resource = exmpp_jid:resource(To),
@ -492,7 +496,8 @@ search(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) ->
case Match of