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"); |
236
|
107 compl_add_category_word(COMPL_ROSTER, "unread_first"); |
|
108 compl_add_category_word(COMPL_ROSTER, "unread_next"); |
127
|
109 |
187
|
110 // Roster category |
|
111 compl_add_category_word(COMPL_BUFFER, "bottom"); |
|
112 compl_add_category_word(COMPL_BUFFER, "clear"); |
|
113 compl_add_category_word(COMPL_BUFFER, "top"); |
|
114 |
127
|
115 // Group category |
|
116 compl_add_category_word(COMPL_GROUP, "expand"); |
|
117 compl_add_category_word(COMPL_GROUP, "shrink"); |
130
|
118 compl_add_category_word(COMPL_GROUP, "toggle"); |
95
|
119 } |
94
|
120 |
102
|
121 // cmd_get |
|
122 // Finds command in the command list structure. |
|
123 // Returns a pointer to the cmd entry, or NULL if command not found. |
|
124 cmd *cmd_get(char *command) |
|
125 { |
|
126 char *p1, *p2; |
|
127 char *com; |
|
128 GSList *sl_com; |
|
129 // Ignore leading '/' |
|
130 for (p1 = command ; *p1 == '/' ; p1++) |
|
131 ; |
|
132 // Locate the end of the command |
|
133 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) |
|
134 ; |
|
135 // Copy the clean command |
|
136 com = g_strndup(p1, p2-p1); |
|
137 |
|
138 // Look for command in the list |
|
139 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) { |
|
140 if (!strcasecmp(com, ((cmd*)sl_com->data)->name)) |
|
141 break; |
|
142 } |
|
143 g_free(com); |
|
144 |
|
145 if (sl_com) // Command has been found. |
|
146 return (cmd*)sl_com->data; |
|
147 return NULL; |
|
148 } |
|
149 |
47
|
150 // send_message(msg) |
|
151 // Write the message in the buddy's window and send the message on |
|
152 // the network. |
|
153 void send_message(char *msg) |
|
154 { |
81
|
155 const char *jid; |
|
156 |
|
157 if (!current_buddy) { |
|
158 scr_LogPrint("No buddy currently selected."); |
|
159 return; |
|
160 } |
|
161 |
|
162 jid = CURRENT_JID; |
|
163 if (!jid) { |
|
164 scr_LogPrint("No buddy currently selected."); |
|
165 return; |
|
166 } |
47
|
167 |
113
|
168 // local part (UI, logging, etc.) |
|
169 hk_message_out(jid, 0, msg); |
47
|
170 |
|
171 // Network part |
213
|
172 jb_send_msg(jid, msg); |
47
|
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; |
236
|
251 } else if (!strcasecmp(arg, "unread_first")) { |
|
252 scr_RosterUnreadMessage(0); |
|
253 } else if (!strcasecmp(arg, "unread_next")) { |
|
254 scr_RosterUnreadMessage(1); |
104
|
255 } else |
|
256 scr_LogPrint("Unrecognized parameter!"); |
|
257 } |
108
|
258 |
116
|
259 void do_status(char *arg) |
|
260 { |
|
261 enum imstatus st; |
|
262 |
|
263 if (!arg || (*arg == 0)) { |
|
264 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]); |
|
265 return; |
|
266 } |
|
267 |
130
|
268 if (!strcasecmp(arg, "offline")) st = offline; |
|
269 else if (!strcasecmp(arg, "online")) st = available; |
|
270 else if (!strcasecmp(arg, "avail")) st = available; |
|
271 else if (!strcasecmp(arg, "away")) st = away; |
|
272 else if (!strcasecmp(arg, "invisible")) st = invisible; |
|
273 else if (!strcasecmp(arg, "dnd")) st = dontdisturb; |
|
274 else if (!strcasecmp(arg, "busy")) st = occupied; |
|
275 else if (!strcasecmp(arg, "notavail")) st = notavail; |
|
276 else if (!strcasecmp(arg, "free")) st = freeforchat; |
116
|
277 else { |
|
278 scr_LogPrint("Unrecognized parameter!"); |
|
279 return; |
|
280 } |
|
281 |
|
282 // XXX special case if offline?? |
|
283 jb_setstatus(st, NULL); // TODO handle message (instead of NULL) |
|
284 } |
|
285 |
124
|
286 void do_add(char *arg) |
|
287 { |
|
288 if (!arg || (*arg == 0)) { |
|
289 scr_LogPrint("Wrong usage"); |
|
290 return; |
|
291 } |
|
292 |
|
293 // FIXME check arg =~ jabber id |
|
294 // 2nd parameter = optional nickname (XXX NULL for now...) |
|
295 jb_addbuddy(arg, NULL); |
232
|
296 scr_LogPrint("Sent presence notfication request to <%s>", arg); |
124
|
297 } |
128
|
298 |
206
|
299 void do_del(char *arg) |
|
300 { |
|
301 const char *jid; |
|
302 |
|
303 if (arg && (*arg)) { |
|
304 scr_LogPrint("Wrong usage"); |
|
305 return; |
|
306 } |
|
307 |
|
308 if (!current_buddy) return; |
|
309 jid = buddy_getjid(BUDDATA(current_buddy)); |
|
310 if (!jid) return; |
|
311 |
|
312 scr_LogPrint("Removing <%s>...", jid); |
|
313 jb_delbuddy(jid); |
|
314 } |
|
315 |
128
|
316 void do_group(char *arg) |
|
317 { |
|
318 gpointer group; |
166
|
319 guint leave_windowbuddy; |
128
|
320 |
|
321 if (!arg || (*arg == 0)) { |
|
322 scr_LogPrint("Missing parameter"); |
|
323 return; |
|
324 } |
|
325 |
166
|
326 if (!current_buddy) return; |
128
|
327 |
166
|
328 group = buddy_getgroup(BUDDATA(current_buddy)); |
212
|
329 // We'll have to redraw the chat window if we're not currently on the group |
|
330 // entry itself, because it means we'll have to leave the current buddy |
|
331 // chat window. |
166
|
332 leave_windowbuddy = (group != BUDDATA(current_buddy)); |
|
333 |
128
|
334 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) { |
166
|
335 scr_LogPrint("You need to select a group"); |
128
|
336 return; |
|
337 } |
166
|
338 |
|
339 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) { |
128
|
340 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE); |
166
|
341 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) { |
128
|
342 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE); |
130
|
343 } else if (!strcasecmp(arg, "toggle")) { |
|
344 buddy_setflags(group, ROSTER_FLAG_HIDE, |
|
345 !(buddy_getflags(group) & ROSTER_FLAG_HIDE)); |
128
|
346 } else { |
|
347 scr_LogPrint("Unrecognized parameter!"); |
|
348 return; |
|
349 } |
|
350 |
|
351 buddylist_build(); |
|
352 update_roster = TRUE; |
166
|
353 if (leave_windowbuddy) scr_ShowBuddyWindow(); |
128
|
354 } |
|
355 |
132
|
356 void do_say(char *arg) |
|
357 { |
164
|
358 gpointer bud; |
132
|
359 |
|
360 scr_set_chatmode(TRUE); |
164
|
361 |
|
362 if (!current_buddy) { |
132
|
363 scr_LogPrint("Who are you talking to??"); |
164
|
364 return; |
132
|
365 } |
164
|
366 |
|
367 bud = BUDDATA(current_buddy); |
|
368 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) { |
|
369 scr_LogPrint("This is not a user"); |
|
370 return; |
|
371 } |
|
372 |
|
373 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE); |
|
374 send_message(arg); |
132
|
375 } |
|
376 |
187
|
377 void do_buffer(char *arg) |
|
378 { |
|
379 if (!strcasecmp(arg, "top")) { |
|
380 scr_BufferTop(); |
|
381 } else if (!strcasecmp(arg, "bottom")) { |
|
382 scr_BufferBottom(); |
|
383 } else if (!strcasecmp(arg, "clear")) { |
|
384 scr_Clear(); |
|
385 } else |
|
386 scr_LogPrint("Unrecognized parameter!"); |
|
387 } |
|
388 |
|
389 void do_clear(char *arg) // Alias for "/buffer clear" |
|
390 { |
|
391 do_buffer("clear"); |
|
392 } |
|
393 |
191
|
394 void do_info(char *arg) |
|
395 { |
|
396 gpointer bud; |
221
|
397 const char *jid, *name, *st_msg; |
191
|
398 guint type; |
|
399 enum imstatus status; |
|
400 char *buffer; |
|
401 |
|
402 if (!current_buddy) return; |
|
403 bud = BUDDATA(current_buddy); |
|
404 |
|
405 jid = buddy_getjid(bud); |
|
406 name = buddy_getname(bud); |
|
407 type = buddy_gettype(bud); |
|
408 status = buddy_getstatus(bud); |
221
|
409 st_msg = buddy_getstatusmsg(bud); |
191
|
410 |
|
411 buffer = g_new(char, 128); |
|
412 |
|
413 if (jid) { |
|
414 char *typestr = "unknown"; |
|
415 |
|
416 snprintf(buffer, 127, "jid: <%s>", jid); |
|
417 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
418 if (name) { |
|
419 snprintf(buffer, 127, "Name: %s", name); |
|
420 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
421 } |
221
|
422 if (st_msg) { |
|
423 snprintf(buffer, 127, "Status message: %s", st_msg); |
|
424 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
425 } |
191
|
426 |
|
427 if (type == ROSTER_TYPE_USER) typestr = "user"; |
|
428 else if (type == ROSTER_TYPE_AGENT) typestr = "agent"; |
|
429 |
|
430 snprintf(buffer, 127, "Type: %s", typestr); |
|
431 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO); |
|
432 } else { |
|
433 if (name) scr_LogPrint("Name: %s", name); |
|
434 scr_LogPrint("Type: %s", |
|
435 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown")); |
|
436 } |
|
437 |
|
438 g_free(buffer); |
|
439 } |
|
440 |
208
|
441 void do_rename(char *arg) |
|
442 { |
|
443 gpointer bud; |
|
444 const char *jid, *group; |
|
445 guint type; |
|
446 char *newname, *p; |
|
447 |
|
448 if (!arg || (*arg == 0)) { |
|
449 scr_LogPrint("Missing parameter"); |
|
450 return; |
|
451 } |
|
452 |
|
453 if (!current_buddy) return; |
|
454 bud = BUDDATA(current_buddy); |
|
455 |
|
456 jid = buddy_getjid(bud); |
|
457 group = buddy_getgroupname(bud); |
|
458 type = buddy_gettype(bud); |
|
459 |
|
460 if (type & ROSTER_TYPE_GROUP) { |
|
461 scr_LogPrint("You can't rename groups"); |
|
462 return; |
|
463 } |
|
464 |
|
465 newname = g_strdup(arg); |
|
466 // Remove trailing space |
|
467 for (p = newname; *p; p++) ; |
|
468 while (p > newname && *p == ' ') *p = 0; |
|
469 |
|
470 buddy_setname(bud, newname); |
|
471 jb_updatebuddy(jid, newname, group); |
|
472 |
|
473 g_free(newname); |
|
474 update_roster = TRUE; |
|
475 } |
|
476 |
210
|
477 void do_move(char *arg) |
|
478 { |
|
479 gpointer bud; |
|
480 const char *jid, *name; |
|
481 guint type; |
|
482 char *newgroupname, *p; |
|
483 |
|
484 if (!current_buddy) return; |
|
485 bud = BUDDATA(current_buddy); |
|
486 |
|
487 jid = buddy_getjid(bud); |
|
488 name = buddy_getname(bud); |
|
489 type = buddy_gettype(bud); |
|
490 |
|
491 if (type & ROSTER_TYPE_GROUP) { |
|
492 scr_LogPrint("You can't move groups!"); |
|
493 return; |
|
494 } |
|
495 |
|
496 newgroupname = g_strdup(arg); |
|
497 // Remove trailing space |
|
498 for (p = newgroupname; *p; p++) ; |
|
499 while (p > newgroupname && *p == ' ') *p = 0; |
|
500 |
|
501 // Call to buddy_setgroup() should be at the end, as current implementation |
|
502 // clones the buddy and deletes the old one (and thus, jid and name are |
|
503 // freed) |
|
504 jb_updatebuddy(jid, name, newgroupname); |
|
505 buddy_setgroup(bud, newgroupname); |
|
506 |
|
507 g_free(newgroupname); |
|
508 update_roster = TRUE; |
|
509 } |
|
510 |