int servlist_save (void) { FILE *fp; char *buf; ircnet *net; ircserver *serv; commandentry *cmd; favchannel *favchan; GSList *list; GSList *netlist; GSList *cmdlist; GSList *favlist; #ifndef WIN32 int first = FALSE; buf = g_build_filename (get_xdir (), "servlist.conf", NULL); if (g_access (buf, F_OK) != 0) first = TRUE; #endif fp = hextor_fopen_file ("servlist.conf", "w", 0); if (!fp) { #ifndef WIN32 g_free (buf); #endif return FALSE; } #ifndef WIN32 if (first) g_chmod (buf, 0600); g_free (buf); #endif fprintf (fp, "v=" PACKAGE_VERSION "\n\n"); list = network_list; while (list) { net = list->data; fprintf (fp, "N=%s\n", net->name); if (net->nick) fprintf (fp, "I=%s\n", net->nick); if (net->nick2) fprintf (fp, "i=%s\n", net->nick2); if (net->user) fprintf (fp, "U=%s\n", net->user); if (net->real) fprintf (fp, "R=%s\n", net->real); if (net->pass) fprintf (fp, "P=%s\n", net->pass); if (net->logintype) fprintf (fp, "L=%d\n", net->logintype); if (net->encoding) { fprintf (fp, "E=%s\n", net->encoding); if (!servlist_check_encoding (net->encoding)) { buf = g_strdup_printf (_("Warning: \"%s\" character set is unknown. No conversion will be applied for network %s."), net->encoding, net->name); fe_message (buf, FE_MSG_WARN); g_free (buf); } } fprintf (fp, "F=%d\nD=%d\n", net->flags, net->selected); netlist = net->servlist; while (netlist) { serv = netlist->data; fprintf (fp, "S=%s\n", serv->hostname); netlist = netlist->next; } cmdlist = net->commandlist; while (cmdlist) { cmd = cmdlist->data; fprintf (fp, "C=%s\n", cmd->command); cmdlist = cmdlist->next; } favlist = net->favchanlist; while (favlist) { favchan = favlist->data; if (favchan->key) { fprintf (fp, "J=%s,%s\n", favchan->name, favchan->key); } else { fprintf (fp, "J=%s\n", favchan->name); } favlist = favlist->next; } if (fprintf (fp, "\n") < 1) { fclose (fp); return FALSE; } list = list->next; } fclose (fp); return TRUE; }
int servlist_save (void) { FILE *fp; char buf[256]; ircnet *net; ircserver *serv; GSList *list; GSList *hlist; #ifndef WIN32 int first = FALSE; snprintf (buf, sizeof (buf), "%s/servlist_.conf", get_xdir_fs ()); if (access (buf, F_OK) != 0) first = TRUE; #endif fp = xchat_fopen_file ("servlist_.conf", "w", 0); if (!fp) return FALSE; #ifndef WIN32 if (first) chmod (buf, 0600); #endif fprintf (fp, "v="PACKAGE_VERSION"\n\n"); list = network_list; while (list) { net = list->data; fprintf (fp, "N=%s\n", net->name); if (net->nick) fprintf (fp, "I=%s\n", net->nick); if (net->nick2) fprintf (fp, "i=%s\n", net->nick2); if (net->user) fprintf (fp, "U=%s\n", net->user); if (net->real) fprintf (fp, "R=%s\n", net->real); if (net->pass) fprintf (fp, "P=%s\n", net->pass); if (net->autojoin) fprintf (fp, "J=%s\n", net->autojoin); if (net->nickserv) fprintf (fp, "B=%s\n", net->nickserv); if (net->encoding && g_ascii_strcasecmp (net->encoding, "System") && g_ascii_strcasecmp (net->encoding, "System default")) { fprintf (fp, "E=%s\n", net->encoding); if (!servlist_check_encoding (net->encoding)) { snprintf (buf, sizeof (buf), _("Warning: \"%s\" character set is unknown. No conversion will be applied for network %s."), net->encoding, net->name); fe_message (buf, FE_MSG_WARN); } } if (net->command) token_foreach (net->command, '\n', servlist_write_ccmd, fp); fprintf (fp, "F=%d\nD=%d\n", net->flags, net->selected); hlist = net->servlist; while (hlist) { serv = hlist->data; fprintf (fp, "S=%s\n", serv->hostname); hlist = hlist->next; } if (fprintf (fp, "\n") < 1) { fclose (fp); return FALSE; } list = list->next; } fclose (fp); return TRUE; }
static int servlist_load (void) { FILE *fp; char buf[2048]; int len; ircnet *net = NULL; /* simple migration we will keep for a short while */ char *oldfile = g_build_filename (get_xdir (), "servlist_.conf", NULL); char *newfile = g_build_filename (get_xdir (), "servlist.conf", NULL); if (g_file_test (oldfile, G_FILE_TEST_EXISTS) && !g_file_test (newfile, G_FILE_TEST_EXISTS)) { g_rename (oldfile, newfile); } g_free (oldfile); g_free (newfile); fp = hextor_fopen_file ("servlist.conf", "r", 0); if (!fp) return FALSE; while (fgets (buf, sizeof (buf) - 2, fp)) { len = strlen (buf); buf[len] = 0; buf[len-1] = 0; /* remove the trailing \n */ if (net) { switch (buf[0]) { case 'I': net->nick = g_strdup (buf + 2); break; case 'i': net->nick2 = g_strdup (buf + 2); break; case 'U': net->user = g_strdup (buf + 2); break; case 'R': net->real = g_strdup (buf + 2); break; case 'P': net->pass = g_strdup (buf + 2); break; case 'L': net->logintype = atoi (buf + 2); break; case 'E': net->encoding = servlist_check_encoding (buf + 2) ? g_strdup (buf + 2) : g_strdup ("UTF-8"); break; case 'F': net->flags = atoi (buf + 2); break; case 'S': /* new server/hostname for this network */ servlist_server_add (net, buf + 2); break; case 'C': servlist_command_add (net, buf + 2); break; case 'J': servlist_favchan_add (net, buf + 2); break; case 'D': net->selected = atoi (buf + 2); break; /* FIXME Migration code. In 2.9.5 the order was: * * P=serverpass, A=saslpass, B=nickservpass * * So if server password was unset, we can safely use SASL * password for our new universal password, or if that's also * unset, use NickServ password. * * Should be removed at some point. */ case 'A': if (!net->pass) { net->pass = g_strdup (buf + 2); if (!net->logintype) { net->logintype = LOGIN_SASL; } } case 'B': if (!net->pass) { net->pass = g_strdup (buf + 2); if (!net->logintype) { net->logintype = LOGIN_NICKSERV; } } } } if (buf[0] == 'N') net = servlist_net_add (buf + 2, /* comment */ NULL, FALSE); } fclose (fp); return TRUE; }
bool servlist_save (void) { namespace bfs = boost::filesystem; namespace bs = boost::system; auto outpath = io::fs::make_config_path("servlist.conf"); #ifndef WIN32 bs::error_code ec; bool first = !bfs::exists(outpath, ec); #endif bfs::ofstream outfile(outpath, std::ios::out | std::ios::trunc); if (!outfile) { return false; } #ifndef WIN32 if (first) { bfs::permissions(outpath, bfs::owner_read | bfs::owner_write, ec); } #endif outfile << "v=" PACKAGE_VERSION "\n\n"; for(const auto & net : glib_helper::glist_iterable<ircnet>(network_list)) { outfile << "N=" << net.name << '\n'; if (net.nick) outfile << "I=" << net.nick.get() << '\n'; if (net.nick2) outfile << "i=" << net.nick2.get() << '\n'; if (net.user) outfile << "U=" << net.user << '\n'; if (net.real) outfile << "R=" << net.real << '\n'; if (net.pass) outfile << "P=" << net.pass << '\n'; if (net.logintype) outfile << "L=" << net.logintype << '\n'; if (net.encoding && g_ascii_strcasecmp (net.encoding, "System") && g_ascii_strcasecmp (net.encoding, "System default")) { outfile << "E=" << net.encoding << '\n'; if (!servlist_check_encoding (net.encoding)) { std::ostringstream buffer; buffer << boost::format(_("Warning: \"%s\" character set is unknown. No conversion will be applied for network %s.")) % net.encoding % net.name; fe_message (buffer.str(), FE_MSG_WARN); } } outfile << "F=" << net.flags << "\nD=" << net.selected << '\n'; for (const auto & serv : glib_helper::glist_iterable<ircserver>(net.servlist)) { outfile << "S=" << serv.hostname << '\n'; } for(const auto & cmd : glib_helper::glist_iterable<commandentry>(net.commandlist)) { outfile << "C=" << cmd.command << '\n'; } for(const auto & favchan : glib_helper::glist_iterable<favchannel>(net.favchanlist)) { outfile << "J=" << favchan.name; if (favchan.key) { outfile << ',' << favchan.key.get(); } outfile << '\n'; } if (!(outfile << '\n')) { return false; } } return true; }