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"); |
|
66 //cmd_add("group"); |
108
|
67 //cmd_add("help"); |
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"); |
95
|
95 } |
94
|
96 |
102
|
97 // cmd_get |
|
98 // Finds command in the command list structure. |
|
99 // Returns a pointer to the cmd entry, or NULL if command not found. |
|
100 cmd *cmd_get(char *command) |
|
101 { |
|
102 char *p1, *p2; |
|
103 char *com; |
|
104 GSList *sl_com; |
|
105 // Ignore leading '/' |
|
106 for (p1 = command ; *p1 == '/' ; p1++) |
|
107 ; |
|
108 // Locate the end of the command |
|
109 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) |
|
110 ; |
|
111 // Copy the clean command |
|
112 com = g_strndup(p1, p2-p1); |
|
113 |
|
114 // Look for command in the list |
|
115 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) { |
|
116 if (!strcasecmp(com, ((cmd*)sl_com->data)->name)) |
|
117 break; |
|
118 } |
|
119 g_free(com); |
|
120 |
|
121 if (sl_com) // Command has been found. |
|
122 return (cmd*)sl_com->data; |
|
123 return NULL; |
|
124 } |
|
125 |
47
|
126 // send_message(msg) |
|
127 // Write the message in the buddy's window and send the message on |
|
128 // the network. |
|
129 void send_message(char *msg) |
|
130 { |
|
131 char *buffer; |
81
|
132 const char *jid; |
|
133 |
|
134 if (!current_buddy) { |
|
135 scr_LogPrint("No buddy currently selected."); |
|
136 return; |
|
137 } |
|
138 |
|
139 jid = CURRENT_JID; |
|
140 if (!jid) { |
|
141 scr_LogPrint("No buddy currently selected."); |
|
142 return; |
|
143 } |
47
|
144 |
113
|
145 // local part (UI, logging, etc.) |
|
146 hk_message_out(jid, 0, msg); |
47
|
147 |
|
148 // Network part |
|
149 buffer = utf8_encode(msg); |
81
|
150 jb_send_msg(jid, buffer); |
47
|
151 free(buffer); |
|
152 } |
|
153 |
|
154 // process_line(line) |
|
155 // Process a command/message line. |
|
156 // If this isn't a command, this is a message and it is sent to the |
|
157 // currently selected buddy. |
|
158 int process_line(char *line) |
|
159 { |
101
|
160 char *p; |
104
|
161 cmd *curcmd; |
|
162 |
47
|
163 if (*line != '/') { |
98
|
164 send_message(line); // FIXME: are we talking to a _buddy_? |
47
|
165 return 0; |
|
166 } |
101
|
167 |
|
168 /* It is a command */ |
|
169 // Remove trailing spaces: |
|
170 for (p=line ; *p ; p++) |
|
171 ; |
|
172 for (p-- ; p>line && (*p == ' ') ; p--) |
|
173 *p = 0; |
|
174 |
|
175 // Command "quit"? |
116
|
176 if (!strncasecmp(line, "/quit", 5)) |
|
177 if (!line[5] || line[5] == ' ') |
|
178 return 255; |
104
|
179 |
47
|
180 // Commands handling |
104
|
181 curcmd = cmd_get(line); |
47
|
182 |
104
|
183 if (!curcmd) { |
|
184 scr_LogPrint("Unrecognized command, sorry."); |
|
185 return 0; |
|
186 } |
|
187 if (!curcmd->func) { |
|
188 scr_LogPrint("Not yet implemented, sorry."); |
|
189 return 0; |
|
190 } |
|
191 // Lets go to the command parameters |
|
192 for (line++; *line && (*line != ' ') ; line++) |
|
193 ; |
|
194 // Skip spaces |
|
195 while (*line && (*line == ' ')) |
|
196 line++; |
|
197 // Call command-specific function |
|
198 (*curcmd->func)(line); |
47
|
199 return 0; |
|
200 } |
|
201 |
104
|
202 /* Commands callback functions */ |
|
203 |
|
204 void do_roster(char *arg) |
|
205 { |
|
206 if (!strcasecmp(arg, "top")) { |
|
207 scr_RosterTop(); |
|
208 scr_DrawRoster(); |
|
209 } else if (!strcasecmp(arg, "bottom")) { |
|
210 scr_RosterBottom(); |
|
211 scr_DrawRoster(); |
|
212 } else if (!strcasecmp(arg, "hide_offline")) { |
120
|
213 buddylist_set_hide_offline_buddies(TRUE); |
104
|
214 if (current_buddy) |
|
215 buddylist_build(); |
|
216 scr_DrawRoster(); |
|
217 } else if (!strcasecmp(arg, "show_offline")) { |
120
|
218 buddylist_set_hide_offline_buddies(FALSE); |
124
|
219 buddylist_build(); |
104
|
220 scr_DrawRoster(); |
|
221 } else |
|
222 scr_LogPrint("Unrecognized parameter!"); |
|
223 } |
108
|
224 |
|
225 void do_clear(char *arg) |
|
226 { |
|
227 scr_Clear(); |
|
228 } |
|
229 |
116
|
230 void do_status(char *arg) |
|
231 { |
|
232 enum imstatus st; |
|
233 |
|
234 if (!arg || (*arg == 0)) { |
|
235 scr_LogPrint("Your status is: %c", imstatus2char[jb_getstatus()]); |
|
236 return; |
|
237 } |
|
238 |
|
239 if (!strcmp(arg, "offline")) st = offline; |
|
240 else if (!strcmp(arg, "online")) st = available; |
|
241 else if (!strcmp(arg, "avail")) st = available; |
|
242 else if (!strcmp(arg, "away")) st = away; |
|
243 else if (!strcmp(arg, "invisible")) st = invisible; |
|
244 else if (!strcmp(arg, "dnd")) st = dontdisturb; |
|
245 else if (!strcmp(arg, "busy")) st = occupied; |
|
246 else if (!strcmp(arg, "notavail")) st = notavail; |
|
247 else if (!strcmp(arg, "free")) st = freeforchat; |
|
248 else { |
|
249 scr_LogPrint("Unrecognized parameter!"); |
|
250 return; |
|
251 } |
|
252 |
|
253 // XXX special case if offline?? |
|
254 jb_setstatus(st, NULL); // TODO handle message (instead of NULL) |
|
255 } |
|
256 |
124
|
257 void do_add(char *arg) |
|
258 { |
|
259 if (!arg || (*arg == 0)) { |
|
260 scr_LogPrint("Wrong usage"); |
|
261 return; |
|
262 } |
|
263 |
|
264 // FIXME check arg =~ jabber id |
|
265 // 2nd parameter = optional nickname (XXX NULL for now...) |
|
266 jb_addbuddy(arg, NULL); |
|
267 } |