summary refs log blame commit diff stats
path: root/src/common/servlist.c
blob: 1ca632010fa4d2d55db2011e18fb63c1cce00306 (plain) (tree)
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288






















                                                                            

















































































































































































































































































































































                                                                   
                           
                                                 

                                                      
                                                    
                                                         
                                                
                                                  






















































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                          
/* X-Chat
 * Copyright (C) 1998 Peter Zelezny.
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "xchat.h"
#include <glib/ghash.h>

#include "cfgfiles.h"
#include "fe.h"
#include "server.h"
#include "text.h"
#include "util.h" /* token_foreach */
#include "xchatc.h"

#include "servlist.h"


struct defaultserver
{
	char *network;
	char *host;
	char *channel;
	char *charset;
};

static const struct defaultserver def[] =
{
	{"2600net",	0},
	{0,			"irc.2600.net"},

	{"7-indonesia",	0},
	{0,			"irc.7-indonesia.org"},

	{"AccessIRC",	0},
	{0,			"irc.accessirc.net"},
	{0,			"eu.accessirc.net"},

	{"AfterNET",	0},
	{0,			"irc.afternet.org"},
	{0,			"us.afternet.org"},
	{0,			"eu.afternet.org"},

	{"Aitvaras",	0},
#ifdef USE_IPV6
#ifdef USE_OPENSSL
	{0,			"irc6.ktu.lt/+7668"},
#endif
	{0,			"irc6.ktu.lt/7666"},
#endif
#ifdef USE_OPENSSL
	{0,			"irc.data.lt/+6668"},
	{0,			"irc-ssl.omnitel.net/+6668"},
	{0,			"irc-ssl.le.lt/+9999"},
#endif
	{0,			"irc.data.lt"},
	{0,			"irc.omnitel.net"},
	{0,			"irc.ktu.lt"},
	{0,			"irc.le.lt"},
	{0,			"irc.takas.lt"},
	{0,			"irc.5ci.net"},
	{0,			"irc.kis.lt"},

	{"AmigaNet",	0},
	{0,			"irc.amiganet.org"},
	{0,			"us.amiganet.org"},
	{0,			"uk.amiganet.org"},
/*	{0,			"no.amiganet.org"},
	{0,			"au.amiganet.org"},*/

	{"ARCNet",	0},
	{0,			"se1.arcnet.vapor.com"},
	{0,			"us1.arcnet.vapor.com"},
	{0,			"us2.arcnet.vapor.com"},
	{0,			"us3.arcnet.vapor.com"},
	{0,			"ca1.arcnet.vapor.com"},
	{0,			"de1.arcnet.vapor.com"},
	{0,			"de3.arcnet.vapor.com"},
	{0,			"ch1.arcnet.vapor.com"},
	{0,			"be1.arcnet.vapor.com"},
	{0,			"nl3.arcnet.vapor.com"},
	{0,			"uk1.arcnet.vapor.com"},
	{0,			"uk2.arcnet.vapor.com"},
/*	{0,			"uk3.arcnet.vapor.com"},*/
	{0,			"fr1.arcnet.vapor.com"},

	{"AstroLink",	0},
	{0,			"irc.astrolink.org"},

	{"AustNet",	0},
	{0,			"au.austnet.org"},
	{0,			"us.austnet.org"},
	{0,			"ca.austnet.org"},

/*	{"AxeNet",	0},
	{0,			"irc.axenet.org"},
	{0,			"angel.axenet.org"},
	{0,			"energy.axenet.org"},
	{0,			"python.axenet.org"},*/

	{"AzzurraNet",	0},
	{0,			"irc.azzurra.org"},
	{0,			"crypto.azzurra.org"},

	{"Beirut", 0},
	{0,			"irc.beirut.com"},

	{"ChattingAway", 0},
	{0,			"irc.chattingaway.com"},

	{"ChatJunkies",	0, "#xchat"},
	{0,			"irc.chatjunkies.org"},
	{0,			"nl.chatjunkies.org"},

	{"ChatNet",	0},
	{0,			"US.ChatNet.Org"},
	{0,			"EU.ChatNet.Org"},

	{"ChatSociety", 0},
	{0,			"us.chatsociety.net"},
	{0,			"eu.chatsociety.net"},

	{"ChatSpike", 0},
	{0,			"irc.chatspike.net"},

	{"CoolChat",	0},
	{0,			"irc.coolchat.net"},
/*	{0,			"unix.coolchat.net"},
	{0,			"toronto.coolchat.net"},*/

	{"Criten", 0},
	{0,			"irc.criten.net"},
	{0,			"irc.eu.criten.net"},

	{"DALnet",	0},
	{0,			"irc.dal.net"},
	{0,			"irc.eu.dal.net"},

	{"Dark-Tou-Net",	0},
	{0,			"irc.d-t-net.de"},
	{0,			"bw.d-t-net.de"},
	{0,			"nc.d-t-net.de"},
	{0,			"wakka.d-t-net.de"},

	{"DarkMyst", 0},
	{0,			"irc.darkmyst.org"},

	{"DeepIRC", 0},
	{0,			"irc.deepirc.net"},

	{"DeltaAnime", 0},
	{0,			"irc.deltaanime.net"},

	{"EFnet",	0},
	{0,			"irc.blackened.com"},
	{0,			"irc.Prison.NET"},
	{0,			"irc.Qeast.net"},
	{0,			"irc.efnet.pl"},
	{0,			"efnet.demon.co.uk"},
/*	{0,			"irc.lagged.org"},*/
	{0,			"irc.lightning.net"},
	{0,			"irc.mindspring.com"},
	{0,			"irc.easynews.com"},
	{0,			"irc.servercentral.net"},

	{"EnterTheGame",	0},
	{0,			"IRC.EnterTheGame.Com"},

	{"EUIrc",	0},
	{0,			"irc.euirc.net"},
	{0,			"irc.ham.de.euirc.net"},
	{0,			"irc.ber.de.euirc.net"},
	{0,			"irc.ffm.de.euirc.net"},
	{0,			"irc.bre.de.euirc.net"},
	{0,			"irc.hes.de.euirc.net"},
	{0,			"irc.vie.at.euirc.net"},
	{0,			"irc.inn.at.euirc.net"},
	{0,			"irc.bas.ch.euirc.net"},

	{"EuropNet", 0},
	{0,			"irc.europnet.org"},

	{"EU-IRC",	0},
	{0,			"irc.eu-irc.net"},

	{"FDFNet",	0},
	{0,			"irc.fdfnet.net"},
	{0,			"irc.eu.fdfnet.net"},

	{"FEFNet",	0},
	{0,			"irc.fef.net"},
	{0,			"irc.ggn.net"},
	{0,			"irc.vendetta.com"},

	{"FreeNode",	0},
	{0,				"irc.freenode.net"},

/*	{"Freeworld",	0},
	{0,			"kabel.freeworld.nu"},
	{0,			"irc.freeworld.nu"},*/

	{"GalaxyNet",	0},
	{0,			"irc.galaxynet.org"},
/*	{0,			"sprynet.us.galaxynet.org"},
	{0,			"atlanta.ga.us.galaxynet.org"},*/

	{"GamesNET",	0},
	{0,				"irc.gamesnet.net"},
/*	{0,				"irc.us.gamesnet.net"},
	{0,				"east.us.gamesnet.net"},
	{0,				"west.us.gamesnet.net"},*/
	{0,				"irc.ca.gamesnet.net"},
	{0,				"irc.eu.gamesnet.net"},

	{"GeekShed",	0},
	{0,			"irc.geekshed.net"},

	{"German-Elite",	0},
	{0,			"dominion.german-elite.net"},
	{0,			"komatu.german-elite.net"},
/*	{0,			"liberty.german-elite.net"},*/

	{"GimpNet",		0},
	{0,			"irc.gimp.org"},
/*	{0,			"irc.au.gimp.org"},*/
	{0,			"irc.us.gimp.org"},

	{"HabberNet",	0},
	{0,			"irc.habber.net"},

	{"Hashmark",	0},
	{0,			"irc.hashmark.net"},

	{"IdleMonkeys", 0},
	{0,			"irc.idlemonkeys.net"},

/*	{"Infinity-IRC",	0},
	{0,			"Atlanta.GA.US.Infinity-IRC.Org"},
	{0,			"Babylon.NY.US.Infinity-IRC.Org"},
	{0,			"Sunshine.Ca.US.Infinity-IRC.Org"},
	{0,			"IRC.Infinity-IRC.Org"},*/

	{"iZ-smart.net",	0},
	{0,			"irc.iZ-smart.net/6666"},
	{0,			"irc.iZ-smart.net/6667"},
	{0,			"irc.iZ-smart.net/6668"},

	{"IrcLink",	0},
	{0,			"irc.irclink.net"},
	{0,			"Alesund.no.eu.irclink.net"},
	{0,			"Oslo.no.eu.irclink.net"},
	{0,			"frogn.no.eu.irclink.net"},
	{0,			"tonsberg.no.eu.irclink.net"},

	{"IRCNet",		0},
	{0,				"irc.ircnet.com"},
	{0,				"irc.stealth.net/6668"},
	{0,				"ircnet.demon.co.uk"},
/*	{0,				"ircnet.hinet.hr"},*/
	{0,				"irc.datacomm.ch"},
/*	{0,				"ircnet.kaptech.fr"},
	{0,				"ircnet.easynet.co.uk"},*/
	{0,				"random.ircd.de"},
	{0,				"ircnet.netvision.net.il"},
/*	{0,				"irc.seed.net.tw"},*/
	{0,				"irc.cs.hut.fi"},

	{"Irctoo.net",	0},
	{0,			"irc.irctoo.net"},

	{"Krstarica", 0},
	{0,			"irc.krstarica.com"},

	{"Librenet",	0},
	{0,			"irc.librenet.net"},
	{0,			"ielf.fr.librenet.net"},

	{"LinkNet",	0},
	{0,			"irc.link-net.org"},
	{0,			"irc.no.link-net.org"},
/*	{0,			"irc.gamesden.net.au"},*/
	{0,			"irc.bahnhof.se"},
/*	{0,			"irc.kinexuseurope.co.uk"},
	{0,			"irc.gamiix.com"},*/

	{"MagicStar",	0},
	{0,			"irc.magicstar.net"},

	{"Majistic",	0},
	{0,			"irc.majistic.net"},

	{"MindForge",	0},
	{0,			"irc.mindforge.org"},

	{"MintIRC",	0},
	{0,			"irc.mintirc.net"},

	{"MIXXnet",		0},
	{0,			"irc.mixxnet.net"},

	{"NeverNET",	0},
	{0,			"irc.nevernet.net"},
	{0,			"imagine.nevernet.net"},
	{0,			"dimension.nevernet.net"},
	{0,			"universe.nevernet.net"},
	{0,			"wayland.nevernet.net"},
	{0,			"forte.nevernet.net"},

	{"NixHelpNet",	0},
	{0,			"irc.nixhelp.org"},
	{0,			"us.nixhelp.org"},
	{0,			"uk.nixhelp.org"},
	{0,			"uk2.nixhelp.org"},
	{0,			"uk3.nixhelp.org"},
	{0,			"nl.nixhelp.org"},
	{0,			"ca.ld.nixhelp.org"},
	{0,			"us.co.nixhelp.org"},
	{0,			"us.ca.nixhelp.org"},
	{0,			"us.pa.nixhelp.org"},

	{"NullusNet",	0},
	{0,			"irc.nullus.net"},

	{"Oceanius", 0},
	{0,			"irc.oceanius.com"},

	{"OFTC",	0},
	{0,			"irc.oftc.net"},

	{"OtherNet",	0},
	{0,			"irc.othernet.org"},

	{"OzNet",	0},
	{0,			"irc.oz.org"},

	{"PIRC.PL",	0},
	{0,			"irc.pirc.pl"},

	{"PTlink",	0},
	{0,			"irc.PTlink.net"},
	{0,			"aaia.PTlink.net"},

	{"PTNet.org",   0},
	{0,			"irc.PTNet.org"},
	{0,			"world.PTnet.org"},
	{0,			"netvisao.PTnet.org"},
	{0,			"uevora.PTnet.org"},
	{0,			"vianetworks.PTnet.org"},
	{0,			"uc.PTnet.org"},
	{0,			"nfsi.ptnet.org"},

	{"QuakeNet",	0},
	{0,			"irc.quakenet.org"},
	{0,			"irc.se.quakenet.org"},
	{0,			"irc.dk.quakenet.org"},
	{0,			"irc.no.quakenet.org"},
	{0,			"irc.fi.quakenet.org"},
	{0,			"irc.be.quakenet.org"},
	{0,			"irc.uk.quakenet.org"},
	{0,			"irc.de.quakenet.org"},
	{0,			"irc.it.quakenet.org"},

	{"RebelChat",	0},
	{0,			"irc.rebelchat.org"},

/*	{"Recycled-IRC",  0},
	{0,			"irc.recycled-irc.org"},
	{0,			"vermin.recycled-irc.org"},
	{0,			"waste.recycled-irc.org"},
	{0,			"lumber.recycled-irc.org"},
	{0,			"trash.recycled-irc.org"},
	{0,			"unwashed.recycled-irc.org"},
	{0,			"garbage.recycled-irc.org"},
	{0,			"dustbin.recycled-irc.org"},*/

	{"RizeNET", 0},
	{0,			"irc.rizenet.org"},
	{0,			"omega.rizenet.org"},
	{0,			"evelance.rizenet.org"},
	{0,			"lisa.rizenet.org"},
	{0,			"scott.rizenet.org"},

	{"Rizon", 0},
	{0,			"irc.rizon.net"},

	{"RusNet", 0, 0, "KOI8-R (Cyrillic)"},
	{0,			"irc.tomsk.net"},
	{0,			"irc.rinet.ru"},
	{0,			"irc.run.net"},
	{0,			"irc.ru"},
	{0,			"irc.lucky.net"},

	{"SceneNet",	0},
	{0,			"irc.scene.org"},
	{0,			"irc.eu.scene.org"},
	{0,			"irc.us.scene.org"},

	{"SeilEn.de",	0},
	{0,			"irc.seilen.de"},

	{"SlashNET",	0},
	{0,			"irc.slashnet.org"},
	{0,			"area51.slashnet.org"},
	{0,			"moo.slashnet.org"},
	{0,			"radon.slashnet.org"},

	{"Sohbet.Net", 0},
	{0,			"irc.sohbet.net"},

	{"SolidIRC", 0},
	{0,			"irc.solidirc.com"},

	{"SorceryNet",	0},
	{0,			"irc.sorcery.net/9000"},
	{0,			"irc.us.sorcery.net/9000"},
	{0,			"irc.eu.sorcery.net/9000"},

	{"Spidernet",	0},
	{0,			"us.spidernet.org"},
	{0,			"eu.spidernet.org"},
	{0,			"irc.spidernet.org"},

	{"StarChat", 0},
	{0,			"irc.starchat.net"},
	{0,			"gainesville.starchat.net"},
	{0,			"freebsd.starchat.net"},
	{0,			"sunset.starchat.net"},
	{0,			"revenge.starchat.net"},
	{0,			"tahoma.starchat.net"},
	{0,			"neo.starchat.net"},

	{"TNI3",			0},
	{0,			"irc.tni3.com"},

	{"TURLINet",			0},
	{0,			"irc.turli.net"},
	{0,			"irc.servx.ru"},
	{0,			"irc.gavnos.ru"},

	{"UnderNet",	0},
	{0,			"us.undernet.org"},
	{0,			"eu.undernet.org"},

	{"UniBG",		0},
	{0,			"irc.lirex.com"},
	{0,			"irc.naturella.com"},
	{0,			"irc.spnet.net"},
	{0,			"irc.techno-link.com"},
	{0,			"irc.telecoms.bg"},
	{0,			"irc.tu-varna.edu"},

	{"Whiffle",	0},
	{0,			"irc.whiffle.org"},

	{"Worldnet",		0},
	{0,			"irc.worldnet.net"},
	{0,			"irc.fr.worldnet.net"},

	{"Xentonix.net",	0},
	{0,			"irc.xentonix.net"},

	{"XWorld",	0},
	{0,			"Buffalo.NY.US.XWorld.org"},
	{0,			"Minneapolis.MN.US.Xworld.Org"},
	{0,			"Rochester.NY.US.XWorld.org"},
	{0,			"Bayern.DE.EU.XWorld.Org"},
	{0,			"Chicago.IL.US.XWorld.Org"},

	{0,0}
};

GSList *network_list = 0;


void
servlist_connect (session *sess, ircnet *net, gboolean join)
{
	ircserver *ircserv;
	GSList *list;
	char *port;
	server *serv;

	if (!sess)
		sess = new_ircwindow (NULL, NULL, SESS_SERVER, TRUE);

	serv = sess->server;

	/* connect to the currently selected Server-row */
	list = g_slist_nth (net->servlist, net->selected);
	if (!list)
		list = net->servlist;
	if (!list)
		return;
	ircserv = list->data;

	/* incase a protocol switch is added to the servlist gui */
	server_fill_her_up (sess->server);

	if (join)
	{
		sess->willjoinchannel[0] = 0;

		if (net->autojoin)
		{
			if (serv->autojoin)
				free (serv->autojoin);
			serv->autojoin = strdup (net->autojoin);
		}
	}

	serv->password[0] = 0;
	if (net->pass)
		safe_strcpy (serv->password, net->pass, sizeof (serv->password));

	if (net->flags & FLAG_USE_GLOBAL)
	{
		strcpy (serv->nick, prefs.nick1);
	} else
	{
		if (net->nick)
			strcpy (serv->nick, net->nick);
	}

	serv->dont_use_proxy = (net->flags & FLAG_USE_PROXY) ? FALSE : TRUE;

#ifdef USE_OPENSSL
	serv->use_ssl = (net->flags & FLAG_USE_SSL) ? TRUE : FALSE;
	serv->accept_invalid_cert =
		(net->flags & FLAG_ALLOW_INVALID) ? TRUE : FALSE;
#endif

	serv->network = net;

	port = strrchr (ircserv->hostname, '/');
	if (port)
	{
		*port = 0;

		/* support "+port" to indicate SSL (like mIRC does) */
		if (port[1] == '+')
		{
#ifdef USE_OPENSSL
			serv->use_ssl = TRUE;
#endif
			serv->connect (serv, ircserv->hostname, atoi (port + 2), FALSE);
		} else
		{
			serv->connect (serv, ircserv->hostname, atoi (port + 1), FALSE);
		}

		*port = '/';
	} else
		serv->connect (serv, ircserv->hostname, -1, FALSE);

	server_set_encoding (serv, net->encoding);
}

int
servlist_connect_by_netname (session *sess, char *network, gboolean join)
{
	ircnet *net;
	GSList *list = network_list;

	while (list)
	{
		net = list->data;

		if (strcasecmp (net->name, network) == 0)
		{
			servlist_connect (sess, net, join);
			return 1;
		}

		list = list->next;
	}

	return 0;
}

int
servlist_have_auto (void)
{
	GSList *list = network_list;
	ircnet *net;

	while (list)
	{
		net = list->data;

		if (net->flags & FLAG_AUTO_CONNECT)
			return 1;

		list = list->next;
	}

	return 0;
}

int
servlist_auto_connect (session *sess)
{
	GSList *list = network_list;
	ircnet *net;
	int ret = 0;

	while (list)
	{
		net = list->data;

		if (net->flags & FLAG_AUTO_CONNECT)
		{
			servlist_connect (sess, net, TRUE);
			ret = 1;
		}

		list = list->next;
	}

	return ret;
}

static gint
servlist_cycle_cb (server *serv)
{
	if (serv->network)
	{
		PrintTextf (serv->server_session,
			_("Cycling to next server in %s...\n"), ((ircnet *)serv->network)->name);
		servlist_connect (serv->server_session, serv->network, TRUE);
	}

	return 0;
}

int
servlist_cycle (server *serv)
{
	ircnet *net;
	int max, del;

	net = serv->network;
	if (net)
	{
		max = g_slist_length (net->servlist);
		if (max > 0)
		{
			/* try the next server, if that option is on */
			if (net->flags & FLAG_CYCLE)
			{
				net->selected++;
				if (net->selected >= max)
					net->selected = 0;
			}

			del = prefs.recon_delay * 1000;
			if (del < 1000)
				del = 500;				  /* so it doesn't block the gui */

			if (del)
				serv->recondelay_tag = fe_timeout_add (del, servlist_cycle_cb, serv);
			else
				servlist_connect (serv->server_session, net, TRUE);

			return TRUE;
		}
	}

	return FALSE;
}

ircserver *
servlist_server_find (ircnet *net, char *name, int *pos)
{
	GSList *list = net->servlist;
	ircserver *serv;
	int i = 0;

	while (list)
	{
		serv = list->data;
		if (strcmp (serv->hostname, name) == 0)
		{
			if (pos)
				*pos = i;
			return serv;
		}
		i++;
		list = list->next;
	}

	return NULL;
}

/* find a network (e.g. (ircnet *) to "FreeNode") from a hostname
   (e.g. "irc.eu.freenode.net") */

ircnet *
servlist_net_find_from_server (char *server_name)
{
	GSList *list = network_list;
	GSList *slist;
	ircnet *net;
	ircserver *serv;

	while (list)
	{
		net = list->data;

		slist = net->servlist;
		while (slist)
		{
			serv = slist->data;
			if (strcasecmp (serv->hostname, server_name) == 0)
				return net;
			slist = slist->next;
		}

		list = list->next;
	}

	return NULL;
}

ircnet *
servlist_net_find (char *name, int *pos, int (*cmpfunc) (const char *, const char *))
{
	GSList *list = network_list;
	ircnet *net;
	int i = 0;

	while (list)
	{
		net = list->data;
		if (cmpfunc (net->name, name) == 0)
		{
			if (pos)
				*pos = i;
			return net;
		}
		i++;
		list = list->next;
	}

	return NULL;
}

ircserver *
servlist_server_add (ircnet *net, char *name)
{
	ircserver *serv;

	serv = malloc (sizeof (ircserver));
	memset (serv, 0, sizeof (ircserver));
	serv->hostname = strdup (name);

	net->servlist = g_slist_append (net->servlist, serv);

	return serv;
}

void
servlist_server_remove (ircnet *net, ircserver *serv)
{
	free (serv->hostname);
	free (serv);
	net->servlist = g_slist_remove (net->servlist, serv);
}

static void
servlist_server_remove_all (ircnet *net)
{
	ircserver *serv;

	while (net->servlist)
	{
		serv = net->servlist->data;
		servlist_server_remove (net, serv);
	}
}

static void
free_and_clear (char *str)
{
	if (str)
	{
		char *orig = str;
		while (*str)
			*str++ = 0;
		free (orig);
	}
}

/* executed on exit: Clear any password strings */

void
servlist_cleanup (void)
{
	GSList *list;
	ircnet *net;

	for (list = network_list; list; list = list->next)
	{
		net = list->data;
		free_and_clear (net->pass);
		free_and_clear (net->nickserv);
	}
}

void
servlist_net_remove (ircnet *net)
{
	GSList *list;
	server *serv;

	servlist_server_remove_all (net);
	network_list = g_slist_remove (network_list, net);

	if (net->nick)
		free (net->nick);
	if (net->nick2)
		free (net->nick2);
	if (net->user)
		free (net->user);
	if (net->real)
		free (net->real);
	free_and_clear (net->pass);
	if (net->autojoin)
		free (net->autojoin);
	if (net->command)
		free (net->command);
	free_and_clear (net->nickserv);
	if (net->comment)
		free (net->comment);
	if (net->encoding)
		free (net->encoding);
	free (net->name);
	free (net);

	/* for safety */
	list = serv_list;
	while (list)
	{
		serv = list->data;
		if (serv->network == net)
			serv->network = NULL;
		list = list->next;
	}
}

ircnet *
servlist_net_add (char *name, char *comment, int prepend)
{
	ircnet *net;

	net = malloc (sizeof (ircnet));
	memset (net, 0, sizeof (ircnet));
	net->name = strdup (name);
/*	net->comment = strdup (comment);*/
	net->flags = FLAG_CYCLE | FLAG_USE_GLOBAL | FLAG_USE_PROXY;

	if (prepend)
		network_list = g_slist_prepend (network_list, net);
	else
		network_list = g_slist_append (network_list, net);

	return net;
}

static void
servlist_load_defaults (void)
{
	int i = 0, j = 0;
	ircnet *net = NULL;

	while (1)
	{
		if (def[i].network)
		{
			net = servlist_net_add (def[i].network, def[i].host, FALSE);
			net->encoding = strdup ("IRC (Latin/Unicode Hybrid)");
			if (def[i].channel)
				net->autojoin = strdup (def[i].channel);
			if (def[i].charset)
			{
				free (net->encoding);
				net->encoding = strdup (def[i].charset);
			}
			if (g_str_hash (def[i].network) == 0x8e1b96f7)
				prefs.slist_select = j;
			j++;
		} else
		{
			servlist_server_add (net, def[i].host);
			if (!def[i+1].host && !def[i+1].network)
				break;
		}
		i++;
	}
}

static int
servlist_load (void)
{
	FILE *fp;
	char buf[2048];
	int len;
	char *tmp;
	ircnet *net = NULL;

	fp = xchat_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 = strdup (buf + 2);
				break;
			case 'i':
				net->nick2 = strdup (buf + 2);
				break;
			case 'U':
				net->user = strdup (buf + 2);
				break;
			case 'R':
				net->real = strdup (buf + 2);
				break;
			case 'P':
				net->pass = strdup (buf + 2);
				break;
			case 'J':
				net->autojoin = strdup (buf + 2);
				break;
			case 'C':
				if (net->command)
				{
					/* concat extra commands with a \n separator */
					tmp = net->command;
					net->command = malloc (strlen (tmp) + strlen (buf + 2) + 2);
					strcpy (net->command, tmp);
					strcat (net->command, "\n");
					strcat (net->command, buf + 2);
					free (tmp);
				} else
					net->command = strdup (buf + 2);
				break;
			case 'F':
				net->flags = atoi (buf + 2);
				break;
			case 'D':
				net->selected = atoi (buf + 2);
				break;
			case 'E':
				net->encoding = strdup (buf + 2);
				break;
			case 'S':	/* new server/hostname for this network */
				servlist_server_add (net, buf + 2);
				break;
			case 'B':
				net->nickserv = strdup (buf + 2);
				break;
			}
		}
		if (buf[0] == 'N')
			net = servlist_net_add (buf + 2, /* comment */ NULL, FALSE);
	}
	fclose (fp);

	return TRUE;
}

void
servlist_init (void)
{
	if (!network_list)
		if (!servlist_load ())
			servlist_load_defaults ();
}

/* check if a charset is known by Iconv */
int
servlist_check_encoding (char *charset)
{
	GIConv gic;
	char *c;

	c = strchr (charset, ' ');
	if (c)
		c[0] = 0;

	if (!strcasecmp (charset, "IRC")) /* special case */
	{
		if (c)
			c[0] = ' ';
		return TRUE;
	}

	gic = g_iconv_open (charset, "UTF-8");

	if (c)
		c[0] = ' ';

	if (gic != (GIConv)-1)
	{
		g_iconv_close (gic);
		return TRUE;
	}

	return FALSE;
}

static int
servlist_write_ccmd (char *str, void *fp)
{
	return fprintf (fp, "C=%s\n", (str[0] == '/') ? str + 1 : str);
}


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 && strcasecmp (net->encoding, "System") &&
			 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 void
joinlist_free1 (GSList *list)
{
	GSList *head = list;

	for (; list; list = list->next)
		g_free (list->data);
	g_slist_free (head);
}

void
joinlist_free (GSList *channels, GSList *keys)
{
	joinlist_free1 (channels);
	joinlist_free1 (keys);
}

gboolean
joinlist_is_in_list (server *serv, char *channel)
{
	GSList *channels, *keys;
	GSList *list;

	if (!serv->network || !((ircnet *)serv->network)->autojoin)
		return FALSE;

	joinlist_split (((ircnet *)serv->network)->autojoin, &channels, &keys);

	for (list = channels; list; list = list->next)
	{
		if (serv->p_cmp (list->data, channel) == 0)
			return TRUE;
	}

	joinlist_free (channels, keys);

	return FALSE;
}

gchar *
joinlist_merge (GSList *channels, GSList *keys)
{
	GString *out = g_string_new (NULL);
	GSList *list;
	int i, j;

	for (; channels; channels = channels->next)
	{
		g_string_append (out, channels->data);

		if (channels->next)
			g_string_append_c (out, ',');
	}

	/* count number of REAL keys */
	for (i = 0, list = keys; list; list = list->next)
		if (list->data)
			i++;

	if (i > 0)
	{
		g_string_append_c (out, ' ');

		for (j = 0; keys; keys = keys->next)
		{
			if (keys->data)
			{
				g_string_append (out, keys->data);
				j++;
				if (j == i)
					break;
			}

			if (keys->next)
				g_string_append_c (out, ',');
		}
	}

	return g_string_free (out, FALSE);
}

void
joinlist_split (char *autojoin, GSList **channels, GSList **keys)
{
	char *parta, *partb;
	char *chan, *key;
	int len;

	*channels = NULL;
	*keys = NULL;

	/* after the first space, the keys begin */
	parta = autojoin;
	partb = strchr (autojoin, ' ');
	if (partb)
		partb++;

	while (1)
	{
		chan = parta;
		key = partb;

		if (1)
		{
			while (parta[0] != 0 && parta[0] != ',' && parta[0] != ' ')
			{
				parta++;
			}
		}

		if (partb)
		{
			while (partb[0] != 0 && partb[0] != ',' && partb[0] != ' ')
			{
				partb++;
			}
		}

		len = parta - chan;
		if (len < 1)
			break;
		*channels = g_slist_append (*channels, g_strndup (chan, len));

		len = partb - key;
		*keys = g_slist_append (*keys, len ? g_strndup (key, len) : NULL);

		if (parta[0] == ' ' || parta[0] == 0)
			break;
		parta++;

		if (partb)
		{
			if (partb[0] == 0 || partb[0] == ' ')
				partb = NULL;	/* no more keys, but maybe more channels? */
			else
				partb++;
		}
	}

#if 0
	GSList *lista, *listb;
	int i;

	printf("-----\n");
	i = 0;
	lista = *channels;
	listb = *keys;
	while (lista)
	{
		printf("%d. |%s| |%s|\n", i, lista->data, listb->data);
		i++;
		lista = lista->next;
		listb = listb->next;
	}
	printf("-----\n\n");
#endif
}
an> /* got a server PRIVMSG, NOTICE, numeric etc... */ int plugin_emit_server (session *sess, char *name, char *word[], char *word_eol[]) { return plugin_hook_run (sess, name, word, word_eol, HOOK_SERVER); } /* see if any plugins are interested in this print event */ int plugin_emit_print (session *sess, char *word[]) { return plugin_hook_run (sess, word[0], word, NULL, HOOK_PRINT); } int plugin_emit_dummy_print (session *sess, char *name) { char *word[32]; int i; word[0] = name; for (i = 1; i < 32; i++) word[i] = "\000"; return plugin_hook_run (sess, name, word, NULL, HOOK_PRINT); } int plugin_emit_keypress (session *sess, unsigned int state, unsigned int keyval, int len, char *string) { char *word[PDIWORDS]; char keyval_str[16]; char state_str[16]; char len_str[16]; int i; if (!hook_list) return 0; sprintf (keyval_str, "%u", keyval); sprintf (state_str, "%u", state); sprintf (len_str, "%d", len); word[0] = "Key Press"; word[1] = keyval_str; word[2] = state_str; word[3] = string; word[4] = len_str; for (i = 5; i < PDIWORDS; i++) word[i] = "\000"; return plugin_hook_run (sess, word[0], word, NULL, HOOK_PRINT); } static int plugin_timeout_cb (xchat_hook *hook) { int ret; /* timer_cb's context starts as front-most-tab */ hook->pl->context = current_sess; /* call the plugin's timeout function */ ret = ((xchat_timer_cb *)hook->callback) (hook->userdata); /* the callback might have already unhooked it! */ if (!g_slist_find (hook_list, hook) || hook->type == HOOK_DELETED) return 0; if (ret == 0) { hook->tag = 0; /* avoid fe_timeout_remove, returning 0 is enough! */ xchat_unhook (hook->pl, hook); } return ret; } /* insert a hook into hook_list according to its priority */ static void plugin_insert_hook (xchat_hook *new_hook) { GSList *list; xchat_hook *hook; list = hook_list; while (list) { hook = list->data; if (hook->type == new_hook->type && hook->pri <= new_hook->pri) { hook_list = g_slist_insert_before (hook_list, list, new_hook); return; } list = list->next; } hook_list = g_slist_append (hook_list, new_hook); } static gboolean plugin_fd_cb (GIOChannel *source, GIOCondition condition, xchat_hook *hook) { int flags = 0, ret; typedef int (xchat_fd_cb2) (int fd, int flags, void *user_data, GIOChannel *); if (condition & G_IO_IN) flags |= XCHAT_FD_READ; if (condition & G_IO_OUT) flags |= XCHAT_FD_WRITE; if (condition & G_IO_PRI) flags |= XCHAT_FD_EXCEPTION; ret = ((xchat_fd_cb2 *)hook->callback) (hook->pri, flags, hook->userdata, source); /* the callback might have already unhooked it! */ if (!g_slist_find (hook_list, hook) || hook->type == HOOK_DELETED) return 0; if (ret == 0) { hook->tag = 0; /* avoid fe_input_remove, returning 0 is enough! */ xchat_unhook (hook->pl, hook); } return ret; } /* allocate and add a hook to our list. Used for all 4 types */ static xchat_hook * plugin_add_hook (xchat_plugin *pl, int type, int pri, const char *name, const char *help_text, void *callb, int timeout, void *userdata) { xchat_hook *hook; hook = malloc (sizeof (xchat_hook)); memset (hook, 0, sizeof (xchat_hook)); hook->type = type; hook->pri = pri; if (name) hook->name = strdup (name); if (help_text) hook->help_text = strdup (help_text); hook->callback = callb; hook->pl = pl; hook->userdata = userdata; /* insert it into the linked list */ plugin_insert_hook (hook); if (type == HOOK_TIMER) hook->tag = fe_timeout_add (timeout, plugin_timeout_cb, hook); return hook; } GList * plugin_command_list(GList *tmp_list) { xchat_hook *hook; GSList *list = hook_list; while (list) { hook = list->data; if (hook->type == HOOK_COMMAND) tmp_list = g_list_prepend(tmp_list, hook->name); list = list->next; } return tmp_list; } void plugin_command_foreach (session *sess, void *userdata, void (*cb) (session *sess, void *userdata, char *name, char *help)) { GSList *list; xchat_hook *hook; list = hook_list; while (list) { hook = list->data; if (hook->type == HOOK_COMMAND && hook->name[0]) { cb (sess, userdata, hook->name, hook->help_text); } list = list->next; } } int plugin_show_help (session *sess, char *cmd) { GSList *list; xchat_hook *hook; list = plugin_hook_find (hook_list, HOOK_COMMAND, cmd); if (list) { hook = list->data; if (hook->help_text) { PrintText (sess, hook->help_text); return 1; } } return 0; } /* ========================================================= */ /* ===== these are the functions plugins actually call ===== */ /* ========================================================= */ void * xchat_unhook (xchat_plugin *ph, xchat_hook *hook) { /* perl.c trips this */ if (!g_slist_find (hook_list, hook) || hook->type == HOOK_DELETED) return NULL; if (hook->type == HOOK_TIMER && hook->tag != 0) fe_timeout_remove (hook->tag); if (hook->type == HOOK_FD && hook->tag != 0) fe_input_remove (hook->tag); hook->type = HOOK_DELETED; /* expunge later */ if (hook->name) free (hook->name); /* NULL for timers & fds */ if (hook->help_text) free (hook->help_text); /* NULL for non-commands */ return hook->userdata; } xchat_hook * xchat_hook_command (xchat_plugin *ph, const char *name, int pri, xchat_cmd_cb *callb, const char *help_text, void *userdata) { return plugin_add_hook (ph, HOOK_COMMAND, pri, name, help_text, callb, 0, userdata); } xchat_hook * xchat_hook_server (xchat_plugin *ph, const char *name, int pri, xchat_serv_cb *callb, void *userdata) { return plugin_add_hook (ph, HOOK_SERVER, pri, name, 0, callb, 0, userdata); } xchat_hook * xchat_hook_print (xchat_plugin *ph, const char *name, int pri, xchat_print_cb *callb, void *userdata) { return plugin_add_hook (ph, HOOK_PRINT, pri, name, 0, callb, 0, userdata); } xchat_hook * xchat_hook_timer (xchat_plugin *ph, int timeout, xchat_timer_cb *callb, void *userdata) { return plugin_add_hook (ph, HOOK_TIMER, 0, 0, 0, callb, timeout, userdata); } xchat_hook * xchat_hook_fd (xchat_plugin *ph, int fd, int flags, xchat_fd_cb *callb, void *userdata) { xchat_hook *hook; hook = plugin_add_hook (ph, HOOK_FD, 0, 0, 0, callb, 0, userdata); hook->pri = fd; /* plugin hook_fd flags correspond exactly to FIA_* flags (fe.h) */ hook->tag = fe_input_add (fd, flags, plugin_fd_cb, hook); return hook; } void xchat_print (xchat_plugin *ph, const char *text) { if (!is_session (ph->context)) { DEBUG(PrintTextf(0, "%s\txchat_print called without a valid context.\n", ph->name)); return; } PrintText (ph->context, (char *)text); } void xchat_printf (xchat_plugin *ph, const char *format, ...) { va_list args; char *buf; va_start (args, format); buf = g_strdup_vprintf (format, args); va_end (args); xchat_print (ph, buf); g_free (buf); } void xchat_command (xchat_plugin *ph, const char *command) { char *conv; int len = -1; if (!is_session (ph->context)) { DEBUG(PrintTextf(0, "%s\txchat_command called without a valid context.\n", ph->name)); return; } /* scripts/plugins continue to send non-UTF8... *sigh* */ conv = text_validate ((char **)&command, &len); handle_command (ph->context, (char *)command, FALSE); g_free (conv); } void xchat_commandf (xchat_plugin *ph, const char *format, ...) { va_list args; char *buf; va_start (args, format); buf = g_strdup_vprintf (format, args); va_end (args); xchat_command (ph, buf); g_free (buf); } int xchat_nickcmp (xchat_plugin *ph, const char *s1, const char *s2) { return ((session *)ph->context)->server->p_cmp (s1, s2); } xchat_context * xchat_get_context (xchat_plugin *ph) { return ph->context; } int xchat_set_context (xchat_plugin *ph, xchat_context *context) { if (is_session (context)) { ph->context = context; return 1; } return 0; } xchat_context * xchat_find_context (xchat_plugin *ph, const char *servname, const char *channel) { GSList *slist, *clist, *sessions = NULL; server *serv; session *sess; char *netname; if (servname == NULL && channel == NULL) return current_sess; slist = serv_list; while (slist) { serv = slist->data; netname = server_get_network (serv, TRUE); if (servname == NULL || rfc_casecmp (servname, serv->servername) == 0 || g_ascii_strcasecmp (servname, serv->hostname) == 0 || g_ascii_strcasecmp (servname, netname) == 0) { if (channel == NULL) return serv->front_session; clist = sess_list; while (clist) { sess = clist->data; if (sess->server == serv) { if (rfc_casecmp (channel, sess->channel) == 0) { if (sess->server == ph->context->server) { g_slist_free (sessions); return sess; } else { sessions = g_slist_prepend (sessions, sess); } } } clist = clist->next; } } slist = slist->next; } if (sessions) { sessions = g_slist_reverse (sessions); sess = sessions->data; g_slist_free (sessions); return sess; } return NULL; } const char * xchat_get_info (xchat_plugin *ph, const char *id) { session *sess; guint32 hash; /* 1234567890 */ if (!strncmp (id, "event_text", 10)) { char *e = (char *)id + 10; if (*e == ' ') e++; /* 2.8.0 only worked without a space */ return text_find_format_string (e); } hash = str_hash (id); /* do the session independant ones first */ switch (hash) { case 0x325acab5: /* libdirfs */ return HEXCHATLIBDIR; case 0x14f51cd8: /* version */ return PACKAGE_VERSION; case 0xdd9b1abd: /* xchatdir */ return get_xdir_utf8 (); case 0xe33f6c4a: /* xchatdirfs */ return get_xdir_fs (); } sess = ph->context; if (!is_session (sess)) { DEBUG(PrintTextf(0, "%s\txchat_get_info called without a valid context.\n", ph->name)); return NULL; } switch (hash) { case 0x2de2ee: /* away */ if (sess->server->is_away) return sess->server->last_away_reason; return NULL; case 0x2c0b7d03: /* channel */ return sess->channel; case 0x2c0d614c: /* charset */ { const char *locale; if (sess->server->encoding) return sess->server->encoding; locale = NULL; g_get_charset (&locale); return locale; } case 0x30f5a8: /* host */ return sess->server->hostname; case 0x1c0e99c1: /* inputbox */ return fe_get_inputbox_contents (sess); case 0x633fb30: /* modes */ return sess->current_modes; case 0x6de15a2e: /* network */ return server_get_network (sess->server, FALSE); case 0x339763: /* nick */ return sess->server->nick; case 0x438fdf9: /* nickserv */ if (sess->server->network) return ((ircnet *)sess->server->network)->nickserv; return NULL; case 0xca022f43: /* server */ if (!sess->server->connected) return NULL; return sess->server->servername; case 0x696cd2f: /* topic */ return sess->topic; case 0x3419f12d: /* gtkwin_ptr */ return fe_gui_info_ptr (sess, 1); case 0x506d600b: /* native win_ptr */ return fe_gui_info_ptr (sess, 0); case 0x6d3431b5: /* win_status */ switch (fe_gui_info (sess, 0)) /* check window status */ { case 0: return "normal"; case 1: return "active"; case 2: return "hidden"; } return NULL; } return NULL; } int xchat_get_prefs (xchat_plugin *ph, const char *name, const char **string, int *integer) { int i = 0; /* some special run-time info (not really prefs, but may aswell throw it in here) */ switch (str_hash (name)) { case 0xf82136c4: /* state_cursor */ *integer = fe_get_inputbox_cursor (ph->context); return 2; case 0xd1b: /* id */ *integer = ph->context->server->id; return 2; } do { if (!g_ascii_strcasecmp (name, vars[i].name)) { switch (vars[i].type) { case TYPE_STR: *string = ((char *) &prefs + vars[i].offset); return 1; case TYPE_INT: *integer = *((int *) &prefs + vars[i].offset); return 2; default: /*case TYPE_BOOL:*/ if (*((int *) &prefs + vars[i].offset)) *integer = 1; else *integer = 0; return 3; } } i++; } while (vars[i].name); return 0; } xchat_list * xchat_list_get (xchat_plugin *ph, const char *name) { xchat_list *list; list = malloc (sizeof (xchat_list)); list->pos = NULL; switch (str_hash (name)) { case 0x556423d0: /* channels */ list->type = LIST_CHANNELS; list->next = sess_list; break; case 0x183c4: /* dcc */ list->type = LIST_DCC; list->next = dcc_list; break; case 0xb90bfdd2: /* ignore */ list->type = LIST_IGNORE; list->next = ignore_list; break; case 0xc2079749: /* notify */ list->type = LIST_NOTIFY; list->next = notify_list; list->head = (void *)ph->context; /* reuse this pointer */ break; case 0x6a68e08: /* users */ if (is_session (ph->context)) { list->type = LIST_USERS; list->head = list->next = userlist_flat_list (ph->context); fe_userlist_set_selected (ph->context); break; } /* fall through */ default: free (list); return NULL; } return list; } void xchat_list_free (xchat_plugin *ph, xchat_list *xlist) { if (xlist->type == LIST_USERS) g_slist_free (xlist->head); free (xlist); } int xchat_list_next (xchat_plugin *ph, xchat_list *xlist) { if (xlist->next == NULL) return 0; xlist->pos = xlist->next; xlist->next = xlist->pos->next; /* NOTIFY LIST: Find the entry which matches the context of the plugin when list_get was originally called. */ if (xlist->type == LIST_NOTIFY) { xlist->notifyps = notify_find_server_entry (xlist->pos->data, ((session *)xlist->head)->server); if (!xlist->notifyps) return 0; } return 1; } const char * const * xchat_list_fields (xchat_plugin *ph, const char *name) { static const char * const dcc_fields[] = { "iaddress32","icps", "sdestfile","sfile", "snick", "iport", "ipos", "iposhigh", "iresume", "iresumehigh", "isize", "isizehigh", "istatus", "itype", NULL }; static const char * const channels_fields[] = { "schannel", "schantypes", "pcontext", "iflags", "iid", "ilag", "imaxmodes", "snetwork", "snickmodes", "snickprefixes", "iqueue", "sserver", "itype", "iusers", NULL }; static const char * const ignore_fields[] = { "iflags", "smask", NULL }; static const char * const notify_fields[] = { "iflags", "snetworks", "snick", "toff", "ton", "tseen", NULL }; static const char * const users_fields[] = { "iaway", "shost", "tlasttalk", "snick", "sprefix", "srealname", "iselected", NULL }; static const char * const list_of_lists[] = { "channels", "dcc", "ignore", "notify", "users", NULL }; switch (str_hash (name)) { case 0x556423d0: /* channels */ return channels_fields; case 0x183c4: /* dcc */ return dcc_fields; case 0xb90bfdd2: /* ignore */ return ignore_fields; case 0xc2079749: /* notify */ return notify_fields; case 0x6a68e08: /* users */ return users_fields; case 0x6236395: /* lists */ return list_of_lists; } return NULL; } time_t xchat_list_time (xchat_plugin *ph, xchat_list *xlist, const char *name) { guint32 hash = str_hash (name); gpointer data; switch (xlist->type) { case LIST_NOTIFY: if (!xlist->notifyps) return (time_t) -1; switch (hash) { case 0x1ad6f: /* off */ return xlist->notifyps->lastoff; case 0xddf: /* on */ return xlist->notifyps->laston; case 0x35ce7b: /* seen */ return xlist->notifyps->lastseen; } break; case LIST_USERS: data = xlist->pos->data; switch (hash) { case 0xa9118c42: /* lasttalk */ return ((struct User *)data)->lasttalk; } } return (time_t) -1; } const char * xchat_list_str (xchat_plugin *ph, xchat_list *xlist, const char *name) { guint32 hash = str_hash (name); gpointer data = ph->context; int type = LIST_CHANNELS; /* a NULL xlist is a shortcut to current "channels" context */ if (xlist) { data = xlist->pos->data; type = xlist->type; } switch (type) { case LIST_CHANNELS: switch (hash) { case 0x2c0b7d03: /* channel */ return ((session *)data)->channel; case 0x577e0867: /* chantypes */ return ((session *)data)->server->chantypes; case 0x38b735af: /* context */ return data; /* this is a session * */ case 0x6de15a2e: /* network */ return server_get_network (((session *)data)->server, FALSE); case 0x8455e723: /* nickprefixes */ return ((session *)data)->server->nick_prefixes; case 0x829689ad: /* nickmodes */ return ((session *)data)->server->nick_modes; case 0xca022f43: /* server */ return ((session *)data)->server->servername; } break; case LIST_DCC: switch (hash) { case 0x3d9ad31e: /* destfile */ return ((struct DCC *)data)->destfile; case 0x2ff57c: /* file */ return ((struct DCC *)data)->file; case 0x339763: /* nick */ return ((struct DCC *)data)->nick; } break; case LIST_IGNORE: switch (hash) { case 0x3306ec: /* mask */ return ((struct ignore *)data)->mask; } break; case LIST_NOTIFY: switch (hash) { case 0x4e49ec05: /* networks */ return ((struct notify *)data)->networks; case 0x339763: /* nick */ return ((struct notify *)data)->name; } break; case LIST_USERS: switch (hash) { case 0x339763: /* nick */ return ((struct User *)data)->nick; case 0x30f5a8: /* host */ return ((struct User *)data)->hostname; case 0xc594b292: /* prefix */ return ((struct User *)data)->prefix; case 0xccc6d529: /* realname */ return ((struct User *)data)->realname; } break; } return NULL; } int xchat_list_int (xchat_plugin *ph, xchat_list *xlist, const char *name) { guint32 hash = str_hash (name); gpointer data = ph->context; int tmp, type = LIST_CHANNELS; /* a NULL xlist is a shortcut to current "channels" context */ if (xlist) { data = xlist->pos->data; type = xlist->type; } switch (type) { case LIST_DCC: switch (hash) { case 0x34207553: /* address32 */ return ((struct DCC *)data)->addr; case 0x181a6: /* cps */ return ((struct DCC *)data)->cps; case 0x349881: /* port */ return ((struct DCC *)data)->port; case 0x1b254: /* pos */ return ((struct DCC *)data)->pos & 0xffffffff; case 0xe8a945f6: /* poshigh */ return (((struct DCC *)data)->pos >> 32) & 0xffffffff; case 0xc84dc82d: /* resume */ return ((struct DCC *)data)->resumable & 0xffffffff; case 0xded4c74f: /* resumehigh */ return (((struct DCC *)data)->resumable >> 32) & 0xffffffff; case 0x35e001: /* size */ return ((struct DCC *)data)->size & 0xffffffff; case 0x3284d523: /* sizehigh */ return (((struct DCC *)data)->size >> 32) & 0xffffffff; case 0xcacdcff2: /* status */ return ((struct DCC *)data)->dccstat; case 0x368f3a: /* type */ return ((struct DCC *)data)->type; } break; case LIST_IGNORE: switch (hash) { case 0x5cfee87: /* flags */ return ((struct ignore *)data)->type; } break; case LIST_CHANNELS: switch (hash) { case 0xd1b: /* id */ return ((struct session *)data)->server->id; case 0x5cfee87: /* flags */ tmp = ((struct session *)data)->alert_taskbar; /* bit 10 */ tmp <<= 1; tmp |= ((struct session *)data)->alert_tray; /* 9 */ tmp <<= 1; tmp |= ((struct session *)data)->alert_beep; /* 8 */ tmp <<= 1; /*tmp |= ((struct session *)data)->color_paste;*/ /* 7 */ tmp <<= 1; tmp |= ((struct session *)data)->text_hidejoinpart; /* 6 */ tmp <<= 1; tmp |= ((struct session *)data)->server->have_idmsg; /* 5 */ tmp <<= 1; tmp |= ((struct session *)data)->server->have_whox; /* 4 */ tmp <<= 1; tmp |= ((struct session *)data)->server->end_of_motd;/* 3 */ tmp <<= 1; tmp |= ((struct session *)data)->server->is_away; /* 2 */ tmp <<= 1; tmp |= ((struct session *)data)->server->connecting; /* 1 */ tmp <<= 1; tmp |= ((struct session *)data)->server->connected; /* 0 */ return tmp; case 0x1a192: /* lag */ return ((struct session *)data)->server->lag; case 0x1916144c: /* maxmodes */ return ((struct session *)data)->server->modes_per_line; case 0x66f1911: /* queue */ return ((struct session *)data)->server->sendq_len; case 0x368f3a: /* type */ return ((struct session *)data)->type; case 0x6a68e08: /* users */ return ((struct session *)data)->total; } break; case LIST_NOTIFY: if (!xlist->notifyps) return -1; switch (hash) { case 0x5cfee87: /* flags */ return xlist->notifyps->ison; } case LIST_USERS: switch (hash) { case 0x2de2ee: /* away */ return ((struct User *)data)->away; case 0x4705f29b: /* selected */ return ((struct User *)data)->selected; } break; } return -1; } void * xchat_plugingui_add (xchat_plugin *ph, const char *filename, const char *name, const char *desc, const char *version, char *reserved) { #ifdef USE_PLUGIN ph = plugin_list_add (NULL, strdup (filename), strdup (name), strdup (desc), strdup (version), NULL, NULL, TRUE, TRUE); fe_pluginlist_update (); #endif return ph; } void xchat_plugingui_remove (xchat_plugin *ph, void *handle) { #ifdef USE_PLUGIN plugin_free (handle, FALSE, FALSE); #endif } int xchat_emit_print (xchat_plugin *ph, const char *event_name, ...) { va_list args; /* currently only 4 because no events use more than 4. This can be easily expanded without breaking the API. */ char *argv[4] = {NULL, NULL, NULL, NULL}; int i = 0; va_start (args, event_name); while (1) { argv[i] = va_arg (args, char *); if (!argv[i]) break; i++; if (i >= 4) break; } i = text_emit_by_name ((char *)event_name, ph->context, argv[0], argv[1], argv[2], argv[3]); va_end (args); return i; } char * xchat_gettext (xchat_plugin *ph, const char *msgid) { /* so that plugins can use xchat's internal gettext strings. */ /* e.g. The EXEC plugin uses this on Windows. */ return _(msgid); } void xchat_send_modes (xchat_plugin *ph, const char **targets, int ntargets, int modes_per_line, char sign, char mode) { char tbuf[514]; /* modes.c needs 512 + null */ send_channel_modes (ph->context, tbuf, (char **)targets, 0, ntargets, sign, mode, modes_per_line); } char * xchat_strip (xchat_plugin *ph, const char *str, int len, int flags) { return strip_color ((char *)str, len, flags); } void xchat_free (xchat_plugin *ph, void *ptr) { g_free (ptr); } static int xchat_pluginpref_set_str_real (xchat_plugin *pl, const char *var, const char *value, int mode) /* mode: 0 = delete, 1 = save */ { FILE *fpIn; int fhOut; int prevSetting; char confname[64]; char confname_tmp[69]; char buffer[512]; /* the same as in cfg_put_str */ char buffer_tmp[512]; char *canon; canon = g_strdup (pl->name); canonalize_key (canon); sprintf (confname, "plugin_%s.conf", canon); g_free (canon); sprintf (confname_tmp, "%s.new", confname); fhOut = xchat_open_file (confname_tmp, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE); fpIn = xchat_fopen_file (confname, "r", 0); if (fhOut == -1) /* unable to save, abort */ { return 0; } else if (fpIn == NULL) /* no previous config file, no parsing */ { if (mode) { sprintf (buffer, "%s = %s\n", var, value); write (fhOut, buffer, strlen (buffer)); close (fhOut); sprintf (buffer, "%s/%s", get_xdir_fs (), confname); sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp); #ifdef WIN32 unlink (buffer); #endif if (rename (buffer_tmp, buffer) == 0) { return 1; } else { return 0; } } else { /* mode = 0, we want to delete but the config file and thus the given setting does not exist, we're ready */ close (fhOut); return 1; } } else /* existing config file, preserve settings and find & replace current var value if any */ { prevSetting = 0; while (fscanf (fpIn, " %[^\n]", &buffer) != EOF) /* read whole lines including whitespaces */ { sprintf (buffer_tmp, "%s ", var); /* add one space, this way it works against var - var2 checks too */ if (strncmp (buffer_tmp, buffer, strlen (var) + 1) == 0) /* given setting already exists */ { if (mode) /* overwrite the existing matching setting if we are in save mode */ { sprintf (buffer, "%s = %s\n", var, value); } else /* erase the setting in delete mode */ { strcpy (buffer, ""); } prevSetting = 1; } else { strcat (buffer, "\n"); /* preserve the existing different settings */ } write (fhOut, buffer, strlen (buffer)); } fclose (fpIn); if (!prevSetting && mode) /* var doesn't exist currently, append if we're in save mode */ { sprintf (buffer, "%s = %s\n", var, value); write (fhOut, buffer, strlen (buffer)); } close (fhOut); sprintf (buffer, "%s/%s", get_xdir_fs (), confname); sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp); #ifdef WIN32 unlink (buffer); #endif if (rename (buffer_tmp, buffer) == 0) { return 1; } else { return 0; } } } int xchat_pluginpref_set_str (xchat_plugin *pl, const char *var, const char *value) { return xchat_pluginpref_set_str_real (pl, var, value, 1); } int xchat_pluginpref_get_str (xchat_plugin *pl, const char *var, char *dest) { int fh; int l; char confname[64]; char *canon; char *cfg; struct stat st; canon = g_strdup (pl->name); canonalize_key (canon); sprintf (confname, "plugin_%s.conf", canon); g_free (canon); /* partly borrowed from palette.c */ fh = xchat_open_file (confname, O_RDONLY, 0, 0); if (fh == -1) { return 0; } fstat (fh, &st); cfg = malloc (st.st_size + 1); if (!cfg) { close (fh); return 0; } cfg[0] = '\0'; l = read (fh, cfg, st.st_size); if (l >= 0) { cfg[l] = '\0'; } if (!cfg_get_str (cfg, var, dest, 512)) /* dest_len is the same as buffer size in set */ { free (cfg); close (fh); return 0; } free (cfg); close (fh); return 1; } int xchat_pluginpref_set_int (xchat_plugin *pl, const char *var, int value) { char buffer[12]; sprintf (buffer, "%d", value); return xchat_pluginpref_set_str_real (pl, var, buffer, 1); } int xchat_pluginpref_get_int (xchat_plugin *pl, const char *var) { char buffer[12]; if (xchat_pluginpref_get_str (pl, var, buffer)) { return atoi (buffer); } else { return -1; } } int xchat_pluginpref_delete (xchat_plugin *pl, const char *var) { return xchat_pluginpref_set_str_real (pl, var, 0, 0); } int xchat_pluginpref_list (xchat_plugin *pl, char* dest) { FILE *fpIn; char confname[64]; char buffer[512]; /* the same as in cfg_put_str */ char *token; token = g_strdup (pl->name); canonalize_key (token); sprintf (confname, "plugin_%s.conf", token); g_free (token); fpIn = xchat_fopen_file (confname, "r", 0); if (fpIn == NULL) /* no existing config file, no parsing */ { return 0; } else /* existing config file, get list of settings */ { strcpy (dest, ""); /* clean up garbage */ while (fscanf (fpIn, " %[^\n]", &buffer) != EOF) /* read whole lines including whitespaces */ { token = strtok (buffer, "="); strncat (dest, token, strlen (token) - 1); strcat (dest, ","); } fclose (fpIn); } return 1; }