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