--- /dev/null
+/* IRCClient.c - TransparentIRC 0.1
+ * Copyright (C) 2011-2012 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "IRCClient.h"
+#include "IOHandler.h"
+#include "UserSession.h"
+#include "UserClient.h"
+#include "tools.h"
+#include "ConfigParser.h"
+
+static void ircclient_callback(struct IOEvent *event);
+
+void ircclient_initialize(struct UserSession *session, struct UserLogin *login) {
+ struct IRCClient *client = calloc(1, sizeof(*client));
+ if(!client) {
+ usersession_error(session, "Could not initialize IRC connection.");
+ return;
+ }
+ client->session = session;
+ session->irc = client;
+
+ char *server_address = (login->server_address ? login->server_address : get_string_field("server.host"));
+ int server_port = (login->server_override_port ? login->server_port : get_int_field("server.port"));
+ int server_ssl = (login->server_override_port ? login->server_ssl : get_int_field("server.ssl"));
+ char *bind_address = (login->bind_address ? login->bind_address : get_string_field("server.bind"));
+
+ char notification[LINELEN];
+ sprintf(notification, "Connecting to %s (%s%d)", server_address, (server_ssl ? "+" : ""), server_port);
+ usersession_client_notification(session, notification);
+
+ client->iofd = iohandler_connect(server_address, server_port, server_ssl, bind_address, ircclient_callback);
+ if(!client->iofd) {
+ usersession_error(session, "Could not initialize IRC connection.");
+ return;
+ }
+ client->iofd->data = client;
+}
+
+void ircclient_close(struct IRCClient *client) {
+ client->session->irc = NULL;
+ iohandler_printf(client->iofd, "QUIT :[TransparentIRC] Quit");
+ iohandler_close(client->iofd);
+ free(client);
+}
+
+static void ircclient_handshake(struct IRCClient *client) {
+ struct UserSession *session = client->session;
+ struct IODescriptor *iofd = client->iofd;
+ iohandler_printf(iofd, "PASS %s:%s", session->username, session->password);
+ iohandler_printf(iofd, "NICK %s", session->nick);
+ iohandler_printf(iofd, "USER %s 0 * :%s", session->username, session->realname);
+ free(session->realname);
+ session->realname = NULL;
+}
+
+static void ircclient_recv(struct IRCClient *client, char *line) {
+ struct UserSession *session = client->session;
+ char *argv[MAXNUMPARAMS];
+ char tmp[LINELEN];
+ strcpy(tmp, line);
+ int argc = parse_line(tmp, argv, 1);
+ int pass_to_client = 1;
+ if(argc > 2 && !stricmp(argv[1], "PING")) {
+ iohandler_printf(client->iofd, "PONG :%s", argv[2]);
+ pass_to_client = 0;
+ }
+ if(!client->auth_confirmed) {
+ if(argc == 4 && !stricmp(argv[1], "NOTICE") && !stricmp(argv[2], "AUTH") && !stricmp(argv[3], "*** Login accepted")) {
+ client->auth_confirmed = 1;
+ } else if(argc > 1 && !stricmp(argv[1], "001")) {
+ usersession_error(session, "IRC Session closed (Please use Login on Connect)");
+ pass_to_client = 0;
+ }
+ } else {
+
+ }
+ if(pass_to_client)
+ usersession_client_raw(session, line);
+}
+
+static void ircclient_callback(struct IOEvent *event) {
+ struct IRCClient *client = event->iofd->data;
+ switch(event->type) {
+ case IOEVENT_NOTCONNECTED:
+ usersession_error(client->session, "Could not connect to the Server.");
+ break;
+ case IOEVENT_CONNECTED:
+ ircclient_handshake(client);
+ break;
+ case IOEVENT_RECV:
+ ircclient_recv(client, event->data.recv_str);
+ break;
+ case IOEVENT_CLOSED:
+ usersession_error(client->session, "Disconnected from the IRC Server.");
+ break;
+ default:
+ break;
+ }
+}
+
+void ircclient_send(struct IRCClient *client, char *line) {
+ iohandler_printf(client->iofd, "%s", line);
+}