irclibs/C/irc.c

238 lines
5.4 KiB
C
Raw Normal View History

2023-12-30 14:09:59 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
2023-08-01 19:40:45 +00:00
#include "irc.h"
2024-02-21 17:11:23 +00:00
int IRCC_urecv(IRCC_client *irc) {
#ifdef ENABLE_SSL
if (irc->irc_usingssl)
return SSL_read(irc->irc_ssl, irc->irc_raw, sizeof(irc->irc_raw));
#endif
return recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
}
int IRCC_usend(IRCC_client *irc, off_t bytes) {
#ifdef ENABLE_SSL
if (irc->irc_usingssl)
return SSL_write(irc->irc_ssl, irc->irc_raw, bytes);
#endif
return send(irc->irc_socket, irc->irc_raw, bytes, 0);
}
2024-01-23 16:43:32 +00:00
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) {
2023-11-02 16:17:12 +00:00
struct hostent *hp = gethostbyname(ip);
if (!hp)
2023-08-01 19:40:45 +00:00
return IRCC_ERROR;
2023-08-09 18:53:12 +00:00
//Only ipv4
2023-08-01 19:40:45 +00:00
struct sockaddr_in client_str;
memset(&client_str, 0, sizeof(client_str));
client_str.sin_family = AF_INET;
client_str.sin_port = htons(port);
2023-11-02 16:17:12 +00:00
client_str.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)hp->h_addr));
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
irc->irc_socket = socket(AF_INET, SOCK_STREAM, 0);
if (irc->irc_socket < 0)
2023-08-01 19:40:45 +00:00
return IRCC_ERROR;
2023-10-06 13:03:43 +00:00
struct timeval tv = {IRCC_PING_TIMEOUT, 0};
2024-01-23 16:43:32 +00:00
if (setsockopt(irc->irc_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
2023-10-06 13:03:43 +00:00
return IRCC_ERROR;
2024-01-23 16:43:32 +00:00
int status = connect(irc->irc_socket, (struct sockaddr *)&client_str, sizeof(client_str));
2023-08-01 19:40:45 +00:00
if (status == -1)
return IRCC_ERROR;
return IRCC_SUCCESS;
}
2024-01-23 16:43:32 +00:00
void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0';
2024-02-21 17:11:23 +00:00
2024-01-23 16:43:32 +00:00
if (tmp != NULL) {
2023-08-01 19:40:45 +00:00
//Message
2023-12-13 13:15:52 +00:00
char *val = strchr(tmp, ':');
2023-12-30 13:55:10 +00:00
if (val != NULL) {
val[0] = '\0';
2024-01-23 16:43:32 +00:00
irc->irc_msg = val + 1;
2023-12-13 13:15:52 +00:00
2023-12-30 13:55:10 +00:00
//Del space before :
*(val - 1) = '\0';
}
2023-08-01 19:40:45 +00:00
//Channel
2023-12-13 13:15:52 +00:00
val = strchr(tmp, ' ');
2023-12-30 13:55:10 +00:00
if (val != NULL) {
val[0] = '\0';
2024-01-23 16:43:32 +00:00
irc->irc_channel = val + 1;
2023-12-30 13:55:10 +00:00
}
2023-08-01 19:40:45 +00:00
//Nickname
2024-01-23 16:43:32 +00:00
val = strchr(irc->irc_raw, '!');
2023-12-30 13:55:10 +00:00
if (val != NULL) {
val[0] = '\0';
2024-01-23 16:43:32 +00:00
irc->irc_nick = irc->irc_raw + 1;
2023-12-30 13:55:10 +00:00
}
2023-08-01 19:40:45 +00:00
}
}
2024-01-23 16:43:32 +00:00
int IRCC_recv(IRCC_client *irc) {
memset(irc->irc_raw, '\0', IRCC_MSG_MAX);
2023-08-01 19:40:45 +00:00
2024-02-21 17:11:23 +00:00
int msg_size = IRCC_urecv(irc);
2023-08-01 19:40:45 +00:00
if (msg_size == 0 || msg_size == -1)
return IRCC_DISCONNECTED;
2024-01-23 16:43:32 +00:00
else if (!strncmp(irc->irc_raw, "PING", 4)) {
*(strchr(irc->irc_raw, 'I')) = 'O';
2024-02-21 17:11:23 +00:00
if (IRCC_usend(irc, strlen(irc->irc_raw)) == -1)
2023-12-13 13:15:52 +00:00
return IRCC_ERROR;
2023-08-01 19:40:45 +00:00
return IRCC_PING;
}
2024-01-23 16:43:32 +00:00
return IRCC_SUCCESS;
}
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
int IRCC_parse(IRCC_client *irc) {
2024-02-21 17:11:23 +00:00
irc->irc_channel = NULL;
irc->irc_msg = NULL;
irc->irc_nick = NULL;
2024-01-23 16:43:32 +00:00
//Check end of motd
if (strstr(irc->irc_raw, "PRIVMSG ") == NULL && strstr(irc->irc_raw, "MOTD"))
return IRCC_CONNECTED;
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
//Other
else if (strstr(irc->irc_raw, "PRIVMSG ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "PRIVMSG "), irc);
return IRCC_PRIVMSG;
}
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
else if (strstr(irc->irc_raw, "NICK ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "NICK "), irc);
return IRCC_NICK;
}
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
else if (strstr(irc->irc_raw, "TOPIC ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "TOPIC "), irc);
return IRCC_TOPIC;
}
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
else if (strstr(irc->irc_raw, "MODE ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "MODE "), irc);
return IRCC_MODE;
}
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
else if (strstr(irc->irc_raw, "JOIN ")) {
IRCC_parse_msg(strstr(irc->irc_raw, "JOIN "), irc);
return IRCC_JOIN;
}
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);
2024-03-27 14:12:42 +00:00
return IRCC_QUIT;
2023-08-01 19:40:45 +00:00
}
return IRCC_SUCCESS;
}
2024-01-23 16:43:32 +00:00
int IRCC_register(IRCC_client *irc, const char *nickname) {
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
2024-02-21 17:11:23 +00:00
if (IRCC_usend(irc, bytes) == -1)
2023-12-13 13:15:52 +00:00
return IRCC_ERROR;
2023-08-01 19:40:45 +00:00
2024-01-23 16:43:32 +00:00
bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
2024-02-21 17:11:23 +00:00
if (IRCC_usend(irc, bytes) == -1)
2023-12-13 13:15:52 +00:00
return IRCC_ERROR;
2023-08-01 19:40:45 +00:00
2023-12-13 13:15:52 +00:00
//Motd skip
while (1) {
int status = IRCC_recv(irc);
2024-01-23 16:43:32 +00:00
if (status == IRCC_DISCONNECTED)
return IRCC_DISCONNECTED;
status = IRCC_parse(irc);
2023-12-13 13:15:52 +00:00
if (status == IRCC_CONNECTED)
break;
}
2023-11-02 21:49:37 +00:00
2023-12-13 13:15:52 +00:00
return IRCC_SUCCESS;
2023-08-01 19:40:45 +00:00
}
2024-01-23 16:43:32 +00:00
int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
2024-02-21 17:11:23 +00:00
if (IRCC_usend(irc, bytes) == -1)
2023-12-30 13:55:10 +00:00
return IRCC_ERROR;
2023-12-13 13:15:52 +00:00
2023-12-30 13:55:10 +00:00
return IRCC_SUCCESS;
2023-12-13 13:15:52 +00:00
}
2024-01-23 16:43:32 +00:00
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
2024-02-21 17:11:23 +00:00
if (IRCC_usend(irc, bytes) == -1)
2023-12-30 13:55:10 +00:00
return IRCC_ERROR;
2023-08-01 19:40:45 +00:00
2023-12-30 13:55:10 +00:00
return IRCC_SUCCESS;
2023-12-13 13:15:52 +00:00
}
2024-03-04 14:34:59 +00:00
int IRCC_initssl(IRCC_client *irc) {
2024-02-21 17:11:23 +00:00
#ifdef ENABLE_SSL
2024-03-04 14:34:59 +00:00
irc->irc_ssl = NULL;
irc->irc_sslctx = NULL;
irc->irc_sslmethod = NULL;
2024-02-21 17:11:23 +00:00
OpenSSL_add_all_algorithms();
2024-03-04 14:34:59 +00:00
if (SSL_library_init() < 0)
2024-02-21 17:11:23 +00:00
return IRCC_ERROR;
irc->irc_sslmethod = (SSL_METHOD *)SSLv23_client_method();
irc->irc_sslctx = SSL_CTX_new(irc->irc_sslmethod);
2024-03-04 14:34:59 +00:00
if (irc->irc_sslctx == NULL)
2024-02-21 17:11:23 +00:00
return IRCC_ERROR;
SSL_CTX_set_options(irc->irc_sslctx, SSL_OP_NO_SSLv2);
irc->irc_ssl = SSL_new(irc->irc_sslctx);
SSL_set_fd(irc->irc_ssl, irc->irc_socket);
2024-03-04 14:34:59 +00:00
if (SSL_connect(irc->irc_ssl) != 1)
2024-02-21 17:11:23 +00:00
return IRCC_ERROR;
irc->irc_usingssl = 1;
#else
(void)irc;
#endif
return IRCC_SUCCESS;
2023-08-01 19:40:45 +00:00
}
2024-01-23 16:43:32 +00:00
void IRCC_close(IRCC_client *irc) {
close(irc->irc_socket);
irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
2024-02-21 17:11:23 +00:00
#ifdef ENABLE_SSL
if (!irc->irc_usingssl)
return;
2024-03-04 14:34:59 +00:00
if (irc->irc_ssl != NULL) {
SSL_shutdown(irc->irc_ssl);
SSL_free(irc->irc_ssl);
}
if (irc->irc_sslctx != NULL)
SSL_CTX_free(irc->irc_sslctx);
2024-02-21 17:11:23 +00:00
#endif
2023-08-01 19:40:45 +00:00
}