1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#!/usr/bin/env python3
import os
import sys
import getopt
import logging
import slixmpp
USAGE = """\
Usage:
xmpp [-d] [-F FROM_JID] -m MESSAGE TO_JID...
xmpp -h"""
HELP = """
Options:
-d run in DEBUG mode
-m MESSAGE the text of the message to be sent
-h, --help show this message
FROM_JID the address used to send the message from
TO_JID the addresses where to send the message to
Send a one-off XMPP message.
Examples:
Send a message to eu@euandre.org:
$ xmpp -m 'Hello, XMPP!' eu@euandre.org"""
class SendMsgBot(slixmpp.ClientXMPP):
def __init__(self, jid, password, on_start):
slixmpp.ClientXMPP.__init__(self, jid, password)
self.on_start = on_start
self.add_event_handler("session_start", self.start)
def start(self, event):
self.on_start(self)
self.disconnect(wait=True)
def main():
logging.basicConfig(level=logging.INFO)
from_ = "bot@euandre.org"
message = ""
for s in sys.argv:
if s == "--":
break
elif s == "--help":
print(USAGE)
print(HELP)
sys.exit()
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:F:dh')
except getopt.GetoptError as err:
print(err, file=sys.stderr)
print(USAGE, file=sys.stderr)
sys.exit(2)
for o, a in opts:
if o == "-m":
message = a
elif o == "-F":
from_ = a
elif o == "-d":
logging.basicConfig(level=logging.DEBUG)
elif o == "-h":
print(USAGE)
print(HELP)
sys.exit()
else:
assert False, "unhandled option"
if message == "":
print("Missing -m MESSAGE", file=sys.stderr)
print(USAGE, file=sys.stderr)
sys.exit(2)
if args == []:
print("Missing TO_JID", file=sys.stderr)
print(USAGE, file=sys.stderr)
sys.exit(2)
passcmd = "pass show from_ | head -n1 | tr -d '\\n'"
password = os.popen(passcmd).read()
def on_start(self):
for to in args:
self.send_message(mto=to, mbody=message, mtype='chat')
xmpp = SendMsgBot(from_, password, on_start)
xmpp.connect()
xmpp.process(forever=False)
if __name__ == "__main__":
main()
|