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