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");
|
|
65 cmd_add("say", "Say something to the selected buddy", 0, 0, NULL);
|
|
66 //cmd_add("search");
|
|
67 //cmd_add("send_auth");
|
|
68 cmd_add("status", "Show or set your status", COMPL_STATUS, 0, NULL);
|
|
69
|
|
70 // Status category
|
|
71 compl_add_category_word(COMPL_STATUS, "online");
|
|
72 compl_add_category_word(COMPL_STATUS, "avail");
|
|
73 compl_add_category_word(COMPL_STATUS, "invisible");
|
|
74 compl_add_category_word(COMPL_STATUS, "free");
|
|
75 compl_add_category_word(COMPL_STATUS, "dnd");
|
|
76 compl_add_category_word(COMPL_STATUS, "busy");
|
|
77 compl_add_category_word(COMPL_STATUS, "notavail");
|
|
78 compl_add_category_word(COMPL_STATUS, "away");
|
|
79 }
|
94
|
80
|
102
|
81 // cmd_get
|
|
82 // Finds command in the command list structure.
|
|
83 // Returns a pointer to the cmd entry, or NULL if command not found.
|
|
84 cmd *cmd_get(char *command)
|
|
85 {
|
|
86 char *p1, *p2;
|
|
87 char *com;
|
|
88 GSList *sl_com;
|
|
89 // Ignore leading '/'
|
|
90 for (p1 = command ; *p1 == '/' ; p1++)
|
|
91 ;
|
|
92 // Locate the end of the command
|
|
93 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
|
|
94 ;
|
|
95 // Copy the clean command
|
|
96 com = g_strndup(p1, p2-p1);
|
|
97
|
|
98 // Look for command in the list
|
|
99 for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
|
|
100 if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
|
|
101 break;
|
|
102 }
|
|
103 g_free(com);
|
|
104
|
|
105 if (sl_com) // Command has been found.
|
|
106 return (cmd*)sl_com->data;
|
|
107 return NULL;
|
|
108 }
|
|
109
|
47
|
110 // send_message(msg)
|
|
111 // Write the message in the buddy's window and send the message on
|
|
112 // the network.
|
|
113 void send_message(char *msg)
|
|
114 {
|
|
115 char *buffer;
|
81
|
116 const char *jid;
|
|
117
|
|
118 if (!current_buddy) {
|
|
119 scr_LogPrint("No buddy currently selected.");
|
|
120 return;
|
|
121 }
|
|
122
|
|
123 jid = CURRENT_JID;
|
|
124 if (!jid) {
|
|
125 scr_LogPrint("No buddy currently selected.");
|
|
126 return;
|
|
127 }
|
47
|
128
|
|
129 // UI part
|
81
|
130 scr_WriteOutgoingMessage(jid, msg);
|
47
|
131
|
|
132 // Network part
|
|
133 buffer = utf8_encode(msg);
|
81
|
134 jb_send_msg(jid, buffer);
|
47
|
135 free(buffer);
|
|
136 }
|
|
137
|
|
138 // process_line(line)
|
|
139 // Process a command/message line.
|
|
140 // If this isn't a command, this is a message and it is sent to the
|
|
141 // currently selected buddy.
|
|
142 int process_line(char *line)
|
|
143 {
|
101
|
144 char *p;
|
47
|
145 if (*line != '/') {
|
98
|
146 send_message(line); // FIXME: are we talking to a _buddy_?
|
47
|
147 return 0;
|
|
148 }
|
101
|
149
|
|
150 /* It is a command */
|
|
151 // Remove trailing spaces:
|
|
152 for (p=line ; *p ; p++)
|
|
153 ;
|
|
154 for (p-- ; p>line && (*p == ' ') ; p--)
|
|
155 *p = 0;
|
|
156
|
|
157 // Command "quit"?
|
47
|
158 if (!strcasecmp(line, "/quit")) {
|
|
159 return 255;
|
|
160 }
|
|
161 // Commands handling
|
|
162 // TODO
|
|
163 // say, send_raw...
|
|
164
|
|
165 scr_LogPrint("Unrecognised command, sorry.");
|
|
166 return 0;
|
|
167 }
|
|
168
|