Upload files to "C"
This commit is contained in:
parent
6a1d2cf850
commit
b4825a3fb7
147
C/irc.c
147
C/irc.c
@ -9,7 +9,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include "irc.h"
|
||||
|
||||
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){
|
||||
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) {
|
||||
struct hostent *hp = gethostbyname(ip);
|
||||
if (!hp)
|
||||
return IRCC_ERROR;
|
||||
@ -21,29 +21,29 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port){
|
||||
client_str.sin_port = htons(port);
|
||||
client_str.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)hp->h_addr));
|
||||
|
||||
irc->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (irc->socket < 0)
|
||||
irc->irc_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (irc->irc_socket < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
struct timeval tv = {IRCC_PING_TIMEOUT, 0};
|
||||
if (setsockopt(irc->socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
||||
if (setsockopt(irc->irc_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
||||
return IRCC_ERROR;
|
||||
|
||||
int status = connect(irc->socket, (struct sockaddr *)&client_str, sizeof(client_str));
|
||||
int status = connect(irc->irc_socket, (struct sockaddr *)&client_str, sizeof(client_str));
|
||||
if (status == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
void IRCC_parse(char *tmp, IRCC_client *irc){
|
||||
irc->raw[strcspn(irc->raw, "\r\n")] = '\0';
|
||||
if (tmp != NULL){
|
||||
void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
|
||||
irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0';
|
||||
if (tmp != NULL) {
|
||||
//Message
|
||||
char *val = strchr(tmp, ':');
|
||||
if (val != NULL) {
|
||||
val[0] = '\0';
|
||||
irc->msg = val + 1;
|
||||
irc->irc_msg = val + 1;
|
||||
|
||||
//Del space before :
|
||||
*(val - 1) = '\0';
|
||||
@ -53,126 +53,125 @@ void IRCC_parse(char *tmp, IRCC_client *irc){
|
||||
val = strchr(tmp, ' ');
|
||||
if (val != NULL) {
|
||||
val[0] = '\0';
|
||||
irc->channel = val + 1;
|
||||
irc->irc_channel = val + 1;
|
||||
}
|
||||
|
||||
//Nickname
|
||||
val = strchr(irc->raw, '!');
|
||||
val = strchr(irc->irc_raw, '!');
|
||||
if (val != NULL) {
|
||||
val[0] = '\0';
|
||||
irc->nick = irc->raw + 1;
|
||||
irc->irc_nick = irc->irc_raw + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int IRCC_recv(IRCC_client *irc){
|
||||
memset(irc->raw, '\0', IRCC_MSG_MAX);
|
||||
irc->channel = NULL;
|
||||
irc->msg = NULL;
|
||||
irc->nick = NULL;
|
||||
int IRCC_recv(IRCC_client *irc) {
|
||||
memset(irc->irc_raw, '\0', IRCC_MSG_MAX);
|
||||
irc->irc_channel = NULL;
|
||||
irc->irc_msg = NULL;
|
||||
irc->irc_nick = NULL;
|
||||
|
||||
int msg_size = recv(irc->socket, irc->raw, sizeof(irc->raw), 0);
|
||||
int msg_size = recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
|
||||
if (msg_size == 0 || msg_size == -1)
|
||||
return IRCC_DISCONNECTED;
|
||||
|
||||
else if (!strncmp(irc->raw, "PING", 4)){
|
||||
*(strchr(irc->raw, 'I')) = 'O';
|
||||
if (send(irc->socket, irc->raw, strlen(irc->raw), 0) == -1)
|
||||
else if (!strncmp(irc->irc_raw, "PING", 4)) {
|
||||
*(strchr(irc->irc_raw, 'I')) = 'O';
|
||||
if (send(irc->irc_socket, irc->irc_raw, strlen(irc->irc_raw), 0) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_PING;
|
||||
}
|
||||
|
||||
else {
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
#ifdef IRCC_DEBUG
|
||||
puts(irc->raw);
|
||||
#endif
|
||||
//Check end of motd
|
||||
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
|
||||
return IRCC_CONNECTED;
|
||||
int IRCC_parse(IRCC_client *irc) {
|
||||
|
||||
//Other
|
||||
else if (strstr(irc->raw, "PRIVMSG ")){
|
||||
IRCC_parse(strstr(irc->raw, "PRIVMSG "), irc);
|
||||
return IRCC_PRIVMSG;
|
||||
}
|
||||
//Check end of motd
|
||||
if (strstr(irc->irc_raw, "PRIVMSG ") == NULL && strstr(irc->irc_raw, "MOTD"))
|
||||
return IRCC_CONNECTED;
|
||||
|
||||
else if (strstr(irc->raw, "NICK ")){
|
||||
IRCC_parse(strstr(irc->raw, "NICK "), irc);
|
||||
return IRCC_NICK;
|
||||
}
|
||||
//Other
|
||||
else if (strstr(irc->irc_raw, "PRIVMSG ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "PRIVMSG "), irc);
|
||||
return IRCC_PRIVMSG;
|
||||
}
|
||||
|
||||
else if (strstr(irc->raw, "TOPIC ")){
|
||||
IRCC_parse(strstr(irc->raw, "TOPIC "), irc);
|
||||
return IRCC_TOPIC;
|
||||
}
|
||||
else if (strstr(irc->irc_raw, "NICK ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "NICK "), irc);
|
||||
return IRCC_NICK;
|
||||
}
|
||||
|
||||
else if (strstr(irc->raw, "MODE ")){
|
||||
IRCC_parse(strstr(irc->raw, "MODE "), irc);
|
||||
return IRCC_MODE;
|
||||
}
|
||||
else if (strstr(irc->irc_raw, "TOPIC ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "TOPIC "), irc);
|
||||
return IRCC_TOPIC;
|
||||
}
|
||||
|
||||
else if (strstr(irc->raw, "JOIN ")){
|
||||
IRCC_parse(strstr(irc->raw, "JOIN "), irc);
|
||||
return IRCC_JOIN;
|
||||
}
|
||||
else if (strstr(irc->irc_raw, "MODE ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "MODE "), irc);
|
||||
return IRCC_MODE;
|
||||
}
|
||||
|
||||
else if (strstr(irc->raw, "PART ") || strstr(irc->raw, "QUIT ")){
|
||||
IRCC_parse(strstr(irc->raw, "PART "), irc);
|
||||
IRCC_parse(strstr(irc->raw, "QUIT "), irc);
|
||||
return IRCC_PART;
|
||||
}
|
||||
else if (strstr(irc->irc_raw, "JOIN ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "JOIN "), irc);
|
||||
return IRCC_JOIN;
|
||||
}
|
||||
|
||||
else if (strstr(irc->irc_raw, "PART ") || strstr(irc->irc_raw, "QUIT ")) {
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "PART "), irc);
|
||||
IRCC_parse_msg(strstr(irc->irc_raw, "QUIT "), irc);
|
||||
return IRCC_PART;
|
||||
}
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
int IRCC_register(IRCC_client *irc, const char *nickname){
|
||||
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "NICK %s\r\n", nickname);
|
||||
if (send(irc->socket, irc->raw, bytes, 0) == -1)
|
||||
int IRCC_register(IRCC_client *irc, const char *nickname) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
bytes = snprintf(irc->raw, sizeof(irc->raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
|
||||
if (send(irc->socket, irc->raw, bytes, 0) == -1)
|
||||
bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
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;
|
||||
|
||||
else if (status == IRCC_DISCONNECTED)
|
||||
return IRCC_DISCONNECTED;
|
||||
}
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
int IRCC_join(IRCC_client *irc, const char *channel, const char *key){
|
||||
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
|
||||
if (send(irc->socket, irc->raw, bytes, 0) == -1)
|
||||
int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg){
|
||||
off_t bytes = snprintf(irc->raw, sizeof(irc->raw), "PRIVMSG %s :%s\r\n", channel, msg);
|
||||
if (send(irc->socket, irc->raw, bytes, 0) == -1)
|
||||
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
int IRCC_init(IRCC_client *irc) {
|
||||
irc->msg = irc->nick = irc->channel = NULL;
|
||||
return IRCC_SUCCESS;
|
||||
void IRCC_init(IRCC_client *irc) {
|
||||
irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
|
||||
}
|
||||
|
||||
void IRCC_close(IRCC_client *irc){
|
||||
close(irc->socket);
|
||||
irc->msg = irc->nick = irc->channel = NULL;
|
||||
void IRCC_close(IRCC_client *irc) {
|
||||
close(irc->irc_socket);
|
||||
irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
|
||||
}
|
||||
|
13
C/irc.h
13
C/irc.h
@ -27,18 +27,19 @@ enum {
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int socket;
|
||||
int irc_socket;
|
||||
|
||||
char raw[IRCC_MSG_MAX + 1];
|
||||
char *msg;
|
||||
char *channel;
|
||||
char *nick;
|
||||
char irc_raw[IRCC_MSG_MAX + 1];
|
||||
char *irc_msg;
|
||||
char *irc_channel;
|
||||
char *irc_nick;
|
||||
} IRCC_client;
|
||||
|
||||
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port);
|
||||
int IRCC_recv(IRCC_client *irc);
|
||||
int IRCC_parse(IRCC_client *irc);
|
||||
int IRCC_join(IRCC_client *irc, const char *channel, const char *key);
|
||||
int IRCC_send(IRCC_client *irc, const char *msg, const char *channel);
|
||||
int IRCC_register(IRCC_client *irc, const char *nickname);
|
||||
int IRCC_init(IRCC_client *irc);
|
||||
void IRCC_init(IRCC_client *irc);
|
||||
void IRCC_close(IRCC_client *irc);
|
||||
|
24
C/main.c
24
C/main.c
@ -5,13 +5,13 @@
|
||||
#include "irc.h"
|
||||
|
||||
//Config
|
||||
#define HOST "irc.rizon.net"
|
||||
#define HOST "192.168.0.184"
|
||||
#define PORT 6667
|
||||
#define NICK "tester134"
|
||||
char *channels[] = {"#hp"};
|
||||
char *channels[] = {"#test"};
|
||||
|
||||
|
||||
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
|
||||
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
|
||||
IRCC_client client;
|
||||
|
||||
void die(char *msg) {
|
||||
@ -26,21 +26,23 @@ void recvinfo(void) {
|
||||
if (irc_status == IRCC_DISCONNECTED)
|
||||
die("Disconnected");
|
||||
|
||||
irc_status = IRCC_parse(&client);
|
||||
|
||||
//Print
|
||||
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
|
||||
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
|
||||
printf("[%s %s] %s\n", client.irc_channel, client.irc_nick, client.irc_msg);
|
||||
|
||||
else if (CHECK_NULL() && irc_status == IRCC_NICK)
|
||||
printf("[N] %s is know as %s\n", client.nick, client.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.msg);
|
||||
printf("[T] %s\n", client.irc_msg);
|
||||
|
||||
else if (client.nick != NULL && irc_status == IRCC_JOIN)
|
||||
printf("[>] %s\n", client.nick);
|
||||
else if (client.irc_nick != NULL && irc_status == IRCC_JOIN)
|
||||
printf("[>] %s\n", client.irc_nick);
|
||||
|
||||
else if (client.nick != NULL && irc_status == IRCC_PART)
|
||||
printf("[<] %s\n", client.nick);
|
||||
else if (client.irc_nick != NULL && irc_status == IRCC_PART)
|
||||
printf("[<] %s\n", client.irc_nick);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user