105 lines
2 KiB
C
105 lines
2 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include "irc.h"
|
|
|
|
/* Config */
|
|
#define HOST "192.168.0.136"
|
|
#define PORT 6667
|
|
#define NICK "tester"
|
|
#define PASS NULL
|
|
char *channels[] = {
|
|
"#test"
|
|
};
|
|
|
|
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
|
|
IRCC_client client;
|
|
|
|
void die(const char *msg) {
|
|
perror(msg);
|
|
IRCC_close(&client);
|
|
exit(1);
|
|
}
|
|
|
|
void handler(int sig) {
|
|
(void)sig;
|
|
|
|
IRCC_close(&client);
|
|
exit(0);
|
|
}
|
|
|
|
void recvinfo(void) {
|
|
while (1) {
|
|
int irc_status = IRCC_recv(&client);
|
|
if (irc_status < 0)
|
|
die("Disconnected");
|
|
|
|
irc_status = IRCC_parse(&client);
|
|
if (irc_status < 0)
|
|
die("Parser");
|
|
|
|
/* Print */
|
|
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
|
|
printf("%s %s\n", client.irc_nick, client.irc_msg);
|
|
|
|
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);
|
|
|
|
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
|
|
printf("[T] %s\n", client.irc_msg);
|
|
|
|
else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
|
|
printf("[>] %s (%s)\n", client.irc_nick, client.irc_msg);
|
|
|
|
else if (client.irc_nick != NULL && irc_status == IRCC_QUIT)
|
|
printf("[<] %s (%s)\n", client.irc_nick, client.irc_msg);
|
|
|
|
else
|
|
printf("> %s\n", client.irc_raw);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
signal(SIGINT, handler);
|
|
|
|
int status = IRCC_connect(&client, HOST, PORT);
|
|
if (status < 0)
|
|
die("Conn refused");
|
|
|
|
if (IRCC_initssl(&client) < 0) {
|
|
IRCC_close(&client);
|
|
die("ssl error");
|
|
}
|
|
|
|
if (PASS) {
|
|
IRCC_send(&client,
|
|
IRCC_GET_CMD(IRCC_FMT_PASS),
|
|
PASS);
|
|
}
|
|
|
|
/* Registration */
|
|
sleep(1);
|
|
IRCC_send(&client,
|
|
IRCC_GET_CMD(IRCC_FMT_USER),
|
|
NICK,
|
|
NICK,
|
|
NICK,
|
|
NICK);
|
|
|
|
IRCC_send(&client,
|
|
IRCC_GET_CMD(IRCC_FMT_NICK),
|
|
NICK);
|
|
|
|
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
|
|
if (channels[i] == NULL)
|
|
break;
|
|
|
|
IRCC_send(&client,
|
|
IRCC_GET_CMD(IRCC_FMT_JOIN),
|
|
channels[i]);
|
|
}
|
|
|
|
recvinfo();
|
|
}
|