mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-24 16:23:40 +01:00
* src/mod_offline_odbc.erl: Implements quota for offline messages
in relational database (EJAB-314) * src/odbc/odbc_queries.erl: Likewise SVN Revision: 893
This commit is contained in:
parent
a78037fc3c
commit
b0bb9a79c3
@ -1,17 +1,19 @@
|
|||||||
%%%----------------------------------------------------------------------
|
%%%----------------------------------------------------------------------
|
||||||
%%% File : mod_offline_odbc.erl
|
%%% File : mod_offline_odbc.erl
|
||||||
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
||||||
%%% Purpose :
|
%%% Purpose : Store and manage offline messages in relational database.
|
||||||
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
||||||
%%%----------------------------------------------------------------------
|
%%%----------------------------------------------------------------------
|
||||||
|
|
||||||
-module(mod_offline_odbc).
|
-module(mod_offline_odbc).
|
||||||
-author('alexey@sevcom.net').
|
-author('alexey@process-one.net').
|
||||||
|
|
||||||
-behaviour(gen_mod).
|
-behaviour(gen_mod).
|
||||||
|
|
||||||
|
-export([count_offline_messages/2]).
|
||||||
|
|
||||||
-export([start/2,
|
-export([start/2,
|
||||||
init/1,
|
init/2,
|
||||||
stop/1,
|
stop/1,
|
||||||
store_packet/3,
|
store_packet/3,
|
||||||
pop_offline_messages/3,
|
pop_offline_messages/3,
|
||||||
@ -29,7 +31,7 @@
|
|||||||
-define(PROCNAME, ejabberd_offline).
|
-define(PROCNAME, ejabberd_offline).
|
||||||
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
|
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
|
||||||
|
|
||||||
start(Host, _Opts) ->
|
start(Host, Opts) ->
|
||||||
ejabberd_hooks:add(offline_message_hook, Host,
|
ejabberd_hooks:add(offline_message_hook, Host,
|
||||||
?MODULE, store_packet, 50),
|
?MODULE, store_packet, 50),
|
||||||
ejabberd_hooks:add(resend_offline_messages_hook, Host,
|
ejabberd_hooks:add(resend_offline_messages_hook, Host,
|
||||||
@ -42,56 +44,72 @@ start(Host, _Opts) ->
|
|||||||
?MODULE, webadmin_page, 50),
|
?MODULE, webadmin_page, 50),
|
||||||
ejabberd_hooks:add(webadmin_user, Host,
|
ejabberd_hooks:add(webadmin_user, Host,
|
||||||
?MODULE, webadmin_user, 50),
|
?MODULE, webadmin_user, 50),
|
||||||
|
MaxOfflineMsgs = gen_mod:get_opt(user_max_messages, Opts, infinity),
|
||||||
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
||||||
spawn(?MODULE, init, [Host])).
|
spawn(?MODULE, init, [Host, MaxOfflineMsgs])).
|
||||||
|
|
||||||
init(Host) ->
|
%% MaxOfflineMsgs is either infinity of integer > 0
|
||||||
loop(Host).
|
init(Host, infinity) ->
|
||||||
|
loop(Host, infinity);
|
||||||
|
init(Host, MaxOfflineMsgs)
|
||||||
|
when integer(MaxOfflineMsgs), MaxOfflineMsgs > 0 ->
|
||||||
|
loop(Host, MaxOfflineMsgs).
|
||||||
|
|
||||||
loop(Host) ->
|
loop(Host, MaxOfflineMsgs) ->
|
||||||
receive
|
receive
|
||||||
#offline_msg{} = Msg ->
|
#offline_msg{user=Username} = Msg ->
|
||||||
Msgs = receive_all([Msg]),
|
Msgs = receive_all(Username, [Msg]),
|
||||||
% TODO
|
Len = length(Msgs),
|
||||||
Query = lists:map(
|
|
||||||
fun(M) ->
|
%% Only count existing messages if needed:
|
||||||
Username =
|
Count = if MaxOfflineMsgs =/= infinity ->
|
||||||
ejabberd_odbc:escape(
|
Len + count_offline_messages(Username, Host);
|
||||||
(M#offline_msg.to)#jid.luser),
|
true -> 0
|
||||||
From = M#offline_msg.from,
|
end,
|
||||||
To = M#offline_msg.to,
|
if
|
||||||
{xmlelement, Name, Attrs, Els} =
|
Count > MaxOfflineMsgs ->
|
||||||
M#offline_msg.packet,
|
discard_warn_sender(Msgs);
|
||||||
Attrs2 = jlib:replace_from_to_attrs(
|
true ->
|
||||||
jlib:jid_to_string(From),
|
Query = lists:map(
|
||||||
jlib:jid_to_string(To),
|
fun(M) ->
|
||||||
Attrs),
|
Username =
|
||||||
Packet = {xmlelement, Name, Attrs2,
|
ejabberd_odbc:escape(
|
||||||
Els ++
|
(M#offline_msg.to)#jid.luser),
|
||||||
[jlib:timestamp_to_xml(
|
From = M#offline_msg.from,
|
||||||
calendar:now_to_universal_time(
|
To = M#offline_msg.to,
|
||||||
M#offline_msg.timestamp))]},
|
{xmlelement, Name, Attrs, Els} =
|
||||||
XML =
|
M#offline_msg.packet,
|
||||||
ejabberd_odbc:escape(
|
Attrs2 = jlib:replace_from_to_attrs(
|
||||||
lists:flatten(
|
jlib:jid_to_string(From),
|
||||||
xml:element_to_string(Packet))),
|
jlib:jid_to_string(To),
|
||||||
odbc_queries:add_spool_sql(Username, XML)
|
Attrs),
|
||||||
end, Msgs),
|
Packet = {xmlelement, Name, Attrs2,
|
||||||
case catch odbc_queries:add_spool(Host, Query) of
|
Els ++
|
||||||
{'EXIT', Reason} ->
|
[jlib:timestamp_to_xml(
|
||||||
?ERROR_MSG("~p~n", [Reason]);
|
calendar:now_to_universal_time(
|
||||||
_ ->
|
M#offline_msg.timestamp))]},
|
||||||
ok
|
XML =
|
||||||
|
ejabberd_odbc:escape(
|
||||||
|
lists:flatten(
|
||||||
|
xml:element_to_string(Packet))),
|
||||||
|
odbc_queries:add_spool_sql(Username, XML)
|
||||||
|
end, Msgs),
|
||||||
|
case catch odbc_queries:add_spool(Host, Query) of
|
||||||
|
{'EXIT', Reason} ->
|
||||||
|
?ERROR_MSG("~p~n", [Reason]);
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
loop(Host);
|
loop(Host, MaxOfflineMsgs);
|
||||||
_ ->
|
_ ->
|
||||||
loop(Host)
|
loop(Host, MaxOfflineMsgs)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
receive_all(Msgs) ->
|
receive_all(Username, Msgs) ->
|
||||||
receive
|
receive
|
||||||
#offline_msg{} = Msg ->
|
#offline_msg{user=Username} = Msg ->
|
||||||
receive_all([Msg | Msgs])
|
receive_all(Username, [Msg | Msgs])
|
||||||
after 0 ->
|
after 0 ->
|
||||||
lists:reverse(Msgs)
|
lists:reverse(Msgs)
|
||||||
end.
|
end.
|
||||||
@ -247,6 +265,25 @@ remove_user(User, Server) ->
|
|||||||
odbc_queries:del_spool_msg(LServer, Username).
|
odbc_queries:del_spool_msg(LServer, Username).
|
||||||
|
|
||||||
|
|
||||||
|
%% Helper functions:
|
||||||
|
|
||||||
|
%% TODO: Warning - This function is a duplicate from mod_offline.erl
|
||||||
|
%% It is duplicate to stay consistent (many functions are duplicated
|
||||||
|
%% in this module). It will be refactored later on.
|
||||||
|
%% Warn senders that their messages have been discarded:
|
||||||
|
discard_warn_sender(Msgs) ->
|
||||||
|
lists:foreach(
|
||||||
|
fun(#offline_msg{from=From, to=To, packet=Packet}) ->
|
||||||
|
ErrText = "Your contact offline message queue is full. The message has been discarded.",
|
||||||
|
Lang = xml:get_tag_attr_s("xml:lang", Packet),
|
||||||
|
Err = jlib:make_error_reply(
|
||||||
|
Packet, ?ERRT_RESOURCE_CONSTRAINT(Lang, ErrText)),
|
||||||
|
ejabberd_router:route(
|
||||||
|
To,
|
||||||
|
From, Err)
|
||||||
|
end, Msgs).
|
||||||
|
|
||||||
|
|
||||||
webadmin_page(_, Host,
|
webadmin_page(_, Host,
|
||||||
#request{us = _US,
|
#request{us = _US,
|
||||||
path = ["user", U, "queue"],
|
path = ["user", U, "queue"],
|
||||||
@ -382,3 +419,16 @@ webadmin_user(Acc, User, Server, Lang) ->
|
|||||||
end,
|
end,
|
||||||
FQueueLen = [?AC("queue/", QueueLen)],
|
FQueueLen = [?AC("queue/", QueueLen)],
|
||||||
Acc ++ [?XCT("h3", "Offline Messages:")] ++ FQueueLen.
|
Acc ++ [?XCT("h3", "Offline Messages:")] ++ FQueueLen.
|
||||||
|
|
||||||
|
%% ------------------------------------------------
|
||||||
|
%% mod_offline: number of messages quota management
|
||||||
|
|
||||||
|
%% Returns as integer the number of offline messages for a given user
|
||||||
|
count_offline_messages(LUser, LServer) ->
|
||||||
|
case catch odbc_queries:count_records_where(
|
||||||
|
LServer, "spool", "where username='" ++ LUser ++ "'") of
|
||||||
|
{selected, [_], [{Res}]} ->
|
||||||
|
list_to_integer(Res);
|
||||||
|
_ ->
|
||||||
|
0
|
||||||
|
end.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
%% Copyrigh 2006, Process-one
|
%% Copyrigh 2006 Process-one
|
||||||
%% This module is intended to take into account relational databases behaviour
|
%% This module is intended to take into account relational databases behaviour
|
||||||
%% differences
|
%% differences
|
||||||
-module(odbc_queries).
|
-module(odbc_queries).
|
||||||
@ -36,7 +36,8 @@
|
|||||||
set_private_data_sql/3,
|
set_private_data_sql/3,
|
||||||
get_private_data/3,
|
get_private_data/3,
|
||||||
del_user_private_storage/2,
|
del_user_private_storage/2,
|
||||||
escape/1]).
|
escape/1,
|
||||||
|
count_records_where/3]).
|
||||||
|
|
||||||
%-define(generic, true).
|
%-define(generic, true).
|
||||||
%-define(mssql, true).
|
%-define(mssql, true).
|
||||||
@ -309,6 +310,11 @@ escape($") -> "\\\"";
|
|||||||
escape($\\) -> "\\\\";
|
escape($\\) -> "\\\\";
|
||||||
escape(C) -> C.
|
escape(C) -> C.
|
||||||
|
|
||||||
|
%% Count number of records in a table given a where clause
|
||||||
|
count_records_where(LServer, Table, WhereClause) ->
|
||||||
|
ejabberd_odbc:sql_query(
|
||||||
|
LServer,
|
||||||
|
["select count(*) from ", Table, " ", WhereClause, ";"]).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
%% -----------------
|
%% -----------------
|
||||||
@ -513,4 +519,10 @@ escape($\r) -> "\\r";
|
|||||||
escape($') -> "\''";
|
escape($') -> "\''";
|
||||||
escape($") -> "\\\"";
|
escape($") -> "\\\"";
|
||||||
escape(C) -> C.
|
escape(C) -> C.
|
||||||
|
|
||||||
|
%% Count number of records in a table given a where clause
|
||||||
|
count_records_where(LServer, Table, WhereClause) ->
|
||||||
|
ejabberd_odbc:sql_query(
|
||||||
|
LServer,
|
||||||
|
["select count(*) from ", Table, " ", WhereClause, " with (nolock)"]).
|
||||||
-endif.
|
-endif.
|
||||||
|
Loading…
Reference in New Issue
Block a user