From b4a5a71cc0a576e8ae137e12377356069f7c1b37 Mon Sep 17 00:00:00 2001 From: Changaco Date: Sun, 15 Apr 2012 18:09:55 +0200 Subject: [PATCH] initial commit --- CONFIG.example | 3 ++ README | 10 ++++++ sendxmpp.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 CONFIG.example create mode 100644 README create mode 100755 sendxmpp.py diff --git a/CONFIG.example b/CONFIG.example new file mode 100644 index 0000000..76a5a29 --- /dev/null +++ b/CONFIG.example @@ -0,0 +1,3 @@ +[DEFAULT] +jid = foo@example.net +password = qwerty diff --git a/README b/README new file mode 100644 index 0000000..9a41e25 --- /dev/null +++ b/README @@ -0,0 +1,10 @@ +sendxmpp.py is the XMPP equivalent of sendmail. + +It is an alternative to the old sendxmpp written in Perl. + +Dependencies: +- sleekxmpp (python3 version) + +Installation: just put sendxmpp.py wherever you want. + +Configuration: see CONFIG.example, default config path is ~/.sendxmpp diff --git a/sendxmpp.py b/sendxmpp.py new file mode 100755 index 0000000..bdca4ab --- /dev/null +++ b/sendxmpp.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# 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 . + + +import argparse +import configparser +import os.path +import sys + +import sleekxmpp + + +class SendMsgBot(sleekxmpp.ClientXMPP): + + def __init__(self, jid, password, recipients, message, subject): + sleekxmpp.ClientXMPP.__init__(self, jid, password) + self.recipients = recipients + self.msg = message + self.subject = subject + self.add_event_handler('session_start', self.start) + + def start(self, event): + self.send_presence() + for recipient in self.recipients: + self.send_message(mto=recipient, + msubject=self.subject, + mbody=self.msg, + mtype='normal') + self.disconnect(wait=True) + +def FirstOf(*types, error='argument "{}" is not valid'): + def f(s): + for t in types: + try: + return t(s) + except: + pass + raise argparse.ArgumentTypeError(error.format(s)) + return f + +file_or_jid = FirstOf(argparse.FileType('r'), + sleekxmpp.basexmpp.JID, + error='"{}" is neither a file nor a valid JID') + +if __name__ == '__main__': + + p = argparse.ArgumentParser() + p.add_argument('recipients', metavar='', nargs='+', type=file_or_jid, help='file format is one JID per line') + p.add_argument('-c', '--config', nargs=1, default=os.path.expanduser('~/.sendxmpp'), type=argparse.FileType('r')) + p.add_argument('-s', '--subject', nargs=1, default='') + try: + global_args = p.parse_args() + except argparse.ArgumentError as e: + print(e) + exit(1) + r = [] + for recipient in global_args.recipients: + if isinstance(recipient, sleekxmpp.basexmpp.JID): + r.append(recipient) + else: + r.extend(map(sleekxmpp.basexmpp.JID, filter(None, recipient.read().split('\n')))) + global_args.recipients = r + + conf = configparser.ConfigParser() + conf.read_file(global_args.config) + main_conf = conf['DEFAULT'] + + jid = sleekxmpp.basexmpp.JID(main_conf['jid']) + jid.resource = jid.resource or 'sendxmpp.py' + xmpp = SendMsgBot(jid, main_conf['password'], global_args.recipients, sys.stdin.read(), global_args.subject) + + if xmpp.connect(): + xmpp.process(block=True) + else: + print('Unable to connect.') + exit(1)