Created
April 20, 2024 21:13
-
-
Save raek/87a6ef86065f85029597d7377238b677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import weechat as w | |
morse = { | |
"0" : "-----", | |
"1" : ".----", | |
"2" : "..---", | |
"3" : "...--", | |
"4" : "....-", | |
"5" : ".....", | |
"6" : "-....", | |
"7" : "--...", | |
"8" : "---..", | |
"9" : "----.", | |
"a" : ".-", | |
"b" : "-...", | |
"c" : "-.-.", | |
"d" : "-..", | |
"e" : ".", | |
"f" : "..-.", | |
"g" : "--.", | |
"h" : "....", | |
"i" : "..", | |
"j" : ".---", | |
"k" : "-.-", | |
"l" : ".-..", | |
"m" : "--", | |
"n" : "-.", | |
"o" : "---", | |
"p" : ".--.", | |
"q" : "--.-", | |
"r" : ".-.", | |
"s" : "...", | |
"t" : "-", | |
"u" : "..-", | |
"v" : "...-", | |
"w" : ".--", | |
"x" : "-..-", | |
"y" : "-.--", | |
"z" : "--..", | |
"=" : "-...-", | |
"/" : "-..-.", | |
"+" : ".-.-.", | |
"-" : "-....-", | |
"." : ".-.-.-", | |
"," : "--..--", | |
"?" : "..--..", | |
":" : "---...", | |
"!" : "-.-.--", | |
"'" : ".----.", | |
";" : "-.-.-.", | |
"&" : ".-...", | |
"@" : ".--.-.", | |
"ä" : ".-.-", | |
"ö" : "---.", | |
"ü" : "..--", | |
"ch" : "----", | |
'"' : ".-..-.", | |
"(" : "-.--.", | |
")" : "-.--.-", | |
"<sk>" : "...-.-", | |
"<bk>" : "-...-.-", | |
} | |
reverse_morse = {value: key for key, value in morse.items()} | |
INCOMING_MESSAGE = "" | |
def main(): | |
w.register("weemopp", "raek", "1.0", "MIT", "Morsecode Over Packet Protocol for Weechat", "", "") | |
w.hook_process_hashtable("nc -u -l -p 7373", {"buffer_flush": "1"}, 0, "udp_cb", "") | |
def udp_cb(data, command, return_code, out, err): | |
global INCOMING_MESSAGE | |
if return_code == w.WEECHAT_HOOK_PROCESS_ERROR: | |
w.prnt("", f"weemopp: error when running nc: {command}") | |
return w.WEECHAT_RC_OK | |
if return_code >= 0: | |
w.prnt("", f"weemopp: nc exited with {return_code}") | |
if not out: | |
return w.WEECHAT_RC_OK | |
if not isinstance(out, bytes): | |
out = out.encode("ascii") | |
text = decode_mopp(out) | |
w.prnt("", f"weemopp: received packet: {text}") | |
if INCOMING_MESSAGE: | |
INCOMING_MESSAGE += " " | |
INCOMING_MESSAGE += text | |
while "<bk>" in INCOMING_MESSAGE: | |
first, rest = INCOMING_MESSAGE.split("<bk>", maxsplit=1) | |
w.prnt("", f"weemopp: got line: {first}") | |
w.command("", first) | |
INCOMING_MESSAGE = rest | |
return w.WEECHAT_RC_OK | |
def decode_mopp(bs): | |
pairs = [] | |
for b in bs: | |
bits = format(b, "0>8b") | |
for i in range(0, 8, 2): | |
pairs.append(bits[i:i+2]) | |
if len(pairs) < 7 or pairs[0] != "01": | |
raise ValueError(f"Invalid MOPP packet: {bs!r}") | |
pairs = pairs[7:] | |
current_char = "" | |
current_word = "" | |
output = "" | |
for pair in pairs: | |
if pair == "01": # dit | |
current_char += "." | |
elif pair == "10": # dah | |
current_char += "-" | |
elif pair == "00": # end of character | |
if current_char: | |
current_word += reverse_morse.get(current_char, "*") | |
current_char = "" | |
elif pair == "11": # end of word | |
if current_word: | |
if output: | |
output += " " | |
output += current_word | |
current_word = "" | |
if current_char: | |
current_word += reverse_morse.get(current_char, "*") | |
current_char = "" | |
if current_word: | |
output += current_word | |
current_word = "" | |
return output | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment