add echobot

This commit is contained in:
Changaco 2014-01-18 10:32:06 +01:00
parent 243173b160
commit 77c24ec172
6 changed files with 95 additions and 16 deletions

View File

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

10
README
View File

@ -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

12
README.md Normal file
View File

@ -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

61
echobot.py Executable file
View File

@ -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 <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)

12
echobot.service Normal file
View File

@ -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

View File

@ -59,7 +59,7 @@ if __name__ == '__main__':
p = argparse.ArgumentParser() p = argparse.ArgumentParser()
p.add_argument('recipients', metavar='<file or JID>', nargs='+', type=file_or_jid, help='file format is one JID per line') 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('~/.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='') p.add_argument('-s', '--subject', nargs='?', default='')
try: try:
global_args = p.parse_args() global_args = p.parse_args()
@ -76,11 +76,11 @@ if __name__ == '__main__':
conf = configparser.ConfigParser() conf = configparser.ConfigParser()
conf.read_file(global_args.config) 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' 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(): if xmpp.connect():
xmpp.process(block=True) xmpp.process(block=True)