47
|
1 /*
|
|
2 * commands.c -- user commands handling
|
|
3 *
|
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or (at
|
|
9 * your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
19 * USA
|
|
20 */
|
|
21
|
95
|
22 #include <string.h>
|
|
23
|
47
|
24 #include "commands.h"
|
|
25 #include "jabglue.h"
|
81
|
26 #include "roster.h"
|
47
|
27 #include "screen.h"
|
95
|
28 #include "compl.h"
|
113
|
29 #include "hooks.h"
|
191
|
30 #include "hbuf.h"
|
81
|
31 #include "utf8.h"
|
47
|
32 #include "utils.h"
|
|
33
|
104
|
34 // Commands callbacks
|
|
35 void do_roster(char *arg);
|
116
|
36 void do_status(char *arg);
|
124
|
37 void do_add(char *arg);
|
205
|
38 void do_del(char *arg);
|
128
|
39 void do_group(char *arg);
|
132
|
40 void do_say(char *arg);
|
187
|
41 void do_buffer(char *arg);
|
|
42 void do_clear(char *arg);
|
191
|
43 void do_info(char *arg);
|
208
|
44 void do_rename(char *arg);
|
210
|
45 void do_move(char *arg);
|
47
|
46
|
104
|
47 // Global variable for the commands list
|
95
|
48 static GSList *Commands;
|
|
49
|
104
|
50
|
95
|
51 // cmd_add()
|
|
52 // Adds a command to the commands list and to the CMD completion list
|
|
53 void cmd_add(const char *name, const char *help,
|
104
|
54 guint flags_row1, guint flags_row2, void (*f)())
|
95
|
55 {
|
|
56 cmd *n_cmd = g_new0(cmd, 1);
|
|
57 strncpy(n_cmd->name, name, 32-1);
|
|
58 n_cmd->help = help;
|
|
59 n_cmd->completion_flags[0] = flags_row1;
|
|
60 n_cmd->completion_flags[1] = flags_row2;
|
|
61 n_cmd->func = f;
|
102
|
62 Commands = g_slist_append(Commands, n_cmd);
|
95
|
63 // Add to completion CMD category
|
|
64 compl_add_category_word(COMPL_CMD, name);
|
|
65 }
|
|
66
|
|
67 // cmd_init()
|
|
68 // ...
|
|
69 void cmd_init(void)
|
|
70 {
|
124
|
71 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add);
|
187
|
72 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)",
|
|
73 COMPL_BUFFER, 0, &do_buffer);
|
108
|
74 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear);
|
205
|
75 cmd_add("del", "Delete the current buddy", 0, 0, &do_del);
|
128
|
76 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group);
|
127
|
77 cmd_add("help", "Display some help", COMPL_CMD, 0, NULL);
|
191
|
78 cmd_add("info", "Show basic infos on current buddy", 0, 0, &do_info);
|
210
|
79 cmd_add("move", "Move the current buddy to another group", 0, 0, &do_move);
|
95
|
80 //cmd_add("nick");
|
|
81 cmd_add("quit", "Exit the software", 0, 0, NULL);
|
208
|
82 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename);
|
95
|
83 //cmd_add("request_auth");
|
128
|
84 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0,
|
|
85 &do_roster);
|
132
|
86 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say);
|
95
|
87 //cmd_add("search");
|
|
88 //cmd_add("send_auth");
|
116
|
89 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status);
|
95
|
90
|
|
91 // Status category
|
|
92 compl_add_category_word(COMPL_STATUS, "online");
|
|
93 compl_add_category_word(COMPL_STATUS, "avail");
|
|
94 compl_add_category_word(COMPL_STATUS, "invisible");
|
|
95 compl_add_category_word(COMPL_STATUS, "free");
|
|
96 compl_add_category_word(COMPL_STATUS, "dnd");
|
|
97 compl_add_category_word(COMPL_STATUS, "busy");
|
|
98 compl_add_category_word(COMPL_STATUS, "notavail");
|
|
99 compl_add_category_word(COMPL_STATUS, "away");
|
103
|
100
|
|
101 // Roster category
|
|
102 compl_add_category_word(COMPL_ROSTER, "bottom");
|
|
103 compl_add_category_word(COMPL_ROSTER, "hide_offline");
|
|
104 compl_add_category_word(COMPL_ROSTER, "show_offline");
|
|
105 compl_add_category_word(COMPL_ROSTER, "top");
|
127
|
106
|
187
|
107 // Roster category
|
|
108 compl_add_category_word(COMPL_BUFFER, "bottom");
|
|
109 compl_add_category_word(COMPL_BUFFER, "clear");
|
|
110 compl_add_category_word(COMPL_BUFFER, "top");
|
|
111
|
127
|
112 // Group category
|
|
113 compl_add_category_word(COMPL_GROUP, "expand");
|
|
114 compl_add_category_word(COMPL_GROUP, "shrink");
|
130
|
115 compl_add_category_word(COMPL_GROUP, "toggle");
|
95
|
116 }
|
94
|
117
|
102
|
118 // cmd_get
|
|
119 // Finds command in the command list structure.
|
|
120 // Returns a pointer to the cmd entry, or NULL if command not found.
|
|
121 cmd *cmd_get(char *command)
|
|
122 {
|
|
123 char *p1, *p2;
|
|
124 char *com;
|
|
125 GSList *sl_com;
|
|
126 // Ignore leading '/'
|
|
127 for (p1 = command ; *p1 == '/' ; p1++)
|
|
128 ;
|
|
129 // Locate the end of the command
|
|
130 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
|
|
131 ;
|
|
132 // Copy the clean command
|
|
133 com = g_strndup(p1, p2-p1);
|
|
134
|
|
135 // Look for command in the list
|
|
136 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
|
|
137 if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
|
|
138 break;
|
|
139 }
|
|
140 g_free(com);
|
|
141
|
|
142 if (sl_com) // Command has been found.
|
|
143 return (cmd*)sl_com->data;
|
|
144 return NULL;
|
|
145 }
|
|
146
|
47
|
147 // send_message(msg)
|
|
148 // Write the message in the buddy's window and send the message on
|
|
149 // the network.
|
|
150 void send_message(char *msg)
|
|
151 {
|
|
152 char *buffer;
|
81
|
153 const char *jid;
|
|
154
|
|
155 if (!current_buddy) {
|
|
156 scr_LogPrint("No buddy currently selected.");
|
|
157 return;
|
|
158 }
|
|
159
|
|
160 jid = CURRENT_JID;
|
|
161 if (!jid) {
|
|
162 scr_LogPrint("No buddy currently selected.");
|
|
163 return;
|
|
164 }
|
47
|
165
|
113
|
166 // local part (UI, logging, etc.)
|
|
167 hk_message_out(jid, 0, msg);
|
47
|
168
|
|
169 // Network part
|
|
170 buffer = utf8_encode(msg);
|
81
|
171 jb_send_msg(jid, buffer);
|
47
|
172 free(buffer);
|
|
173 }
|
|
174
|
|
175 // process_line(line)
|
|
176 // Process a command/message line.
|
|
177 // If this isn't a command, this is a message and it is sent to the
|
|
178 // currently selected buddy.
|
|
179 int process_line(char *line)
|
|
180 {
|
101
|
181 char *p;
|
104
|
182 cmd *curcmd;
|
|
183
|
132
|
184 if (!*line) { // User only pressed enter
|
129
|
185 if (current_buddy) {
|
132
|
186 scr_set_chatmode(TRUE);
|
129
|
187 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
132
|
188 scr_ShowBuddyWindow();
|
|
189 }
|
|
190 return 0;
|
|
191 }
|
129
|
192
|
132
|
193 if (*line != '/') {
|
|
194 do_say(line);
|
47
|
195 return 0;
|
|
196 }
|
101
|
197
|
|
198 /* It is a command */
|
|
199 // Remove trailing spaces:
|
|
200 for (p=line ; *p ; p++)
|
|
201 ;
|
|
202 for (p-- ; p>line && (*p == ' ') ; p--)
|
|
203 *p = 0;
|
|
204
|
|
205 // Command "quit"?
|
116
|
206 if (!strncasecmp(line, "/quit", 5))
|
|
207 if (!line[5] || line[5] == ' ')
|
|
208 return 255;
|
104
|
209
|
47
|
210 // Commands handling
|
104
|
211 curcmd = cmd_get(line);
|
47
|
212
|
104
|
213 if (!curcmd) {
|
|
214 scr_LogPrint("Unrecognized command, sorry.");
|
|
215 return 0;
|
|
216 }
|
|
217 if (!curcmd->func) {
|
|
218 scr_LogPrint("Not yet implemented, sorry.");
|
|
219 return 0;
|
|
220 }
|
|
221 // Lets go to the command parameters
|
|
222 for (line++; *line && (*line != ' ') ; line++)
|
|
223 ;
|
|
224 // Skip spaces
|
|
225 while (*line && (*line == ' '))
|
|
226 line++;
|
|
227 // Call command-specific function
|
|
228 (*curcmd->func)(line);
|
47
|
229 return 0;
|
|
230 }
|
|
231
|
104
|
232 /* Commands callback functions */
|
|
233
|
|
234 void do_roster(char *arg)
|
|
235 {
|
|
236 if (!strcasecmp(arg, "top")) {
|
|
237 scr_RosterTop();
|
128
|
238 update_roster = TRUE;
|
104
|
239 } else if (!strcasecmp(arg, "bottom")) {
|
|
240 scr_RosterBottom();
|
128
|
241 update_roster = TRUE;
|
104
|
242 } else if (!strcasecmp(arg, "hide_offline")) {
|
120
|
243 buddylist_set_hide_offline_buddies(TRUE);
|
104
|
244 if (current_buddy)
|
|
245 buddylist_build();
|
128
|
246 update_roster = TRUE;
|
104
|
247 } else if (!strcasecmp(arg, "show_offline")) {
|
120
|
248 buddylist_set_hide_offline_buddies(FALSE);
|
124
|
249 buddylist_build();
|
128
|
250 update_roster = TRUE;
|
104
|
251 } else
|
|
252 scr_LogPrint("Unrecognized parameter!");
|
|
253 }
|
108
|
254
|
116
|
255 void do_status(char *arg)
|
|
256 {
|
|
257 enum imstatus st;
|
|
258
|
|
259 if (!arg || (*arg == 0)) {
|
|
260 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]);
|
|
261 return;
|
|
262 }
|
|
263
|
130
|
264 if (!strcasecmp(arg, "offline")) st = offline;
|
|
265 else if (!strcasecmp(arg, "online")) st = available;
|
|
266 else if (!strcasecmp(arg, "avail")) st = available;
|
|
267 else if (!strcasecmp(arg, "away")) st = away;
|
|
268 else if (!strcasecmp(arg, "invisible")) st = invisible;
|
|
269 else if (!strcasecmp(arg, "dnd")) st = dontdisturb;
|
|
270 else if (!strcasecmp(arg, "busy")) st = occupied;
|
|
271 else if (!strcasecmp(arg, "notavail")) st = notavail;
|
|
272 else if (!strcasecmp(arg, "free")) st = freeforchat;
|
116
|
273 else {
|
|
274 scr_LogPrint("Unrecognized parameter!");
|
|
275 return;
|
|
276 }
|
|
277
|
|
278 // XXX special case if offline??
|
|
279 jb_setstatus(st, NULL); // TODO handle message (instead of NULL)
|
|
280 }
|
|
281
|
124
|
282 void do_add(char *arg)
|
|
283 {
|
|
284 if (!arg || (*arg == 0)) {
|
|
285 scr_LogPrint("Wrong usage");
|
|
286 return;
|
|
287 }
|
|
288
|
|
289 // FIXME check arg =~ jabber id
|
|
290 // 2nd parameter = optional nickname (XXX NULL for now...)
|
|
291 jb_addbuddy(arg, NULL);
|
|
292 }
|
128
|
293
|
206
|
294 void do_del(char *arg)
|
|
295 {
|
|
296 const char *jid;
|
|
297
|
|
298 if (arg && (*arg)) {
|
|
299 scr_LogPrint("Wrong usage");
|
|
300 return;
|
|
301 }
|
|
302
|
|
303 if (!current_buddy) return;
|
|
304 jid = buddy_getjid(BUDDATA(current_buddy));
|
|
305 if (!jid) return;
|
|
306
|
|
307 scr_LogPrint("Removing <%s>...", jid);
|
|
308 jb_delbuddy(jid);
|
|
309 }
|
|
310
|
128
|
311 void do_group(char *arg)
|
|
312 {
|
|
313 gpointer group;
|
166
|
314 guint leave_windowbuddy;
|
128
|
315
|
|
316 if (!arg || (*arg == 0)) {
|
|
317 scr_LogPrint("Missing parameter");
|
|
318 return;
|
|
319 }
|
|
320
|
166
|
321 if (!current_buddy) return;
|
128
|
322
|
166
|
323 group = buddy_getgroup(BUDDATA(current_buddy));
|
212
|
324 // We'll have to redraw the chat window if we're not currently on the group
|
|
325 // entry itself, because it means we'll have to leave the current buddy
|
|
326 // chat window.
|
166
|
327 leave_windowbuddy = (group != BUDDATA(current_buddy));
|
|
328
|
128
|
329 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) {
|
166
|
330 scr_LogPrint("You need to select a group");
|
128
|
331 return;
|
|
332 }
|
166
|
333
|
|
334 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) {
|
128
|
335 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE);
|
166
|
336 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) {
|
128
|
337 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE);
|
130
|
338 } else if (!strcasecmp(arg, "toggle")) {
|
|
339 buddy_setflags(group, ROSTER_FLAG_HIDE,
|
|
340 !(buddy_getflags(group) & ROSTER_FLAG_HIDE));
|
128
|
341 } else {
|
|
342 scr_LogPrint("Unrecognized parameter!");
|
|
343 return;
|
|
344 }
|
|
345
|
|
346 buddylist_build();
|
|
347 update_roster = TRUE;
|
166
|
348 if (leave_windowbuddy) scr_ShowBuddyWindow();
|
128
|
349 }
|
|
350
|
132
|
351 void do_say(char *arg)
|
|
352 {
|
164
|
353 gpointer bud;
|
132
|
354
|
|
355 scr_set_chatmode(TRUE);
|
164
|
356
|
|
357 if (!current_buddy) {
|
132
|
358 scr_LogPrint("Who are you talking to??");
|
164
|
359 return;
|
132
|
360 }
|
164
|
361
|
|
362 bud = BUDDATA(current_buddy);
|
|
363 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) {
|
|
364 scr_LogPrint("This is not a user");
|
|
365 return;
|
|
366 }
|
|
367
|
|
368 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
|
|
369 send_message(arg);
|
132
|
370 }
|
|
371
|
187
|
372 void do_buffer(char *arg)
|
|
373 {
|
|
374 if (!strcasecmp(arg, "top")) {
|
|
375 scr_BufferTop();
|
|
376 } else if (!strcasecmp(arg, "bottom")) {
|
|
377 scr_BufferBottom();
|
|
378 } else if (!strcasecmp(arg, "clear")) {
|
|
379 scr_Clear();
|
|
380 } else
|
|
381 scr_LogPrint("Unrecognized parameter!");
|
|
382 }
|
|
383
|
|
384 void do_clear(char *arg) // Alias for "/buffer clear"
|
|
385 {
|
|
386 do_buffer("clear");
|
|
387 }
|
|
388
|
191
|
389 void do_info(char *arg)
|
|
390 {
|
|
391 gpointer bud;
|
|
392 const char *jid, *name;
|
|
393 guint type;
|
|
394 enum imstatus status;
|
|
395 char *buffer;
|
|
396
|
|
397 if (!current_buddy) return;
|
|
398 bud = BUDDATA(current_buddy);
|
|
399
|
|
400 jid = buddy_getjid(bud);
|
|
401 name = buddy_getname(bud);
|
|
402 type = buddy_gettype(bud);
|
|
403 status = buddy_getstatus(bud);
|
|
404
|
|
405 buffer = g_new(char, 128);
|
|
406
|
|
407 if (jid) {
|
|
408 char *typestr = "unknown";
|
|
409
|
|
410 snprintf(buffer, 127, "jid: <%s>", jid);
|
|
411 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
412 if (name) {
|
|
413 snprintf(buffer, 127, "Name: %s", name);
|
|
414 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
415 }
|
|
416
|
|
417 if (type == ROSTER_TYPE_USER) typestr = "user";
|
|
418 else if (type == ROSTER_TYPE_AGENT) typestr = "agent";
|
|
419
|
|
420 snprintf(buffer, 127, "Type: %s", typestr);
|
|
421 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
422 } else {
|
|
423 if (name) scr_LogPrint("Name: %s", name);
|
|
424 scr_LogPrint("Type: %s",
|
|
425 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown"));
|
|
426 }
|
|
427
|
|
428 g_free(buffer);
|
|
429 }
|
|
430
|
208
|
431 void do_rename(char *arg)
|
|
432 {
|
|
433 gpointer bud;
|
|
434 const char *jid, *group;
|
|
435 guint type;
|
|
436 char *newname, *p;
|
|
437
|
|
438 if (!arg || (*arg == 0)) {
|
|
439 scr_LogPrint("Missing parameter");
|
|
440 return;
|
|
441 }
|
|
442
|
|
443 if (!current_buddy) return;
|
|
444 bud = BUDDATA(current_buddy);
|
|
445
|
|
446 jid = buddy_getjid(bud);
|
|
447 group = buddy_getgroupname(bud);
|
|
448 type = buddy_gettype(bud);
|
|
449
|
|
450 if (type & ROSTER_TYPE_GROUP) {
|
|
451 scr_LogPrint("You can't rename groups");
|
|
452 return;
|
|
453 }
|
|
454
|
|
455 newname = g_strdup(arg);
|
|
456 // Remove trailing space
|
|
457 for (p = newname; *p; p++) ;
|
|
458 while (p > newname && *p == ' ') *p = 0;
|
|
459
|
|
460 buddy_setname(bud, newname);
|
|
461 jb_updatebuddy(jid, newname, group);
|
|
462
|
|
463 g_free(newname);
|
|
464 update_roster = TRUE;
|
|
465 }
|
|
466
|
210
|
467 void do_move(char *arg)
|
|
468 {
|
|
469 gpointer bud;
|
|
470 const char *jid, *name;
|
|
471 guint type;
|
|
472 char *newgroupname, *p;
|
|
473
|
|
474 if (!current_buddy) return;
|
|
475 bud = BUDDATA(current_buddy);
|
|
476
|
|
477 jid = buddy_getjid(bud);
|
|
478 name = buddy_getname(bud);
|
|
479 type = buddy_gettype(bud);
|
|
480
|
|
481 if (type & ROSTER_TYPE_GROUP) {
|
|
482 scr_LogPrint("You can't move groups!");
|
|
483 return;
|
|
484 }
|
|
485
|
|
486 newgroupname = g_strdup(arg);
|
|
487 // Remove trailing space
|
|
488 for (p = newgroupname; *p; p++) ;
|
|
489 while (p > newgroupname && *p == ' ') *p = 0;
|
|
490
|
|
491 // Call to buddy_setgroup() should be at the end, as current implementation
|
|
492 // clones the buddy and deletes the old one (and thus, jid and name are
|
|
493 // freed)
|
|
494 jb_updatebuddy(jid, name, newgroupname);
|
|
495 buddy_setgroup(bud, newgroupname);
|
|
496
|
|
497 g_free(newgroupname);
|
|
498 update_roster = TRUE;
|
|
499 }
|
|
500
|