Загрузить файлы в «C»
This commit is contained in:
parent
569a6faea5
commit
a50ebb3d26
1
C/irc.c
1
C/irc.c
@ -31,6 +31,7 @@ unsigned int IRCC_register(IRCC_client *irc, const char *nickname){
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sleep(2);
|
||||||
snprintf(tmp, size, "NICK %s\r\nUSER %s 0 localhost :%s\r\n", nickname, nickname, nickname);
|
snprintf(tmp, size, "NICK %s\r\nUSER %s 0 localhost :%s\r\n", nickname, nickname, nickname);
|
||||||
send(irc->socket, tmp, strlen(tmp), 0);
|
send(irc->socket, tmp, strlen(tmp), 0);
|
||||||
|
|
||||||
|
1
C/irc.h
1
C/irc.h
@ -20,6 +20,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#define IRCC_MSG_MAX 512
|
#define IRCC_MSG_MAX 512
|
||||||
|
#define IRCC_PING_TIMEOUT 600
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
IRCC_NICK = 1,
|
IRCC_NICK = 1,
|
||||||
|
34
C/main.c
34
C/main.c
@ -1,10 +1,11 @@
|
|||||||
#include "irc.h"
|
#include "irc.h"
|
||||||
|
|
||||||
//Config
|
//Config
|
||||||
#define HOST "irc.server.net"
|
#define HOST "localhost"
|
||||||
#define PORT 6667
|
#define PORT 6667
|
||||||
#define NICK "tester"
|
#define NICK "tester134"
|
||||||
char *channels[] = {"#test"};
|
char *channels[] = {"#channel"};
|
||||||
|
|
||||||
|
|
||||||
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
|
#define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL)
|
||||||
IRCC_client client;
|
IRCC_client client;
|
||||||
@ -15,29 +16,23 @@ void die(char *msg) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void recvinfo(int ignore) {
|
void recvinfo(void) {
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned int irc_status = IRCC_recv(&client);
|
unsigned int irc_status = IRCC_recv(&client);
|
||||||
if (irc_status == IRCC_DISCONNECTED) {
|
if (irc_status == IRCC_DISCONNECTED)
|
||||||
if (!ignore)
|
die("Disconnected");
|
||||||
die("Disconnected");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (strstr(client.raw, "No Ident") || irc_status == IRCC_CONNECTED)
|
else if (irc_status == IRCC_CONNECTED)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//Print
|
//Print
|
||||||
if (!CHECK_NULL())
|
if (CHECK_NULL() && irc_status == IRCC_PRIVMSG)
|
||||||
return;
|
|
||||||
|
|
||||||
if (irc_status == IRCC_PRIVMSG)
|
|
||||||
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
|
printf("[%s %s] %s\n", client.channel, client.nick, client.msg);
|
||||||
|
|
||||||
else if (irc_status == IRCC_NICK)
|
else if (CHECK_NULL() && irc_status == IRCC_NICK)
|
||||||
printf("[N] %s is know as %s\n", client.nick, client.msg);
|
printf("[N] %s is know as %s\n", client.nick, client.msg);
|
||||||
|
|
||||||
else if (irc_status == IRCC_TOPIC)
|
else if (CHECK_NULL() && irc_status == IRCC_TOPIC)
|
||||||
printf("[T] %s\n", client.msg);
|
printf("[T] %s\n", client.msg);
|
||||||
|
|
||||||
else if (irc_status == IRCC_JOIN)
|
else if (irc_status == IRCC_JOIN)
|
||||||
@ -57,16 +52,15 @@ int main(void) {
|
|||||||
die("Conn refused");
|
die("Conn refused");
|
||||||
|
|
||||||
//Socket timeout
|
//Socket timeout
|
||||||
struct timeval tv = {5, 5};
|
struct timeval tv = {IRCC_PING_TIMEOUT, IRCC_PING_TIMEOUT};
|
||||||
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
||||||
die("setsockopt");
|
die("setsockopt");
|
||||||
|
|
||||||
//Register
|
//Register
|
||||||
recvinfo(1);
|
|
||||||
IRCC_register(&client, NICK);
|
IRCC_register(&client, NICK);
|
||||||
|
|
||||||
//Recv motd and join in channel
|
//Recv motd and join in channel
|
||||||
recvinfo(0);
|
recvinfo();
|
||||||
sleep(5);
|
sleep(5);
|
||||||
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
|
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
|
||||||
if (channels[i] == NULL)
|
if (channels[i] == NULL)
|
||||||
@ -82,6 +76,6 @@ int main(void) {
|
|||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
recvinfo(0);
|
recvinfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user