#include "irc.h"
-void processPacket(struct client_data *client) {
+int processPacket(struct client_data *client) {
char *packet = client->buf;
//size_t packetLen = client->bufLen;
if(strncmp(packet, "PING", 4) == 0) {
data[1+client->nickLen] = ' ';
strcpy(data+2+client->nickLen, packet);
sendall_log(client->FD, data, strlen(data), 0);
+ } else if(strncmp(packet, "QUIT", 4) == 0) {
+ killConnection(client);
+ return 1;
}
+ return 0;
}
void newUser(struct client_data *client) {
#include "common.h"
#include "net.h"
-void processPacket(struct client_data *client);
+int processPacket(struct client_data *client);
void newUser(struct client_data *client);
void sendMOTD(struct client_data *client);
void setNick(struct client_data *client, char* nickname, size_t size);
#include "net.h"
#include "irc.h"
+#include <stdbool.h>
void sendall_log(SOCKET socket, const char *buffer, size_t length, int flags) {
int i;
void killConnection(struct client_data *cl) {
int i;
char client_addr[100];
+ bool valid=false;
if(cl == 0) return;
+ for(i=0;i<CLIENTS;i++) {
+ if(clients[i] == cl) {
+ clients[i] = 0;
+ valid=true;
+ }
+ }
+ if(!valid){
+ return;
+ }
#ifdef SUPPORT_IPV4
if(cl->type == 4) {
getpeername(cl->FD,(struct sockaddr*)&serverAddrV4,(socklen_t*)&serverAddrV4len);
fflush(stdout);
}
#endif
- for(i=0;i<CLIENTS;i++) {
- if(clients[i] == cl) {
- clients[i] = 0;
- }
- }
+ shutdown(cl->FD, SHUT_RDWR);
closesocket(cl->FD);
free(cl);
}
ssize_t bytesRead;
char *x;
char tmp;
+ int packetReturn;
while(1){
// I hate you
if(cl->bufLen == cl->bufCap) { killConnection(cl); return; }
if(!x) break;
tmp = *(x+1);
*(x+1) = '\0';
- processPacket(cl);
+ packetReturn = processPacket(cl);
+ // Client QUIT
+ if(packetReturn == 1) {
+ return;
+ }
*(x+1) = tmp;
memmove(cl->buf, x+1, cl->buf + cl->bufLen-(x+1));
cl->bufLen -= x+1 - cl->buf;