Major re-structure to sendxmpp, suitable for system-wide install now

This commit is contained in:
Travis Burtrum 2017-01-09 22:53:18 -05:00
parent 85c832d9be
commit 7512ee84a6
6 changed files with 51 additions and 102 deletions

View File

@ -1,7 +0,0 @@
[echobot]
jid = echo@example.net
password = azerty
[sendxmpp]
jid = username@example.net
password = qwerty

View File

@ -1,11 +1,9 @@
# A collection of XMPP utils
# sendxmpp-py
`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.
`sendxmpp` is the XMPP equivalent of sendmail. It is an alternative to the old sendxmpp written in Perl.
Dependencies:
- python 3
- dnspython
- sleekxmpp
@ -15,11 +13,13 @@ To install them on Ubuntu:
sudo apt-get install python3 python3-pip
sudo pip install dnspython sleekxmpp
Installation: just put the scripts wherever you want.
[Arch AUR package](https://aur.archlinux.org/packages/sendxmpp-py/)
Configuration: `cp CONFIG.example ~/.xmpputils` and edit `~/.xmpputils` with your XMPP credentials
Installation: just put the script wherever you want.
Configuration: `cp sendxmpp.cfg ~/.config/` and edit `~/.config/sendxmpp.cfg` with your XMPP credentials
Usage examples:
- `echo "This is a test" | sendxmpp.py user@host`
- `sendxmpp.py user@host <README.md`
- `echo "This is a test" | sendxmpp user@host`
- `sendxmpp user@host <README.md`

View File

@ -1,61 +0,0 @@
#!/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 <http://www.gnu.org/licenses/>.
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)

View File

@ -1,12 +0,0 @@
[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

View File

@ -114,14 +114,10 @@ file_or_jid = FirstOf(argparse.FileType('r'),
if __name__ == '__main__':
# do nothing for empty messages
msg = sys.stdin.read().strip()
if not msg:
exit(0)
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(description='Send a message with XMPP.')
p.add_argument('recipients', metavar='<file or JID>', nargs='+', type=file_or_jid, help='file format is one JID per line')
p.add_argument('-c', '--config', nargs='?', default=os.path.expanduser('~/.xmpputils'), type=argparse.FileType('r'))
p.add_argument('-c', '--config', nargs='?', help='path to config file. default: /etc/sendxmpp/sendxmpp.cfg and ~/.config/sendxmpp.cfg')
p.add_argument('-a', '--account', nargs='?', default='sendxmpp', help='account to use in config file. default: sendxmpp')
p.add_argument('-s', '--subject', nargs='?', default='', help='WARNING: subject never encrypted')
group = p.add_mutually_exclusive_group()
group.add_argument("-e", "--force-pgp", action="store_true", help='Force OpenPGP encryption for all recipients')
@ -131,6 +127,31 @@ if __name__ == '__main__':
except argparse.ArgumentError as e:
print(e)
exit(1)
# do nothing for empty messages
msg = sys.stdin.read().strip()
if not msg:
exit(0)
jid_conf = None
pass_conf = None
try:
conf = configparser.ConfigParser()
files_read = None
if global_args.config:
files_read = conf.read([global_args.config])
else:
files_read = conf.read([ '/etc/sendxmpp/sendxmpp.cfg', os.path.expanduser('~/.config/sendxmpp.cfg') ])
if len(files_read) == 0:
raise argparse.ArgumentTypeError("missing config files")
sendxmpp_conf = lambda key: conf.get(global_args.account, key)
jid_conf = sendxmpp_conf('jid')
pass_conf = sendxmpp_conf('password')
except Exception as e:
print(e)
exit(1)
r = []
for recipient in global_args.recipients:
if isinstance(recipient, sleekxmpp.basexmpp.JID):
@ -139,13 +160,9 @@ if __name__ == '__main__':
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)
sendxmpp_conf = lambda key: conf.get('sendxmpp', key)
jid = sleekxmpp.basexmpp.JID(sendxmpp_conf('jid'))
jid = sleekxmpp.basexmpp.JID(jid_conf)
jid.resource = jid.resource or 'sendxmpp.py'
xmpp = SendMsgBot(jid, sendxmpp_conf('password'), global_args.recipients, msg, global_args.subject, global_args.force_pgp, global_args.attempt_pgp)
xmpp = SendMsgBot(jid, pass_conf, global_args.recipients, msg, global_args.subject, global_args.force_pgp, global_args.attempt_pgp)
if xmpp.connect():
xmpp.process(block=True)

12
sendxmpp.cfg Normal file
View File

@ -0,0 +1,12 @@
# put as many accounts as you want here
# sendxmpp is the default unless you
# specify another
#[sendxmpp]
#jid = username@example.net
#password = qwerty
#[otheraccount]
#jid = other@example.net
#password = qwerty