sendxmpp-py/sendxmpp.py

90 lines
3.0 KiB
Python
Raw Normal View History

2012-04-15 12:09:55 -04:00
#!/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')
2014-01-18 04:32:06 -05:00
p.add_argument('-c', '--config', nargs='?', default=os.path.expanduser('~/.xmpputils'), type=argparse.FileType('r'))
p.add_argument('-s', '--subject', nargs='?', default='')
2012-04-15 12:09:55 -04:00
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)
2014-01-18 04:32:06 -05:00
sendxmpp_conf = lambda key: conf.get('sendxmpp', key)
2012-04-15 12:09:55 -04:00
2014-01-18 04:32:06 -05:00
jid = sleekxmpp.basexmpp.JID(sendxmpp_conf('jid'))
2012-04-15 12:09:55 -04:00
jid.resource = jid.resource or 'sendxmpp.py'
2014-01-18 04:32:06 -05:00
xmpp = SendMsgBot(jid, sendxmpp_conf('password'), global_args.recipients, sys.stdin.read(), global_args.subject)
2012-04-15 12:09:55 -04:00
if xmpp.connect():
xmpp.process(block=True)
else:
print('Unable to connect.')
exit(1)