matrix-shoutbox

git clone https://git.tarina.org/matrix-shoutbox
Log | Files | Refs

commit 77812321d04e890012586e0198806989ea69593d
parent 940ca2145a0293c15061809df26c3e2ac3e2e6a2
Author: rob <rob@tarina.org>
Date:   Tue, 16 Apr 2024 01:07:45 +0300

yes

Diffstat:
Mclient.py | 4++--
Ashoutbox-recv.py | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashoutbox-server.py | 43+++++++++++++++++++++++++++++++++++++++++++
Atcpclient.py | 21+++++++++++++++++++++
4 files changed, 137 insertions(+), 2 deletions(-)

diff --git a/client.py b/client.py @@ -2,7 +2,7 @@ import asyncio import sys async def tcp_echo_client(message, loop): - reader, writer = await asyncio.open_connection('127.0.0.1', 8888, loop=loop) + reader, writer = await asyncio.open_connection('127.0.0.1', 8558) print('Send: %r' % message) writer.write(message.encode()) data = await reader.read(1000) @@ -12,5 +12,5 @@ async def tcp_echo_client(message, loop): message = sys.argv[1] loop = asyncio.get_event_loop() -loop.run_until_complete(tcp_echo_client(message, loop)) +loop.run_until_complete(tcp_echo_client(message,loop)) loop.close() diff --git a/shoutbox-recv.py b/shoutbox-recv.py @@ -0,0 +1,71 @@ +import asyncio +from nio import (AsyncClient, RoomMessageText) +from nio import (AsyncClient, RoomMessageImage) +import config +from datetime import datetime, timedelta + +msg_to_file = '/srv/www/radiorymd.com/public_html/static/msg.html' +open(msg_to_file, 'w').close() + +datestamp = '' + +async def message_cb(room, event): + global datestamp + todaysdate = datetime.now().strftime('%A %d %B') + if room.display_name in config.display_name: + print("{}: {}".format(room.user_name(event.sender), event.body)) + #msg_file = open(msg_to_file, 'a') + #msg_file.write(room.user_name(event.sender) + ': ' + event.body + '\n') + timestamp = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%H:%M") + datestamp_new = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%A %d %B") + time_html = '<span id="time">' + timestamp + '</span>' + user_html = '<span id="user"> ' + room.user_name(event.sender) + ' </span>' + msg_html = '<span id="msg"> ' + event.body + ' </span>' + if datestamp != '' and datestamp != todaysdate and datestamp != datestamp_new: + new_msg = '<p>' + time_html + user_html + msg_html + '</p>\n'+'<br><h4>'+datestamp+'</h4>\n' + else: + new_msg = '<p>' + time_html + user_html + msg_html + '</p>\n' + datestamp = datestamp_new + with open(msg_to_file, 'r') as original: + data = original.readlines()[0:100] + data = ''.join(data) + #print(data) + with open(msg_to_file, 'w') as modified: + modified.write(new_msg + "\n" + data) + +async def image_cb(room, event): + global datestamp + todaysdate = datetime.now().strftime('%A %d %B') + if room.display_name in config.display_name: + media_mxc = event.url + media_url = media_mxc + media_http = await client.mxc_to_http(media_url, config.matrixserver) + print(media_http) + now = datetime.now() + timestamp = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%H:%M") + datestamp_new = (datetime.utcfromtimestamp(event.server_timestamp/1000) + timedelta(hours=3)).strftime("%A %d %B") + time_html = '<span id="time">' + timestamp + '</span>' + user_html = '<span id="user"> ' + room.user_name(event.sender) + ' </span>' + msg_html = '<span id="msg"> ' + event.body + ' </span>' + if datestamp != '' and datestamp != todaysdate and datestamp != datestamp_new: + new_msg = '<p>' + time_html + user_html + ' ' + '</p>\n' + '<img src="' + str(media_http) + '" style="border-radius:5px;"/>\n'+'<br><h4>'+datestamp+'</h4>\n' + else: + new_msg = '<p>' + time_html + user_html + ' ' + '</p>\n' + '<img src="' + str(media_http) + '" style="border-radius:5px;"/>\n' + datestamp = datestamp_new + with open(msg_to_file, 'r') as original: + data = original.readlines()[0:100] + data = ''.join(data) + #print(data) + with open(msg_to_file, 'w') as modified: + modified.write(new_msg + "\n" + data) + +async def main(): + global client, datestamp + client = AsyncClient(config.matrixserver, config.username) + client.add_event_callback(message_cb, RoomMessageText) + client.add_event_callback(image_cb, RoomMessageImage) + await client.login(config.password) + await client.sync_forever(timeout=30000) + +asyncio.get_event_loop().run_until_complete(main()) + diff --git a/shoutbox-server.py b/shoutbox-server.py @@ -0,0 +1,43 @@ +import asyncio +from nio import AsyncClient +import config + +async def handle_echo(reader, writer): + client = AsyncClient(config.matrixserver, config.username) + await client.login(config.password) + + data = await reader.read(512) + message = data.decode() + addr = writer.get_extra_info('peername') + print("Received %r from %r" % (message, addr)) + writer.write(data) + await writer.drain() + print("Send: %r" % message) + print("Close the client socket") + writer.close() + print('sending msg to matrix') + await client.room_send( + room_id=config.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": message + } + ) + await client.close() + +loop = asyncio.get_event_loop() +coro = asyncio.start_server(handle_echo, '127.0.0.1', 8558) +server = loop.run_until_complete(coro) + +# Serve requests until Ctrl+C is pressed +print('Serving on {}'.format(server.sockets[0].getsockname())) +try: + loop.run_forever() +except KeyboardInterrupt: + pass + +# Close the server +server.close() +loop.run_until_complete(server.wait_closed()) +loop.close() diff --git a/tcpclient.py b/tcpclient.py @@ -0,0 +1,21 @@ +import socket + +HOST = "127.0.0.1" +PORT = 8558 + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # cf. tcpserver.py +print('Socket created') + +s.connect((HOST,PORT)) +print('Connected to', HOST, ':', PORT) + +data = "" # Initialize data + +while(data != 'q'): + msg = input("Say: ") + s.sendall(msg.encode()) + data = s.recv(1024).decode() + print('Echo:', repr(data)) + +s.close() +print('Connection closed')