This commit is contained in:
Your Name 2025-06-28 10:58:12 +03:00
parent 718f290ba4
commit 73b55ebd79
3 changed files with 103 additions and 64 deletions

38
C/irc.c
View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
@ -57,11 +58,12 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const int port) {
return IRCC_ERROR;
}
irc->irc_alloc = 1;
return IRCC_SUCCESS;
}
void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0';
if (tmp != NULL) {
/* Message */
char *val = strchr(tmp, ':');
@ -79,7 +81,7 @@ void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
val[0] = '\0';
irc->irc_channel = val + 1;
}
/* Nickname */
val = strchr(irc->irc_raw, '!');
if (val != NULL) {
@ -94,6 +96,7 @@ int IRCC_recv(IRCC_client *irc) {
free(irc->irc_raw);
irc->irc_raw = NULL;
irc->irc_alloc = 0;
size_t size = 0;
char *buf = malloc(1);
@ -127,6 +130,7 @@ int IRCC_recv(IRCC_client *irc) {
}
irc->irc_raw = buf;
irc->irc_alloc = 1;
return IRCC_SUCCESS;
}
@ -182,32 +186,14 @@ int IRCC_parse(IRCC_client *irc) {
return IRCC_SUCCESS;
}
int IRCC_register(IRCC_client *irc, const char *nickname) {
int IRCC_send(IRCC_client *irc, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char buf[512];
ssize_t bytes = vsnprintf(buf, sizeof(buf) - 1, fmt, args);
va_end(args);
int bytes = snprintf(buf, sizeof(buf) - 1, "NICK %s\r\n", nickname);
if (IRCC_usend(irc, buf, bytes) < 0)
return IRCC_ERROR;
bytes = snprintf(buf, sizeof(buf) - 1, "USER %s 0 localhost :%s\r\n", nickname, nickname);
if (IRCC_usend(irc, buf, bytes) < 0)
return IRCC_ERROR;
return IRCC_SUCCESS;
}
int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
char buf[512];
ssize_t bytes = snprintf(buf, sizeof(buf) - 1, "JOIN %s %s\r\n", channel, (key) ? key : "");
if (IRCC_usend(irc, buf, bytes) < 0)
return IRCC_ERROR;
return IRCC_SUCCESS;
}
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
char buf[512];
ssize_t bytes = snprintf(buf, sizeof(buf) - 1, "PRIVMSG %s :%s\r\n", channel, msg);
if (IRCC_usend(irc, buf, bytes) < 0)
return IRCC_ERROR;