changed Makefile; build all commands as an own file
[NeonServV5.git] / ClientSocket.c
index 002a633516f84713d7d480afc10dca972b28bed5..62e367d7b9ce9ca4d21458211348aed287b675e9 100644 (file)
@@ -1,6 +1,7 @@
 
 #include "ClientSocket.h"
-#include IRCParser.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)
     {
@@ -22,32 +23,39 @@ static void init() {
     sockets->count = 0;
 }
 
-struct ClientSocket* create_socket(char *host, int *port, char *pass, struct UserNode *user) {
-    if(sockets == NULL) init();
+struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
+    if(sockets == NULL) init_sockets();
     struct ClientSocket *client = malloc(sizeof(*client));
     if (!client)
     {
         perror("malloc() failed");
-        return;
+        return NULL;
     }
     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;
 }
 
-int connect_socket(struct ClientSocket *socket) {
-    if((socket->flsgs & SOCKET_FLAG_CONNECTED)) return 1;
+int connect_socket(struct ClientSocket *client) {
+    if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
     struct hostent *host;
     struct sockaddr_in addr;
     int sock;
-    if (!inet_aton(socket->host, &addr.sin_addr))
+    if (!inet_aton(client->host, &addr.sin_addr))
     {
-        host = gethostbyname(socket->host);
+        host = gethostbyname(client->host);
         if (!host)
         {
             herror("gethostbyname() failed");
@@ -62,7 +70,7 @@ int connect_socket(struct ClientSocket *socket) {
         return 0;
     }
 
-    addr.sin_port = htons(socket->port);
+    addr.sin_port = htons(client->port);
     addr.sin_family = AF_INET;
 
     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
@@ -71,31 +79,33 @@ int connect_socket(struct ClientSocket *socket) {
         return 0;
     }
 
+    client->sock = sock;
+    client->flags |= SOCKET_FLAG_CONNECTED;
+    client->connection_time = time(0);
+
     //send the IRC Headers
     char sendBuf[512];
     int len;
 
-    if(socket->pass) {
-        len = sprintf(sendBuf, "PASS :%s\n", socket->pass);
-        write_socket(sock, sendBuf, len);
+    if(client->pass) {
+        len = sprintf(sendBuf, "PASS :%s\n", client->pass);
+        write_socket(client, sendBuf, len);
     }
-    len = sprintf(sendBuf, "USER %s 0 0 :%s\n", socket->user->ident, socket->user->realname);
-    write_socket(sock, sendBuf, len);
-    len = sprintf(sendBuf, "NICK %s\n", socket->user->nick);
-    write_socket(sock, sendBuf, len);
+    len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
+    write_socket(client, sendBuf, len);
+    len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
+    write_socket(client, sendBuf, len);
 
-    socket->socket = sock;
-    socket->flags |= SOCKET_FLAG_CONNECTED;
     return 1;
 }
 
-int close_socket(struct ClientSocket *socket) {
-    if(socket == NULL) return 0;
-    if((socket->flags & SOCKET_FLAG_CONNECTED))
-        close(socket->sock);
+int close_socket(struct ClientSocket *client) {
+    if(client == NULL) return 0;
+    if((client->flags & SOCKET_FLAG_CONNECTED))
+        close(client->sock);
     struct ClientSocket *sock, *last_sock = NULL;
     for (sock = sockets->data; sock; sock = sock->next) {
-        if(sock == socket) {
+        if(sock == client) {
             if(last_sock)
                 last_sock->next = sock->next;
             else
@@ -104,19 +114,21 @@ int close_socket(struct ClientSocket *socket) {
         } else
             last_sock = sock;
     }
-    free(socket->host);
-    free(socket->pass);
-    free(socket);
+    free(client->host);
+    free(client->pass);
+    free(client);
+    return 1;
 }
 
-int write_socket(struct ClientSocket *socket, char* msg, int len) {
-    if(!(socket->flags & SOCKET_FLAG_CONNECTED)) return 0;
+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(socket->sock, msg, len);
+    write(client->sock, msg, len);
+    client->traffic_out += len;
     return 1;
 }
 
-void socket_loop(int timeout) {
+void socket_loop(int timeout_seconds) {
     if(sockets == NULL) return;
     fd_set fds;
     struct timeval timeout;
@@ -126,11 +138,11 @@ void socket_loop(int timeout) {
     FD_ZERO(&fds);
     for (sock = sockets->data; sock; sock = sock->next) {
         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
-        FD_SET(sock->sock, fds);
+        FD_SET(sock->sock, &fds);
         if(sock->sock > ret)
             ret = sock->sock;
     }
-    timeout.tv_sec = timeout;
+    timeout.tv_sec = timeout_seconds;
     timeout.tv_usec = 0;
     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
     if(ret == 0) return;
@@ -150,11 +162,13 @@ void socket_loop(int timeout) {
                 if(bytes > 0)
                     sock->bufferpos = bytes;
             }
-            if(bytes >= 0) {
+            if(bytes <= 0) {
                 //error
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
+                bot_disconnect(sock);
             } else {
-                int used = parse_lines(sock->buffer, sock->bufferpos);
+                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
                     sock->bufferpos = 0;
@@ -169,3 +183,44 @@ void socket_loop(int timeout) {
     }
 }
 
+void
+putsock(struct ClientSocket *client, const char *text, ...)
+{
+    va_list arg_list;
+    char sendBuf[MAXLEN];
+    int pos;
+    if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
+    sendBuf[0] = '\0';
+    va_start(arg_list, text);
+    pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
+    va_end(arg_list);
+    if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
+    sendBuf[pos] = '\n';
+    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;
+}