added small static content replay (channels will follow)
[TransparentIRC.git] / src / IRCClient.c
1 /* IRCClient.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 "IRCClient.h"
18 #include "IOHandler.h"
19 #include "UserSession.h"
20 #include "UserClient.h"
21 #include "tools.h"
22 #include "ConfigParser.h"
23
24 static void ircclient_callback(struct IOEvent *event);
25
26 void ircclient_initialize(struct UserSession *session, struct UserLogin *login) {
27     struct IRCClient *client = calloc(1, sizeof(*client));
28     if(!client) {
29         usersession_error(session, "Could not initialize IRC connection.");
30         return;
31     }
32     client->session = session;
33     session->irc = client;
34     
35     char *server_address = (login->server_address ? login->server_address : get_string_field("server.host"));
36     int server_port = (login->server_override_port ? login->server_port : get_int_field("server.port"));
37     int server_ssl = (login->server_override_port ? login->server_ssl : get_int_field("server.ssl"));
38     char *bind_address = (login->bind_address ? login->bind_address : get_string_field("server.bind"));
39     
40     char notification[LINELEN];
41     sprintf(notification, "Connecting to %s (%s%d)", server_address, (server_ssl ? "+" : ""), server_port);
42     usersession_client_notification(session, notification);
43     
44     client->iofd = iohandler_connect(server_address, server_port, server_ssl, bind_address, ircclient_callback);
45     if(!client->iofd) {
46         usersession_error(session, "Could not initialize IRC connection.");
47         return;
48     }
49     client->iofd->data = client;
50 }
51
52 void ircclient_close(struct IRCClient *client) {
53     client->session->irc = NULL;
54     iohandler_printf(client->iofd, "QUIT :[TransparentIRC] Quit");
55     iohandler_close(client->iofd);
56     struct IRCLine *recover_line, *next_line;
57     for(recover_line = client->recover_header; recover_line; recover_line = next_line) {
58         next_line = recover_line->next;
59         free(recover_line->line);
60         free(recover_line);
61     }
62     free(client);
63 }
64
65 static void ircclient_handshake(struct IRCClient *client) {
66     struct UserSession *session = client->session;
67     struct IODescriptor *iofd = client->iofd;
68     iohandler_printf(iofd, "PASS %s:%s", session->username, session->password);
69     iohandler_printf(iofd, "NICK %s", session->nick);
70     iohandler_printf(iofd, "USER %s 0 * :%s", session->username, session->realname);
71     free(session->realname);
72     session->realname = NULL;
73 }
74
75 static void ircclient_recv(struct IRCClient *client, char *line) {
76     struct UserSession *session = client->session;
77     char *argv[MAXNUMPARAMS];
78     char tmp[LINELEN];
79     strcpy(tmp, line);
80     int argc = parse_line(tmp, argv, 1);
81     int pass_to_client = 1;
82     if(argc > 2 && !stricmp(argv[1], "PING")) {
83         iohandler_printf(client->iofd, "PONG :%s", argv[2]);
84         pass_to_client = 0;
85     }
86     if(!client->auth_confirmed) {
87         if(argc == 4 && !stricmp(argv[1], "NOTICE") && !stricmp(argv[2], "AUTH") && !stricmp(argv[3], "*** Login accepted")) {
88             client->auth_confirmed = 1;
89         } else if(argc > 1 && !stricmp(argv[1], "001")) {
90             usersession_error(session, "IRC Session closed (Please use Login on Connect)");
91             pass_to_client = 0;
92         }
93     } else {
94         if(!stricmp(argv[1], "001") ||
95             !stricmp(argv[1], "002") ||
96             !stricmp(argv[1], "003") ||
97             !stricmp(argv[1], "004") ||
98             !stricmp(argv[1], "005") ||
99             !stricmp(argv[1], "375") ||
100             !stricmp(argv[1], "372") ||
101             !stricmp(argv[1], "376")
102           ) {
103             //save these raw's for recovering the connection later
104             struct IRCLine *recover_line = NULL, *new_line;
105             if(client->recover_header)
106                 for(recover_line = client->recover_header; recover_line->next; recover_line = recover_line->next) {};
107             new_line = malloc(sizeof(*new_line));
108             if(new_line) {
109                 new_line->line = strdup(line);
110                 new_line->next = NULL;
111                 if(recover_line)
112                     recover_line->next = new_line;
113                 else
114                     client->recover_header = new_line;
115             }
116         }
117     }
118     if(pass_to_client)
119         usersession_client_raw(session, line);
120 }
121
122 static void ircclient_callback(struct IOEvent *event) {
123     struct IRCClient *client = event->iofd->data;
124     switch(event->type) {
125         case IOEVENT_NOTCONNECTED:
126             usersession_error(client->session, "Could not connect to the Server.");
127             break;
128         case IOEVENT_CONNECTED:
129             ircclient_handshake(client);
130             break;
131         case IOEVENT_RECV:
132             ircclient_recv(client, event->data.recv_str);
133             break;
134         case IOEVENT_CLOSED:
135             usersession_error(client->session, "Disconnected from the IRC Server.");
136             break;
137         default:
138             break;
139     }
140 }
141
142 void ircclient_send(struct IRCClient *client, char *line) {
143     iohandler_printf(client->iofd, "%s", line);
144 }
145
146 void ircclient_recover_session(struct UserSession *session) {
147     struct IRCClient *client = session->irc;
148     //replay header
149     struct IRCLine *recover_line;
150     for(recover_line = client->recover_header; recover_line; recover_line = recover_line->next) {
151         usersession_client_raw(session, recover_line->line);
152     }
153     
154 }