mirror of
https://github.com/moparisthebest/sendxmpp-py
synced 2024-11-17 22:45:03 -05:00
initial commit
This commit is contained in:
parent
85f11eff2b
commit
b4a5a71cc0
3
CONFIG.example
Normal file
3
CONFIG.example
Normal file
@ -0,0 +1,3 @@
|
||||
[DEFAULT]
|
||||
jid = foo@example.net
|
||||
password = qwerty
|
10
README
Normal file
10
README
Normal file
@ -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
|
89
sendxmpp.py
Executable file
89
sendxmpp.py
Executable file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
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='<file or JID>', 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)
|
Loading…
Reference in New Issue
Block a user