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:
cc *.c -omain -O0 -g3
cc *.c -omain -O0 -g3 -pedantic -Wall -Wextra
clean:
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;
}
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){
irc->raw[strcspn(irc->raw, "\r\n")] = '\0';
if (tmp != NULL){
//Message
if (strstr(tmp, ":") != NULL)
irc->msg = strstr(tmp, ":");
char *val = strchr(tmp, ':');
if (val == NULL)
return;
val[0] = '\0';
irc->msg = val + 1;
//Del space before :
*(val - 1) = '\0';
//Channel
if (strstr(tmp, " ") != NULL){
irc->channel = strstr(tmp, " ") + 1;
irc->channel[strcspn(irc->channel, ":") - 1] = '\0';
}
val = strchr(tmp, ' ');
if (val == NULL)
return;
val[0] = '\0';
irc->channel = val + 1;
//Nickname
irc->raw[strcspn(irc->raw, "!")] = '\0';
val = strchr(irc->raw, '!');
if (val == NULL)
return;
val[0] = '\0';
irc->nick = irc->raw + 1;
}
}
@ -66,19 +65,23 @@ int IRCC_recv(IRCC_client *irc){
irc->msg = 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)
return IRCC_DISCONNECTED;
else if (!strncmp(irc->raw, "PING", 4)){
*(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;
}
else {
//puts(irc->raw);
#ifdef IRCC_DEBUG
puts(irc->raw);
#endif
//Check end of motd
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
return IRCC_CONNECTED;
@ -119,34 +122,51 @@ int IRCC_recv(IRCC_client *irc){
return IRCC_SUCCESS;
}
int IRCC_send(IRCC_client *irc, const char *msg, const char *channel){
size_t size = strlen("PRIVMSG :\r\n") + strlen(channel) + strlen(msg);
int IRCC_register(IRCC_client *irc, const char *nickname){
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);
if (tmp == NULL)
return IRCC_ALLOC;
bytes = snprintf(irc->raw, sizeof(irc->raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
if (send(irc->socket, irc->raw, bytes, 0) == -1)
return IRCC_ERROR;
snprintf(tmp, size, "PRIVMSG %s :%s\r\n", channel, msg);
int ret = send(irc->socket, tmp, strlen(tmp), 0);
//Motd skip
while (1) {
int status = IRCC_recv(irc);
if (status == IRCC_CONNECTED)
break;
memset(tmp, 0, size);
free(tmp);
else if (status == IRCC_DISCONNECTED)
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) {
irc->raw = (char *)malloc(IRCC_MSG_MAX + 1);
if (irc->raw == NULL)
return IRCC_ALLOC;
irc->msg = irc->nick = irc->channel = NULL;
return IRCC_SUCCESS;
}
void IRCC_close(IRCC_client *irc){
close(irc->socket);
free(irc->raw);
irc->raw = irc->msg = irc->nick = irc->channel = NULL;
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
Dont support:
ssl
ddc
ipv6
motd parsing
motd
*/
#include <stdio.h>
@ -32,22 +32,22 @@ enum {
IRCC_CONNECTED,
IRCC_DISCONNECTED,
IRCC_ERROR,
IRCC_ALLOC,
IRCC_SUCCESS
};
typedef struct {
int socket;
char *raw;
char raw[IRCC_MSG_MAX + 1];
char *msg;
char *channel;
char *nick;
} IRCC_client;
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_join(IRCC_client *irc, const char *channel, const char *key);
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);
void IRCC_close(IRCC_client *irc);

View File

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