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