*** VERSION 5.2.0 ***
[NeonServV5.git] / src / ClientSocket.c
index 62e367d7b9ce9ca4d21458211348aed287b675e9..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;
@@ -33,7 +50,7 @@ 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);
+    printf("Connect: %s:%d\n", client->host, client->port);
     client->pass = (pass == NULL ? NULL : strdup(pass));
     client->user = user;
     client->flags = 0;
@@ -43,11 +60,65 @@ 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;
 }
 
+#ifdef WIN32
+
+int connect_socket(struct ClientSocket *client) {
+    if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
+    struct hostent *host;
+    struct sockaddr_in addr;
+    int sock;
+    addr.sin_addr.s_addr = inet_addr(client->host);
+    if (addr.sin_addr.s_addr == INADDR_NONE) {
+        host = gethostbyname(client->host);
+        if(!host) {
+            return SOCKET_ERROR;
+        }
+        memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
+    }
+    sock = socket(PF_INET, SOCK_STREAM, 0);
+    if (sock == -1)
+    {
+        perror("socket() failed");
+        return 0;
+    }
+
+    addr.sin_port = htons(client->port);
+    addr.sin_family = AF_INET;
+
+    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
+    {
+        perror("connect() failed");
+        return 0;
+    }
+
+    client->sock = sock;
+    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 && strcmp(client->pass, "")) {
+        len = sprintf(sendBuf, "PASS :%s\n", client->pass);
+        write_socket(client, 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);
+
+    return 1;
+}
+
+#else
+
 int connect_socket(struct ClientSocket *client) {
     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
     struct hostent *host;
@@ -58,7 +129,7 @@ int connect_socket(struct ClientSocket *client) {
         host = gethostbyname(client->host);
         if (!host)
         {
-            herror("gethostbyname() failed");
+            perror("gethostbyname() failed");
             return 0;
         }
         addr.sin_addr = *(struct in_addr*)host->h_addr;
@@ -80,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);
     }
@@ -99,6 +170,8 @@ int connect_socket(struct ClientSocket *client) {
     return 1;
 }
 
+#endif
+
 int close_socket(struct ClientSocket *client) {
     if(client == NULL) return 0;
     if((client->flags & SOCKET_FLAG_CONNECTED))
@@ -114,20 +187,33 @@ 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);
+    #else
     write(client->sock, msg, len);
+    #endif
     client->traffic_out += 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;
@@ -149,7 +235,11 @@ void socket_loop(int timeout_seconds) {
     for (sock = sockets->data; sock; sock = sock->next) {
         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
             if(sock->bufferpos != 0) {
+                #ifdef WIN32
+                bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
+                #else
                 bytes = read(sock->sock, buffer, sizeof(buffer));
+                #endif
                 if(bytes > 0) {
                     for(i = 0; i < bytes; i++) {
                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
@@ -158,7 +248,11 @@ void socket_loop(int timeout_seconds) {
                     sock->bufferpos += i;
                 }
             } else {
+                #ifdef WIN32
+                bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
+                #else
                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
+                #endif
                 if(bytes > 0)
                     sock->bufferpos = bytes;
             }
@@ -166,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);
@@ -179,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);
+            }
         }
     }
 }
@@ -217,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);