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