1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
|
/*
* plugin-api.c - extra functions for plugin API
*
* Copyright (C) 2003-2014 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat 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 WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <locale.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-hashtable.h"
#include "../core/wee-hook.h"
#include "../core/wee-infolist.h"
#include "../core/wee-input.h"
#include "../core/wee-proxy.h"
#include "../core/wee-string.h"
#include "../core/wee-url.h"
#include "../core/wee-util.h"
#include "../core/wee-version.h"
#include "../gui/gui-bar.h"
#include "../gui/gui-bar-item.h"
#include "../gui/gui-bar-window.h"
#include "../gui/gui-buffer.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-completion.h"
#include "../gui/gui-color.h"
#include "../gui/gui-cursor.h"
#include "../gui/gui-filter.h"
#include "../gui/gui-history.h"
#include "../gui/gui-hotlist.h"
#include "../gui/gui-key.h"
#include "../gui/gui-layout.h"
#include "../gui/gui-line.h"
#include "../gui/gui-nicklist.h"
#include "../gui/gui-window.h"
#include "plugin.h"
#include "plugin-api.h"
#include "plugin-config.h"
/*
* Sets plugin charset.
*/
void
plugin_api_charset_set (struct t_weechat_plugin *plugin, const char *charset)
{
if (!plugin || !charset)
return;
if (plugin->charset)
free (plugin->charset);
plugin->charset = (charset) ? strdup (charset) : NULL;
}
/*
* Translates a string using gettext.
*/
const char *
plugin_api_gettext (const char *string)
{
return _(string);
}
/*
* Translates a string using gettext (with plural form).
*/
const char *
plugin_api_ngettext (const char *single, const char *plural, int count)
{
/* make C compiler happy */
(void) single;
(void) count;
return NG_(single, plural, count);
}
/*
* Gets pointer on an option.
*/
struct t_config_option *
plugin_api_config_get (const char *option_name)
{
struct t_config_option *ptr_option;
config_file_search_with_string (option_name, NULL, NULL, &ptr_option, NULL);
return ptr_option;
}
/*
* Gets value of a plugin option.
*/
const char *
plugin_api_config_get_plugin (struct t_weechat_plugin *plugin,
const char *option_name)
{
struct t_config_option *ptr_option;
if (!plugin || !option_name)
return NULL;
ptr_option = plugin_config_search (plugin->name, option_name);
if (ptr_option)
return ptr_option->value;
/* option not found */
return NULL;
}
/*
* Checks if a plugin option is set.
*
* Returns:
* 1: plugin option is set
* 0: plugin option does not exist
*/
int
plugin_api_config_is_set_plugin (struct t_weechat_plugin *plugin,
const char *option_name)
{
struct t_config_option *ptr_option;
if (!plugin || !option_name)
return 0;
ptr_option = plugin_config_search (plugin->name, option_name);
if (ptr_option)
return 1;
return 0;
}
/*
* Sets value of a plugin option.
*/
int
plugin_api_config_set_plugin (struct t_weechat_plugin *plugin,
const char *option_name, const char *value)
{
if (!plugin || !option_name)
return WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND;
return plugin_config_set (plugin->name, option_name, value);
}
/*
* Sets description of a plugin option.
*/
void
plugin_api_config_set_desc_plugin (struct t_weechat_plugin *plugin,
const char *option_name,
const char *description)
{
if (plugin && option_name)
plugin_config_set_desc (plugin->name, option_name, description);
}
/*
* Unsets a plugin option.
*/
int
plugin_api_config_unset_plugin (struct t_weechat_plugin *plugin,
const char *option_name)
{
struct t_config_option *ptr_option;
if (!plugin || !option_name)
return WEECHAT_CONFIG_OPTION_UNSET_ERROR;
ptr_option = plugin_config_search (plugin->name, option_name);
if (!ptr_option)
return WEECHAT_CONFIG_OPTION_UNSET_ERROR;
return config_file_option_unset (ptr_option);
}
/*
* Returns a prefix for display with printf.
*/
const char *
plugin_api_prefix (const char *prefix)
{
if (!prefix)
return gui_chat_prefix_empty;
if (string_strcasecmp (prefix, "error") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_ERROR];
if (string_strcasecmp (prefix, "network") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_NETWORK];
if (string_strcasecmp (prefix, "action") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_ACTION];
if (string_strcasecmp (prefix, "join") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_JOIN];
if (string_strcasecmp (prefix, "quit") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_QUIT];
return gui_chat_prefix_empty;
}
/*
* Returns a WeeChat color for display with printf.
*/
const char *
plugin_api_color (const char *color_name)
{
const char *str_color;
if (!color_name)
return GUI_NO_COLOR;
/* name is a weechat color option ? => then return this color */
str_color = gui_color_search_config (color_name);
if (str_color)
return str_color;
return gui_color_get_custom (color_name);
}
/*
* Executes a command on a buffer (simulates user entry).
*/
void
plugin_api_command (struct t_weechat_plugin *plugin,
struct t_gui_buffer *buffer, const char *command)
{
char *command2;
if (!plugin || !command)
return;
command2 = string_iconv_to_internal (plugin->charset, command);
if (!buffer)
buffer = gui_current_window->buffer;
input_data (buffer, (command2) ? command2 : command);
if (command2)
free (command2);
}
/*
* Modifier to decode ANSI colors.
*/
char *
plugin_api_modifier_color_decode_ansi (void *data,
const char *modifier,
const char *modifier_data,
const char *string)
{
/* make C compiler happy */
(void) data;
(void) modifier;
return gui_color_decode_ansi (string,
(modifier_data && (strcmp (modifier_data, "1") == 0)) ?
1: 0);
}
/*
* Gets info about WeeChat.
*/
const char *
plugin_api_info_get_internal (void *data, const char *info_name,
const char *arguments)
{
time_t inactivity;
static char value[32], version_number[32] = { '\0' };
static char weechat_dir_absolute_path[PATH_MAX] = { '\0' };
int rgb, limit;
char *pos, *color;
/* make C compiler happy */
(void) data;
if (!info_name)
return NULL;
if (string_strcasecmp (info_name, "version") == 0)
{
return version_get_version ();
}
else if (string_strcasecmp (info_name, "version_number") == 0)
{
if (!version_number[0])
{
snprintf (version_number, sizeof (version_number), "%d",
util_version_number (version_get_version ()));
}
return version_number;
}
else if (string_strcasecmp (info_name, "version_git") == 0)
{
return version_get_git ();
}
else if (string_strcasecmp (info_name, "date") == 0)
{
return version_get_compilation_date ();
}
else if (string_strcasecmp (info_name, "dir_separator") == 0)
{
return DIR_SEPARATOR;
}
else if (string_strcasecmp (info_name, "weechat_dir") == 0)
{
if (!weechat_dir_absolute_path[0])
{
if (!realpath (weechat_home, weechat_dir_absolute_path))
return NULL;
}
return (weechat_dir_absolute_path[0]) ?
weechat_dir_absolute_path : weechat_home;
}
else if (string_strcasecmp (info_name, "weechat_libdir") == 0)
{
return WEECHAT_LIBDIR;
}
else if (string_strcasecmp (info_name, "weechat_sharedir") == 0)
{
return WEECHAT_SHAREDIR;
}
else if (string_strcasecmp (info_name, "weechat_localedir") == 0)
{
return LOCALEDIR;
}
else if (string_strcasecmp (info_name, "weechat_site") == 0)
{
return WEECHAT_WEBSITE;
}
else if (string_strcasecmp (info_name, "weechat_site_download") == 0)
{
return WEECHAT_WEBSITE_DOWNLOAD;
}
else if (string_strcasecmp (info_name, "weechat_upgrading") == 0)
{
snprintf (value, sizeof (value), "%d", weechat_upgrading);
return value;
}
else if (string_strcasecmp (info_name, "charset_terminal") == 0)
{
return weechat_local_charset;
}
else if (string_strcasecmp (info_name, "charset_internal") == 0)
{
return WEECHAT_INTERNAL_CHARSET;
}
else if (string_strcasecmp (info_name, "locale") == 0)
{
return setlocale (LC_MESSAGES, NULL);
}
else if (string_strcasecmp (info_name, "inactivity") == 0)
{
if (gui_key_last_activity_time == 0)
inactivity = 0;
else
inactivity = time (NULL) - gui_key_last_activity_time;
snprintf (value, sizeof (value), "%ld", (long int)inactivity);
return value;
}
else if (string_strcasecmp (info_name, "filters_enabled") == 0)
{
snprintf (value, sizeof (value), "%d", gui_filters_enabled);
return value;
}
else if (string_strcasecmp (info_name, "cursor_mode") == 0)
{
snprintf (value, sizeof (value), "%d", gui_cursor_mode);
return value;
}
else if (string_strcasecmp (info_name, "term_width") == 0)
{
snprintf (value, sizeof (value), "%d", gui_window_get_width ());
return value;
}
else if (string_strcasecmp (info_name, "term_height") == 0)
{
snprintf (value, sizeof (value), "%d", gui_window_get_height ());
return value;
}
else if (string_strcasecmp (info_name, "color_ansi_regex") == 0)
{
return GUI_COLOR_REGEX_ANSI_DECODE;
}
else if (string_strcasecmp (info_name, "color_term2rgb") == 0)
{
if (arguments && arguments[0])
{
snprintf (value, sizeof (value),
"%d",
gui_color_convert_term_to_rgb (atoi (arguments)));
return value;
}
}
else if (string_strcasecmp (info_name, "color_rgb2term") == 0)
{
if (arguments && arguments[0])
{
limit = 256;
pos = strchr (arguments, ',');
if (pos)
{
color = string_strndup (arguments, pos - arguments);
if (!color)
return NULL;
rgb = atoi (color);
limit = atoi (pos + 1);
free (color);
}
else
rgb = atoi (arguments);
snprintf (value, sizeof (value),
"%d",
gui_color_convert_rgb_to_term (rgb, limit));
return value;
}
}
/* info not found */
return NULL;
}
/*
* Gets infolist about WeeChat.
*
* Note: result must be freed with function "weechat_infolist_free".
*/
struct t_infolist *
plugin_api_infolist_get_internal (void *data, const char *infolist_name,
void *pointer, const char *arguments)
{
struct t_infolist *ptr_infolist;
struct t_gui_bar *ptr_bar;
struct t_gui_bar_item *ptr_bar_item;
struct t_gui_bar_window *ptr_bar_window;
struct t_gui_buffer *ptr_buffer;
struct t_gui_line *ptr_line;
struct t_gui_history *ptr_history;
struct t_gui_filter *ptr_filter;
struct t_gui_window *ptr_window;
struct t_gui_hotlist *ptr_hotlist;
struct t_gui_key *ptr_key;
struct t_weechat_plugin *ptr_plugin;
struct t_proxy *ptr_proxy;
struct t_gui_layout *ptr_layout;
int context, number, i;
char *error;
/* make C compiler happy */
(void) data;
if (!infolist_name || !infolist_name[0])
return NULL;
if (string_strcasecmp (infolist_name, "bar") == 0)
{
/* invalid bar pointer ? */
if (pointer && (!gui_bar_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one bar */
if (!gui_bar_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all bars matching arguments */
for (ptr_bar = gui_bars; ptr_bar; ptr_bar = ptr_bar->next_bar)
{
if (!arguments || !arguments[0]
|| string_match (ptr_bar->name, arguments, 0))
{
if (!gui_bar_add_to_infolist (ptr_infolist, ptr_bar))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "bar_item") == 0)
{
/* invalid bar item pointer ? */
if (pointer && (!gui_bar_item_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one bar item */
if (!gui_bar_item_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all bar items matching arguments */
for (ptr_bar_item = gui_bar_items; ptr_bar_item;
ptr_bar_item = ptr_bar_item->next_item)
{
if (!arguments || !arguments[0]
|| string_match (ptr_bar_item->name, arguments, 0))
{
if (!gui_bar_item_add_to_infolist (ptr_infolist, ptr_bar_item))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "bar_window") == 0)
{
/* invalid bar window pointer ? */
if (pointer && (!gui_bar_window_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one bar window */
if (!gui_bar_window_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all bar windows (from root and window bars) */
for (ptr_bar = gui_bars; ptr_bar; ptr_bar = ptr_bar->next_bar)
{
if (ptr_bar->bar_window)
{
if (!gui_bar_window_add_to_infolist (ptr_infolist, ptr_bar->bar_window))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
for (ptr_window = gui_windows; ptr_window;
ptr_window = ptr_window->next_window)
{
for (ptr_bar_window = ptr_window->bar_windows;
ptr_bar_window;
ptr_bar_window = ptr_bar_window->next_bar_window)
{
if (!gui_bar_window_add_to_infolist (ptr_infolist, ptr_bar_window))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "buffer") == 0)
{
/* invalid buffer pointer ? */
if (pointer && (!gui_buffer_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one buffer */
if (!gui_buffer_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all buffers matching arguments */
for (ptr_buffer = gui_buffers; ptr_buffer;
ptr_buffer = ptr_buffer->next_buffer)
{
if (!arguments || !arguments[0]
|| string_match (ptr_buffer->full_name, arguments, 0))
{
if (!gui_buffer_add_to_infolist (ptr_infolist, ptr_buffer))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "buffer_lines") == 0)
{
if (!pointer)
pointer = gui_buffers;
else
{
/* invalid buffer pointer ? */
if (!gui_buffer_valid (pointer))
return NULL;
}
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (ptr_line = ((struct t_gui_buffer *)pointer)->own_lines->first_line;
ptr_line; ptr_line = ptr_line->next_line)
{
if (!gui_line_add_to_infolist (ptr_infolist,
((struct t_gui_buffer *)pointer)->own_lines,
ptr_line))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "filter") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (ptr_filter = gui_filters; ptr_filter;
ptr_filter = ptr_filter->next_filter)
{
if (!arguments || !arguments[0]
|| string_match (ptr_filter->name, arguments, 0))
{
if (!gui_filter_add_to_infolist (ptr_infolist, ptr_filter))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "history") == 0)
{
/* invalid buffer pointer ? */
if (pointer && (!gui_buffer_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (ptr_history = (pointer) ?
((struct t_gui_buffer *)pointer)->history : gui_history;
ptr_history; ptr_history = ptr_history->next_history)
{
if (!gui_history_add_to_infolist (ptr_infolist, ptr_history))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "hook") == 0)
{
/* invalid hook pointer ? */
if (pointer && !hook_valid (pointer))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (!hook_add_to_infolist (ptr_infolist, pointer, arguments))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "hotlist") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (ptr_hotlist = gui_hotlist; ptr_hotlist;
ptr_hotlist = ptr_hotlist->next_hotlist)
{
if (!gui_hotlist_add_to_infolist (ptr_infolist, ptr_hotlist))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "key") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (arguments && arguments[0])
context = gui_key_search_context (arguments);
else
context = GUI_KEY_CONTEXT_DEFAULT;
if (context >= 0)
{
for (ptr_key = gui_keys[context]; ptr_key;
ptr_key = ptr_key->next_key)
{
if (!gui_key_add_to_infolist (ptr_infolist, ptr_key))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "layout") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (ptr_layout = gui_layouts; ptr_layout;
ptr_layout = ptr_layout->next_layout)
{
if (!gui_layout_add_to_infolist (ptr_infolist,ptr_layout))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "nicklist") == 0)
{
/* invalid buffer pointer ? */
if (!pointer || (!gui_buffer_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (!gui_nicklist_add_to_infolist (ptr_infolist, pointer, arguments))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "option") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (!config_file_add_to_infolist (ptr_infolist, arguments))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "plugin") == 0)
{
/* invalid plugin pointer ? */
if (pointer && (!plugin_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one plugin */
if (!plugin_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all plugins matching arguments */
for (ptr_plugin = weechat_plugins; ptr_plugin;
ptr_plugin = ptr_plugin->next_plugin)
{
if (!arguments || !arguments[0]
|| string_match (ptr_plugin->name, arguments, 0))
{
if (!plugin_add_to_infolist (ptr_infolist, ptr_plugin))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "proxy") == 0)
{
/* invalid proxy pointer ? */
if (pointer && (!proxy_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one proxy */
if (!proxy_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all proxies matching arguments */
for (ptr_proxy = weechat_proxies; ptr_proxy;
ptr_proxy = ptr_proxy->next_proxy)
{
if (!arguments || !arguments[0]
|| string_match (ptr_proxy->name, arguments, 0))
{
if (!proxy_add_to_infolist (ptr_infolist, ptr_proxy))
{
infolist_free (ptr_infolist);
return NULL;
}
}
}
return ptr_infolist;
}
}
}
else if (string_strcasecmp (infolist_name, "url_options") == 0)
{
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
for (i = 0; url_options[i].name; i++)
{
if (!weeurl_option_add_to_infolist (ptr_infolist, &url_options[i]))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
else if (string_strcasecmp (infolist_name, "window") == 0)
{
/* invalid window pointer ? */
if (pointer && (!gui_window_valid (pointer)))
return NULL;
ptr_infolist = infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one window */
if (!gui_window_add_to_infolist (ptr_infolist, pointer))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
if (arguments && arguments[0])
{
if ((string_strcasecmp (arguments, "current") == 0))
{
if (gui_current_window)
{
if (!gui_window_add_to_infolist (ptr_infolist,
gui_current_window))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
return NULL;
}
/* check if argument is a window number */
error = NULL;
number = (int)strtol (arguments, &error, 10);
if (error && !error[0])
{
ptr_window = gui_window_search_by_number (number);
if (ptr_window)
{
if (!gui_window_add_to_infolist (ptr_infolist,
ptr_window))
{
infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
}
return NULL;
}
else
{
/* build list with all windows */
for (ptr_window = gui_windows; ptr_window;
ptr_window = ptr_window->next_window)
{
if (!gui_window_add_to_infolist (ptr_infolist,
ptr_window))
{
infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
}
}
/* infolist not found */
return NULL;
}
/*
* Moves item pointer to next item in an infolist.
*
* Returns:
* 1: pointer is still OK
* 0: end of infolist was reached
*/
int
plugin_api_infolist_next (struct t_infolist *infolist)
{
if (!infolist || !infolist_valid (infolist))
return 0;
return (infolist_next (infolist)) ? 1 : 0;
}
/*
* Moves pointer to previous item in an infolist.
*
* Returns:
* 1: pointer is still OK
* 0: beginning of infolist was reached
*/
int
plugin_api_infolist_prev (struct t_infolist *infolist)
{
if (!infolist || !infolist_valid (infolist))
return 0;
return (infolist_prev (infolist)) ? 1 : 0;
}
/*
* Resets item cursor in infolist.
*/
void
plugin_api_infolist_reset_item_cursor (struct t_infolist *infolist)
{
if (infolist && infolist_valid (infolist))
{
infolist_reset_item_cursor (infolist);
}
}
/*
* Gets list of fields for current infolist item.
*/
const char *
plugin_api_infolist_fields (struct t_infolist *infolist)
{
if (!infolist || !infolist_valid (infolist))
return NULL;
return infolist_fields (infolist);
}
/*
* Gets integer value for a variable in current infolist item.
*/
int
plugin_api_infolist_integer (struct t_infolist *infolist, const char *var)
{
if (!infolist || !infolist_valid (infolist) || !infolist->ptr_item)
return 0;
return infolist_integer (infolist, var);
}
/*
* Gets string value for a variable in current infolist item.
*/
const char *
plugin_api_infolist_string (struct t_infolist *infolist, const char *var)
{
if (!infolist || !infolist_valid (infolist) || !infolist->ptr_item)
return NULL;
return infolist_string (infolist, var);
}
/*
* Gets pointer value for a variable in current infolist item.
*/
void *
plugin_api_infolist_pointer (struct t_infolist *infolist, const char *var)
{
if (!infolist || !infolist_valid (infolist) || !infolist->ptr_item)
return NULL;
return infolist_pointer (infolist, var);
}
/*
* Gets buffer value for a variable in current infolist item.
*
* Argument "size" is set with the size of buffer.
*/
void *
plugin_api_infolist_buffer (struct t_infolist *infolist, const char *var,
int *size)
{
if (!infolist || !infolist_valid (infolist) || !infolist->ptr_item)
return NULL;
return infolist_buffer (infolist, var, size);
}
/*
* Gets time value for a variable in current infolist item.
*/
time_t
plugin_api_infolist_time (struct t_infolist *infolist, const char *var)
{
if (!infolist || !infolist_valid (infolist) || !infolist->ptr_item)
return 0;
return infolist_time (infolist, var);
}
/*
* Frees an infolist.
*/
void
plugin_api_infolist_free (struct t_infolist *infolist)
{
if (infolist && infolist_valid (infolist))
infolist_free (infolist);
}
/*
* Initializes plugin API.
*/
void
plugin_api_init ()
{
/* WeeChat core modifiers */
hook_modifier (NULL, "color_decode_ansi",
&plugin_api_modifier_color_decode_ansi, NULL);
/* WeeChat core info hooks */
hook_info (NULL, "version", N_("WeeChat version"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "version_number", N_("WeeChat version (as number)"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "version_git", N_("WeeChat git version (output of "
"command \"git describe\" for a "
"development version only, empty for a "
"stable release)"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "date", N_("WeeChat compilation date"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "dir_separator", N_("directory separator"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_dir", N_("WeeChat directory"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_libdir", N_("WeeChat \"lib\" directory"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_sharedir", N_("WeeChat \"share\" directory"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_localedir", N_("WeeChat \"locale\" directory"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_site", N_("WeeChat site"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_site_download", N_("WeeChat site, download page"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "weechat_upgrading", N_("1 if WeeChat is upgrading (command `/upgrade`)"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "charset_terminal", N_("terminal charset"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "charset_internal", N_("WeeChat internal charset"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "locale", N_("locale used for translating messages"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "inactivity", N_("keyboard inactivity (seconds)"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "filters_enabled", N_("1 if filters are enabled"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "cursor_mode", N_("1 if cursor mode is enabled"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "term_width", N_("width of terminal"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "term_height", N_("height of terminal"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_ansi_regex", N_("POSIX extended regular expression to search ANSI escape codes"), NULL,
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_term2rgb", N_("terminal color (0-255) converted to RGB color"),
N_("color (terminal color: 0-255)"),
&plugin_api_info_get_internal, NULL);
hook_info (NULL, "color_rgb2term", N_("RGB color converted to terminal color (0-255)"),
N_("rgb,limit (limit is optional and is set to 256 by default)"),
&plugin_api_info_get_internal, NULL);
/* WeeChat core infolist hooks */
hook_infolist (NULL, "bar", N_("list of bars"),
N_("bar pointer (optional)"),
N_("bar name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "bar_item", N_("list of bar items"),
N_("bar item pointer (optional)"),
N_("bar item name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "bar_window", N_("list of bar windows"),
N_("bar window pointer (optional)"),
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "buffer", N_("list of buffers"),
N_("buffer pointer (optional)"),
N_("buffer name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "buffer_lines", N_("lines of a buffer"),
N_("buffer pointer"),
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "filter", N_("list of filters"),
NULL,
N_("filter name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "history", N_("history of commands"),
N_("buffer pointer (if not set, return global history) (optional)"),
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "hook", N_("list of hooks"),
N_("hook pointer (optional)"),
N_("type,arguments (type is command/timer/.., arguments to "
"get only some hooks (wildcard \"*\" is allowed), "
"both are optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "hotlist", N_("list of buffers in hotlist"),
NULL,
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "key", N_("list of key bindings"),
NULL,
N_("context (\"default\", \"search\", \"cursor\" or "
"\"mouse\") (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "layout", N_("list of layouts"),
NULL,
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "nicklist", N_("nicks in nicklist for a buffer"),
N_("buffer pointer"),
N_("nick_xxx or group_xxx to get only nick/group xxx "
"(optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "option", N_("list of options"),
NULL,
N_("option name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "plugin", N_("list of plugins"),
N_("plugin pointer (optional)"),
N_("plugin name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "proxy", N_("list of proxies"),
N_("proxy pointer (optional)"),
N_("proxy name (wildcard \"*\" is allowed) (optional)"),
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "url_options", N_("options for URL"),
NULL,
NULL,
&plugin_api_infolist_get_internal, NULL);
hook_infolist (NULL, "window", N_("list of windows"),
N_("window pointer (optional)"),
N_("\"current\" for current window or a window number (optional)"),
&plugin_api_infolist_get_internal, NULL);
/* WeeChat core hdata */
hook_hdata (NULL, "bar", N_("bar"),
&gui_bar_hdata_bar_cb, NULL);
hook_hdata (NULL, "bar_item", N_("bar item"),
&gui_bar_item_hdata_bar_item_cb, NULL);
hook_hdata (NULL, "bar_window", N_("bar window"),
&gui_bar_window_hdata_bar_window_cb, NULL);
hook_hdata (NULL, "buffer", N_("buffer"),
&gui_buffer_hdata_buffer_cb, NULL);
hook_hdata (NULL, "buffer_visited", N_("visited buffer"),
&gui_buffer_hdata_buffer_visited_cb, NULL);
hook_hdata (NULL, "completion", N_("structure with completion"),
&gui_completion_hdata_completion_cb, NULL);
hook_hdata (NULL, "completion_partial", N_("structure with partial completion"),
&gui_completion_hdata_completion_partial_cb, NULL);
hook_hdata (NULL, "config_file", N_("config file"),
&config_file_hdata_config_file_cb, NULL);
hook_hdata (NULL, "config_section", N_("config section"),
&config_file_hdata_config_section_cb, NULL);
hook_hdata (NULL, "config_option", N_("config option"),
&config_file_hdata_config_option_cb, NULL);
hook_hdata (NULL, "filter", N_("filter"),
&gui_filter_hdata_filter_cb, NULL);
hook_hdata (NULL, "history", N_("history of commands in buffer"),
&gui_history_hdata_history_cb, NULL);
hook_hdata (NULL, "hotlist", N_("hotlist"),
&gui_hotlist_hdata_hotlist_cb, NULL);
hook_hdata (NULL, "input_undo", N_("structure with undo for input line"),
&gui_buffer_hdata_input_undo_cb, NULL);
hook_hdata (NULL, "key", N_("a key (keyboard shortcut)"),
&gui_key_hdata_key_cb, NULL);
hook_hdata (NULL, "layout", N_("layout"),
&gui_layout_hdata_layout_cb, NULL);
hook_hdata (NULL, "layout_buffer", N_("buffer layout"),
&gui_layout_hdata_layout_buffer_cb, NULL);
hook_hdata (NULL, "layout_window", N_("window layout"),
&gui_layout_hdata_layout_window_cb, NULL);
hook_hdata (NULL, "lines", N_("structure with lines"),
&gui_line_hdata_lines_cb, NULL);
hook_hdata (NULL, "line", N_("structure with one line"),
&gui_line_hdata_line_cb, NULL);
hook_hdata (NULL, "line_data", N_("structure with one line data"),
&gui_line_hdata_line_data_cb, NULL);
hook_hdata (NULL, "nick_group", N_("group in nicklist"),
&gui_nicklist_hdata_nick_group_cb, NULL);
hook_hdata (NULL, "nick", N_("nick in nicklist"),
&gui_nicklist_hdata_nick_cb, NULL);
hook_hdata (NULL, "plugin", N_("plugin"),
&plugin_hdata_plugin_cb, NULL);
hook_hdata (NULL, "proxy", N_("proxy"),
&proxy_hdata_proxy_cb, NULL);
hook_hdata (NULL, "window", N_("window"),
&gui_window_hdata_window_cb, NULL);
hook_hdata (NULL, "window_scroll", N_("scroll info in window"),
&gui_window_hdata_window_scroll_cb, NULL);
hook_hdata (NULL, "window_tree", N_("tree of windows"),
&gui_window_hdata_window_tree_cb, NULL);
}
|