-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghbot_regex.py
executable file
·95 lines (64 loc) · 2.48 KB
/
ghbot_regex.py
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
#! /usr/bin/python3
# by FvH, released under Apache License v2.0
# either install 'python3-paho-mqtt' or 'pip3 install paho-mqtt'
import paho.mqtt.client as mqtt
import sqlite3
import threading
import time
import socket
import sys
mqtt_server = 'mqtt.vm.nurd.space'
topic_prefix = 'GHBot/'
channels = ['nurdbottest', 'test', 'nurdsbofh', 'nurds']
history = dict()
for channel in channels:
history[channel] = []
def on_message(client, userdata, message):
global history
try:
text = message.payload.decode('utf-8')
topic = message.topic[len(topic_prefix):]
parts = topic.split('/')
channel = parts[2] if len(parts) >= 3 else 'nurds'
nick = parts[3] if len(parts) >= 4 else 'jemoeder'
if parts[-1] == 'topic':
return
if channel in channels or (len(channel) >= 1 and channel[0] == '\\'):
if len(text) == 0:
return
org_in = text
while True:
re_idx = text.find('s/')
if re_idx == -1:
break
sep_1 = text.find('/', re_idx + 2)
if sep_1 == -1:
break
sep_2 = text.find('/', sep_1 + 2)
if sep_2 == -1:
break
search_item = text[re_idx + 2:sep_1]
if len(search_item) == 0:
break
response_topic = f'{topic_prefix}to/irc/{channel}/notice'
for line in reversed(history[channel]):
if search_item in line:
new_line = line.replace(search_item, text[sep_1 + 1:sep_2])
print(f'old: {org_in}')
print(f'new: {new_line}')
client.publish(response_topic, f'> {new_line}')
break
# one replacement is good enough for now
break
history[channel].append(org_in)
while len(history[channel]) > 10:
del history[channel][0]
except Exception as e:
print(f'{e}, line number: {e.__traceback__.tb_lineno}')
def on_connect(client, userdata, flags, rc):
client.subscribe(f'{topic_prefix}from/irc/#')
client = mqtt.Client(f'{socket.gethostname()}_{sys.argv[0]}', clean_session=False)
client.on_message = on_message
client.on_connect = on_connect
client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
client.loop_forever()