+++ /dev/null
-
-#include "ClientSocket.h"
-#include "IRCParser.h"
-#include "UserNode.h"
-
-struct socket_list {
- struct ClientSocket *data;
- unsigned count;
-};
-
-//the magic list :P
-static struct socket_list *sockets = NULL;
-static char buffer[BUF_SIZ];
-
-static void init_sockets() {
- sockets = malloc(sizeof(*sockets));
- if (!sockets)
- {
- perror("malloc() failed");
- 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 *client = malloc(sizeof(*client));
- if (!client)
- {
- perror("malloc() failed");
- 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 *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)
- {
- herror("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;
- 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);
- }
- 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;
-}
-
-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 == client) {
- if(last_sock)
- last_sock->next = sock->next;
- else
- sockets->data = sock->next;
- sockets->count--;
- } else
- last_sock = sock;
- }
- 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;
- printf("[send %d] %s", len, msg);
- write(client->sock, msg, len);
- client->traffic_out += len;
- return 1;
-}
-
-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;
- }
- 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) {
- bytes = read(sock->sock, buffer, sizeof(buffer));
- 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 {
- bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
- if(bytes > 0)
- sock->bufferpos = bytes;
- }
- if(bytes <= 0) {
- //error
- sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
- bot_disconnect(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;
- }
- }
- }
- }
-}
-
-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;
-}