Загрузить файлы в «C»

This commit is contained in:
8nlight 2023-08-16 10:53:53 +03:00
parent ad09f6fba5
commit a31b8a5bab
3 changed files with 99 additions and 39 deletions

16
C/irc.c
View File

@ -74,7 +74,7 @@ unsigned int IRCC_recv(IRCC_client *irc){
}
else {
//puts(irc->raw);
puts(irc->raw);
//Check end of 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;
}
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 ")){
IRCC_parse(strstr(irc->raw, "JOIN "), irc);
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){
if (size <= 200){
fprintf(stderr, "Low buffer size (IRCC_init)\n");
if (size <= 250){
fprintf(stderr, "Low buffer size (IRCC_init) (Min 250/512)\n");
return IRCC_ERROR;
}

18
C/irc.h
View File

@ -1,3 +1,13 @@
/*
LICENSE: MIT
Simple libary for working with ipv4 irc servers
Dont support:
ssl
ddc
ipv6
motd parsing
*/
#ifndef IRC_LINUX
#define IRC_LINUX
@ -9,14 +19,18 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define IRCC_MSG_MAX 512
enum {
IRCC_CONNECTED = 1,
IRCC_NICK,
IRCC_NICK = 1,
IRCC_PRIVMSG,
IRCC_JOIN,
IRCC_PART,
IRCC_PING,
IRCC_TOPIC,
IRCC_MODE,
IRCC_CONNECTED,
IRCC_DISCONNECTED,
IRCC_ERROR,
IRCC_SUCCESS

102
C/main.c
View File

@ -1,51 +1,87 @@
#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;
void die(char *msg){
void die(char *msg) {
puts(msg);
IRCC_close(&client);
exit(1);
}
int main(void){
IRCC_init(&client, 250);
int status = IRCC_connect(&client, "irc.libera.chat", 6667);
void recvinfo(int ignore) {
while (1) {
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)
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);
send(client.socket, "JOIN #channel\r\n", strlen("JOIN #channel\r\n"), 0);
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);
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
if (channels[i] == NULL)
break;
case IRCC_NICK:
printf("%s is know as %s", client.nick, client.msg);
break;
size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1;
char *tmp = malloc(join_size);
if (tmp == NULL)
die("malloc retured NULL");
case IRCC_JOIN:
printf("< %s\n", client.nick);
break;
case IRCC_PART:
printf("> %s\n", client.nick);
break;
default:
break;
}
}
snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]);
send(client.socket, tmp, join_size, 0);
free(tmp);
}
recvinfo(0);
}