2012-02-09 18:15:53 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
#include "command.h"
|
|
|
|
#include "jabber.h"
|
|
|
|
#include "windows.h"
|
2012-02-18 17:51:08 -05:00
|
|
|
#include "util.h"
|
2012-02-09 18:15:53 -05:00
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_quit(void);
|
|
|
|
static int _cmd_help(void);
|
|
|
|
static int _cmd_who(void);
|
|
|
|
static int _cmd_msg(char *cmd);
|
|
|
|
static int _cmd_close(char *cmd);
|
|
|
|
static int _cmd_default(char *cmd);
|
2012-02-18 17:51:08 -05:00
|
|
|
static int _valid_start_command(char *cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
|
2012-02-18 17:51:08 -05:00
|
|
|
int handle_start_command(char *inp)
|
2012-02-10 18:53:13 -05:00
|
|
|
{
|
|
|
|
int result;
|
2012-02-18 17:51:08 -05:00
|
|
|
|
|
|
|
// trim input and take a copy
|
|
|
|
char inp_cpy[100];
|
|
|
|
inp = trim(inp);
|
|
|
|
strcpy(inp_cpy, inp);
|
|
|
|
|
|
|
|
// get the command "/command"
|
|
|
|
char *command = strtok(inp_cpy, " ");
|
2012-02-10 18:53:13 -05:00
|
|
|
|
2012-02-18 17:51:08 -05:00
|
|
|
// handle invalid commands
|
|
|
|
if (!_valid_start_command(command)) {
|
|
|
|
cons_bad_command(command);
|
|
|
|
gui_refresh();
|
|
|
|
result = AWAIT_COMMAND;
|
|
|
|
|
|
|
|
// quit
|
|
|
|
} else if (strcmp(command, "/quit") == 0) {
|
2012-02-10 18:53:13 -05:00
|
|
|
result = QUIT_PROF;
|
2012-02-18 17:51:08 -05:00
|
|
|
|
|
|
|
// help
|
|
|
|
} else if (strcmp(command, "/help") == 0) {
|
2012-02-12 17:23:51 -05:00
|
|
|
cons_help();
|
2012-02-16 19:42:41 -05:00
|
|
|
gui_refresh();
|
2012-02-10 18:53:13 -05:00
|
|
|
result = AWAIT_COMMAND;
|
2012-02-18 17:51:08 -05:00
|
|
|
|
|
|
|
// connect
|
|
|
|
} else if (strcmp(command, "/connect") == 0) {
|
|
|
|
if (strlen(inp) < 10) {
|
|
|
|
cons_bad_connect();
|
|
|
|
gui_refresh();
|
2012-02-16 19:42:41 -05:00
|
|
|
result = AWAIT_COMMAND;
|
2012-02-18 17:51:08 -05:00
|
|
|
} else {
|
|
|
|
char *user;
|
|
|
|
user = strndup(inp+9, strlen(inp)-9);
|
|
|
|
|
|
|
|
status_bar_get_password();
|
|
|
|
status_bar_refresh();
|
|
|
|
char passwd[20];
|
|
|
|
inp_get_password(passwd);
|
|
|
|
int connect_status = jabber_connect(user, passwd);
|
|
|
|
if (connect_status == CONNECTING)
|
|
|
|
result = START_MAIN;
|
|
|
|
else
|
|
|
|
result = AWAIT_COMMAND;
|
|
|
|
}
|
2012-02-10 18:53:13 -05:00
|
|
|
} else {
|
2012-02-18 17:51:08 -05:00
|
|
|
cons_bad_command(inp);
|
2012-02-16 19:42:41 -05:00
|
|
|
gui_refresh();
|
2012-02-10 18:53:13 -05:00
|
|
|
result = AWAIT_COMMAND;
|
|
|
|
}
|
|
|
|
|
2012-02-12 16:47:19 -05:00
|
|
|
inp_clear();
|
|
|
|
|
2012-02-10 18:53:13 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:15:53 -05:00
|
|
|
int handle_command(char *cmd)
|
|
|
|
{
|
|
|
|
int result = FALSE;
|
|
|
|
if (strcmp(cmd, "/quit") == 0) {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_quit();
|
2012-02-09 18:15:53 -05:00
|
|
|
} else if (strncmp(cmd, "/help", 5) == 0) {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_help();
|
2012-02-09 18:15:53 -05:00
|
|
|
} else if (strncmp(cmd, "/who", 4) == 0) {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_who();
|
2012-02-09 18:15:53 -05:00
|
|
|
} else if (strncmp(cmd, "/msg ", 5) == 0) {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_msg(cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
} else if (strncmp(cmd, "/close", 6) == 0) {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_close(cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
} else {
|
2012-02-12 17:09:07 -05:00
|
|
|
result = _cmd_default(cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
}
|
|
|
|
|
2012-02-12 16:47:19 -05:00
|
|
|
inp_clear();
|
|
|
|
|
2012-02-09 18:15:53 -05:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_quit(void)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_help(void)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
2012-02-12 17:23:51 -05:00
|
|
|
cons_help();
|
2012-02-09 18:15:53 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_who(void)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
|
|
|
jabber_roster_request();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_msg(char *cmd)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
|
|
|
char *usr_msg = NULL;
|
|
|
|
char *usr = NULL;
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
usr_msg = strndup(cmd+5, strlen(cmd)-5);
|
|
|
|
usr = strtok(usr_msg, " ");
|
|
|
|
if (usr != NULL) {
|
|
|
|
msg = strndup(cmd+5+strlen(usr)+1, strlen(cmd)-(5+strlen(usr)+1));
|
|
|
|
if (msg != NULL) {
|
|
|
|
jabber_send(msg, usr);
|
2012-02-12 17:09:07 -05:00
|
|
|
win_show_outgoing_msg("me", usr, msg);
|
2012-02-09 18:15:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_close(char *cmd)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
2012-02-12 17:09:07 -05:00
|
|
|
if (win_in_chat()) {
|
|
|
|
win_close_win();
|
2012-02-09 18:15:53 -05:00
|
|
|
} else {
|
2012-02-12 17:23:51 -05:00
|
|
|
cons_bad_command(cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-12 17:09:07 -05:00
|
|
|
static int _cmd_default(char *cmd)
|
2012-02-09 18:15:53 -05:00
|
|
|
{
|
2012-02-12 17:09:07 -05:00
|
|
|
if (win_in_chat()) {
|
2012-02-09 18:15:53 -05:00
|
|
|
char recipient[100] = "";
|
2012-02-12 17:09:07 -05:00
|
|
|
win_get_recipient(recipient);
|
2012-02-09 18:15:53 -05:00
|
|
|
jabber_send(cmd, recipient);
|
2012-02-12 17:09:07 -05:00
|
|
|
win_show_outgoing_msg("me", recipient, cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
} else {
|
2012-02-12 17:23:51 -05:00
|
|
|
cons_bad_command(cmd);
|
2012-02-09 18:15:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-02-18 17:51:08 -05:00
|
|
|
|
|
|
|
static int _valid_start_command(char *cmd)
|
|
|
|
{
|
|
|
|
return ((strcmp(cmd, "/quit") == 0) ||
|
|
|
|
(strcmp(cmd, "/help") == 0) ||
|
|
|
|
(strcmp(cmd, "/connect") == 0));
|
|
|
|
}
|