2 #include "ClientSocket.h"
7 struct ClientSocket *data;
12 static struct socket_list *sockets = NULL;
13 static char buffer[BUF_SIZ];
15 static void init_sockets() {
16 sockets = malloc(sizeof(*sockets));
19 perror("malloc() failed");
26 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
27 if(sockets == NULL) init_sockets();
28 struct ClientSocket *client = malloc(sizeof(*client));
31 perror("malloc() failed");
34 client->host = strdup(host);
36 printf("Connect: %s:%d", client->host, client->port);
37 client->pass = (pass == NULL ? NULL : strdup(pass));
40 client->bufferpos = 0;
43 client->next = sockets->data;
44 sockets->data = client;
48 int connect_socket(struct ClientSocket *client) {
49 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
51 struct sockaddr_in addr;
53 if (!inet_aton(client->host, &addr.sin_addr))
55 host = gethostbyname(client->host);
58 herror("gethostbyname() failed");
61 addr.sin_addr = *(struct in_addr*)host->h_addr;
63 sock = socket(PF_INET, SOCK_STREAM, 0);
66 perror("socket() failed");
70 addr.sin_port = htons(client->port);
71 addr.sin_family = AF_INET;
73 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
75 perror("connect() failed");
80 client->flags |= SOCKET_FLAG_CONNECTED;
82 //send the IRC Headers
87 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
88 write_socket(client, sendBuf, len);
90 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
91 write_socket(client, sendBuf, len);
92 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
93 write_socket(client, sendBuf, len);
98 int close_socket(struct ClientSocket *client) {
99 if(client == NULL) return 0;
100 if((client->flags & SOCKET_FLAG_CONNECTED))
102 struct ClientSocket *sock, *last_sock = NULL;
103 for (sock = sockets->data; sock; sock = sock->next) {
106 last_sock->next = sock->next;
108 sockets->data = sock->next;
119 int write_socket(struct ClientSocket *client, char* msg, int len) {
120 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
121 printf("[send %d] %s", len, msg);
122 write(client->sock, msg, len);
126 void socket_loop(int timeout_seconds) {
127 if(sockets == NULL) return;
129 struct timeval timeout;
130 struct ClientSocket *sock;
131 int ret = 0, bytes, i;
134 for (sock = sockets->data; sock; sock = sock->next) {
135 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
136 FD_SET(sock->sock, &fds);
140 timeout.tv_sec = timeout_seconds;
142 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
144 for (sock = sockets->data; sock; sock = sock->next) {
145 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
146 if(sock->bufferpos != 0) {
147 bytes = read(sock->sock, buffer, sizeof(buffer));
149 for(i = 0; i < bytes; i++) {
150 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
151 sock->buffer[sock->bufferpos + i] = buffer[i];
153 sock->bufferpos += i;
156 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
158 sock->bufferpos = bytes;
162 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
163 bot_disconnect(sock);
165 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
166 if(used == sock->bufferpos + 1) {
167 //used all bytes so just reset the bufferpos
170 for(i = 0; i < sock->bufferpos - used; i++) {
171 sock->buffer[i] = sock->buffer[i+used];
173 sock->bufferpos -= used;
181 putsock(struct ClientSocket *client, const char *text, ...)
184 char sendBuf[MAXLEN];
186 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
188 va_start(arg_list, text);
189 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
191 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
193 sendBuf[pos+1] = '\0';
194 write_socket(client, sendBuf, pos+1);
197 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
198 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
199 if(sock == NULL) return NULL;
200 for (; sock; sock = sock->next) {
201 if((sock->flags & flags) == flags)
207 void free_sockets() {
209 struct ClientSocket *client, *next;
210 for (client = sockets->data; client; client = next) {
212 if((client->flags & SOCKET_FLAG_CONNECTED))