Upload files to "C"

This commit is contained in:
8nl 2024-01-23 16:43:32 +00:00
parent 6a1d2cf850
commit b4825a3fb7
3 changed files with 93 additions and 91 deletions

147
C/irc.c
View File

@ -9,7 +9,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include "irc.h" #include "irc.h"
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) {
struct hostent *hp = gethostbyname(ip); struct hostent *hp = gethostbyname(ip);
if (!hp) if (!hp)
return IRCC_ERROR; return IRCC_ERROR;
@ -21,29 +21,29 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){
client_str.sin_port = htons(port); client_str.sin_port = htons(port);
client_str.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)hp->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); irc->irc_socket = socket(AF_INET, SOCK_STREAM, 0);
if (irc->socket < 0) if (irc->irc_socket < 0)
return IRCC_ERROR; return IRCC_ERROR;
struct timeval tv = {IRCC_PING_TIMEOUT, 0}; struct timeval tv = {IRCC_PING_TIMEOUT, 0};
if (setsockopt(irc->socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) if (setsockopt(irc->irc_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
return IRCC_ERROR; return IRCC_ERROR;
int status = connect(irc->socket, (struct sockaddr *)&client_str, sizeof(client_str)); int status = connect(irc->irc_socket, (struct sockaddr *)&client_str, sizeof(client_str));
if (status == -1) if (status == -1)
return IRCC_ERROR; return IRCC_ERROR;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
void IRCC_parse(char *tmp, IRCC_client *irc){ void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
irc->raw[strcspn(irc->raw, "\r\n")] = '\0'; irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0';
if (tmp != NULL){ if (tmp != NULL) {
//Message //Message
char *val = strchr(tmp, ':'); char *val = strchr(tmp, ':');
if (val != NULL) { if (val != NULL) {
val[0] = '\0'; val[0] = '\0';
irc->msg = val + 1; irc->irc_msg = val + 1;
//Del space before : //Del space before :
*(val - 1) = '\0'; *(val - 1) = '\0';
@ -53,126 +53,125 @@ void IRCC_parse(char *tmp, IRCC_client *irc){
val = strchr(tmp, ' '); val = strchr(tmp, ' ');
if (val != NULL) { if (val != NULL) {
val[0] = '\0'; val[0] = '\0';
irc->channel = val + 1; irc->irc_channel = val + 1;
} }
//Nickname //Nickname
val = strchr(irc->raw, '!'); val = strchr(irc->irc_raw, '!');
if (val != NULL) { if (val != NULL) {
val[0] = '\0'; val[0] = '\0';
irc->nick = irc->raw + 1; irc->irc_nick = irc->irc_raw + 1;
} }
} }
} }
int IRCC_recv(IRCC_client *irc){ int IRCC_recv(IRCC_client *irc) {
memset(irc->raw, '\0', IRCC_MSG_MAX); memset(irc->irc_raw, '\0', IRCC_MSG_MAX);
irc->channel = NULL; irc->irc_channel = NULL;
irc->msg = NULL; irc->irc_msg = NULL;
irc->nick = NULL; irc->irc_nick = NULL;
int msg_size = recv(irc->socket, irc->raw, sizeof(irc->raw), 0); int msg_size = recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
if (msg_size == 0 || msg_size == -1) if (msg_size == 0 || msg_size == -1)
return IRCC_DISCONNECTED; return IRCC_DISCONNECTED;
else if (!strncmp(irc->raw, "PING", 4)){ else if (!strncmp(irc->irc_raw, "PING", 4)) {
*(strchr(irc->raw, 'I')) = 'O'; *(strchr(irc->irc_raw, 'I')) = 'O';
if (send(irc->socket, irc->raw, strlen(irc->raw), 0) == -1) if (send(irc->irc_socket, irc->irc_raw, strlen(irc->irc_raw), 0) == -1)
return IRCC_ERROR; return IRCC_ERROR;
return IRCC_PING; return IRCC_PING;
} }
else { return IRCC_SUCCESS;
}
#ifdef IRCC_DEBUG int IRCC_parse(IRCC_client *irc) {
puts(irc->raw);
#endif
//Check end of motd
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
return IRCC_CONNECTED;
//Other //Check end of motd
else if (strstr(irc->raw, "PRIVMSG ")){ if (strstr(irc->irc_raw, "PRIVMSG ") == NULL && strstr(irc->irc_raw, "MOTD"))
IRCC_parse(strstr(irc->raw, "PRIVMSG "), irc); return IRCC_CONNECTED;
return IRCC_PRIVMSG;
}
else if (strstr(irc->raw, "NICK ")){ //Other
IRCC_parse(strstr(irc->raw, "NICK "), irc); else if (strstr(irc->irc_raw, "PRIVMSG ")) {
return IRCC_NICK; IRCC_parse_msg(strstr(irc->irc_raw, "PRIVMSG "), irc);
} return IRCC_PRIVMSG;
}
else if (strstr(irc->raw, "TOPIC ")){ else if (strstr(irc->irc_raw, "NICK ")) {
IRCC_parse(strstr(irc->raw, "TOPIC "), irc); IRCC_parse_msg(strstr(irc->irc_raw, "NICK "), irc);
return IRCC_TOPIC; return IRCC_NICK;
} }
else if (strstr(irc->raw, "MODE ")){ else if (strstr(irc->irc_raw, "TOPIC ")) {
IRCC_parse(strstr(irc->raw, "MODE "), irc); IRCC_parse_msg(strstr(irc->irc_raw, "TOPIC "), irc);
return IRCC_MODE; return IRCC_TOPIC;
} }
else if (strstr(irc->raw, "JOIN ")){ else if (strstr(irc->irc_raw, "MODE ")) {
IRCC_parse(strstr(irc->raw, "JOIN "), irc); IRCC_parse_msg(strstr(irc->irc_raw, "MODE "), irc);
return IRCC_JOIN; return IRCC_MODE;
} }
else if (strstr(irc->raw, "PART ") || strstr(irc->raw, "QUIT ")){ else if (strstr(irc->irc_raw, "JOIN ")) {
IRCC_parse(strstr(irc->raw, "PART "), irc); IRCC_parse_msg(strstr(irc->irc_raw, "JOIN "), irc);
IRCC_parse(strstr(irc->raw, "QUIT "), irc); return IRCC_JOIN;
return IRCC_PART; }
}
else if (strstr(irc->irc_raw, "PART ") || strstr(irc->irc_raw, "QUIT ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "PART "), irc);
IRCC_parse_msg(strstr(irc->irc_raw, "QUIT "), irc);
return IRCC_PART;
} }
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_register(IRCC_client *irc, const char *nickname){ int IRCC_register(IRCC_client *irc, const char *nickname) {
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "NICK %s\r\n", nickname); off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
if (send(irc->socket, irc->raw, bytes, 0) == -1) if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
return IRCC_ERROR; return IRCC_ERROR;
bytes = snprintf(irc->raw, sizeof(irc->raw), "USER %s 0 localhost :%s\r\n", nickname, nickname); bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
if (send(irc->socket, irc->raw, bytes, 0) == -1) if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
return IRCC_ERROR; return IRCC_ERROR;
//Motd skip //Motd skip
while (1) { while (1) {
int status = IRCC_recv(irc); int status = IRCC_recv(irc);
if (status == IRCC_DISCONNECTED)
return IRCC_DISCONNECTED;
status = IRCC_parse(irc);
if (status == IRCC_CONNECTED) if (status == IRCC_CONNECTED)
break; break;
else if (status == IRCC_DISCONNECTED)
return IRCC_DISCONNECTED;
} }
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_join(IRCC_client *irc, const char *channel, const char *key){ int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "JOIN %s %s\r\n", channel, (key) ? key : ""); off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
if (send(irc->socket, irc->raw, bytes, 0) == -1) if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
return IRCC_ERROR; return IRCC_ERROR;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg){ int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "PRIVMSG %s :%s\r\n", channel, msg); off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
if (send(irc->socket, irc->raw, bytes, 0) == -1) if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
return IRCC_ERROR; return IRCC_ERROR;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_init(IRCC_client *irc) { void IRCC_init(IRCC_client *irc) {
irc->msg = irc->nick = irc->channel = NULL; irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
return IRCC_SUCCESS;
} }
void IRCC_close(IRCC_client *irc){ void IRCC_close(IRCC_client *irc) {
close(irc->socket); close(irc->irc_socket);
irc->msg = irc->nick = irc->channel = NULL; irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
} }

13
C/irc.h
View File

@ -27,18 +27,19 @@ enum {
}; };
typedef struct { typedef struct {
int socket; int irc_socket;
char raw[IRCC_MSG_MAX + 1]; char irc_raw[IRCC_MSG_MAX + 1];
char *msg; char *irc_msg;
char *channel; char *irc_channel;
char *nick; char *irc_nick;
} IRCC_client; } IRCC_client;
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port); int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port);
int IRCC_recv(IRCC_client *irc); int IRCC_recv(IRCC_client *irc);
int IRCC_parse(IRCC_client *irc);
int IRCC_join(IRCC_client *irc, const char *channel, const char *key); int IRCC_join(IRCC_client *irc, const char *channel, const char *key);
int IRCC_send(IRCC_client *irc, const char *msg, const char *channel); int IRCC_send(IRCC_client *irc, const char *msg, const char *channel);
int IRCC_register(IRCC_client *irc, const char *nickname); int IRCC_register(IRCC_client *irc, const char *nickname);
int IRCC_init(IRCC_client *irc); void IRCC_init(IRCC_client *irc);
void IRCC_close(IRCC_client *irc); void IRCC_close(IRCC_client *irc);

View File

@ -5,13 +5,13 @@
#include "irc.h" #include "irc.h"
//Config //Config
#define HOST "irc.rizon.net" #define HOST "192.168.0.184"
#define PORT 6667 #define PORT 6667
#define NICK "tester134" #define NICK "tester134"
char *channels[] = {"#hp"}; char *channels[] = {"#test"};
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL) #define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
IRCC_client client; IRCC_client client;
void die(char *msg) { void die(char *msg) {
@ -26,21 +26,23 @@ void recvinfo(void) {
if (irc_status == IRCC_DISCONNECTED) if (irc_status == IRCC_DISCONNECTED)
die("Disconnected"); die("Disconnected");
irc_status = IRCC_parse(&client);
//Print //Print
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG) if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
printf("[%s %s] %s\n", client.channel, client.nick, client.msg); printf("[%s %s] %s\n", client.irc_channel, client.irc_nick, client.irc_msg);
else if (CHECK_NULL() && irc_status == IRCC_NICK) else if (client.irc_nick != NULL && client.irc_msg != NULL && irc_status == IRCC_NICK)
printf("[N] %s is know as %s\n", client.nick, client.msg); printf("[N] %s is know as %s\n", client.irc_nick, client.irc_msg);
else if (CHECK_NULL() && irc_status == IRCC_TOPIC) else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
printf("[T] %s\n", client.msg); printf("[T] %s\n", client.irc_msg);
else if (client.nick != NULL && irc_status == IRCC_JOIN) else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
printf("[>] %s\n", client.nick); printf("[>] %s\n", client.irc_nick);
else if (client.nick != NULL && irc_status == IRCC_PART) else if (client.irc_nick != NULL && irc_status == IRCC_PART)
printf("[<] %s\n", client.nick); printf("[<] %s\n", client.irc_nick);
} }
} }