From ae5fd75c880271eabeab37c3fa9f7c963978e4d3 Mon Sep 17 00:00:00 2001 From: erebion Date: Thu, 29 May 2025 01:41:37 +0200 Subject: [PATCH 01/10] add mod_unified_push --- mod_unified_push/COPYING | 339 +++++++++ mod_unified_push/README.txt | 11 + mod_unified_push/mod_unified_push.spec | 5 + mod_unified_push/rebar.config | 6 + mod_unified_push/src/mod_unified_push.erl | 868 ++++++++++++++++++++++ mod_unified_push/xmpp_spec.patch | 50 ++ 6 files changed, 1279 insertions(+) create mode 100644 mod_unified_push/COPYING create mode 100644 mod_unified_push/README.txt create mode 100644 mod_unified_push/mod_unified_push.spec create mode 100644 mod_unified_push/rebar.config create mode 100644 mod_unified_push/src/mod_unified_push.erl create mode 100644 mod_unified_push/xmpp_spec.patch diff --git a/mod_unified_push/COPYING b/mod_unified_push/COPYING new file mode 100644 index 00000000..d159169d --- /dev/null +++ b/mod_unified_push/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/mod_unified_push/README.txt b/mod_unified_push/README.txt new file mode 100644 index 00000000..c8ff019d --- /dev/null +++ b/mod_unified_push/README.txt @@ -0,0 +1,11 @@ +# About + +Please refer to the `*.spec` file. + +# XMPP + +To interact with the custom IQ stanzas, this project includes code generated with the xmpp library [0] using the steps outlined in [1]. +See `xmpp_spec.patch` for details on the spec update. + +[0]: https://github.com/processone/xmpp +[1]: https://github.com/processone/xmpp/issues/9#issue-205855303 diff --git a/mod_unified_push/mod_unified_push.spec b/mod_unified_push/mod_unified_push.spec new file mode 100644 index 00000000..4210d2fb --- /dev/null +++ b/mod_unified_push/mod_unified_push.spec @@ -0,0 +1,5 @@ +category: "HTTP" +summary: "Conversations-compatible UnifiedPush push server implementation" +url: "git@codeberg.org:itd/mod_unified_push.git" +home: "https://codeberg.org/itd/mod_unified_push" +author: "itd@net.in.tum.de" diff --git a/mod_unified_push/rebar.config b/mod_unified_push/rebar.config new file mode 100644 index 00000000..16956cb9 --- /dev/null +++ b/mod_unified_push/rebar.config @@ -0,0 +1,6 @@ +{erl_opts, [debug_info]}. +{deps, []}. +{project_plugins, [ + erlfmt +]}. +{erlfmt, [{files, "{src,include,test}/*.{hrl,erl}"}, verbose, write]}. diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl new file mode 100644 index 00000000..1ae6bb2f --- /dev/null +++ b/mod_unified_push/src/mod_unified_push.erl @@ -0,0 +1,868 @@ +%%%---------------------------------------------------------------------- +%%% File : mod_unified_push.erl +%%% Author : itd +%%% Purpose : Handle UnifiedPush requests +%%% Created : 01 Feb 2025 by itd +%%% +%%% +%%% mod_unified_push, Copyright (C) 2025 itd +%%% +%%% 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_unified_push). +-behaviour(gen_mod). +-author('itd@net.in.tum.de'). + +%% Required by ?DEBUG macros +-include("logger.hrl"). + +%% Required by ?T macro +-include("translate.hrl"). + +%% Enables calls to process/2 +-include("ejabberd_http.hrl"). + +%% XMPP structs such as #iq +-include_lib("p1_xmpp/include/xmpp.hrl"). + +%% JOSE JWK structs such as #jose_jwk +-include_lib("jose/include/jose_jwk.hrl"). + +%% gen_mod API callbacks +-export([ + start/2, + stop/1, + process/2, + depends/2, + mod_opt_type/1, + mod_options/1, + mod_doc/0 +]). + +%% IQ handler +-export([iq_handler/1]). + +%% Exports for XMPP-generated code +-export([ + do_decode/4, + tags/0, + do_encode/2, + do_get_name/1, + do_get_ns/1, + pp/2, + records/0, + decode_unified_push_register/3, + decode_unified_push_register_attrs/4, + encode_unified_push_register/2, + decode_unified_push_register_attr_application/2, + decode_unified_push_register_attr_instance/2 +]). + +%% API subpaths +-define(ENDPOINT_PUSH, "push"). +-define(ENDPOINT_MESSAGE, "message"). + +%% Misc. string constants +-define(XMLNS_UNIFIED_PUSH, <<"http://gultsch.de/xmpp/drafts/unified-push">>). +-define(UP_EXPIRATION, <<"e">>). +-define(UP_OWNER, <<"o">>). +-define(UP_APPLICATION, <<"a">>). +-define(UP_INSTANCE, <<"i">>). + +%% Records/types from XMPP-generated code +-record(unified_push_register, { + application = <<>> :: binary(), + instance = <<>> :: binary() +}). + +-record(unified_push_registered, { + expiration :: erlang:timestamp(), + endpoint = <<>> :: binary() +}). + +-record(unified_push_push, { + application = <<>> :: binary(), + instance = <<>> :: binary(), + data = <<>> :: binary() +}). + +%% called by ejabberd_http +-spec process([binary()], http_request()) -> {integer(), [{binary(), binary()}], binary()}. +process(_LocalPath, #request{method = 'POST', data = <<>>}) -> + ?DEBUG("bad POST request for ~p: no data", [_LocalPath]), + {400, [], []}; +process([<>, MaybeJwtToken], #request{ + method = 'POST', data = Data, headers = Headers, host = Host +}) -> + Ttl = get_ttl(Headers), + Jwk = get_jwk(Host), + validate_request(Host, Jwk, MaybeJwtToken, Data, Ttl); +process([], #request{method = 'GET'}) -> + {200, + [ + {<<"Content-Type">>, <<"application/json">>} + ], + ["{\"unifiedpush\":{\"version\":1}}"]}; +process([<> | _], _Request) -> + %% RFC8030: 6.2. + {410, [], []}; +process(_LocalPath, _Request) -> + ?DEBUG("bad request for ~p: ~p", [_LocalPath, _Request]), + {400, [], []}. + +-spec get_ttl([{atom(), binary()}]) -> integer(). +get_ttl(Headers) -> + try string:to_integer(proplists:get_value(<<"Ttl">>, Headers, "1")) of + {T, <<>>} -> T; + {_, _} -> -1 + catch + _:_ -> + %% assume illegal behavior + -1 + end. + +-spec validate_request(binary(), binary(), binary(), binary(), integer()) -> + {integer(), [{binary(), binary()}], binary()}. +validate_request(_Host, _Jwk, _MaybeJwtToken, _Data, Ttl) when Ttl < 0 -> + {400, [], []}; +validate_request(Host, Jwk, MaybeJwtToken, Data, Ttl) when Data =/= <<"">> -> + ?DEBUG("verifying jwt validity", []), + try jose_jwt:verify(Jwk, MaybeJwtToken) of + {true, {jose_jwt, #{?UP_EXPIRATION := _ExpTest} = Fields}, Signature} -> + Now = erlang:system_time(second), + ?DEBUG("jwt verify at system timestamp ~p: ~p - ~p~n", [Now, Fields, Signature]), + case maps:find(?UP_EXPIRATION, Fields) of + error -> + ?DEBUG("rejecting jwt without exp(iry) field", []), + {401, [], []}; + {ok, Exp} -> + if + Exp > Now -> + ?DEBUG("valid request, forwarding notification: ~p", [Fields]), + forward_push_message(Host, MaybeJwtToken, Data, Ttl, Fields); + true -> + ?DEBUG("rejecting expired jwt: ~p > ~p", [Now, Exp]), + {401, [], []} + end + end; + {false, _, _} -> + ?DEBUG("jose_jwt:verify failed for token: ~p", [MaybeJwtToken]), + {401, [], []} + catch + A:B -> + ?DEBUG( + "jose_jwt:verify failed for JWK and token: ~p~n with error: ~p", + [{Jwk, MaybeJwtToken}, {A, B}] + ), + {401, [], []} + end; +validate_request(_Host, _Jwk, _MaybeJwtToken, _Data, _Ttl) -> + {400, [], []}. + +-spec forward_push_message(binary(), binary(), binary(), integer(), map()) -> + {integer(), [{binary(), binary()}], binary()}. +forward_push_message(Host, Jwt, Data, Ttl, #{ + ?UP_APPLICATION := Application, ?UP_INSTANCE := Instance, ?UP_OWNER := To +}) -> + IQ = #iq{ + type = set, + from = #jid{server = Host}, + to = jid:decode(To), + sub_els = [ + #unified_push_push{ + application = Application, + instance = Instance, + data = base64:encode(Data) + } + ] + }, + ejabberd_router:route_iq( + IQ, + fun(_Res) -> + ?DEBUG("~p: IQ callback: ~p", [?MODULE, _Res]), + ok + end, + Ttl + ), + UrlPrefix = misc:expand_keyword(<<"@HOST@">>, guess_url_prefix(any), Host), + %% XXX(RFC8030) indicate no support for receive acks / message storage + {201, + [ + {<<"TTL">>, <<"0">>}, + {<<"Location">>, <>} + ], + []}. + +-spec iq_handler(iq()) -> iq(). +iq_handler( + #iq{ + type = set, + to = #jid{server = Host}, + from = From, + sub_els = [#unified_push_register{application = Application, instance = Instance}] + } = IQ +) -> + ?DEBUG("processing unified push IQ: ~p", [IQ]), + %% TODO is non-monotonic time an issue here? + Offset = gen_mod:get_module_opt(Host, ?MODULE, expiration), + {OldMegaSecs, OldSecs, MicroSecs} = os:timestamp(), + MegaSecs = OldMegaSecs + (OldSecs + Offset) div 1000_000, + Secs = (OldSecs + Offset) rem 1000_000, + Expiration = {MegaSecs, Secs, MicroSecs}, + Jwk = get_jwk(Host), + %% TODO what are sensible JW[STK] for this scenario? + Jws = #{<<"alg">> => <<"HS256">>}, + Jwt = #{ + ?UP_EXPIRATION => MegaSecs * 1_000_000 + Secs, + ?UP_OWNER => jid:encode(From), + ?UP_APPLICATION => Application, + ?UP_INSTANCE => Instance + }, + Signed = jose_jwt:sign(Jwk, Jws, Jwt), + {#{}, CompactSigned} = jose_jws:compact(Signed), + UrlPrefix = misc:expand_keyword(<<"@HOST@">>, guess_url_prefix(any), Host), + xmpp:make_iq_result(IQ, #unified_push_registered{ + expiration = Expiration, + endpoint = <> + }); +iq_handler(#iq{lang = Lang} = IQ) -> + Txt = ?T("No module is handling this query"), + xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)). + +-spec start(binary(), gen_mod:opts()) -> ok. +start(Host, _Opts) -> + xmpp:register_codec(?MODULE), + gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?XMLNS_UNIFIED_PUSH, ?MODULE, iq_handler), + ok. + +-spec stop(binary()) -> ok. +stop(Host) -> + xmpp:unregister_codec(?MODULE), + gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?XMLNS_UNIFIED_PUSH), + ok. + +-spec depends(binary(), gen_mod:opts()) -> []. +depends(_Host, _Opts) -> + []. + +-spec get_jwk(binary()) -> any(). +get_jwk(Host) -> + JwkOptions = gen_mod:get_module_opt(Host, ?MODULE, jwk), + jose_jwk:from_map(JwkOptions). + +-spec mod_opt_type(atom()) -> econf:validator(). +mod_opt_type(expiration) -> + %% TODO is there an upper bound for the validity of the JWT token? + econf:int(0, 60 * 60 * 24); +mod_opt_type(jwk) -> + econf:map(econf:binary(), econf:either(econf:binary(), econf:int())). + +-spec mod_options(binary()) -> [{atom(), any()}]. +mod_options(_Host) -> + [ + {expiration, 60 * 15}, + {jwk, jose_jwk:to_map(jose_jwk:generate_key({oct, 128}))} + ]. + +%% Based on mod_host_meta's `get_auto_url` and `find_handler_port_path` functions +-spec guess_url_prefix(any | boolean()) -> undefined | binary(). +guess_url_prefix(Tls) -> + case + lists:filtermap( + fun + ( + {{Port, _, _}, ejabberd_http, #{tls := ThisTls, request_handlers := Handlers}} + ) when + (Tls == any) or (Tls == ThisTls) + -> + case lists:keyfind(?MODULE, 2, Handlers) of + false -> false; + {Path, ?MODULE} -> {true, {ThisTls, Port, Path}} + end; + (_) -> + false + end, + ets:tab2list(ejabberd_listener) + ) + of + [] -> + undefined; + [{ThisTls, Port, Path} | _] -> + Protocol = + case ThisTls of + false -> <<"http">>; + true -> <<"https">> + end, + <>))/binary>> + end. + +-spec mod_doc() -> map(). +mod_doc() -> + #{ + desc => ?T("UnifiedPush push server implementation."), + note => "experimental", + opts => [ + {expiration, #{ + value => ?T("Number"), + desc => + ?T( + "Every generated push URL will be valid only " + "for the specified interval, in seconds." + ) + }}, + {jwk, #{ + value => ?T("Map"), + desc => + ?T( + "Passed to `jose_jwk:from_map` to generate the " + "JSON Web Key (JWK) used when generating URLs." + ) + }} + ], + example => [ + "listen:", + " -", + " port: 5222", + " module: ejabberd_c2s", + " -", + " port: 5443", + " module: ejabberd_http", + " request_handlers:", + " /up: mod_unified_push", + "", + "modules:", + " mod_unified_push:", + " expiration: 120", + " jwk: {\"k\" => \"a4-...\",\"kty\" => \"oct\"}" + ] + }. + +%% FIXME The following lines add support for the custom IQ stanza. +%% If possible they should be moved to the xmpp package. +%% Created automatically by XML generator (fxml_gen.erl) +%% Source: xmpp_codec.spec + +do_decode( + <<"push">>, + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + El, + Opts +) -> + decode_unified_push_push( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + Opts, + El + ); +do_decode( + <<"registered">>, + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + El, + Opts +) -> + decode_unified_push_registered( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + Opts, + El + ); +do_decode( + <<"register">>, + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + El, + Opts +) -> + decode_unified_push_register( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + Opts, + El + ); +do_decode(Name, <<>>, _, _) -> + erlang:error({xmpp_codec, {missing_tag_xmlns, Name}}); +do_decode(Name, XMLNS, _, _) -> + erlang:error({xmpp_codec, {unknown_tag, Name, XMLNS}}). + +tags() -> + [ + {<<"push">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>}, + {<<"registered">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>}, + {<<"register">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>} + ]. + +do_encode( + {unified_push_register, _, _} = Register, + TopXMLNS +) -> + encode_unified_push_register(Register, TopXMLNS); +do_encode( + {unified_push_registered, _, _} = Registered, + TopXMLNS +) -> + encode_unified_push_registered(Registered, TopXMLNS); +do_encode( + {unified_push_push, _, _, _} = Push, + TopXMLNS +) -> + encode_unified_push_push(Push, TopXMLNS). + +do_get_name({unified_push_push, _, _, _}) -> <<"push">>; +do_get_name({unified_push_register, _, _}) -> <<"register">>; +do_get_name({unified_push_registered, _, _}) -> <<"registered">>. + +do_get_ns({unified_push_push, _, _, _}) -> + <<"http://gultsch.de/xmpp/drafts/unified-push">>; +do_get_ns({unified_push_register, _, _}) -> + <<"http://gultsch.de/xmpp/drafts/unified-push">>; +do_get_ns({unified_push_registered, _, _}) -> + <<"http://gultsch.de/xmpp/drafts/unified-push">>. + +pp(unified_push_register, 2) -> [application, instance]; +pp(unified_push_registered, 2) -> [expiration, endpoint]; +pp(unified_push_push, 3) -> [application, instance, data]; +pp(_, _) -> no. + +records() -> + [ + {unified_push_register, 2}, + {unified_push_registered, 2}, + {unified_push_push, 3} + ]. + +dec_utc(Val) -> xmpp_util:decode_timestamp(Val). + +enc_utc(Val) -> xmpp_util:encode_timestamp(Val). + +decode_unified_push_push( + __TopXMLNS, + __Opts, + {xmlel, <<"push">>, _attrs, _els} +) -> + Data = decode_unified_push_push_els( + __TopXMLNS, + __Opts, + _els, + <<>> + ), + {Application, Instance} = + decode_unified_push_push_attrs( + __TopXMLNS, + _attrs, + undefined, + undefined + ), + {unified_push_push, Application, Instance, Data}. + +decode_unified_push_push_els( + __TopXMLNS, + __Opts, + [], + Data +) -> + decode_unified_push_push_cdata(__TopXMLNS, Data); +decode_unified_push_push_els( + __TopXMLNS, + __Opts, + [{xmlcdata, _data} | _els], + Data +) -> + decode_unified_push_push_els( + __TopXMLNS, + __Opts, + _els, + <> + ); +decode_unified_push_push_els( + __TopXMLNS, + __Opts, + [_ | _els], + Data +) -> + decode_unified_push_push_els( + __TopXMLNS, + __Opts, + _els, + Data + ). + +decode_unified_push_push_attrs( + __TopXMLNS, + [{<<"application">>, _val} | _attrs], + _Application, + Instance +) -> + decode_unified_push_push_attrs( + __TopXMLNS, + _attrs, + _val, + Instance + ); +decode_unified_push_push_attrs( + __TopXMLNS, + [{<<"instance">>, _val} | _attrs], + Application, + _Instance +) -> + decode_unified_push_push_attrs( + __TopXMLNS, + _attrs, + Application, + _val + ); +decode_unified_push_push_attrs( + __TopXMLNS, + [_ | _attrs], + Application, + Instance +) -> + decode_unified_push_push_attrs( + __TopXMLNS, + _attrs, + Application, + Instance + ); +decode_unified_push_push_attrs( + __TopXMLNS, + [], + Application, + Instance +) -> + { + decode_unified_push_push_attr_application( + __TopXMLNS, + Application + ), + decode_unified_push_push_attr_instance( + __TopXMLNS, + Instance + ) + }. + +encode_unified_push_push( + {unified_push_push, Application, Instance, Data}, + __TopXMLNS +) -> + __NewTopXMLNS = + xmpp_codec:choose_top_xmlns( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + [], + __TopXMLNS + ), + _els = encode_unified_push_push_cdata(Data, []), + _attrs = + encode_unified_push_push_attr_instance( + Instance, + encode_unified_push_push_attr_application( + Application, + xmpp_codec:enc_xmlns_attrs( + __NewTopXMLNS, + __TopXMLNS + ) + ) + ), + {xmlel, <<"push">>, _attrs, _els}. + +decode_unified_push_push_attr_application( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"application">>, <<"push">>, __TopXMLNS}}); +decode_unified_push_push_attr_application( + __TopXMLNS, + _val +) -> + _val. + +encode_unified_push_push_attr_application(_val, _acc) -> + [{<<"application">>, _val} | _acc]. + +decode_unified_push_push_attr_instance( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"instance">>, <<"push">>, __TopXMLNS}}); +decode_unified_push_push_attr_instance( + __TopXMLNS, + _val +) -> + _val. + +encode_unified_push_push_attr_instance(_val, _acc) -> + [{<<"instance">>, _val} | _acc]. + +decode_unified_push_push_cdata(__TopXMLNS, <<>>) -> + <<>>; +decode_unified_push_push_cdata(__TopXMLNS, _val) -> + _val. + +encode_unified_push_push_cdata(<<>>, _acc) -> _acc; +encode_unified_push_push_cdata(_val, _acc) -> [{xmlcdata, _val} | _acc]. + +decode_unified_push_registered( + __TopXMLNS, + __Opts, + {xmlel, <<"registered">>, _attrs, _els} +) -> + {Expiration, Endpoint} = + decode_unified_push_registered_attrs( + __TopXMLNS, + _attrs, + undefined, + undefined + ), + {unified_push_registered, Expiration, Endpoint}. + +decode_unified_push_registered_attrs( + __TopXMLNS, + [{<<"expiration">>, _val} | _attrs], + _Expiration, + Endpoint +) -> + decode_unified_push_registered_attrs( + __TopXMLNS, + _attrs, + _val, + Endpoint + ); +decode_unified_push_registered_attrs( + __TopXMLNS, + [{<<"endpoint">>, _val} | _attrs], + Expiration, + _Endpoint +) -> + decode_unified_push_registered_attrs( + __TopXMLNS, + _attrs, + Expiration, + _val + ); +decode_unified_push_registered_attrs( + __TopXMLNS, + [_ | _attrs], + Expiration, + Endpoint +) -> + decode_unified_push_registered_attrs( + __TopXMLNS, + _attrs, + Expiration, + Endpoint + ); +decode_unified_push_registered_attrs( + __TopXMLNS, + [], + Expiration, + Endpoint +) -> + { + decode_unified_push_registered_attr_expiration( + __TopXMLNS, + Expiration + ), + decode_unified_push_registered_attr_endpoint( + __TopXMLNS, + Endpoint + ) + }. + +encode_unified_push_registered( + {unified_push_registered, Expiration, Endpoint}, + __TopXMLNS +) -> + __NewTopXMLNS = + xmpp_codec:choose_top_xmlns( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + [], + __TopXMLNS + ), + _els = [], + _attrs = + encode_unified_push_registered_attr_endpoint( + Endpoint, + encode_unified_push_registered_attr_expiration( + Expiration, + xmpp_codec:enc_xmlns_attrs( + __NewTopXMLNS, + __TopXMLNS + ) + ) + ), + {xmlel, <<"registered">>, _attrs, _els}. + +decode_unified_push_registered_attr_expiration( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"expiration">>, <<"registered">>, __TopXMLNS}}); +decode_unified_push_registered_attr_expiration( + __TopXMLNS, + _val +) -> + case catch dec_utc(_val) of + {'EXIT', _} -> + erlang:error( + {xmpp_codec, {bad_attr_value, <<"expiration">>, <<"registered">>, __TopXMLNS}} + ); + _res -> + _res + end. + +encode_unified_push_registered_attr_expiration( + _val, + _acc +) -> + [{<<"expiration">>, enc_utc(_val)} | _acc]. + +decode_unified_push_registered_attr_endpoint( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"endpoint">>, <<"registered">>, __TopXMLNS}}); +decode_unified_push_registered_attr_endpoint( + __TopXMLNS, + _val +) -> + _val. + +encode_unified_push_registered_attr_endpoint( + _val, + _acc +) -> + [{<<"endpoint">>, _val} | _acc]. + +decode_unified_push_register( + __TopXMLNS, + __Opts, + {xmlel, <<"register">>, _attrs, _els} +) -> + {Application, Instance} = + decode_unified_push_register_attrs( + __TopXMLNS, + _attrs, + undefined, + undefined + ), + {unified_push_register, Application, Instance}. + +decode_unified_push_register_attrs( + __TopXMLNS, + [{<<"application">>, _val} | _attrs], + _Application, + Instance +) -> + decode_unified_push_register_attrs( + __TopXMLNS, + _attrs, + _val, + Instance + ); +decode_unified_push_register_attrs( + __TopXMLNS, + [{<<"instance">>, _val} | _attrs], + Application, + _Instance +) -> + decode_unified_push_register_attrs( + __TopXMLNS, + _attrs, + Application, + _val + ); +decode_unified_push_register_attrs( + __TopXMLNS, + [_ | _attrs], + Application, + Instance +) -> + decode_unified_push_register_attrs( + __TopXMLNS, + _attrs, + Application, + Instance + ); +decode_unified_push_register_attrs( + __TopXMLNS, + [], + Application, + Instance +) -> + { + decode_unified_push_register_attr_application( + __TopXMLNS, + Application + ), + decode_unified_push_register_attr_instance( + __TopXMLNS, + Instance + ) + }. + +encode_unified_push_register( + {unified_push_register, Application, Instance}, + __TopXMLNS +) -> + __NewTopXMLNS = + xmpp_codec:choose_top_xmlns( + <<"http://gultsch.de/xmpp/drafts/unified-push">>, + [], + __TopXMLNS + ), + _els = [], + _attrs = + encode_unified_push_register_attr_instance( + Instance, + encode_unified_push_register_attr_application( + Application, + xmpp_codec:enc_xmlns_attrs( + __NewTopXMLNS, + __TopXMLNS + ) + ) + ), + {xmlel, <<"register">>, _attrs, _els}. + +decode_unified_push_register_attr_application( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"application">>, <<"register">>, __TopXMLNS}}); +decode_unified_push_register_attr_application( + __TopXMLNS, + _val +) -> + _val. + +encode_unified_push_register_attr_application( + _val, + _acc +) -> + [{<<"application">>, _val} | _acc]. + +decode_unified_push_register_attr_instance( + __TopXMLNS, + undefined +) -> + erlang:error({xmpp_codec, {missing_attr, <<"instance">>, <<"register">>, __TopXMLNS}}); +decode_unified_push_register_attr_instance( + __TopXMLNS, + _val +) -> + _val. + +encode_unified_push_register_attr_instance( + _val, + _acc +) -> + [{<<"instance">>, _val} | _acc]. diff --git a/mod_unified_push/xmpp_spec.patch b/mod_unified_push/xmpp_spec.patch new file mode 100644 index 00000000..05f790bd --- /dev/null +++ b/mod_unified_push/xmpp_spec.patch @@ -0,0 +1,50 @@ +diff --git a/specs/xmpp_codec.spec b/specs/xmpp_codec.spec +index 3223290..dcf7424 100644 +--- a/specs/xmpp_codec.spec ++++ b/specs/xmpp_codec.spec +@@ -452,6 +452,45 @@ + dec = {xmpp_lang, check, []}, + label = '$lang'}]}). + ++-xml(unified_push_register, ++ #elem{name = <<"register">>, ++ xmlns = <<"http://gultsch.de/xmpp/drafts/unified-push">>, ++ module = unifiedpush, ++ result = {unified_push_register, '$application', '$instance'}, ++ attrs = [#attr{name = <<"application">>, ++ required = true, ++ label = '$application'}, ++ #attr{name = <<"instance">>, ++ required = true, ++ label = '$instance'}]}). ++ ++-xml(unified_push_registered, ++ #elem{name = <<"registered">>, ++ xmlns = <<"http://gultsch.de/xmpp/drafts/unified-push">>, ++ module = unifiedpush, ++ result = {unified_push_registered, '$expiration', '$endpoint'}, ++ attrs = [#attr{name = <<"expiration">>, ++ required = true, ++ label = '$expiration', ++ dec = {dec_utc, []}, ++ enc = {enc_utc, []}}, ++ #attr{name = <<"endpoint">>, ++ required = true, ++ label = '$endpoint'}]}). ++ ++-xml(unified_push_push, ++ #elem{name = <<"push">>, ++ xmlns = <<"http://gultsch.de/xmpp/drafts/unified-push">>, ++ module = unifiedpush, ++ result = {unified_push_push, '$application', '$instance', '$data'}, ++ cdata = #cdata{label = '$data'}, ++ attrs = [#attr{name = <<"application">>, ++ required = true, ++ label = '$application'}, ++ #attr{name = <<"instance">>, ++ required = true, ++ label = '$instance'}]}). ++ + -xml(message_subject, + #elem{name = <<"subject">>, + xmlns = [<<"jabber:client">>, <<"jabber:server">>, From 1b9044295bf1ec9335cd7bcdb5690bc751266113 Mon Sep 17 00:00:00 2001 From: erebion Date: Thu, 5 Jun 2025 21:37:20 +0200 Subject: [PATCH 02/10] mod_unified_push: fix file extension --- mod_unified_push/{README.txt => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename mod_unified_push/{README.txt => README.md} (100%) diff --git a/mod_unified_push/README.txt b/mod_unified_push/README.md similarity index 100% rename from mod_unified_push/README.txt rename to mod_unified_push/README.md From 902c5be2fa381c180b397304dd9ae87aa6e01de5 Mon Sep 17 00:00:00 2001 From: erebion Date: Thu, 5 Jun 2025 22:11:46 +0200 Subject: [PATCH 03/10] mod_unified_push: Add config instructions to README --- mod_unified_push/README.md | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/mod_unified_push/README.md b/mod_unified_push/README.md index c8ff019d..df80498f 100644 --- a/mod_unified_push/README.md +++ b/mod_unified_push/README.md @@ -1,11 +1,33 @@ -# About +# mod_unified_push - Conversations-compatible UnifiedPush push server implementation -Please refer to the `*.spec` file. +- Author: itd (https://codeberg.org/itd) +- Copyright (C) 2025 itd -# XMPP +This module adds Unified Push support compatible with Conversations. To interact with the custom IQ stanzas, this project includes code generated with the xmpp library [0] using the steps outlined in [1]. See `xmpp_spec.patch` for details on the spec update. [0]: https://github.com/processone/xmpp [1]: https://github.com/processone/xmpp/issues/9#issue-205855303 + +## Configuration + +To use this module, load `mod_unified_push` in the `modules` section of the config file and add `/up: mod_unified_push` to a listen directive: + +``` + - + port: 5443 + ip: "::" + module: ejabberd_http + tls: true + protocol_options: 'TLS_OPTIONS' + request_handlers: + /api: mod_http_api + /bosh: mod_bosh + /upload: mod_http_upload + /ws: ejabberd_http_ws + /up: mod_unified_push +``` + +Once the config has been reloaded, the server can be added as a Unified Push distributor in the settings of Conversations, using the same domain used for the JID. From 94bc7319632153042e5648674715b171d631ce74 Mon Sep 17 00:00:00 2001 From: erebion Date: Thu, 5 Jun 2025 22:12:06 +0200 Subject: [PATCH 04/10] mod_unified_push: fix repo URL --- mod_unified_push/mod_unified_push.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod_unified_push/mod_unified_push.spec b/mod_unified_push/mod_unified_push.spec index 4210d2fb..94abe79a 100644 --- a/mod_unified_push/mod_unified_push.spec +++ b/mod_unified_push/mod_unified_push.spec @@ -1,5 +1,5 @@ category: "HTTP" summary: "Conversations-compatible UnifiedPush push server implementation" -url: "git@codeberg.org:itd/mod_unified_push.git" +url: "git@github.com:processone/ejabberd-contrib.git" home: "https://codeberg.org/itd/mod_unified_push" author: "itd@net.in.tum.de" From 1bad138e0f1fc5ad818fe7238de1755124d9b5c8 Mon Sep 17 00:00:00 2001 From: erebion Date: Mon, 9 Jun 2025 02:35:04 +0200 Subject: [PATCH 05/10] mod_unified_push: fix include path --- mod_unified_push/src/mod_unified_push.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl index 1ae6bb2f..d5998c9e 100644 --- a/mod_unified_push/src/mod_unified_push.erl +++ b/mod_unified_push/src/mod_unified_push.erl @@ -37,7 +37,7 @@ -include("ejabberd_http.hrl"). %% XMPP structs such as #iq --include_lib("p1_xmpp/include/xmpp.hrl"). +-include_lib("xmpp/include/xmpp.hrl"). %% JOSE JWK structs such as #jose_jwk -include_lib("jose/include/jose_jwk.hrl"). From 2e0594cc9fe4af5c2e6b523126c0d8e1f10b9ca5 Mon Sep 17 00:00:00 2001 From: erebion Date: Thu, 26 Jun 2025 23:10:39 +0200 Subject: [PATCH 06/10] mod_unified_push: remove superfluous code that is not part of other libraries that are included --- mod_unified_push/src/mod_unified_push.erl | 515 ---------------------- 1 file changed, 515 deletions(-) diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl index d5998c9e..d472cfe2 100644 --- a/mod_unified_push/src/mod_unified_push.erl +++ b/mod_unified_push/src/mod_unified_push.erl @@ -83,23 +83,6 @@ -define(UP_APPLICATION, <<"a">>). -define(UP_INSTANCE, <<"i">>). -%% Records/types from XMPP-generated code --record(unified_push_register, { - application = <<>> :: binary(), - instance = <<>> :: binary() -}). - --record(unified_push_registered, { - expiration :: erlang:timestamp(), - endpoint = <<>> :: binary() -}). - --record(unified_push_push, { - application = <<>> :: binary(), - instance = <<>> :: binary(), - data = <<>> :: binary() -}). - %% called by ejabberd_http -spec process([binary()], http_request()) -> {integer(), [{binary(), binary()}], binary()}. process(_LocalPath, #request{method = 'POST', data = <<>>}) -> @@ -368,501 +351,3 @@ do_decode( Opts, El ); -do_decode( - <<"registered">>, - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - El, - Opts -) -> - decode_unified_push_registered( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - Opts, - El - ); -do_decode( - <<"register">>, - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - El, - Opts -) -> - decode_unified_push_register( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - Opts, - El - ); -do_decode(Name, <<>>, _, _) -> - erlang:error({xmpp_codec, {missing_tag_xmlns, Name}}); -do_decode(Name, XMLNS, _, _) -> - erlang:error({xmpp_codec, {unknown_tag, Name, XMLNS}}). - -tags() -> - [ - {<<"push">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>}, - {<<"registered">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>}, - {<<"register">>, <<"http://gultsch.de/xmpp/drafts/unified-push">>} - ]. - -do_encode( - {unified_push_register, _, _} = Register, - TopXMLNS -) -> - encode_unified_push_register(Register, TopXMLNS); -do_encode( - {unified_push_registered, _, _} = Registered, - TopXMLNS -) -> - encode_unified_push_registered(Registered, TopXMLNS); -do_encode( - {unified_push_push, _, _, _} = Push, - TopXMLNS -) -> - encode_unified_push_push(Push, TopXMLNS). - -do_get_name({unified_push_push, _, _, _}) -> <<"push">>; -do_get_name({unified_push_register, _, _}) -> <<"register">>; -do_get_name({unified_push_registered, _, _}) -> <<"registered">>. - -do_get_ns({unified_push_push, _, _, _}) -> - <<"http://gultsch.de/xmpp/drafts/unified-push">>; -do_get_ns({unified_push_register, _, _}) -> - <<"http://gultsch.de/xmpp/drafts/unified-push">>; -do_get_ns({unified_push_registered, _, _}) -> - <<"http://gultsch.de/xmpp/drafts/unified-push">>. - -pp(unified_push_register, 2) -> [application, instance]; -pp(unified_push_registered, 2) -> [expiration, endpoint]; -pp(unified_push_push, 3) -> [application, instance, data]; -pp(_, _) -> no. - -records() -> - [ - {unified_push_register, 2}, - {unified_push_registered, 2}, - {unified_push_push, 3} - ]. - -dec_utc(Val) -> xmpp_util:decode_timestamp(Val). - -enc_utc(Val) -> xmpp_util:encode_timestamp(Val). - -decode_unified_push_push( - __TopXMLNS, - __Opts, - {xmlel, <<"push">>, _attrs, _els} -) -> - Data = decode_unified_push_push_els( - __TopXMLNS, - __Opts, - _els, - <<>> - ), - {Application, Instance} = - decode_unified_push_push_attrs( - __TopXMLNS, - _attrs, - undefined, - undefined - ), - {unified_push_push, Application, Instance, Data}. - -decode_unified_push_push_els( - __TopXMLNS, - __Opts, - [], - Data -) -> - decode_unified_push_push_cdata(__TopXMLNS, Data); -decode_unified_push_push_els( - __TopXMLNS, - __Opts, - [{xmlcdata, _data} | _els], - Data -) -> - decode_unified_push_push_els( - __TopXMLNS, - __Opts, - _els, - <> - ); -decode_unified_push_push_els( - __TopXMLNS, - __Opts, - [_ | _els], - Data -) -> - decode_unified_push_push_els( - __TopXMLNS, - __Opts, - _els, - Data - ). - -decode_unified_push_push_attrs( - __TopXMLNS, - [{<<"application">>, _val} | _attrs], - _Application, - Instance -) -> - decode_unified_push_push_attrs( - __TopXMLNS, - _attrs, - _val, - Instance - ); -decode_unified_push_push_attrs( - __TopXMLNS, - [{<<"instance">>, _val} | _attrs], - Application, - _Instance -) -> - decode_unified_push_push_attrs( - __TopXMLNS, - _attrs, - Application, - _val - ); -decode_unified_push_push_attrs( - __TopXMLNS, - [_ | _attrs], - Application, - Instance -) -> - decode_unified_push_push_attrs( - __TopXMLNS, - _attrs, - Application, - Instance - ); -decode_unified_push_push_attrs( - __TopXMLNS, - [], - Application, - Instance -) -> - { - decode_unified_push_push_attr_application( - __TopXMLNS, - Application - ), - decode_unified_push_push_attr_instance( - __TopXMLNS, - Instance - ) - }. - -encode_unified_push_push( - {unified_push_push, Application, Instance, Data}, - __TopXMLNS -) -> - __NewTopXMLNS = - xmpp_codec:choose_top_xmlns( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - [], - __TopXMLNS - ), - _els = encode_unified_push_push_cdata(Data, []), - _attrs = - encode_unified_push_push_attr_instance( - Instance, - encode_unified_push_push_attr_application( - Application, - xmpp_codec:enc_xmlns_attrs( - __NewTopXMLNS, - __TopXMLNS - ) - ) - ), - {xmlel, <<"push">>, _attrs, _els}. - -decode_unified_push_push_attr_application( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"application">>, <<"push">>, __TopXMLNS}}); -decode_unified_push_push_attr_application( - __TopXMLNS, - _val -) -> - _val. - -encode_unified_push_push_attr_application(_val, _acc) -> - [{<<"application">>, _val} | _acc]. - -decode_unified_push_push_attr_instance( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"instance">>, <<"push">>, __TopXMLNS}}); -decode_unified_push_push_attr_instance( - __TopXMLNS, - _val -) -> - _val. - -encode_unified_push_push_attr_instance(_val, _acc) -> - [{<<"instance">>, _val} | _acc]. - -decode_unified_push_push_cdata(__TopXMLNS, <<>>) -> - <<>>; -decode_unified_push_push_cdata(__TopXMLNS, _val) -> - _val. - -encode_unified_push_push_cdata(<<>>, _acc) -> _acc; -encode_unified_push_push_cdata(_val, _acc) -> [{xmlcdata, _val} | _acc]. - -decode_unified_push_registered( - __TopXMLNS, - __Opts, - {xmlel, <<"registered">>, _attrs, _els} -) -> - {Expiration, Endpoint} = - decode_unified_push_registered_attrs( - __TopXMLNS, - _attrs, - undefined, - undefined - ), - {unified_push_registered, Expiration, Endpoint}. - -decode_unified_push_registered_attrs( - __TopXMLNS, - [{<<"expiration">>, _val} | _attrs], - _Expiration, - Endpoint -) -> - decode_unified_push_registered_attrs( - __TopXMLNS, - _attrs, - _val, - Endpoint - ); -decode_unified_push_registered_attrs( - __TopXMLNS, - [{<<"endpoint">>, _val} | _attrs], - Expiration, - _Endpoint -) -> - decode_unified_push_registered_attrs( - __TopXMLNS, - _attrs, - Expiration, - _val - ); -decode_unified_push_registered_attrs( - __TopXMLNS, - [_ | _attrs], - Expiration, - Endpoint -) -> - decode_unified_push_registered_attrs( - __TopXMLNS, - _attrs, - Expiration, - Endpoint - ); -decode_unified_push_registered_attrs( - __TopXMLNS, - [], - Expiration, - Endpoint -) -> - { - decode_unified_push_registered_attr_expiration( - __TopXMLNS, - Expiration - ), - decode_unified_push_registered_attr_endpoint( - __TopXMLNS, - Endpoint - ) - }. - -encode_unified_push_registered( - {unified_push_registered, Expiration, Endpoint}, - __TopXMLNS -) -> - __NewTopXMLNS = - xmpp_codec:choose_top_xmlns( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - [], - __TopXMLNS - ), - _els = [], - _attrs = - encode_unified_push_registered_attr_endpoint( - Endpoint, - encode_unified_push_registered_attr_expiration( - Expiration, - xmpp_codec:enc_xmlns_attrs( - __NewTopXMLNS, - __TopXMLNS - ) - ) - ), - {xmlel, <<"registered">>, _attrs, _els}. - -decode_unified_push_registered_attr_expiration( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"expiration">>, <<"registered">>, __TopXMLNS}}); -decode_unified_push_registered_attr_expiration( - __TopXMLNS, - _val -) -> - case catch dec_utc(_val) of - {'EXIT', _} -> - erlang:error( - {xmpp_codec, {bad_attr_value, <<"expiration">>, <<"registered">>, __TopXMLNS}} - ); - _res -> - _res - end. - -encode_unified_push_registered_attr_expiration( - _val, - _acc -) -> - [{<<"expiration">>, enc_utc(_val)} | _acc]. - -decode_unified_push_registered_attr_endpoint( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"endpoint">>, <<"registered">>, __TopXMLNS}}); -decode_unified_push_registered_attr_endpoint( - __TopXMLNS, - _val -) -> - _val. - -encode_unified_push_registered_attr_endpoint( - _val, - _acc -) -> - [{<<"endpoint">>, _val} | _acc]. - -decode_unified_push_register( - __TopXMLNS, - __Opts, - {xmlel, <<"register">>, _attrs, _els} -) -> - {Application, Instance} = - decode_unified_push_register_attrs( - __TopXMLNS, - _attrs, - undefined, - undefined - ), - {unified_push_register, Application, Instance}. - -decode_unified_push_register_attrs( - __TopXMLNS, - [{<<"application">>, _val} | _attrs], - _Application, - Instance -) -> - decode_unified_push_register_attrs( - __TopXMLNS, - _attrs, - _val, - Instance - ); -decode_unified_push_register_attrs( - __TopXMLNS, - [{<<"instance">>, _val} | _attrs], - Application, - _Instance -) -> - decode_unified_push_register_attrs( - __TopXMLNS, - _attrs, - Application, - _val - ); -decode_unified_push_register_attrs( - __TopXMLNS, - [_ | _attrs], - Application, - Instance -) -> - decode_unified_push_register_attrs( - __TopXMLNS, - _attrs, - Application, - Instance - ); -decode_unified_push_register_attrs( - __TopXMLNS, - [], - Application, - Instance -) -> - { - decode_unified_push_register_attr_application( - __TopXMLNS, - Application - ), - decode_unified_push_register_attr_instance( - __TopXMLNS, - Instance - ) - }. - -encode_unified_push_register( - {unified_push_register, Application, Instance}, - __TopXMLNS -) -> - __NewTopXMLNS = - xmpp_codec:choose_top_xmlns( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - [], - __TopXMLNS - ), - _els = [], - _attrs = - encode_unified_push_register_attr_instance( - Instance, - encode_unified_push_register_attr_application( - Application, - xmpp_codec:enc_xmlns_attrs( - __NewTopXMLNS, - __TopXMLNS - ) - ) - ), - {xmlel, <<"register">>, _attrs, _els}. - -decode_unified_push_register_attr_application( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"application">>, <<"register">>, __TopXMLNS}}); -decode_unified_push_register_attr_application( - __TopXMLNS, - _val -) -> - _val. - -encode_unified_push_register_attr_application( - _val, - _acc -) -> - [{<<"application">>, _val} | _acc]. - -decode_unified_push_register_attr_instance( - __TopXMLNS, - undefined -) -> - erlang:error({xmpp_codec, {missing_attr, <<"instance">>, <<"register">>, __TopXMLNS}}); -decode_unified_push_register_attr_instance( - __TopXMLNS, - _val -) -> - _val. - -encode_unified_push_register_attr_instance( - _val, - _acc -) -> - [{<<"instance">>, _val} | _acc]. From c43c0861a842fb391b914b3046ec6130a81a04fc Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 30 Jun 2025 13:17:42 +0200 Subject: [PATCH 07/10] mod_unified_push: Remove more code that is nowadays implemented in the xmpp erlang library --- mod_unified_push/src/mod_unified_push.erl | 33 ----------------------- 1 file changed, 33 deletions(-) diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl index d472cfe2..5b69e51c 100644 --- a/mod_unified_push/src/mod_unified_push.erl +++ b/mod_unified_push/src/mod_unified_push.erl @@ -56,22 +56,6 @@ %% IQ handler -export([iq_handler/1]). -%% Exports for XMPP-generated code --export([ - do_decode/4, - tags/0, - do_encode/2, - do_get_name/1, - do_get_ns/1, - pp/2, - records/0, - decode_unified_push_register/3, - decode_unified_push_register_attrs/4, - encode_unified_push_register/2, - decode_unified_push_register_attr_application/2, - decode_unified_push_register_attr_instance/2 -]). - %% API subpaths -define(ENDPOINT_PUSH, "push"). -define(ENDPOINT_MESSAGE, "message"). @@ -334,20 +318,3 @@ mod_doc() -> " jwk: {\"k\" => \"a4-...\",\"kty\" => \"oct\"}" ] }. - -%% FIXME The following lines add support for the custom IQ stanza. -%% If possible they should be moved to the xmpp package. -%% Created automatically by XML generator (fxml_gen.erl) -%% Source: xmpp_codec.spec - -do_decode( - <<"push">>, - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - El, - Opts -) -> - decode_unified_push_push( - <<"http://gultsch.de/xmpp/drafts/unified-push">>, - Opts, - El - ); From a812762844e764193251ba5555dfa1f5806a254e Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 30 Jun 2025 13:11:56 +0200 Subject: [PATCH 08/10] mod_unified_push: Update specs and function calls to solve Dialyzer warnings --- mod_unified_push/src/mod_unified_push.erl | 25 +++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl index 5b69e51c..23c51feb 100644 --- a/mod_unified_push/src/mod_unified_push.erl +++ b/mod_unified_push/src/mod_unified_push.erl @@ -56,6 +56,8 @@ %% IQ handler -export([iq_handler/1]). +-type ttl_timeout() :: undefined | non_neg_integer(). + %% API subpaths -define(ENDPOINT_PUSH, "push"). -define(ENDPOINT_MESSAGE, "message"). @@ -68,7 +70,7 @@ -define(UP_INSTANCE, <<"i">>). %% called by ejabberd_http --spec process([binary()], http_request()) -> {integer(), [{binary(), binary()}], binary()}. +-spec process([binary()], http_request()) -> {integer(), [{binary(), binary()}], [binary()]}. process(_LocalPath, #request{method = 'POST', data = <<>>}) -> ?DEBUG("bad POST request for ~p: no data", [_LocalPath]), {400, [], []}; @@ -83,7 +85,7 @@ process([], #request{method = 'GET'}) -> [ {<<"Content-Type">>, <<"application/json">>} ], - ["{\"unifiedpush\":{\"version\":1}}"]}; + [<<"{\"unifiedpush\":{\"version\":1}}">>]}; process([<> | _], _Request) -> %% RFC8030: 6.2. {410, [], []}; @@ -91,20 +93,20 @@ process(_LocalPath, _Request) -> ?DEBUG("bad request for ~p: ~p", [_LocalPath, _Request]), {400, [], []}. --spec get_ttl([{atom(), binary()}]) -> integer(). +-spec get_ttl([{atom(), binary()}]) -> ttl_timeout(). get_ttl(Headers) -> try string:to_integer(proplists:get_value(<<"Ttl">>, Headers, "1")) of - {T, <<>>} -> T; - {_, _} -> -1 + {T, <<>>} when T >= 0 -> T; + {_, _} -> undefined catch _:_ -> %% assume illegal behavior - -1 + undefined end. --spec validate_request(binary(), binary(), binary(), binary(), integer()) -> - {integer(), [{binary(), binary()}], binary()}. -validate_request(_Host, _Jwk, _MaybeJwtToken, _Data, Ttl) when Ttl < 0 -> +-spec validate_request(binary(), any(), binary(), binary(), ttl_timeout()) -> + {integer(), [{binary(), binary()}], []}. +validate_request(_Host, _Jwk, _MaybeJwtToken, _Data, undefined) -> {400, [], []}; validate_request(Host, Jwk, MaybeJwtToken, Data, Ttl) when Data =/= <<"">> -> ?DEBUG("verifying jwt validity", []), @@ -140,8 +142,8 @@ validate_request(Host, Jwk, MaybeJwtToken, Data, Ttl) when Data =/= <<"">> -> validate_request(_Host, _Jwk, _MaybeJwtToken, _Data, _Ttl) -> {400, [], []}. --spec forward_push_message(binary(), binary(), binary(), integer(), map()) -> - {integer(), [{binary(), binary()}], binary()}. +-spec forward_push_message(binary(), binary(), binary(), ttl_timeout(), map()) -> + {integer(), [{binary(), binary()}], []}. forward_push_message(Host, Jwt, Data, Ttl, #{ ?UP_APPLICATION := Application, ?UP_INSTANCE := Instance, ?UP_OWNER := To }) -> @@ -163,6 +165,7 @@ forward_push_message(Host, Jwt, Data, Ttl, #{ ?DEBUG("~p: IQ callback: ~p", [?MODULE, _Res]), ok end, + undefined, Ttl ), UrlPrefix = misc:expand_keyword(<<"@HOST@">>, guess_url_prefix(any), Host), From 37be7360886a625bc1cabb72decb2a4e4d4d0809 Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 30 Jun 2025 13:21:51 +0200 Subject: [PATCH 09/10] mod_unified_push: Update documentation to mention suitable ejabberd version --- mod_unified_push/README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mod_unified_push/README.md b/mod_unified_push/README.md index df80498f..98976f4f 100644 --- a/mod_unified_push/README.md +++ b/mod_unified_push/README.md @@ -1,15 +1,16 @@ -# mod_unified_push - Conversations-compatible UnifiedPush push server implementation +# mod_unified_push - Conversations-compatible UnifiedPush push server implementation - Author: itd (https://codeberg.org/itd) - Copyright (C) 2025 itd This module adds Unified Push support compatible with Conversations. -To interact with the custom IQ stanzas, this project includes code generated with the xmpp library [0] using the steps outlined in [1]. -See `xmpp_spec.patch` for details on the spec update. - -[0]: https://github.com/processone/xmpp -[1]: https://github.com/processone/xmpp/issues/9#issue-205855303 +ejabberd newer than 25.04 is required, +as it requires an updated version of the +[xmpp erlang library](https://github.com/processone/xmpp) +that includes the improvements from the file `xmpp_spec.patch`. +Or you can +[apply the patch yourself](https://github.com/processone/xmpp/issues/9#issue-205855303) ## Configuration From 33ea9e056b085134e0fae7dfa74ce3ceec506ca9 Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 30 Jun 2025 13:51:51 +0200 Subject: [PATCH 10/10] mod_unified_push: Fix problem running "make options" --- mod_unified_push/src/mod_unified_push.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod_unified_push/src/mod_unified_push.erl b/mod_unified_push/src/mod_unified_push.erl index 23c51feb..478f3c97 100644 --- a/mod_unified_push/src/mod_unified_push.erl +++ b/mod_unified_push/src/mod_unified_push.erl @@ -237,7 +237,7 @@ get_jwk(Host) -> -spec mod_opt_type(atom()) -> econf:validator(). mod_opt_type(expiration) -> %% TODO is there an upper bound for the validity of the JWT token? - econf:int(0, 60 * 60 * 24); + econf:int(0, 86400); %% 60 * 60 * 24 mod_opt_type(jwk) -> econf:map(econf:binary(), econf:either(econf:binary(), econf:int())).