changed Makefile; build all commands as an own file
[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->traffic_in = 0;
42     client->traffic_out = 0;
43     client->connection_time = 0;
44         client->botid = 0;
45     client->clientid = 0;
46     client->next = sockets->data;
47     sockets->data = client;
48     return client;
49 }
50
51 int connect_socket(struct ClientSocket *client) {
52     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
53     struct hostent *host;
54     struct sockaddr_in addr;
55     int sock;
56     if (!inet_aton(client->host, &addr.sin_addr))
57     {
58         host = gethostbyname(client->host);
59         if (!host)
60         {
61             herror("gethostbyname() failed");
62             return 0;
63         }
64         addr.sin_addr = *(struct in_addr*)host->h_addr;
65     }
66     sock = socket(PF_INET, SOCK_STREAM, 0);
67     if (sock == -1)
68     {
69         perror("socket() failed");
70         return 0;
71     }
72
73     addr.sin_port = htons(client->port);
74     addr.sin_family = AF_INET;
75
76     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
77     {
78         perror("connect() failed");
79         return 0;
80     }
81
82     client->sock = sock;
83     client->flags |= SOCKET_FLAG_CONNECTED;
84     client->connection_time = time(0);
85
86     //send the IRC Headers
87     char sendBuf[512];
88     int len;
89
90     if(client->pass) {
91         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
92         write_socket(client, sendBuf, len);
93     }
94     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
95     write_socket(client, sendBuf, len);
96     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
97     write_socket(client, sendBuf, len);
98
99     return 1;
100 }
101
102 int close_socket(struct ClientSocket *client) {
103     if(client == NULL) return 0;
104     if((client->flags & SOCKET_FLAG_CONNECTED))
105         close(client->sock);
106     struct ClientSocket *sock, *last_sock = NULL;
107     for (sock = sockets->data; sock; sock = sock->next) {
108         if(sock == client) {
109             if(last_sock)
110                 last_sock->next = sock->next;
111             else
112                 sockets->data = sock->next;
113             sockets->count--;
114         } else
115             last_sock = sock;
116     }
117     free(client->host);
118     free(client->pass);
119     free(client);
120     return 1;
121 }
122
123 int write_socket(struct ClientSocket *client, char* msg, int len) {
124     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
125     printf("[send %d] %s", len, msg);
126     write(client->sock, msg, len);
127     client->traffic_out += len;
128     return 1;
129 }
130
131 void socket_loop(int timeout_seconds) {
132     if(sockets == NULL) return;
133     fd_set fds;
134     struct timeval timeout;
135     struct ClientSocket *sock;
136     int ret = 0, bytes, i;
137     
138     FD_ZERO(&fds);
139     for (sock = sockets->data; sock; sock = sock->next) {
140         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
141         FD_SET(sock->sock, &fds);
142         if(sock->sock > ret)
143             ret = sock->sock;
144     }
145     timeout.tv_sec = timeout_seconds;
146     timeout.tv_usec = 0;
147     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
148     if(ret == 0) return;
149     for (sock = sockets->data; sock; sock = sock->next) {
150         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
151             if(sock->bufferpos != 0) {
152                 bytes = read(sock->sock, buffer, sizeof(buffer));
153                 if(bytes > 0) {
154                     for(i = 0; i < bytes; i++) {
155                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
156                         sock->buffer[sock->bufferpos + i] = buffer[i];
157                     }
158                     sock->bufferpos += i;
159                 }
160             } else {
161                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
162                 if(bytes > 0)
163                     sock->bufferpos = bytes;
164             }
165             if(bytes <= 0) {
166                 //error
167                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
168                 bot_disconnect(sock);
169             } else {
170                 sock->traffic_in += bytes;
171                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
172                 if(used == sock->bufferpos + 1) {
173                     //used all bytes so just reset the bufferpos
174                     sock->bufferpos = 0;
175                 } else {
176                     for(i = 0; i < sock->bufferpos - used; i++) {
177                         sock->buffer[i] = sock->buffer[i+used];
178                     }
179                     sock->bufferpos -= used;
180                 }
181             }
182         }
183     }
184 }
185
186 void
187 putsock(struct ClientSocket *client, const char *text, ...)
188 {
189     va_list arg_list;
190     char sendBuf[MAXLEN];
191     int pos;
192     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
193     sendBuf[0] = '\0';
194     va_start(arg_list, text);
195     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
196     va_end(arg_list);
197     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
198     sendBuf[pos] = '\n';
199     sendBuf[pos+1] = '\0';
200     write_socket(client, sendBuf, pos+1);
201 }
202
203 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
204     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
205     if(sock == NULL) return NULL;
206     for (; sock; sock = sock->next) {
207         if(!flags || (sock->flags & flags) == flags)
208             return sock;
209     }
210     return NULL;
211 }
212
213 void free_sockets() {
214     if(!sockets) return;
215     struct ClientSocket *client, *next;
216     for (client = sockets->data; client; client = next) {
217         next = client->next;
218         if((client->flags & SOCKET_FLAG_CONNECTED))
219             close(client->sock);
220         free(client->host);
221         free(client->pass);
222         free(client);
223     }
224     free(sockets);
225     sockets = NULL;
226 }