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