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 client->pass = (pass == NULL ? NULL : strdup(pass));
39 client->bufferpos = 0;
42 client->next = sockets->data;
43 sockets->data = client;
47 int connect_socket(struct ClientSocket *client) {
48 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
50 struct sockaddr_in addr;
52 if (!inet_aton(client->host, &addr.sin_addr))
54 host = gethostbyname(client->host);
57 herror("gethostbyname() failed");
60 addr.sin_addr = *(struct in_addr*)host->h_addr;
62 sock = socket(PF_INET, SOCK_STREAM, 0);
65 perror("socket() failed");
69 addr.sin_port = htons(client->port);
70 addr.sin_family = AF_INET;
72 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
74 perror("connect() failed");
79 client->flags |= SOCKET_FLAG_CONNECTED;
81 //send the IRC Headers
86 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
87 write_socket(client, sendBuf, len);
89 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
90 write_socket(client, sendBuf, len);
91 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
92 write_socket(client, sendBuf, len);
97 int close_socket(struct ClientSocket *client) {
98 if(client == NULL) return 0;
99 if((client->flags & SOCKET_FLAG_CONNECTED))
101 struct ClientSocket *sock, *last_sock = NULL;
102 for (sock = sockets->data; sock; sock = sock->next) {
105 last_sock->next = sock->next;
107 sockets->data = sock->next;
118 int write_socket(struct ClientSocket *client, char* msg, int len) {
119 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
120 printf("[send %d] %s", len, msg);
121 write(client->sock, msg, len);
125 void socket_loop(int timeout_seconds) {
126 if(sockets == NULL) return;
128 struct timeval timeout;
129 struct ClientSocket *sock;
130 int ret = 0, bytes, i;
133 for (sock = sockets->data; sock; sock = sock->next) {
134 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
135 FD_SET(sock->sock, &fds);
139 timeout.tv_sec = timeout_seconds;
141 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
143 for (sock = sockets->data; sock; sock = sock->next) {
144 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
145 if(sock->bufferpos != 0) {
146 bytes = read(sock->sock, buffer, sizeof(buffer));
148 for(i = 0; i < bytes; i++) {
149 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
150 sock->buffer[sock->bufferpos + i] = buffer[i];
152 sock->bufferpos += i;
155 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
157 sock->bufferpos = bytes;
161 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
162 bot_disconnect(sock);
164 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
165 if(used == sock->bufferpos + 1) {
166 //used all bytes so just reset the bufferpos
169 for(i = 0; i < sock->bufferpos - used; i++) {
170 sock->buffer[i] = sock->buffer[i+used];
172 sock->bufferpos -= used;
180 putsock(struct ClientSocket *client, const char *text, ...)
183 char sendBuf[MAXLEN];
185 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
187 va_start(arg_list, text);
188 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
190 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
192 sendBuf[pos+1] = '\0';
193 write_socket(client, sendBuf, pos+1);
196 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
197 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
198 if(sock == NULL) return NULL;
199 for (; sock; sock = sock->next) {
200 if((sock->flags & flags) == flags)
206 void free_sockets() {
208 struct ClientSocket *client, *next;
209 for (client = sockets->data; client; client = next) {
211 if((client->flags & SOCKET_FLAG_CONNECTED))