irclibs/C/main.c

88 lines
1.8 KiB
C
Raw Normal View History

2023-08-01 19:40:45 +00:00
#include "irc.h"
2023-08-16 07:53:53 +00:00
//Config
#define HOST "irc.server.net"
#define PORT 6667
#define NICK "tester"
char *channels[] = {"#test"};
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
2023-08-01 19:40:45 +00:00
IRCC_client client;
2023-08-16 07:53:53 +00:00
void die(char *msg) {
2023-08-01 19:40:45 +00:00
puts(msg);
IRCC_close(&client);
exit(1);
}
2023-08-16 07:53:53 +00:00
void recvinfo(int ignore) {
while (1) {
unsigned int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED) {
if (!ignore)
die("Disconnected");
return;
}
else if (strstr(client.raw, "No Ident") || irc_status == IRCC_CONNECTED)
return;
//Print
if (!CHECK_NULL())
return;
if (irc_status == IRCC_PRIVMSG)
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
else if (irc_status == IRCC_NICK)
printf("[N] %s is know as %s\n", client.nick, client.msg);
else if (irc_status == IRCC_TOPIC)
printf("[T] %s\n", client.msg);
else if (irc_status == IRCC_JOIN)
printf("[>] %s\n", client.nick);
else if (irc_status == IRCC_PART)
printf("[<] %s\n", client.nick);
}
}
int main(void) {
//512 - size of raw buffer (max irc)
IRCC_init(&client, IRCC_MSG_MAX);
int status = IRCC_connect(&client, HOST, PORT);
2023-08-01 19:40:45 +00:00
if (status == IRCC_ERROR)
die("Conn refused");
2023-08-16 07:53:53 +00:00
//Socket timeout
struct timeval tv = {5, 5};
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
die("setsockopt");
//Register
recvinfo(1);
IRCC_register(&client, NICK);
//Recv motd and join in channel
recvinfo(0);
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-08-16 07:53:53 +00:00
size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1;
char *tmp = malloc(join_size);
if (tmp == NULL)
die("malloc retured NULL");
snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]);
send(client.socket, tmp, join_size, 0);
free(tmp);
2023-08-01 19:40:45 +00:00
}
2023-08-16 07:53:53 +00:00
recvinfo(0);
2023-08-01 19:40:45 +00:00
}
2023-08-16 07:53:53 +00:00