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 |
94
|
33 // Command structure |
|
34 typedef struct { |
|
35 char name[32]; |
95
|
36 const char *help; |
|
37 guint completion_flags[2]; |
94
|
38 void *(*func)(); |
|
39 } cmd; |
|
40 |
95
|
41 static GSList *Commands; |
|
42 |
|
43 // cmd_add() |
|
44 // Adds a command to the commands list and to the CMD completion list |
|
45 void cmd_add(const char *name, const char *help, |
|
46 guint flags_row1, guint flags_row2, void *(*f)()) |
|
47 { |
|
48 cmd *n_cmd = g_new0(cmd, 1); |
|
49 strncpy(n_cmd->name, name, 32-1); |
|
50 n_cmd->help = help; |
|
51 n_cmd->completion_flags[0] = flags_row1; |
|
52 n_cmd->completion_flags[1] = flags_row2; |
|
53 n_cmd->func = f; |
|
54 g_slist_append(Commands, n_cmd); |
|
55 // Add to completion CMD category |
|
56 compl_add_category_word(COMPL_CMD, name); |
|
57 } |
|
58 |
|
59 // cmd_init() |
|
60 // ... |
|
61 void cmd_init(void) |
|
62 { |
|
63 //cmd_add("add"); |
|
64 //cmd_add("clear"); |
|
65 //cmd_add("del"); |
|
66 //cmd_add("group"); |
|
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"); |
|
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"); |
|
87 } |
94
|
88 |
47
|
89 // send_message(msg) |
|
90 // Write the message in the buddy's window and send the message on |
|
91 // the network. |
|
92 void send_message(char *msg) |
|
93 { |
|
94 char *buffer; |
81
|
95 const char *jid; |
|
96 |
|
97 if (!current_buddy) { |
|
98 scr_LogPrint("No buddy currently selected."); |
|
99 return; |
|
100 } |
|
101 |
|
102 jid = CURRENT_JID; |
|
103 if (!jid) { |
|
104 scr_LogPrint("No buddy currently selected."); |
|
105 return; |
|
106 } |
47
|
107 |
|
108 // UI part |
81
|
109 scr_WriteOutgoingMessage(jid, msg); |
47
|
110 |
|
111 // Network part |
|
112 buffer = utf8_encode(msg); |
81
|
113 jb_send_msg(jid, buffer); |
47
|
114 free(buffer); |
|
115 } |
|
116 |
|
117 // process_line(line) |
|
118 // Process a command/message line. |
|
119 // If this isn't a command, this is a message and it is sent to the |
|
120 // currently selected buddy. |
|
121 int process_line(char *line) |
|
122 { |
101
|
123 char *p; |
47
|
124 if (*line != '/') { |
98
|
125 send_message(line); // FIXME: are we talking to a _buddy_? |
47
|
126 return 0; |
|
127 } |
101
|
128 |
|
129 /* It is a command */ |
|
130 // Remove trailing spaces: |
|
131 for (p=line ; *p ; p++) |
|
132 ; |
|
133 for (p-- ; p>line && (*p == ' ') ; p--) |
|
134 *p = 0; |
|
135 |
|
136 // Command "quit"? |
47
|
137 if (!strcasecmp(line, "/quit")) { |
|
138 return 255; |
|
139 } |
|
140 // Commands handling |
|
141 // TODO |
|
142 // say, send_raw... |
|
143 |
|
144 scr_LogPrint("Unrecognised command, sorry."); |
|
145 return 0; |
|
146 } |
|
147 |