3 #include <readline/readline.h>
4 #include <readline/history.h>
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);
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 }
33 static char *stripwhite (char *string)
37 for (s = string; whitespace(*s); s++)
43 t = s + strlen (s) - 1;
44 while (t > s && whitespace (*t))
51 static char *cmdgenerator(char *text, int state)
53 static int list_index, len;
61 while ((name = cmdlist[list_index].name)) {
63 if (strncmp (name, text, len) == 0)
64 return (strdup(name));
67 /* If no names matched, then return NULL. */
71 static char **cmdcomplete(char *text, int start, int end)
75 matches = (char **)NULL;
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
83 matches = completion_matches(text, cmdgenerator);
88 static Function *cmdfind(char *name)
92 for (i = 0; cmdlist[i].name; i++)
93 if (strcmp (name, cmdlist[i].name) == 0)
94 return cmdlist[i].func;
99 static int cmdexec(char *line)
105 /* Isolate the command word. */
107 while (line[i] && whitespace (line[i]))
111 while (line[i] && !whitespace (line[i]))
117 if (!(cmd = cmdfind(word))) {
118 fprintf(stderr, "%s: invalid command\n", word);
121 /* Get argument to command, if any. */
122 while (whitespace (line[i]))
127 /* Call the function. */
131 static void fullline(void)
135 stripped = stripwhite(rl_line_buffer);
138 add_history(stripped);
148 rl_attempted_completion_function = cmdcomplete;
150 printf("Welcome to faimtest.\n");
152 rl_callback_handler_install("faimtest> ", fullline);
157 void cmd_gotkey(void)
160 rl_callback_read_char();
165 static int cmd_help(char *arg)
169 for (i = 0; cmdlist[i].name; i++)
170 printf("%16s\t\t%s\n", cmdlist[i].name, cmdlist[i].doc);
175 static int cmd_quit(char *arg)
182 static int cmd_login(char *arg)
184 char *sn = NULL, *passwd = NULL;
188 if ((passwd = index(sn, ' '))) {
194 if (login(sn, passwd) != 0)
195 printf("login failed\n");
200 static int cmd_logout(char *arg)
207 static int cmd_connlist(char *arg)
209 struct aim_conn_t *cur;
211 printf("Open connections:\n");
212 for (cur = aimsess.connlist; cur; cur = cur->next) {
213 printf(" fd=%d type=0x%02x\n", cur->fd, cur->type);
219 static int cmd_goodday(char *arg)
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.", strlen("Good day to you too."));
224 printf("no one to say hello to!\n");
229 static int cmd_warn(char *arg)
231 if (arg && strlen(arg) && (strlen(arg) < MAXSNLEN))
232 aim_send_warning(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), arg, 0);
234 printf("no one to warn!\n");
239 static int cmd_anonwarn(char *arg)
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);
244 printf("no one to anonwarn!\n");
249 static int cmd_sendmsg(char *arg)
252 char sn[MAXSNLEN+1], *newbuf = NULL;
254 if ((sscanf(arg, "%d %32s", &len, sn) != 2) ||
255 (len >= 10000) || (strlen(sn) > MAXSNLEN)) {
256 printf("invalid args\n");
260 printf("sending %d bytes to %s\n", len, sn);
262 if (!(newbuf = malloc(len+1)))
265 for (z = 0; z < len; z++)
266 newbuf[z] = (z % 10)+0x30;
269 aim_send_im(&aimsess, aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS), sn, AIM_IMFLAGS_ACK, newbuf, strlen(newbuf));
276 void cmd_uninit(void)
279 rl_callback_handler_remove();