Now inbound_cap_ls() can enable extensions when a bouncer uses a namespace for the...
[rofl0r-ixchat.git] / src / common / servlist.c
blob5f77bc736a98420f73376a44e38b02f874f7824b
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
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 2 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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
26 #include "xchat.h"
27 #include <glib.h>
29 #include "cfgfiles.h"
30 #include "fe.h"
31 #include "server.h"
32 #include "text.h"
33 #include "util.h" /* token_foreach */
34 #include "xchatc.h"
36 #include "servlist.h"
39 struct defaultserver
41 char *network;
42 char *host;
43 char *channel;
44 char *charset;
45 int loginmode; /* default authentication type */
48 static const struct defaultserver def[] =
50 {"2600net", 0},
51 {0, "irc.2600.net"},
53 {"7-indonesia", 0},
54 {0, "irc.7-indonesia.org"},
56 {"AccessIRC", 0},
57 {0, "irc.accessirc.net"},
58 {0, "eu.accessirc.net"},
60 {"AfterNET", 0},
61 {0, "irc.afternet.org"},
62 {0, "us.afternet.org"},
63 {0, "eu.afternet.org"},
65 {"Aitvaras", 0},
66 #ifdef USE_IPV6
67 #ifdef USE_OPENSSL
68 {0, "irc6.ktu.lt/+7668"},
69 #endif
70 {0, "irc6.ktu.lt/7666"},
71 #endif
72 #ifdef USE_OPENSSL
73 {0, "irc.data.lt/+6668"},
74 {0, "irc-ssl.omnitel.net/+6668"},
75 {0, "irc-ssl.le.lt/+9999"},
76 #endif
77 {0, "irc.data.lt"},
78 {0, "irc.omnitel.net"},
79 {0, "irc.ktu.lt"},
80 {0, "irc.le.lt"},
81 {0, "irc.takas.lt"},
82 {0, "irc.5ci.net"},
83 {0, "irc.kis.lt"},
85 {"AmigaNet", 0},
86 {0, "irc.amiganet.org"},
87 {0, "us.amiganet.org"},
88 {0, "uk.amiganet.org"},
89 /* {0, "no.amiganet.org"},
90 {0, "au.amiganet.org"},*/
92 {"ARCNet", 0},
93 {0, "se1.arcnet.vapor.com"},
94 {0, "us1.arcnet.vapor.com"},
95 {0, "us2.arcnet.vapor.com"},
96 {0, "us3.arcnet.vapor.com"},
97 {0, "ca1.arcnet.vapor.com"},
98 {0, "de1.arcnet.vapor.com"},
99 {0, "de3.arcnet.vapor.com"},
100 {0, "ch1.arcnet.vapor.com"},
101 {0, "be1.arcnet.vapor.com"},
102 {0, "nl3.arcnet.vapor.com"},
103 {0, "uk1.arcnet.vapor.com"},
104 {0, "uk2.arcnet.vapor.com"},
105 /* {0, "uk3.arcnet.vapor.com"},*/
106 {0, "fr1.arcnet.vapor.com"},
108 {"AstroLink", 0},
109 {0, "irc.astrolink.org"},
111 {"AustNet", 0},
112 {0, "au.austnet.org"},
113 {0, "us.austnet.org"},
114 {0, "ca.austnet.org"},
116 /* {"AxeNet", 0},
117 {0, "irc.axenet.org"},
118 {0, "angel.axenet.org"},
119 {0, "energy.axenet.org"},
120 {0, "python.axenet.org"},*/
122 {"AzzurraNet", 0},
123 {0, "irc.azzurra.org"},
124 {0, "crypto.azzurra.org"},
126 {"Beirut", 0},
127 {0, "irc.beirut.com"},
129 {"ChattingAway", 0},
130 {0, "irc.chattingaway.com"},
132 {"ChatJunkies", 0, "#xchat"},
133 {0, "irc.chatjunkies.org"},
134 {0, "nl.chatjunkies.org"},
136 {"ChatNet", 0},
137 {0, "US.ChatNet.Org"},
138 {0, "EU.ChatNet.Org"},
140 {"ChatSociety", 0},
141 {0, "us.chatsociety.net"},
142 {0, "eu.chatsociety.net"},
144 {"ChatSpike", 0},
145 {0, "irc.chatspike.net"},
147 {"CoolChat", 0},
148 {0, "irc.coolchat.net"},
149 /* {0, "unix.coolchat.net"},
150 {0, "toronto.coolchat.net"},*/
152 {"Criten", 0},
153 {0, "irc.criten.net"},
154 {0, "irc.eu.criten.net"},
156 {"DALnet", 0},
157 {0, "irc.dal.net"},
158 {0, "irc.eu.dal.net"},
160 {"Dark-Tou-Net", 0},
161 {0, "irc.d-t-net.de"},
162 {0, "bw.d-t-net.de"},
163 {0, "nc.d-t-net.de"},
164 {0, "wakka.d-t-net.de"},
166 {"DarkMyst", 0},
167 {0, "irc.darkmyst.org"},
169 {"DeepIRC", 0},
170 {0, "irc.deepirc.net"},
172 {"DeltaAnime", 0},
173 {0, "irc.deltaanime.net"},
175 {"EFnet", 0},
176 {0, "irc.blackened.com"},
177 {0, "irc.Prison.NET"},
178 {0, "irc.Qeast.net"},
179 {0, "irc.efnet.pl"},
180 {0, "efnet.demon.co.uk"},
181 /* {0, "irc.lagged.org"},*/
182 {0, "irc.lightning.net"},
183 {0, "irc.mindspring.com"},
184 {0, "irc.easynews.com"},
185 {0, "irc.servercentral.net"},
187 {"EnterTheGame", 0},
188 {0, "IRC.EnterTheGame.Com"},
190 {"EUIrc", 0},
191 {0, "irc.euirc.net"},
192 {0, "irc.ham.de.euirc.net"},
193 {0, "irc.ber.de.euirc.net"},
194 {0, "irc.ffm.de.euirc.net"},
195 {0, "irc.bre.de.euirc.net"},
196 {0, "irc.hes.de.euirc.net"},
197 {0, "irc.vie.at.euirc.net"},
198 {0, "irc.inn.at.euirc.net"},
199 {0, "irc.bas.ch.euirc.net"},
201 {"EuropNet", 0},
202 {0, "irc.europnet.org"},
204 {"EU-IRC", 0},
205 {0, "irc.eu-irc.net"},
207 {"FDFNet", 0},
208 {0, "irc.fdfnet.net"},
209 {0, "irc.eu.fdfnet.net"},
211 {"FEFNet", 0},
212 {0, "irc.fef.net"},
213 {0, "irc.ggn.net"},
214 {0, "irc.vendetta.com"},
216 {"freenode", 0, "#xchat", 0, LOGIN_SASL},
217 {0, "irc.freenode.net"},
219 /* {"Freeworld", 0},
220 {0, "kabel.freeworld.nu"},
221 {0, "irc.freeworld.nu"},*/
223 {"GalaxyNet", 0},
224 {0, "irc.galaxynet.org"},
225 /* {0, "sprynet.us.galaxynet.org"},
226 {0, "atlanta.ga.us.galaxynet.org"},*/
228 {"GamesNET", 0},
229 {0, "irc.gamesnet.net"},
230 /* {0, "irc.us.gamesnet.net"},
231 {0, "east.us.gamesnet.net"},
232 {0, "west.us.gamesnet.net"},*/
233 {0, "irc.ca.gamesnet.net"},
234 {0, "irc.eu.gamesnet.net"},
236 {"GeekShed", 0},
237 {0, "irc.geekshed.net"},
239 {"German-Elite", 0},
240 {0, "dominion.german-elite.net"},
241 {0, "komatu.german-elite.net"},
242 /* {0, "liberty.german-elite.net"},*/
244 {"GimpNet", 0},
245 {0, "irc.gimp.org"},
246 /* {0, "irc.au.gimp.org"},*/
247 {0, "irc.us.gimp.org"},
249 {"HabberNet", 0},
250 {0, "irc.habber.net"},
252 {"Hashmark", 0},
253 {0, "irc.hashmark.net"},
255 {"IdleMonkeys", 0},
256 {0, "irc.idlemonkeys.net"},
258 /* {"Infinity-IRC", 0},
259 {0, "Atlanta.GA.US.Infinity-IRC.Org"},
260 {0, "Babylon.NY.US.Infinity-IRC.Org"},
261 {0, "Sunshine.Ca.US.Infinity-IRC.Org"},
262 {0, "IRC.Infinity-IRC.Org"},*/
264 {"iZ-smart.net", 0},
265 {0, "irc.iZ-smart.net/6666"},
266 {0, "irc.iZ-smart.net/6667"},
267 {0, "irc.iZ-smart.net/6668"},
269 {"IrcLink", 0},
270 {0, "irc.irclink.net"},
271 {0, "Alesund.no.eu.irclink.net"},
272 {0, "Oslo.no.eu.irclink.net"},
273 {0, "frogn.no.eu.irclink.net"},
274 {0, "tonsberg.no.eu.irclink.net"},
276 {"IRCNet", 0},
277 {0, "irc.ircnet.com"},
278 {0, "irc.stealth.net/6668"},
279 {0, "ircnet.demon.co.uk"},
280 /* {0, "ircnet.hinet.hr"},*/
281 {0, "irc.datacomm.ch"},
282 /* {0, "ircnet.kaptech.fr"},
283 {0, "ircnet.easynet.co.uk"},*/
284 {0, "random.ircd.de"},
285 {0, "ircnet.netvision.net.il"},
286 /* {0, "irc.seed.net.tw"},*/
287 {0, "irc.cs.hut.fi"},
289 {"Irctoo.net", 0},
290 {0, "irc.irctoo.net"},
292 {"Krstarica", 0},
293 {0, "irc.krstarica.com"},
295 {"Librenet", 0},
296 {0, "irc.librenet.net"},
297 {0, "ielf.fr.librenet.net"},
299 {"LinkNet", 0},
300 {0, "irc.link-net.org"},
301 {0, "irc.no.link-net.org"},
302 /* {0, "irc.gamesden.net.au"},*/
303 {0, "irc.bahnhof.se"},
304 /* {0, "irc.kinexuseurope.co.uk"},
305 {0, "irc.gamiix.com"},*/
307 {"MagicStar", 0},
308 {0, "irc.magicstar.net"},
310 {"Majistic", 0},
311 {0, "irc.majistic.net"},
313 {"MindForge", 0},
314 {0, "irc.mindforge.org"},
316 {"MintIRC", 0},
317 {0, "irc.mintirc.net"},
319 {"MIXXnet", 0},
320 {0, "irc.mixxnet.net"},
322 {"NeverNET", 0},
323 {0, "irc.nevernet.net"},
324 {0, "imagine.nevernet.net"},
325 {0, "dimension.nevernet.net"},
326 {0, "universe.nevernet.net"},
327 {0, "wayland.nevernet.net"},
328 {0, "forte.nevernet.net"},
330 {"NixHelpNet", 0},
331 {0, "irc.nixhelp.org"},
332 {0, "us.nixhelp.org"},
333 {0, "uk.nixhelp.org"},
334 {0, "uk2.nixhelp.org"},
335 {0, "uk3.nixhelp.org"},
336 {0, "nl.nixhelp.org"},
337 {0, "ca.ld.nixhelp.org"},
338 {0, "us.co.nixhelp.org"},
339 {0, "us.ca.nixhelp.org"},
340 {0, "us.pa.nixhelp.org"},
342 {"NullusNet", 0},
343 {0, "irc.nullus.net"},
345 {"Oceanius", 0},
346 {0, "irc.oceanius.com"},
348 {"OFTC", 0},
349 {0, "irc.oftc.net"},
351 {"OtherNet", 0},
352 {0, "irc.othernet.org"},
354 {"OzNet", 0},
355 {0, "irc.oz.org"},
357 {"PIRC.PL", 0},
358 {0, "irc.pirc.pl"},
360 {"PTlink", 0},
361 {0, "irc.PTlink.net"},
362 {0, "aaia.PTlink.net"},
364 {"PTNet.org", 0},
365 {0, "irc.PTNet.org"},
366 {0, "world.PTnet.org"},
367 {0, "netvisao.PTnet.org"},
368 {0, "uevora.PTnet.org"},
369 {0, "vianetworks.PTnet.org"},
370 {0, "uc.PTnet.org"},
371 {0, "nfsi.ptnet.org"},
373 {"QuakeNet", 0, 0, 0, LOGIN_CHALLENGEAUTH},
374 {0, "irc.quakenet.org"},
375 {0, "irc.se.quakenet.org"},
376 {0, "irc.dk.quakenet.org"},
377 {0, "irc.no.quakenet.org"},
378 {0, "irc.fi.quakenet.org"},
379 {0, "irc.be.quakenet.org"},
380 {0, "irc.uk.quakenet.org"},
381 {0, "irc.de.quakenet.org"},
382 {0, "irc.it.quakenet.org"},
384 {"RebelChat", 0},
385 {0, "irc.rebelchat.org"},
387 /* {"Recycled-IRC", 0},
388 {0, "irc.recycled-irc.org"},
389 {0, "vermin.recycled-irc.org"},
390 {0, "waste.recycled-irc.org"},
391 {0, "lumber.recycled-irc.org"},
392 {0, "trash.recycled-irc.org"},
393 {0, "unwashed.recycled-irc.org"},
394 {0, "garbage.recycled-irc.org"},
395 {0, "dustbin.recycled-irc.org"},*/
397 {"RizeNET", 0},
398 {0, "irc.rizenet.org"},
399 {0, "omega.rizenet.org"},
400 {0, "evelance.rizenet.org"},
401 {0, "lisa.rizenet.org"},
402 {0, "scott.rizenet.org"},
404 {"Rizon", 0},
405 {0, "irc.rizon.net"},
407 {"RusNet", 0, 0, "KOI8-R (Cyrillic)"},
408 {0, "irc.tomsk.net"},
409 {0, "irc.rinet.ru"},
410 {0, "irc.run.net"},
411 {0, "irc.ru"},
412 {0, "irc.lucky.net"},
414 {"SceneNet", 0},
415 {0, "irc.scene.org"},
416 {0, "irc.eu.scene.org"},
417 {0, "irc.us.scene.org"},
419 {"SeilEn.de", 0},
420 {0, "irc.seilen.de"},
422 {"SlashNET", 0},
423 {0, "irc.slashnet.org"},
424 {0, "area51.slashnet.org"},
425 {0, "moo.slashnet.org"},
426 {0, "radon.slashnet.org"},
428 {"Sohbet.Net", 0},
429 {0, "irc.sohbet.net"},
431 {"SolidIRC", 0},
432 {0, "irc.solidirc.com"},
434 {"SorceryNet", 0},
435 {0, "irc.sorcery.net/9000"},
436 {0, "irc.us.sorcery.net/9000"},
437 {0, "irc.eu.sorcery.net/9000"},
439 {"Spidernet", 0},
440 {0, "us.spidernet.org"},
441 {0, "eu.spidernet.org"},
442 {0, "irc.spidernet.org"},
444 {"StarChat", 0},
445 {0, "irc.starchat.net"},
446 {0, "gainesville.starchat.net"},
447 {0, "freebsd.starchat.net"},
448 {0, "sunset.starchat.net"},
449 {0, "revenge.starchat.net"},
450 {0, "tahoma.starchat.net"},
451 {0, "neo.starchat.net"},
453 {"TNI3", 0},
454 {0, "irc.tni3.com"},
456 {"TURLINet", 0},
457 {0, "irc.turli.net"},
458 {0, "irc.servx.ru"},
459 {0, "irc.gavnos.ru"},
461 {"UnderNet", 0, 0, 0, LOGIN_CUSTOM},
462 {0, "us.undernet.org"},
463 {0, "eu.undernet.org"},
465 {"UniBG", 0, 0, 0, LOGIN_CUSTOM},
466 {0, "irc.lirex.com"},
467 {0, "irc.naturella.com"},
468 {0, "irc.spnet.net"},
469 {0, "irc.techno-link.com"},
470 {0, "irc.telecoms.bg"},
471 {0, "irc.tu-varna.edu"},
473 {"Whiffle", 0},
474 {0, "irc.whiffle.org"},
476 {"Worldnet", 0},
477 {0, "irc.worldnet.net"},
478 {0, "irc.fr.worldnet.net"},
480 {"Xentonix.net", 0},
481 {0, "irc.xentonix.net"},
483 {"XWorld", 0},
484 {0, "Buffalo.NY.US.XWorld.org"},
485 {0, "Minneapolis.MN.US.Xworld.Org"},
486 {0, "Rochester.NY.US.XWorld.org"},
487 {0, "Bayern.DE.EU.XWorld.Org"},
488 {0, "Chicago.IL.US.XWorld.Org"},
490 {0,0}
493 GSList *network_list = 0;
496 void
497 servlist_connect (session *sess, ircnet *net, gboolean join)
499 ircserver *ircserv;
500 GSList *list;
501 char *port;
502 server *serv;
504 if (!sess)
505 sess = new_ircwindow (NULL, NULL, SESS_SERVER, TRUE);
507 serv = sess->server;
509 /* connect to the currently selected Server-row */
510 list = g_slist_nth (net->servlist, net->selected);
511 if (!list)
512 list = net->servlist;
513 if (!list)
514 return;
515 ircserv = list->data;
517 /* incase a protocol switch is added to the servlist gui */
518 server_fill_her_up (sess->server);
520 if (join)
522 sess->willjoinchannel[0] = 0;
524 if (net->autojoin)
526 if (serv->autojoin)
527 free (serv->autojoin);
528 serv->autojoin = strdup (net->autojoin);
532 if (net->logintype)
534 serv->loginmethod = net->logintype;
536 else
538 serv->loginmethod = LOGIN_DEFAULT_REAL;
541 serv->password[0] = 0;
543 if (net->pass)
544 safe_strcpy (serv->password, net->pass, sizeof (serv->password));
546 if (net->flags & FLAG_USE_GLOBAL)
548 strcpy (serv->nick, prefs.nick1);
549 } else
551 if (net->nick) {
552 strcpy (serv->nick, net->nick);
556 serv->dont_use_proxy = (net->flags & FLAG_USE_PROXY) ? FALSE : TRUE;
558 #ifdef USE_OPENSSL
559 serv->use_ssl = (net->flags & FLAG_USE_SSL) ? TRUE : FALSE;
560 serv->accept_invalid_cert =
561 (net->flags & FLAG_ALLOW_INVALID) ? TRUE : FALSE;
562 #endif
564 serv->network = net;
566 port = strrchr (ircserv->hostname, '/');
567 if (port)
569 *port = 0;
571 /* support "+port" to indicate SSL (like mIRC does) */
572 if (port[1] == '+')
574 #ifdef USE_OPENSSL
575 serv->use_ssl = TRUE;
576 #endif
577 serv->connect (serv, ircserv->hostname, atoi (port + 2), FALSE);
578 } else
580 serv->connect (serv, ircserv->hostname, atoi (port + 1), FALSE);
583 *port = '/';
584 } else
585 serv->connect (serv, ircserv->hostname, -1, FALSE);
587 server_set_encoding (serv, net->encoding);
591 servlist_connect_by_netname (session *sess, char *network, gboolean join)
593 ircnet *net;
594 GSList *list = network_list;
596 while (list)
598 net = list->data;
600 if (strcasecmp (net->name, network) == 0)
602 servlist_connect (sess, net, join);
603 return 1;
606 list = list->next;
609 return 0;
613 servlist_have_auto (void)
615 GSList *list = network_list;
616 ircnet *net;
618 while (list)
620 net = list->data;
622 if (net->flags & FLAG_AUTO_CONNECT)
623 return 1;
625 list = list->next;
628 return 0;
632 servlist_auto_connect (session *sess)
634 GSList *list = network_list;
635 ircnet *net;
636 int ret = 0;
638 while (list)
640 net = list->data;
642 if (net->flags & FLAG_AUTO_CONNECT)
644 servlist_connect (sess, net, TRUE);
645 ret = 1;
648 list = list->next;
651 return ret;
654 static gint
655 servlist_cycle_cb (server *serv)
657 if (serv->network)
659 PrintTextf (serv->server_session,
660 _("Cycling to next server in %s...\n"), ((ircnet *)serv->network)->name);
661 servlist_connect (serv->server_session, serv->network, TRUE);
664 return 0;
668 servlist_cycle (server *serv)
670 ircnet *net;
671 int max, del;
673 net = serv->network;
674 if (net)
676 max = g_slist_length (net->servlist);
677 if (max > 0)
679 /* try the next server, if that option is on */
680 if (net->flags & FLAG_CYCLE)
682 net->selected++;
683 if (net->selected >= max)
684 net->selected = 0;
687 del = prefs.recon_delay * 1000;
688 if (del < 1000)
689 del = 500; /* so it doesn't block the gui */
691 if (del)
692 serv->recondelay_tag = fe_timeout_add (del, servlist_cycle_cb, serv);
693 else
694 servlist_connect (serv->server_session, net, TRUE);
696 return TRUE;
700 return FALSE;
703 ircserver *
704 servlist_server_find (ircnet *net, char *name, int *pos)
706 GSList *list = net->servlist;
707 ircserver *serv;
708 int i = 0;
710 while (list)
712 serv = list->data;
713 if (strcmp (serv->hostname, name) == 0)
715 if (pos)
716 *pos = i;
717 return serv;
719 i++;
720 list = list->next;
723 return NULL;
726 /* find a network (e.g. (ircnet *) to "FreeNode") from a hostname
727 (e.g. "irc.eu.freenode.net") */
729 ircnet *
730 servlist_net_find_from_server (char *server_name)
732 GSList *list = network_list;
733 GSList *slist;
734 ircnet *net;
735 ircserver *serv;
737 while (list)
739 net = list->data;
741 slist = net->servlist;
742 while (slist)
744 serv = slist->data;
745 if (strcasecmp (serv->hostname, server_name) == 0)
746 return net;
747 slist = slist->next;
750 list = list->next;
753 return NULL;
756 ircnet *
757 servlist_net_find (char *name, int *pos, int (*cmpfunc) (const char *, const char *))
759 GSList *list = network_list;
760 ircnet *net;
761 int i = 0;
763 while (list)
765 net = list->data;
766 if (cmpfunc (net->name, name) == 0)
768 if (pos)
769 *pos = i;
770 return net;
772 i++;
773 list = list->next;
776 return NULL;
779 ircserver *
780 servlist_server_add (ircnet *net, char *name)
782 ircserver *serv;
784 serv = malloc (sizeof (ircserver));
785 memset (serv, 0, sizeof (ircserver));
786 serv->hostname = strdup (name);
788 net->servlist = g_slist_append (net->servlist, serv);
790 return serv;
793 void
794 servlist_server_remove (ircnet *net, ircserver *serv)
796 free (serv->hostname);
797 free (serv);
798 net->servlist = g_slist_remove (net->servlist, serv);
801 static void
802 servlist_server_remove_all (ircnet *net)
804 ircserver *serv;
806 while (net->servlist)
808 serv = net->servlist->data;
809 servlist_server_remove (net, serv);
813 static void
814 free_and_clear (char *str)
816 if (str)
818 char *orig = str;
819 while (*str)
820 *str++ = 0;
821 free (orig);
825 /* executed on exit: Clear any password strings */
827 void
828 servlist_cleanup (void)
830 GSList *list;
831 ircnet *net;
833 for (list = network_list; list; list = list->next)
835 net = list->data;
836 free_and_clear (net->pass);
840 void
841 servlist_net_remove (ircnet *net)
843 GSList *list;
844 server *serv;
846 servlist_server_remove_all (net);
847 network_list = g_slist_remove (network_list, net);
849 if (net->nick)
850 free (net->nick);
851 if (net->nick2)
852 free (net->nick2);
853 if (net->user)
854 free (net->user);
855 if (net->real)
856 free (net->real);
857 free_and_clear (net->pass);
858 if (net->autojoin)
859 free (net->autojoin);
860 if (net->command)
861 free (net->command);
862 if (net->comment)
863 free (net->comment);
864 if (net->encoding)
865 free (net->encoding);
866 free (net->name);
867 free (net);
869 /* for safety */
870 list = serv_list;
871 while (list)
873 serv = list->data;
874 if (serv->network == net)
875 serv->network = NULL;
876 list = list->next;
880 ircnet *
881 servlist_net_add (char *name, char *comment, int prepend)
883 ircnet *net;
885 net = malloc (sizeof (ircnet));
886 memset (net, 0, sizeof (ircnet));
887 net->name = strdup (name);
888 /* net->comment = strdup (comment);*/
889 net->flags = FLAG_CYCLE | FLAG_USE_GLOBAL | FLAG_USE_PROXY;
891 if (prepend)
892 network_list = g_slist_prepend (network_list, net);
893 else
894 network_list = g_slist_append (network_list, net);
896 return net;
899 static void
900 servlist_load_defaults (void)
902 int i = 0, j = 0;
903 ircnet *net = NULL;
905 while (1)
907 if (def[i].network)
909 net = servlist_net_add (def[i].network, def[i].host, FALSE);
910 net->encoding = strdup ("UTF-8 (Unicode)");
911 if (def[i].channel)
912 net->autojoin = strdup (def[i].channel);
913 if (def[i].charset)
915 free (net->encoding);
916 net->encoding = strdup (def[i].charset);
918 if (def[i].loginmode)
920 net->logintype = def[i].loginmode;
922 if (g_str_hash (def[i].network) == 0x8e1b96f7)
923 prefs.slist_select = j;
924 j++;
925 } else
927 servlist_server_add (net, def[i].host);
928 if (!def[i+1].host && !def[i+1].network)
929 break;
931 i++;
935 static int
936 servlist_load (void)
938 FILE *fp;
939 char buf[2048];
940 int len;
941 char *tmp;
942 ircnet *net = NULL;
944 fp = xchat_fopen_file ("servlist_.conf", "r", 0);
945 if (!fp)
946 return FALSE;
948 while (fgets (buf, sizeof (buf) - 2, fp))
950 len = strlen (buf);
951 buf[len] = 0;
952 buf[len-1] = 0; /* remove the trailing \n */
953 if (net)
955 switch (buf[0])
957 case 'I':
958 net->nick = strdup (buf + 2);
959 break;
960 case 'i':
961 net->nick2 = strdup (buf + 2);
962 break;
963 case 'U':
964 net->user = strdup (buf + 2);
965 break;
966 case 'R':
967 net->real = strdup (buf + 2);
968 break;
969 case 'P':
970 net->pass = strdup (buf + 2);
971 break;
972 case 'J':
973 net->autojoin = strdup (buf + 2);
974 break;
975 case 'C':
976 if (net->command)
978 /* concat extra commands with a \n separator */
979 tmp = net->command;
980 net->command = malloc (strlen (tmp) + strlen (buf + 2) + 2);
981 strcpy (net->command, tmp);
982 strcat (net->command, "\n");
983 strcat (net->command, buf + 2);
984 free (tmp);
985 } else
986 net->command = strdup (buf + 2);
987 break;
988 case 'F':
989 net->flags = atoi (buf + 2);
990 break;
991 case 'D':
992 net->selected = atoi (buf + 2);
993 break;
994 case 'E':
995 net->encoding = strdup (buf + 2);
996 break;
997 case 'S': /* new server/hostname for this network */
998 servlist_server_add (net, buf + 2);
999 break;
1000 case 'L':
1001 net->logintype = atoi (buf + 2);
1002 break;
1005 if (buf[0] == 'N')
1006 net = servlist_net_add (buf + 2, /* comment */ NULL, FALSE);
1008 fclose (fp);
1010 return TRUE;
1013 void
1014 servlist_init (void)
1016 if (!network_list)
1017 if (!servlist_load ())
1018 servlist_load_defaults ();
1021 /* check if a charset is known by Iconv */
1023 servlist_check_encoding (char *charset)
1025 GIConv gic;
1026 char *c;
1028 c = strchr (charset, ' ');
1029 if (c)
1030 c[0] = 0;
1032 if (!strcasecmp (charset, "IRC")) /* special case */
1034 if (c)
1035 c[0] = ' ';
1036 return TRUE;
1039 gic = g_iconv_open (charset, "UTF-8");
1041 if (c)
1042 c[0] = ' ';
1044 if (gic != (GIConv)-1)
1046 g_iconv_close (gic);
1047 return TRUE;
1050 return FALSE;
1053 static int
1054 servlist_write_ccmd (char *str, void *fp)
1056 return fprintf (fp, "C=%s\n", (str[0] == '/') ? str + 1 : str);
1061 servlist_save (void)
1063 FILE *fp;
1064 char buf[256];
1065 ircnet *net;
1066 ircserver *serv;
1067 GSList *list;
1068 GSList *hlist;
1069 int first = FALSE;
1071 snprintf (buf, sizeof (buf), "%s/servlist_.conf", get_xdir_fs ());
1072 if (access (buf, F_OK) != 0)
1073 first = TRUE;
1075 fp = xchat_fopen_file ("servlist_.conf", "w", 0);
1076 if (!fp)
1077 return FALSE;
1079 if (first)
1080 chmod (buf, 0600);
1082 fprintf (fp, "v="PACKAGE_VERSION"\n\n");
1084 list = network_list;
1085 while (list)
1087 net = list->data;
1089 fprintf (fp, "N=%s\n", net->name);
1090 if (net->nick)
1091 fprintf (fp, "I=%s\n", net->nick);
1092 if (net->nick2)
1093 fprintf (fp, "i=%s\n", net->nick2);
1094 if (net->user)
1095 fprintf (fp, "U=%s\n", net->user);
1096 if (net->real)
1097 fprintf (fp, "R=%s\n", net->real);
1098 if (net->pass)
1099 fprintf (fp, "P=%s\n", net->pass);
1100 if (net->autojoin)
1101 fprintf (fp, "J=%s\n", net->autojoin);
1102 if (net->logintype)
1103 fprintf (fp, "L=%d\n", net->logintype);
1104 if (net->encoding && strcasecmp (net->encoding, "System") &&
1105 strcasecmp (net->encoding, "System default"))
1107 fprintf (fp, "E=%s\n", net->encoding);
1108 if (!servlist_check_encoding (net->encoding))
1110 snprintf (buf, sizeof (buf), _("Warning: \"%s\" character set is unknown. No conversion will be applied for network %s."),
1111 net->encoding, net->name);
1112 fe_message (buf, FE_MSG_WARN);
1116 if (net->command)
1117 token_foreach (net->command, '\n', servlist_write_ccmd, fp);
1119 fprintf (fp, "F=%d\nD=%d\n", net->flags, net->selected);
1121 hlist = net->servlist;
1122 while (hlist)
1124 serv = hlist->data;
1125 fprintf (fp, "S=%s\n", serv->hostname);
1126 hlist = hlist->next;
1129 if (fprintf (fp, "\n") < 1)
1131 fclose (fp);
1132 return FALSE;
1135 list = list->next;
1138 fclose (fp);
1139 return TRUE;
1142 static void
1143 joinlist_free1 (GSList *list)
1145 GSList *head = list;
1147 for (; list; list = list->next)
1148 g_free (list->data);
1149 g_slist_free (head);
1152 void
1153 joinlist_free (GSList *channels, GSList *keys)
1155 joinlist_free1 (channels);
1156 joinlist_free1 (keys);
1159 gboolean
1160 joinlist_is_in_list (server *serv, char *channel)
1162 GSList *channels, *keys;
1163 GSList *list;
1165 if (!serv->network || !((ircnet *)serv->network)->autojoin)
1166 return FALSE;
1168 joinlist_split (((ircnet *)serv->network)->autojoin, &channels, &keys);
1170 for (list = channels; list; list = list->next)
1172 if (serv->p_cmp (list->data, channel) == 0)
1173 return TRUE;
1176 joinlist_free (channels, keys);
1178 return FALSE;
1181 gchar *
1182 joinlist_merge (GSList *channels, GSList *keys)
1184 GString *out = g_string_new (NULL);
1185 GSList *list;
1186 int i, j;
1188 for (; channels; channels = channels->next)
1190 g_string_append (out, channels->data);
1192 if (channels->next)
1193 g_string_append_c (out, ',');
1196 /* count number of REAL keys */
1197 for (i = 0, list = keys; list; list = list->next)
1198 if (list->data)
1199 i++;
1201 if (i > 0)
1203 g_string_append_c (out, ' ');
1205 for (j = 0; keys; keys = keys->next)
1207 if (keys->data)
1209 g_string_append (out, keys->data);
1210 j++;
1211 if (j == i)
1212 break;
1215 if (keys->next)
1216 g_string_append_c (out, ',');
1220 return g_string_free (out, FALSE);
1223 void
1224 joinlist_split (char *autojoin, GSList **channels, GSList **keys)
1226 char *parta, *partb;
1227 char *chan, *key;
1228 int len;
1230 *channels = NULL;
1231 *keys = NULL;
1233 /* after the first space, the keys begin */
1234 parta = autojoin;
1235 partb = strchr (autojoin, ' ');
1236 if (partb)
1237 partb++;
1239 while (1)
1241 chan = parta;
1242 key = partb;
1244 if (1)
1246 while (parta[0] != 0 && parta[0] != ',' && parta[0] != ' ')
1248 parta++;
1252 if (partb)
1254 while (partb[0] != 0 && partb[0] != ',' && partb[0] != ' ')
1256 partb++;
1260 len = parta - chan;
1261 if (len < 1)
1262 break;
1263 *channels = g_slist_append (*channels, g_strndup (chan, len));
1265 len = partb - key;
1266 *keys = g_slist_append (*keys, len ? g_strndup (key, len) : NULL);
1268 if (parta[0] == ' ' || parta[0] == 0)
1269 break;
1270 parta++;
1272 if (partb)
1274 if (partb[0] == 0 || partb[0] == ' ')
1275 partb = NULL; /* no more keys, but maybe more channels? */
1276 else
1277 partb++;
1281 #if 0
1282 GSList *lista, *listb;
1283 int i;
1285 printf("-----\n");
1286 i = 0;
1287 lista = *channels;
1288 listb = *keys;
1289 while (lista)
1291 printf("%d. |%s| |%s|\n", i, lista->data, listb->data);
1292 i++;
1293 lista = lista->next;
1294 listb = listb->next;
1296 printf("-----\n\n");
1297 #endif