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