]> andersk Git - libfaim.git/blob - utils/faimtest/commands.c
- Sun Oct 14 19:45:54 PDT 2001
[libfaim.git] / utils / faimtest / commands.c
1
2 #include "faimtest.h"
3 #include <readline/readline.h>
4 #include <readline/history.h>
5
6 static int cmd_help(char *arg);
7 static int cmd_quit(char *arg);
8 static int cmd_login(char *arg);
9 static int cmd_logout(char *arg);
10 static int cmd_connlist(char *arg);
11 static int cmd_goodday(char *arg);
12 static int cmd_warn(char *arg);
13 static int cmd_anonwarn(char *arg);
14 static int cmd_sendmsg(char *arg);
15
16 struct {
17         char *name;
18         Function *func;
19         char *doc;
20 } cmdlist[] = {
21         { "help", cmd_help, "Help"},
22         { "quit", cmd_quit, "Quit"},
23         { "login", cmd_login, "Log into AIM"},
24         { "logout", cmd_logout, "Log out of AIM"},
25         { "connlist", cmd_connlist, "List open connections"},
26         { "goodday", cmd_goodday, "Say goodday to arg"},
27         { "warn", cmd_warn, "Warn arg"},
28         { "anonwarn", cmd_anonwarn, "Anonymously warn arg"},
29         { "sendmsg", cmd_sendmsg, "Send arg[0] bytes to arg[1]"},
30         { (char *)NULL, (Function *)NULL, (char *)NULL }
31 };
32
33 static char *stripwhite(char *string)
34 {
35         char *s, *t;
36
37         for (s = string; whitespace(*s); s++)
38                 ;
39
40         if (*s == 0)
41                 return (s);
42
43         t = s + strlen (s) - 1;
44         while (t > s && whitespace (*t))
45                 t--;
46         *++t = '\0';
47
48         return s;
49 }
50
51 static char *cmdgenerator(const char *text, int state)
52 {
53         static int list_index, len;
54         char *name;
55
56         if (!state) {
57                 list_index = 0;
58                 len = strlen (text);
59         }
60
61         while ((name = cmdlist[list_index].name)) {
62                 list_index++;
63                 if (strncmp (name, text, len) == 0)
64                         return (strdup(name));
65         }
66
67         /* If no names matched, then return NULL. */
68         return (char *)NULL;
69 }
70
71 static char **cmdcomplete(const char *text, int start, int end)
72 {
73         char **matches;
74
75         matches = (char **)NULL;
76
77         /* 
78          * If this word is at the start of the line, then it is a command
79          * to complete.  Otherwise it is the name of a file in the current
80          * directory. 
81          */
82         if (start == 0)
83                 matches = completion_matches(text, cmdgenerator);
84
85         return matches;
86 }
87
88 static Function *cmdfind(char *name)
89 {
90         int i;
91
92         for (i = 0; cmdlist[i].name; i++) {
93                 if (strcmp (name, cmdlist[i].name) == 0)
94                         return cmdlist[i].func;
95         }
96
97         return NULL;
98 }
99
100 static int cmdexec(char *line)
101 {
102         int i;
103         Function *cmd;
104         char *word;
105
106         /* Isolate the command word. */
107         i = 0;
108         while (line[i] && whitespace (line[i]))
109                 i++;
110         word = line + i;
111
112         while (line[i] && !whitespace (line[i]))
113                 i++;
114
115         if (line[i])
116                 line[i++] = '\0';
117
118         if (!(cmd = cmdfind(word))) {
119                 fprintf(stderr, "%s: invalid command\n", word);
120                 return -1;
121         }
122         /* Get argument to command, if any. */
123         while (whitespace (line[i]))
124                 i++;
125
126         word = line + i;
127
128         /* Call the function. */
129         return cmd(word);
130 }
131
132 static void fullline(char *x) 
133 {
134         char *stripped;
135
136         stripped = stripwhite(rl_line_buffer);
137
138         if (*stripped) {
139                 add_history(stripped);
140                 cmdexec(stripped);
141         }
142
143         return;
144 }
145
146 void cmd_init(void)
147 {
148
149         rl_attempted_completion_function = cmdcomplete;
150
151         printf("Welcome to faimtest.\n");
152
153         rl_callback_handler_install("faimtest> ", fullline);
154
155         return;
156 }
157
158 void cmd_gotkey(void)
159 {
160
161         rl_callback_read_char();
162
163         return;
164 }
165
166 static int cmd_help(char *arg)
167 {
168         int i;
169
170         for (i = 0; cmdlist[i].name; i++)
171                 printf("%16s\t\t%s\n", cmdlist[i].name, cmdlist[i].doc);
172
173         return 0;
174 }
175
176 static int cmd_quit(char *arg)
177 {
178         keepgoing = 0;
179
180         return 0;
181 }
182
183 static int cmd_login(char *arg)
184 {
185         char *sn = NULL, *passwd = NULL;
186
187         if (arg) {
188                 sn = arg;
189                 if ((passwd = index(sn, ' '))) {
190                         *(passwd) = '\0';
191                         passwd++;
192                 }
193         }
194
195         if (login(&aimsess, sn, passwd) != 0)
196                 printf("login failed\n");
197
198         return 0;
199 }
200
201 static int cmd_logout(char *arg)
202 {
203         logout(&aimsess);
204
205         return 0;
206 }
207
208 static int cmd_connlist(char *arg)
209 {
210         aim_conn_t *cur;
211
212         printf("Open connections:\n");
213         for (cur = aimsess.connlist; cur; cur = cur->next) 
214                 printf(" fd=%d  type=0x%02x\n", cur->fd, cur->type);
215
216         return 0;
217 }
218
219 static int cmd_goodday(char *arg)
220 {
221         if (arg && strlen(arg) && (strlen(arg) < MAXSNLEN))
222                 aim_send_im(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), arg, AIM_IMFLAGS_ACK, "Good day to you too.");
223         else
224                 printf("no one to say hello to!\n");
225
226         return 0;
227 }
228
229 static int cmd_warn(char *arg)
230 {
231         if (arg && strlen(arg) && (strlen(arg) < MAXSNLEN))
232                 aim_send_warning(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), arg, 0);
233         else
234                 printf("no one to warn!\n");
235
236         return 0;
237 }
238
239 static int cmd_anonwarn(char *arg)
240 {
241         if (arg && strlen(arg) && (strlen(arg) < MAXSNLEN))
242                 aim_send_warning(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), arg, AIM_WARN_ANON);
243         else
244                 printf("no one to anonwarn!\n");
245
246         return 0;
247 }
248
249 static int cmd_sendmsg(char *arg)
250 {
251         int len = 0, z;
252         char sn[MAXSNLEN+1], *newbuf = NULL;
253
254         if ((sscanf(arg, "%d %32s", &len, sn) != 2) || 
255                         (len >= 10000) || (strlen(sn) > MAXSNLEN)) {
256                 printf("invalid args\n");
257                 return 0;
258         }
259
260         printf("sending %d bytes to %s\n", len, sn);
261
262         if (!(newbuf = malloc(len+1)))
263                 return 0;
264
265         for (z = 0; z < len; z++)
266                 newbuf[z] = (z % 10)+0x30;
267         newbuf[len] = '\0';
268
269         aim_send_im(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), sn, AIM_IMFLAGS_ACK, newbuf);
270
271         free(newbuf);
272
273         return 0;
274 }
275
276 void cmd_uninit(void)
277 {
278
279         rl_callback_handler_remove();
280
281         return;
282 }
283
284
This page took 0.935553 seconds and 5 git commands to generate.