-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc_messages.py
More file actions
181 lines (134 loc) · 5.56 KB
/
Copy pathfunc_messages.py
File metadata and controls
181 lines (134 loc) · 5.56 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import re
import time
import requests
def extract_message(message_text, settings):
"""
This takes a raw line from the DAPNETGateway logfile that we're
currently monitoring, and if it recognises is as one of the message
types we're looking for, extracts the pertinent data into a more
structured format via the message dictionary, which is returned.
"""
# Checking for Message
pattern = re.compile(settings["regex"]["message"])
matches = pattern.findall(message_text)
message = {}
message["type"] = "X"
if matches:
message["type"] = "M"
message["date"] = matches[0][0]
message["ric"] = matches[0][1]
message["text"] = matches[0][2]
message["subject"] = "Message via DAPNET"
message["body"] = message["text"]
message["tags"] = f'spiral_notepad, dapnet, message, ric-{message["ric"]}, v2'
message["priority"] = 3
# Checking for Error
pattern = re.compile(settings["regex"]["error"])
matches = pattern.findall(message_text)
if matches:
message["type"] = "E"
message["date"] = matches[0][0]
message["ric"] = "0000000"
message["text"] = matches[0][1]
message["subject"] = "ERROR FROM DAPNET"
message["body"] = f'[{message["date"]}] {message["text"]}'
message["tags"] = "rotating_light, dapnet, error, urgent, v2"
message["priority"] = 5
# Checking for Debug
pattern = re.compile(settings["regex"]["debug"])
matches = pattern.findall(message_text)
if matches:
message["type"] = "D"
message["date"] = matches[0][0]
message["ric"] = matches[0][1]
message["text"] = matches[0][2]
message["subject"] = "Debug via DAPNET"
message["body"] = message["text"]
message["tags"] = f'gear, dapnet, debug, ric-{message["ric"]}, v2'
message["priority"] = 1
return message
def send_to_ntfy(message, endpoint):
"""
Send the message, defined in the message dictionary, to the ntf.sh service
"""
ntfy_headers = {}
ntfy_headers["Tags"] = message["tags"]
ntfy_headers["Priority"] = str(message["priority"])
ntfy_headers["Title"] = message["subject"]
response = requests.post(endpoint, data=message["body"], headers=ntfy_headers)
response_text = str(response.status_code)
# Gonna wait a second so we don't end up with multiple messages racing each other
time.sleep(1)
print(f"[{response_text}]")
def info_message(infotype, settings, **kwargs):
"""
A special function for crafting informational messages.
These are static, and defined by the infotype parameter
"""
logfile = kwargs.get("optional", None)
message = {}
message["ric"] = "0000000"
message["text"] = ""
message["type"] = "I"
send = False
if infotype == "online":
message["subject"] = "DAPNET ntfy pager online"
message["body"] = "Monitoring for DAPNET calls"
message["tags"] = "wave, ntfy, online, v2"
message["priority"] = 3
send = True
elif infotype == "logfile_waiting":
message["subject"] = "Waiting for logfile"
message["body"] = logfile
message["tags"] = "hourglass, ntfy, logfile, v2"
message["priority"] = 1
send = True
elif infotype == "logfile_monitoring":
message["subject"] = "Monitoring logfile"
message["body"] = logfile
message["tags"] = "floppy_disk, ntfy, logfile, v2"
message["priority"] = 1
send = True
else:
print("invalid infotype")
if send:
send_message(message, settings)
def send_message(message, settings):
"""
This function prepares the message for sending, and applies
a series of rules to determine whether the message should be
sent out of not. If it deems that it should, it does!
"""
print(message)
for profile in settings["profiles"]:
is_addressable_to_target_profile = False
is_target_profile_enabled = False
# The rules here are a little complex, but to determine whether a message can potentially be sent to a given user:
# a. the type needs to contained within the enabled message types within the profile, AND any of:
# x. the target RIC is same as that in the profile, or
# y. the text contains the same callsign as that in the profile, or
# z. the message type is I (information) or E (error)
if message["type"] in settings["profiles"][profile]["messagetypes"]:
if (
message["ric"] == settings["profiles"][profile]["ric"]
or (
message["text"]
.upper()
.find(settings["profiles"][profile]["call"].upper())
>= 0
and settings["profiles"][profile]["alertoncall"]
and message["ric"] != "0000008"
)
or message["type"] in ["I", "E"]
):
is_addressable_to_target_profile = True
# The rule here is much simpler: is the profile that we want to send to actually enabled?
if settings["profiles"][profile]["enabled"]:
is_target_profile_enabled = True
# If the message is ready to be sent AND the target profile is enabled, then send the message!
if (
is_addressable_to_target_profile == True
and is_target_profile_enabled == True
):
print(settings["profiles"][profile])
send_to_ntfy(message, settings["profiles"][profile]["endpoint"])