fixed
This commit is contained in:
parent
ea0f4d0651
commit
a620a5d2d0
3 changed files with 17 additions and 104 deletions
39
C/irc.c
39
C/irc.c
|
@ -18,7 +18,7 @@ int IRCC_urecv(IRCC_client *irc) {
|
|||
return recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
|
||||
}
|
||||
|
||||
int IRCC_usend(IRCC_client *irc, const char *msg, const int bytes) {
|
||||
int IRCC_usend(IRCC_client *irc, const char *msg, const size_t bytes) {
|
||||
#if defined(ENABLE_SSL) || defined(ENABLE_TLS)
|
||||
if (irc->irc_usingssl)
|
||||
return SSL_write(irc->irc_ssl, msg, bytes);
|
||||
|
@ -44,12 +44,16 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const int port) {
|
|||
return IRCC_ERROR;
|
||||
|
||||
struct timeval tv = {IRCC_PING_TIMEOUT, 0};
|
||||
if (setsockopt(irc->irc_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
||||
if (setsockopt(irc->irc_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
|
||||
close(irc->irc_socket);
|
||||
return IRCC_ERROR;
|
||||
}
|
||||
|
||||
int status = connect(irc->irc_socket, (struct sockaddr *)&client_str, sizeof(client_str));
|
||||
if (status == -1)
|
||||
if (status == -1) {
|
||||
close(irc->irc_socket);
|
||||
return IRCC_ERROR;
|
||||
}
|
||||
|
||||
irc->irc_alloc = 1;
|
||||
return IRCC_SUCCESS;
|
||||
|
@ -89,13 +93,13 @@ int IRCC_recv(IRCC_client *irc) {
|
|||
memset(irc->irc_raw, '\0', IRCC_MSG_MAX);
|
||||
|
||||
int msg_size = IRCC_urecv(irc);
|
||||
if (msg_size == 0 || msg_size == -1)
|
||||
if (msg_size <= 0)
|
||||
return IRCC_DISCONNECTED;
|
||||
|
||||
char *ptr;
|
||||
if ((ptr = strstr(irc->irc_raw, "PING :")) != NULL) {
|
||||
*(strchr(ptr, 'I')) = 'O';
|
||||
if (IRCC_usend(irc, ptr, strlen(irc->irc_raw)) == -1)
|
||||
if (IRCC_usend(irc, ptr, strlen(irc->irc_raw)) < 0)
|
||||
return IRCC_ERROR;
|
||||
}
|
||||
|
||||
|
@ -146,39 +150,28 @@ int IRCC_parse(IRCC_client *irc) {
|
|||
}
|
||||
|
||||
int IRCC_register(IRCC_client *irc, const char *nickname) {
|
||||
int bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) == -1)
|
||||
size_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) == -1)
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
/* Motd skip */
|
||||
while (1) {
|
||||
int status = IRCC_recv(irc);
|
||||
if (status == IRCC_DISCONNECTED)
|
||||
return IRCC_DISCONNECTED;
|
||||
|
||||
status = IRCC_parse(irc);
|
||||
if (status == IRCC_CONNECTED)
|
||||
break;
|
||||
}
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
|
||||
ssize_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) == -1)
|
||||
size_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
|
||||
ssize_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) == -1)
|
||||
size_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
|
||||
if (IRCC_usend(irc, irc->irc_raw, bytes) < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
|
|
2
C/irc.h
2
C/irc.h
|
@ -12,7 +12,7 @@
|
|||
|
||||
#define IRCC_MSG_MAX 512
|
||||
#define IRCC_PING_TIMEOUT 600
|
||||
#define IRCC_VERSION "2.0.1"
|
||||
#define IRCC_VERSION "2.0.2"
|
||||
|
||||
#if defined(ENABLE_SSL) || defined(ENABLE_TLS)
|
||||
#include <openssl/ssl.h>
|
||||
|
|
80
C/main.c
80
C/main.c
|
@ -1,80 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include "irc.h"
|
||||
|
||||
//Config
|
||||
#define HOST "irc.rizon.net"
|
||||
#define PORT 6667
|
||||
#define NICK "tester134"
|
||||
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 == IRCC_DISCONNECTED)
|
||||
die("Disconnected");
|
||||
|
||||
irc_status = IRCC_parse(&client);
|
||||
|
||||
//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\n", client.irc_nick);
|
||||
|
||||
else if (client.irc_nick != NULL && irc_status == IRCC_QUIT)
|
||||
printf("[<] %s\n", client.irc_nick);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
signal(SIGINT, handler);
|
||||
|
||||
int status = IRCC_connect(&client, HOST, PORT);
|
||||
if (status == IRCC_ERROR)
|
||||
die("Conn refused");
|
||||
|
||||
if (IRCC_initssl(&client) == IRCC_ERROR) {
|
||||
IRCC_close(&client);
|
||||
die("ssl error");
|
||||
}
|
||||
|
||||
//Register and skip motd
|
||||
IRCC_register(&client, NICK);
|
||||
|
||||
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();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue