added debug output
[NeonServV5.git] / ClientSocket.c
1
2 #include "ClientSocket.h"
3 #include "IRCParser.h"
4 #include "UserNode.h"
5
6 struct socket_list {
7     struct ClientSocket *data;
8     unsigned count;
9 };
10
11 //the magic list :P
12 static struct socket_list *sockets = NULL;
13 static char buffer[BUF_SIZ];
14
15 static void init_sockets() {
16     sockets = malloc(sizeof(*sockets));
17     if (!sockets)
18     {
19         perror("malloc() failed");
20         return;
21     }
22     sockets->data = NULL;
23     sockets->count = 0;
24 }
25
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));
29     if (!client)
30     {
31         perror("malloc() failed");
32         return NULL;
33     }
34     client->host = strdup(host);
35     client->port = port;
36     printf("Connect: %s:%d", client->host, client->port);
37     client->pass = (pass == NULL ? NULL : strdup(pass));
38     client->user = user;
39     client->flags = 0;
40     client->bufferpos = 0;
41         client->botid = 0;
42     client->clientid = 0;
43     client->next = sockets->data;
44     sockets->data = client;
45     return client;
46 }
47
48 int connect_socket(struct ClientSocket *client) {
49     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
50     struct hostent *host;
51     struct sockaddr_in addr;
52     int sock;
53     if (!inet_aton(client->host, &addr.sin_addr))
54     {
55         host = gethostbyname(client->host);
56         if (!host)
57         {
58             herror("gethostbyname() failed");
59             return 0;
60         }
61         addr.sin_addr = *(struct in_addr*)host->h_addr;
62     }
63     sock = socket(PF_INET, SOCK_STREAM, 0);
64     if (sock == -1)
65     {
66         perror("socket() failed");
67         return 0;
68     }
69
70     addr.sin_port = htons(client->port);
71     addr.sin_family = AF_INET;
72
73     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
74     {
75         perror("connect() failed");
76         return 0;
77     }
78
79     client->sock = sock;
80     client->flags |= SOCKET_FLAG_CONNECTED;
81
82     //send the IRC Headers
83     char sendBuf[512];
84     int len;
85
86     if(client->pass) {
87         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
88         write_socket(client, sendBuf, len);
89     }
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);
94
95     return 1;
96 }
97
98 int close_socket(struct ClientSocket *client) {
99     if(client == NULL) return 0;
100     if((client->flags & SOCKET_FLAG_CONNECTED))
101         close(client->sock);
102     struct ClientSocket *sock, *last_sock = NULL;
103     for (sock = sockets->data; sock; sock = sock->next) {
104         if(sock == client) {
105             if(last_sock)
106                 last_sock->next = sock->next;
107             else
108                 sockets->data = sock->next;
109             sockets->count--;
110         } else
111             last_sock = sock;
112     }
113     free(client->host);
114     free(client->pass);
115     free(client);
116     return 1;
117 }
118
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);
123     return 1;
124 }
125
126 void socket_loop(int timeout_seconds) {
127     if(sockets == NULL) return;
128     fd_set fds;
129     struct timeval timeout;
130     struct ClientSocket *sock;
131     int ret = 0, bytes, i;
132     
133     FD_ZERO(&fds);
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);
137         if(sock->sock > ret)
138             ret = sock->sock;
139     }
140     timeout.tv_sec = timeout_seconds;
141     timeout.tv_usec = 0;
142     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
143     if(ret == 0) return;
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));
148                 if(bytes > 0) {
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];
152                     }
153                     sock->bufferpos += i;
154                 }
155             } else {
156                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
157                 if(bytes > 0)
158                     sock->bufferpos = bytes;
159             }
160             if(bytes <= 0) {
161                 //error
162                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
163                 bot_disconnect(sock);
164             } else {
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
168                     sock->bufferpos = 0;
169                 } else {
170                     for(i = 0; i < sock->bufferpos - used; i++) {
171                         sock->buffer[i] = sock->buffer[i+used];
172                     }
173                     sock->bufferpos -= used;
174                 }
175             }
176         }
177     }
178 }
179
180 void
181 putsock(struct ClientSocket *client, const char *text, ...)
182 {
183     va_list arg_list;
184     char sendBuf[MAXLEN];
185     int pos;
186     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
187     sendBuf[0] = '\0';
188     va_start(arg_list, text);
189     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
190     va_end(arg_list);
191     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
192     sendBuf[pos] = '\n';
193     sendBuf[pos+1] = '\0';
194     write_socket(client, sendBuf, pos+1);
195 }
196
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)
202             return sock;
203     }
204     return NULL;
205 }
206
207 void free_sockets() {
208     if(!sockets) return;
209     struct ClientSocket *client, *next;
210     for (client = sockets->data; client; client = next) {
211         next = client->next;
212         if((client->flags & SOCKET_FLAG_CONNECTED))
213             close(client->sock);
214         free(client->host);
215         free(client->pass);
216         free(client);
217     }
218     free(sockets);
219     sockets = NULL;
220 }