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