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);
|
|
290 }
|
128
|
291
|
206
|
292 void do_del(char *arg)
|
|
293 {
|
|
294 const char *jid;
|
|
295
|
|
296 if (arg && (*arg)) {
|
|
297 scr_LogPrint("Wrong usage");
|
|
298 return;
|
|
299 }
|
|
300
|
|
301 if (!current_buddy) return;
|
|
302 jid = buddy_getjid(BUDDATA(current_buddy));
|
|
303 if (!jid) return;
|
|
304
|
|
305 scr_LogPrint("Removing <%s>...", jid);
|
|
306 jb_delbuddy(jid);
|
|
307 }
|
|
308
|
128
|
309 void do_group(char *arg)
|
|
310 {
|
|
311 gpointer group;
|
166
|
312 guint leave_windowbuddy;
|
128
|
313
|
|
314 if (!arg || (*arg == 0)) {
|
|
315 scr_LogPrint("Missing parameter");
|
|
316 return;
|
|
317 }
|
|
318
|
166
|
319 if (!current_buddy) return;
|
128
|
320
|
166
|
321 group = buddy_getgroup(BUDDATA(current_buddy));
|
212
|
322 // We'll have to redraw the chat window if we're not currently on the group
|
|
323 // entry itself, because it means we'll have to leave the current buddy
|
|
324 // chat window.
|
166
|
325 leave_windowbuddy = (group != BUDDATA(current_buddy));
|
|
326
|
128
|
327 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) {
|
166
|
328 scr_LogPrint("You need to select a group");
|
128
|
329 return;
|
|
330 }
|
166
|
331
|
|
332 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) {
|
128
|
333 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE);
|
166
|
334 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) {
|
128
|
335 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE);
|
130
|
336 } else if (!strcasecmp(arg, "toggle")) {
|
|
337 buddy_setflags(group, ROSTER_FLAG_HIDE,
|
|
338 !(buddy_getflags(group) & ROSTER_FLAG_HIDE));
|
128
|
339 } else {
|
|
340 scr_LogPrint("Unrecognized parameter!");
|
|
341 return;
|
|
342 }
|
|
343
|
|
344 buddylist_build();
|
|
345 update_roster = TRUE;
|
166
|
346 if (leave_windowbuddy) scr_ShowBuddyWindow();
|
128
|
347 }
|
|
348
|
132
|
349 void do_say(char *arg)
|
|
350 {
|
164
|
351 gpointer bud;
|
132
|
352
|
|
353 scr_set_chatmode(TRUE);
|
164
|
354
|
|
355 if (!current_buddy) {
|
132
|
356 scr_LogPrint("Who are you talking to??");
|
164
|
357 return;
|
132
|
358 }
|
164
|
359
|
|
360 bud = BUDDATA(current_buddy);
|
|
361 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) {
|
|
362 scr_LogPrint("This is not a user");
|
|
363 return;
|
|
364 }
|
|
365
|
|
366 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
|
|
367 send_message(arg);
|
132
|
368 }
|
|
369
|
187
|
370 void do_buffer(char *arg)
|
|
371 {
|
|
372 if (!strcasecmp(arg, "top")) {
|
|
373 scr_BufferTop();
|
|
374 } else if (!strcasecmp(arg, "bottom")) {
|
|
375 scr_BufferBottom();
|
|
376 } else if (!strcasecmp(arg, "clear")) {
|
|
377 scr_Clear();
|
|
378 } else
|
|
379 scr_LogPrint("Unrecognized parameter!");
|
|
380 }
|
|
381
|
|
382 void do_clear(char *arg) // Alias for "/buffer clear"
|
|
383 {
|
|
384 do_buffer("clear");
|
|
385 }
|
|
386
|
191
|
387 void do_info(char *arg)
|
|
388 {
|
|
389 gpointer bud;
|
221
|
390 const char *jid, *name, *st_msg;
|
191
|
391 guint type;
|
|
392 enum imstatus status;
|
|
393 char *buffer;
|
|
394
|
|
395 if (!current_buddy) return;
|
|
396 bud = BUDDATA(current_buddy);
|
|
397
|
|
398 jid = buddy_getjid(bud);
|
|
399 name = buddy_getname(bud);
|
|
400 type = buddy_gettype(bud);
|
|
401 status = buddy_getstatus(bud);
|
221
|
402 st_msg = buddy_getstatusmsg(bud);
|
191
|
403
|
|
404 buffer = g_new(char, 128);
|
|
405
|
|
406 if (jid) {
|
|
407 char *typestr = "unknown";
|
|
408
|
|
409 snprintf(buffer, 127, "jid: <%s>", jid);
|
|
410 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
411 if (name) {
|
|
412 snprintf(buffer, 127, "Name: %s", name);
|
|
413 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
414 }
|
221
|
415 if (st_msg) {
|
|
416 snprintf(buffer, 127, "Status message: %s", st_msg);
|
|
417 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
418 }
|
191
|
419
|
|
420 if (type == ROSTER_TYPE_USER) typestr = "user";
|
|
421 else if (type == ROSTER_TYPE_AGENT) typestr = "agent";
|
|
422
|
|
423 snprintf(buffer, 127, "Type: %s", typestr);
|
|
424 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
425 } else {
|
|
426 if (name) scr_LogPrint("Name: %s", name);
|
|
427 scr_LogPrint("Type: %s",
|
|
428 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown"));
|
|
429 }
|
|
430
|
|
431 g_free(buffer);
|
|
432 }
|
|
433
|
208
|
434 void do_rename(char *arg)
|
|
435 {
|
|
436 gpointer bud;
|
|
437 const char *jid, *group;
|
|
438 guint type;
|
|
439 char *newname, *p;
|
|
440
|
|
441 if (!arg || (*arg == 0)) {
|
|
442 scr_LogPrint("Missing parameter");
|
|
443 return;
|
|
444 }
|
|
445
|
|
446 if (!current_buddy) return;
|
|
447 bud = BUDDATA(current_buddy);
|
|
448
|
|
449 jid = buddy_getjid(bud);
|
|
450 group = buddy_getgroupname(bud);
|
|
451 type = buddy_gettype(bud);
|
|
452
|
|
453 if (type & ROSTER_TYPE_GROUP) {
|
|
454 scr_LogPrint("You can't rename groups");
|
|
455 return;
|
|
456 }
|
|
457
|
|
458 newname = g_strdup(arg);
|
|
459 // Remove trailing space
|
|
460 for (p = newname; *p; p++) ;
|
|
461 while (p > newname && *p == ' ') *p = 0;
|
|
462
|
|
463 buddy_setname(bud, newname);
|
|
464 jb_updatebuddy(jid, newname, group);
|
|
465
|
|
466 g_free(newname);
|
|
467 update_roster = TRUE;
|
|
468 }
|
|
469
|
210
|
470 void do_move(char *arg)
|
|
471 {
|
|
472 gpointer bud;
|
|
473 const char *jid, *name;
|
|
474 guint type;
|
|
475 char *newgroupname, *p;
|
|
476
|
|
477 if (!current_buddy) return;
|
|
478 bud = BUDDATA(current_buddy);
|
|
479
|
|
480 jid = buddy_getjid(bud);
|
|
481 name = buddy_getname(bud);
|
|
482 type = buddy_gettype(bud);
|
|
483
|
|
484 if (type & ROSTER_TYPE_GROUP) {
|
|
485 scr_LogPrint("You can't move groups!");
|
|
486 return;
|
|
487 }
|
|
488
|
|
489 newgroupname = g_strdup(arg);
|
|
490 // Remove trailing space
|
|
491 for (p = newgroupname; *p; p++) ;
|
|
492 while (p > newgroupname && *p == ' ') *p = 0;
|
|
493
|
|
494 // Call to buddy_setgroup() should be at the end, as current implementation
|
|
495 // clones the buddy and deletes the old one (and thus, jid and name are
|
|
496 // freed)
|
|
497 jb_updatebuddy(jid, name, newgroupname);
|
|
498 buddy_setgroup(bud, newgroupname);
|
|
499
|
|
500 g_free(newgroupname);
|
|
501 update_roster = TRUE;
|
|
502 }
|
|
503
|