changed Makefile; build all commands as an own file
[NeonServV5.git] / ClientSocket.c
index c071e1cfbd31db235a34a39037d8694dbe16d089..62e367d7b9ce9ca4d21458211348aed287b675e9 100644 (file)
@@ -1,6 +1,7 @@
 
 #include "ClientSocket.h"
 #include "IRCParser.h"
+#include "UserNode.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -11,7 +12,7 @@ struct socket_list {
 static struct socket_list *sockets = NULL;
 static char buffer[BUF_SIZ];
 
-static void init() {
+static void init_sockets() {
     sockets = malloc(sizeof(*sockets));
     if (!sockets)
     {
@@ -23,7 +24,7 @@ static void init() {
 }
 
 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
-    if(sockets == NULL) init();
+    if(sockets == NULL) init_sockets();
     struct ClientSocket *client = malloc(sizeof(*client));
     if (!client)
     {
@@ -32,10 +33,16 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     }
     client->host = strdup(host);
     client->port = port;
+    printf("Connect: %s:%d", client->host, client->port);
     client->pass = (pass == NULL ? NULL : strdup(pass));
     client->user = user;
     client->flags = 0;
     client->bufferpos = 0;
+    client->traffic_in = 0;
+    client->traffic_out = 0;
+    client->connection_time = 0;
+       client->botid = 0;
+    client->clientid = 0;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -74,6 +81,7 @@ int connect_socket(struct ClientSocket *client) {
 
     client->sock = sock;
     client->flags |= SOCKET_FLAG_CONNECTED;
+    client->connection_time = time(0);
 
     //send the IRC Headers
     char sendBuf[512];
@@ -116,6 +124,7 @@ int write_socket(struct ClientSocket *client, char* msg, int len) {
     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
     printf("[send %d] %s", len, msg);
     write(client->sock, msg, len);
+    client->traffic_out += len;
     return 1;
 }
 
@@ -147,20 +156,18 @@ void socket_loop(int timeout_seconds) {
                         sock->buffer[sock->bufferpos + i] = buffer[i];
                     }
                     sock->bufferpos += i;
-                    sock->buffer[sock->bufferpos] = 0; //debug only
-                    printf("BUFFER: %s\n", sock->buffer);
                 }
             } else {
                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
-                sock->buffer[bytes] = 0; //debug only
-                printf("BUFFER: %s\n", sock->buffer);
                 if(bytes > 0)
                     sock->bufferpos = bytes;
             }
             if(bytes <= 0) {
                 //error
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
+                bot_disconnect(sock);
             } else {
+                sock->traffic_in += bytes;
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
                 if(used == sock->bufferpos + 1) {
                     //used all bytes so just reset the bufferpos
@@ -189,7 +196,31 @@ putsock(struct ClientSocket *client, const char *text, ...)
     va_end(arg_list);
     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
     sendBuf[pos] = '\n';
-    sendBuf[pos+1] = '0';
+    sendBuf[pos+1] = '\0';
     write_socket(client, sendBuf, pos+1);
 }
 
+struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
+    struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
+    if(sock == NULL) return NULL;
+    for (; sock; sock = sock->next) {
+        if(!flags || (sock->flags & flags) == flags)
+            return sock;
+    }
+    return NULL;
+}
+
+void free_sockets() {
+    if(!sockets) return;
+    struct ClientSocket *client, *next;
+    for (client = sockets->data; client; client = next) {
+        next = client->next;
+        if((client->flags & SOCKET_FLAG_CONNECTED))
+            close(client->sock);
+        free(client->host);
+        free(client->pass);
+        free(client);
+    }
+    free(sockets);
+    sockets = NULL;
+}