#!/usr/bin/env python3 import os import sys import getopt import logging import slixmpp USAGE = """\ Usage: xmpp [-d] [-F FROM_JID] -m MESSAGE TO_JID... xmpp -h""" HELP = """ Options: -d run in DEBUG mode -m MESSAGE the text of the message to be sent -h, --help show this message FROM_JID the address used to send the message from TO_JID the addresses where to send the message to Send a one-off XMPP message. Examples: Send a message to eu@euandre.org: $ xmpp -m 'Hello, XMPP!' eu@euandre.org""" class SendMsgBot(slixmpp.ClientXMPP): def __init__(self, jid, password, on_start): slixmpp.ClientXMPP.__init__(self, jid, password) self.on_start = on_start self.add_event_handler("session_start", self.start) def start(self, event): self.on_start(self) self.disconnect(wait=True) def main(): logging.basicConfig(level=logging.INFO) from_ = "bot@euandre.org" message = "" for s in sys.argv: if s == "--": break elif s == "--help": print(USAGE) print(HELP) sys.exit() try: opts, args = getopt.getopt(sys.argv[1:], 'm:F:dh') except getopt.GetoptError as err: print(err, file=sys.stderr) print(USAGE, file=sys.stderr) sys.exit(2) for o, a in opts: if o == "-m": message = a elif o == "-F": from_ = a elif o == "-d": logging.basicConfig(level=logging.DEBUG) elif o == "-h": print(USAGE) print(HELP) sys.exit() else: assert False, "unhandled option" if message == "": print("Missing -m MESSAGE", file=sys.stderr) print(USAGE, file=sys.stderr) sys.exit(2) if args == []: print("Missing TO_JID", file=sys.stderr) print(USAGE, file=sys.stderr) sys.exit(2) passcmd = "pass show from_ | head -n1 | tr -d '\\n'" password = os.popen(passcmd).read() def on_start(self): for to in args: self.send_message(mto=to, mbody=message, mtype='chat') xmpp = SendMsgBot(from_, password, on_start) xmpp.connect() xmpp.process(forever=False) if __name__ == "__main__": main()