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);
|
225
|
79 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME,
|
|
80 0, &do_move);
|
95
|
81 //cmd_add("nick");
|
|
82 cmd_add("quit", "Exit the software", 0, 0, NULL);
|
208
|
83 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename);
|
95
|
84 //cmd_add("request_auth");
|
128
|
85 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0,
|
|
86 &do_roster);
|
132
|
87 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say);
|
95
|
88 //cmd_add("search");
|
|
89 //cmd_add("send_auth");
|
116
|
90 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status);
|
95
|
91
|
|
92 // Status category
|
|
93 compl_add_category_word(COMPL_STATUS, "online");
|
|
94 compl_add_category_word(COMPL_STATUS, "avail");
|
|
95 compl_add_category_word(COMPL_STATUS, "invisible");
|
|
96 compl_add_category_word(COMPL_STATUS, "free");
|
|
97 compl_add_category_word(COMPL_STATUS, "dnd");
|
|
98 compl_add_category_word(COMPL_STATUS, "busy");
|
|
99 compl_add_category_word(COMPL_STATUS, "notavail");
|
|
100 compl_add_category_word(COMPL_STATUS, "away");
|
103
|
101
|
|
102 // Roster category
|
|
103 compl_add_category_word(COMPL_ROSTER, "bottom");
|
|
104 compl_add_category_word(COMPL_ROSTER, "hide_offline");
|
|
105 compl_add_category_word(COMPL_ROSTER, "show_offline");
|
|
106 compl_add_category_word(COMPL_ROSTER, "top");
|
127
|
107
|
187
|
108 // Roster category
|
|
109 compl_add_category_word(COMPL_BUFFER, "bottom");
|
|
110 compl_add_category_word(COMPL_BUFFER, "clear");
|
|
111 compl_add_category_word(COMPL_BUFFER, "top");
|
|
112
|
127
|
113 // Group category
|
|
114 compl_add_category_word(COMPL_GROUP, "expand");
|
|
115 compl_add_category_word(COMPL_GROUP, "shrink");
|
130
|
116 compl_add_category_word(COMPL_GROUP, "toggle");
|
95
|
117 }
|
94
|
118
|
102
|
119 // cmd_get
|
|
120 // Finds command in the command list structure.
|
|
121 // Returns a pointer to the cmd entry, or NULL if command not found.
|
|
122 cmd *cmd_get(char *command)
|
|
123 {
|
|
124 char *p1, *p2;
|
|
125 char *com;
|
|
126 GSList *sl_com;
|
|
127 // Ignore leading '/'
|
|
128 for (p1 = command ; *p1 == '/' ; p1++)
|
|
129 ;
|
|
130 // Locate the end of the command
|
|
131 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
|
|
132 ;
|
|
133 // Copy the clean command
|
|
134 com = g_strndup(p1, p2-p1);
|
|
135
|
|
136 // Look for command in the list
|
|
137 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
|
|
138 if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
|
|
139 break;
|
|
140 }
|
|
141 g_free(com);
|
|
142
|
|
143 if (sl_com) // Command has been found.
|
|
144 return (cmd*)sl_com->data;
|
|
145 return NULL;
|
|
146 }
|
|
147
|
47
|
148 // send_message(msg)
|
|
149 // Write the message in the buddy's window and send the message on
|
|
150 // the network.
|
|
151 void send_message(char *msg)
|
|
152 {
|
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
|
213
|
170 jb_send_msg(jid, msg);
|
47
|
171 }
|
|
172
|
|
173 // process_line(line)
|
|
174 // Process a command/message line.
|
|
175 // If this isn't a command, this is a message and it is sent to the
|
|
176 // currently selected buddy.
|
|
177 int process_line(char *line)
|
|
178 {
|
101
|
179 char *p;
|
104
|
180 cmd *curcmd;
|
|
181
|
132
|
182 if (!*line) { // User only pressed enter
|
129
|
183 if (current_buddy) {
|
132
|
184 scr_set_chatmode(TRUE);
|
129
|
185 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
132
|
186 scr_ShowBuddyWindow();
|
|
187 }
|
|
188 return 0;
|
|
189 }
|
129
|
190
|
132
|
191 if (*line != '/') {
|
|
192 do_say(line);
|
47
|
193 return 0;
|
|
194 }
|
101
|
195
|
|
196 /* It is a command */
|
|
197 // Remove trailing spaces:
|
|
198 for (p=line ; *p ; p++)
|
|
199 ;
|
|
200 for (p-- ; p>line && (*p == ' ') ; p--)
|
|
201 *p = 0;
|
|
202
|
|
203 // Command "quit"?
|
116
|
204 if (!strncasecmp(line, "/quit", 5))
|
|
205 if (!line[5] || line[5] == ' ')
|
|
206 return 255;
|
104
|
207
|
47
|
208 // Commands handling
|
104
|
209 curcmd = cmd_get(line);
|
47
|
210
|
104
|
211 if (!curcmd) {
|
|
212 scr_LogPrint("Unrecognized command, sorry.");
|
|
213 return 0;
|
|
214 }
|
|
215 if (!curcmd->func) {
|
|
216 scr_LogPrint("Not yet implemented, sorry.");
|
|
217 return 0;
|
|
218 }
|
|
219 // Lets go to the command parameters
|
|
220 for (line++; *line && (*line != ' ') ; line++)
|
|
221 ;
|
|
222 // Skip spaces
|
|
223 while (*line && (*line == ' '))
|
|
224 line++;
|
|
225 // Call command-specific function
|
|
226 (*curcmd->func)(line);
|
47
|
227 return 0;
|
|
228 }
|
|
229
|
104
|
230 /* Commands callback functions */
|
|
231
|
|
232 void do_roster(char *arg)
|
|
233 {
|
|
234 if (!strcasecmp(arg, "top")) {
|
|
235 scr_RosterTop();
|
128
|
236 update_roster = TRUE;
|
104
|
237 } else if (!strcasecmp(arg, "bottom")) {
|
|
238 scr_RosterBottom();
|
128
|
239 update_roster = TRUE;
|
104
|
240 } else if (!strcasecmp(arg, "hide_offline")) {
|
120
|
241 buddylist_set_hide_offline_buddies(TRUE);
|
104
|
242 if (current_buddy)
|
|
243 buddylist_build();
|
128
|
244 update_roster = TRUE;
|
104
|
245 } else if (!strcasecmp(arg, "show_offline")) {
|
120
|
246 buddylist_set_hide_offline_buddies(FALSE);
|
124
|
247 buddylist_build();
|
128
|
248 update_roster = TRUE;
|
104
|
249 } else
|
|
250 scr_LogPrint("Unrecognized parameter!");
|
|
251 }
|
108
|
252
|
116
|
253 void do_status(char *arg)
|
|
254 {
|
|
255 enum imstatus st;
|
|
256
|
|
257 if (!arg || (*arg == 0)) {
|
|
258 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]);
|
|
259 return;
|
|
260 }
|
|
261
|
130
|
262 if (!strcasecmp(arg, "offline")) st = offline;
|
|
263 else if (!strcasecmp(arg, "online")) st = available;
|
|
264 else if (!strcasecmp(arg, "avail")) st = available;
|
|
265 else if (!strcasecmp(arg, "away")) st = away;
|
|
266 else if (!strcasecmp(arg, "invisible")) st = invisible;
|
|
267 else if (!strcasecmp(arg, "dnd")) st = dontdisturb;
|
|
268 else if (!strcasecmp(arg, "busy")) st = occupied;
|
|
269 else if (!strcasecmp(arg, "notavail")) st = notavail;
|
|
270 else if (!strcasecmp(arg, "free")) st = freeforchat;
|
116
|
271 else {
|
|
272 scr_LogPrint("Unrecognized parameter!");
|
|
273 return;
|
|
274 }
|
|
275
|
|
276 // XXX special case if offline??
|
|
277 jb_setstatus(st, NULL); // TODO handle message (instead of NULL)
|
|
278 }
|
|
279
|
124
|
280 void do_add(char *arg)
|
|
281 {
|
|
282 if (!arg || (*arg == 0)) {
|
|
283 scr_LogPrint("Wrong usage");
|
|
284 return;
|
|
285 }
|
|
286
|
|
287 // FIXME check arg =~ jabber id
|
|
288 // 2nd parameter = optional nickname (XXX NULL for now...)
|
|
289 jb_addbuddy(arg, NULL);
|
232
|
290 scr_LogPrint("Sent presence notfication request to <%s>", arg);
|
124
|
291 }
|
128
|
292
|
206
|
293 void do_del(char *arg)
|
|
294 {
|
|
295 const char *jid;
|
|
296
|
|
297 if (arg && (*arg)) {
|
|
298 scr_LogPrint("Wrong usage");
|
|
299 return;
|
|
300 }
|
|
301
|
|
302 if (!current_buddy) return;
|
|
303 jid = buddy_getjid(BUDDATA(current_buddy));
|
|
304 if (!jid) return;
|
|
305
|
|
306 scr_LogPrint("Removing <%s>...", jid);
|
|
307 jb_delbuddy(jid);
|
|
308 }
|
|
309
|
128
|
310 void do_group(char *arg)
|
|
311 {
|
|
312 gpointer group;
|
166
|
313 guint leave_windowbuddy;
|
128
|
314
|
|
315 if (!arg || (*arg == 0)) {
|
|
316 scr_LogPrint("Missing parameter");
|
|
317 return;
|
|
318 }
|
|
319
|
166
|
320 if (!current_buddy) return;
|
128
|
321
|
166
|
322 group = buddy_getgroup(BUDDATA(current_buddy));
|
212
|
323 // We'll have to redraw the chat window if we're not currently on the group
|
|
324 // entry itself, because it means we'll have to leave the current buddy
|
|
325 // chat window.
|
166
|
326 leave_windowbuddy = (group != BUDDATA(current_buddy));
|
|
327
|
128
|
328 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) {
|
166
|
329 scr_LogPrint("You need to select a group");
|
128
|
330 return;
|
|
331 }
|
166
|
332
|
|
333 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) {
|
128
|
334 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE);
|
166
|
335 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) {
|
128
|
336 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE);
|
130
|
337 } else if (!strcasecmp(arg, "toggle")) {
|
|
338 buddy_setflags(group, ROSTER_FLAG_HIDE,
|
|
339 !(buddy_getflags(group) & ROSTER_FLAG_HIDE));
|
128
|
340 } else {
|
|
341 scr_LogPrint("Unrecognized parameter!");
|
|
342 return;
|
|
343 }
|
|
344
|
|
345 buddylist_build();
|
|
346 update_roster = TRUE;
|
166
|
347 if (leave_windowbuddy) scr_ShowBuddyWindow();
|
128
|
348 }
|
|
349
|
132
|
350 void do_say(char *arg)
|
|
351 {
|
164
|
352 gpointer bud;
|
132
|
353
|
|
354 scr_set_chatmode(TRUE);
|
164
|
355
|
|
356 if (!current_buddy) {
|
132
|
357 scr_LogPrint("Who are you talking to??");
|
164
|
358 return;
|
132
|
359 }
|
164
|
360
|
|
361 bud = BUDDATA(current_buddy);
|
|
362 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) {
|
|
363 scr_LogPrint("This is not a user");
|
|
364 return;
|
|
365 }
|
|
366
|
|
367 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
|
|
368 send_message(arg);
|
132
|
369 }
|
|
370
|
187
|
371 void do_buffer(char *arg)
|
|
372 {
|
|
373 if (!strcasecmp(arg, "top")) {
|
|
374 scr_BufferTop();
|
|
375 } else if (!strcasecmp(arg, "bottom")) {
|
|
376 scr_BufferBottom();
|
|
377 } else if (!strcasecmp(arg, "clear")) {
|
|
378 scr_Clear();
|
|
379 } else
|
|
380 scr_LogPrint("Unrecognized parameter!");
|
|
381 }
|
|
382
|
|
383 void do_clear(char *arg) // Alias for "/buffer clear"
|
|
384 {
|
|
385 do_buffer("clear");
|
|
386 }
|
|
387
|
191
|
388 void do_info(char *arg)
|
|
389 {
|
|
390 gpointer bud;
|
221
|
391 const char *jid, *name, *st_msg;
|
191
|
392 guint type;
|
|
393 enum imstatus status;
|
|
394 char *buffer;
|
|
395
|
|
396 if (!current_buddy) return;
|
|
397 bud = BUDDATA(current_buddy);
|
|
398
|
|
399 jid = buddy_getjid(bud);
|
|
400 name = buddy_getname(bud);
|
|
401 type = buddy_gettype(bud);
|
|
402 status = buddy_getstatus(bud);
|
221
|
403 st_msg = buddy_getstatusmsg(bud);
|
191
|
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 }
|
221
|
416 if (st_msg) {
|
|
417 snprintf(buffer, 127, "Status message: %s", st_msg);
|
|
418 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
419 }
|
191
|
420
|
|
421 if (type == ROSTER_TYPE_USER) typestr = "user";
|
|
422 else if (type == ROSTER_TYPE_AGENT) typestr = "agent";
|
|
423
|
|
424 snprintf(buffer, 127, "Type: %s", typestr);
|
|
425 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
426 } else {
|
|
427 if (name) scr_LogPrint("Name: %s", name);
|
|
428 scr_LogPrint("Type: %s",
|
|
429 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown"));
|
|
430 }
|
|
431
|
|
432 g_free(buffer);
|
|
433 }
|
|
434
|
208
|
435 void do_rename(char *arg)
|
|
436 {
|
|
437 gpointer bud;
|
|
438 const char *jid, *group;
|
|
439 guint type;
|
|
440 char *newname, *p;
|
|
441
|
|
442 if (!arg || (*arg == 0)) {
|
|
443 scr_LogPrint("Missing parameter");
|
|
444 return;
|
|
445 }
|
|
446
|
|
447 if (!current_buddy) return;
|
|
448 bud = BUDDATA(current_buddy);
|
|
449
|
|
450 jid = buddy_getjid(bud);
|
|
451 group = buddy_getgroupname(bud);
|
|
452 type = buddy_gettype(bud);
|
|
453
|
|
454 if (type & ROSTER_TYPE_GROUP) {
|
|
455 scr_LogPrint("You can't rename groups");
|
|
456 return;
|
|
457 }
|
|
458
|
|
459 newname = g_strdup(arg);
|
|
460 // Remove trailing space
|
|
461 for (p = newname; *p; p++) ;
|
|
462 while (p > newname && *p == ' ') *p = 0;
|
|
463
|
|
464 buddy_setname(bud, newname);
|
|
465 jb_updatebuddy(jid, newname, group);
|
|
466
|
|
467 g_free(newname);
|
|
468 update_roster = TRUE;
|
|
469 }
|
|
470
|
210
|
471 void do_move(char *arg)
|
|
472 {
|
|
473 gpointer bud;
|
|
474 const char *jid, *name;
|
|
475 guint type;
|
|
476 char *newgroupname, *p;
|
|
477
|
|
478 if (!current_buddy) return;
|
|
479 bud = BUDDATA(current_buddy);
|
|
480
|
|
481 jid = buddy_getjid(bud);
|
|
482 name = buddy_getname(bud);
|
|
483 type = buddy_gettype(bud);
|
|
484
|
|
485 if (type & ROSTER_TYPE_GROUP) {
|
|
486 scr_LogPrint("You can't move groups!");
|
|
487 return;
|
|
488 }
|
|
489
|
|
490 newgroupname = g_strdup(arg);
|
|
491 // Remove trailing space
|
|
492 for (p = newgroupname; *p; p++) ;
|
|
493 while (p > newgroupname && *p == ' ') *p = 0;
|
|
494
|
|
495 // Call to buddy_setgroup() should be at the end, as current implementation
|
|
496 // clones the buddy and deletes the old one (and thus, jid and name are
|
|
497 // freed)
|
|
498 jb_updatebuddy(jid, name, newgroupname);
|
|
499 buddy_setgroup(bud, newgroupname);
|
|
500
|
|
501 g_free(newgroupname);
|
|
502 update_roster = TRUE;
|
|
503 }
|
|
504
|