1 /* UserClient.c - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
17 #include "UserClient.h"
18 #include "IOHandler.h"
19 #include "ServerSocket.h"
21 static void userclient_callback(struct IOEvent *event);
23 static struct UserClient *userclients = NULL;
25 void userclient_accepted(struct ServerSocket *server, int sockfd) {
26 struct UserClient *client;
27 struct IODescriptor *iofd = iohandler_add(sockfd, IOTYPE_CLIENT, userclient_callback);
29 iofd->state = IO_CONNECTED;
31 iohandler_update(iofd);
32 client = malloc(sizeof(*client));
35 client->server = server;
36 server->clientcount++;
38 //add UserClient to the list
40 client->next = userclients;
42 userclients->prev = client;
45 //let's say hello to the client
46 iohandler_printf(iofd, "NOTICE AUTH :*** TransparentIRC " TRANSIRC_VERSION " (use /quote transirc for more information)");
49 void userclient_close(struct UserClient *client) {
50 iohandler_close(client->iofd);
51 client->server->clientcount--;
53 client->prev->next = client->next;
55 userclients = client->next;
57 client->next->prev = client->prev;
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) {
67 client->server = NULL;
69 userclient_close(client);
74 static void userclient_recv(struct UserClient *client, char *line) {
75 iohandler_printf(client->iofd, "reply: %s", line);
78 static void userclient_callback(struct IOEvent *event) {
79 struct UserClient *client = event->iofd->data;
82 userclient_recv(client, event->data.recv_str);
85 userclient_close(client);