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

175 lines
4.1 KiB
C
Raw Normal View History

2012-02-20 15:07:38 -05:00
/*
* profanity.c
*
* Copyright (C) 2012 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
2012-08-25 20:50:50 -04:00
#include "config.h"
2012-08-25 20:50:50 -04:00
#include <stdlib.h>
#include <string.h>
2012-02-09 17:24:03 -05:00
2012-07-22 15:53:55 -04:00
#include "chat_log.h"
2012-02-09 18:15:53 -05:00
#include "command.h"
2012-08-25 20:50:50 -04:00
#include "common.h"
#include "contact_list.h"
2012-08-25 20:50:50 -04:00
#include "history.h"
#include "log.h"
#include "preferences.h"
#include "profanity.h"
2012-08-25 20:50:50 -04:00
#include "ui.h"
2012-02-09 17:24:03 -05:00
static log_level_t _get_log_level(char *log_level);
2012-08-22 19:41:22 -04:00
gboolean _process_input(char *inp);
static void _create_config_directory();
2012-07-24 18:19:48 -04:00
void
profanity_run(void)
2012-02-09 17:50:19 -05:00
{
2012-04-23 20:02:22 -04:00
gboolean cmd_result = TRUE;
2012-02-09 17:50:19 -05:00
log_info("Starting main event loop");
2012-02-09 17:50:19 -05:00
inp_non_block();
2012-02-09 18:15:53 -05:00
while(cmd_result == TRUE) {
2012-02-09 17:50:19 -05:00
int ch = ERR;
char inp[INP_WIN_MAX];
2012-02-09 17:50:19 -05:00
int size = 0;
2012-02-29 20:52:35 -05:00
while(ch != '\n') {
win_handle_special_keys(&ch);
2012-04-22 15:59:36 -04:00
2012-04-17 18:28:21 -04:00
if (ch == KEY_RESIZE) {
gui_resize(ch, inp, size);
}
2012-04-22 15:59:36 -04:00
gui_refresh();
jabber_process_events();
inp_get_char(&ch, inp, &size);
2012-02-29 20:52:35 -05:00
}
2012-02-16 19:42:41 -05:00
2012-02-19 20:42:29 -05:00
inp[size++] = '\0';
2012-08-22 19:41:22 -04:00
cmd_result = _process_input(inp);
2012-02-09 17:50:19 -05:00
}
}
2012-07-24 18:19:48 -04:00
void
2012-08-18 22:43:18 -04:00
profanity_init(const int disable_tls, char *log_level)
2012-02-26 20:48:51 -05:00
{
_create_config_directory();
log_level_t prof_log_level = _get_log_level(log_level);
2012-08-18 22:43:18 -04:00
log_init(prof_log_level);
log_info("Starting Profanity (%s)...", PACKAGE_VERSION);
2012-07-22 15:53:55 -04:00
chat_log_init();
2012-06-18 18:06:17 -04:00
prefs_load();
2012-02-29 20:44:47 -05:00
gui_init();
2012-02-26 20:48:51 -05:00
jabber_init(disable_tls);
2012-08-22 19:30:11 -04:00
cmd_init();
log_info("Initialising contact list");
contact_list_init();
atexit(profanity_shutdown_init);
2012-02-26 20:48:51 -05:00
}
2012-07-24 18:19:48 -04:00
void
profanity_shutdown_init(void)
2012-02-29 20:44:47 -05:00
{
gboolean wait_response = jabber_disconnect();
if (wait_response) {
while (jabber_connection_status() == JABBER_DISCONNECTING) {
jabber_process_events();
}
}
profanity_shutdown();
}
void
profanity_shutdown(void)
{
gui_close();
chat_log_close();
prefs_close();
log_close();
2012-02-29 20:44:47 -05:00
}
2012-08-18 22:43:18 -04:00
static log_level_t
_get_log_level(char *log_level)
2012-08-18 22:43:18 -04:00
{
if (strcmp(log_level, "DEBUG") == 0) {
return PROF_LEVEL_DEBUG;
} else if (strcmp(log_level, "INFO") == 0) {
return PROF_LEVEL_INFO;
} else if (strcmp(log_level, "WARN") == 0) {
return PROF_LEVEL_WARN;
} else {
return PROF_LEVEL_ERROR;
}
}
2012-08-22 19:41:22 -04:00
/*
* Take a line of input and process it, return TRUE if profanity is to
* continue, FALSE otherwise
*/
gboolean
_process_input(char *inp)
{
log_debug("Input recieved: %s", inp);
2012-08-22 19:41:22 -04:00
gboolean result = FALSE;
g_strstrip(inp);
// add line to history if something typed
if (strlen(inp) > 0) {
history_append(inp);
}
// just carry on if no input
if (strlen(inp) == 0) {
result = TRUE;
// habdle command if input starts with a '/'
} else if (inp[0] == '/') {
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
char *command = strtok(inp_cpy, " ");
result = cmd_execute(command, inp);
// call a default handler if input didn't start with '/'
} else {
result = cmd_execute_default(inp);
}
inp_clear();
reset_search_attempts();
win_page_off();
return result;
}
static void
_create_config_directory()
{
GString *dir = g_string_new(getenv("HOME"));
g_string_append(dir, "/.profanity");
create_dir(dir->str);
g_string_free(dir, TRUE);
}