Skip to content

Instantly share code, notes, and snippets.

@aszlig
Created September 1, 2015 07:03
Show Gist options
  • Save aszlig/e05b15e2aac2b33fb781 to your computer and use it in GitHub Desktop.
Save aszlig/e05b15e2aac2b33fb781 to your computer and use it in GitHub Desktop.
Converting the most relevant tables from an Ejabberd 2.10.0 text dump to a MongooseIM 1.5.1 text dump
#!/usr/bin/env escript
mxml([]) -> [].
m(Str) when is_list(Str) -> unicode:characters_to_binary(Str);
m(Str) when is_binary(Str) -> Str;
m(_) -> exit("WAT!?").
mjid({jid, A, B, C, D, E, F}) -> {jid, m(A), m(B), m(C), m(D), m(E), m(F)}.
mangle_aff({{A, B, C}, Aff}) -> {{m(A), m(B), m(C)}, Aff}.
mangle_room_opt(title, S) -> m(S);
mangle_room_opt(description, S) -> m(S);
mangle_room_opt(password, S) -> m(S);
mangle_room_opt(subject, S) -> m(S);
mangle_room_opt(subject_author, S) -> m(S);
mangle_room_opt(affiliations, S) -> [mangle_aff(A) || A <- S];
mangle_room_opt(_, Val) when is_atom(Val) -> Val;
mangle_room_opt(_, Val) when is_integer(Val) -> Val.
mangle_deflist(none) -> none;
mangle_deflist(Str) when is_list(Str) -> m(Str).
mangle_listitem({listitem, jid, {A, B, C}, D, E, F, G, H, I, J}) ->
{listitem, jid, {m(A), m(B), m(C)}, D, E, F, G, H, I, J};
mangle_listitem({listitem, group, Name, D, E, F, G, H, I, J}) ->
{listitem, group, m(Name), D, E, F, G, H, I, J};
mangle_listitem({listitem, subscription, _, _, _, _, _, _, _, _} = LI) -> LI;
mangle_listitem({listitem, none, _, _, _, _, _, _, _, _} = LI) -> LI.
mangle_privlists({Key, Vals}) -> {m(Key), [mangle_listitem(LI) || LI <- Vals]}.
mxmlel({xmlcdata, Val}) -> {xmlcdata, m(Val)};
mxmlel({xmlelement, Name, Attrs, Children}) ->
{xmlel, m(Name), [{m(K), m(V)} || {K, V} <- Attrs],
[mxmlel(C) || C <- Children]}.
mangle({roster, {A, B, {C, D, E}}, {F, G}, {H, I, J}, K, X1, X2, Gs, L, XML}) ->
{roster, {m(A), m(B), {m(C), m(D), m(E)}}, {m(F), m(G)}, {m(H), m(I),
m(J)}, m(K), X1, X2, [m(Gr) || Gr <- Gs], m(L), mxml(XML)};
mangle({muc_room, {A, B}, Opts}) ->
{muc_room, {m(A), m(B)},
[{O, mangle_room_opt(O, V)} || {O, V} <- Opts, O /= captcha_whitelist]};
mangle({privacy, {A, B}, Default, Lists}) ->
{privacy, {m(A), m(B)}, mangle_deflist(Default),
[mangle_privlists(L) || L <- Lists]};
mangle({vcard_search, {A, B}, {C, D}, E, F, G, H, I, J, K, L, M, N, O, P, Q, R,
S, T, U, V, W, X, Y, Z, Aa}) ->
{vcard_search, {m(A), m(B)}, {m(C), m(D)}, m(E), m(F), m(G), m(H), m(I),
m(J), m(K), m(L), m(M), m(N), m(O), m(P), m(Q), m(R), m(S), m(T), m(U),
m(V), m(W), m(X), m(Y), m(Z), m(Aa)};
mangle({offline_msg, {A, B}, TS, Ex, From, To, Xml}) ->
{offline_msg, {m(A), m(B)}, TS, Ex, mjid(From), mjid(To), mxmlel(Xml)};
mangle({private_storage, {A, B, C}, Xml}) ->
{private_storage, {m(A), m(B), m(C)}, mxmlel(Xml)};
mangle({passwd, {A, B}, C}) ->
{passwd, {m(A), m(B)}, C};
mangle({vcard, {A, B}, Xml}) ->
{vcard, {m(A), m(B)}, mxmlel(Xml)};
mangle({tables, List}) ->
{tables, List}.
main(_) ->
{ok, Terms} = file:consult("ejabberd.dump"),
NewTerms = [mangle(T) || T <- Terms],
PPrint = fun(T) -> io_lib:format("~p.~n", [T]) end,
Data = lists:map(PPrint, NewTerms),
file:write_file("mongooseim.dump", Data).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment