hptoad/gsend.py

56 lines
1.4 KiB
Python
Raw Normal View History

2017-02-18 18:35:12 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import sleekxmpp
opts = {
"jid": "botname@example.com",
"resource": "resource",
"password": "password",
"connect": "xmpp.example.org:5222",
}
def on_session_start(event):
client.get_roster()
# client.send_presence()
body = "\n".join(sys.argv[1:]).strip()
try:
if body:
client.send_message(mto=opts["jid"], mbody=body, mtype="chat")
except Exception as e:
print("%s: %s" % (type(e).__name__, str(e)))
finally:
client.disconnect(wait=True)
sys.exit(0)
if __name__ == "__main__":
if sys.version_info.major < 3:
sleekxmpp.util.misc_ops.setdefaultencoding("utf-8")
if len(sys.argv) <= 1:
print("At least one argument is required.")
sys.exit(1)
client = sleekxmpp.ClientXMPP("%s/%s" % (opts["jid"], opts["resource"]),
opts["password"])
if opts["connect"]:
connect = opts["connect"].split(":", 1)
if len(connect) != 2 or not connect[1].isdigit():
print("Connection server format is invalid, should be " +
"example.org:5222")
sys.exit(1)
else:
connect = ()
if client.connect(connect):
client.add_event_handler("session_start", on_session_start)
client.process(block=True)
else:
print("Could not connect to server, or password mismatch!")
sys.exit(1)