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