static void session_save_server(SERVER_REC *server, CONFIG_REC *config, CONFIG_NODE *node) { int handle; node = config_node_section(node, NULL, NODE_TYPE_BLOCK); config_node_set_str(config, node, "chat_type", chat_protocol_find_id(server->chat_type)->name); config_node_set_str(config, node, "address", server->connrec->address); config_node_set_int(config, node, "port", server->connrec->port); config_node_set_str(config, node, "chatnet", server->connrec->chatnet); config_node_set_str(config, node, "password", server->connrec->password); config_node_set_str(config, node, "nick", server->nick); config_node_set_bool(config, node, "use_ssl", server->connrec->use_ssl); handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle)); config_node_set_int(config, node, "handle", handle); signal_emit("session save server", 3, server, config, node); /* fake the server disconnection */ g_io_channel_unref(net_sendbuffer_handle(server->handle)); net_sendbuffer_destroy(server->handle, FALSE); server->handle = NULL; server->connection_lost = TRUE; server->no_reconnect = TRUE; server_disconnect(server); }
static void dcc_chat_passive(CHAT_DCC_REC *dcc) { IPADDR own_ip; int port; GIOChannel *handle; char host[MAX_IP_LEN]; g_return_if_fail(IS_DCC_CHAT(dcc)); if (dcc->addrstr[0] == '\0' || dcc->starttime != 0 || dcc->handle != NULL) { /* already sent a chat request / already chatting */ return; } handle = dcc_listen(net_sendbuffer_handle(dcc->server->handle), &own_ip, &port); if (handle == NULL) cmd_return_error(CMDERR_ERRNO); dcc->handle = handle; dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ, (GInputFunction) dcc_chat_listen, dcc); /* Let's send the reply to the other client! */ dcc_ip2str(&own_ip, host); irc_send_cmdv(dcc->server, "PRIVMSG %s :\001DCC CHAT CHAT %s %d %d\001", dcc->nick, host, port, dcc->pasv_id); }
GIOChannel *net_start_ssl(SERVER_REC *server) { GIOChannel *handle, *ssl_handle; g_return_val_if_fail(server != NULL, NULL); handle = net_sendbuffer_handle(server->handle); if (handle == NULL) return NULL; ssl_handle = irssi_ssl_get_iochannel(handle, server->connrec->port, server); return ssl_handle; }
void server_disconnect(SERVER_REC *server) { int chans; g_return_if_fail(IS_SERVER(server)); if (server->disconnected) return; if (server->connect_tag != -1) { /* still connecting to server.. */ if (server->connect_pid != -1) net_disconnect_nonblock(server->connect_pid); server_connect_failed(server, NULL); return; } servers = g_slist_remove(servers, server); server->disconnected = TRUE; signal_emit("server disconnected", 1, server); /* close all channels */ chans = server_remove_channels(server); if (server->handle != NULL) { if (!chans || server->connection_lost) net_sendbuffer_destroy(server->handle, TRUE); else { /* we were on some channels, try to let the server disconnect so that our quit message is guaranteed to get displayed */ net_disconnect_later(net_sendbuffer_handle(server->handle)); net_sendbuffer_destroy(server->handle, FALSE); } server->handle = NULL; } if (server->readtag > 0) { g_source_remove(server->readtag); server->readtag = -1; } server_unref(server); }
/* SYNTAX: DCC SERVER [+|-scf] [port] */ static void cmd_dcc_server(const char *data, IRC_SERVER_REC *server) { void *free_arg; GIOChannel *handle; SERVER_DCC_REC *dcc; IPADDR own_ip; char *flags, *port; g_return_if_fail(data != NULL); if (!cmd_get_params(data, &free_arg, 2, &flags, &port)) return; dcc = dcc_server_find_port(port); if (dcc != NULL) { /* Server is already running, update it */ dcc_server_update_flags(dcc, flags); cmd_params_free(free_arg); return; } /* start listening */ if (!IS_IRC_SERVER(server) || !server->connected) { cmd_param_error(CMDERR_NOT_CONNECTED); } handle = dcc_listen_port(net_sendbuffer_handle(server->handle), &own_ip, atoi(port)); if (handle == NULL) { cmd_param_error(CMDERR_ERRNO); } dcc = dcc_server_create(server, flags); dcc->handle = handle; dcc->port = atoi(port); dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ, (GInputFunction) dcc_server_listen, dcc); signal_emit("dcc server started", 1, dcc); cmd_params_free(free_arg); }
void dcc_get_passive(GET_DCC_REC *dcc) { GIOChannel *handle; IPADDR own_ip; int port; char host[MAX_IP_LEN]; handle = dcc_listen(net_sendbuffer_handle(dcc->server->handle), &own_ip, &port); if (handle == NULL) cmd_return_error(CMDERR_ERRNO); dcc->handle = handle; dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ, (GInputFunction) dcc_get_listen, dcc); /* Let's send the reply to the other client! */ dcc_ip2str(&own_ip, host); irc_send_cmdv(dcc->server, "PRIVMSG %s :\001DCC SEND %s %s %d %"PRIuUOFF_T" %d\001", dcc->nick, dcc->arg, host, port, dcc->size, dcc->pasv_id); }
static void quassel_chan_read(Quassel_CHANNEL_REC* chanrec) { GIOChannel *giochan = net_sendbuffer_handle(chanrec->server->handle); quassel_set_last_seen_msg(giochan, chanrec->buffer_id, chanrec->last_msg_id); quassel_set_marker(giochan, chanrec->buffer_id, chanrec->last_msg_id); quassel_mark_as_read(giochan, chanrec->buffer_id); }
gboolean proxy_init(void) { gchar ipaddr[MAX_IP_LEN]; const char *password; const char *addr; int port; proxy_settings_init(); proxy_data = g_new0(PLUGIN_DATA, 1); password = settings_get_str("proxy_listen_password"); addr = settings_get_str("proxy_listen_addr"); port = settings_get_int("proxy_listen_port"); plug = module_find("proxy"); proxy_data->plugin = plug; if (*password != '\0') { /* args = password */ proxy_data->password = g_strdup(password); } if (*addr != '\0') { /* specify ip address to listen */ net_host2ip(addr, &proxy_data->ip); } if (port != 0) { /* specify port to use */ proxy_data->port = port; } if (proxy_data->password == NULL) { /* no password - bad idea! */ printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE, "Warning!! Password not specified, everyone can use this proxy! Use /set proxy_listen_password <password> to set it"); } if (servers == NULL) { /* FIXME: not good */ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "You need to specify IP address to listen with /set proxy_listen_addr <address>"); deinit(); return FALSE; } else { SERVER_REC *server; server = servers->data; if (net_getsockname(net_sendbuffer_handle(server->handle), &proxy_data->ip, NULL)) { deinit(); return FALSE; } } net_ip2host(&proxy_data->ip, ipaddr); printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE, "Proxy plugin loaded - listening in interface %s port %d", ipaddr, proxy_data->port); plugin_proxy_listen_init(proxy_data); proxy_data->loaded = TRUE; return TRUE; }
static void sig_listen_client(CLIENT_REC *client, gint handle) { char tmpbuf[1024], *str, *cmd, *args, *p; int ret, recvlen; g_return_if_fail(client != NULL); for (;;) { recvlen = net_receive(handle, tmpbuf, sizeof(tmpbuf)); ret = line_split(tmpbuf, recvlen, &str, &client->buffer); if (ret == -1) { /* connection lost */ remove_client(proxy_data, client); break; } if (ret == 0) break; if (client->server == NULL) continue; cmd = g_strdup(str); args = strchr(cmd, ' '); if (args != NULL) *args++ = '\0'; else args = ""; if (*args == ':') args++; g_strup(cmd); if (!client->connected) { if (proxy_data->password != NULL && strcmp(cmd, "PASS") == 0) { if (strcmp(proxy_data->password, args) != 0) { /* wrong password! */ remove_client(proxy_data, client); break; } client->pass_sent = TRUE; } else if (strcmp(cmd, "NICK") == 0) client->nick = g_strdup(args); else if (strcmp(cmd, "USER") == 0) { if (client->nick == NULL || (proxy_data->password != NULL && !client->pass_sent)) { /* stupid client didn't send us NICK/PASS or, kill it */ remove_client(proxy_data, client); break; } client->connected = TRUE; plugin_proxy_dump_data(client); } } else if (strcmp(cmd, "QUIT") == 0) { remove_client(proxy_data, client); break; } else if (strcmp(cmd, "PING") == 0) { net_transmit(handle, "PONG proxy :nick\n", 17); } else { net_transmit(net_sendbuffer_handle(client->server->handle), str, strlen(str)); net_transmit(net_sendbuffer_handle(client->server->handle), "\n", 1); if (strcmp(cmd, "WHO") == 0) { grab_who(client, args); } else if (strcmp(cmd, "WHOIS") == 0) { /* convert dots to spaces */ for (p = args; *p != '\0'; p++) if (*p == ',') *p = ' '; proxy_redirect_event(client, args, 2, "event 318", -1, "event 402", -1, "event 401", 1, "event 311", 1, "event 301", 1, "event 312", 1, "event 313", 1, "event 317", 1, "event 319", 1, NULL); } else if (strcmp(cmd, "ISON") == 0) { proxy_redirect_event(client, NULL, 1, "event 303", -1, NULL); } else if (strcmp(cmd, "USERHOST") == 0) { proxy_redirect_event(client, args, 1, "event 302", -1, "event 401", 1, NULL); } else if (strcmp(cmd, "MODE") == 0) { /* convert dots to spaces */ gchar *slist, *str, mode; gint argc; p = strchr(args, ' '); if (p != NULL) *p++ = '\0'; mode = p == NULL ? '\0' : *p; slist = g_strdup(args); argc = 1; for (p = slist; *p != '\0'; p++) { if (*p == ',') { *p = ' '; argc++; } } /* get channel mode / bans / exception / invite list */ str = g_strdup_printf("%s %s", args, slist); switch (mode) { case '\0': while (argc-- > 0) proxy_redirect_event(client, str, 3, "event 403", 1, "event 443", 1, "event 324", 1, NULL); break; case 'b': while (argc-- > 0) proxy_redirect_event(client, str, 2, "event 403", 1, "event 368", 1, "event 367", 1, NULL); break; case 'e': while (argc-- > 0) proxy_redirect_event(client, str, 4, "event 403", 1, "event 482", 1, "event 472", -1, "event 349", 1, "event 348", 1, NULL); break; case 'I': while (argc-- > 0) proxy_redirect_event(client, str, 4, "event 403", 1, "event 482", 1, "event 472", -1, "event 347", 1, "event 346", 1, NULL); break; } g_free(str); g_free(slist); } } g_free(cmd); } }
/* SYNTAX: DCC CHAT [-passive] [<nick>] */ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server) { void *free_arg; CHAT_DCC_REC *dcc; IPADDR own_ip; GIOChannel *handle; GHashTable *optlist; int p_id; char *nick, host[MAX_IP_LEN]; int port; g_return_if_fail(data != NULL); if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS, "dcc chat", &optlist, &nick)) return; if (*nick == '\0') { dcc = DCC_CHAT(dcc_find_request_latest(DCC_CHAT_TYPE)); if (dcc != NULL) { if (!dcc_is_passive(dcc)) dcc_chat_connect(dcc); else dcc_chat_passive(dcc); } cmd_params_free(free_arg); return; } dcc = dcc_chat_find_id(nick); if (dcc != NULL && dcc_is_waiting_user(dcc)) { if (!dcc_is_passive(dcc)) { /* found from dcc chat requests, we're the connecting side */ dcc_chat_connect(dcc); } else { /* We are accepting a passive DCC CHAT. */ dcc_chat_passive(dcc); } cmd_params_free(free_arg); return; } if (dcc != NULL && dcc_is_listening(dcc) && dcc->server == server) { /* sending request again even while old request is still waiting, remove it. */ dcc_destroy(DCC(dcc)); } if (!IS_IRC_SERVER(server) || !server->connected) cmd_param_error(CMDERR_NOT_CONNECTED); dcc = dcc_chat_create(server, NULL, nick, "chat"); if (dcc == NULL) { cmd_params_free(free_arg); g_warn_if_reached(); return; } if (g_hash_table_lookup(optlist, "passive") == NULL) { /* Standard DCC CHAT... let's listen for incoming connections */ handle = dcc_listen(net_sendbuffer_handle(server->handle), &own_ip, &port); if (handle == NULL) cmd_param_error(CMDERR_ERRNO); dcc->handle = handle; dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ, (GInputFunction) dcc_chat_listen, dcc); /* send the chat request */ signal_emit("dcc request send", 1, dcc); dcc_ip2str(&own_ip, host); irc_send_cmdv(server, "PRIVMSG %s :\001DCC CHAT CHAT %s %d\001", nick, host, port); } else { /* Passive protocol... we want the other side to listen */ /* send the chat request */ dcc->port = 0; signal_emit("dcc request send", 1, dcc); /* generate a random id */ p_id = rand() % 64; dcc->pasv_id = p_id; /* 16843009 is the long format of 1.1.1.1, we use a fake IP since the other side shouldn't care of it: they will send the address for us to connect to in the reply */ irc_send_cmdv(server, "PRIVMSG %s :\001DCC CHAT CHAT 16843009 0 %d\001", nick, p_id); } cmd_params_free(free_arg); }
static int dcc_send_one_file(int queue, const char *target, const char *fname, IRC_SERVER_REC *server, CHAT_DCC_REC *chat) { struct stat st; char *str; char host[MAX_IP_LEN]; int hfile, port; SEND_DCC_REC *dcc; IPADDR own_ip; GIOChannel *handle; if (dcc_find_request(DCC_SEND_TYPE, target, fname)) { signal_emit("dcc error send exists", 2, target, fname); return FALSE; } str = dcc_send_get_file(fname); hfile = open(str, O_RDONLY); g_free(str); if (hfile == -1) { signal_emit("dcc error file open", 3, target, fname, GINT_TO_POINTER(errno)); return FALSE; } if (fstat(hfile, &st) < 0) { g_warning("fstat() failed: %s", strerror(errno)); close(hfile); return FALSE; } /* start listening */ handle = dcc_listen(chat != NULL ? chat->handle : net_sendbuffer_handle(server->handle), &own_ip, &port); if (handle == NULL) { close(hfile); g_warning("dcc_listen() failed: %s", strerror(errno)); return FALSE; } fname = g_basename(fname); /* Replace all the spaces with underscore so that lesser intellgent clients can communicate.. */ if (!settings_get_bool("dcc_send_replace_space_with_underscore")) str = NULL; else { str = g_strdup(fname); g_strdelimit(str, " ", '_'); fname = str; } dcc = dcc_send_create(server, chat, target, fname); g_free(str); dcc->handle = handle; dcc->port = port; dcc->size = st.st_size; dcc->fhandle = hfile; dcc->queue = queue; dcc->file_quoted = strchr(fname, ' ') != NULL; dcc->tagconn = g_input_add(handle, G_INPUT_READ, (GInputFunction) dcc_send_connected, dcc); /* send DCC request */ signal_emit("dcc request send", 1, dcc); dcc_ip2str(&own_ip, host); str = g_strdup_printf(dcc->file_quoted ? "DCC SEND \"%s\" %s %d %"PRIuUOFF_T : "DCC SEND %s %s %d %"PRIuUOFF_T, dcc->arg, host, port, dcc->size); dcc_ctcp_message(server, target, chat, FALSE, str); g_free(str); return TRUE; }
/* SYNTAX: DCC SEND <nick> <file> */ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server, WI_IRC_REC *item) { char *target, *fname, *str, *ptr; void *free_arg; char host[MAX_IP_LEN]; int hfile, hlisten, port; long fsize; DCC_REC *dcc, *chat; IPADDR own_ip; g_return_if_fail(data != NULL); if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &fname)) return; if (*target == '\0' || *fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); /* if we're in dcc chat, send the request via it. */ chat = item_get_dcc(item); if (chat != NULL && (chat->mirc_ctcp || g_strcasecmp(target, chat->nick) != 0)) chat = NULL; if ((server == NULL || !server->connected) && chat == NULL) cmd_param_error(CMDERR_NOT_CONNECTED); if (dcc_find_item(DCC_TYPE_SEND, target, fname)) { signal_emit("dcc error send exists", 2, target, fname); cmd_params_free(free_arg); return; } str = convert_home(fname); if (!g_path_is_absolute(str)) { char *path; g_free(str); path = convert_home(settings_get_str("dcc_upload_path")); str = g_strconcat(path, G_DIR_SEPARATOR_S, fname, NULL); g_free(path); } hfile = open(str, O_RDONLY); g_free(str); if (hfile == -1) { signal_emit("dcc error file not found", 2, target, fname); cmd_params_free(free_arg); return; } fsize = lseek(hfile, 0, SEEK_END); lseek(hfile, 0, SEEK_SET); /* get the IP address we use with IRC server */ if (net_getsockname(chat != NULL ? chat->handle : net_sendbuffer_handle(server->handle), &own_ip, NULL) == -1) { close(hfile); cmd_param_error(CMDERR_ERRNO); } /* start listening */ port = settings_get_int("dcc_port"); hlisten = net_listen(&own_ip, &port); if (hlisten == -1) { close(hfile); cmd_param_error(CMDERR_ERRNO); } /* skip path, change all spaces to _ */ fname = g_strdup(g_basename(fname)); for (ptr = fname; *ptr != '\0'; ptr++) if (*ptr == ' ') *ptr = '_'; dcc = dcc_create(DCC_TYPE_SEND, hlisten, target, fname, server, chat); dcc->port = port; dcc->size = fsize; dcc->fhandle = hfile; dcc->tagconn = g_input_add(hlisten, G_INPUT_READ, (GInputFunction) dcc_send_init, dcc); /* send DCC request */ dcc_make_address(&own_ip, host); str = g_strdup_printf("DCC SEND %s %s %d %lu", fname, host, port, fsize); dcc_ctcp_message(target, server, chat, FALSE, str); g_free(str); g_free(fname); cmd_params_free(free_arg); }