1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-29 19:56:07 -04:00
profanity/app.c

167 lines
4.2 KiB
C
Raw Normal View History

2012-02-06 19:08:59 -05:00
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include "log.h"
#include "windows.h"
#include "jabber.h"
2012-02-09 17:16:27 -05:00
#define AWAIT_COMMAND 1
#define START_MAIN 2
#define QUIT_PROF 3
2012-02-06 19:08:59 -05:00
static void main_event_loop(void);
2012-02-09 17:16:27 -05:00
static int handle_pre_command(char *cmd);
2012-02-06 19:08:59 -05:00
void start_profanity(void)
{
2012-02-09 17:16:27 -05:00
int cmd_result = AWAIT_COMMAND;
2012-02-06 19:08:59 -05:00
char cmd[50];
2012-02-09 17:16:27 -05:00
while (cmd_result == AWAIT_COMMAND) {
inp_get_command_str(cmd);
cmd_result = handle_pre_command(cmd);
}
2012-02-06 19:08:59 -05:00
2012-02-09 17:16:27 -05:00
if (cmd_result == START_MAIN) {
main_event_loop();
}
}
2012-02-06 19:08:59 -05:00
2012-02-09 17:16:27 -05:00
static int handle_pre_command(char *cmd)
{
int result;
if (strcmp(cmd, "/quit") == 0) {
result = QUIT_PROF;
} else if (strncmp(cmd, "/help", 5) == 0) {
cons_help();
inp_clear();
result = AWAIT_COMMAND;
} else if (strncmp(cmd, "/connect ", 9) == 0) {
char *user;
user = strndup(cmd+9, strlen(cmd)-9);
inp_bar_print_message("Enter password:");
char passwd[20];
inp_get_password(passwd);
inp_bar_print_message(user);
jabber_connect(user, passwd);
result = START_MAIN;
} else {
cons_bad_command(cmd);
inp_clear();
result = AWAIT_COMMAND;
2012-02-06 19:08:59 -05:00
}
2012-02-09 17:16:27 -05:00
return result;
2012-02-06 19:08:59 -05:00
}
static void main_event_loop(void)
{
inp_non_block();
2012-02-06 19:08:59 -05:00
while(TRUE) {
int ch = ERR;
char command[100];
int size = 0;
2012-02-06 19:37:42 -05:00
// while not enter, process all events, and try to get another char
2012-02-06 19:08:59 -05:00
while(ch != '\n') {
usleep(1);
2012-02-06 19:37:42 -05:00
2012-02-06 19:08:59 -05:00
// handle incoming messages
jabber_process_events();
// determine if they changed windows
if (ch == KEY_F(1)) {
switch_to(0);
} else if (ch == KEY_F(2)) {
switch_to(1);
} else if (ch == KEY_F(3)) {
switch_to(2);
} else if (ch == KEY_F(4)) {
switch_to(3);
} else if (ch == KEY_F(5)) {
switch_to(4);
} else if (ch == KEY_F(6)) {
switch_to(5);
} else if (ch == KEY_F(7)) {
switch_to(6);
} else if (ch == KEY_F(8)) {
switch_to(7);
} else if (ch == KEY_F(9)) {
switch_to(8);
} else if (ch == KEY_F(10)) {
switch_to(9);
}
2012-02-06 19:37:42 -05:00
// get another character from the command box
inp_poll_char(&ch, command, &size);
2012-02-06 19:08:59 -05:00
}
2012-02-06 19:37:42 -05:00
// null terminate the input
2012-02-06 19:08:59 -05:00
command[size++] = '\0';
2012-02-08 17:46:35 -05:00
// deal with input
// /quit command -> exit
2012-02-06 19:08:59 -05:00
if (strcmp(command, "/quit") == 0) {
break;
2012-02-08 17:46:35 -05:00
// /help -> print help to console
} else if (strncmp(command, "/help", 5) == 0) {
cons_help();
inp_clear();
2012-02-08 17:46:35 -05:00
2012-02-08 21:47:25 -05:00
// /who -> request roster
} else if (strncmp(command, "/who", 4) == 0) {
jabber_roster_request();
inp_clear();
2012-02-09 16:45:31 -05:00
// /msg -> send message to a user
} else if (strncmp(command, "/msg ", 5) == 0) {
char *usr_msg = NULL;
char *usr = NULL;
char *msg = NULL;
usr_msg = strndup(command+5, strlen(command)-5);
usr = strtok(usr_msg, " ");
if (usr != NULL) {
msg = strndup(command+5+strlen(usr)+1, strlen(command)-(5+strlen(usr)+1));
if (msg != NULL) {
jabber_send(msg, usr);
show_outgoing_msg("me", usr, msg);
}
}
inp_clear();
2012-02-08 18:12:34 -05:00
// /close -> close the current chat window, if in chat
} else if (strncmp(command, "/close", 6) == 0) {
if (in_chat()) {
close_win();
} else {
cons_bad_command(command);
}
inp_clear();
// send message to recipient, if in chat
2012-02-06 19:08:59 -05:00
} else {
2012-02-08 17:46:35 -05:00
if (in_chat()) {
char recipient[100] = "";
get_recipient(recipient);
jabber_send(command, recipient);
2012-02-09 16:45:31 -05:00
show_outgoing_msg("me", recipient, command);
2012-02-08 18:12:34 -05:00
} else {
cons_bad_command(command);
2012-02-08 17:46:35 -05:00
}
2012-02-08 18:12:34 -05:00
inp_clear();
2012-02-06 19:08:59 -05:00
}
}
jabber_disconnect();
}