irclibs/C/main.c

81 lines
1.7 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>
2024-03-04 14:34:59 +00:00
#include <signal.h>
2023-08-01 19:40:45 +00:00
#include "irc.h"
2023-08-16 07:53:53 +00:00
//Config
2024-02-21 17:11:23 +00:00
#define HOST "irc.rizon.net"
2024-03-27 14:12:42 +00:00
#define PORT 6667
2023-08-17 07:01:33 +00:00
#define NICK "tester134"
2024-03-04 14:34:59 +00:00
char *channels[] = {"#test"};
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
2023-08-01 19:40:45 +00:00
IRCC_client client;
2024-02-21 17:11:23 +00:00
void die(const char *msg) {
perror(msg);
2023-08-01 19:40:45 +00:00
IRCC_close(&client);
exit(1);
}
2024-03-04 14:34:59 +00:00
void handler(int sig) {
(void)sig;
IRCC_close(&client);
exit(0);
}
2023-08-17 07:01:33 +00:00
void recvinfo(void) {
2023-08-16 07:53:53 +00:00
while (1) {
2024-03-27 14:12:42 +00:00
int irc_status = IRCC_recv(&client);
2023-08-17 07:01:33 +00:00
if (irc_status == IRCC_DISCONNECTED)
die("Disconnected");
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
irc_status = IRCC_parse(&client);
2023-08-16 07:53:53 +00:00
//Print
2023-08-17 07:01:33 +00:00
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
2024-03-27 14:12:42 +00:00
printf("%s %s\n", client.irc_nick, client.irc_msg);
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
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);
2023-08-16 07:53:53 +00:00
2023-08-17 07:01:33 +00:00
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
2024-01-23 16:43:32 +00:00
printf("[T] %s\n", client.irc_msg);
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
printf("[>] %s\n", client.irc_nick);
2023-08-16 07:53:53 +00:00
2024-01-23 16:43:32 +00:00
else if (client.irc_nick != NULL && irc_status == IRCC_PART)
printf("[<] %s\n", client.irc_nick);
2023-08-16 07:53:53 +00:00
}
}
int main(void) {
2024-03-04 14:34:59 +00:00
signal(SIGINT, handler);
2023-08-16 07:53:53 +00:00
int status = IRCC_connect(&client, HOST, PORT);
2023-08-01 19:40:45 +00:00
if (status == IRCC_ERROR)
die("Conn refused");
2024-03-04 14:34:59 +00:00
if (IRCC_initssl(&client) == IRCC_ERROR) {
IRCC_close(&client);
die("ssl error");
}
2024-02-21 17:11:23 +00:00
//Register and skip motd
2023-08-16 07:53:53 +00:00
IRCC_register(&client, NICK);
2023-08-01 19:40:45 +00:00
sleep(5);
2023-08-16 07:53:53 +00:00
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
if (channels[i] == NULL)
break;
2023-08-01 19:40:45 +00:00
2023-12-13 13:15:52 +00:00
IRCC_join(&client, channels[i], NULL);
2023-08-01 19:40:45 +00:00
}
2023-08-16 07:53:53 +00:00
2023-08-17 07:01:33 +00:00
recvinfo();
2023-08-01 19:40:45 +00:00
}