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