irclibs/C/main.c
2024-03-27 17:12:42 +03:00

81 lines
1.7 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "irc.h"
//Config
#define HOST "irc.rizon.net"
#define PORT 6667
#define NICK "tester134"
char *channels[] = {"#test"};
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
IRCC_client client;
void die(const char *msg) {
perror(msg);
IRCC_close(&client);
exit(1);
}
void handler(int sig) {
(void)sig;
IRCC_close(&client);
exit(0);
}
void recvinfo(void) {
while (1) {
int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED)
die("Disconnected");
irc_status = IRCC_parse(&client);
//Print
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
printf("%s %s\n", client.irc_nick, client.irc_msg);
else if (client.irc_nick != NULL && client.irc_msg != NULL && irc_status == IRCC_NICK)
printf("[N] %s is know as %s\n", client.irc_nick, client.irc_msg);
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
printf("[T] %s\n", client.irc_msg);
else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
printf("[>] %s\n", client.irc_nick);
else if (client.irc_nick != NULL && irc_status == IRCC_PART)
printf("[<] %s\n", client.irc_nick);
}
}
int main(void) {
signal(SIGINT, handler);
int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR)
die("Conn refused");
if (IRCC_initssl(&client) == IRCC_ERROR) {
IRCC_close(&client);
die("ssl error");
}
//Register and skip motd
IRCC_register(&client, NICK);
sleep(5);
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
if (channels[i] == NULL)
break;
IRCC_join(&client, channels[i], NULL);
}
recvinfo();
}