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