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);
|
47
|
45
|
104
|
46 // Global variable for the commands list
|
95
|
47 static GSList *Commands;
|
|
48
|
104
|
49
|
95
|
50 // cmd_add()
|
|
51 // Adds a command to the commands list and to the CMD completion list
|
|
52 void cmd_add(const char *name, const char *help,
|
104
|
53 guint flags_row1, guint flags_row2, void (*f)())
|
95
|
54 {
|
|
55 cmd *n_cmd = g_new0(cmd, 1);
|
|
56 strncpy(n_cmd->name, name, 32-1);
|
|
57 n_cmd->help = help;
|
|
58 n_cmd->completion_flags[0] = flags_row1;
|
|
59 n_cmd->completion_flags[1] = flags_row2;
|
|
60 n_cmd->func = f;
|
102
|
61 Commands = g_slist_append(Commands, n_cmd);
|
95
|
62 // Add to completion CMD category
|
|
63 compl_add_category_word(COMPL_CMD, name);
|
|
64 }
|
|
65
|
|
66 // cmd_init()
|
|
67 // ...
|
|
68 void cmd_init(void)
|
|
69 {
|
124
|
70 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add);
|
187
|
71 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)",
|
|
72 COMPL_BUFFER, 0, &do_buffer);
|
108
|
73 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear);
|
205
|
74 cmd_add("del", "Delete the current buddy", 0, 0, &do_del);
|
128
|
75 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group);
|
127
|
76 cmd_add("help", "Display some help", COMPL_CMD, 0, NULL);
|
191
|
77 cmd_add("info", "Show basic infos on current buddy", 0, 0, &do_info);
|
95
|
78 //cmd_add("move");
|
|
79 //cmd_add("nick");
|
|
80 cmd_add("quit", "Exit the software", 0, 0, NULL);
|
208
|
81 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename);
|
95
|
82 //cmd_add("request_auth");
|
128
|
83 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0,
|
|
84 &do_roster);
|
132
|
85 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say);
|
95
|
86 //cmd_add("search");
|
|
87 //cmd_add("send_auth");
|
116
|
88 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status);
|
95
|
89
|
|
90 // Status category
|
|
91 compl_add_category_word(COMPL_STATUS, "online");
|
|
92 compl_add_category_word(COMPL_STATUS, "avail");
|
|
93 compl_add_category_word(COMPL_STATUS, "invisible");
|
|
94 compl_add_category_word(COMPL_STATUS, "free");
|
|
95 compl_add_category_word(COMPL_STATUS, "dnd");
|
|
96 compl_add_category_word(COMPL_STATUS, "busy");
|
|
97 compl_add_category_word(COMPL_STATUS, "notavail");
|
|
98 compl_add_category_word(COMPL_STATUS, "away");
|
103
|
99
|
|
100 // Roster category
|
|
101 compl_add_category_word(COMPL_ROSTER, "bottom");
|
|
102 compl_add_category_word(COMPL_ROSTER, "hide_offline");
|
|
103 compl_add_category_word(COMPL_ROSTER, "show_offline");
|
|
104 compl_add_category_word(COMPL_ROSTER, "top");
|
127
|
105
|
187
|
106 // Roster category
|
|
107 compl_add_category_word(COMPL_BUFFER, "bottom");
|
|
108 compl_add_category_word(COMPL_BUFFER, "clear");
|
|
109 compl_add_category_word(COMPL_BUFFER, "top");
|
|
110
|
127
|
111 // Group category
|
|
112 compl_add_category_word(COMPL_GROUP, "expand");
|
|
113 compl_add_category_word(COMPL_GROUP, "shrink");
|
130
|
114 compl_add_category_word(COMPL_GROUP, "toggle");
|
95
|
115 }
|
94
|
116
|
102
|
117 // cmd_get
|
|
118 // Finds command in the command list structure.
|
|
119 // Returns a pointer to the cmd entry, or NULL if command not found.
|
|
120 cmd *cmd_get(char *command)
|
|
121 {
|
|
122 char *p1, *p2;
|
|
123 char *com;
|
|
124 GSList *sl_com;
|
|
125 // Ignore leading '/'
|
|
126 for (p1 = command ; *p1 == '/' ; p1++)
|
|
127 ;
|
|
128 // Locate the end of the command
|
|
129 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
|
|
130 ;
|
|
131 // Copy the clean command
|
|
132 com = g_strndup(p1, p2-p1);
|
|
133
|
|
134 // Look for command in the list
|
|
135 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
|
|
136 if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
|
|
137 break;
|
|
138 }
|
|
139 g_free(com);
|
|
140
|
|
141 if (sl_com) // Command has been found.
|
|
142 return (cmd*)sl_com->data;
|
|
143 return NULL;
|
|
144 }
|
|
145
|
47
|
146 // send_message(msg)
|
|
147 // Write the message in the buddy's window and send the message on
|
|
148 // the network.
|
|
149 void send_message(char *msg)
|
|
150 {
|
|
151 char *buffer;
|
81
|
152 const char *jid;
|
|
153
|
|
154 if (!current_buddy) {
|
|
155 scr_LogPrint("No buddy currently selected.");
|
|
156 return;
|
|
157 }
|
|
158
|
|
159 jid = CURRENT_JID;
|
|
160 if (!jid) {
|
|
161 scr_LogPrint("No buddy currently selected.");
|
|
162 return;
|
|
163 }
|
47
|
164
|
113
|
165 // local part (UI, logging, etc.)
|
|
166 hk_message_out(jid, 0, msg);
|
47
|
167
|
|
168 // Network part
|
|
169 buffer = utf8_encode(msg);
|
81
|
170 jb_send_msg(jid, buffer);
|
47
|
171 free(buffer);
|
|
172 }
|
|
173
|
|
174 // process_line(line)
|
|
175 // Process a command/message line.
|
|
176 // If this isn't a command, this is a message and it is sent to the
|
|
177 // currently selected buddy.
|
|
178 int process_line(char *line)
|
|
179 {
|
101
|
180 char *p;
|
104
|
181 cmd *curcmd;
|
|
182
|
132
|
183 if (!*line) { // User only pressed enter
|
129
|
184 if (current_buddy) {
|
132
|
185 scr_set_chatmode(TRUE);
|
129
|
186 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
132
|
187 scr_ShowBuddyWindow();
|
|
188 }
|
|
189 return 0;
|
|
190 }
|
129
|
191
|
132
|
192 if (*line != '/') {
|
|
193 do_say(line);
|
47
|
194 return 0;
|
|
195 }
|
101
|
196
|
|
197 /* It is a command */
|
|
198 // Remove trailing spaces:
|
|
199 for (p=line ; *p ; p++)
|
|
200 ;
|
|
201 for (p-- ; p>line && (*p == ' ') ; p--)
|
|
202 *p = 0;
|
|
203
|
|
204 // Command "quit"?
|
116
|
205 if (!strncasecmp(line, "/quit", 5))
|
|
206 if (!line[5] || line[5] == ' ')
|
|
207 return 255;
|
104
|
208
|
47
|
209 // Commands handling
|
104
|
210 curcmd = cmd_get(line);
|
47
|
211
|
104
|
212 if (!curcmd) {
|
|
213 scr_LogPrint("Unrecognized command, sorry.");
|
|
214 return 0;
|
|
215 }
|
|
216 if (!curcmd->func) {
|
|
217 scr_LogPrint("Not yet implemented, sorry.");
|
|
218 return 0;
|
|
219 }
|
|
220 // Lets go to the command parameters
|
|
221 for (line++; *line && (*line != ' ') ; line++)
|
|
222 ;
|
|
223 // Skip spaces
|
|
224 while (*line && (*line == ' '))
|
|
225 line++;
|
|
226 // Call command-specific function
|
|
227 (*curcmd->func)(line);
|
47
|
228 return 0;
|
|
229 }
|
|
230
|
104
|
231 /* Commands callback functions */
|
|
232
|
|
233 void do_roster(char *arg)
|
|
234 {
|
|
235 if (!strcasecmp(arg, "top")) {
|
|
236 scr_RosterTop();
|
128
|
237 update_roster = TRUE;
|
104
|
238 } else if (!strcasecmp(arg, "bottom")) {
|
|
239 scr_RosterBottom();
|
128
|
240 update_roster = TRUE;
|
104
|
241 } else if (!strcasecmp(arg, "hide_offline")) {
|
120
|
242 buddylist_set_hide_offline_buddies(TRUE);
|
104
|
243 if (current_buddy)
|
|
244 buddylist_build();
|
128
|
245 update_roster = TRUE;
|
104
|
246 } else if (!strcasecmp(arg, "show_offline")) {
|
120
|
247 buddylist_set_hide_offline_buddies(FALSE);
|
124
|
248 buddylist_build();
|
128
|
249 update_roster = TRUE;
|
104
|
250 } else
|
|
251 scr_LogPrint("Unrecognized parameter!");
|
|
252 }
|
108
|
253
|
116
|
254 void do_status(char *arg)
|
|
255 {
|
|
256 enum imstatus st;
|
|
257
|
|
258 if (!arg || (*arg == 0)) {
|
|
259 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]);
|
|
260 return;
|
|
261 }
|
|
262
|
130
|
263 if (!strcasecmp(arg, "offline")) st = offline;
|
|
264 else if (!strcasecmp(arg, "online")) st = available;
|
|
265 else if (!strcasecmp(arg, "avail")) st = available;
|
|
266 else if (!strcasecmp(arg, "away")) st = away;
|
|
267 else if (!strcasecmp(arg, "invisible")) st = invisible;
|
|
268 else if (!strcasecmp(arg, "dnd")) st = dontdisturb;
|
|
269 else if (!strcasecmp(arg, "busy")) st = occupied;
|
|
270 else if (!strcasecmp(arg, "notavail")) st = notavail;
|
|
271 else if (!strcasecmp(arg, "free")) st = freeforchat;
|
116
|
272 else {
|
|
273 scr_LogPrint("Unrecognized parameter!");
|
|
274 return;
|
|
275 }
|
|
276
|
|
277 // XXX special case if offline??
|
|
278 jb_setstatus(st, NULL); // TODO handle message (instead of NULL)
|
|
279 }
|
|
280
|
124
|
281 void do_add(char *arg)
|
|
282 {
|
|
283 if (!arg || (*arg == 0)) {
|
|
284 scr_LogPrint("Wrong usage");
|
|
285 return;
|
|
286 }
|
|
287
|
|
288 // FIXME check arg =~ jabber id
|
|
289 // 2nd parameter = optional nickname (XXX NULL for now...)
|
|
290 jb_addbuddy(arg, NULL);
|
|
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));
|
|
323 leave_windowbuddy = (group != BUDDATA(current_buddy));
|
|
324
|
128
|
325 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) {
|
166
|
326 scr_LogPrint("You need to select a group");
|
128
|
327 return;
|
|
328 }
|
166
|
329
|
|
330 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) {
|
128
|
331 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE);
|
166
|
332 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) {
|
128
|
333 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE);
|
130
|
334 } else if (!strcasecmp(arg, "toggle")) {
|
|
335 buddy_setflags(group, ROSTER_FLAG_HIDE,
|
|
336 !(buddy_getflags(group) & ROSTER_FLAG_HIDE));
|
128
|
337 } else {
|
|
338 scr_LogPrint("Unrecognized parameter!");
|
|
339 return;
|
|
340 }
|
|
341
|
|
342 buddylist_build();
|
|
343 update_roster = TRUE;
|
166
|
344 if (leave_windowbuddy) scr_ShowBuddyWindow();
|
128
|
345 }
|
|
346
|
132
|
347 void do_say(char *arg)
|
|
348 {
|
164
|
349 gpointer bud;
|
132
|
350
|
|
351 scr_set_chatmode(TRUE);
|
164
|
352
|
|
353 if (!current_buddy) {
|
132
|
354 scr_LogPrint("Who are you talking to??");
|
164
|
355 return;
|
132
|
356 }
|
164
|
357
|
|
358 bud = BUDDATA(current_buddy);
|
|
359 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) {
|
|
360 scr_LogPrint("This is not a user");
|
|
361 return;
|
|
362 }
|
|
363
|
|
364 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
|
|
365 send_message(arg);
|
132
|
366 }
|
|
367
|
187
|
368 void do_buffer(char *arg)
|
|
369 {
|
|
370 if (!strcasecmp(arg, "top")) {
|
|
371 scr_BufferTop();
|
|
372 } else if (!strcasecmp(arg, "bottom")) {
|
|
373 scr_BufferBottom();
|
|
374 } else if (!strcasecmp(arg, "clear")) {
|
|
375 scr_Clear();
|
|
376 } else
|
|
377 scr_LogPrint("Unrecognized parameter!");
|
|
378 }
|
|
379
|
|
380 void do_clear(char *arg) // Alias for "/buffer clear"
|
|
381 {
|
|
382 do_buffer("clear");
|
|
383 }
|
|
384
|
191
|
385 void do_info(char *arg)
|
|
386 {
|
|
387 gpointer bud;
|
|
388 const char *jid, *name;
|
|
389 guint type;
|
|
390 enum imstatus status;
|
|
391 char *buffer;
|
|
392
|
|
393 if (!current_buddy) return;
|
|
394 bud = BUDDATA(current_buddy);
|
|
395
|
|
396 jid = buddy_getjid(bud);
|
|
397 name = buddy_getname(bud);
|
|
398 type = buddy_gettype(bud);
|
|
399 status = buddy_getstatus(bud);
|
|
400
|
|
401 buffer = g_new(char, 128);
|
|
402
|
|
403 if (jid) {
|
|
404 char *typestr = "unknown";
|
|
405
|
|
406 snprintf(buffer, 127, "jid: <%s>", jid);
|
|
407 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
408 if (name) {
|
|
409 snprintf(buffer, 127, "Name: %s", name);
|
|
410 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
411 }
|
|
412
|
|
413 if (type == ROSTER_TYPE_USER) typestr = "user";
|
|
414 else if (type == ROSTER_TYPE_AGENT) typestr = "agent";
|
|
415
|
|
416 snprintf(buffer, 127, "Type: %s", typestr);
|
|
417 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
418 } else {
|
|
419 if (name) scr_LogPrint("Name: %s", name);
|
|
420 scr_LogPrint("Type: %s",
|
|
421 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown"));
|
|
422 }
|
|
423
|
|
424 g_free(buffer);
|
|
425 }
|
|
426
|
208
|
427 void do_rename(char *arg)
|
|
428 {
|
|
429 gpointer bud;
|
|
430 const char *jid, *group;
|
|
431 guint type;
|
|
432 char *newname, *p;
|
|
433
|
|
434 if (!arg || (*arg == 0)) {
|
|
435 scr_LogPrint("Missing parameter");
|
|
436 return;
|
|
437 }
|
|
438
|
|
439 if (!current_buddy) return;
|
|
440 bud = BUDDATA(current_buddy);
|
|
441
|
|
442 jid = buddy_getjid(bud);
|
|
443 group = buddy_getgroupname(bud);
|
|
444 type = buddy_gettype(bud);
|
|
445
|
|
446 if (type & ROSTER_TYPE_GROUP) {
|
|
447 scr_LogPrint("You can't rename groups");
|
|
448 return;
|
|
449 }
|
|
450
|
|
451 newname = g_strdup(arg);
|
|
452 // Remove trailing space
|
|
453 for (p = newname; *p; p++) ;
|
|
454 while (p > newname && *p == ' ') *p = 0;
|
|
455
|
|
456 buddy_setname(bud, newname);
|
|
457 jb_updatebuddy(jid, newname, group);
|
|
458
|
|
459 g_free(newname);
|
|
460 update_roster = TRUE;
|
|
461 }
|
|
462
|