1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00
profanity/command.c

240 lines
5.0 KiB
C
Raw Normal View History

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-18 18:09:21 -05:00
static int _cmd_start_quit(void);
static int _cmd_start_help(void);
static int _cmd_start_connect(char *inp);
static int _cmd_start_default(char *inp);
2012-02-18 19:17:36 -05:00
static int _valid_command(char *cmd);
2012-02-18 18:09:21 -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);
2012-02-18 19:17:36 -05:00
static int _cmd_msg(char *inp);
static int _cmd_close(char *inp);
static int _cmd_default(char *inp);
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:53:07 -05:00
// handle nothing
if (strlen(inp) == 0) {
gui_refresh();
return AWAIT_COMMAND;
}
2012-02-18 17:51:08 -05:00
// trim input and take a copy
inp = trim(inp);
2012-02-19 15:16:33 -05:00
char inp_cpy[strlen(inp) + 1];
2012-02-18 17:51:08 -05:00
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
if (!_valid_start_command(command)) {
cons_bad_command(command);
gui_refresh();
result = AWAIT_COMMAND;
} else if (strcmp(command, "/quit") == 0) {
2012-02-18 18:09:21 -05:00
result = _cmd_start_quit();
2012-02-18 17:51:08 -05:00
} else if (strcmp(command, "/help") == 0) {
2012-02-18 18:09:21 -05:00
result = _cmd_start_help();
2012-02-18 17:51:08 -05:00
} else if (strcmp(command, "/connect") == 0) {
2012-02-18 18:09:21 -05:00
result = _cmd_start_connect(inp);
2012-02-10 18:53:13 -05:00
} else {
2012-02-18 18:09:21 -05:00
result = _cmd_start_default(inp);
2012-02-10 18:53:13 -05:00
}
2012-02-12 16:47:19 -05:00
inp_clear();
2012-02-10 18:53:13 -05:00
return result;
}
2012-02-18 18:17:01 -05:00
int handle_command(char *inp)
2012-02-09 18:15:53 -05:00
{
int result = FALSE;
2012-02-18 18:17:01 -05:00
// handle nothing
if (strlen(inp) == 0) {
gui_refresh();
return TRUE;
}
2012-02-18 19:17:36 -05:00
// if it was a command, dispatch it
if (inp[0] == '/') {
// trim input and take a copy
inp = trim(inp);
2012-02-19 15:16:33 -05:00
char inp_cpy[strlen(inp) + 1];
2012-02-18 19:17:36 -05:00
strcpy(inp_cpy, inp);
// get the command "/command"
char *command = strtok(inp_cpy, " ");
// if we don't know it, treat it as a message
if (!_valid_command(command)) {
result = _cmd_default(inp);
// otherwise handle it
} else if (strcmp(inp, "/quit") == 0) {
result = _cmd_quit();
} else if (strncmp(inp, "/help", 5) == 0) {
result = _cmd_help();
} else if (strncmp(inp, "/who", 4) == 0) {
result = _cmd_who();
} else if (strncmp(inp, "/msg", 4) == 0) {
result = _cmd_msg(inp);
} else if (strncmp(inp, "/close", 6) == 0) {
result = _cmd_close(inp);
}
// was just text, send it
2012-02-09 18:15:53 -05:00
} else {
2012-02-18 18:17:01 -05:00
result = _cmd_default(inp);
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-18 18:09:21 -05:00
static int _cmd_start_quit(void)
{
return QUIT_PROF;
}
static int _cmd_start_help(void)
{
cons_help();
gui_refresh();
return AWAIT_COMMAND;
}
static int _cmd_start_connect(char *inp)
{
int result = AWAIT_COMMAND;
if (strlen(inp) < 10) {
cons_bad_connect();
gui_refresh();
result = AWAIT_COMMAND;
} else {
char *user;
user = strndup(inp+9, strlen(inp)-9);
status_bar_get_password();
status_bar_refresh();
2012-02-19 15:16:33 -05:00
char passwd[21];
2012-02-18 18:09:21 -05:00
inp_get_password(passwd);
int connect_status = jabber_connect(user, passwd);
if (connect_status == CONNECTING)
result = START_MAIN;
else
result = AWAIT_COMMAND;
}
return result;
}
static int _cmd_start_default(char *inp)
{
cons_bad_command(inp);
gui_refresh();
return AWAIT_COMMAND;
}
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-18 19:17:36 -05:00
static int _cmd_msg(char *inp)
2012-02-09 18:15:53 -05:00
{
char *usr = NULL;
char *msg = NULL;
2012-02-18 19:17:36 -05:00
// copy input
char inp_cpy[100];
strcpy(inp_cpy, inp);
// get user
strtok(inp_cpy, " ");
usr = strtok(NULL, " ");
if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) {
msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1));
2012-02-09 18:15:53 -05:00
if (msg != NULL) {
jabber_send(msg, usr);
2012-02-12 17:09:07 -05:00
win_show_outgoing_msg("me", usr, msg);
2012-02-18 19:17:36 -05:00
} else {
cons_bad_message();
2012-02-09 18:15:53 -05:00
}
2012-02-18 19:17:36 -05:00
} else {
cons_bad_message();
2012-02-09 18:15:53 -05:00
}
return TRUE;
}
2012-02-18 19:17:36 -05:00
static int _cmd_close(char *inp)
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-18 19:17:36 -05:00
cons_bad_command(inp);
2012-02-09 18:15:53 -05:00
}
return TRUE;
}
2012-02-18 19:17:36 -05:00
static int _cmd_default(char *inp)
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-18 19:17:36 -05:00
jabber_send(inp, recipient);
win_show_outgoing_msg("me", recipient, inp);
2012-02-09 18:15:53 -05:00
} else {
2012-02-18 19:17:36 -05:00
cons_bad_command(inp);
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));
}
2012-02-18 19:17:36 -05:00
static int _valid_command(char *cmd)
{
return ((strcmp(cmd, "/quit") == 0) ||
(strcmp(cmd, "/help") == 0) ||
(strcmp(cmd, "/who") == 0) ||
(strcmp(cmd, "/msg") == 0) ||
(strcmp(cmd, "/close") == 0));
}