rewrite parser

This commit is contained in:
Your Name 2023-12-13 16:15:52 +03:00
parent d6d2f02367
commit 751260e234
5 changed files with 77 additions and 64 deletions

View File

@ -1,5 +1,5 @@
all: all:
cc *.c -omain -O0 -g3 cc *.c -omain -O0 -g3 -pedantic -Wall -Wextra
clean: clean:
rm main rm main

102
C/irc.c
View File

@ -27,35 +27,34 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_register(IRCC_client *irc, const char *nickname){
size_t size = strlen("NICK %s\r\nUSER %s 0 localhost :%s \r\n") + (strlen(nickname) * 3);
char *tmp = (char *)malloc(size);
if (tmp == NULL)
return IRCC_ALLOC;
sleep(2);
snprintf(tmp, size, "NICK %s\r\nUSER %s 0 localhost :%s\r\n", nickname, nickname, nickname);
send(irc->socket, tmp, strlen(tmp), 0);
free(tmp);
return IRCC_SUCCESS;
}
void IRCC_parse(char *tmp, IRCC_client *irc){ void IRCC_parse(char *tmp, IRCC_client *irc){
irc->raw[strcspn(irc->raw, "\r\n")] = '\0'; irc->raw[strcspn(irc->raw, "\r\n")] = '\0';
if (tmp != NULL){ if (tmp != NULL){
//Message //Message
if (strstr(tmp, ":") != NULL) char *val = strchr(tmp, ':');
irc->msg = strstr(tmp, ":"); if (val == NULL)
return;
val[0] = '\0';
irc->msg = val + 1;
//Del space before :
*(val - 1) = '\0';
//Channel //Channel
if (strstr(tmp, " ") != NULL){ val = strchr(tmp, ' ');
irc->channel = strstr(tmp, " ") + 1; if (val == NULL)
irc->channel[strcspn(irc->channel, ":") - 1] = '\0'; return;
}
val[0] = '\0';
irc->channel = val + 1;
//Nickname //Nickname
irc->raw[strcspn(irc->raw, "!")] = '\0'; val = strchr(irc->raw, '!');
if (val == NULL)
return;
val[0] = '\0';
irc->nick = irc->raw + 1; irc->nick = irc->raw + 1;
} }
} }
@ -66,19 +65,23 @@ int IRCC_recv(IRCC_client *irc){
irc->msg = NULL; irc->msg = NULL;
irc->nick = NULL; irc->nick = NULL;
int msg_size = recv(irc->socket, irc->raw, IRCC_MSG_MAX, 0); int msg_size = recv(irc->socket, irc->raw, sizeof(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->raw, "PING", 4)){
*(strchr(irc->raw, 'I')) = 'O'; *(strchr(irc->raw, 'I')) = 'O';
send(irc->socket, irc->raw, strlen(irc->raw), 0); if (send(irc->socket, irc->raw, strlen(irc->raw), 0) == -1)
return IRCC_ERROR;
return IRCC_PING; return IRCC_PING;
} }
else { else {
//puts(irc->raw);
#ifdef IRCC_DEBUG
puts(irc->raw);
#endif
//Check end of motd //Check end of motd
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD")) if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
return IRCC_CONNECTED; return IRCC_CONNECTED;
@ -119,34 +122,51 @@ int IRCC_recv(IRCC_client *irc){
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
int IRCC_send(IRCC_client *irc, const char *msg, const char *channel){ int IRCC_register(IRCC_client *irc, const char *nickname){
size_t size = strlen("PRIVMSG :\r\n") + strlen(channel) + strlen(msg); off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "NICK %s\r\n", nickname);
if (send(irc->socket, irc->raw, bytes, 0) == -1)
return IRCC_ERROR;
char *tmp = (char *)malloc(size + 1); bytes = snprintf(irc->raw, sizeof(irc->raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
if (tmp == NULL) if (send(irc->socket, irc->raw, bytes, 0) == -1)
return IRCC_ALLOC; return IRCC_ERROR;
snprintf(tmp, size, "PRIVMSG %s :%s\r\n", channel, msg); //Motd skip
int ret = send(irc->socket, tmp, strlen(tmp), 0); while (1) {
int status = IRCC_recv(irc);
if (status == IRCC_CONNECTED)
break;
memset(tmp, 0, size); else if (status == IRCC_DISCONNECTED)
free(tmp); return IRCC_DISCONNECTED;
}
return IRCC_SUCCESS;
}
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 : "");
if (send(irc->socket, irc->raw, bytes, 0) == -1)
return IRCC_SUCCESS;
return IRCC_ERROR;
}
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);
if (send(irc->socket, irc->raw, bytes, 0) == -1)
return IRCC_SUCCESS;
return IRCC_ERROR;
return ret;
} }
int IRCC_init(IRCC_client *irc) { int IRCC_init(IRCC_client *irc) {
irc->raw = (char *)malloc(IRCC_MSG_MAX + 1);
if (irc->raw == NULL)
return IRCC_ALLOC;
irc->msg = irc->nick = irc->channel = NULL; irc->msg = irc->nick = irc->channel = NULL;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
void IRCC_close(IRCC_client *irc){ void IRCC_close(IRCC_client *irc){
close(irc->socket); close(irc->socket);
irc->msg = irc->nick = irc->channel = NULL;
free(irc->raw);
irc->raw = irc->msg = irc->nick = irc->channel = NULL;
} }

10
C/irc.h
View File

@ -1,11 +1,11 @@
/* /*
LICENSE: MIT LICENSE: WTFPL
Simple libary for working with ipv4 irc servers Simple libary for working with ipv4 irc servers
Dont support: Dont support:
ssl ssl
ddc ddc
ipv6 ipv6
motd parsing motd
*/ */
#include <stdio.h> #include <stdio.h>
@ -32,22 +32,22 @@ enum {
IRCC_CONNECTED, IRCC_CONNECTED,
IRCC_DISCONNECTED, IRCC_DISCONNECTED,
IRCC_ERROR, IRCC_ERROR,
IRCC_ALLOC,
IRCC_SUCCESS IRCC_SUCCESS
}; };
typedef struct { typedef struct {
int socket; int socket;
char *raw; char raw[IRCC_MSG_MAX + 1];
char *msg; char *msg;
char *channel; char *channel;
char *nick; char *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_register(IRCC_client *irc, const char *nickname);
int IRCC_recv(IRCC_client *irc); int IRCC_recv(IRCC_client *irc);
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_init(IRCC_client *irc); int IRCC_init(IRCC_client *irc);
void IRCC_close(IRCC_client *irc); void IRCC_close(IRCC_client *irc);

View File

@ -22,9 +22,6 @@ void recvinfo(void) {
if (irc_status == IRCC_DISCONNECTED) if (irc_status == IRCC_DISCONNECTED)
die("Disconnected"); die("Disconnected");
else if (irc_status == IRCC_CONNECTED)
return;
//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.channel, client.nick, client.msg);
@ -55,20 +52,12 @@ int main(void) {
IRCC_register(&client, NICK); IRCC_register(&client, NICK);
//Recv motd and join in channel //Recv motd and join in channel
recvinfo();
sleep(5); sleep(5);
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) { for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
if (channels[i] == NULL) if (channels[i] == NULL)
break; break;
size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1; IRCC_join(&client, channels[i], NULL);
char *tmp = malloc(join_size);
if (tmp == NULL)
die("malloc retured NULL");
snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]);
send(client.socket, tmp, join_size, 0);
free(tmp);
} }
recvinfo(); recvinfo();

14
LICENSE
View File

@ -1,9 +1,13 @@
MIT License DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (c) <year> <copyright holders> Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
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: Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
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. 0. You just DO WHAT THE FUCK YOU WANT TO.