Загрузить файлы в «C»
This commit is contained in:
parent
ad09f6fba5
commit
a31b8a5bab
16
C/irc.c
16
C/irc.c
@ -74,7 +74,7 @@ unsigned int IRCC_recv(IRCC_client *irc){
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
//puts(irc->raw);
|
puts(irc->raw);
|
||||||
|
|
||||||
//Check end of motd
|
//Check end of motd
|
||||||
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
|
if (strstr(irc->raw, "PRIVMSG ") == NULL && strstr(irc->raw, "MOTD"))
|
||||||
@ -91,6 +91,16 @@ unsigned int IRCC_recv(IRCC_client *irc){
|
|||||||
return IRCC_NICK;
|
return IRCC_NICK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (strstr(irc->raw, "TOPIC ")){
|
||||||
|
IRCC_parse(strstr(irc->raw, "TOPIC "), irc);
|
||||||
|
return IRCC_TOPIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (strstr(irc->raw, "MODE ")){
|
||||||
|
IRCC_parse(strstr(irc->raw, "MODE "), irc);
|
||||||
|
return IRCC_MODE;
|
||||||
|
}
|
||||||
|
|
||||||
else if (strstr(irc->raw, "JOIN ")){
|
else if (strstr(irc->raw, "JOIN ")){
|
||||||
IRCC_parse(strstr(irc->raw, "JOIN "), irc);
|
IRCC_parse(strstr(irc->raw, "JOIN "), irc);
|
||||||
return IRCC_JOIN;
|
return IRCC_JOIN;
|
||||||
@ -123,8 +133,8 @@ void IRCC_send(IRCC_client *irc, char *msg, char *channel){
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int IRCC_init(IRCC_client *irc, size_t size){
|
unsigned int IRCC_init(IRCC_client *irc, size_t size){
|
||||||
if (size <= 200){
|
if (size <= 250){
|
||||||
fprintf(stderr, "Low buffer size (IRCC_init)\n");
|
fprintf(stderr, "Low buffer size (IRCC_init) (Min 250/512)\n");
|
||||||
return IRCC_ERROR;
|
return IRCC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
C/irc.h
18
C/irc.h
@ -1,3 +1,13 @@
|
|||||||
|
/*
|
||||||
|
LICENSE: MIT
|
||||||
|
Simple libary for working with ipv4 irc servers
|
||||||
|
Dont support:
|
||||||
|
ssl
|
||||||
|
ddc
|
||||||
|
ipv6
|
||||||
|
motd parsing
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef IRC_LINUX
|
#ifndef IRC_LINUX
|
||||||
#define IRC_LINUX
|
#define IRC_LINUX
|
||||||
|
|
||||||
@ -9,14 +19,18 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#define IRCC_MSG_MAX 512
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
IRCC_CONNECTED = 1,
|
IRCC_NICK = 1,
|
||||||
IRCC_NICK,
|
|
||||||
IRCC_PRIVMSG,
|
IRCC_PRIVMSG,
|
||||||
IRCC_JOIN,
|
IRCC_JOIN,
|
||||||
IRCC_PART,
|
IRCC_PART,
|
||||||
IRCC_PING,
|
IRCC_PING,
|
||||||
|
IRCC_TOPIC,
|
||||||
|
IRCC_MODE,
|
||||||
|
|
||||||
|
IRCC_CONNECTED,
|
||||||
IRCC_DISCONNECTED,
|
IRCC_DISCONNECTED,
|
||||||
IRCC_ERROR,
|
IRCC_ERROR,
|
||||||
IRCC_SUCCESS
|
IRCC_SUCCESS
|
||||||
|
102
C/main.c
102
C/main.c
@ -1,51 +1,87 @@
|
|||||||
#include "irc.h"
|
#include "irc.h"
|
||||||
#define IF_NULL(client) (client.msg != NULL || client.nick != NULL || client.channel != NULL)
|
|
||||||
|
|
||||||
|
//Config
|
||||||
|
#define HOST "irc.server.net"
|
||||||
|
#define PORT 6667
|
||||||
|
#define NICK "tester"
|
||||||
|
char *channels[] = {"#test"};
|
||||||
|
|
||||||
|
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
|
||||||
IRCC_client client;
|
IRCC_client client;
|
||||||
|
|
||||||
void die(char *msg){
|
void die(char *msg) {
|
||||||
puts(msg);
|
puts(msg);
|
||||||
IRCC_close(&client);
|
IRCC_close(&client);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
void recvinfo(int ignore) {
|
||||||
IRCC_init(&client, 250);
|
while (1) {
|
||||||
int status = IRCC_connect(&client, "irc.libera.chat", 6667);
|
unsigned int irc_status = IRCC_recv(&client);
|
||||||
|
if (irc_status == IRCC_DISCONNECTED) {
|
||||||
|
if (!ignore)
|
||||||
|
die("Disconnected");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (strstr(client.raw, "No Ident") || irc_status == IRCC_CONNECTED)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Print
|
||||||
|
if (!CHECK_NULL())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (irc_status == IRCC_PRIVMSG)
|
||||||
|
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
|
||||||
|
|
||||||
|
else if (irc_status == IRCC_NICK)
|
||||||
|
printf("[N] %s is know as %s\n", client.nick, client.msg);
|
||||||
|
|
||||||
|
else if (irc_status == IRCC_TOPIC)
|
||||||
|
printf("[T] %s\n", client.msg);
|
||||||
|
|
||||||
|
else if (irc_status == IRCC_JOIN)
|
||||||
|
printf("[>] %s\n", client.nick);
|
||||||
|
|
||||||
|
else if (irc_status == IRCC_PART)
|
||||||
|
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);
|
||||||
if (status == IRCC_ERROR)
|
if (status == IRCC_ERROR)
|
||||||
die("Conn refused");
|
die("Conn refused");
|
||||||
|
|
||||||
IRCC_register(&client, "bot");
|
//Socket timeout
|
||||||
|
struct timeval tv = {5, 5};
|
||||||
|
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
||||||
|
die("setsockopt");
|
||||||
|
|
||||||
|
//Register
|
||||||
|
recvinfo(1);
|
||||||
|
IRCC_register(&client, NICK);
|
||||||
|
|
||||||
|
//Recv motd and join in channel
|
||||||
|
recvinfo(0);
|
||||||
sleep(5);
|
sleep(5);
|
||||||
send(client.socket, "JOIN #channel\r\n", strlen("JOIN #channel\r\n"), 0);
|
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
|
||||||
|
if (channels[i] == NULL)
|
||||||
while (1){
|
|
||||||
unsigned int irc_status = IRCC_recv(&client);
|
|
||||||
if (irc_status == IRCC_DISCONNECTED)
|
|
||||||
die("Disconnected");
|
|
||||||
|
|
||||||
if (IF_NULL(client)) {
|
|
||||||
client.msg++;
|
|
||||||
switch (irc_status) {
|
|
||||||
case IRCC_PRIVMSG:
|
|
||||||
printf("%s %s %s\n", client.channel, client.nick, client.msg);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IRCC_NICK:
|
size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1;
|
||||||
printf("%s is know as %s", client.nick, client.msg);
|
char *tmp = malloc(join_size);
|
||||||
break;
|
if (tmp == NULL)
|
||||||
|
die("malloc retured NULL");
|
||||||
|
|
||||||
case IRCC_JOIN:
|
snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]);
|
||||||
printf("< %s\n", client.nick);
|
send(client.socket, tmp, join_size, 0);
|
||||||
break;
|
free(tmp);
|
||||||
|
|
||||||
case IRCC_PART:
|
|
||||||
printf("> %s\n", client.nick);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recvinfo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user