+/* 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"
+#include "WHOHandler.h"
+#include "HandleInfoHandler.h"
struct socket_list {
struct ClientSocket *data;
}
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;
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;
client->next = sockets->data;
sockets->data = client;
return 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);
}
}
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);
}
} else
last_sock = sock;
}
+ if(client->queue)
+ queue_destroy(client);
+ if(client->whoqueue_first)
+ clear_whoqueue(client);
+ if(client->handleinfo_first)
+ clear_handleinfoqueue(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);
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;
//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);
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);
+ }
}
}
}
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);