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); |
238
|
41 void do_msay(char *arg); |
187
|
42 void do_buffer(char *arg); |
|
43 void do_clear(char *arg); |
191
|
44 void do_info(char *arg); |
208
|
45 void do_rename(char *arg); |
210
|
46 void do_move(char *arg); |
47
|
47 |
104
|
48 // Global variable for the commands list |
95
|
49 static GSList *Commands; |
|
50 |
104
|
51 |
95
|
52 // cmd_add() |
|
53 // Adds a command to the commands list and to the CMD completion list |
|
54 void cmd_add(const char *name, const char *help, |
104
|
55 guint flags_row1, guint flags_row2, void (*f)()) |
95
|
56 { |
|
57 cmd *n_cmd = g_new0(cmd, 1); |
|
58 strncpy(n_cmd->name, name, 32-1); |
|
59 n_cmd->help = help; |
|
60 n_cmd->completion_flags[0] = flags_row1; |
|
61 n_cmd->completion_flags[1] = flags_row2; |
|
62 n_cmd->func = f; |
102
|
63 Commands = g_slist_append(Commands, n_cmd); |
95
|
64 // Add to completion CMD category |
|
65 compl_add_category_word(COMPL_CMD, name); |
|
66 } |
|
67 |
|
68 // cmd_init() |
|
69 // ... |
|
70 void cmd_init(void) |
|
71 { |
124
|
72 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); |
187
|
73 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)", |
|
74 COMPL_BUFFER, 0, &do_buffer); |
108
|
75 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); |
205
|
76 cmd_add("del", "Delete the current buddy", 0, 0, &do_del); |
128
|
77 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group); |
127
|
78 cmd_add("help", "Display some help", COMPL_CMD, 0, NULL); |
191
|
79 cmd_add("info", "Show basic infos on current buddy", 0, 0, &do_info); |
225
|
80 cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME, |
|
81 0, &do_move); |
238
|
82 cmd_add("msay", "Send a multi-lines message to the selected buddy", |
|
83 COMPL_MULTILINE, 0, &do_msay); |
95
|
84 //cmd_add("nick"); |
|
85 cmd_add("quit", "Exit the software", 0, 0, NULL); |
208
|
86 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename); |
95
|
87 //cmd_add("request_auth"); |
128
|
88 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0, |
|
89 &do_roster); |
132
|
90 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say); |
95
|
91 //cmd_add("search"); |
|
92 //cmd_add("send_auth"); |
116
|
93 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status); |
95
|
94 |
|
95 // Status category |
|
96 compl_add_category_word(COMPL_STATUS, "online"); |
|
97 compl_add_category_word(COMPL_STATUS, "avail"); |
|
98 compl_add_category_word(COMPL_STATUS, "invisible"); |
|
99 compl_add_category_word(COMPL_STATUS, "free"); |
|
100 compl_add_category_word(COMPL_STATUS, "dnd"); |
|
101 compl_add_category_word(COMPL_STATUS, "busy"); |
|
102 compl_add_category_word(COMPL_STATUS, "notavail"); |
|
103 compl_add_category_word(COMPL_STATUS, "away"); |
103
|
104 |
|
105 // Roster category |
|
106 compl_add_category_word(COMPL_ROSTER, "bottom"); |
|
107 compl_add_category_word(COMPL_ROSTER, "hide_offline"); |
|
108 compl_add_category_word(COMPL_ROSTER, "show_offline"); |
|
109 compl_add_category_word(COMPL_ROSTER, "top"); |
236
|
110 compl_add_category_word(COMPL_ROSTER, "unread_first"); |
|
111 compl_add_category_word(COMPL_ROSTER, "unread_next"); |
127
|
112 |
187
|
113 // Roster category |
|
114 compl_add_category_word(COMPL_BUFFER, "bottom"); |
|
115 compl_add_category_word(COMPL_BUFFER, "clear"); |
|
116 compl_add_category_word(COMPL_BUFFER, "top"); |
|
117 |
127
|
118 // Group category |
|
119 compl_add_category_word(COMPL_GROUP, "expand"); |
|
120 compl_add_category_word(COMPL_GROUP, "shrink"); |
130
|
121 compl_add_category_word(COMPL_GROUP, "toggle"); |
238
|
122 |
|
123 // Multi-line (msay) category |
|
124 compl_add_category_word(COMPL_MULTILINE, "abort"); |
|
125 compl_add_category_word(COMPL_MULTILINE, "begin"); |
|
126 compl_add_category_word(COMPL_MULTILINE, "send"); |
95
|
127 } |
94
|
128 |
102
|
129 // cmd_get |
|
130 // Finds command in the command list structure. |
|
131 // Returns a pointer to the cmd entry, or NULL if command not found. |
238
|
132 cmd *cmd_get(const char *command) |
102
|
133 { |
238
|
134 const char *p1, *p2; |
102
|
135 char *com; |
|
136 GSList *sl_com; |
|
137 // Ignore leading '/' |
|
138 for (p1 = command ; *p1 == '/' ; p1++) |
|
139 ; |
|
140 // Locate the end of the command |
|
141 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) |
|
142 ; |
|
143 // Copy the clean command |
|
144 com = g_strndup(p1, p2-p1); |
|
145 |
|
146 // Look for command in the list |
|
147 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) { |
|
148 if (!strcasecmp(com, ((cmd*)sl_com->data)->name)) |
|
149 break; |
|
150 } |
|
151 g_free(com); |
|
152 |
|
153 if (sl_com) // Command has been found. |
|
154 return (cmd*)sl_com->data; |
|
155 return NULL; |
|
156 } |
|
157 |
47
|
158 // send_message(msg) |
|
159 // Write the message in the buddy's window and send the message on |
|
160 // the network. |
238
|
161 void send_message(const char *msg) |
47
|
162 { |
81
|
163 const char *jid; |
|
164 |
|
165 if (!current_buddy) { |
|
166 scr_LogPrint("No buddy currently selected."); |
|
167 return; |
|
168 } |
|
169 |
|
170 jid = CURRENT_JID; |
|
171 if (!jid) { |
|
172 scr_LogPrint("No buddy currently selected."); |
|
173 return; |
|
174 } |
47
|
175 |
113
|
176 // local part (UI, logging, etc.) |
|
177 hk_message_out(jid, 0, msg); |
47
|
178 |
|
179 // Network part |
213
|
180 jb_send_msg(jid, msg); |
47
|
181 } |
|
182 |
|
183 // process_line(line) |
|
184 // Process a command/message line. |
|
185 // If this isn't a command, this is a message and it is sent to the |
|
186 // currently selected buddy. |
|
187 int process_line(char *line) |
|
188 { |
101
|
189 char *p; |
104
|
190 cmd *curcmd; |
|
191 |
132
|
192 if (!*line) { // User only pressed enter |
238
|
193 if (scr_get_multimode()) { |
|
194 scr_append_multiline(""); |
|
195 return 0; |
|
196 } |
129
|
197 if (current_buddy) { |
132
|
198 scr_set_chatmode(TRUE); |
129
|
199 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
132
|
200 scr_ShowBuddyWindow(); |
|
201 } |
|
202 return 0; |
|
203 } |
129
|
204 |
132
|
205 if (*line != '/') { |
238
|
206 if (scr_get_multimode()) |
|
207 scr_append_multiline(line); |
|
208 else |
|
209 do_say(line); |
47
|
210 return 0; |
|
211 } |
101
|
212 |
|
213 /* It is a command */ |
|
214 // Remove trailing spaces: |
|
215 for (p=line ; *p ; p++) |
|
216 ; |
|
217 for (p-- ; p>line && (*p == ' ') ; p--) |
|
218 *p = 0; |
|
219 |
|
220 // Command "quit"? |
116
|
221 if (!strncasecmp(line, "/quit", 5)) |
|
222 if (!line[5] || line[5] == ' ') |
|
223 return 255; |
104
|
224 |
47
|
225 // Commands handling |
104
|
226 curcmd = cmd_get(line); |
47
|
227 |
104
|
228 if (!curcmd) { |
|
229 scr_LogPrint("Unrecognized command, sorry."); |
|
230 return 0; |
|
231 } |
|
232 if (!curcmd->func) { |
|
233 scr_LogPrint("Not yet implemented, sorry."); |
|
234 return 0; |
|
235 } |
|
236 // Lets go to the command parameters |
|
237 for (line++; *line && (*line != ' ') ; line++) |
|
238 ; |
|
239 // Skip spaces |
|
240 while (*line && (*line == ' ')) |
|
241 line++; |
|
242 // Call command-specific function |
|
243 (*curcmd->func)(line); |
47
|
244 return 0; |
|
245 } |
|
246 |
104
|
247 /* Commands callback functions */ |
|
248 |
|
249 void do_roster(char *arg) |
|
250 { |
|
251 if (!strcasecmp(arg, "top")) { |
|
252 scr_RosterTop(); |
128
|
253 update_roster = TRUE; |
104
|
254 } else if (!strcasecmp(arg, "bottom")) { |
|
255 scr_RosterBottom(); |
128
|
256 update_roster = TRUE; |
104
|
257 } else if (!strcasecmp(arg, "hide_offline")) { |
120
|
258 buddylist_set_hide_offline_buddies(TRUE); |
104
|
259 if (current_buddy) |
|
260 buddylist_build(); |
128
|
261 update_roster = TRUE; |
104
|
262 } else if (!strcasecmp(arg, "show_offline")) { |
120
|
263 buddylist_set_hide_offline_buddies(FALSE); |
124
|
264 buddylist_build(); |
128
|
265 update_roster = TRUE; |
236
|
266 } else if (!strcasecmp(arg, "unread_first")) { |
|
267 scr_RosterUnreadMessage(0); |
|
268 } else if (!strcasecmp(arg, "unread_next")) { |
|
269 scr_RosterUnreadMessage(1); |
104
|
270 } else |
|
271 scr_LogPrint("Unrecognized parameter!"); |
|
272 } |
108
|
273 |
116
|
274 void do_status(char *arg) |
|
275 { |
|
276 enum imstatus st; |
|
277 |
|
278 if (!arg || (*arg == 0)) { |
|
279 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]); |
|
280 return; |
|
281 } |
|
282 |
130
|
283 if (!strcasecmp(arg, "offline")) st = offline; |
|
284 else if (!strcasecmp(arg, "online")) st = available; |
|
285 else if (!strcasecmp(arg, "avail")) st = available; |
|
286 else if (!strcasecmp(arg, "away")) st = away; |
|
287 else if (!strcasecmp(arg, "invisible")) st = invisible; |
|
288 else if (!strcasecmp(arg, "dnd")) st = dontdisturb; |
|
289 else if (!strcasecmp(arg, "busy")) st = occupied; |
|
290 else if (!strcasecmp(arg, "notavail")) st = notavail; |
|
291 else if (!strcasecmp(arg, "free")) st = freeforchat; |
116
|
292 else { |
|
293 scr_LogPrint("Unrecognized parameter!"); |
|
294 return; |
|
295 } |
|
296 |
|
297 // XXX special case if offline?? |
|
298 jb_setstatus(st, NULL); // TODO handle message (instead of NULL) |
|
299 } |
|
300 |
124
|
301 void do_add(char *arg) |
|
302 { |
|
303 if (!arg || (*arg == 0)) { |
|
304 scr_LogPrint("Wrong usage"); |
|
305 return; |
|
306 } |
|
307 |
|
308 // FIXME check arg =~ jabber id |
|
309 // 2nd parameter = optional nickname (XXX NULL for now...) |
|
310 jb_addbuddy(arg, NULL); |
232
|
311 scr_LogPrint("Sent presence notfication request to <%s>", arg); |
124
|
312 } |
128
|
313 |
206
|
314 void do_del(char *arg) |
|
315 { |
|
316 const char *jid; |
|
317 |
|
318 if (arg && (*arg)) { |
|
319 scr_LogPrint("Wrong usage"); |
|
320 return; |
|
321 } |
|
322 |
|
323 if (!current_buddy) return; |
|
324 jid = buddy_getjid(BUDDATA(current_buddy)); |
|
325 if (!jid) return; |
|
326 |
|
327 scr_LogPrint("Removing <%s>...", jid); |
|
328 jb_delbuddy(jid); |
|
329 } |
|
330 |
128
|
331 void do_group(char *arg) |
|
332 { |
|
333 gpointer group; |
166
|
334 guint leave_windowbuddy; |
128
|
335 |
|
336 if (!arg || (*arg == 0)) { |
|
337 scr_LogPrint("Missing parameter"); |
|
338 return; |
|
339 } |
|
340 |
166
|
341 if (!current_buddy) return; |
128
|
342 |
166
|
343 group = buddy_getgroup(BUDDATA(current_buddy)); |
212
|
344 // We'll have to redraw the chat window if we're not currently on the group |
|
345 // entry itself, because it means we'll have to leave the current buddy |
|
346 // chat window. |
166
|
347 leave_windowbuddy = (group != BUDDATA(current_buddy)); |
|
348 |
128
|
349 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) { |
166
|
350 scr_LogPrint("You need to select a group"); |
128
|
351 return; |
|
352 } |
166
|
353 |
|
354 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) { |
128
|
355 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE); |
166
|
356 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) { |
128
|
357 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE); |
130
|
358 } else if (!strcasecmp(arg, "toggle")) { |
|
359 buddy_setflags(group, ROSTER_FLAG_HIDE, |
|
360 !(buddy_getflags(group) & ROSTER_FLAG_HIDE)); |
128
|
361 } else { |
|
362 scr_LogPrint("Unrecognized parameter!"); |
|
363 return; |
|
364 } |
|
365 |
|
366 buddylist_build(); |
|
367 update_roster = TRUE; |
166
|
368 if (leave_windowbuddy) scr_ShowBuddyWindow(); |
128
|
369 } |
|
370 |
132
|
371 void do_say(char *arg) |
|
372 { |
164
|
373 gpointer bud; |
132
|
374 |
|
375 scr_set_chatmode(TRUE); |
164
|
376 |
|
377 if (!current_buddy) { |
132
|
378 scr_LogPrint("Who are you talking to??"); |
164
|
379 return; |
132
|
380 } |
164
|
381 |
|
382 bud = BUDDATA(current_buddy); |
|
383 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) { |
|
384 scr_LogPrint("This is not a user"); |
|
385 return; |
|
386 } |
|
387 |
|
388 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE); |
|
389 send_message(arg); |
132
|
390 } |
|
391 |
238
|
392 void do_msay(char *arg) |
|
393 { |
|
394 /* begin abort send */ |
|
395 gpointer bud; |
|
396 |
|
397 if (!strcasecmp(arg, "abort")) { |
|
398 scr_set_multimode(FALSE); |
|
399 return; |
|
400 } else if (!strcasecmp(arg, "begin")) { |
|
401 scr_set_multimode(TRUE); |
|
402 scr_LogPrint("Entered multi-line message mode."); |
|
403 scr_LogPrint("Select a buddy and use \"/msay send\" " |
|
404 "when your message is ready."); |
|
405 return; |
|
406 } else if (*arg == 0) { |
|
407 scr_LogPrint("Please read the manual before using the /msay command."); |
|
408 scr_LogPrint("(Use /msay begin to enter multi-line mode...)"); |
|
409 return; |
|
410 } else if (strcasecmp(arg, "send")) { |
|
411 scr_LogPrint("Unrecognized parameter!"); |
|
412 return; |
|
413 } |
|
414 |
|
415 // send command |
|
416 |
|
417 if (!scr_get_multimode()) { |
|
418 scr_LogPrint("No message to send. Use \"/msay begin\" first."); |
|
419 return; |
|
420 } |
|
421 |
|
422 scr_set_chatmode(TRUE); |
|
423 |
|
424 if (!current_buddy) { |
|
425 scr_LogPrint("Who are you talking to??"); |
|
426 return; |
|
427 } |
|
428 |
|
429 bud = BUDDATA(current_buddy); |
|
430 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) { |
|
431 scr_LogPrint("This is not a user"); |
|
432 return; |
|
433 } |
|
434 |
|
435 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE); |
|
436 send_message(scr_get_multiline()); |
|
437 scr_set_multimode(FALSE); |
|
438 } |
|
439 |
187
|
440 void do_buffer(char *arg) |
|
441 { |
|
442 if (!strcasecmp(arg, "top")) { |
|
443 scr_BufferTop(); |
|
444 } else if (!strcasecmp(arg, "bottom")) { |
|
445 scr_BufferBottom(); |
|
446 } else if (!strcasecmp(arg, "clear")) { |
|
447 scr_Clear(); |
|
448 } else |
|
449 scr_LogPrint("Unrecognized parameter!"); |
|
450 } |
|
451 |
|
452 void do_clear(char *arg) // Alias for "/buffer clear" |
|
453 { |
|
454 do_buffer("clear"); |
|
455 } |
|
456 |
191
|
457 void do_info(char *arg) |
|
458 { |
|
459 gpointer bud; |
221
|
460 const char *jid, *name, *st_msg; |
191
|
461 guint type; |
|
462 enum imstatus status; |
|
463 char *buffer; |
|
464 |
|
465 if (!current_buddy) return; |
|
466 bud = BUDDATA(current_buddy); |
|
467 |
|
468 jid = buddy_getjid(bud); |
|
469 name = buddy_getname(bud); |
|
470 type = buddy_gettype(bud); |
|
471 status = buddy_getstatus(bud); |
221
|
472 st_msg = buddy_getstatusmsg(bud); |
191
|
473 |
|
474 buffer = g_new(char, 128); |
|
475 |
|
476 if (jid) { |
|
477 char *typestr = "unknown"; |
|
478 |
|
479 snprintf(buffer, 127, "jid: <%s>", jid); |
|
480 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
481 if (name) { |
|
482 snprintf(buffer, 127, "Name: %s", name); |
|
483 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
484 } |
221
|
485 if (st_msg) { |
|
486 snprintf(buffer, 127, "Status message: %s", st_msg); |
|
487 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
488 } |
191
|
489 |
|
490 if (type == ROSTER_TYPE_USER) typestr = "user"; |
|
491 else if (type == ROSTER_TYPE_AGENT) typestr = "agent"; |
|
492 |
|
493 snprintf(buffer, 127, "Type: %s", typestr); |
|
494 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
495 } else { |
|
496 if (name) scr_LogPrint("Name: %s", name); |
|
497 scr_LogPrint("Type: %s", |
|
498 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown")); |
|
499 } |
|
500 |
|
501 g_free(buffer); |
|
502 } |
|
503 |
208
|
504 void do_rename(char *arg) |
|
505 { |
|
506 gpointer bud; |
|
507 const char *jid, *group; |
|
508 guint type; |
|
509 char *newname, *p; |
|
510 |
|
511 if (!arg || (*arg == 0)) { |
|
512 scr_LogPrint("Missing parameter"); |
|
513 return; |
|
514 } |
|
515 |
|
516 if (!current_buddy) return; |
|
517 bud = BUDDATA(current_buddy); |
|
518 |
|
519 jid = buddy_getjid(bud); |
|
520 group = buddy_getgroupname(bud); |
|
521 type = buddy_gettype(bud); |
|
522 |
|
523 if (type & ROSTER_TYPE_GROUP) { |
|
524 scr_LogPrint("You can't rename groups"); |
|
525 return; |
|
526 } |
|
527 |
|
528 newname = g_strdup(arg); |
|
529 // Remove trailing space |
|
530 for (p = newname; *p; p++) ; |
|
531 while (p > newname && *p == ' ') *p = 0; |
|
532 |
|
533 buddy_setname(bud, newname); |
|
534 jb_updatebuddy(jid, newname, group); |
|
535 |
|
536 g_free(newname); |
|
537 update_roster = TRUE; |
|
538 } |
|
539 |
210
|
540 void do_move(char *arg) |
|
541 { |
|
542 gpointer bud; |
|
543 const char *jid, *name; |
|
544 guint type; |
|
545 char *newgroupname, *p; |
|
546 |
|
547 if (!current_buddy) return; |
|
548 bud = BUDDATA(current_buddy); |
|
549 |
|
550 jid = buddy_getjid(bud); |
|
551 name = buddy_getname(bud); |
|
552 type = buddy_gettype(bud); |
|
553 |
|
554 if (type & ROSTER_TYPE_GROUP) { |
|
555 scr_LogPrint("You can't move groups!"); |
|
556 return; |
|
557 } |
|
558 |
|
559 newgroupname = g_strdup(arg); |
|
560 // Remove trailing space |
|
561 for (p = newgroupname; *p; p++) ; |
|
562 while (p > newgroupname && *p == ' ') *p = 0; |
|
563 |
|
564 // Call to buddy_setgroup() should be at the end, as current implementation |
|
565 // clones the buddy and deletes the old one (and thus, jid and name are |
|
566 // freed) |
|
567 jb_updatebuddy(jid, name, newgroupname); |
|
568 buddy_setgroup(bud, newgroupname); |
|
569 |
|
570 g_free(newgroupname); |
|
571 update_roster = TRUE; |
|
572 } |
|
573 |