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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue