mirror of
https://github.com/a-bad-dev/irc2bash.git
synced 2026-06-09 00:51:32 +00:00
Rewrite most of the script (especially the message parser)
This commit is contained in:
parent
fea04ee12b
commit
586cf1ad90
1 changed files with 127 additions and 57 deletions
144
main.py
144
main.py
|
|
@ -1,69 +1,139 @@
|
||||||
import os, socket, _thread, time
|
import os, socket, _thread, time
|
||||||
|
|
||||||
def main() -> None:
|
class IRC2BASH:
|
||||||
ip = ""
|
ip: str = "" # up to the user to set these values
|
||||||
port = 6667
|
port: int = 0
|
||||||
name = "username_here"
|
name: str = ""
|
||||||
nick = "nick_here"
|
nick: str = ""
|
||||||
global chan
|
chan: str = ""
|
||||||
chan = "#channel_name_here"
|
|
||||||
|
|
||||||
|
send = lambda string: IRC2BASH.sock.send(f"{string}\r\n".encode("utf-8"))
|
||||||
|
server = (ip, port)
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
sock.connect((ip, port))
|
def main() -> None:
|
||||||
|
IRC2BASH.sock.connect(IRC2BASH.server)
|
||||||
|
|
||||||
time.sleep(5) # wait for server to connect
|
time.sleep(5) # wait for server to connect
|
||||||
|
|
||||||
sock.send(f"USER {name} * * :{nick}\r\n".encode("utf-8"))
|
IRC2BASH.send(f"USER {IRC2BASH.nick} * * :{IRC2BASH.name}")
|
||||||
time.sleep(1) # wait for server to process
|
time.sleep(1) # wait for server to process
|
||||||
|
|
||||||
sock.send(f"NICK {nick}\r\n".encode("utf-8"))
|
IRC2BASH.send(f"NICK {IRC2BASH.nick}")
|
||||||
time.sleep(5) # wait for server to process
|
time.sleep(5) # wait for server to process
|
||||||
|
|
||||||
sock.send(f"JOIN {chan}\r\n".encode("utf-8"))
|
IRC2BASH.send(f"JOIN {IRC2BASH.chan}")
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
_thread.start_new_thread(receive_messages, (sock,))
|
_thread.start_new_thread(IRC2BASH.receive_messages, (IRC2BASH.sock,))
|
||||||
while True:
|
|
||||||
|
while True: # ping the server every minute so the connection stays alive
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
sock.send(f"PING :{ip}\r\n".encode("utf-8"))
|
IRC2BASH.send(f"PING :{IRC2BASH.ip}")
|
||||||
def receive_messages(sock) -> None:
|
|
||||||
|
def receive_messages(sock) -> None:
|
||||||
while True:
|
while True:
|
||||||
data = sock.recv(1024)
|
data = sock.recv(1024)
|
||||||
if not data:
|
if not data:
|
||||||
break
|
break
|
||||||
|
|
||||||
response = data.decode("utf-8")
|
response = data.decode("utf-8")
|
||||||
print(response)
|
response = response.strip()
|
||||||
if "PRIVMSG" in response and chan in response:
|
response = response[1:]
|
||||||
|
response = response.split(" ")
|
||||||
|
|
||||||
|
#
|
||||||
|
# attempt to find a username in part #1
|
||||||
|
#
|
||||||
|
|
||||||
try:
|
try:
|
||||||
command = response.split(" ", 3)[3][1:]
|
username = response[0]
|
||||||
|
username = username.split("!")
|
||||||
|
username = username[0]
|
||||||
|
except Exception:
|
||||||
|
username = ""
|
||||||
|
|
||||||
|
#
|
||||||
|
# attempt to find the channel name
|
||||||
|
#
|
||||||
|
|
||||||
|
try:
|
||||||
|
if response[1] == "PRIVMSG" and response[2] == IRC2BASH.chan: # correct channel
|
||||||
|
channel = response[2]
|
||||||
|
else:
|
||||||
|
channel = ""
|
||||||
|
except Exception:
|
||||||
|
channel = ""
|
||||||
|
|
||||||
|
#
|
||||||
|
# attempt to parse the actual message
|
||||||
|
#
|
||||||
|
|
||||||
|
try:
|
||||||
|
message = response[3:]
|
||||||
|
parsed_message = ""
|
||||||
|
for word in message:
|
||||||
|
parsed_message = f"{parsed_message} {word}"
|
||||||
|
|
||||||
|
message = parsed_message
|
||||||
|
parsed_message = None
|
||||||
|
|
||||||
|
message = message.strip()
|
||||||
|
message = message[1:]
|
||||||
|
except Exception:
|
||||||
|
message = ""
|
||||||
|
|
||||||
|
#
|
||||||
|
# see if the message is a command
|
||||||
|
#
|
||||||
|
|
||||||
|
try:
|
||||||
|
if message.startswith("$ "):
|
||||||
|
command = message[2:]
|
||||||
|
else:
|
||||||
|
command = ""
|
||||||
except Exception:
|
except Exception:
|
||||||
command = ""
|
command = ""
|
||||||
|
|
||||||
if command.startswith("$"):
|
#
|
||||||
try:
|
# we now (probably) have the command :D, time to run it
|
||||||
command = command.split(" ", 1)[1]
|
#
|
||||||
except Exception:
|
|
||||||
|
if username != "" and channel != "" and message != "" and command != "":
|
||||||
|
|
||||||
|
#
|
||||||
|
# escape quotation marks and backslashes
|
||||||
|
#
|
||||||
|
|
||||||
|
command_list = []
|
||||||
|
for letter in command:
|
||||||
|
match letter:
|
||||||
|
case "\\":
|
||||||
|
command_list.append("\\\\")
|
||||||
|
|
||||||
|
case "\"":
|
||||||
|
command_list.append("\\\"")
|
||||||
|
|
||||||
|
case _:
|
||||||
|
command_list.append(letter)
|
||||||
|
|
||||||
command = ""
|
command = ""
|
||||||
|
for letter in command_list:
|
||||||
|
command = f"{command}{letter}"
|
||||||
|
|
||||||
|
command_list = None
|
||||||
|
|
||||||
|
print(f"{channel}: <{username}> {command}")
|
||||||
|
os.system(f"/bin/bash -c \"{command}\" >/tmp/.command 2>&1")
|
||||||
|
|
||||||
command = f"/bin/bash -c \"{command.strip()}\" >/tmp/.command 2>&1"
|
|
||||||
os.system(command)
|
|
||||||
with open("/tmp/.command", "r") as f:
|
with open("/tmp/.command", "r") as f:
|
||||||
output = f.read()
|
output = f.read()
|
||||||
|
|
||||||
|
output = output.strip().split("\n")
|
||||||
print(output)
|
print(output)
|
||||||
for line in output.split("\n"):
|
for line in output:
|
||||||
sock.send(f"PRIVMSG {chan} :{line}\r\n".encode("utf-8"))
|
IRC2BASH.send(f"PRIVMSG {IRC2BASH.chan} :{line}")
|
||||||
time.sleep(0.5)
|
time.sleep(0.75)
|
||||||
|
|
||||||
else:
|
|
||||||
print("message not ok")
|
|
||||||
|
|
||||||
def send_ping(sock, server) -> None:
|
|
||||||
while True:
|
|
||||||
time.sleep(60)
|
|
||||||
sock.send(f"PING :{server}\r\n".encode("utf-8"))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
IRC2BASH.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue