irclibs/C/main.c
Your Name 6a1d2cf850 fix
2023-12-30 17:09:59 +03:00

70 lines
1.5 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "irc.h"
//Config
#define HOST "irc.rizon.net"
#define PORT 6667
#define NICK "tester134"
char *channels[] = {"#hp"};
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
IRCC_client client;
void die(char *msg) {
puts(msg);
IRCC_close(&client);
exit(1);
}
void recvinfo(void) {
while (1) {
unsigned int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED)
die("Disconnected");
//Print
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
else if (CHECK_NULL() && irc_status == IRCC_NICK)
printf("[N] %s is know as %s\n", client.nick, client.msg);
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
printf("[T] %s\n", client.msg);
else if (client.nick != NULL && irc_status == IRCC_JOIN)
printf("[>] %s\n", client.nick);
else if (client.nick != NULL && irc_status == IRCC_PART)
printf("[<] %s\n", client.nick);
}
}
int main(void) {
//512 - size of raw buffer (max irc)
IRCC_init(&client);
int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR)
die("Conn refused");
//Register
IRCC_register(&client, NICK);
//Recv motd and join in channel
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();
}