mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-22 16:20:52 +01:00
Get rid of duplicated code
This commit is contained in:
parent
41a0eae0bd
commit
cea23b055b
@ -305,7 +305,7 @@ export_muc_room(Server, Output) ->
|
||||
true ->
|
||||
SName = ejabberd_odbc:escape(Name),
|
||||
SRoomHost = ejabberd_odbc:escape(RoomHost),
|
||||
SOpts = mod_muc_odbc:encode_opts(Opts),
|
||||
SOpts = ejabberd_odbc:encode_term(Opts),
|
||||
["delete from muc_room where name='", SName,
|
||||
"' and host='", SRoomHost, "';",
|
||||
"insert into muc_room(name, host, opts) values (",
|
||||
@ -345,7 +345,7 @@ export_irc_custom(Server, Output) ->
|
||||
jlib:jid_to_string(
|
||||
jlib:make_jid(U, S, ""))),
|
||||
SIRCHost = ejabberd_odbc:escape(IRCHost),
|
||||
SData = mod_irc_odbc:encode_data(Data),
|
||||
SData = ejabberd_odbc:encode_term(Data),
|
||||
["delete from irc_custom where jid='", SJID,
|
||||
"' and host='", SIRCHost, "';"
|
||||
"insert into irc_custom(jid, host, data) values ("
|
||||
@ -407,7 +407,7 @@ export_sr_group(Server, Output) ->
|
||||
fun(Host, #sr_group{group_host = {Group, LServer}, opts = Opts})
|
||||
when LServer == Host ->
|
||||
SGroup = ejabberd_odbc:escape(Group),
|
||||
SOpts = mod_shared_roster_odbc:encode_opts(Opts),
|
||||
SOpts = ejabberd_odbc:encode_term(Opts),
|
||||
["delete from sr_group where name='", Group, "';"
|
||||
"insert into sr_group(name, opts) values ('",
|
||||
SGroup, "', '", SOpts, "');"];
|
||||
|
@ -742,7 +742,8 @@ announce_motd(Host, Packet) ->
|
||||
lists:foreach(
|
||||
fun({U, _S, _R}) ->
|
||||
Username = ejabberd_odbc:escape(U),
|
||||
update_t("motd",
|
||||
odbc_queries:update_t(
|
||||
"motd",
|
||||
["username", "xml"],
|
||||
[Username, ""],
|
||||
["username='", Username, "'"])
|
||||
@ -777,7 +778,8 @@ announce_motd_update(LServer, Packet) ->
|
||||
announce_motd_delete(LServer),
|
||||
XML = ejabberd_odbc:escape(xml:element_to_binary(Packet)),
|
||||
F = fun() ->
|
||||
update_t("motd",
|
||||
odbc_queries:update_t(
|
||||
"motd",
|
||||
["username", "xml"],
|
||||
["", XML],
|
||||
["username=''"])
|
||||
@ -829,8 +831,8 @@ send_motd(#jid{luser = LUser, lserver = LServer} = JID) when LUser /= "" ->
|
||||
Local = jlib:make_jid("", LServer, ""),
|
||||
ejabberd_router:route(Local, JID, Packet),
|
||||
F = fun() ->
|
||||
update_t(
|
||||
["motd"],
|
||||
odbc_queries:update_t(
|
||||
"motd",
|
||||
["username", "xml"],
|
||||
[Username, ""],
|
||||
["username='", Username, "'"])
|
||||
@ -881,27 +883,3 @@ send_announcement_to_all(Host, SubjectS, BodyS) ->
|
||||
Dest = jlib:make_jid(U, S, R),
|
||||
ejabberd_router:route(Local, Dest, Packet)
|
||||
end, Sessions).
|
||||
|
||||
%% Almost a copy of string:join/2.
|
||||
%% We use this version because string:join/2 is relatively
|
||||
%% new function (introduced in R12B-0).
|
||||
join([], _Sep) ->
|
||||
[];
|
||||
join([H|T], Sep) ->
|
||||
[H, [[Sep, X] || X <- T]].
|
||||
|
||||
%% Safe atomic update.
|
||||
update_t(Table, Fields, Vals, Where) ->
|
||||
UPairs = lists:zipwith(fun(A, B) -> A ++ "='" ++ B ++ "'" end,
|
||||
Fields, Vals),
|
||||
case ejabberd_odbc:sql_query_t(
|
||||
["update ", Table, " set ",
|
||||
join(UPairs, ", "),
|
||||
" where ", Where, ";"]) of
|
||||
{updated, 1} ->
|
||||
ok;
|
||||
_ ->
|
||||
ejabberd_odbc:sql_query_t(
|
||||
["insert into ", Table, "(", join(Fields, ", "),
|
||||
") values ('", join(Vals, "', '"), "');"])
|
||||
end.
|
||||
|
@ -34,7 +34,6 @@
|
||||
-export([start_link/2,
|
||||
start/2,
|
||||
stop/1,
|
||||
encode_data/1,
|
||||
closed_connection/3,
|
||||
get_connection_params/3]).
|
||||
|
||||
@ -549,7 +548,7 @@ get_form(ServerHost, Host, From, [], Lang) ->
|
||||
["select data from irc_custom where "
|
||||
"jid='", SJID, "' and host='", SHost, "';"]) of
|
||||
{selected, ["data"], [{SData}]} ->
|
||||
Data = decode_data(SData),
|
||||
Data = ejabberd_odbc:decode_term(SData),
|
||||
{xml:get_attr_s(username, Data),
|
||||
xml:get_attr_s(connections_params, Data)};
|
||||
{'EXIT', _} ->
|
||||
@ -647,7 +646,7 @@ set_form(ServerHost, Host, From, [], _Lang, XData) ->
|
||||
{ok, Tokens, _} ->
|
||||
case erl_parse:parse_term(Tokens) of
|
||||
{ok, ConnectionsParams} ->
|
||||
SData = encode_data(
|
||||
SData = ejabberd_odbc:encode_term(
|
||||
[{username,
|
||||
Username},
|
||||
{connections_params,
|
||||
@ -655,7 +654,8 @@ set_form(ServerHost, Host, From, [], _Lang, XData) ->
|
||||
case ejabberd_odbc:sql_transaction(
|
||||
LServer,
|
||||
fun() ->
|
||||
update_t("irc_custom",
|
||||
odbc_queries:update_t(
|
||||
"irc_custom",
|
||||
["jid", "host", "data"],
|
||||
[SJID, SHost, SData],
|
||||
["jid='", SJID,
|
||||
@ -715,7 +715,7 @@ get_connection_params(Host, ServerHost, From, IRCServer) ->
|
||||
{selected, ["data"], []} ->
|
||||
{User, DefaultEncoding, ?DEFAULT_IRC_PORT, ""};
|
||||
{selected, ["data"], [{SData}]} ->
|
||||
Data = decode_data(SData),
|
||||
Data = ejabberd_odbc:decode_term(SData),
|
||||
Username = xml:get_attr_s(username, Data),
|
||||
{NewUsername, NewEncoding, NewPort, NewPassword} =
|
||||
case lists:keysearch(
|
||||
@ -844,7 +844,7 @@ adhoc_register(ServerHost, From, To, #adhoc_request{lang = Lang,
|
||||
Username = User,
|
||||
ConnectionsParams = [];
|
||||
{selected, ["data"], [{Data1}]} ->
|
||||
Data = decode_data(Data1),
|
||||
Data = ejabberd_odbc:decode_term(Data1),
|
||||
Username = xml:get_attr_s(username, Data),
|
||||
ConnectionsParams = xml:get_attr_s(connections_params, Data)
|
||||
end,
|
||||
@ -870,12 +870,14 @@ adhoc_register(ServerHost, From, To, #adhoc_request{lang = Lang,
|
||||
if Error /= false ->
|
||||
Error;
|
||||
Action == "complete" ->
|
||||
SData = encode_data([{username, Username},
|
||||
SData = ejabberd_odbc:encode_term(
|
||||
[{username, Username},
|
||||
{connections_params, ConnectionsParams}]),
|
||||
case catch ejabberd_odbc:sql_transaction(
|
||||
LServer,
|
||||
fun() ->
|
||||
update_t("irc_custom",
|
||||
odbc_queries:update_t(
|
||||
"irc_custom",
|
||||
["jid", "host", "data"],
|
||||
[SJID, SHost, SData],
|
||||
["jid='", SJID,
|
||||
@ -1029,35 +1031,3 @@ parse_connections_params([{ServerN, Server} | Servers], Encodings, Ports, Passwo
|
||||
{NewPort, NewPorts} = retrieve_connections_params(Ports, ServerN),
|
||||
{NewPassword, NewPasswords} = retrieve_connections_params(Passwords, ServerN),
|
||||
[{Server, NewEncoding, NewPort, NewPassword} | parse_connections_params(Servers, NewEncodings, NewPorts, NewPasswords)].
|
||||
|
||||
encode_data(Data) ->
|
||||
ejabberd_odbc:escape(erl_prettypr:format(erl_syntax:abstract(Data))).
|
||||
|
||||
decode_data(Str) ->
|
||||
{ok, Tokens, _} = erl_scan:string(Str ++ "."),
|
||||
{ok, Data} = erl_parse:parse_term(Tokens),
|
||||
Data.
|
||||
|
||||
%% Almost a copy of string:join/2.
|
||||
%% We use this version because string:join/2 is relatively
|
||||
%% new function (introduced in R12B-0).
|
||||
join([], _Sep) ->
|
||||
[];
|
||||
join([H|T], Sep) ->
|
||||
[H, [[Sep, X] || X <- T]].
|
||||
|
||||
%% Safe atomic update.
|
||||
update_t(Table, Fields, Vals, Where) ->
|
||||
UPairs = lists:zipwith(fun(A, B) -> A ++ "='" ++ B ++ "'" end,
|
||||
Fields, Vals),
|
||||
case ejabberd_odbc:sql_query_t(
|
||||
["update ", Table, " set ",
|
||||
join(UPairs, ", "),
|
||||
" where ", Where, ";"]) of
|
||||
{updated, 1} ->
|
||||
ok;
|
||||
_ ->
|
||||
ejabberd_odbc:sql_query_t(
|
||||
["insert into ", Table, "(", join(Fields, ", "),
|
||||
") values ('", join(Vals, "', '"), "');"])
|
||||
end.
|
||||
|
@ -41,8 +41,6 @@
|
||||
create_room/5,
|
||||
process_iq_disco_items/4,
|
||||
broadcast_service_message/2,
|
||||
encode_opts/1,
|
||||
decode_opts/1,
|
||||
can_use_nick/4]).
|
||||
|
||||
%% gen_server callbacks
|
||||
@ -115,11 +113,12 @@ store_room(ServerHost, Host, Name, Opts) ->
|
||||
SName = ejabberd_odbc:escape(Name),
|
||||
SHost = ejabberd_odbc:escape(Host),
|
||||
LServer = jlib:nameprep(ServerHost),
|
||||
SOpts = ejabberd_odbc:encode_term(Opts),
|
||||
F = fun() ->
|
||||
update_t(
|
||||
odbc_queries:update_t(
|
||||
"muc_room",
|
||||
["name", "host", "opts"],
|
||||
[SName, SHost, encode_opts(Opts)],
|
||||
[SName, SHost, SOpts],
|
||||
["name='", SName, "' and host='", SHost, "'"])
|
||||
end,
|
||||
ejabberd_odbc:sql_transaction(LServer, F).
|
||||
@ -132,7 +131,7 @@ restore_room(ServerHost, Host, Name) ->
|
||||
LServer, ["select opts from muc_room where name='",
|
||||
SName, "' and host='", SHost, "';"]) of
|
||||
{selected, ["opts"], [{Opts}]} ->
|
||||
decode_opts(Opts);
|
||||
ejabberd_odbc:decode_term(Opts);
|
||||
_ ->
|
||||
error
|
||||
end.
|
||||
@ -549,7 +548,7 @@ load_permanent_rooms(Host, ServerHost, Access, HistorySize, RoomShaper) ->
|
||||
Room,
|
||||
HistorySize,
|
||||
RoomShaper,
|
||||
decode_opts(Opts),
|
||||
ejabberd_odbc:decode_term(Opts),
|
||||
?MODULE),
|
||||
register_room(Host, Room, Pid);
|
||||
_ ->
|
||||
@ -577,7 +576,8 @@ start_new_room(Host, ServerHost, Access, Room,
|
||||
?DEBUG("MUC: restore room '~s'~n", [Room]),
|
||||
mod_muc_room:start(Host, ServerHost, Access,
|
||||
Room, HistorySize,
|
||||
RoomShaper, decode_opts(Opts), ?MODULE)
|
||||
RoomShaper, ejabberd_odbc:decode_term(Opts),
|
||||
?MODULE)
|
||||
end.
|
||||
|
||||
register_room(Host, Room, Pid) ->
|
||||
@ -766,7 +766,7 @@ iq_set_register_info(ServerHost, Host, From, Nick, Lang) ->
|
||||
true
|
||||
end,
|
||||
if Allow ->
|
||||
update_t(
|
||||
odbc_queries:update_t(
|
||||
"muc_registered",
|
||||
["jid", "host", "nick"],
|
||||
[SJID, SHost, SNick],
|
||||
@ -873,35 +873,3 @@ clean_table_from_bad_node(Node, Host) ->
|
||||
end, Es)
|
||||
end,
|
||||
mnesia:async_dirty(F).
|
||||
|
||||
encode_opts(Opts) ->
|
||||
ejabberd_odbc:escape(erl_prettypr:format(erl_syntax:abstract(Opts))).
|
||||
|
||||
decode_opts(Str) ->
|
||||
{ok, Tokens, _} = erl_scan:string(Str ++ "."),
|
||||
{ok, Opts} = erl_parse:parse_term(Tokens),
|
||||
Opts.
|
||||
|
||||
%% Almost a copy of string:join/2.
|
||||
%% We use this version because string:join/2 is relatively
|
||||
%% new function (introduced in R12B-0).
|
||||
join([], _Sep) ->
|
||||
[];
|
||||
join([H|T], Sep) ->
|
||||
[H, [[Sep, X] || X <- T]].
|
||||
|
||||
%% Safe atomic update.
|
||||
update_t(Table, Fields, Vals, Where) ->
|
||||
UPairs = lists:zipwith(fun(A, B) -> A ++ "='" ++ B ++ "'" end,
|
||||
Fields, Vals),
|
||||
case ejabberd_odbc:sql_query_t(
|
||||
["update ", Table, " set ",
|
||||
join(UPairs, ", "),
|
||||
" where ", Where, ";"]) of
|
||||
{updated, 1} ->
|
||||
ok;
|
||||
_ ->
|
||||
ejabberd_odbc:sql_query_t(
|
||||
["insert into ", Table, "(", join(Fields, ", "),
|
||||
") values ('", join(Vals, "', '"), "');"])
|
||||
end.
|
||||
|
@ -52,7 +52,6 @@
|
||||
get_group_explicit_users/2,
|
||||
is_user_in_group/3,
|
||||
add_user_to_group/3,
|
||||
encode_opts/1,
|
||||
remove_user_from_group/3]).
|
||||
|
||||
-include("ejabberd.hrl").
|
||||
@ -403,7 +402,7 @@ groups_with_opts(Host) ->
|
||||
case ejabberd_odbc:sql_query(
|
||||
Host, ["select name, opts from sr_group;"]) of
|
||||
{selected, ["name", "opts"], Rs} ->
|
||||
[{G, decode_opts(Opts)} || {G, Opts} <- Rs];
|
||||
[{G, ejabberd_odbc:decode_term(Opts)} || {G, Opts} <- Rs];
|
||||
_ ->
|
||||
[]
|
||||
end.
|
||||
@ -413,9 +412,9 @@ create_group(Host, Group) ->
|
||||
|
||||
create_group(Host, Group, Opts) ->
|
||||
SGroup = ejabberd_odbc:escape(Group),
|
||||
SOpts = encode_opts(Opts),
|
||||
SOpts = ejabberd_odbc:encode_term(Opts),
|
||||
F = fun() ->
|
||||
update_t("sr_group",
|
||||
odbc_queries:update_t("sr_group",
|
||||
["name", "opts"],
|
||||
[SGroup, SOpts],
|
||||
["name='", SGroup, "'"])
|
||||
@ -438,16 +437,16 @@ get_group_opts(Host, Group) ->
|
||||
Host, ["select opts from sr_group "
|
||||
"where name='", SGroup, "';"]) of
|
||||
{selected, ["opts"], [{SOpts}]} ->
|
||||
decode_opts(SOpts);
|
||||
ejabberd_odbc:decode_term(SOpts);
|
||||
_ ->
|
||||
error
|
||||
end.
|
||||
|
||||
set_group_opts(Host, Group, Opts) ->
|
||||
SGroup = ejabberd_odbc:escape(Group),
|
||||
SOpts = encode_opts(Opts),
|
||||
SOpts = ejabberd_odbc:encode_term(Opts),
|
||||
F = fun() ->
|
||||
update_t("sr_group",
|
||||
odbc_queries:update_t("sr_group",
|
||||
["name", "opts"],
|
||||
[SGroup, SOpts],
|
||||
["name='", SGroup, "'"])
|
||||
@ -473,7 +472,7 @@ is_group_enabled(Host1, Group1) ->
|
||||
Host, ["select opts from sr_group "
|
||||
"where name='", SGroup, "';"]) of
|
||||
{selected, ["opts"], [{SOpts}]} ->
|
||||
Opts = decode_opts(SOpts),
|
||||
Opts = ejabberd_odbc:decode_term(SOpts),
|
||||
not lists:member(disabled, Opts);
|
||||
_ ->
|
||||
false
|
||||
@ -486,7 +485,7 @@ get_group_opt(Host, Group, Opt, Default) ->
|
||||
Host, ["select opts from sr_group "
|
||||
"where name='", SGroup, "';"]) of
|
||||
{selected, ["opts"], [{SOpts}]} ->
|
||||
Opts = decode_opts(SOpts),
|
||||
Opts = ejabberd_odbc:decode_term(SOpts),
|
||||
case lists:keysearch(Opt, 1, Opts) of
|
||||
{value, {_, Val}} ->
|
||||
Val;
|
||||
@ -659,7 +658,8 @@ add_user_to_group(Host, US, Group) ->
|
||||
SJID = make_jid_s(US),
|
||||
SGroup = ejabberd_odbc:escape(Group),
|
||||
F = fun() ->
|
||||
update_t("sr_user",
|
||||
odbc_queries:update_t(
|
||||
"sr_user",
|
||||
["jid", "grp"],
|
||||
[SJID, SGroup],
|
||||
["jid='", SJID, "' and grp='", SGroup, "'"])
|
||||
@ -1163,35 +1163,3 @@ make_jid_s(U, S) ->
|
||||
|
||||
make_jid_s({U, S}) ->
|
||||
make_jid_s(U, S).
|
||||
|
||||
encode_opts(Opts) ->
|
||||
ejabberd_odbc:escape(erl_prettypr:format(erl_syntax:abstract(Opts))).
|
||||
|
||||
decode_opts(Str) ->
|
||||
{ok, Tokens, _} = erl_scan:string(Str ++ "."),
|
||||
{ok, Opts} = erl_parse:parse_term(Tokens),
|
||||
Opts.
|
||||
|
||||
%% Almost a copy of string:join/2.
|
||||
%% We use this version because string:join/2 is relatively
|
||||
%% new function (introduced in R12B-0).
|
||||
join([], _Sep) ->
|
||||
[];
|
||||
join([H|T], Sep) ->
|
||||
[H, [[Sep, X] || X <- T]].
|
||||
|
||||
%% Safe atomic update.
|
||||
update_t(Table, Fields, Vals, Where) ->
|
||||
UPairs = lists:zipwith(fun(A, B) -> A ++ "='" ++ B ++ "'" end,
|
||||
Fields, Vals),
|
||||
case ejabberd_odbc:sql_query_t(
|
||||
["update ", Table, " set ",
|
||||
join(UPairs, ", "),
|
||||
" where ", Where, ";"]) of
|
||||
{updated, 1} ->
|
||||
ok;
|
||||
_ ->
|
||||
ejabberd_odbc:sql_query_t(
|
||||
["insert into ", Table, "(", join(Fields, ", "),
|
||||
") values ('", join(Vals, "', '"), "');"])
|
||||
end.
|
||||
|
@ -75,8 +75,8 @@ add_xupdate(LUser, LServer, Hash) ->
|
||||
Username = ejabberd_odbc:escape(LUser),
|
||||
SHash = ejabberd_odbc:escape(Hash),
|
||||
F = fun() ->
|
||||
update_t(
|
||||
["vcard_xupdate"],
|
||||
odbc_queries:update_t(
|
||||
"vcard_xupdate",
|
||||
["username", "hash"],
|
||||
[Username, SHash],
|
||||
["username='", Username, "'"])
|
||||
@ -131,27 +131,3 @@ build_xphotoel(User, Host) ->
|
||||
end,
|
||||
PhotoEl = [{xmlelement, "photo", [], PhotoSubEls}],
|
||||
{xmlelement, "x", [{"xmlns", ?NS_VCARD_UPDATE}], PhotoEl}.
|
||||
|
||||
%% Almost a copy of string:join/2.
|
||||
%% We use this version because string:join/2 is relatively
|
||||
%% new function (introduced in R12B-0).
|
||||
join([], _Sep) ->
|
||||
[];
|
||||
join([H|T], Sep) ->
|
||||
[H, [[Sep, X] || X <- T]].
|
||||
|
||||
%% Safe atomic update.
|
||||
update_t(Table, Fields, Vals, Where) ->
|
||||
UPairs = lists:zipwith(fun(A, B) -> A ++ "='" ++ B ++ "'" end,
|
||||
Fields, Vals),
|
||||
case ejabberd_odbc:sql_query_t(
|
||||
["update ", Table, " set ",
|
||||
join(UPairs, ", "),
|
||||
" where ", Where, ";"]) of
|
||||
{updated, 1} ->
|
||||
ok;
|
||||
_ ->
|
||||
ejabberd_odbc:sql_query_t(
|
||||
["insert into ", Table, "(", join(Fields, ", "),
|
||||
") values ('", join(Vals, "', '"), "');"])
|
||||
end.
|
||||
|
@ -40,6 +40,8 @@
|
||||
escape/1,
|
||||
escape_like/1,
|
||||
to_bool/1,
|
||||
encode_term/1,
|
||||
decode_term/1,
|
||||
keep_alive/1]).
|
||||
|
||||
%% gen_fsm callbacks
|
||||
@ -169,6 +171,14 @@ to_bool(true) -> true;
|
||||
to_bool(1) -> true;
|
||||
to_bool(_) -> false.
|
||||
|
||||
encode_term(Term) ->
|
||||
escape(erl_prettypr:format(erl_syntax:abstract(Term))).
|
||||
|
||||
decode_term(Str) ->
|
||||
{ok, Tokens, _} = erl_scan:string(Str ++ "."),
|
||||
{ok, Term} = erl_parse:parse_term(Tokens),
|
||||
Term.
|
||||
|
||||
%%%----------------------------------------------------------------------
|
||||
%%% Callback functions from gen_fsm
|
||||
%%%----------------------------------------------------------------------
|
||||
|
@ -28,6 +28,7 @@
|
||||
-author("mremond@process-one.net").
|
||||
|
||||
-export([get_db_type/0,
|
||||
update_t/4,
|
||||
sql_transaction/2,
|
||||
get_last/2,
|
||||
set_last_t/4,
|
||||
|
Loading…
Reference in New Issue
Block a user