#!/usr/bin/env swipl /* xmpp_send_message.pl Author: Gimenez, Christian. Copyright (C) 2026 Gimenez, Christian 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 3 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, see . 2026-05-03 */ :- module(xmpp_send_message, []). /** xmpp_send_message: Send a message through XMPP. @author Christian Gimenez @license GPLv3 */ :- use_module(library(xmpp/xmpp)). :- use_module(library(xmpp/xmpp_core)). :- use_module(library(xmpp/xmpp_auth)). :- initialization(main, main). %! message_sent % % This predicate is asserted when the bot has just sent the mesage and % it has disconnected. :- dynamic message_sent/0. opt_type(d, debug, boolean). opt_type(s, subject, atom). %% opt_type(j, jid, atom). %% opt_type(t, jid_to, atom). opt_help(debug, 'Enable debug mode'). opt_help(subject, 'Add a subject to the message'). %% opt_help(jid, 'Jabber ID to connect to'). %% opt_help(jid_to, 'Jabber ID to send a "hello"'). msg_opts('', []) :- !. msg_opts( Subject, [ content([element(subject, [], [Subject])]) ]). send_msg(JIDTo, Msg, Subject, _) :- debug(xmppbot, 'Bot: Sending message to ~s', [JIDTo]), msg_opts(Subject, MsgOpts), send_message(JIDTo, Msg, MsgOpts), debug(xmppbot, 'Bot: Sent!', []), sleep(1), debug(xmppbot, 'Bot: Disconnecting...', []), disconnect. %! end_program(+Args: list) % % Hook handler used after disconnecting. % Tells the bot to end the program. % % @param Args Ignored. % % @see message_sent/0 end_program(_) :- asserta(message_sent). %! wait_until_end % % Repeat until message_sent/0 is asserted. wait_until_send :- repeat, sleep(1), message_sent. connect_and_send(JID, Password) :- debug(xmppbot, 'Bot: Connecting to ~s...', [JID]), connect(JID, Password). enable_debug(Options) :- option(debug(true), Options, false), debug(xmpp), % debug(xmpp_auth), debug(xmpp_ping), debug(xmppbot). get_envdata(JID, Password) :- getenv('BOTJID', JID), getenv('BOTPASS', Password). get_envdata(_, _) :- throw(error(no_envvar_set, context(get_envdata/2, 'Please, set BOTJID and BOTPASS environment variables'))). main(ArgV) :- argv_options(ArgV, [JIDTo, Message], Options), ignore(enable_debug(Options)), option(subject(Subject), Options, ''), get_envdata(JID, Password), % format('~s password?', [JID]), nl, % read_line_to_string(current_input, PasswordS), % atom_string(Password, PasswordS), add_hook(after_connect, xmpp_send_message:send_msg(JIDTo, Message, Subject)), add_hook(after_disconnect, xmpp_send_message:end_program), connect_and_send(JID, Password), wait_until_send.