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);
|
47
|
44
|
104
|
45 // Global variable for the commands list
|
95
|
46 static GSList *Commands;
|
|
47
|
104
|
48
|
95
|
49 // cmd_add()
|
|
50 // Adds a command to the commands list and to the CMD completion list
|
|
51 void cmd_add(const char *name, const char *help,
|
104
|
52 guint flags_row1, guint flags_row2, void (*f)())
|
95
|
53 {
|
|
54 cmd *n_cmd = g_new0(cmd, 1);
|
|
55 strncpy(n_cmd->name, name, 32-1);
|
|
56 n_cmd->help = help;
|
|
57 n_cmd->completion_flags[0] = flags_row1;
|
|
58 n_cmd->completion_flags[1] = flags_row2;
|
|
59 n_cmd->func = f;
|
102
|
60 Commands = g_slist_append(Commands, n_cmd);
|
95
|
61 // Add to completion CMD category
|
|
62 compl_add_category_word(COMPL_CMD, name);
|
|
63 }
|
|
64
|
|
65 // cmd_init()
|
|
66 // ...
|
|
67 void cmd_init(void)
|
|
68 {
|
124
|
69 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add);
|
187
|
70 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)",
|
|
71 COMPL_BUFFER, 0, &do_buffer);
|
108
|
72 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear);
|
205
|
73 cmd_add("del", "Delete the current buddy", 0, 0, &do_del);
|
128
|
74 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group);
|
127
|
75 cmd_add("help", "Display some help", COMPL_CMD, 0, NULL);
|
191
|
76 cmd_add("info", "Show basic infos on current buddy", 0, 0, &do_info);
|
95
|
77 //cmd_add("move");
|
|
78 //cmd_add("nick");
|
|
79 cmd_add("quit", "Exit the software", 0, 0, NULL);
|
|
80 //cmd_add("rename");
|
|
81 //cmd_add("request_auth");
|
128
|
82 cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0,
|
|
83 &do_roster);
|
132
|
84 cmd_add("say", "Say something to the selected buddy", 0, 0, &do_say);
|
95
|
85 //cmd_add("search");
|
|
86 //cmd_add("send_auth");
|
116
|
87 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status);
|
95
|
88
|
|
89 // Status category
|
|
90 compl_add_category_word(COMPL_STATUS, "online");
|
|
91 compl_add_category_word(COMPL_STATUS, "avail");
|
|
92 compl_add_category_word(COMPL_STATUS, "invisible");
|
|
93 compl_add_category_word(COMPL_STATUS, "free");
|
|
94 compl_add_category_word(COMPL_STATUS, "dnd");
|
|
95 compl_add_category_word(COMPL_STATUS, "busy");
|
|
96 compl_add_category_word(COMPL_STATUS, "notavail");
|
|
97 compl_add_category_word(COMPL_STATUS, "away");
|
103
|
98
|
|
99 // Roster category
|
|
100 compl_add_category_word(COMPL_ROSTER, "bottom");
|
|
101 compl_add_category_word(COMPL_ROSTER, "hide_offline");
|
|
102 compl_add_category_word(COMPL_ROSTER, "show_offline");
|
|
103 compl_add_category_word(COMPL_ROSTER, "top");
|
127
|
104
|
187
|
105 // Roster category
|
|
106 compl_add_category_word(COMPL_BUFFER, "bottom");
|
|
107 compl_add_category_word(COMPL_BUFFER, "clear");
|
|
108 compl_add_category_word(COMPL_BUFFER, "top");
|
|
109
|
127
|
110 // Group category
|
|
111 compl_add_category_word(COMPL_GROUP, "expand");
|
|
112 compl_add_category_word(COMPL_GROUP, "shrink");
|
130
|
113 compl_add_category_word(COMPL_GROUP, "toggle");
|
95
|
114 }
|
94
|
115
|
102
|
116 // cmd_get
|
|
117 // Finds command in the command list structure.
|
|
118 // Returns a pointer to the cmd entry, or NULL if command not found.
|
|
119 cmd *cmd_get(char *command)
|
|
120 {
|
|
121 char *p1, *p2;
|
|
122 char *com;
|
|
123 GSList *sl_com;
|
|
124 // Ignore leading '/'
|
|
125 for (p1 = command ; *p1 == '/' ; p1++)
|
|
126 ;
|
|
127 // Locate the end of the command
|
|
128 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
|
|
129 ;
|
|
130 // Copy the clean command
|
|
131 com = g_strndup(p1, p2-p1);
|
|
132
|
|
133 // Look for command in the list
|
|
134 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
|
|
135 if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
|
|
136 break;
|
|
137 }
|
|
138 g_free(com);
|
|
139
|
|
140 if (sl_com) // Command has been found.
|
|
141 return (cmd*)sl_com->data;
|
|
142 return NULL;
|
|
143 }
|
|
144
|
47
|
145 // send_message(msg)
|
|
146 // Write the message in the buddy's window and send the message on
|
|
147 // the network.
|
|
148 void send_message(char *msg)
|
|
149 {
|
|
150 char *buffer;
|
81
|
151 const char *jid;
|
|
152
|
|
153 if (!current_buddy) {
|
|
154 scr_LogPrint("No buddy currently selected.");
|
|
155 return;
|
|
156 }
|
|
157
|
|
158 jid = CURRENT_JID;
|
|
159 if (!jid) {
|
|
160 scr_LogPrint("No buddy currently selected.");
|
|
161 return;
|
|
162 }
|
47
|
163
|
113
|
164 // local part (UI, logging, etc.)
|
|
165 hk_message_out(jid, 0, msg);
|
47
|
166
|
|
167 // Network part
|
|
168 buffer = utf8_encode(msg);
|
81
|
169 jb_send_msg(jid, buffer);
|
47
|
170 free(buffer);
|
|
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));
|
|
322 leave_windowbuddy = (group != BUDDATA(current_buddy));
|
|
323
|
128
|
324 if (!(buddy_gettype(group) & ROSTER_TYPE_GROUP)) {
|
166
|
325 scr_LogPrint("You need to select a group");
|
128
|
326 return;
|
|
327 }
|
166
|
328
|
|
329 if (!strcasecmp(arg, "expand") || !strcasecmp(arg, "unfold")) {
|
128
|
330 buddy_setflags(group, ROSTER_FLAG_HIDE, FALSE);
|
166
|
331 } else if (!strcasecmp(arg, "shrink") || !strcasecmp(arg, "fold")) {
|
128
|
332 buddy_setflags(group, ROSTER_FLAG_HIDE, TRUE);
|
130
|
333 } else if (!strcasecmp(arg, "toggle")) {
|
|
334 buddy_setflags(group, ROSTER_FLAG_HIDE,
|
|
335 !(buddy_getflags(group) & ROSTER_FLAG_HIDE));
|
128
|
336 } else {
|
|
337 scr_LogPrint("Unrecognized parameter!");
|
|
338 return;
|
|
339 }
|
|
340
|
|
341 buddylist_build();
|
|
342 update_roster = TRUE;
|
166
|
343 if (leave_windowbuddy) scr_ShowBuddyWindow();
|
128
|
344 }
|
|
345
|
132
|
346 void do_say(char *arg)
|
|
347 {
|
164
|
348 gpointer bud;
|
132
|
349
|
|
350 scr_set_chatmode(TRUE);
|
164
|
351
|
|
352 if (!current_buddy) {
|
132
|
353 scr_LogPrint("Who are you talking to??");
|
164
|
354 return;
|
132
|
355 }
|
164
|
356
|
|
357 bud = BUDDATA(current_buddy);
|
|
358 if (!(buddy_gettype(bud) & ROSTER_TYPE_USER)) {
|
|
359 scr_LogPrint("This is not a user");
|
|
360 return;
|
|
361 }
|
|
362
|
|
363 buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
|
|
364 send_message(arg);
|
132
|
365 }
|
|
366
|
187
|
367 void do_buffer(char *arg)
|
|
368 {
|
|
369 if (!strcasecmp(arg, "top")) {
|
|
370 scr_BufferTop();
|
|
371 } else if (!strcasecmp(arg, "bottom")) {
|
|
372 scr_BufferBottom();
|
|
373 } else if (!strcasecmp(arg, "clear")) {
|
|
374 scr_Clear();
|
|
375 } else
|
|
376 scr_LogPrint("Unrecognized parameter!");
|
|
377 }
|
|
378
|
|
379 void do_clear(char *arg) // Alias for "/buffer clear"
|
|
380 {
|
|
381 do_buffer("clear");
|
|
382 }
|
|
383
|
191
|
384 void do_info(char *arg)
|
|
385 {
|
|
386 gpointer bud;
|
|
387 const char *jid, *name;
|
|
388 guint type;
|
|
389 enum imstatus status;
|
|
390 char *buffer;
|
|
391
|
|
392 if (!current_buddy) return;
|
|
393 bud = BUDDATA(current_buddy);
|
|
394
|
|
395 jid = buddy_getjid(bud);
|
|
396 name = buddy_getname(bud);
|
|
397 type = buddy_gettype(bud);
|
|
398 status = buddy_getstatus(bud);
|
|
399
|
|
400 buffer = g_new(char, 128);
|
|
401
|
|
402 if (jid) {
|
|
403 char *typestr = "unknown";
|
|
404
|
|
405 snprintf(buffer, 127, "jid: <%s>", jid);
|
|
406 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
407 if (name) {
|
|
408 snprintf(buffer, 127, "Name: %s", name);
|
|
409 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
410 }
|
|
411
|
|
412 if (type == ROSTER_TYPE_USER) typestr = "user";
|
|
413 else if (type == ROSTER_TYPE_AGENT) typestr = "agent";
|
|
414
|
|
415 snprintf(buffer, 127, "Type: %s", typestr);
|
|
416 scr_WriteIncomingMessage(jid, buffer, 0, HBB_PREFIX_INFO);
|
|
417 } else {
|
|
418 if (name) scr_LogPrint("Name: %s", name);
|
|
419 scr_LogPrint("Type: %s",
|
|
420 ((type == ROSTER_TYPE_GROUP) ? "group" : "unknown"));
|
|
421 }
|
|
422
|
|
423 g_free(buffer);
|
|
424 }
|
|
425
|