*** VERSION 5.2.0 ***
[NeonServV5.git] / src / ClientSocket.c
index 30c2efb4ec7e2d9c7cf69280fc47719720a36884..2c77b080207e1eb607bda3eafbc241c8f6e672d0 100644 (file)
@@ -1,7 +1,24 @@
+/* ClientSocket.c - NeonServ v5.2
+ * Copyright (C) 2011  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
 
 #include "ClientSocket.h"
 #include "IRCParser.h"
 #include "UserNode.h"
+#include "IRCQueue.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -43,6 +60,7 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     client->connection_time = 0;
        client->botid = 0;
     client->clientid = 0;
+    client->queue = NULL;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -80,7 +98,7 @@ int connect_socket(struct ClientSocket *client) {
     }
 
     client->sock = sock;
-    client->flags |= SOCKET_FLAG_CONNECTED;
+    client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
     client->connection_time = time(0);
 
     //send the IRC Headers
@@ -133,14 +151,14 @@ int connect_socket(struct ClientSocket *client) {
     }
 
     client->sock = sock;
-    client->flags |= SOCKET_FLAG_CONNECTED;
+    client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
     client->connection_time = time(0);
 
     //send the IRC Headers
     char sendBuf[512];
     int len;
 
-    if(client->pass) {
+    if(client->pass && strcmp(client->pass, "")) {
         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
         write_socket(client, sendBuf, len);
     }
@@ -169,14 +187,15 @@ int close_socket(struct ClientSocket *client) {
         } else
             last_sock = sock;
     }
+    if(client->queue)
+        queue_destroy(client);
     free(client->host);
     free(client->pass);
     free(client);
     return 1;
 }
 
-int write_socket(struct ClientSocket *client, char* msg, int len) {
-    if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
+int write_socket_force(struct ClientSocket *client, char* msg, int len) {
     printf("[send %d] %s", len, msg);
     #ifdef WIN32
     send(client->sock, msg, len, 0);
@@ -187,6 +206,14 @@ int write_socket(struct ClientSocket *client, char* msg, int len) {
     return 1;
 }
 
+int write_socket(struct ClientSocket *client, char* msg, int len) {
+    if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
+    if(client->flags & SOCKET_FLAG_USE_QUEUE)
+        return queue_add(client, msg, len);
+    else
+        return write_socket_force(client, msg, len);
+}
+
 void socket_loop(int timeout_seconds) {
     if(sockets == NULL) return;
     fd_set fds;
@@ -233,6 +260,8 @@ void socket_loop(int timeout_seconds) {
                 //error
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
                 bot_disconnect(sock);
+                if(sock->queue)
+                    queue_destroy(sock);
             } else {
                 sock->traffic_in += bytes;
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
@@ -246,6 +275,10 @@ void socket_loop(int timeout_seconds) {
                     sock->bufferpos -= used;
                 }
             }
+        } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
+            if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
+                connect_socket(sock);
+            }
         }
     }
 }
@@ -284,6 +317,8 @@ void free_sockets() {
         next = client->next;
         if((client->flags & SOCKET_FLAG_CONNECTED))
             close(client->sock);
+        if(client->queue)
+            queue_destroy(client);
         free(client->host);
         free(client->pass);
         free(client);