increased MAXLANGUAGES definition
[NeonServV5.git] / src / ClientSocket.c
index f9faf44807eaf8b7f896704409ef8dc2e529e738..a624a305498337f8f2ff690144e78df2005a51c1 100644 (file)
@@ -1,5 +1,5 @@
-/* ClientSocket.c - NeonServ v5.1
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* ClientSocket.c - NeonServ v5.6
+ * Copyright (C) 2011-2012  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
  * 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 "main.h"
 #include "ClientSocket.h"
 #include "IRCParser.h"
 #include "UserNode.h"
 #include "IRCQueue.h"
+#include "WHOHandler.h"
+#include "HandleInfoHandler.h"
+#include "ConfigParser.h"
+#include "version.h"
+#include "IOHandler.h"
+#include "IRCEvents.h"
+#include "log.h"
 
 struct socket_list {
     struct ClientSocket *data;
     unsigned count;
 };
 
+#ifdef HAVE_THREADS
+static pthread_mutex_t synchronized;
+static pthread_mutex_t synchronized_recv;
+
+struct ParseOrder {
+    unsigned int tid;
+    struct ParseOrder *next;
+};
+struct ParseOrder *parse_order = NULL;
+#endif
+
 //the magic list :P
 static struct socket_list *sockets = NULL;
-static char buffer[BUF_SIZ];
 
-static void init_sockets() {
+static IOHANDLER_CALLBACK(socket_callback);
+
+void init_sockets() {
+    THREAD_MUTEX_INIT(synchronized);
+    THREAD_MUTEX_INIT(synchronized_recv);
+    
     sockets = malloc(sizeof(*sockets));
     if (!sockets)
     {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return;
     }
     sockets->data = NULL;
     sockets->count = 0;
 }
 
-struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
-    if(sockets == NULL) init_sockets();
+struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
+    if(!sockets)
+        init_sockets();
     struct ClientSocket *client = malloc(sizeof(*client));
-    if (!client)
-    {
-        perror("malloc() failed");
+    if (!client) {
         return NULL;
     }
+    client->iofd = NULL;
     client->host = strdup(host);
     client->port = port;
-    printf("Connect: %s:%d\n", client->host, client->port);
+    client->bind = (bindto ? strdup(bindto) : NULL);
     client->pass = (pass == NULL ? NULL : strdup(pass));
-    client->user = user;
+    client->nick = strdup(nick);
+    client->ident = strdup(ident);
+    client->realname = strdup(realname);
+    client->user = NULL;
+    client->network_name = NULL;
     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->queue = NULL;
+    client->whoqueue_first = NULL;
+    client->whoqueue_last = NULL;
+    client->handleinfo_first = NULL;
+    client->handleinfo_last = NULL;
+    SYNCHRONIZE(synchronized);
     client->next = sockets->data;
     sockets->data = client;
+    DESYNCHRONIZE(synchronized);
     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;
+void connect_socket(struct ClientSocket *client) {
+    client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
+    client->iofd->data = client;
+    client->flags |= SOCKET_FLAG_RECONNECT;
 }
 
-#else
-
-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(client->host, &addr.sin_addr))
-    {
-        host = gethostbyname(client->host);
-        if (!host)
-        {
-            perror("gethostbyname() failed");
-            return 0;
-        }
-        addr.sin_addr = *(struct in_addr*)host->h_addr;
-    }
-    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) {
-        len = sprintf(sendBuf, "PASS :%s\n", client->pass);
-        write_socket(client, sendBuf, len);
+static int _close_socket(struct ClientSocket *client) {
+    if(client == NULL) return 0;
+    if((client->flags & SOCKET_FLAG_CONNECTED)) {
+        iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
+        
+        bot_disconnect(client);
+        
+        iohandler_close(client->iofd);
+        client->iofd = NULL;
     }
-    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);
-
+    client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
+    if(client->queue)
+        queue_destroy(client);
+    if(client->whoqueue_first)
+        clear_whoqueue(client);
+    if(client->handleinfo_first)
+        clear_handleinfoqueue(client);
     return 1;
 }
 
-#endif
+int close_socket(struct ClientSocket *client) { //external call (don't reconnect)
+    if(client == NULL) return 0;
+    client->flags &= ~SOCKET_FLAG_RECONNECT;
+    return _close_socket(client);
+}
 
-int close_socket(struct ClientSocket *client) {
+int destroy_socket(struct ClientSocket *client) {
     if(client == NULL) return 0;
-    if((client->flags & SOCKET_FLAG_CONNECTED))
-        close(client->sock);
+    close_socket(client);
+    SYNCHRONIZE(synchronized);
     struct ClientSocket *sock, *last_sock = NULL;
     for (sock = sockets->data; sock; sock = sock->next) {
         if(sock == client) {
@@ -184,112 +141,140 @@ int close_socket(struct ClientSocket *client) {
             else
                 sockets->data = sock->next;
             sockets->count--;
+            break;
         } else
             last_sock = sock;
     }
-    if(client->queue)
-        queue_destroy(client);
+    event_freeclient(client);
     free(client->host);
-    free(client->pass);
+    if(client->bind)
+        free(client->bind);
+    if(client->pass)
+        free(client->pass);
+    if(client->network_name)
+        free(client->network_name);
+    if(client->iofd) //reconnect timer?
+        iohandler_close(client->iofd);
     free(client);
+    DESYNCHRONIZE(synchronized);
     return 1;
 }
 
 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);
+    if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
+    SYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
     #else
-    write(client->sock, msg, len);
+    printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg);
     #endif
+       iohandler_send(client->iofd, msg, len);
     client->traffic_out += len;
+    DESYNCHRONIZE(synchronized);
     return 1;
 }
 
 int write_socket(struct ClientSocket *client, char* msg, int len) {
-    if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
+    if(!(client && (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;
-    struct timeval timeout;
-    struct ClientSocket *sock;
-    int ret = 0, bytes, i;
-    
-    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);
-        if(sock->sock > ret)
-            ret = sock->sock;
+#if HAVE_THREADS
+static void clientsocket_start_of_recv(unsigned int tid) {
+    SYNCHRONIZE(whohandler_sync);
+    struct ParseOrder *entry, *last;
+    for(last = parse_order; last; last = last->next) {
+        if(last->next == NULL)
+            break;
     }
-    timeout.tv_sec = timeout_seconds;
-    timeout.tv_usec = 0;
-    ret = select(ret + 1, &fds, NULL, NULL, &timeout);
-    if(ret == 0) return;
-    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
-                        sock->buffer[sock->bufferpos + i] = buffer[i];
-                    }
-                    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;
-            }
-            if(bytes <= 0) {
-                //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);
-                if(used == sock->bufferpos + 1) {
-                    //used all bytes so just reset the bufferpos
-                    sock->bufferpos = 0;
-                } else {
-                    for(i = 0; i < sock->bufferpos - used; i++) {
-                        sock->buffer[i] = sock->buffer[i+used];
-                    }
-                    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);
-            }
+    entry = malloc(sizeof(*entry));
+    entry->tid = tid;
+    entry->next = NULL;
+    if(last)
+        last->next = entry;
+    else
+        parse_order = entry;
+    DESYNCHRONIZE(whohandler_sync);
+}
+
+static void clientsocket_end_of_recv(unsigned int tid) {
+    SYNCHRONIZE(whohandler_sync);
+    struct ParseOrder *entry, *last = NULL;
+    for(entry = parse_order; entry; entry = entry->next) {
+        if(entry->tid == tid) {
+            if(last)
+                last->next = entry->next;
+            else
+                parse_order = entry->next;
+            free(entry);
+            break;
+        } else
+            last = entry;
+    }
+    DESYNCHRONIZE(whohandler_sync);
+}
+
+int clientsocket_parseorder_top(unsigned int tid) {
+    if(parse_order && parse_order->tid == tid)
+        return 1;
+    else
+        return 0;
+}
+#endif
+
+static IOHANDLER_CALLBACK(socket_callback) {
+    struct ClientSocket *client = event->iofd->data;
+    #ifdef HAVE_THREADS
+    unsigned int tid;
+    #endif
+    if(process_state.running == 0)
+        return; //just ignore the event (shutdown sequence)
+    switch(event->type) {
+    case IOEVENT_CONNECTED:
+        client->flags |= SOCKET_FLAG_CONNECTED;
+        if(client->pass && strcmp(client->pass, ""))
+            putsock(client, "PASS :%s", client->pass);
+        putsock(client, "USER %s 0 0 :%s", client->ident, client->realname);
+        putsock(client, "NICK %s", client->nick);
+        break;
+    case IOEVENT_NOTCONNECTED:
+    case IOEVENT_CLOSED:
+        _close_socket(client);
+        if(client->flags & SOCKET_FLAG_RECONNECT) {
+            struct timeval timeout;
+            gettimeofday(&timeout, NULL);
+            timeout.tv_sec += SOCKET_RECONNECT_TIME;
+            client->iofd = iohandler_timer(timeout, socket_callback);
+            client->iofd->data = client;
         }
+        break;
+    case IOEVENT_TIMEOUT: //reconnect timer
+        connect_socket(client);
+        break;
+    case IOEVENT_RECV:
+        #ifdef HAVE_THREADS
+        tid = (unsigned int) pthread_self_tid();
+        clientsocket_start_of_recv(tid);
+        #endif
+        client->traffic_in += strlen(event->data.recv_str);
+        parse_line(client, event->data.recv_str);
+        #ifdef HAVE_THREADS
+        clientsocket_end_of_recv(tid);
+        #endif
+        break;
+    default:
+        break;
     }
 }
 
-void
-putsock(struct ClientSocket *client, const char *text, ...)
-{
+void putsock(struct ClientSocket *client, const char *text, ...) {
     va_list arg_list;
     char sendBuf[MAXLEN];
     int pos;
-    if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
+    if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
     sendBuf[0] = '\0';
     va_start(arg_list, text);
     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
@@ -310,19 +295,19 @@ struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
     return NULL;
 }
 
-void free_sockets() {
+void free_sockets(int close_only) {
     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);
-        if(client->queue)
-            queue_destroy(client);
-        free(client->host);
-        free(client->pass);
-        free(client);
+        if(close_only) {
+            if((client->flags & SOCKET_FLAG_CONNECTED))
+                iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
+        } else
+            destroy_socket(client);
+    }
+    if(!close_only) {
+        free(sockets);
+        sockets = NULL;
     }
-    free(sockets);
-    sockets = NULL;
 }