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