added channel rejoin faker (session recover)
[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 struct IRCUser ircclient_parse_user(char *from) {
76     struct IRCUser user;
77     char *a = from, *b = from;
78     while(*b != '!') {
79         b++;
80     }
81     user.nick = a;
82     if(!*b) {
83         user.ident = "";
84         user.host = "";
85         return user;
86     }
87     *b = '\0';
88     b++;
89     a = b;
90     while(*b && *b != '@') {
91         b++;
92     }
93     user.ident = a;
94     if(!*b) {
95         user.host = "";
96         return user;
97     }
98     *b = '\0';
99     b++;
100     user.host = b;
101     return user;
102 }
103
104 static void ircclient_recv(struct IRCClient *client, char *line) {
105     struct UserSession *session = client->session;
106     char *argv[MAXNUMPARAMS];
107     char tmp[LINELEN];
108     strcpy(tmp, line);
109     int argc = parse_line(tmp, argv, 1);
110     int pass_to_client = 1;
111     if(argc > 2 && !stricmp(argv[1], "PING")) {
112         iohandler_printf(client->iofd, "PONG :%s", argv[2]);
113         pass_to_client = 0;
114     }
115     if(!client->auth_confirmed) {
116         if(argc == 4 && !stricmp(argv[1], "NOTICE") && !stricmp(argv[2], "AUTH") && !stricmp(argv[3], "*** Login accepted")) {
117             client->auth_confirmed = 1;
118         } else if(argc > 1 && !stricmp(argv[1], "001")) {
119             usersession_error(session, "IRC Session closed (Please use Login on Connect)");
120             pass_to_client = 0;
121         }
122     } else {
123         if(!stricmp(argv[1], "001")) {
124             free(session->nick);
125             session->nick = strdup(argv[2]);
126             client->fully_connected = 1;
127         }
128         if(!stricmp(argv[1], "001") ||
129             !stricmp(argv[1], "002") ||
130             !stricmp(argv[1], "003") ||
131             !stricmp(argv[1], "004") ||
132             !stricmp(argv[1], "005") ||
133             !stricmp(argv[1], "375") ||
134             !stricmp(argv[1], "372") ||
135             !stricmp(argv[1], "376")
136           ) {
137             //save these raw's for recovering the connection later
138             struct IRCLine *recover_line = NULL, *new_line;
139             if(client->recover_header)
140                 for(recover_line = client->recover_header; recover_line->next; recover_line = recover_line->next) {};
141             new_line = malloc(sizeof(*new_line));
142             if(new_line) {
143                 new_line->line = strdup(line);
144                 new_line->next = NULL;
145                 if(recover_line)
146                     recover_line->next = new_line;
147                 else
148                     client->recover_header = new_line;
149             }
150         } else if(!stricmp(argv[1], "JOIN")) {
151             struct IRCUser from = ircclient_parse_user(argv[0]);
152             if(!stricmp(from.nick, session->nick)) {
153                 struct IRCChannel *lchannel = NULL, *channel = malloc(sizeof(*channel));
154                 if(client->channel)
155                     for(lchannel = client->channel; lchannel->next; lchannel = lchannel->next) {}
156                 channel->name = strdup(argv[2]);
157                 channel->next = NULL;
158                 channel->prev = lchannel;
159                 if(lchannel)
160                     lchannel->next = channel;
161                 else
162                     client->channel = channel;
163             }
164         } else if(!stricmp(argv[1], "PART")) {
165             struct IRCUser from = ircclient_parse_user(argv[0]);
166             if(!stricmp(from.nick, session->nick)) {
167                 struct IRCChannel *channel;
168                 for(channel = client->channel; channel; channel = channel->next) {
169                     if(!stricmp(channel->name, argv[2])) {
170                         if(channel->prev)
171                             channel->prev->next = channel->next;
172                         else
173                             client->channel = channel->next;
174                         if(channel->next)
175                             channel->next->prev = channel->prev;
176                         free(channel->name);
177                         free(channel);
178                         break;
179                     }
180                 }
181             }
182         } else if(!stricmp(argv[1], "KICK")) {
183             if(!stricmp(argv[3], session->nick)) {
184                 struct IRCChannel *channel;
185                 for(channel = client->channel; channel; channel = channel->next) {
186                     if(!stricmp(channel->name, argv[2])) {
187                         if(channel->prev)
188                             channel->prev->next = channel->next;
189                         else
190                             client->channel = channel->next;
191                         if(channel->next)
192                             channel->next->prev = channel->prev;
193                         free(channel->name);
194                         free(channel);
195                         break;
196                     }
197                 }
198             }
199         } else if(!stricmp(argv[1], "NICK")) {
200             struct IRCUser from = ircclient_parse_user(argv[0]);
201             if(!stricmp(from.nick, session->nick)) {
202                 free(session->nick);
203                 session->nick = strdup(argv[2]);
204             }
205         }
206     }
207     if(pass_to_client)
208         usersession_client_raw(session, line);
209 }
210
211 static void ircclient_callback(struct IOEvent *event) {
212     struct IRCClient *client = event->iofd->data;
213     switch(event->type) {
214         case IOEVENT_NOTCONNECTED:
215             usersession_error(client->session, "Could not connect to the Server.");
216             break;
217         case IOEVENT_CONNECTED:
218             ircclient_handshake(client);
219             break;
220         case IOEVENT_RECV:
221             ircclient_recv(client, event->data.recv_str);
222             break;
223         case IOEVENT_CLOSED:
224             usersession_error(client->session, "Disconnected from the IRC Server.");
225             break;
226         default:
227             break;
228     }
229 }
230
231 void ircclient_send(struct IRCClient *client, char *line) {
232     iohandler_printf(client->iofd, "%s", line);
233 }
234
235 void ircclient_recover_session(struct UserSession *session) {
236     struct IRCClient *client = session->irc;
237     //replay header
238     struct IRCLine *recover_line;
239     for(recover_line = client->recover_header; recover_line; recover_line = recover_line->next) {
240         usersession_client_raw(session, recover_line->line);
241     }
242     struct IRCChannel *channel;
243     char raw[LINELEN];
244     for(channel = client->channel; channel; channel = channel->next) {
245         sprintf(raw, ":%s!%s@TransparentIRC.session.recover JOIN %s", client->session->nick, client->session->username, channel->name);
246         usersession_client_raw(session, raw);
247         
248         sprintf(raw, "NAMES %s", channel->name);
249         ircclient_send(client, raw);
250     }
251 }