1 /* IRCClient.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 "IRCClient.h"
18 #include "IOHandler.h"
19 #include "UserSession.h"
20 #include "UserClient.h"
22 #include "ConfigParser.h"
24 static void ircclient_callback(struct IOEvent *event);
26 void ircclient_initialize(struct UserSession *session, struct UserLogin *login) {
27 struct IRCClient *client = calloc(1, sizeof(*client));
29 usersession_error(session, "Could not initialize IRC connection.");
32 client->session = session;
33 session->irc = client;
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"));
40 char notification[LINELEN];
41 sprintf(notification, "Connecting to %s (%s%d)", server_address, (server_ssl ? "+" : ""), server_port);
42 usersession_client_notification(session, notification);
44 client->iofd = iohandler_connect(server_address, server_port, server_ssl, bind_address, ircclient_callback);
46 usersession_error(session, "Could not initialize IRC connection.");
49 client->iofd->data = client;
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);
59 static void ircclient_handshake(struct IRCClient *client) {
60 struct UserSession *session = client->session;
61 struct IODescriptor *iofd = client->iofd;
62 iohandler_printf(iofd, "PASS %s:%s", session->username, session->password);
63 iohandler_printf(iofd, "NICK %s", session->nick);
64 iohandler_printf(iofd, "USER %s 0 * :%s", session->username, session->realname);
65 free(session->realname);
66 session->realname = NULL;
69 static void ircclient_recv(struct IRCClient *client, char *line) {
70 struct UserSession *session = client->session;
71 char *argv[MAXNUMPARAMS];
74 int argc = parse_line(tmp, argv, 1);
75 int pass_to_client = 1;
76 if(argc > 2 && !stricmp(argv[1], "PING")) {
77 iohandler_printf(client->iofd, "PONG :%s", argv[2]);
80 if(!client->auth_confirmed) {
81 if(argc == 4 && !stricmp(argv[1], "NOTICE") && !stricmp(argv[2], "AUTH") && !stricmp(argv[3], "*** Login accepted")) {
82 client->auth_confirmed = 1;
83 } else if(argc > 1 && !stricmp(argv[1], "001")) {
84 usersession_error(session, "IRC Session closed (Please use Login on Connect)");
91 usersession_client_raw(session, line);
94 static void ircclient_callback(struct IOEvent *event) {
95 struct IRCClient *client = event->iofd->data;
97 case IOEVENT_NOTCONNECTED:
98 usersession_error(client->session, "Could not connect to the Server.");
100 case IOEVENT_CONNECTED:
101 ircclient_handshake(client);
104 ircclient_recv(client, event->data.recv_str);
107 usersession_error(client->session, "Disconnected from the IRC Server.");
114 void ircclient_send(struct IRCClient *client, char *line) {
115 iohandler_printf(client->iofd, "%s", line);