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" |
81
|
30 |
#include "utf8.h" |
47
|
31 |
#include "utils.h" |
|
32 |
|
104
|
33 |
// Commands callbacks |
|
34 |
void do_roster(char *arg); |
108
|
35 |
void do_clear(char *arg); |
116
|
36 |
void do_status(char *arg); |
124
|
37 |
void do_add(char *arg); |
47
|
38 |
|
104
|
39 |
// Global variable for the commands list |
95
|
40 |
static GSList *Commands; |
|
41 |
|
104
|
42 |
|
95
|
43 |
// cmd_add() |
|
44 |
// Adds a command to the commands list and to the CMD completion list |
|
45 |
void cmd_add(const char *name, const char *help, |
104
|
46 |
guint flags_row1, guint flags_row2, void (*f)()) |
95
|
47 |
{ |
|
48 |
cmd *n_cmd = g_new0(cmd, 1); |
|
49 |
strncpy(n_cmd->name, name, 32-1); |
|
50 |
n_cmd->help = help; |
|
51 |
n_cmd->completion_flags[0] = flags_row1; |
|
52 |
n_cmd->completion_flags[1] = flags_row2; |
|
53 |
n_cmd->func = f; |
102
|
54 |
Commands = g_slist_append(Commands, n_cmd); |
95
|
55 |
// Add to completion CMD category |
|
56 |
compl_add_category_word(COMPL_CMD, name); |
|
57 |
} |
|
58 |
|
|
59 |
// cmd_init() |
|
60 |
// ... |
|
61 |
void cmd_init(void) |
|
62 |
{ |
124
|
63 |
cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); |
108
|
64 |
cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); |
95
|
65 |
//cmd_add("del"); |
127
|
66 |
cmd_add("group", "Change group display settings", COMPL_GROUP, 0, NULL); |
|
67 |
cmd_add("help", "Display some help", COMPL_CMD, 0, NULL); |
95
|
68 |
//cmd_add("info"); |
|
69 |
//cmd_add("move"); |
|
70 |
//cmd_add("nick"); |
|
71 |
cmd_add("quit", "Exit the software", 0, 0, NULL); |
|
72 |
//cmd_add("rename"); |
|
73 |
//cmd_add("request_auth"); |
104
|
74 |
cmd_add("roster", "Manipulate the roster/buddylist", COMPL_ROSTER, 0, &do_roster); |
95
|
75 |
cmd_add("say", "Say something to the selected buddy", 0, 0, NULL); |
|
76 |
//cmd_add("search"); |
|
77 |
//cmd_add("send_auth"); |
116
|
78 |
cmd_add("status", "Show or set your status", COMPL_STATUS, 0, &do_status); |
95
|
79 |
|
|
80 |
// Status category |
|
81 |
compl_add_category_word(COMPL_STATUS, "online"); |
|
82 |
compl_add_category_word(COMPL_STATUS, "avail"); |
|
83 |
compl_add_category_word(COMPL_STATUS, "invisible"); |
|
84 |
compl_add_category_word(COMPL_STATUS, "free"); |
|
85 |
compl_add_category_word(COMPL_STATUS, "dnd"); |
|
86 |
compl_add_category_word(COMPL_STATUS, "busy"); |
|
87 |
compl_add_category_word(COMPL_STATUS, "notavail"); |
|
88 |
compl_add_category_word(COMPL_STATUS, "away"); |
103
|
89 |
|
|
90 |
// Roster category |
|
91 |
compl_add_category_word(COMPL_ROSTER, "bottom"); |
|
92 |
compl_add_category_word(COMPL_ROSTER, "hide_offline"); |
|
93 |
compl_add_category_word(COMPL_ROSTER, "show_offline"); |
|
94 |
compl_add_category_word(COMPL_ROSTER, "top"); |
127
|
95 |
|
|
96 |
// Group category |
|
97 |
compl_add_category_word(COMPL_GROUP, "expand"); |
|
98 |
compl_add_category_word(COMPL_GROUP, "shrink"); |
95
|
99 |
} |
94
|
100 |
|
102
|
101 |
// cmd_get |
|
102 |
// Finds command in the command list structure. |
|
103 |
// Returns a pointer to the cmd entry, or NULL if command not found. |
|
104 |
cmd *cmd_get(char *command) |
|
105 |
{ |
|
106 |
char *p1, *p2; |
|
107 |
char *com; |
|
108 |
GSList *sl_com; |
|
109 |
// Ignore leading '/' |
|
110 |
for (p1 = command ; *p1 == '/' ; p1++) |
|
111 |
; |
|
112 |
// Locate the end of the command |
|
113 |
for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) |
|
114 |
; |
|
115 |
// Copy the clean command |
|
116 |
com = g_strndup(p1, p2-p1); |
|
117 |
|
|
118 |
// Look for command in the list |
|
119 |
for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) { |
|
120 |
if (!strcasecmp(com, ((cmd*)sl_com->data)->name)) |
|
121 |
break; |
|
122 |
} |
|
123 |
g_free(com); |
|
124 |
|
|
125 |
if (sl_com) // Command has been found. |
|
126 |
return (cmd*)sl_com->data; |
|
127 |
return NULL; |
|
128 |
} |
|
129 |
|
47
|
130 |
// send_message(msg) |
|
131 |
// Write the message in the buddy's window and send the message on |
|
132 |
// the network. |
|
133 |
void send_message(char *msg) |
|
134 |
{ |
|
135 |
char *buffer; |
81
|
136 |
const char *jid; |
|
137 |
|
|
138 |
if (!current_buddy) { |
|
139 |
scr_LogPrint("No buddy currently selected."); |
|
140 |
return; |
|
141 |
} |
|
142 |
|
|
143 |
jid = CURRENT_JID; |
|
144 |
if (!jid) { |
|
145 |
scr_LogPrint("No buddy currently selected."); |
|
146 |
return; |
|
147 |
} |
47
|
148 |
|
113
|
149 |
// local part (UI, logging, etc.) |
|
150 |
hk_message_out(jid, 0, msg); |
47
|
151 |
|
|
152 |
// Network part |
|
153 |
buffer = utf8_encode(msg); |
81
|
154 |
jb_send_msg(jid, buffer); |
47
|
155 |
free(buffer); |
|
156 |
} |
|
157 |
|
|
158 |
// process_line(line) |
|
159 |
// Process a command/message line. |
|
160 |
// If this isn't a command, this is a message and it is sent to the |
|
161 |
// currently selected buddy. |
|
162 |
int process_line(char *line) |
|
163 |
{ |
101
|
164 |
char *p; |
104
|
165 |
cmd *curcmd; |
|
166 |
|
47
|
167 |
if (*line != '/') { |
98
|
168 |
send_message(line); // FIXME: are we talking to a _buddy_? |
47
|
169 |
return 0; |
|
170 |
} |
101
|
171 |
|
|
172 |
/* It is a command */ |
|
173 |
// Remove trailing spaces: |
|
174 |
for (p=line ; *p ; p++) |
|
175 |
; |
|
176 |
for (p-- ; p>line && (*p == ' ') ; p--) |
|
177 |
*p = 0; |
|
178 |
|
|
179 |
// Command "quit"? |
116
|
180 |
if (!strncasecmp(line, "/quit", 5)) |
|
181 |
if (!line[5] || line[5] == ' ') |
|
182 |
return 255; |
104
|
183 |
|
47
|
184 |
// Commands handling |
104
|
185 |
curcmd = cmd_get(line); |
47
|
186 |
|
104
|
187 |
if (!curcmd) { |
|
188 |
scr_LogPrint("Unrecognized command, sorry."); |
|
189 |
return 0; |
|
190 |
} |
|
191 |
if (!curcmd->func) { |
|
192 |
scr_LogPrint("Not yet implemented, sorry."); |
|
193 |
return 0; |
|
194 |
} |
|
195 |
// Lets go to the command parameters |
|
196 |
for (line++; *line && (*line != ' ') ; line++) |
|
197 |
; |
|
198 |
// Skip spaces |
|
199 |
while (*line && (*line == ' ')) |
|
200 |
line++; |
|
201 |
// Call command-specific function |
|
202 |
(*curcmd->func)(line); |
47
|
203 |
return 0; |
|
204 |
} |
|
205 |
|
104
|
206 |
/* Commands callback functions */ |
|
207 |
|
|
208 |
void do_roster(char *arg) |
|
209 |
{ |
|
210 |
if (!strcasecmp(arg, "top")) { |
|
211 |
scr_RosterTop(); |
|
212 |
scr_DrawRoster(); |
|
213 |
} else if (!strcasecmp(arg, "bottom")) { |
|
214 |
scr_RosterBottom(); |
|
215 |
scr_DrawRoster(); |
|
216 |
} else if (!strcasecmp(arg, "hide_offline")) { |
120
|
217 |
buddylist_set_hide_offline_buddies(TRUE); |
104
|
218 |
if (current_buddy) |
|
219 |
buddylist_build(); |
|
220 |
scr_DrawRoster(); |
|
221 |
} else if (!strcasecmp(arg, "show_offline")) { |
120
|
222 |
buddylist_set_hide_offline_buddies(FALSE); |
124
|
223 |
buddylist_build(); |
104
|
224 |
scr_DrawRoster(); |
|
225 |
} else |
|
226 |
scr_LogPrint("Unrecognized parameter!"); |
|
227 |
} |
108
|
228 |
|
|
229 |
void do_clear(char *arg) |
|
230 |
{ |
|
231 |
scr_Clear(); |
|
232 |
} |
|
233 |
|
116
|
234 |
void do_status(char *arg) |
|
235 |
{ |
|
236 |
enum imstatus st; |
|
237 |
|
|
238 |
if (!arg || (*arg == 0)) { |
|
239 |
scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]); |
|
240 |
return; |
|
241 |
} |
|
242 |
|
|
243 |
if (!strcmp(arg, "offline")) st = offline; |
|
244 |
else if (!strcmp(arg, "online")) st = available; |
|
245 |
else if (!strcmp(arg, "avail")) st = available; |
|
246 |
else if (!strcmp(arg, "away")) st = away; |
|
247 |
else if (!strcmp(arg, "invisible")) st = invisible; |
|
248 |
else if (!strcmp(arg, "dnd")) st = dontdisturb; |
|
249 |
else if (!strcmp(arg, "busy")) st = occupied; |
|
250 |
else if (!strcmp(arg, "notavail")) st = notavail; |
|
251 |
else if (!strcmp(arg, "free")) st = freeforchat; |
|
252 |
else { |
|
253 |
scr_LogPrint("Unrecognized parameter!"); |
|
254 |
return; |
|
255 |
} |
|
256 |
|
|
257 |
// XXX special case if offline?? |
|
258 |
jb_setstatus(st, NULL); // TODO handle message (instead of NULL) |
|
259 |
} |
|
260 |
|
124
|
261 |
void do_add(char *arg) |
|
262 |
{ |
|
263 |
if (!arg || (*arg == 0)) { |
|
264 |
scr_LogPrint("Wrong usage"); |
|
265 |
return; |
|
266 |
} |
|
267 |
|
|
268 |
// FIXME check arg =~ jabber id |
|
269 |
// 2nd parameter = optional nickname (XXX NULL for now...) |
|
270 |
jb_addbuddy(arg, NULL); |
|
271 |
} |