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