added .gitignore
[NeonServV5.git] / ClientSocket.c
index 7ca76d2eedce63bcece32cd5c7dfa25ceabc52f9..78467a1c94b5d1d725d216ed4f57e261d6a4706e 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)
     {
@@ -36,6 +37,8 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     client->user = user;
     client->flags = 0;
     client->bufferpos = 0;
+       client->botid = 0;
+    client->clientid = 0;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -72,6 +75,9 @@ int connect_socket(struct ClientSocket *client) {
         return 0;
     }
 
+    client->sock = sock;
+    client->flags |= SOCKET_FLAG_CONNECTED;
+
     //send the IRC Headers
     char sendBuf[512];
     int len;
@@ -85,8 +91,6 @@ int connect_socket(struct ClientSocket *client) {
     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
     write_socket(client, sendBuf, len);
 
-    client->sock = sock;
-    client->flags |= SOCKET_FLAG_CONNECTED;
     return 1;
 }
 
@@ -108,6 +112,7 @@ int close_socket(struct ClientSocket *client) {
     free(client->host);
     free(client->pass);
     free(client);
+    return 1;
 }
 
 int write_socket(struct ClientSocket *client, char* msg, int len) {
@@ -127,7 +132,7 @@ void socket_loop(int timeout_seconds) {
     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;
     }
@@ -151,9 +156,10 @@ void socket_loop(int timeout_seconds) {
                 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, sock->buffer, sock->bufferpos);
                 if(used == sock->bufferpos + 1) {
@@ -170,3 +176,44 @@ void socket_loop(int timeout_seconds) {
     }
 }
 
+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((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;
+}