2005-08-29 21:00:10 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejd2odbc.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2005-09-10 20:03:48 +02:00
|
|
|
%%% Purpose : Export some mnesia tables to SQL DB
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 22 Aug 2005 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 12:41:41 +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 12:41:41 +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
|
|
|
|
%%%
|
2005-08-29 21:00:10 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejd2odbc).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2005-08-29 21:00:10 +02:00
|
|
|
|
|
|
|
%% External exports
|
2005-09-10 19:01:30 +02:00
|
|
|
-export([export_passwd/2,
|
|
|
|
export_roster/2,
|
|
|
|
export_offline/2,
|
2005-10-07 01:57:34 +02:00
|
|
|
export_last/2,
|
|
|
|
export_vcard/2,
|
2006-11-20 14:20:47 +01:00
|
|
|
export_vcard_search/2,
|
|
|
|
export_private_storage/2]).
|
2005-08-29 21:00:10 +02:00
|
|
|
|
2008-10-13 11:39:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2005-08-29 21:00:10 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("mod_roster.hrl").
|
|
|
|
|
|
|
|
-record(offline_msg, {us, timestamp, expire, from, to, packet}).
|
|
|
|
-record(last_activity, {us, timestamp, status}).
|
2005-10-07 01:57:34 +02:00
|
|
|
-record(vcard, {us, vcard}).
|
|
|
|
-record(vcard_search, {us,
|
|
|
|
user, luser,
|
|
|
|
fn, lfn,
|
|
|
|
family, lfamily,
|
|
|
|
given, lgiven,
|
|
|
|
middle, lmiddle,
|
|
|
|
nickname, lnickname,
|
|
|
|
bday, lbday,
|
|
|
|
ctry, lctry,
|
|
|
|
locality, llocality,
|
|
|
|
email, lemail,
|
|
|
|
orgname, lorgname,
|
|
|
|
orgunit, lorgunit
|
|
|
|
}).
|
2006-11-20 14:20:47 +01:00
|
|
|
-record(private_storage, {usns, xml}).
|
2005-08-29 21:00:10 +02:00
|
|
|
|
|
|
|
-define(MAX_RECORDS_PER_TRANSACTION, 1000).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2006-11-20 14:20:47 +01:00
|
|
|
%%% How to use:
|
|
|
|
%%% A table can be converted from Mnesia to an ODBC database by calling
|
|
|
|
%%% one of the API function with the following parameters:
|
|
|
|
%%% - Server is the server domain you want to convert
|
|
|
|
%%% - Output can be either odbc to export to the configured relational
|
|
|
|
%%% database or "Filename" to export to text file.
|
2005-08-29 21:00:10 +02:00
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
export_passwd(Server, Output) ->
|
2005-08-29 21:00:10 +02:00
|
|
|
export_common(
|
2005-09-10 19:01:30 +02:00
|
|
|
Server, passwd, Output,
|
2006-11-20 14:20:47 +01:00
|
|
|
fun(Host, {passwd, {LUser, LServer}, Password} = _R)
|
2005-08-29 21:00:10 +02:00
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Pass = ejabberd_odbc:escape(Password),
|
|
|
|
["delete from users where username='", Username ,"';"
|
|
|
|
"insert into users(username, password) "
|
|
|
|
"values ('", Username, "', '", Pass, "');"];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
export_roster(Server, Output) ->
|
2005-08-29 21:00:10 +02:00
|
|
|
export_common(
|
2005-09-10 19:01:30 +02:00
|
|
|
Server, roster, Output,
|
2008-10-13 11:39:58 +02:00
|
|
|
fun(Host, #roster{usj = {LUser, LServer, {N, D, Res} = _LJID}} = R)
|
2005-08-29 21:00:10 +02:00
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2009-06-01 18:52:14 +02:00
|
|
|
SJID = ejabberd_odbc:escape(exmpp_jid:to_list(N, D, Res)),
|
2005-08-29 21:00:10 +02:00
|
|
|
ItemVals = record_to_string(R),
|
|
|
|
ItemGroups = groups_to_string(R),
|
|
|
|
["delete from rosterusers "
|
|
|
|
" where username='", Username, "' "
|
|
|
|
" and jid='", SJID, "';"
|
|
|
|
"insert into rosterusers("
|
|
|
|
" username, jid, nick, "
|
2006-09-10 21:25:13 +02:00
|
|
|
" subscription, ask, askmessage, "
|
2005-08-29 21:00:10 +02:00
|
|
|
" server, subscribe, type) "
|
|
|
|
" values ", ItemVals, ";"
|
|
|
|
"delete from rostergroups "
|
|
|
|
" where username='", Username, "' "
|
|
|
|
" and jid='", SJID, "';",
|
|
|
|
[["insert into rostergroups("
|
|
|
|
" username, jid, grp) "
|
|
|
|
" values ", ItemGroup, ";"] ||
|
|
|
|
ItemGroup <- ItemGroups]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
export_offline(Server, Output) ->
|
2005-08-29 21:00:10 +02:00
|
|
|
export_common(
|
2005-09-10 19:01:30 +02:00
|
|
|
Server, offline_msg, Output,
|
2005-08-29 21:00:10 +02:00
|
|
|
fun(Host, #offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2008-10-13 11:39:58 +02:00
|
|
|
Packet0 = exmpp_stanza:set_jids(Packet,
|
2009-06-01 18:52:14 +02:00
|
|
|
exmpp_jid:to_list(From),
|
|
|
|
exmpp_jid:to_list(To)),
|
2008-10-13 11:39:58 +02:00
|
|
|
Packet1 = exmpp_xml:append_child(Packet0,
|
|
|
|
jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(TimeStamp))),
|
2005-08-29 21:00:10 +02:00
|
|
|
XML =
|
|
|
|
ejabberd_odbc:escape(
|
2008-10-13 11:39:58 +02:00
|
|
|
exmpp_xml:document_to_list(Packet1)),
|
2005-08-29 21:00:10 +02:00
|
|
|
["insert into spool(username, xml) "
|
|
|
|
"values ('", Username, "', '",
|
|
|
|
XML,
|
|
|
|
"');"];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
export_last(Server, Output) ->
|
2005-08-29 21:00:10 +02:00
|
|
|
export_common(
|
2005-09-10 19:01:30 +02:00
|
|
|
Server, last_activity, Output,
|
2005-08-29 21:00:10 +02:00
|
|
|
fun(Host, #last_activity{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
status = Status})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Seconds = ejabberd_odbc:escape(integer_to_list(TimeStamp)),
|
|
|
|
State = ejabberd_odbc:escape(Status),
|
|
|
|
["delete from last where username='", Username, "';"
|
|
|
|
"insert into last(username, seconds, state) "
|
|
|
|
"values ('", Username, "', '", Seconds, "', '", State, "');"];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
2005-10-07 01:57:34 +02:00
|
|
|
export_vcard(Server, Output) ->
|
|
|
|
export_common(
|
|
|
|
Server, vcard, Output,
|
|
|
|
fun(Host, #vcard{us = {LUser, LServer},
|
|
|
|
vcard = VCARD})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SVCARD = ejabberd_odbc:escape(
|
2008-10-13 11:39:58 +02:00
|
|
|
exmpp_xml:document_to_list(VCARD)),
|
2005-10-07 01:57:34 +02:00
|
|
|
["delete from vcard where username='", Username, "';"
|
|
|
|
"insert into vcard(username, vcard) "
|
|
|
|
"values ('", Username, "', '", SVCARD, "');"];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
|
|
|
export_vcard_search(Server, Output) ->
|
|
|
|
export_common(
|
|
|
|
Server, vcard_search, Output,
|
|
|
|
fun(Host, #vcard_search{user = {User, LServer},
|
|
|
|
luser = LUser,
|
|
|
|
fn = FN, lfn = LFN,
|
|
|
|
family = Family, lfamily = LFamily,
|
|
|
|
given = Given, lgiven = LGiven,
|
|
|
|
middle = Middle, lmiddle = LMiddle,
|
|
|
|
nickname = Nickname, lnickname = LNickname,
|
|
|
|
bday = BDay, lbday = LBDay,
|
|
|
|
ctry = CTRY, lctry = LCTRY,
|
|
|
|
locality = Locality, llocality = LLocality,
|
|
|
|
email = EMail, lemail = LEMail,
|
|
|
|
orgname = OrgName, lorgname = LOrgName,
|
|
|
|
orgunit = OrgUnit, lorgunit = LOrgUnit
|
|
|
|
})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(User),
|
|
|
|
LUsername = ejabberd_odbc:escape(LUser),
|
|
|
|
|
|
|
|
SFN = ejabberd_odbc:escape(FN),
|
|
|
|
SLFN = ejabberd_odbc:escape(LFN),
|
|
|
|
SFamily = ejabberd_odbc:escape(Family),
|
|
|
|
SLFamily = ejabberd_odbc:escape(LFamily),
|
|
|
|
SGiven = ejabberd_odbc:escape(Given),
|
|
|
|
SLGiven = ejabberd_odbc:escape(LGiven),
|
|
|
|
SMiddle = ejabberd_odbc:escape(Middle),
|
|
|
|
SLMiddle = ejabberd_odbc:escape(LMiddle),
|
|
|
|
SNickname = ejabberd_odbc:escape(Nickname),
|
|
|
|
SLNickname = ejabberd_odbc:escape(LNickname),
|
|
|
|
SBDay = ejabberd_odbc:escape(BDay),
|
|
|
|
SLBDay = ejabberd_odbc:escape(LBDay),
|
|
|
|
SCTRY = ejabberd_odbc:escape(CTRY),
|
|
|
|
SLCTRY = ejabberd_odbc:escape(LCTRY),
|
|
|
|
SLocality = ejabberd_odbc:escape(Locality),
|
|
|
|
SLLocality = ejabberd_odbc:escape(LLocality),
|
|
|
|
SEMail = ejabberd_odbc:escape(EMail),
|
|
|
|
SLEMail = ejabberd_odbc:escape(LEMail),
|
|
|
|
SOrgName = ejabberd_odbc:escape(OrgName),
|
|
|
|
SLOrgName = ejabberd_odbc:escape(LOrgName),
|
|
|
|
SOrgUnit = ejabberd_odbc:escape(OrgUnit),
|
|
|
|
SLOrgUnit = ejabberd_odbc:escape(LOrgUnit),
|
|
|
|
|
|
|
|
["delete from vcard_search where lusername='", LUsername, "';"
|
|
|
|
"insert into vcard_search("
|
|
|
|
" username, lusername, fn, lfn, family, lfamily,"
|
|
|
|
" given, lgiven, middle, lmiddle, nickname, lnickname,"
|
|
|
|
" bday, lbday, ctry, lctry, locality, llocality,"
|
|
|
|
" email, lemail, orgname, lorgname, orgunit, lorgunit)"
|
|
|
|
"values (",
|
|
|
|
" '", Username, "', '", LUsername, "',"
|
|
|
|
" '", SFN, "', '", SLFN, "',"
|
|
|
|
" '", SFamily, "', '", SLFamily, "',"
|
|
|
|
" '", SGiven, "', '", SLGiven, "',"
|
|
|
|
" '", SMiddle, "', '", SLMiddle, "',"
|
|
|
|
" '", SNickname, "', '", SLNickname, "',"
|
|
|
|
" '", SBDay, "', '", SLBDay, "',"
|
|
|
|
" '", SCTRY, "', '", SLCTRY, "',"
|
|
|
|
" '", SLocality, "', '", SLLocality, "',"
|
|
|
|
" '", SEMail, "', '", SLEMail, "',"
|
|
|
|
" '", SOrgName, "', '", SLOrgName, "',"
|
|
|
|
" '", SOrgUnit, "', '", SLOrgUnit, "');"];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end).
|
|
|
|
|
2006-11-20 14:20:47 +01:00
|
|
|
export_private_storage(Server, Output) ->
|
|
|
|
export_common(
|
2006-12-21 00:22:05 +01:00
|
|
|
Server, private_storage, Output,
|
|
|
|
fun(Host, #private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = Data})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-11-20 14:20:47 +01:00
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
2006-12-21 00:22:05 +01:00
|
|
|
SData = ejabberd_odbc:escape(
|
2008-10-13 11:39:58 +02:00
|
|
|
exmpp_xml:document_to_list(Data)),
|
2006-11-20 14:20:47 +01:00
|
|
|
odbc_queries:set_private_data_sql(Username, LXMLNS, SData);
|
2006-12-21 00:22:05 +01:00
|
|
|
(_Host, _R) ->
|
2006-11-20 14:20:47 +01:00
|
|
|
[]
|
2006-12-21 00:22:05 +01:00
|
|
|
end).
|
2006-11-20 14:20:47 +01:00
|
|
|
|
2005-08-29 21:00:10 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
export_common(Server, Table, Output, ConvertFun) ->
|
|
|
|
IO = case Output of
|
|
|
|
odbc ->
|
|
|
|
odbc;
|
|
|
|
_ ->
|
|
|
|
{ok, IODevice} = file:open(Output, [write, raw]),
|
|
|
|
IODevice
|
|
|
|
end,
|
2005-08-29 21:00:10 +02:00
|
|
|
mnesia:transaction(
|
|
|
|
fun() ->
|
2005-09-10 19:01:30 +02:00
|
|
|
mnesia:read_lock_table(Table),
|
2008-10-13 11:39:58 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-08-29 21:00:10 +02:00
|
|
|
{_N, SQLs} =
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, {N, SQLs} = Acc) ->
|
|
|
|
case ConvertFun(LServer, R) of
|
|
|
|
[] ->
|
|
|
|
Acc;
|
|
|
|
SQL ->
|
|
|
|
if
|
|
|
|
N < ?MAX_RECORDS_PER_TRANSACTION - 1 ->
|
|
|
|
{N + 1, [SQL | SQLs]};
|
|
|
|
true ->
|
2006-11-20 14:20:47 +01:00
|
|
|
%% Execute full SQL transaction
|
2005-09-10 19:01:30 +02:00
|
|
|
output(LServer, IO,
|
|
|
|
["begin;",
|
|
|
|
lists:reverse([SQL | SQLs]),
|
|
|
|
"commit"]),
|
2005-08-29 21:00:10 +02:00
|
|
|
{0, []}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, {0, []}, Table),
|
2006-11-20 14:20:47 +01:00
|
|
|
%% Execute SQL transaction with remaining records
|
2005-09-10 19:01:30 +02:00
|
|
|
output(LServer, IO,
|
|
|
|
["begin;",
|
|
|
|
lists:reverse(SQLs),
|
|
|
|
"commit"])
|
2005-08-29 21:00:10 +02:00
|
|
|
end).
|
|
|
|
|
2005-09-10 19:01:30 +02:00
|
|
|
output(LServer, IO, SQL) ->
|
|
|
|
case IO of
|
|
|
|
odbc ->
|
|
|
|
catch ejabberd_odbc:sql_query(LServer, SQL);
|
|
|
|
_ ->
|
2005-09-10 20:03:48 +02:00
|
|
|
file:write(IO, [SQL, $;, $\n])
|
2005-09-10 19:01:30 +02:00
|
|
|
end.
|
|
|
|
|
2008-10-13 11:39:58 +02:00
|
|
|
record_to_string(#roster{usj = {User, _Server, {N, D, R} = _JID},
|
2005-08-29 21:00:10 +02:00
|
|
|
name = Name,
|
|
|
|
subscription = Subscription,
|
2006-09-10 21:25:13 +02:00
|
|
|
ask = Ask,
|
|
|
|
askmessage = AskMessage}) ->
|
2005-08-29 21:00:10 +02:00
|
|
|
Username = ejabberd_odbc:escape(User),
|
2009-06-01 18:52:14 +02:00
|
|
|
SJID = ejabberd_odbc:escape(exmpp_jid:to_list(N, D, R)),
|
2005-08-29 21:00:10 +02:00
|
|
|
Nick = ejabberd_odbc:escape(Name),
|
|
|
|
SSubscription = case Subscription of
|
|
|
|
both -> "B";
|
|
|
|
to -> "T";
|
|
|
|
from -> "F";
|
|
|
|
none -> "N"
|
|
|
|
end,
|
|
|
|
SAsk = case Ask of
|
|
|
|
subscribe -> "S";
|
|
|
|
unsubscribe -> "U";
|
|
|
|
both -> "B";
|
|
|
|
out -> "O";
|
|
|
|
in -> "I";
|
|
|
|
none -> "N"
|
|
|
|
end,
|
2007-05-15 02:07:25 +02:00
|
|
|
SAskMessage =
|
|
|
|
case catch ejabberd_odbc:escape(
|
|
|
|
binary_to_list(list_to_binary([AskMessage]))) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
[];
|
|
|
|
SAM ->
|
|
|
|
SAM
|
|
|
|
end,
|
2005-08-29 21:00:10 +02:00
|
|
|
["("
|
|
|
|
"'", Username, "',"
|
|
|
|
"'", SJID, "',"
|
|
|
|
"'", Nick, "',"
|
|
|
|
"'", SSubscription, "',"
|
|
|
|
"'", SAsk, "',"
|
2007-01-30 16:23:48 +01:00
|
|
|
"'", SAskMessage, "',"
|
2005-08-29 21:00:10 +02:00
|
|
|
"'N', '', 'item')"].
|
|
|
|
|
2008-10-13 11:39:58 +02:00
|
|
|
groups_to_string(#roster{usj = {User, _Server, {N, D, R} = _JID},
|
2005-08-29 21:00:10 +02:00
|
|
|
groups = Groups}) ->
|
|
|
|
Username = ejabberd_odbc:escape(User),
|
2009-06-01 18:52:14 +02:00
|
|
|
SJID = ejabberd_odbc:escape(exmpp_jid:to_list(N, D, R)),
|
2005-08-29 21:00:10 +02:00
|
|
|
[["("
|
|
|
|
"'", Username, "',"
|
|
|
|
"'", SJID, "',"
|
|
|
|
"'", ejabberd_odbc:escape(Group), "')"] || Group <- Groups].
|