|
51 | 51 | announce_all_hosts_motd_update/1, |
52 | 52 | announce_motd_delete/1, |
53 | 53 | announce_all_hosts_motd_delete/1]). |
| 54 | +%% ejabberd_commands |
| 55 | +-export([announce_send_all/3, |
| 56 | + announce_send_online/3, |
| 57 | + get_stored_motd/1, |
| 58 | + announce_motd_set_online/3, |
| 59 | + announce_motd_update/3, |
| 60 | + announce_motd_delete_api/1, |
| 61 | + get_commands_spec/0]). |
| 62 | +%% WebAdmin |
| 63 | +-export([webadmin_menu/3, webadmin_page/3]). |
| 64 | + |
| 65 | +-import(ejabberd_web_admin, [make_command/4, make_command/2]). |
54 | 66 |
|
55 | 67 | -include("logger.hrl"). |
56 | 68 | -include_lib("xmpp/include/xmpp.hrl"). |
| 69 | +-include("ejabberd_commands.hrl"). |
| 70 | +-include("ejabberd_http.hrl"). |
| 71 | +-include("ejabberd_web_admin.hrl"). |
57 | 72 | -include("mod_announce.hrl"). |
58 | 73 | -include("translate.hrl"). |
59 | 74 |
|
@@ -82,9 +97,25 @@ tokenize(Node) -> str:tokens(Node, <<"/#">>). |
82 | 97 | %%; gen_mod callbacks |
83 | 98 |
|
84 | 99 | start(Host, Opts) -> |
| 100 | + case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of |
| 101 | + false -> |
| 102 | + ejabberd_commands:register_commands(?MODULE, get_commands_spec()); |
| 103 | + true -> |
| 104 | + ok |
| 105 | + end, |
| 106 | + ejabberd_hooks:add(webadmin_menu_host, Host, ?MODULE, webadmin_menu, 50), |
| 107 | + ejabberd_hooks:add(webadmin_page_host, Host, ?MODULE, webadmin_page, 50), |
85 | 108 | gen_mod:start_child(?MODULE, Host, Opts). |
86 | 109 |
|
87 | 110 | stop(Host) -> |
| 111 | + case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of |
| 112 | + false -> |
| 113 | + ejabberd_commands:unregister_commands(get_commands_spec()); |
| 114 | + true -> |
| 115 | + ok |
| 116 | + end, |
| 117 | + ejabberd_hooks:delete(webadmin_menu_host, Host, ?MODULE, webadmin_menu, 50), |
| 118 | + ejabberd_hooks:delete(webadmin_page_host, Host, ?MODULE, webadmin_page, 50), |
88 | 119 | gen_mod:stop_child(?MODULE, Host). |
89 | 120 |
|
90 | 121 | reload(Host, NewOpts, OldOpts) -> |
@@ -859,6 +890,148 @@ route_forbidden_error(Packet) -> |
859 | 890 | Err = xmpp:err_forbidden(?T("Access denied by service policy"), Lang), |
860 | 891 | ejabberd_router:route_error(Packet, Err). |
861 | 892 |
|
| 893 | + |
| 894 | +%%==================================================================== |
| 895 | +%%; Announcing via API commands |
| 896 | + |
| 897 | +%% @format-begin |
| 898 | + |
| 899 | +-spec get_commands_spec() -> [ejabberd_commands()]. |
| 900 | +get_commands_spec() -> |
| 901 | + [#ejabberd_commands{name = announce_send_all, |
| 902 | + tags = [announce], |
| 903 | + desc = "Send announcement to all users", |
| 904 | + longdesc = "If HOST is `all`, send to all hosts.", |
| 905 | + module = ?MODULE, |
| 906 | + function = announce_send_all, |
| 907 | + note = "added in 25.xx", |
| 908 | + args = [{host, binary}, {subject, binary}, {body, binary}], |
| 909 | + result = {res, rescode}}, |
| 910 | + #ejabberd_commands{name = announce_send_online, |
| 911 | + tags = [announce], |
| 912 | + desc = "Send announcement to online users", |
| 913 | + longdesc = "If HOST is `all`, send to all hosts.", |
| 914 | + module = ?MODULE, |
| 915 | + function = announce_send_online, |
| 916 | + note = "added in 25.xx", |
| 917 | + args = [{host, binary}, {subject, binary}, {body, binary}], |
| 918 | + result = {res, rescode}}, |
| 919 | + #ejabberd_commands{name = announce_motd_get, |
| 920 | + tags = [announce], |
| 921 | + desc = "Get Message Of The Day", |
| 922 | + module = ?MODULE, |
| 923 | + function = get_stored_motd, |
| 924 | + note = "added in 25.xx", |
| 925 | + args = [{host, binary}], |
| 926 | + result = {motd, {tuple, [{subject, string}, {body, string}]}}}, |
| 927 | + #ejabberd_commands{name = announce_motd_set_online, |
| 928 | + tags = [announce], |
| 929 | + desc = "Set Message Of The Day and send to online users", |
| 930 | + longdesc = "If HOST is `all`, send to all hosts.", |
| 931 | + module = ?MODULE, |
| 932 | + function = announce_motd_set_online, |
| 933 | + note = "added in 25.xx", |
| 934 | + args = [{host, binary}, {subject, binary}, {body, binary}], |
| 935 | + result = {res, rescode}}, |
| 936 | + #ejabberd_commands{name = announce_motd_update, |
| 937 | + tags = [announce], |
| 938 | + desc = "Update Message Of The Day", |
| 939 | + longdesc = "If HOST is `all`, send to all hosts.", |
| 940 | + module = ?MODULE, |
| 941 | + function = announce_motd_update, |
| 942 | + note = "added in 25.xx", |
| 943 | + args = [{host, binary}, {subject, binary}, {body, binary}], |
| 944 | + result = {res, rescode}}, |
| 945 | + #ejabberd_commands{name = announce_motd_delete, |
| 946 | + tags = [announce], |
| 947 | + desc = "Delete Message Of The Day", |
| 948 | + longdesc = "If HOST is `all`, send to all hosts.", |
| 949 | + module = ?MODULE, |
| 950 | + function = announce_motd_delete_api, |
| 951 | + note = "added in 25.xx", |
| 952 | + args = [{host, binary}], |
| 953 | + result = {res, rescode}}]. |
| 954 | + |
| 955 | +announce_send_all(<<"all">>, Subject, Body) -> |
| 956 | + Host = hd(ejabberd_option:hosts()), |
| 957 | + announce_all_hosts_all(make_packet(Host, Body, Subject)); |
| 958 | +announce_send_all(Host, Subject, Body) -> |
| 959 | + announce_all(make_packet(Host, Body, Subject)). |
| 960 | + |
| 961 | +announce_send_online(<<"all">>, Subject, Body) -> |
| 962 | + Host = hd(ejabberd_option:hosts()), |
| 963 | + announce_all_hosts_online(make_packet(Host, Body, Subject)); |
| 964 | +announce_send_online(Host, Subject, Body) -> |
| 965 | + announce_online(make_packet(Host, Body, Subject)). |
| 966 | + |
| 967 | +announce_motd_set_online(<<"all">>, Subject, Body) -> |
| 968 | + Host = hd(ejabberd_option:hosts()), |
| 969 | + announce_all_hosts_motd(make_packet(Host, Body, Subject)); |
| 970 | +announce_motd_set_online(Host, Subject, Body) -> |
| 971 | + announce_motd(make_packet(Host, Body, Subject)). |
| 972 | + |
| 973 | +announce_motd_update(<<"all">>, Subject, Body) -> |
| 974 | + Host = hd(ejabberd_option:hosts()), |
| 975 | + [{ok, _} | _] = announce_all_hosts_motd_update(make_packet(Host, Body, Subject)), |
| 976 | + ok; |
| 977 | +announce_motd_update(Host, Subject, Body) -> |
| 978 | + {ok, _} = announce_motd_update(make_packet(Host, Body, Subject)), |
| 979 | + ok. |
| 980 | + |
| 981 | +announce_motd_delete_api(<<"all">>) -> |
| 982 | + Host = hd(ejabberd_option:hosts()), |
| 983 | + announce_all_hosts_motd_delete(make_packet(Host)); |
| 984 | +announce_motd_delete_api(Host) -> |
| 985 | + announce_motd_delete(make_packet(Host)). |
| 986 | + |
| 987 | +make_packet(Host) -> |
| 988 | + From = To = jid:make(Host), |
| 989 | + #message{from = From, to = To}. |
| 990 | + |
| 991 | +make_packet(Host, Body, Subject) -> |
| 992 | + From = To = jid:make(Host), |
| 993 | + Body2 = binary:replace(Body, <<"\\n">>, <<"\n">>, [global]), |
| 994 | + #message{from = From, |
| 995 | + to = To, |
| 996 | + type = headline, |
| 997 | + body = xmpp:mk_text(Body2), |
| 998 | + subject = xmpp:mk_text(Subject)}. |
| 999 | + |
| 1000 | +%%==================================================================== |
| 1001 | +%%; Announcing via WebAdmin |
| 1002 | + |
| 1003 | +webadmin_menu(Acc, _Host, Lang) -> |
| 1004 | + [{<<"announce">>, translate:translate(Lang, ?T("Announcements"))} | Acc]. |
| 1005 | + |
| 1006 | +webadmin_page(_, |
| 1007 | + Host, |
| 1008 | + #request{us = _US, |
| 1009 | + path = [<<"announce">>], |
| 1010 | + lang = Lang} = |
| 1011 | + R) -> |
| 1012 | + PageTitle = translate:translate(Lang, ?T("Announcements")), |
| 1013 | + Head = ?H1GL(PageTitle, <<"modules/#mod_announce">>, <<"mod_announce">>), |
| 1014 | + Ann = [?X(<<"hr">>), |
| 1015 | + ?XE(<<"blockquote">>, |
| 1016 | + [make_command(announce_send_all, R, [{<<"host">>, Host}], []), |
| 1017 | + make_command(announce_send_online, R, [{<<"host">>, Host}], [])])], |
| 1018 | + SetMotd = |
| 1019 | + [make_command(announce_motd_set_online, R, [{<<"host">>, Host}], []), |
| 1020 | + make_command(announce_motd_update, R, [{<<"host">>, Host}], []), |
| 1021 | + make_command(announce_motd_delete, R, [{<<"host">>, Host}], [{style, danger}])], |
| 1022 | + GetMotd = [make_command(announce_motd_get, R, [{<<"host">>, Host}], [])], |
| 1023 | + Motd = |
| 1024 | + [?X(<<"hr">>), |
| 1025 | + ?XAC(<<"h2">>, [{<<"id">>, <<"motd">>}], <<"Message Of The Day">>), |
| 1026 | + ?XE(<<"blockquote">>, GetMotd ++ SetMotd)], |
| 1027 | + {stop, Head ++ Ann ++ Motd}; |
| 1028 | +webadmin_page(Acc, _, _) -> |
| 1029 | + Acc. |
| 1030 | +%% @format-end |
| 1031 | + |
| 1032 | +%%==================================================================== |
| 1033 | +%%; Cache management |
| 1034 | + |
862 | 1035 | -spec init_cache(module(), binary(), gen_mod:opts()) -> ok. |
863 | 1036 | init_cache(Mod, Host, Opts) -> |
864 | 1037 | case use_cache(Mod, Host) of |
@@ -946,6 +1119,8 @@ mod_doc() -> |
946 | 1119 | "Configured users can perform these actions with an XMPP " |
947 | 1120 | "client either using Ad-Hoc Commands or sending messages " |
948 | 1121 | "to specific JIDs."), "", |
| 1122 | + ?T("Equivalent API commands are also available. " |
| 1123 | + "You can use ' \\n ' in the message body to write a newline."), "", |
949 | 1124 | ?T("NOTE: This module can be resource intensive on large " |
950 | 1125 | "deployments as it may broadcast a lot of messages. This module " |
951 | 1126 | "should be disabled for instances of ejabberd with hundreds of " |
|
0 commit comments