From 77c24ec17213812119a81b9c5b81cc37bfa37830 Mon Sep 17 00:00:00 2001 From: Changaco Date: Sat, 18 Jan 2014 10:32:06 +0100 Subject: [PATCH] add echobot --- CONFIG.example | 8 +++++-- README | 10 -------- README.md | 12 ++++++++++ echobot.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ echobot.service | 12 ++++++++++ sendxmpp.py | 8 +++---- 6 files changed, 95 insertions(+), 16 deletions(-) delete mode 100644 README create mode 100644 README.md create mode 100755 echobot.py create mode 100644 echobot.service diff --git a/CONFIG.example b/CONFIG.example index 76a5a29..c3173b2 100644 --- a/CONFIG.example +++ b/CONFIG.example @@ -1,3 +1,7 @@ -[DEFAULT] -jid = foo@example.net +[echobot] +jid = echo@example.net +password = azerty + +[sendxmpp] +jid = username@example.net password = qwerty diff --git a/README b/README deleted file mode 100644 index 9a41e25..0000000 --- a/README +++ /dev/null @@ -1,10 +0,0 @@ -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/README.md b/README.md new file mode 100644 index 0000000..7ff1477 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# A collection of XMPP utils + +echobot.py is a bot that just echoes back whatever message you send to it. Useful for testing purposes. + +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 the scripts wherever you want. + +Configuration: see CONFIG.example, default config path is ~/.xmpputils diff --git a/echobot.py b/echobot.py new file mode 100755 index 0000000..fa00b14 --- /dev/null +++ b/echobot.py @@ -0,0 +1,61 @@ +#!/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 sleekxmpp + + +class EchoBot(sleekxmpp.ClientXMPP): + + def __init__(self, jid, password): + sleekxmpp.ClientXMPP.__init__(self, jid, password) + self.add_event_handler('message', self.message) + self.add_event_handler('session_start', self.start) + + def message(self, msg): + if msg['type'] in ('chat', 'normal'): + msg.reply(msg['body']).send() + + def start(self, event): + self.send_presence() + +if __name__ == '__main__': + + p = argparse.ArgumentParser() + p.add_argument('-c', '--config', nargs='?', default=os.path.expanduser('~/.xmpputils'), type=argparse.FileType('r')) + try: + global_args = p.parse_args() + except argparse.ArgumentError as e: + print(e) + exit(1) + + conf = configparser.ConfigParser() + conf.read_file(global_args.config) + echobot_conf = conf['echobot'] + + jid = sleekxmpp.basexmpp.JID(echobot_conf['jid']) + jid.resource = jid.resource or 'echobot' + xmpp = EchoBot(jid, echobot_conf['password']) + print('Connecting as', jid) + if xmpp.connect(): + xmpp.process(block=True) + else: + print('Unable to connect.') + exit(1) diff --git a/echobot.service b/echobot.service new file mode 100644 index 0000000..a225055 --- /dev/null +++ b/echobot.service @@ -0,0 +1,12 @@ +[Unit] +Description=XMPP Echo Bot +After=prosody.service + +[Service] +User=prosody +Group=prosody +Type=simple +ExecStart=/usr/bin/echobot.py + +[Install] +WantedBy=multi-user.target diff --git a/sendxmpp.py b/sendxmpp.py index 6f91a39..0b71e6e 100755 --- a/sendxmpp.py +++ b/sendxmpp.py @@ -59,7 +59,7 @@ 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='?', default=os.path.expanduser('~/.sendxmpp'), type=argparse.FileType('r')) + p.add_argument('-c', '--config', nargs='?', default=os.path.expanduser('~/.xmpputils'), type=argparse.FileType('r')) p.add_argument('-s', '--subject', nargs='?', default='') try: global_args = p.parse_args() @@ -76,11 +76,11 @@ if __name__ == '__main__': conf = configparser.ConfigParser() conf.read_file(global_args.config) - main_conf = conf['DEFAULT'] + sendxmpp_conf = lambda key: conf.get('sendxmpp', key) - jid = sleekxmpp.basexmpp.JID(main_conf['jid']) + jid = sleekxmpp.basexmpp.JID(sendxmpp_conf('jid')) jid.resource = jid.resource or 'sendxmpp.py' - xmpp = SendMsgBot(jid, main_conf['password'], global_args.recipients, sys.stdin.read(), global_args.subject) + xmpp = SendMsgBot(jid, sendxmpp_conf('password'), global_args.recipients, sys.stdin.read(), global_args.subject) if xmpp.connect(): xmpp.process(block=True)