aboutsummaryrefslogtreecommitdiff
path: root/src/mod_offline_mnesia.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mod_offline_mnesia.erl')
-rw-r--r--src/mod_offline_mnesia.erl138
1 files changed, 69 insertions, 69 deletions
diff --git a/src/mod_offline_mnesia.erl b/src/mod_offline_mnesia.erl
index 6a1d9e309..5d0bf2921 100644
--- a/src/mod_offline_mnesia.erl
+++ b/src/mod_offline_mnesia.erl
@@ -1,51 +1,52 @@
%%%-------------------------------------------------------------------
-%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
-%%% @copyright (C) 2016, Evgeny Khramtsov
-%%% @doc
-%%%
-%%% @end
+%%% File : mod_offline_mnesia.erl
+%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 15 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
-%%%-------------------------------------------------------------------
+%%%
+%%%
+%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
+%%%
+%%% 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.
+%%%
+%%% 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.,
+%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+%%%
+%%%----------------------------------------------------------------------
+
-module(mod_offline_mnesia).
-behaviour(mod_offline).
--export([init/2, store_messages/5, pop_messages/2, remove_expired_messages/1,
+-export([init/2, store_message/1, pop_messages/2, remove_expired_messages/1,
remove_old_messages/2, remove_user/2, read_message_headers/2,
read_message/3, remove_message/3, read_all_messages/2,
- remove_all_messages/2, count_messages/2, import/2]).
+ remove_all_messages/2, count_messages/2, import/1]).
+-export([need_transform/1, transform/1]).
--include("jlib.hrl").
+-include("xmpp.hrl").
-include("mod_offline.hrl").
-include("logger.hrl").
--define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
-
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
- mnesia:create_table(offline_msg,
- [{disc_only_copies, [node()]}, {type, bag},
- {attributes, record_info(fields, offline_msg)}]),
- update_table().
+ ejabberd_mnesia:create(?MODULE, offline_msg,
+ [{disc_only_copies, [node()]}, {type, bag},
+ {attributes, record_info(fields, offline_msg)}]).
-store_messages(_Host, US, Msgs, Len, MaxOfflineMsgs) ->
- F = fun () ->
- Count = if MaxOfflineMsgs =/= infinity ->
- Len + count_mnesia_records(US);
- true -> 0
- end,
- if Count > MaxOfflineMsgs -> discard;
- true ->
- if Len >= (?OFFLINE_TABLE_LOCK_THRESHOLD) ->
- mnesia:write_lock_table(offline_msg);
- true -> ok
- end,
- lists:foreach(fun (M) -> mnesia:write(M) end, Msgs)
- end
- end,
- mnesia:transaction(F).
+store_message(#offline_msg{packet = Pkt} = OffMsg) ->
+ El = xmpp:encode(Pkt),
+ mnesia:dirty_write(OffMsg#offline_msg{packet = El}).
pop_messages(LUser, LServer) ->
US = {LUser, LServer},
@@ -62,7 +63,7 @@ pop_messages(LUser, LServer) ->
end.
remove_expired_messages(_LServer) ->
- TimeStamp = p1_time_compat:timestamp(),
+ TimeStamp = erlang:timestamp(),
F = fun () ->
mnesia:write_lock_table(offline_msg),
mnesia:foldl(fun (Rec, _Acc) ->
@@ -80,7 +81,7 @@ remove_expired_messages(_LServer) ->
mnesia:transaction(F).
remove_old_messages(Days, _LServer) ->
- S = p1_time_compat:system_time(seconds) - 60 * 60 * 24 * Days,
+ S = erlang:system_time(second) - 60 * 60 * 24 * Days,
MegaSecs1 = S div 1000000,
Secs1 = S rem 1000000,
TimeStamp = {MegaSecs1, Secs1, 0},
@@ -107,9 +108,7 @@ read_message_headers(LUser, LServer) ->
fun(#offline_msg{from = From, to = To, packet = Pkt,
timestamp = TS}) ->
Seq = now_to_integer(TS),
- NewPkt = jlib:add_delay_info(Pkt, LServer, TS,
- <<"Offline Storage">>),
- {Seq, From, To, NewPkt}
+ {Seq, From, To, TS, Pkt}
end, Msgs),
lists:keysort(1, Hdrs).
@@ -127,12 +126,16 @@ read_message(LUser, LServer, I) ->
remove_message(LUser, LServer, I) ->
US = {LUser, LServer},
TS = integer_to_now(I),
- Msgs = mnesia:dirty_match_object(
- offline_msg, #offline_msg{us = US, timestamp = TS, _ = '_'}),
- lists:foreach(
- fun(Msg) ->
- mnesia:dirty_delete_object(Msg)
- end, Msgs).
+ case mnesia:dirty_match_object(
+ offline_msg, #offline_msg{us = US, timestamp = TS, _ = '_'}) of
+ [] ->
+ {error, notfound};
+ Msgs ->
+ lists:foreach(
+ fun(Msg) ->
+ mnesia:dirty_delete_object(Msg)
+ end, Msgs)
+ end.
read_all_messages(LUser, LServer) ->
US = {LUser, LServer},
@@ -153,14 +156,33 @@ count_messages(LUser, LServer) ->
F = fun () ->
count_mnesia_records(US)
end,
- case catch mnesia:async_dirty(F) of
- I when is_integer(I) -> I;
- _ -> 0
- end.
+ {cache, case mnesia:async_dirty(F) of
+ I when is_integer(I) -> I;
+ _ -> 0
+ end}.
-import(_LServer, #offline_msg{} = Msg) ->
+import(#offline_msg{} = Msg) ->
mnesia:dirty_write(Msg).
+need_transform({offline_msg, {U, S}, _, _, _, _, _})
+ when is_list(U) orelse is_list(S) ->
+ ?INFO_MSG("Mnesia table 'offline_msg' will be converted to binary", []),
+ true;
+need_transform({offline_msg, _, _, _, _, _, _, _}) ->
+ true;
+need_transform(_) ->
+ false.
+
+transform({offline_msg, {U, S}, Timestamp, Expire, From, To, _, Packet}) ->
+ #offline_msg{us = {U, S}, timestamp = Timestamp, expire = Expire,
+ from = From ,to = To, packet = Packet};
+transform(#offline_msg{us = {U, S}, from = From, to = To,
+ packet = El} = R) ->
+ R#offline_msg{us = {iolist_to_binary(U), iolist_to_binary(S)},
+ from = jid_to_binary(From),
+ to = jid_to_binary(To),
+ packet = fxml:to_xmlel(El)}.
+
%%%===================================================================
%%% Internal functions
%%%===================================================================
@@ -208,25 +230,3 @@ integer_to_now(Int) ->
MSec = Secs div 1000000,
Sec = Secs rem 1000000,
{MSec, Sec, USec}.
-
-update_table() ->
- Fields = record_info(fields, offline_msg),
- case mnesia:table_info(offline_msg, attributes) of
- Fields ->
- ejabberd_config:convert_table_to_binary(
- offline_msg, Fields, bag,
- fun(#offline_msg{us = {U, _}}) -> U end,
- fun(#offline_msg{us = {U, S},
- from = From,
- to = To,
- packet = El} = R) ->
- R#offline_msg{us = {iolist_to_binary(U),
- iolist_to_binary(S)},
- from = jid_to_binary(From),
- to = jid_to_binary(To),
- packet = fxml:to_xmlel(El)}
- end);
- _ ->
- ?INFO_MSG("Recreating offline_msg table", []),
- mnesia:transform_table(offline_msg, ignore, Fields)
- end.