irclibs/C/main.c

82 lines
1.9 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
2023-08-17 07:01:33 +00:00
#define HOST "localhost"
2023-08-16 07:53:53 +00:00
#define PORT 6667
2023-08-17 07:01:33 +00:00
#define NICK "tester134"
char *channels[] = {"#channel"};
2023-08-16 07:53:53 +00:00
#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-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
2023-08-17 07:01:33 +00:00
else if (irc_status == IRCC_CONNECTED)
2023-08-16 07:53:53 +00:00
return;
//Print
2023-08-17 07:01:33 +00:00
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
2023-08-16 07:53:53 +00:00
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
2023-08-17 07:01:33 +00:00
else if (CHECK_NULL() && irc_status == IRCC_NICK)
2023-08-16 07:53:53 +00:00
printf("[N] %s is know as %s\n", client.nick, client.msg);
2023-08-17 07:01:33 +00:00
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
2023-08-16 07:53:53 +00:00
printf("[T] %s\n", client.msg);
2023-08-23 15:58:58 +00:00
else if (client.nick != NULL && irc_status == IRCC_JOIN)
2023-08-16 07:53:53 +00:00
printf("[>] %s\n", client.nick);
2023-08-23 15:58:58 +00:00
else if (client.nick != NULL && irc_status == IRCC_PART)
2023-08-16 07:53:53 +00:00
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
2023-08-17 07:01:33 +00:00
struct timeval tv = {IRCC_PING_TIMEOUT, IRCC_PING_TIMEOUT};
2023-08-16 07:53:53 +00:00
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
die("setsockopt");
//Register
IRCC_register(&client, NICK);
//Recv motd and join in channel
2023-08-17 07:01:33 +00:00
recvinfo();
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
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