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
|
/*
* Copyright (c) 2003-2007 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/* plugin-api.c: WeeChat <--> plugin API */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../core/weechat.h"
#include "../core/wee-command.h"
#include "../core/wee-config.h"
#include "../core/wee-hook.h"
#include "../core/wee-input.h"
#include "../core/wee-log.h"
#include "../core/wee-string.h"
#include "../core/wee-utf8.h"
#include "../core/wee-util.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-infobar.h"
#include "../gui/gui-input.h"
#include "../gui/gui-keyboard.h"
#include "../gui/gui-nicklist.h"
#include "../gui/gui-status.h"
#include "../gui/gui-window.h"
#include "plugin.h"
#include "plugin-config.h"
#include "plugin-list.h"
/*
* plugin_api_charset_set: set plugin charset
*/
void
plugin_api_charset_set (struct t_weechat_plugin *plugin, char *charset)
{
if (!plugin || !charset)
return;
if (plugin->charset)
free (plugin->charset);
plugin->charset = (charset) ? strdup (charset) : NULL;
}
/*
* plugin_api_iconv_to_internal: encode string from a charset to WeeChat
* internal charset
*/
char *
plugin_api_iconv_to_internal (struct t_weechat_plugin *plugin,
char *charset, char *string)
{
if (!plugin || !string)
return NULL;
return string_iconv_to_internal (charset, string);
}
/*
* plugin_api_iconv_from_internal: encode string from WeeChat internal
* charset to another
*/
char *
plugin_api_iconv_from_internal (struct t_weechat_plugin *plugin,
char *charset, char *string)
{
if (!plugin || !string)
return NULL;
return string_iconv_from_internal (charset, string);
}
/*
* plugin_api_gettext: translate a string using gettext
*/
char *
plugin_api_gettext (struct t_weechat_plugin *plugin, char *string)
{
/* make C compiler happy */
(void) plugin;
return _(string);
}
/*
* plugin_api_ngettext: translate a string using gettext
*/
char *
plugin_api_ngettext (struct t_weechat_plugin *plugin, char *single,
char *plural, int count)
{
/* make C compiler happy */
(void) plugin;
return NG_(single, plural, count);
}
/*
* plugin_api_strcasecmp: locale and case independent string comparison
*/
int
plugin_api_strcasecmp (struct t_weechat_plugin *plugin,
char *string1, char *string2)
{
/* make C compiler happy */
(void) plugin;
return string_strcasecmp (string1, string2);
}
/*
* plugin_api_strncasecmp: locale and case independent string comparison
* with max length
*/
int
plugin_api_strncasecmp (struct t_weechat_plugin *plugin,
char *string1, char *string2, int max)
{
/* make C compiler happy */
(void) plugin;
return string_strncasecmp (string1, string2, max);
}
/*
* plugin_api_string_explode: explode a string
*/
char **
plugin_api_string_explode (struct t_weechat_plugin *plugin, char *string,
char *separators, int num_items_max,
int *num_items)
{
/* make C compiler happy */
(void) plugin;
if (!plugin || !string || !separators || !num_items)
return NULL;
return string_explode (string, separators, num_items_max,
num_items);
}
/*
* plugin_api_string_free_exploded: free exploded string
*/
void
plugin_api_string_free_exploded (struct t_weechat_plugin *plugin,
char **exploded_string)
{
/* make C compiler happy */
(void) plugin;
string_free_exploded (exploded_string);
}
/*
* plugin_api_mkdir_home: create a directory in WeeChat home
*/
int
plugin_api_mkdir_home (struct t_weechat_plugin *plugin, char *directory)
{
char *dir_name;
int dir_length;
/* make C compiler happy */
(void) plugin;
if (!directory)
return 0;
/* build directory, adding WeeChat home */
dir_length = strlen (weechat_home) + strlen (directory) + 2;
dir_name =
(char *) malloc (dir_length * sizeof (char));
if (!dir_name)
return 0;
snprintf (dir_name, dir_length, "%s/%s", weechat_home, directory);
if (mkdir (dir_name, 0755) < 0)
{
if (errno != EEXIST)
{
free (dir_name);
return 0;
}
}
free (dir_name);
return 1;
}
/*
* plugin_api_exec_on_files: find files in a directory and execute a
* function on each file
*/
void
plugin_api_exec_on_files (struct t_weechat_plugin *plugin, char *directory,
int (*callback)(char *))
{
/* make C compiler happy */
(void) plugin;
if (directory && callback)
util_exec_on_files (directory, callback);
}
/*
* plugin_api_printf: print a message on a buffer
*/
void
plugin_api_printf (struct t_weechat_plugin *plugin,
void *buffer, char *format, ...)
{
va_list argptr;
char buf[8192];
if (!plugin || !format)
return;
va_start (argptr, format);
vsnprintf (buf, sizeof (buf) - 1, format, argptr);
va_end (argptr);
gui_chat_printf ((struct t_gui_buffer *)buffer, buf);
}
/*
* plugin_api_prefix: return a prefix for display with printf
*/
char *
plugin_api_prefix (struct t_weechat_plugin *plugin, char *prefix)
{
static char empty_prefix[] = "";
if (!plugin || !prefix)
return empty_prefix;
if (string_strcasecmp (prefix, "info") == 0)
return gui_chat_prefix[GUI_CHAT_PREFIX_INFO];
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 empty_prefix;
}
/*
* plugin_api_color: return a WeeChat color for display with printf
*/
char *
plugin_api_color (struct t_weechat_plugin *plugin, char *color_name)
{
int num_color;
if (!plugin || !color_name)
return GUI_NO_COLOR;
num_color = gui_color_search_config (color_name);
if (num_color >= 0)
return GUI_COLOR(num_color);
return GUI_NO_COLOR;
}
/*
* plugin_api_print_infobar: print a message in infobar
*/
void
plugin_api_print_infobar (struct t_weechat_plugin *plugin, int time_displayed,
char *message, ...)
{
(void) plugin;
(void) time_displayed;
(void) message;
/*va_list argptr;
static char buf[1024];
char *buf2;
if (!plugin || (time_displayed < 0) || !message)
return;
va_start (argptr, message);
vsnprintf (buf, sizeof (buf) - 1, message, argptr);
va_end (argptr);
buf2 = string_iconv_to_internal (plugin->charset, buf);
gui_infobar_printf (time_displayed, GUI_COLOR_WIN_INFOBAR, "%s",
(buf2) ? buf2 : buf);
if (buf2)
free (buf2);*/
}
/*
* plugin_api_infobar_remove: remove message(s) in infobar
*/
void
plugin_api_infobar_remove (struct t_weechat_plugin *plugin, int how_many)
{
if (!plugin)
return;
if (how_many <= 0)
gui_infobar_remove_all ();
else
{
while ((gui_infobar) && (how_many > 0))
{
gui_infobar_remove ();
how_many--;
}
}
gui_infobar_draw (gui_current_window->buffer, 1);
}
/*
* plugin_api_hook_command: hook a command
*/
struct t_hook *
plugin_api_hook_command (struct t_weechat_plugin *plugin, char *command,
char *description, char *args,
char *args_desc, char *completion,
int (*callback)(void *, char *), void *data)
{
if (plugin && callback)
return hook_command (plugin, command, description, args,
args_desc, completion,
callback, data);
return NULL;
}
/*
* plugin_api_hook_message: hook a message
*/
struct t_hook *
plugin_api_hook_message (struct t_weechat_plugin *plugin, char *message,
int (*callback)(void *, char *), void *data)
{
if (plugin && callback)
return hook_message (plugin, message, callback, data);
return NULL;
}
/*
* plugin_api_hook_config: hook a config option
*/
struct t_hook *
plugin_api_hook_config (struct t_weechat_plugin *plugin, char *config_type,
char *config_option,
int (*callback)(void *, char *, char *, char *),
void *data)
{
if (plugin && callback)
return hook_config (plugin, config_type, config_option,
callback, data);
return NULL;
}
/*
* plugin_api_hook_timer: hook a timer
*/
struct t_hook *
plugin_api_hook_timer (struct t_weechat_plugin *plugin, long interval,
int max_calls, int (*callback)(void *), void *data)
{
if (plugin && (interval > 0) && callback)
return hook_timer (plugin, interval, max_calls, callback, data);
return NULL;
}
/*
* plugin_api_hook_fd: hook a file descriptor
*/
struct t_hook *
plugin_api_hook_fd (struct t_weechat_plugin *plugin, int fd,
int flag_read, int flag_write, int flag_exception,
int (*callback)(void *), void *data)
{
int flags;
if (plugin && (fd >= 0) && callback)
{
flags = 0;
if (flag_read)
flags |= HOOK_FD_FLAG_READ;
if (flag_write)
flags |= HOOK_FD_FLAG_WRITE;
if (flag_exception)
flags |= HOOK_FD_FLAG_EXCEPTION;
return hook_fd (plugin, fd, flags, callback, data);
}
return NULL;
}
/*
* plugin_api_unhook: unhook something
*/
void
plugin_api_unhook (struct t_weechat_plugin *plugin, void *hook)
{
if (plugin && hook
&& (hook_valid_for_plugin (plugin, (struct t_hook *)hook)))
unhook ((struct t_hook *)hook);
}
/*
* plugin_api_unhook_all: unhook all for a plugin
*/
void
plugin_api_unhook_all (struct t_weechat_plugin *plugin)
{
if (plugin)
unhook_all (plugin);
}
/*
* plugin_api_buffer_new: create a new buffer
*/
struct t_gui_buffer *
plugin_api_buffer_new (struct t_weechat_plugin *plugin, char *category,
char *name)
{
if (plugin && name && name[0])
return gui_buffer_new (plugin, category, name);
return NULL;
}
/*
* plugin_api_buffer_search: search a buffer
*/
struct t_gui_buffer *
plugin_api_buffer_search (struct t_weechat_plugin *plugin, char *category,
char *name)
{
if (plugin)
return gui_buffer_search_by_category_name (category, name);
return NULL;
}
/*
* plugin_api_buffer_close: close a buffer
*/
void
plugin_api_buffer_close (struct t_weechat_plugin *plugin, void *buffer)
{
if (plugin && buffer
&& gui_buffer_valid ((struct t_gui_buffer *)buffer))
gui_buffer_free ((struct t_gui_buffer *)buffer, 1);
}
/*
* plugin_api_buffer_set: set a buffer property
*/
void
plugin_api_buffer_set (struct t_weechat_plugin *plugin, void *buffer,
char *property, char *value)
{
long number;
char *error;
if (plugin && buffer && property && property[0])
{
if (string_strcasecmp (property, "display") == 0)
{
gui_window_switch_to_buffer (gui_current_window,
(struct t_gui_buffer *)buffer);
gui_window_redraw_buffer ((struct t_gui_buffer *)buffer);
}
else if (string_strcasecmp (property, "category") == 0)
{
gui_buffer_set_category ((struct t_gui_buffer *)buffer, value);
gui_status_draw ((struct t_gui_buffer *)buffer, 0);
}
else if (string_strcasecmp (property, "name") == 0)
{
gui_buffer_set_name ((struct t_gui_buffer *)buffer, value);
gui_status_draw ((struct t_gui_buffer *)buffer, 0);
}
else if (string_strcasecmp (property, "log") == 0)
{
gui_buffer_set_log ((struct t_gui_buffer *)buffer, value);
}
else if (string_strcasecmp (property, "title") == 0)
{
gui_buffer_set_title ((struct t_gui_buffer *)buffer, value);
gui_chat_draw_title ((struct t_gui_buffer *)buffer, 0);
}
else if (string_strcasecmp (property, "nick_case_sensitive") == 0)
{
error = NULL;
number = strtol (value, &error, 10);
if (error && (error[0] == '\0'))
gui_buffer_set_nick_case_sensitive ((struct t_gui_buffer *)buffer,
number);
}
else if (string_strcasecmp (property, "nick") == 0)
{
gui_buffer_set_nick ((struct t_gui_buffer *)buffer, value);
gui_input_draw ((struct t_gui_buffer *)buffer, 0);
}
}
}
/*
* plugin_api_buffer_nick_add: add a nick to a buffer nicklist
*/
void
plugin_api_buffer_nick_add (struct t_weechat_plugin *plugin, void *buffer,
char *nick, int sort_index, char *color_nick,
char prefix, char *color_prefix)
{
int num_color_nick, num_color_prefix;
struct t_gui_nick *ptr_nick;
if (plugin && buffer && gui_buffer_valid ((struct t_gui_buffer *)buffer)
&& nick && nick[0])
{
num_color_nick = gui_color_search_config (color_nick);
if (num_color_nick < 0)
num_color_nick = GUI_COLOR_NICKLIST;
num_color_prefix = gui_color_search_config (color_prefix);
if (num_color_prefix < 0)
num_color_prefix = GUI_COLOR_NICKLIST;
ptr_nick = gui_nicklist_search ((struct t_gui_buffer *)buffer, nick);
if (ptr_nick)
gui_nicklist_update ((struct t_gui_buffer *)buffer,
ptr_nick, nick, sort_index, num_color_nick,
prefix, num_color_prefix);
else
gui_nicklist_add ((struct t_gui_buffer *)buffer,
nick, sort_index, num_color_nick,
prefix, num_color_prefix);
}
}
/*
* plugin_api_buffer_nick_remove: remove a nick from a buffer nicklist
*/
void
plugin_api_buffer_nick_remove (struct t_weechat_plugin *plugin, void *buffer,
char *nick)
{
if (plugin && buffer && gui_buffer_valid ((struct t_gui_buffer *)buffer)
&& nick && nick[0])
{
if (gui_nicklist_remove ((struct t_gui_buffer *)buffer, nick))
gui_nicklist_draw ((struct t_gui_buffer *)buffer, 0);
//gui_nicklist_remove ((struct t_gui_buffer *)buffer, nick);
}
}
/*
* plugin_api_command: execute a command (simulate user entry)
*/
void
plugin_api_command (struct t_weechat_plugin *plugin, void *buffer,
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, 0);
if (command2)
free (command2);
}
/*
* plugin_api_info_get: get info about WeeChat
* WARNING: caller has to free string returned
* by this function after use
*/
char *
plugin_api_info_get (struct t_weechat_plugin *plugin, char *info)
{
//t_irc_server *ptr_server;
//t_irc_channel *ptr_channel;
time_t inactivity;
char *return_str;
if (!plugin || !info)
return NULL;
/* below are infos that do NOT need server to return info */
if (string_strcasecmp (info, "version") == 0)
{
return strdup (PACKAGE_VERSION);
}
else if (string_strcasecmp (info, "weechat_dir") == 0)
{
return strdup (weechat_home);
}
else if (string_strcasecmp (info, "weechat_libdir") == 0)
{
return strdup (WEECHAT_LIBDIR);
}
else if (string_strcasecmp (info, "weechat_sharedir") == 0)
{
return strdup (WEECHAT_SHAREDIR);
}
else if (string_strcasecmp (info, "charset_terminal") == 0)
{
return strdup (local_charset);
}
else if (string_strcasecmp (info, "charset_internal") == 0)
{
return strdup (WEECHAT_INTERNAL_CHARSET);
}
else if (string_strcasecmp (info, "inactivity") == 0)
{
if (gui_keyboard_last_activity_time == 0)
inactivity = 0;
else
inactivity = time (NULL) - gui_keyboard_last_activity_time;
return_str = (char *) malloc (32);
if (!return_str)
return NULL;
snprintf (return_str, 32, "%ld", (long int)inactivity);
return return_str;
}
else if (string_strcasecmp (info, "input") == 0)
{
if (gui_current_window->buffer->input)
{
return_str = string_iconv_from_internal (plugin->charset,
gui_current_window->buffer->input_buffer);
return (return_str) ? return_str : strdup ("");
}
else
return strdup ("");
}
else if (string_strcasecmp (info, "input_mask") == 0)
{
if (gui_current_window->buffer->input)
return strdup (gui_current_window->buffer->input_buffer_color_mask);
else
return strdup ("");
}
else if (string_strcasecmp (info, "input_pos") == 0)
{
if (gui_current_window->buffer->input)
{
return_str = (char *) malloc (32);
if (!return_str)
return NULL;
snprintf (return_str, 32, "%d",
gui_current_window->buffer->input_buffer_pos);
return return_str;
}
else
return strdup ("");
}
/* below are infos that need server to return value */
/*plugin_find_server_channel (server, NULL, &ptr_server, &ptr_channel);
if (string_strcasecmp (info, "nick") == 0)
{
if (ptr_server && ptr_server->is_connected && ptr_server->nick)
return strdup (ptr_server->nick);
}
else if (string_strcasecmp (info, "channel") == 0)
{
if (GUI_BUFFER_IS_CHANNEL(gui_current_window->buffer)
|| GUI_BUFFER_IS_PRIVATE(gui_current_window->buffer))
return strdup (GUI_CHANNEL(gui_current_window->buffer)->name);
}
else if (string_strcasecmp (info, "server") == 0)
{
if (ptr_server && ptr_server->is_connected && ptr_server->name)
return strdup (ptr_server->name);
}
else if (string_strcasecmp (info, "type") == 0)
{
return_str = (char *) malloc (32);
if (!return_str)
return NULL;
snprintf (return_str, 32, "%d",
gui_current_window->buffer->type);
return return_str;
}
else if (string_strcasecmp (info, "away") == 0)
{
if (ptr_server && ptr_server->is_connected && ptr_server->is_away)
return strdup ("1");
else
return strdup ("0");
}*/
/* info not found */
return NULL;
}
/*
* plugin_api_list_get_add_buffer: add a buffer in a list
* return 1 if ok, 0 if error
*/
int
plugin_api_list_get_add_buffer (struct t_plugin_list *list,
struct t_gui_buffer *buffer)
{
struct t_plugin_list_item *ptr_item;
if (!list || !buffer)
return 0;
ptr_item = plugin_list_new_item (list);
if (!ptr_item)
return 0;
if (!plugin_list_new_var_pointer (ptr_item, "pointer", buffer))
return 0;
if (!plugin_list_new_var_int (ptr_item, "number", buffer->number))
return 0;
if (!plugin_list_new_var_string (ptr_item, "category", buffer->category))
return 0;
if (!plugin_list_new_var_string (ptr_item, "name", buffer->name))
return 0;
if (!plugin_list_new_var_int (ptr_item, "type", buffer->type))
return 0;
if (!plugin_list_new_var_int (ptr_item, "notify_level", buffer->notify_level))
return 0;
if (!plugin_list_new_var_int (ptr_item, "num_displayed", buffer->num_displayed))
return 0;
if (!plugin_list_new_var_string (ptr_item, "log_filename", buffer->log_filename))
return 0;
if (!plugin_list_new_var_string (ptr_item, "title", buffer->title))
return 0;
if (!plugin_list_new_var_int (ptr_item, "input", buffer->input))
return 0;
if (!plugin_list_new_var_string (ptr_item, "input_nick", buffer->input_nick))
return 0;
if (!plugin_list_new_var_string (ptr_item, "input_string", buffer->input_buffer))
return 0;
return 1;
}
/*
* plugin_api_list_get_add_buffer_line: add a buffer line in a list
* return 1 if ok, 0 if error
*/
int
plugin_api_list_get_add_buffer_line (struct t_plugin_list *list,
struct t_gui_line *line)
{
struct t_plugin_list_item *ptr_item;
if (!list || !line)
return 0;
ptr_item = plugin_list_new_item (list);
if (!ptr_item)
return 0;
if (!plugin_list_new_var_time (ptr_item, "date", line->date))
return 0;
if (!plugin_list_new_var_string (ptr_item, "str_time", line->str_time))
return 0;
if (!plugin_list_new_var_string (ptr_item, "prefix", line->prefix))
return 0;
if (!plugin_list_new_var_string (ptr_item, "message", line->message))
return 0;
return 1;
}
/*
* plugin_api_list_get: get list with infos about WeeChat structures
* WARNING: caller has to free string returned
* by this function after use, with weechat_list_free()
*/
struct t_plugin_list *
plugin_api_list_get (struct t_weechat_plugin *plugin, char *name,
void *pointer)
{
struct t_plugin_list *ptr_list;
struct t_gui_buffer *ptr_buffer;
struct t_gui_line *ptr_line;
if (!plugin || !name || !name[0])
return NULL;
if (string_strcasecmp (name, "buffer") == 0)
{
/* invalid buffer pointer ? */
if (pointer && (!gui_buffer_valid ((struct t_gui_buffer *)pointer)))
return NULL;
ptr_list = plugin_list_new ();
if (ptr_list)
{
if (pointer)
{
/* build list with only one buffer */
if (!plugin_api_list_get_add_buffer (ptr_list,
(struct t_gui_buffer *)pointer))
{
plugin_list_free (ptr_list);
return NULL;
}
return ptr_list;
}
else
{
/* build list with all buffers */
for (ptr_buffer = gui_buffers; ptr_buffer;
ptr_buffer = ptr_buffer->next_buffer)
{
if (!plugin_api_list_get_add_buffer (ptr_list,
ptr_buffer))
{
plugin_list_free (ptr_list);
return NULL;
}
}
return ptr_list;
}
}
}
else if (string_strcasecmp (name, "buffer_lines") == 0)
{
/* buffer pointer is mandatory for this list */
if (!pointer)
return NULL;
/* invalid buffer pointer ? */
if (!gui_buffer_valid ((struct t_gui_buffer *)pointer))
return NULL;
ptr_list = plugin_list_new ();
if (ptr_list)
{
for (ptr_line = ((struct t_gui_buffer *)pointer)->lines; ptr_line;
ptr_line = ptr_line->next_line)
{
if (!plugin_api_list_get_add_buffer_line (ptr_list,
ptr_line))
{
plugin_list_free (ptr_list);
return NULL;
}
}
return ptr_list;
}
}
/* list not found */
return NULL;
}
/*
* plugin_api_list_next: move item pointer to next item in a list
* return 1 if pointer is still ok
* 0 if end of list was reached
*/
int
plugin_api_list_next (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return 0;
return (plugin_list_next_item ((struct t_plugin_list *)list)) ? 1 : 0;
}
/*
* plugin_api_list_prev: move item pointer to previous item in a list
* return 1 if pointer is still ok
* 0 if beginning of list was reached
*/
int
plugin_api_list_prev (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return 0;
return (plugin_list_prev_item ((struct t_plugin_list *)list)) ? 1 : 0;
}
/*
* plugin_api_list_fields: get list of fields for current list item
*/
char *
plugin_api_list_fields (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return NULL;
return plugin_list_get_fields ((struct t_plugin_list *)list);
}
/*
* plugin_api_list_int: get an integer variable value in current list item
*/
int
plugin_api_list_int (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return 0;
return plugin_list_get_int ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_string: get a string variable value in current list item
*/
char *
plugin_api_list_string (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return NULL;
return plugin_list_get_string ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_pointer: get a pointer variable value in current list item
*/
void *
plugin_api_list_pointer (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return NULL;
return plugin_list_get_pointer ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_time: get a time variable value in current list item
*/
time_t
plugin_api_list_time (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return 0;
return plugin_list_get_time ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_free: free a list
*/
void
plugin_api_list_free (struct t_weechat_plugin *plugin, void *list)
{
if (plugin && list && plugin_list_valid ((struct t_plugin_list *)list))
plugin_list_free ((struct t_plugin_list *)list);
}
/*
* plugin_api_get_config_str_value: return string value for any option
* This function should never be called directly
* (only used by weechat_get_config)
*/
char *
plugin_api_get_config_str_value (struct t_config_option *option)
{
char buf_temp[1024], *color_name;
switch (option->type)
{
case OPTION_TYPE_BOOLEAN:
return (*((int *)(option->ptr_int))) ?
strdup ("on") : strdup ("off");
break;
case OPTION_TYPE_INT:
snprintf (buf_temp, sizeof (buf_temp), "%d",
*((int *)(option->ptr_int)));
return strdup (buf_temp);
break;
case OPTION_TYPE_INT_WITH_STRING:
return strdup (option->array_values[*((int *)(option->ptr_int))]);
break;
case OPTION_TYPE_STRING:
return (*((char **)(option->ptr_string))) ?
strdup (*((char **)(option->ptr_string))) : strdup ("");
break;
case OPTION_TYPE_COLOR:
color_name = gui_color_get_name (*((int *)(option->ptr_int)));
return (color_name) ? strdup (color_name) : strdup ("");
break;
}
/* should never be executed! */
return NULL;
}
/*
* plugin_api_config_get: get value of a config option
*/
char *
plugin_api_config_get (struct t_weechat_plugin *plugin, char *option_name)
{
struct t_config_option *ptr_option;
/* make C compiler happy */
(void) plugin;
/* search a WeeChat command */
ptr_option = config_option_section_option_search (weechat_config_sections,
weechat_config_options,
option_name);
if (ptr_option)
return plugin_api_get_config_str_value (ptr_option);
/* option not found */
return NULL;
}
/*
* plugin_api_config_set: set value of a config option
*/
int
plugin_api_config_set (struct t_weechat_plugin *plugin, char *option_name,
char *value)
{
struct t_config_option *ptr_option;
/* make C compiler happy */
(void) plugin;
if (!option_name || !value)
return 0;
/* search and set WeeChat option if found */
ptr_option = config_option_section_option_search (weechat_config_sections,
weechat_config_options,
option_name);
if (ptr_option)
{
if (ptr_option->handler_change)
{
if (config_option_set (ptr_option, value) == 0)
{
(void) (ptr_option->handler_change());
return 1;
}
}
else
return 0;
}
/* failed to set config option */
return 0;
}
/*
* plugin_api_plugin_config_get: get value of a plugin config option
*/
char *
plugin_api_plugin_config_get (struct t_weechat_plugin *plugin, char *option)
{
struct t_plugin_option *ptr_plugin_option;
if (!option)
return NULL;
ptr_plugin_option = plugin_config_search (plugin->name, option);
if (ptr_plugin_option)
return (ptr_plugin_option->value) ? strdup (ptr_plugin_option->value) : NULL;
/* option not found */
return NULL;
}
/*
* plugin_api_plugin_config_set: set value of a plugin config option
*/
int
plugin_api_plugin_config_set (struct t_weechat_plugin *plugin, char *option,
char *value)
{
if (!option)
return 0;
if (plugin_config_set (plugin->name, option, value))
{
plugin_config_write ();
return 1;
}
return 0;
}
/*
* plugin_api_log: add a message in buffer log file
*/
void
plugin_api_log (struct t_weechat_plugin *plugin,
char *server, char *channel, char *message, ...)
{
(void) plugin;
(void) server;
(void) channel;
(void) message;
/*t_gui_buffer *ptr_buffer;
va_list argptr;
static char buf[8192];
char *buf2;
if (!plugin || !message)
return;
ptr_buffer = gui_buffer_search (server, channel);
if (ptr_buffer)
{
va_start (argptr, message);
vsnprintf (buf, sizeof (buf) - 1, message, argptr);
va_end (argptr);
buf2 = string_iconv_to_internal (plugin->charset, buf);
gui_log_write_line (ptr_buffer, (buf2) ? buf2 : buf);
if (buf2)
free (buf2);
}*/
}
|