2019-10-09 12:36:25 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-06-28 07:56:48 +00:00
|
|
|
# Copyright 2019-2021 Benedikt Ziemons
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
# this software and associated documentation files (the "Software"), to deal in
|
|
|
|
# the Software without restriction, including without limitation the rights to
|
|
|
|
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
# the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2019-10-09 12:36:25 +00:00
|
|
|
|
|
|
|
import argparse
|
2021-06-28 07:56:48 +00:00
|
|
|
import asyncio
|
2019-10-09 12:36:25 +00:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
import nio
|
|
|
|
|
2021-10-28 20:58:37 +00:00
|
|
|
from .common import (
|
|
|
|
client_login,
|
|
|
|
send_message,
|
|
|
|
resolve_room,
|
|
|
|
MatrixException,
|
|
|
|
load_configuration,
|
|
|
|
)
|
2019-10-09 12:36:25 +00:00
|
|
|
|
|
|
|
# Not going to care for specifics like the underscore.
|
|
|
|
# Generally match !anything:example.com with unicode support.
|
2020-09-27 15:56:36 +00:00
|
|
|
room_pattern = re.compile(r"^!\w+:[\w\-.]+$")
|
2019-10-09 12:36:25 +00:00
|
|
|
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
async def main():
|
2019-10-09 12:36:25 +00:00
|
|
|
"""
|
|
|
|
config.yml Example:
|
|
|
|
|
|
|
|
matrix:
|
|
|
|
server: https://matrix.org
|
|
|
|
username: ...
|
|
|
|
password: "..."
|
|
|
|
"""
|
2021-10-28 20:58:37 +00:00
|
|
|
cfg = load_configuration()
|
2019-10-09 12:36:25 +00:00
|
|
|
|
2020-09-27 15:56:36 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Notify a matrix channel.")
|
|
|
|
parser.add_argument(
|
|
|
|
"-c", "--channel", required=True, help="the channel to send the message to"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--type",
|
|
|
|
required=False,
|
|
|
|
help="the msgtype",
|
|
|
|
choices=("m.text", "m.notice"),
|
|
|
|
default="m.text",
|
|
|
|
)
|
|
|
|
parser.add_argument("text", help="the text message to send to the channel")
|
|
|
|
parser.add_argument(
|
|
|
|
"html", nargs="?", help="the html message to send to the channel"
|
|
|
|
)
|
2019-10-09 12:36:25 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if room_pattern.fullmatch(args.channel) is None:
|
|
|
|
print("ERROR: Couldn't parse channel as a matrix channel", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
client = await client_login(cfg)
|
|
|
|
try:
|
|
|
|
room_id = await resolve_room(client=client, room=args.channel)
|
|
|
|
response = await client.join(room_id=room_id)
|
|
|
|
if isinstance(response, nio.ErrorResponse):
|
2021-10-28 18:48:06 +00:00
|
|
|
raise MatrixException(response)
|
2021-06-28 07:56:48 +00:00
|
|
|
|
|
|
|
if "html" in args:
|
2021-10-28 18:48:06 +00:00
|
|
|
response = await send_message(
|
|
|
|
client=client,
|
|
|
|
room_id=room_id,
|
|
|
|
text=(args.text or ""),
|
|
|
|
msgtype=args.type,
|
|
|
|
html=args.html,
|
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
else:
|
2021-10-28 18:48:06 +00:00
|
|
|
response = await send_message(
|
|
|
|
client=client, room_id=room_id, text=args.text, msgtype=args.type
|
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
print("Message sent.", file=sys.stderr, flush=True)
|
|
|
|
finally:
|
|
|
|
await client.close()
|
|
|
|
print(response.event_id)
|
2019-10-09 12:36:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-28 07:56:48 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(main())
|