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
|
/*
* 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/>.
*/
/* demo.c: demo plugin for WeeChat */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include "../weechat-plugin.h"
char plugin_name[] = "demo";
char plugin_version[] = "0.1";
char plugin_description[] = "Demo plugin for WeeChat";
struct t_weechat_plugin *weechat_demo_plugin = NULL;
#define weechat_plugin weechat_demo_plugin
/*
* demo_printf_command_cb: demo command for printf
*/
int
demo_printf_command_cb (void *data, void *buffer, int argc, char **argv,
char **argv_eol)
{
/* make C compiler happy */
(void) data;
(void) argv;
if (argc > 1)
weechat_printf (buffer,
"demo_printf: '%s'", argv_eol[1]);
else
{
weechat_printf (buffer,
_("demo message without prefix"));
weechat_printf (buffer,
_("%sdemo message with info prefix"),
weechat_prefix ("info"));
weechat_printf (buffer,
_("%sdemo message with error prefix"),
weechat_prefix ("error"));
weechat_printf (buffer,
_("colors: %s buffer %s nick1 %s nick2 %s nick3 "
"%s nick4"),
weechat_color ("color_chat_buffer"),
weechat_color ("color_chat_nick_color1"),
weechat_color ("color_chat_nick_color2"),
weechat_color ("color_chat_nick_color3"),
weechat_color ("color_chat_nick_color4"));
}
return WEECHAT_RC_OK;
}
/*
* demo_buffer_input_data_cb: callback for input data on buffer
*/
void
demo_buffer_input_data_cb (struct t_gui_buffer *buffer, char *data)
{
weechat_printf (buffer, "buffer input_data_cb: data = '%s'", data);
}
/*
* demo_buffer_command_cb: demo command for creatig new buffer
*/
int
demo_buffer_command_cb (void *data, void *buffer, int argc, char **argv,
char **argv_eol)
{
struct t_gui_buffer *new_buffer;
/* make C compiler happy */
(void) data;
(void) buffer;
(void) argv_eol;
if (argc > 2)
{
new_buffer = weechat_buffer_new (argv[1], argv[2],
demo_buffer_input_data_cb);
if (new_buffer)
weechat_buffer_set (new_buffer, "display", "1");
}
return WEECHAT_RC_OK;
}
/*
* demo_infolist_print: display an infolist
*/
void
demo_infolist_print (void *infolist, char *item_name)
{
char *fields, **argv;
int i, j, argc;
time_t date;
i = 1;
while (weechat_infolist_next (infolist))
{
weechat_printf (NULL, "--- %s #%d ---", item_name, i);
fields = weechat_infolist_fields (infolist);
if (fields)
{
argv = weechat_string_explode (fields, ",", 0, 0, &argc);
if (argv && (argc > 0))
{
for (j = 0; j < argc; j++)
{
switch (argv[j][0])
{
case 'i':
weechat_printf (NULL, " %s: %d",
argv[j] + 2,
weechat_infolist_integer (infolist,
argv[j] + 2));
break;
case 's':
weechat_printf (NULL, " %s: %s",
argv[j] + 2,
weechat_infolist_string (infolist,
argv[j] + 2));
break;
case 'p':
weechat_printf (NULL, " %s: %X",
argv[j] + 2,
weechat_infolist_pointer (infolist,
argv[j] + 2));
break;
case 't':
date = weechat_infolist_time (infolist, argv[j] + 2);
weechat_printf (NULL, " %s: (%ld) %s",
argv[j] + 2,
date, ctime (&date));
break;
}
}
}
if (argv)
weechat_string_free_exploded (argv);
}
i++;
}
}
/*
* demo_infolist_command_cb: demo command for list
*/
int
demo_infolist_command_cb (void *data, void *buffer, int argc, char **argv,
char **argv_eol)
{
struct t_plugin_infolist *infolist;
/* make C compiler happy */
(void) data;
(void) buffer;
(void) argv_eol;
if (argc > 1)
{
if (weechat_strcasecmp (argv[1], "buffer") == 0)
{
infolist = weechat_infolist_get ("buffer", NULL);
if (infolist)
{
demo_infolist_print (infolist, "buffer");
weechat_infolist_free (infolist);
}
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "buffer_lines") == 0)
{
infolist = weechat_infolist_get ("buffer_lines", NULL);
if (infolist)
{
demo_infolist_print (infolist, "buffer_line");
weechat_infolist_free (infolist);
}
return WEECHAT_RC_OK;
}
}
weechat_printf (NULL,
_("%sDemo: missing argument for /%s command "
"(try /help %s)"),
weechat_prefix ("error"),
"demo_infolist", "demo_infolist");
return WEECHAT_RC_OK;
}
/*
* demo_info_command_cb: demo command for info_get
*/
int
demo_info_command_cb (void *data, void *buffer, int argc, char **argv,
char **argv_eol)
{
/* make C compiler happy */
(void) data;
(void) buffer;
(void) argv_eol;
if (argc > 1)
weechat_printf (NULL, "%sinfo \"%s\" = \"%s\"",
weechat_prefix ("info"),
argv[1],
weechat_info_get (argv[1]));
else
weechat_printf (NULL,
_("%sDemo: missing argument for /%s command "
"(try /help %s)"),
weechat_prefix ("error"),
"demo_info", "demo_info");
return WEECHAT_RC_OK;
}
/*
* demo_event_cb: callback for event hook
*/
int
demo_event_cb (void *data, char *event, void *pointer)
{
/* make C compiler happy */
(void) data;
weechat_printf (NULL,
_("demo_event: event: %s, pointer: %X"),
event, pointer);
return WEECHAT_RC_OK;
}
/*
* weechat_plugin_init: initialize demo plugin
*/
int
weechat_plugin_init (struct t_weechat_plugin *plugin)
{
weechat_plugin = plugin;
weechat_hook_command ("demo_printf",
_("demo command: print some messages"),
_("[text]"),
_("text: write some text on current buffer"),
"",
&demo_printf_command_cb, NULL);
weechat_hook_command ("demo_buffer",
_("open a new buffer"),
_("category name"),
"",
"",
&demo_buffer_command_cb, NULL);
weechat_hook_command ("demo_infolist",
_("demo command: get and display list"),
_("infolist"),
_("infolist: infolist to display (values: buffer, "
"buffer_lines)"),
"buffer|buffer_lines",
&demo_infolist_command_cb, NULL);
weechat_hook_command ("demo_info",
_("demo command: get and display info"),
_("info"),
_("info: info to display (values: version, "
"weechat_dir, weechat_libdir, weechat_sharedir, "
"charset_terminal, charset_internal, inactivity, "
"input, input_mask, input_pos)"),
"version|weechat_dir|weechat_libdir|"
"weechat_sharedir|charset_terminal|charset_internal|"
"inactivity|input|input_mask|input_pos",
&demo_info_command_cb, NULL);
weechat_hook_event ("*", &demo_event_cb, NULL);
return WEECHAT_RC_OK;
}
/*
* weechat_plugin_end: end demo plugin
*/
int
weechat_plugin_end ()
{
return WEECHAT_RC_OK;
}
|