2012-10-21 15:02:20 -04:00
|
|
|
/*
|
2012-02-20 15:07:38 -05:00
|
|
|
* profanity.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 James Booth <boothj5@gmail.com>
|
2012-10-21 15:02:20 -04:00
|
|
|
*
|
2012-02-20 15:07:38 -05:00
|
|
|
* 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-07-19 17:38:46 -04:00
|
|
|
|
2012-08-25 20:50:50 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-02-09 17:24:03 -05:00
|
|
|
|
2012-09-23 15:30:32 -04:00
|
|
|
#include <glib.h>
|
|
|
|
|
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"
|
2012-05-13 18:06:01 -04:00
|
|
|
#include "contact_list.h"
|
2012-08-25 20:50:50 -04:00
|
|
|
#include "history.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "preferences.h"
|
2012-09-11 17:55:59 -04:00
|
|
|
#include "profanity.h"
|
2012-10-01 20:04:53 -04:00
|
|
|
#include "jabber.h"
|
2012-08-25 20:50:50 -04:00
|
|
|
#include "ui.h"
|
2012-02-09 17:24:03 -05:00
|
|
|
|
2012-08-21 19:30:24 -04:00
|
|
|
static log_level_t _get_log_level(char *log_level);
|
2012-10-01 20:24:48 -04:00
|
|
|
static gboolean _process_input(char *inp);
|
2012-08-26 13:01:44 -04:00
|
|
|
static void _create_config_directory();
|
2012-10-01 20:04:53 -04:00
|
|
|
static void _free_roster_entry(jabber_roster_entry *entry);
|
2012-10-01 20:24:48 -04:00
|
|
|
static void _init(const int disable_tls, char *log_level);
|
2012-10-27 13:15:26 -04:00
|
|
|
static void _shutdown(void);
|
2012-03-10 15:37:42 -05:00
|
|
|
|
2012-07-24 18:19:48 -04:00
|
|
|
void
|
2012-10-01 20:24:48 -04:00
|
|
|
prof_run(const int disable_tls, char *log_level)
|
2012-02-09 17:50:19 -05:00
|
|
|
{
|
2012-10-01 20:24:48 -04:00
|
|
|
_init(disable_tls, log_level);
|
2012-08-25 19:54:18 -04:00
|
|
|
log_info("Starting main event loop");
|
2012-02-09 17:50:19 -05:00
|
|
|
inp_non_block();
|
2012-09-23 15:30:32 -04:00
|
|
|
GTimer *timer = g_timer_new();
|
2012-10-01 20:24:48 -04:00
|
|
|
gboolean cmd_result = TRUE;
|
2012-09-23 15:30:32 -04:00
|
|
|
|
2012-10-04 19:42:02 -04:00
|
|
|
char inp[INP_WIN_MAX];
|
|
|
|
int size = 0;
|
|
|
|
|
2012-02-09 18:15:53 -05:00
|
|
|
while(cmd_result == TRUE) {
|
2012-02-09 17:50:19 -05:00
|
|
|
int ch = ERR;
|
2012-10-04 19:42:02 -04:00
|
|
|
size = 0;
|
2012-02-09 17:50:19 -05:00
|
|
|
|
2012-02-29 20:52:35 -05:00
|
|
|
while(ch != '\n') {
|
2012-09-23 15:30:32 -04:00
|
|
|
|
|
|
|
gdouble elapsed = g_timer_elapsed(timer, NULL);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-09-23 17:24:31 -04:00
|
|
|
gint remind_period = prefs_get_remind();
|
2012-09-23 15:30:32 -04:00
|
|
|
|
2012-09-23 15:38:26 -04:00
|
|
|
// 0 means to not remind
|
|
|
|
if (remind_period > 0 && elapsed >= remind_period) {
|
2012-09-23 15:52:47 -04:00
|
|
|
win_remind();
|
|
|
|
g_timer_start(timer);
|
2012-09-23 15:30:32 -04:00
|
|
|
}
|
|
|
|
|
2012-04-17 17:43:35 -04:00
|
|
|
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-10-21 15:02:20 -04:00
|
|
|
|
2012-04-22 15:59:36 -04:00
|
|
|
gui_refresh();
|
|
|
|
jabber_process_events();
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-04-22 15:59:36 -04:00
|
|
|
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-10-01 18:41:36 -04:00
|
|
|
void
|
|
|
|
prof_handle_typing(char *from)
|
|
|
|
{
|
|
|
|
win_show_typing(from);
|
|
|
|
win_page_off();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_handle_incoming_message(char *from, char *message)
|
|
|
|
{
|
|
|
|
win_show_incomming_msg(from, message);
|
|
|
|
win_page_off();
|
2012-10-21 15:02:20 -04:00
|
|
|
|
|
|
|
if (prefs_get_chlog()) {
|
|
|
|
char from_cpy[strlen(from) + 1];
|
|
|
|
strcpy(from_cpy, from);
|
|
|
|
char *short_from = strtok(from_cpy, "/");
|
|
|
|
const char *jid = jabber_get_jid();
|
|
|
|
|
|
|
|
chat_log_chat(jid, short_from, message, IN);
|
|
|
|
}
|
2012-10-01 18:41:36 -04:00
|
|
|
}
|
|
|
|
|
2012-10-18 11:18:44 -04:00
|
|
|
void
|
|
|
|
prof_handle_error_message(const char *from, const char *err_msg)
|
|
|
|
{
|
|
|
|
char *msg, *fmt;
|
|
|
|
|
|
|
|
if (err_msg != NULL) {
|
|
|
|
fmt = "Error received from server: %s";
|
|
|
|
msg = (char *)malloc(strlen(err_msg) + strlen(fmt) - 1);
|
|
|
|
if (msg == NULL)
|
|
|
|
goto loop_out;
|
|
|
|
sprintf(msg, fmt, err_msg);
|
|
|
|
cons_bad_show(msg);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
loop_out:
|
|
|
|
win_show_error_msg(from, err_msg);
|
|
|
|
}
|
|
|
|
|
2012-10-01 18:53:26 -04:00
|
|
|
void
|
|
|
|
prof_handle_login_success(const char *jid)
|
|
|
|
{
|
2012-10-27 13:12:04 -04:00
|
|
|
const char *msg = "logged in successfully.";
|
2012-10-03 17:19:46 -04:00
|
|
|
cons_show("%s %s", jid, msg);
|
2012-10-01 18:53:26 -04:00
|
|
|
title_bar_set_status(PRESENCE_ONLINE);
|
2012-10-03 17:19:46 -04:00
|
|
|
log_info("%s %s", jid, msg);
|
2012-10-01 18:53:26 -04:00
|
|
|
win_page_off();
|
|
|
|
status_bar_print_message(jid);
|
|
|
|
status_bar_refresh();
|
|
|
|
prefs_add_login(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_handle_lost_connection(void)
|
|
|
|
{
|
|
|
|
cons_bad_show("Lost connection.");
|
|
|
|
log_info("Lost connection");
|
|
|
|
win_disconnected();
|
|
|
|
win_page_off();
|
|
|
|
log_info("disconnected");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_handle_failed_login(void)
|
|
|
|
{
|
|
|
|
cons_bad_show("Login failed.");
|
|
|
|
log_info("Login failed");
|
|
|
|
win_page_off();
|
|
|
|
log_info("disconnected");
|
|
|
|
}
|
|
|
|
|
2012-10-01 19:03:53 -04:00
|
|
|
void
|
|
|
|
prof_handle_contact_online(char *contact, char *show, char *status)
|
|
|
|
{
|
|
|
|
gboolean result = contact_list_add(contact, show, status);
|
|
|
|
if (result) {
|
|
|
|
win_contact_online(contact, show, status);
|
|
|
|
}
|
|
|
|
win_page_off();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_handle_contact_offline(char *contact, char *show, char *status)
|
|
|
|
{
|
2012-10-04 16:25:19 -04:00
|
|
|
gboolean result = contact_list_add(contact, "offline", status);
|
2012-10-01 19:03:53 -04:00
|
|
|
if (result) {
|
|
|
|
win_contact_offline(contact, show, status);
|
|
|
|
}
|
|
|
|
win_page_off();
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:24:48 -04:00
|
|
|
void
|
|
|
|
prof_handle_roster(GSList *roster)
|
2012-10-01 20:04:53 -04:00
|
|
|
{
|
|
|
|
while (roster != NULL) {
|
|
|
|
jabber_roster_entry *entry = roster->data;
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-10-04 16:31:09 -04:00
|
|
|
// if contact not in contact list add them as offline
|
2012-10-21 18:46:30 -04:00
|
|
|
if (contact_list_find_contact(entry->jid) == NULL) {
|
2012-10-04 16:31:09 -04:00
|
|
|
contact_list_add(entry->jid, "offline", NULL);
|
|
|
|
}
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-10-01 20:04:53 -04:00
|
|
|
roster = g_slist_next(roster);
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:02:20 -04:00
|
|
|
g_slist_free_full(roster, (GDestroyNotify)_free_roster_entry);
|
2012-10-01 20:04:53 -04:00
|
|
|
}
|
|
|
|
|
2012-08-26 13:01:44 -04:00
|
|
|
static void
|
2012-10-27 13:12:04 -04:00
|
|
|
_create_config_directory(void)
|
2012-08-26 13:01:44 -04:00
|
|
|
{
|
|
|
|
GString *dir = g_string_new(getenv("HOME"));
|
|
|
|
g_string_append(dir, "/.profanity");
|
|
|
|
create_dir(dir->str);
|
|
|
|
g_string_free(dir, TRUE);
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:04:53 -04:00
|
|
|
static void
|
|
|
|
_free_roster_entry(jabber_roster_entry *entry)
|
|
|
|
{
|
|
|
|
if (entry->name != NULL) {
|
|
|
|
free(entry->name);
|
|
|
|
entry->name = NULL;
|
|
|
|
}
|
|
|
|
free(entry->jid);
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:02:20 -04:00
|
|
|
static log_level_t
|
2012-10-01 20:24:48 -04:00
|
|
|
_get_log_level(char *log_level)
|
|
|
|
{
|
|
|
|
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-10-21 15:02:20 -04:00
|
|
|
/*
|
|
|
|
* Take a line of input and process it, return TRUE if profanity is to
|
2012-10-01 20:24:48 -04:00
|
|
|
* continue, FALSE otherwise
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
_process_input(char *inp)
|
|
|
|
{
|
|
|
|
log_debug("Input recieved: %s", inp);
|
|
|
|
gboolean result = FALSE;
|
|
|
|
g_strstrip(inp);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-10-01 20:24:48 -04:00
|
|
|
// 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();
|
2012-10-21 18:46:30 -04:00
|
|
|
contact_list_reset_search_attempts();
|
2012-10-01 20:24:48 -04:00
|
|
|
win_page_off();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_init(const int disable_tls, char *log_level)
|
|
|
|
{
|
|
|
|
_create_config_directory();
|
|
|
|
log_level_t prof_log_level = _get_log_level(log_level);
|
|
|
|
log_init(prof_log_level);
|
|
|
|
log_info("Starting Profanity (%s)...", PACKAGE_VERSION);
|
|
|
|
chat_log_init();
|
|
|
|
prefs_load();
|
|
|
|
gui_init();
|
|
|
|
jabber_init(disable_tls);
|
|
|
|
cmd_init();
|
|
|
|
log_info("Initialising contact list");
|
|
|
|
contact_list_init();
|
2012-10-27 13:15:26 -04:00
|
|
|
atexit(_shutdown);
|
2012-10-01 20:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-27 13:15:26 -04:00
|
|
|
_shutdown(void)
|
2012-10-01 20:24:48 -04:00
|
|
|
{
|
2012-10-27 13:26:57 -04:00
|
|
|
jabber_disconnect();
|
2012-10-21 19:29:39 -04:00
|
|
|
contact_list_clear();
|
2012-10-21 15:02:20 -04:00
|
|
|
gui_close();
|
|
|
|
chat_log_close();
|
|
|
|
prefs_close();
|
2012-10-21 19:29:39 -04:00
|
|
|
cmd_close();
|
2012-10-21 15:02:20 -04:00
|
|
|
log_close();
|
2012-10-01 20:24:48 -04:00
|
|
|
}
|