From 66599bc6bdcb289d6c9d5eef0ab577d2d6dc4408 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 2 Nov 2023 19:17:12 +0300 Subject: [PATCH] fix --- C/irc.c | 8 +++---- C/irc.h | 4 ---- LICENSE | 9 ++++++++ Python/README.md | 1 + Python/irc.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ Python/main.py | 28 ++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 LICENSE create mode 100644 Python/README.md create mode 100644 Python/irc.py create mode 100644 Python/main.py diff --git a/C/irc.c b/C/irc.c index 736c86f..f14d3f3 100644 --- a/C/irc.c +++ b/C/irc.c @@ -1,8 +1,8 @@ #include "irc.h" unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){ - struct hostent *ghbn = gethostbyname(ip); - if (!ghbn) + struct hostent *hp = gethostbyname(ip); + if (!hp) return IRCC_ERROR; //Only ipv4 @@ -10,7 +10,7 @@ unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int p memset(&client_str, 0, sizeof(client_str)); client_str.sin_family = AF_INET; client_str.sin_port = htons(port); - client_str.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)ghbn->h_addr)); + client_str.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)hp->h_addr)); irc->socket = socket(AF_INET, SOCK_STREAM, 0); if (irc->socket < 0) @@ -79,7 +79,7 @@ unsigned int IRCC_recv(IRCC_client *irc){ } else { - //puts(irc->raw); + puts(irc->raw); //Check end of motd if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD")) diff --git a/C/irc.h b/C/irc.h index d376ae1..bf4cb4c 100644 --- a/C/irc.h +++ b/C/irc.h @@ -8,10 +8,6 @@ motd parsing */ -#if defined(_WIN32) || defined(_WIN64) - #error "Only unix" -#endif - #include #include #include diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Python/README.md b/Python/README.md new file mode 100644 index 0000000..955c919 --- /dev/null +++ b/Python/README.md @@ -0,0 +1 @@ +Delete me diff --git a/Python/irc.py b/Python/irc.py new file mode 100644 index 0000000..7c1070d --- /dev/null +++ b/Python/irc.py @@ -0,0 +1,55 @@ +import socket +import time +import sys + +class IRC: + def __init__(self, channel, code): + self.channel = channel + self.sock = socket.socket() + self.code = code + + self.sender = {"ip":"", "nickname":""} + self.data = {"type":""} + + def connect(self, host, nick): + self.sock.connect(host) + + self.sock.send(f"USER {nick} 0 0 {nick}\r\n".encode(self.code)) + self.sock.send(f"NICK {nick}\r\n".encode(self.code)) + time.sleep(7) + self.sock.send(f"JOIN {self.channel}\r\n".encode(self.code)) + + def recv(self, size): + try: + data = self.sock.recv(size).decode() + except UnicodeDecodeError: + return None + + if len(data) == 0: + self.sock.close() + raise BrokenPipeError("Disconnected") + + if "PING" in data.split(":")[0]: + self.sock.send(f"PONG {data.split()[-1]}\r\n".encode()) + return data + + def parser(self, data): + data = data.split(" ", 3) + self.sender["ip"] = data[0].split("@", 1)[-1] + self.sender["nickname"] = data[0].split("!", 1)[0][1:] + + if data[1] == "PRIVMSG": + self.data["channel"] = data[2] + self.data["msg"] = data[-1][1:].strip() + self.data["type"] = "PRIVMSG" + + elif data[1] == "JOIN": + self.data["type"] = "JOIN" + elif data[1] == "PART": + self.data["type"] = "PART" + + def sendMSG(self, msg, channel): + self.sock.send(f"PRIVMSG {channel} :{msg}\r\n".encode(self.code)) + + def sendRAW(self, msg): + self.sock.send(f"{msg}\r\n".encode(self.code)) diff --git a/Python/main.py b/Python/main.py new file mode 100644 index 0000000..a5b2453 --- /dev/null +++ b/Python/main.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +import sys +import time +from irc import IRC + +def main(): + main = IRC("#mainchannel", "cp1251") + main.connect(("irc..ru", 6667), "tester") + + while True: + data = main.recv(512) + if data is None: + continue + + main.parser(data) + print(main.sender) + print(main.data) + +if __name__ == "__main__": + while True: + try: + main() + + except KeyboardInterrupt: + break + + except: + pass