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