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

36
C/irc.c
View file

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include <sys/time.h> #include <sys/time.h>
@ -57,11 +58,12 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const int port) {
return IRCC_ERROR; return IRCC_ERROR;
} }
irc->irc_alloc = 1;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
void IRCC_parse_msg(char *tmp, IRCC_client *irc) { void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
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, ':');
@ -94,6 +96,7 @@ int IRCC_recv(IRCC_client *irc) {
free(irc->irc_raw); free(irc->irc_raw);
irc->irc_raw = NULL; irc->irc_raw = NULL;
irc->irc_alloc = 0;
size_t size = 0; size_t size = 0;
char *buf = malloc(1); char *buf = malloc(1);
@ -127,6 +130,7 @@ int IRCC_recv(IRCC_client *irc) {
} }
irc->irc_raw = buf; irc->irc_raw = buf;
irc->irc_alloc = 1;
return IRCC_SUCCESS; return IRCC_SUCCESS;
} }
@ -182,32 +186,14 @@ int IRCC_parse(IRCC_client *irc) {
return IRCC_SUCCESS; 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]; 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) if (IRCC_usend(irc, buf, bytes) < 0)
return IRCC_ERROR; return IRCC_ERROR;

85
C/irc.h
View file

@ -4,15 +4,15 @@
Dont support: Dont support:
ddc ddc
ipv6 ipv6
motd
*/ */
#ifndef _IRCC_H #ifndef _IRCC_H
#define _IRCC_H #define _IRCC_H
#define IRCC_PING_TIMEOUT 600 #define IRCC_PING_TIMEOUT 600
#define IRCC_VERSION "2.1.0b" #define IRCC_VERSION "2.1.0"
#include <stdarg.h>
#if defined(ENABLE_SSL) || defined(ENABLE_TLS) #if defined(ENABLE_SSL) || defined(ENABLE_TLS)
#include <openssl/ssl.h> #include <openssl/ssl.h>
#endif #endif
@ -34,43 +34,74 @@ enum {
IRCC_MODE IRCC_MODE
}; };
static const char *IRCC_ERROR_MSGS[] = {
"IRCC_ALLOC",
"IRCC_DISCONNECTED",
"IRCC_ERROR",
};
#define IRCC_GET_ERROR(n) (IRCC_ERROR_MSGS[n + 3]) #ifndef IRCC_FMT
#define IRCC_FMT
enum {
IRCC_FMT_SEND,
IRCC_FMT_TOPIC,
IRCC_FMT_USER,
IRCC_FMT_NICK,
IRCC_FMT_PASS,
IRCC_FMT_JOIN,
IRCC_FMT_JOIN_PASS,
_IRCC_FMT_MAX
};
typedef struct { static const char *IRCC_FMTS[] = {
int irc_connected; "PRIVMSG %s :%s\r\n",
int irc_socket; "TOPIC %s :%s\r\n",
"USER %s %s %s %s\r\n",
"NICK %s\r\n",
"PASS %s\r\n",
"JOIN %s\r\n",
"JOIN %s :%s\r\n",
};
int irc_alloc; #define IRCC_GET_CMD(n) ((n >= _IRCC_FMT_MAX) ? "error" : IRCC_FMTS[n])
char *irc_raw; #endif
/* RONLY */ #ifndef IRCC_ERR_STR
char *irc_msg; #define IRCC_ERR_STR
char *irc_channel; static const char *IRCC_ERROR_MSGS[] = {
char *irc_nick; "IRCC_ALLOC",
"IRCC_DISCONNECTED",
"IRCC_ERROR",
};
#define IRCC_GET_ERROR(n) ((n >= sizeof(IRCC_ERROR_MSGS) / sizeof(char *)) ? "error" : IRCC_ERROR_MSGS[n + 3])
#endif
#ifndef IRCC_CLIENT
#define IRCC_CLIENT
typedef struct {
int irc_connected;
int irc_socket;
int irc_alloc;
char *irc_raw;
#if defined(ENABLE_SSL) || defined(ENABLE_TLS)
/* RONLY */ /* RONLY */
int irc_usingssl; char *irc_msg;
char *irc_channel;
char *irc_nick;
SSL_METHOD *irc_sslmethod; #if defined(ENABLE_SSL) || defined(ENABLE_TLS)
SSL_CTX *irc_sslctx; /* RONLY */
SSL *irc_ssl; int irc_usingssl;
#endif
} IRCC_client; SSL_METHOD *irc_sslmethod;
SSL_CTX *irc_sslctx;
SSL *irc_ssl;
#endif
} IRCC_client;
#endif
int IRCC_connect(IRCC_client *irc, const char *ip, const int port); int IRCC_connect(IRCC_client *irc, const char *ip, const int port);
int IRCC_recv(IRCC_client *irc); int IRCC_recv(IRCC_client *irc);
int IRCC_parse(IRCC_client *irc); int IRCC_parse(IRCC_client *irc);
int IRCC_register(IRCC_client *irc, const char *nickname); int IRCC_send(IRCC_client *irc, const char *fmt, ...);
int IRCC_join(IRCC_client *irc, const char *channel, const char *key);
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg);
int IRCC_initssl(IRCC_client *irc); int IRCC_initssl(IRCC_client *irc);
void IRCC_close(IRCC_client *irc); void IRCC_close(IRCC_client *irc);

View file

@ -6,10 +6,13 @@
#include "irc.h" #include "irc.h"
/* Config */ /* Config */
#define HOST "irc.rizon.net" #define HOST "192.168.0.136"
#define PORT 6667 #define PORT 6667
#define NICK "tester134" #define NICK "tester"
char *channels[] = {"#testo"}; #define PASS NULL
char *channels[] = {
"#test"
};
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL) #define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
IRCC_client client; IRCC_client client;
@ -34,6 +37,8 @@ void recvinfo(void) {
die("Disconnected"); die("Disconnected");
irc_status = IRCC_parse(&client); irc_status = IRCC_parse(&client);
if (irc_status < 0)
die("Parser");
/* Print */ /* Print */
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG) if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
@ -46,10 +51,10 @@ void recvinfo(void) {
printf("[T] %s\n", client.irc_msg); printf("[T] %s\n", client.irc_msg);
else if (client.irc_nick != NULL && irc_status == IRCC_JOIN) else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
printf("[>] %s\n", client.irc_nick); printf("[>] %s (%s)\n", client.irc_nick, client.irc_msg);
else if (client.irc_nick != NULL && irc_status == IRCC_QUIT) else if (client.irc_nick != NULL && irc_status == IRCC_QUIT)
printf("[<] %s\n", client.irc_nick); printf("[<] %s (%s)\n", client.irc_nick, client.irc_msg);
else else
printf("> %s\n", client.irc_raw); printf("> %s\n", client.irc_raw);
@ -60,23 +65,40 @@ int main(void) {
signal(SIGINT, handler); signal(SIGINT, handler);
int status = IRCC_connect(&client, HOST, PORT); int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR) if (status < 0)
die("Conn refused"); die("Conn refused");
if (IRCC_initssl(&client) == IRCC_ERROR) { if (IRCC_initssl(&client) < 0) {
IRCC_close(&client); IRCC_close(&client);
die("ssl error"); die("ssl error");
} }
/* Register and skip motd */ if (PASS) {
IRCC_register(&client, NICK); IRCC_send(&client,
IRCC_GET_CMD(IRCC_FMT_PASS),
PASS);
}
/* Registration */
sleep(1);
IRCC_send(&client,
IRCC_GET_CMD(IRCC_FMT_USER),
NICK,
NICK,
NICK,
NICK);
IRCC_send(&client,
IRCC_GET_CMD(IRCC_FMT_NICK),
NICK);
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;
IRCC_join(&client, channels[i], NULL); IRCC_send(&client,
IRCC_GET_CMD(IRCC_FMT_JOIN),
channels[i]);
} }
recvinfo(); recvinfo();