added Makefile
[TransparentIRC.git] / src / UserClient.c
1 /* UserClient.c - TransparentIRC 0.1
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "UserClient.h"
18 #include "IOHandler.h"
19 #include "ServerSocket.h"
20
21 static void userclient_callback(struct IOEvent *event);
22
23 static struct UserClient *userclients = NULL;
24
25 void userclient_accepted(struct ServerSocket *server, int sockfd) {
26     struct UserClient *client;
27     struct IODescriptor *iofd = iohandler_add(sockfd, IOTYPE_CLIENT, userclient_callback);
28     if(!iofd) return;
29     iofd->state = IO_CONNECTED;
30     iofd->read_lines = 1;
31     iohandler_update(iofd);
32     client = malloc(sizeof(*client));
33     client->iofd = iofd;
34     iofd->data = client;
35     client->server = server;
36     server->clientcount++;
37     
38     //add UserClient to the list
39     client->prev = NULL;
40     client->next = userclients;
41     if(userclients)
42         userclients->prev = client;
43     userclients = client;
44     
45     //let's say hello to the client
46     iohandler_printf(iofd, "NOTICE AUTH :*** TransparentIRC " TRANSIRC_VERSION " (use /quote transirc for more information)");
47 }
48
49 void userclient_close(struct UserClient *client) {
50     iohandler_close(client->iofd);
51     client->server->clientcount--;
52     if(client->prev)
53         client->prev->next = client->next;
54     else
55         userclients = client->next;
56     if(client->next)
57         client->next->prev = client->prev;
58     free(client);
59 }
60
61 void userclient_close_server(struct ServerSocket *server, int keep_clients) {
62     struct UserClient *client, *next_client;
63     for(client = userclients; client; client = next_client) {
64         next_client = client->next;
65         if(client->server == server) {
66             if(keep_clients)
67                 client->server = NULL;
68             else
69                 userclient_close(client);
70         }
71     }
72 }
73
74 static void userclient_recv(struct UserClient *client, char *line) {
75     iohandler_printf(client->iofd, "reply: %s", line);
76 }
77
78 static void userclient_callback(struct IOEvent *event) {
79     struct UserClient *client = event->iofd->data;
80     switch(event->type) {
81         case IOEVENT_RECV:
82             userclient_recv(client, event->data.recv_str);
83             break;
84         case IOEVENT_CLOSED:
85             userclient_close(client);
86             break;
87         default:
88             break;
89     }
90 }