irclibs/C/main.c

71 lines
1.6 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>
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"
#define PORT 6697
2023-08-17 07:01:33 +00:00
#define NICK "tester134"
2024-02-21 17:11:23 +00:00
char *channels[] = {"#testssl"};
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);
}
2023-08-17 07:01:33 +00:00
void recvinfo(void) {
2023-08-16 07:53:53 +00:00
while (1) {
unsigned 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-02-21 17:11:23 +00:00
printf("%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 && 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) {
int status = IRCC_connect(&client, HOST, PORT);
2023-08-01 19:40:45 +00:00
if (status == IRCC_ERROR)
die("Conn refused");
2024-02-21 17:11:23 +00:00
int ie = 0;
if (IRCC_initssl(&client, &ie) == IRCC_ERROR)
die(IRCC_errno[ie]);
//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
}
2023-08-16 07:53:53 +00:00