From fc8982e7616ae1e0086acedef1b7e223ba68757c Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 17:46:22 +0100 Subject: [PATCH 01/33] Added muc_window --- Makefile.am | 1 + src/ui/muc_window.c | 39 +++++++++++++++++++++++++++++++++++++++ src/ui/muc_window.h | 33 +++++++++++++++++++++++++++++++++ src/ui/window.c | 33 ++++++++------------------------- src/ui/window.h | 2 ++ 5 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 src/ui/muc_window.c create mode 100644 src/ui/muc_window.h diff --git a/Makefile.am b/Makefile.am index 9da258eb..88bed76f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ core_sources = \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ src/ui/windows.c src/ui/windows.h \ + src/ui/muc_window.c src/ui/muc_window.h \ src/command/command.h src/command/command.c src/command/history.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ diff --git a/src/ui/muc_window.c b/src/ui/muc_window.c new file mode 100644 index 00000000..ef4e1c9b --- /dev/null +++ b/src/ui/muc_window.c @@ -0,0 +1,39 @@ +/* + * muc_window.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include + +#include "ui/window.h" + +gboolean +muc_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg) +{ + gboolean handled = FALSE; + if (g_strcmp0(err_msg, "conflict") == 0) { + win_print_line(self, "Nickname already in use."); + win_refresh(self); + handled = TRUE; + } + + return handled; +} diff --git a/src/ui/muc_window.h b/src/ui/muc_window.h new file mode 100644 index 00000000..e67954a7 --- /dev/null +++ b/src/ui/muc_window.h @@ -0,0 +1,33 @@ +/* + * muc_window.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#ifndef MUC_WINDOW_H +#define MUC_WINDOW_H + +#include + +#include "ui/window.h" + +gboolean muc_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg); + +#endif diff --git a/src/ui/window.c b/src/ui/window.c index 13956b22..a1d5a0fc 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -34,14 +34,11 @@ #include "config/theme.h" #include "ui/window.h" +#include "ui/muc_window.h" -static gboolean _muc_handle_error_message(ProfWin *self, const char * const from, - const char * const err_msg); static gboolean _default_handle_error_message(ProfWin *self, const char * const from, const char * const err_msg); static void _win_print_time(ProfWin *self, char show_char); -static void _win_print_line(ProfWin *self, const char * const msg, ...); -static void _win_refresh(ProfWin *self); static void _win_presence_colour_on(ProfWin *self, const char * const presence); static void _win_presence_colour_off(ProfWin *self, const char * const presence); static void _win_show_contact(ProfWin *self, PContact contact); @@ -60,8 +57,8 @@ win_create(const char * const title, int cols, win_type_t type) new_win->type = type; new_win->print_time = _win_print_time; - new_win->print_line = _win_print_line; - new_win->refresh_win = _win_refresh; + new_win->print_line = win_print_line; + new_win->refresh_win = win_refresh; new_win->presence_colour_on = _win_presence_colour_on; new_win->presence_colour_off = _win_presence_colour_off; new_win->show_contact = _win_show_contact; @@ -69,7 +66,7 @@ win_create(const char * const title, int cols, win_type_t type) switch (new_win->type) { case WIN_MUC: - new_win->handle_error_message = _muc_handle_error_message; + new_win->handle_error_message = muc_handle_error_message; break; default: new_win->handle_error_message = _default_handle_error_message; @@ -102,8 +99,8 @@ _win_print_time(ProfWin* self, char show_char) g_free(date_fmt); } -static void -_win_print_line(ProfWin *self, const char * const msg, ...) +void +win_print_line(ProfWin *self, const char * const msg, ...) { va_list arg; va_start(arg, msg); @@ -115,8 +112,8 @@ _win_print_line(ProfWin *self, const char * const msg, ...) va_end(arg); } -static void -_win_refresh(ProfWin *self) +void +win_refresh(ProfWin *self) { int rows, cols; getmaxyx(stdscr, rows, cols); @@ -207,20 +204,6 @@ _win_show_contact(ProfWin *self, PContact contact) _win_presence_colour_off(self, presence); } -static gboolean -_muc_handle_error_message(ProfWin *self, const char * const from, - const char * const err_msg) -{ - gboolean handled = FALSE; - if (g_strcmp0(err_msg, "conflict") == 0) { - _win_print_line(self, "Nickname already in use."); - _win_refresh(self); - handled = TRUE; - } - - return handled; -} - static gboolean _default_handle_error_message(ProfWin *self, const char * const from, const char * const err_msg) diff --git a/src/ui/window.h b/src/ui/window.h index 2eec3b01..5f8a0e36 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -64,5 +64,7 @@ typedef struct prof_win_t { ProfWin* win_create(const char * const title, int cols, win_type_t type); void win_free(ProfWin *window); +void win_print_line(ProfWin *self, const char * const msg, ...); +void win_refresh(ProfWin *self); #endif From a527beabd31e22bce17a0745e92cabb415fde06f Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 18:00:22 +0100 Subject: [PATCH 02/33] Added show_char and attrs to ProfWin->print_line --- src/ui/core.c | 2 +- src/ui/muc_window.c | 2 +- src/ui/window.c | 7 +++++-- src/ui/window.h | 6 ++++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 2249a944..8e84274f 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -824,7 +824,7 @@ ui_current_print_line(const char * const msg, ...) ProfWin *current = wins_get_current(); va_list arg; va_start(arg, msg); - current->print_line(current, msg, arg); + current->print_line(current, '-', 0, msg, arg); va_end(arg); current->refresh_win(current); } diff --git a/src/ui/muc_window.c b/src/ui/muc_window.c index ef4e1c9b..72382032 100644 --- a/src/ui/muc_window.c +++ b/src/ui/muc_window.c @@ -30,7 +30,7 @@ muc_handle_error_message(ProfWin *self, const char * const from, { gboolean handled = FALSE; if (g_strcmp0(err_msg, "conflict") == 0) { - win_print_line(self, "Nickname already in use."); + win_print_line(self, '-', 0, "Nickname already in use."); win_refresh(self); handled = TRUE; } diff --git a/src/ui/window.c b/src/ui/window.c index a1d5a0fc..3ba49fff 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -100,14 +100,17 @@ _win_print_time(ProfWin* self, char show_char) } void -win_print_line(ProfWin *self, const char * const msg, ...) +win_print_line(ProfWin *self, const char show_char, int attrs, + const char * const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - _win_print_time(self, '-'); + _win_print_time(self, show_char); + wattron(self->win, attrs); wprintw(self->win, "%s\n", fmt_msg->str); + wattroff(self->win, attrs); g_string_free(fmt_msg, TRUE); va_end(arg); } diff --git a/src/ui/window.h b/src/ui/window.h index 5f8a0e36..9be3a7fd 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -53,7 +53,8 @@ typedef struct prof_win_t { int unread; int history_shown; void (*print_time)(struct prof_win_t *self, char show_char); - void (*print_line)(struct prof_win_t *self, const char * const msg, ...); + void (*print_line)(struct prof_win_t *self, const char show_char, + int attrs, const char * const msg, ...); void (*refresh_win)(struct prof_win_t *self); void (*presence_colour_on)(struct prof_win_t *self, const char * const presence); void (*presence_colour_off)(struct prof_win_t *self, const char * const presence); @@ -64,7 +65,8 @@ typedef struct prof_win_t { ProfWin* win_create(const char * const title, int cols, win_type_t type); void win_free(ProfWin *window); -void win_print_line(ProfWin *self, const char * const msg, ...); +void win_print_line(ProfWin *self, const char show_char, int attrs, + const char * const msg, ...); void win_refresh(ProfWin *self); #endif From 78ee448a8db1622c2c2ad8cad6f8b974743b0394 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 18:05:51 +0100 Subject: [PATCH 03/33] Use ProfWin->print_line for console typing notifications --- src/ui/console.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ui/console.c b/src/ui/console.c index 732fc53b..348a3ec0 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -131,10 +131,7 @@ cons_show_typing(const char * const barejid) display_usr = barejid; } - console->print_time(console, '-'); - wattron(console->win, COLOUR_TYPING); - wprintw(console->win, "!! %s is typing a message...\n", display_usr); - wattroff(console->win, COLOUR_TYPING); + console->print_line(console, '-', COLOUR_TYPING, "!! %s is typing a message...", display_usr); wins_refresh_console(); cons_alert(); From 47c96ed4e7d4787ba4560b49544f0d624038f8c6 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 19:11:51 +0100 Subject: [PATCH 04/33] ProfWin->print_incoming_message added --- src/ui/core.c | 23 +--------------------- src/ui/window.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/window.h | 2 ++ 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 8e84274f..1fac0dc3 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -279,28 +279,7 @@ ui_incoming_msg(const char * const from, const char * const message, // currently viewing chat window with sender if (wins_is_current(window)) { - if (tv_stamp == NULL) { - window->print_time(window, '-'); - } else { - GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); - gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); - wattron(window->win, COLOUR_TIME); - wprintw(window->win, "%s - ", date_fmt); - wattroff(window->win, COLOUR_TIME); - g_date_time_unref(time); - g_free(date_fmt); - } - - if (strncmp(message, "/me ", 4) == 0) { - wattron(window->win, COLOUR_THEM); - wprintw(window->win, "*%s ", display_from); - waddstr(window->win, message + 4); - wprintw(window->win, "\n"); - wattroff(window->win, COLOUR_THEM); - } else { - _win_show_user(window->win, display_from, 1); - _win_show_message(window->win, message); - } + window->print_incoming_message(window, tv_stamp, display_from, message); title_bar_set_typing(FALSE); title_bar_draw(); status_bar_active(num); diff --git a/src/ui/window.c b/src/ui/window.c index 3ba49fff..fa881df0 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -42,6 +42,8 @@ static void _win_print_time(ProfWin *self, char show_char); static void _win_presence_colour_on(ProfWin *self, const char * const presence); static void _win_presence_colour_off(ProfWin *self, const char * const presence); static void _win_show_contact(ProfWin *self, PContact contact); +static void _print_incoming_message(ProfWin *self, GTimeVal *tv_stamp, + const char * const from, const char * const message); ProfWin* win_create(const char * const title, int cols, win_type_t type) @@ -65,11 +67,29 @@ win_create(const char * const title, int cols, win_type_t type) switch (new_win->type) { + case WIN_CONSOLE: + new_win->handle_error_message = _default_handle_error_message; + new_win->print_incoming_message = NULL; + break; + case WIN_CHAT: + new_win->handle_error_message = _default_handle_error_message; + new_win->print_incoming_message = _print_incoming_message; + break; case WIN_MUC: new_win->handle_error_message = muc_handle_error_message; + new_win->print_incoming_message = NULL; + break; + case WIN_PRIVATE: + new_win->handle_error_message = _default_handle_error_message; + new_win->print_incoming_message = _print_incoming_message; + break; + case WIN_DUCK: + new_win->handle_error_message = _default_handle_error_message; + new_win->print_incoming_message = NULL; break; default: new_win->handle_error_message = _default_handle_error_message; + new_win->print_incoming_message = NULL; break; } @@ -213,3 +233,34 @@ _default_handle_error_message(ProfWin *self, const char * const from, { return FALSE; } + +static void +_print_incoming_message(ProfWin *self, GTimeVal *tv_stamp, + const char * const from, const char * const message) +{ + if (tv_stamp == NULL) { + self->print_time(self, '-'); + } else { + GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); + gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(self->win, COLOUR_TIME); + wprintw(self->win, "%s - ", date_fmt); + wattroff(self->win, COLOUR_TIME); + g_date_time_unref(time); + g_free(date_fmt); + } + + if (strncmp(message, "/me ", 4) == 0) { + wattron(self->win, COLOUR_THEM); + wprintw(self->win, "*%s ", from); + waddstr(self->win, message + 4); + wprintw(self->win, "\n"); + wattroff(self->win, COLOUR_THEM); + } else { + wattron(self->win, COLOUR_THEM); + wprintw(self->win, "%s: ", from); + wattroff(self->win, COLOUR_THEM); + waddstr(self->win, message); + wprintw(self->win, "\n"); + } +} diff --git a/src/ui/window.h b/src/ui/window.h index 9be3a7fd..ccf68dab 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -61,6 +61,8 @@ typedef struct prof_win_t { void (*show_contact)(struct prof_win_t *self, PContact contact); gboolean (*handle_error_message)(struct prof_win_t *self, const char * const from, const char * const err_msg); + void (*print_incoming_message)(struct prof_win_t *self, GTimeVal *tv_stamp, + const char * const from, const char * const message); } ProfWin; ProfWin* win_create(const char * const title, int cols, win_type_t type); From 3e7c6e295169028b14d6da6a88ec6cc3a546b9bd Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 19:16:20 +0100 Subject: [PATCH 05/33] Moved status display when receiveing delayed delivery --- src/ui/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 1fac0dc3..0d949361 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -297,16 +297,17 @@ ui_incoming_msg(const char * const from, const char * const message, _win_show_history(window->win, num, from); } + // show users status first, when receiving message via delayed delivery + if ((tv_stamp != NULL) && (win_created)) { + PContact pcontact = roster_get_contact(from); + if (pcontact != NULL) { + window->show_contact(window, pcontact); + } + } + if (tv_stamp == NULL) { window->print_time(window, '-'); } else { - // show users status first, when receiving message via delayed delivery - if (win_created) { - PContact pcontact = roster_get_contact(from); - if (pcontact != NULL) { - window->show_contact(window, pcontact); - } - } GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); wattron(window->win, COLOUR_TIME); From eee49e8ad8e72f083e4019e7452e9405ce7cb037 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 19:20:03 +0100 Subject: [PATCH 06/33] Refactor ui_incoming_message --- src/ui/core.c | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 0d949361..5559639a 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -305,28 +305,7 @@ ui_incoming_msg(const char * const from, const char * const message, } } - if (tv_stamp == NULL) { - window->print_time(window, '-'); - } else { - GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); - gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); - wattron(window->win, COLOUR_TIME); - wprintw(window->win, "%s - ", date_fmt); - wattroff(window->win, COLOUR_TIME); - g_date_time_unref(time); - g_free(date_fmt); - } - - if (strncmp(message, "/me ", 4) == 0) { - wattron(window->win, COLOUR_THEM); - wprintw(window->win, "*%s ", display_from); - waddstr(window->win, message + 4); - wprintw(window->win, "\n"); - wattroff(window->win, COLOUR_THEM); - } else { - _win_show_user(window->win, display_from, 1); - _win_show_message(window->win, message); - } + window->print_incoming_message(window, tv_stamp, display_from, message); } int ui_index = num; From dd4deafe93bc1a17560ef0e381ae2ad751e8e6ca Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 22:46:04 +0100 Subject: [PATCH 07/33] Refactor ui_current_error_line --- src/ui/core.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 5559639a..807625d8 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -792,12 +792,8 @@ void ui_current_error_line(const char * const msg) { ProfWin *current = wins_get_current(); - current->print_time(current, '-'); - wattron(current->win, COLOUR_ERROR); - wprintw(current->win, "%s\n", msg); - wattroff(current->win, COLOUR_ERROR); - - wins_refresh_current(); + current->print_line(current, '-', COLOUR_ERROR, msg); + current->refresh_win(current); } void From c6c0a94bb03a5bcd11f555478a7cc7e31f8a3551 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:22:46 +0100 Subject: [PATCH 08/33] Removed generic functions from ProfWin --- src/ui/console.c | 154 +++++++++++++++++++++++------------------------ src/ui/core.c | 56 ++++++++--------- src/ui/window.c | 97 +++++++++++++---------------- src/ui/window.h | 13 ++-- src/ui/windows.c | 2 +- 5 files changed, 154 insertions(+), 168 deletions(-) diff --git a/src/ui/console.c b/src/ui/console.c index 348a3ec0..9fecf4e3 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -51,7 +51,7 @@ void cons_show_time(void) { ProfWin *console = wins_get_console(); - console->print_time(console, '-'); + win_print_time(console, '-'); wins_refresh_console(); } @@ -72,7 +72,7 @@ cons_debug(const char * const msg, ...) va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "%s\n", fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); @@ -93,7 +93,7 @@ cons_show(const char * const msg, ...) va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "%s\n", fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); @@ -108,7 +108,7 @@ cons_show_error(const char * const msg, ...) va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_ERROR); wprintw(console->win, "%s\n", fmt_msg->str); wattroff(console->win, COLOUR_ERROR); @@ -131,7 +131,7 @@ cons_show_typing(const char * const barejid) display_usr = barejid; } - console->print_line(console, '-', COLOUR_TYPING, "!! %s is typing a message...", display_usr); + win_print_line(console, '-', COLOUR_TYPING, "!! %s is typing a message...", display_usr); wins_refresh_console(); cons_alert(); @@ -146,7 +146,7 @@ cons_show_incoming_message(const char * const short_from, const int win_index) if (ui_index == 10) { ui_index = 0; } - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_INCOMING); wprintw(console->win, "<< incoming from %s (%d)\n", short_from, ui_index); wattroff(console->win, COLOUR_INCOMING); @@ -165,7 +165,7 @@ cons_about(void) if (prefs_get_boolean(PREF_SPLASH)) { _cons_splash_logo(); } else { - console->print_time(console, '-'); + win_print_time(console, '-'); if (strcmp(PACKAGE_STATUS, "development") == 0) { @@ -179,22 +179,22 @@ cons_about(void) } } - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "Copyright (C) 2012, 2013 James Booth <%s>.\n", PACKAGE_BUGREPORT); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "License GPLv3+: GNU GPL version 3 or later \n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "This is free software; you are free to change and redistribute it.\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "There is NO WARRANTY, to the extent permitted by law.\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "Type '/help' to show complete help.\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "\n"); if (prefs_get_boolean(PREF_VERCHECK)) { @@ -218,12 +218,12 @@ cons_check_version(gboolean not_available_msg) if (relase_valid) { if (release_is_new(latest_release)) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "A new version of Profanity is available: %s", latest_release); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "Check for details.\n"); free(latest_release); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "\n"); } else { if (not_available_msg) { @@ -242,15 +242,15 @@ void cons_show_login_success(ProfAccount *account) { ProfWin *console = wins_get_console(); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "%s logged in successfully, ", account->jid); resource_presence_t presence = accounts_get_login_presence(account->name); const char *presence_str = string_from_resource_presence(presence); - console->presence_colour_on(console, presence_str); + win_presence_colour_on(console, presence_str); wprintw(console->win, "%s", presence_str); - console->presence_colour_off(console, presence_str); + win_presence_colour_off(console, presence_str); wprintw(console->win, " (priority %d)", accounts_get_priority_for_presence_type(account->name, presence)); wprintw(console->win, ".\n"); @@ -268,7 +268,7 @@ cons_show_wins(void) GSList *curr = window_strings; while (curr != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, curr->data); wprintw(console->win, "\n"); curr = g_slist_next(curr); @@ -310,19 +310,19 @@ cons_show_info(PContact pcontact) GDateTime *last_activity = p_contact_last_activity(pcontact); WINDOW *win = console->win; - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "\n"); - console->print_time(console, '-'); - console->presence_colour_on(console, presence); + win_print_time(console, '-'); + win_presence_colour_on(console, presence); wprintw(win, "%s", barejid); if (name != NULL) { wprintw(win, " (%s)", name); } - console->presence_colour_off(console, presence); + win_presence_colour_off(console, presence); wprintw(win, ":\n"); if (sub != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Subscription: %s\n", sub); } @@ -330,7 +330,7 @@ cons_show_info(PContact pcontact) GDateTime *now = g_date_time_new_now_local(); GTimeSpan span = g_date_time_difference(now, last_activity); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Last activity: "); int hours = span / G_TIME_SPAN_HOUR; @@ -352,7 +352,7 @@ cons_show_info(PContact pcontact) } if (resources != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Resources:\n"); // sort in order of availabiltiy @@ -367,21 +367,21 @@ cons_show_info(PContact pcontact) while (ordered_resources != NULL) { Resource *resource = ordered_resources->data; const char *resource_presence = string_from_resource_presence(resource->presence); - console->print_time(console, '-'); - console->presence_colour_on(console, resource_presence); + win_print_time(console, '-'); + win_presence_colour_on(console, resource_presence); wprintw(win, " %s (%d), %s", resource->name, resource->priority, resource_presence); if (resource->status != NULL) { wprintw(win, ", \"%s\"", resource->status); } wprintw(win, "\n"); - console->presence_colour_off(console, resource_presence); + win_presence_colour_off(console, resource_presence); if (resource->caps_str != NULL) { Capabilities *caps = caps_get(resource->caps_str); if (caps != NULL) { // show identity if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " Identity: "); if (caps->name != NULL) { wprintw(win, "%s", caps->name); @@ -401,7 +401,7 @@ cons_show_info(PContact pcontact) wprintw(win, "\n"); } if (caps->software != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " Software: %s", caps->software); } if (caps->software_version != NULL) { @@ -411,7 +411,7 @@ cons_show_info(PContact pcontact) wprintw(win, "\n"); } if (caps->os != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " OS: %s", caps->os); } if (caps->os_version != NULL) { @@ -437,10 +437,10 @@ cons_show_caps(const char * const contact, Resource *resource) WINDOW *win = console->win; cons_show(""); const char *resource_presence = string_from_resource_presence(resource->presence); - console->print_time(console, '-'); - console->presence_colour_on(console, resource_presence); + win_print_time(console, '-'); + win_presence_colour_on(console, resource_presence); wprintw(console->win, "%s", contact); - console->presence_colour_off(console, resource_presence); + win_presence_colour_off(console, resource_presence); wprintw(win, ":\n"); if (resource->caps_str != NULL) { @@ -448,7 +448,7 @@ cons_show_caps(const char * const contact, Resource *resource) if (caps != NULL) { // show identity if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Identity: "); if (caps->name != NULL) { wprintw(win, "%s", caps->name); @@ -468,7 +468,7 @@ cons_show_caps(const char * const contact, Resource *resource) wprintw(win, "\n"); } if (caps->software != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Software: %s", caps->software); } if (caps->software_version != NULL) { @@ -478,7 +478,7 @@ cons_show_caps(const char * const contact, Resource *resource) wprintw(win, "\n"); } if (caps->os != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "OS: %s", caps->os); } if (caps->os_version != NULL) { @@ -489,11 +489,11 @@ cons_show_caps(const char * const contact, Resource *resource) } if (caps->features != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Features:\n"); GSList *feature = caps->features; while (feature != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " %s\n", feature->data); feature = g_slist_next(feature); } @@ -512,10 +512,10 @@ cons_show_software_version(const char * const jid, const char * const presence, ProfWin *console = wins_get_console(); if ((name != NULL) || (version != NULL) || (os != NULL)) { cons_show(""); - console->print_time(console, '-'); - console->presence_colour_on(console, presence); + win_print_time(console, '-'); + win_presence_colour_on(console, presence); wprintw(console->win, "%s", jid); - console->presence_colour_off(console, presence); + win_presence_colour_off(console, presence); wprintw(console->win, ":\n"); } if (name != NULL) { @@ -582,7 +582,7 @@ cons_show_room_list(GSList *rooms, const char * const conference_node) cons_show("Chat rooms at %s:", conference_node); while (rooms != NULL) { DiscoItem *room = rooms->data; - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, " %s", room->jid); if (room->name != NULL) { wprintw(console->win, ", (%s)", room->name); @@ -612,7 +612,7 @@ cons_show_bookmarks(const GList *list) ProfWin *console = wins_get_console(); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, " %s", item->jid); if (item->nick != NULL) { wprintw(console->win, "/%s", item->nick); @@ -680,7 +680,7 @@ cons_show_disco_items(GSList *items, const char * const jid) cons_show("Service discovery items for %s:", jid); while (items != NULL) { DiscoItem *item = items->data; - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, " %s", item->jid); if (item->name != NULL) { wprintw(console->win, ", (%s)", item->name); @@ -703,7 +703,7 @@ cons_show_status(const char * const barejid) PContact pcontact = roster_get_contact(barejid); if (pcontact != NULL) { - console->show_contact(console, pcontact); + win_show_contact(console, pcontact); } else { cons_show("No such contact \"%s\" in roster.", barejid); } @@ -760,10 +760,10 @@ cons_show_account_list(gchar **accounts) if ((jabber_get_connection_status() == JABBER_CONNECTED) && (g_strcmp0(jabber_get_account_name(), accounts[i]) == 0)) { resource_presence_t presence = accounts_get_last_presence(accounts[i]); - console->print_time(console, '-'); - console->presence_colour_on(console, string_from_resource_presence(presence)); + win_print_time(console, '-'); + win_presence_colour_on(console, string_from_resource_presence(presence)); wprintw(console->win, "%s\n", accounts[i]); - console->presence_colour_off(console, string_from_resource_presence(presence)); + win_presence_colour_off(console, string_from_resource_presence(presence)); } else { cons_show(accounts[i]); } @@ -819,7 +819,7 @@ cons_show_account(ProfAccount *account) WINDOW *win = console->win; if (resources != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, "Resources:\n"); // sort in order of availabiltiy @@ -834,21 +834,21 @@ cons_show_account(ProfAccount *account) while (ordered_resources != NULL) { Resource *resource = ordered_resources->data; const char *resource_presence = string_from_resource_presence(resource->presence); - console->print_time(console, '-'); - console->presence_colour_on(console, resource_presence); + win_print_time(console, '-'); + win_presence_colour_on(console, resource_presence); wprintw(win, " %s (%d), %s", resource->name, resource->priority, resource_presence); if (resource->status != NULL) { wprintw(win, ", \"%s\"", resource->status); } wprintw(win, "\n"); - console->presence_colour_off(console, resource_presence); + win_presence_colour_off(console, resource_presence); if (resource->caps_str != NULL) { Capabilities *caps = caps_get(resource->caps_str); if (caps != NULL) { // show identity if ((caps->category != NULL) || (caps->type != NULL) || (caps->name != NULL)) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " Identity: "); if (caps->name != NULL) { wprintw(win, "%s", caps->name); @@ -868,7 +868,7 @@ cons_show_account(ProfAccount *account) wprintw(win, "\n"); } if (caps->software != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " Software: %s", caps->software); } if (caps->software_version != NULL) { @@ -878,7 +878,7 @@ cons_show_account(ProfAccount *account) wprintw(win, "\n"); } if (caps->os != NULL) { - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(win, " OS: %s", caps->os); } if (caps->os_version != NULL) { @@ -1341,7 +1341,7 @@ cons_show_contacts(GSList *list) PContact contact = curr->data; if ((strcmp(p_contact_subscription(contact), "to") == 0) || (strcmp(p_contact_subscription(contact), "both") == 0)) { - console->show_contact(console, contact); + win_show_contact(console, contact); } curr = g_slist_next(curr); } @@ -1362,47 +1362,47 @@ static void _cons_splash_logo(void) { ProfWin *console = wins_get_console(); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "Welcome to\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, " ___ _ \n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, " / __) (_)_ \n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, " ____ ____ ___ | |__ ____ ____ _| |_ _ _ \n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, "| _ \\ / ___) _ \\| __) _ | _ \\| | _) | | |\n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, "| | | | | | |_| | | ( ( | | | | | | |_| |_| |\n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, "| ||_/|_| \\___/|_| \\_||_|_| |_|_|\\___)__ |\n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wattron(console->win, COLOUR_SPLASH); wprintw(console->win, "|_| (____/ \n"); wattroff(console->win, COLOUR_SPLASH); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, "\n"); - console->print_time(console, '-'); + win_print_time(console, '-'); if (strcmp(PACKAGE_STATUS, "development") == 0) { #ifdef HAVE_GIT_VERSION wprintw(console->win, "Version %sdev.%s.%s\n", PACKAGE_VERSION, PROF_GIT_BRANCH, PROF_GIT_REVISION); @@ -1431,20 +1431,20 @@ _show_roster_contacts(GSList *list, gboolean show_groups) } const char *presence = p_contact_presence(contact); - console->print_time(console, '-'); + win_print_time(console, '-'); if (p_contact_subscribed(contact)) { - console->presence_colour_on(console, presence); + win_presence_colour_on(console, presence); wprintw(console->win, "%s\n", title->str); - console->presence_colour_off(console, presence); + win_presence_colour_off(console, presence); } else { - console->presence_colour_on(console, "offline"); + win_presence_colour_on(console, "offline"); wprintw(console->win, "%s\n", title->str); - console->presence_colour_off(console, "offline"); + win_presence_colour_off(console, "offline"); } g_string_free(title, TRUE); - console->print_time(console, '-'); + win_print_time(console, '-'); wprintw(console->win, " Subscription : "); GString *sub = g_string_new(""); sub = g_string_append(sub, p_contact_subscription(contact)); diff --git a/src/ui/core.c b/src/ui/core.c index 807625d8..d5418cef 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -301,7 +301,7 @@ ui_incoming_msg(const char * const from, const char * const message, if ((tv_stamp != NULL) && (win_created)) { PContact pcontact = roster_get_contact(from); if (pcontact != NULL) { - window->show_contact(window, pcontact); + win_show_contact(window, pcontact); } } @@ -783,17 +783,17 @@ ui_current_print_line(const char * const msg, ...) ProfWin *current = wins_get_current(); va_list arg; va_start(arg, msg); - current->print_line(current, '-', 0, msg, arg); + win_print_line(current, '-', 0, msg, arg); va_end(arg); - current->refresh_win(current); + win_refresh(current); } void ui_current_error_line(const char * const msg) { ProfWin *current = wins_get_current(); - current->print_line(current, '-', COLOUR_ERROR, msg); - current->refresh_win(current); + win_print_line(current, '-', COLOUR_ERROR, msg); + win_refresh(current); } void @@ -822,7 +822,7 @@ ui_print_error_from_recipient(const char * const from, const char *err_msg) ProfWin *window = wins_get_by_recipient(from); if (window != NULL) { - window->print_time(window, '-'); + win_print_time(window, '-'); _win_show_error_msg(window->win, err_msg); if (wins_is_current(window)) { wins_refresh_current(); @@ -856,7 +856,7 @@ ui_print_system_msg_from_recipient(const char * const from, const char *message) } } - window->print_time(window, '-'); + win_print_time(window, '-'); wprintw(window->win, "*%s %s\n", bare_jid, message); // this is the current window @@ -881,7 +881,7 @@ ui_recipient_gone(const char * const barejid) ProfWin *window = wins_get_by_recipient(barejid); if (window != NULL) { - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_GONE); wprintw(window->win, "<- %s ", display_usr); wprintw(window->win, "has left the conversation."); @@ -938,7 +938,7 @@ ui_create_duck_win(void) ProfWin *window = wins_new("DuckDuckGo search", WIN_DUCK); int num = wins_get_num(window); ui_switch_win(num); - window->print_time(window, '-'); + win_print_time(window, '-'); wprintw(window->win, "Type ':help' to find out more.\n"); } @@ -957,9 +957,9 @@ ui_duck(const char * const query) { ProfWin *window = wins_get_by_recipient("DuckDuckGo search"); if (window != NULL) { - window->print_time(window, '-'); + win_print_time(window, '-'); wprintw(window->win, "\n"); - window->print_time(window, '-'); + win_print_time(window, '-'); wattron(window->win, COLOUR_ME); wprintw(window->win, "Query : "); wattroff(window->win, COLOUR_ME); @@ -974,7 +974,7 @@ ui_duck_result(const char * const result) ProfWin *window = wins_get_by_recipient("DuckDuckGo search"); if (window != NULL) { - window->print_time(window, '-'); + win_print_time(window, '-'); wattron(window->win, COLOUR_THEM); wprintw(window->win, "Result : "); wattroff(window->win, COLOUR_THEM); @@ -985,7 +985,7 @@ ui_duck_result(const char * const result) gunichar unichar = g_utf8_get_char(ptr); if (unichar == '\n') { wprintw(window->win, "\n"); - window->print_time(window, '-'); + win_print_time(window, '-'); } else { gchar *string = g_ucs4_to_utf8(&unichar, 1, NULL, NULL, NULL); if (string != NULL) { @@ -1039,7 +1039,7 @@ ui_outgoing_msg(const char * const from, const char * const to, num = wins_get_num(window); } - window->print_time(window, '-'); + win_print_time(window, '-'); if (strncmp(message, "/me ", 4) == 0) { wattron(window->win, COLOUR_ME); wprintw(window->win, "*%s ", from); @@ -1073,7 +1073,7 @@ ui_room_roster(const char * const room, GList *roster, const char * const presen { ProfWin *window = wins_get_by_recipient(room); - window->print_time(window, '!'); + win_print_time(window, '!'); if ((roster == NULL) || (g_list_length(roster) == 0)) { wattron(window->win, COLOUR_ROOMINFO); if (presence == NULL) { @@ -1103,9 +1103,9 @@ ui_room_roster(const char * const room, GList *roster, const char * const presen const char const *nick = p_contact_barejid(member); const char const *show = p_contact_presence(member); - window->presence_colour_on(window, show); + win_presence_colour_on(window, show); wprintw(window->win, "%s", nick); - window->presence_colour_off(window, show); + win_presence_colour_off(window, show); if (roster->next != NULL) { wprintw(window->win, ", "); @@ -1128,7 +1128,7 @@ ui_room_member_offline(const char * const room, const char * const nick) { ProfWin *window = wins_get_by_recipient(room); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_OFFLINE); wprintw(window->win, "<- %s has left the room.\n", nick); wattroff(window->win, COLOUR_OFFLINE); @@ -1144,7 +1144,7 @@ ui_room_member_online(const char * const room, const char * const nick, { ProfWin *window = wins_get_by_recipient(room); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_ONLINE); wprintw(window->win, "-> %s has joined the room.\n", nick); wattroff(window->win, COLOUR_ONLINE); @@ -1175,7 +1175,7 @@ ui_room_member_nick_change(const char * const room, { ProfWin *window = wins_get_by_recipient(room); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_THEM); wprintw(window->win, "** %s is now known as %s\n", old_nick, nick); wattroff(window->win, COLOUR_THEM); @@ -1190,7 +1190,7 @@ ui_room_nick_change(const char * const room, const char * const nick) { ProfWin *window = wins_get_by_recipient(room); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_ME); wprintw(window->win, "** You are now known as %s\n", nick); wattroff(window->win, COLOUR_ME); @@ -1233,7 +1233,7 @@ ui_room_message(const char * const room_jid, const char * const nick, ProfWin *window = wins_get_by_recipient(room_jid); int num = wins_get_num(window); - window->print_time(window, '-'); + win_print_time(window, '-'); if (strcmp(nick, muc_get_room_nick(room_jid)) != 0) { if (strncmp(message, "/me ", 4) == 0) { wattron(window->win, COLOUR_THEM); @@ -1304,7 +1304,7 @@ ui_room_subject(const char * const room_jid, const char * const subject) ProfWin *window = wins_get_by_recipient(room_jid); int num = wins_get_num(window); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_ROOMINFO); wprintw(window->win, "Room subject: "); wattroff(window->win, COLOUR_ROOMINFO); @@ -1327,7 +1327,7 @@ ui_room_broadcast(const char * const room_jid, const char * const message) ProfWin *window = wins_get_by_recipient(room_jid); int num = wins_get_num(window); - window->print_time(window, '!'); + win_print_time(window, '!'); wattron(window->win, COLOUR_ROOMINFO); wprintw(window->win, "Room message: "); wattroff(window->win, COLOUR_ROOMINFO); @@ -1352,7 +1352,7 @@ ui_status(void) ProfWin *current = wins_get_current(); if (pcontact != NULL) { - current->show_contact(current, pcontact); + win_show_contact(current, pcontact); } else { ui_current_print_line("Error getting contact info."); } @@ -1366,7 +1366,7 @@ ui_status_private(void) ProfWin *current = wins_get_current(); if (pcontact != NULL) { - current->show_contact(current, pcontact); + win_show_contact(current, pcontact); } else { ui_current_print_line("Error getting contact info."); } @@ -1381,7 +1381,7 @@ ui_status_room(const char * const contact) ProfWin *current = wins_get_current(); if (pcontact != NULL) { - current->show_contact(current, pcontact); + win_show_contact(current, pcontact); } else { ui_current_print_line("No such participant \"%s\" in room.", contact); } @@ -1493,7 +1493,7 @@ _show_status_string(ProfWin *window, const char * const from, if (!prefs_get_boolean(PREF_STATUSES)) return; - window->print_time(window, '-'); + win_print_time(window, '-'); if (show != NULL) { if (strcmp(show, "away") == 0) { diff --git a/src/ui/window.c b/src/ui/window.c index fa881df0..d21297a3 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -38,10 +38,6 @@ static gboolean _default_handle_error_message(ProfWin *self, const char * const from, const char * const err_msg); -static void _win_print_time(ProfWin *self, char show_char); -static void _win_presence_colour_on(ProfWin *self, const char * const presence); -static void _win_presence_colour_off(ProfWin *self, const char * const presence); -static void _win_show_contact(ProfWin *self, PContact contact); static void _print_incoming_message(ProfWin *self, GTimeVal *tv_stamp, const char * const from, const char * const message); @@ -58,13 +54,6 @@ win_create(const char * const title, int cols, win_type_t type) new_win->history_shown = 0; new_win->type = type; - new_win->print_time = _win_print_time; - new_win->print_line = win_print_line; - new_win->refresh_win = win_refresh; - new_win->presence_colour_on = _win_presence_colour_on; - new_win->presence_colour_off = _win_presence_colour_off; - new_win->show_contact = _win_show_contact; - switch (new_win->type) { case WIN_CONSOLE: @@ -107,80 +96,80 @@ win_free(ProfWin* window) window = NULL; } -static void -_win_print_time(ProfWin* self, char show_char) +void +win_print_time(ProfWin* window, char show_char) { GDateTime *time = g_date_time_new_now_local(); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); - wattron(self->win, COLOUR_TIME); - wprintw(self->win, "%s %c ", date_fmt, show_char); - wattroff(self->win, COLOUR_TIME); + wattron(window->win, COLOUR_TIME); + wprintw(window->win, "%s %c ", date_fmt, show_char); + wattroff(window->win, COLOUR_TIME); g_date_time_unref(time); g_free(date_fmt); } void -win_print_line(ProfWin *self, const char show_char, int attrs, +win_print_line(ProfWin *window, const char show_char, int attrs, const char * const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - _win_print_time(self, show_char); - wattron(self->win, attrs); - wprintw(self->win, "%s\n", fmt_msg->str); - wattroff(self->win, attrs); + win_print_time(window, show_char); + wattron(window->win, attrs); + wprintw(window->win, "%s\n", fmt_msg->str); + wattroff(window->win, attrs); g_string_free(fmt_msg, TRUE); va_end(arg); } void -win_refresh(ProfWin *self) +win_refresh(ProfWin *window) { int rows, cols; getmaxyx(stdscr, rows, cols); - prefresh(self->win, self->y_pos, 0, 1, 0, rows-3, cols-1); + prefresh(window->win, window->y_pos, 0, 1, 0, rows-3, cols-1); } -static void -_win_presence_colour_on(ProfWin *self, const char * const presence) +void +win_presence_colour_on(ProfWin *window, const char * const presence) { if (g_strcmp0(presence, "online") == 0) { - wattron(self->win, COLOUR_ONLINE); + wattron(window->win, COLOUR_ONLINE); } else if (g_strcmp0(presence, "away") == 0) { - wattron(self->win, COLOUR_AWAY); + wattron(window->win, COLOUR_AWAY); } else if (g_strcmp0(presence, "chat") == 0) { - wattron(self->win, COLOUR_CHAT); + wattron(window->win, COLOUR_CHAT); } else if (g_strcmp0(presence, "dnd") == 0) { - wattron(self->win, COLOUR_DND); + wattron(window->win, COLOUR_DND); } else if (g_strcmp0(presence, "xa") == 0) { - wattron(self->win, COLOUR_XA); + wattron(window->win, COLOUR_XA); } else { - wattron(self->win, COLOUR_OFFLINE); + wattron(window->win, COLOUR_OFFLINE); } } -static void -_win_presence_colour_off(ProfWin *self, const char * const presence) +void +win_presence_colour_off(ProfWin *window, const char * const presence) { if (g_strcmp0(presence, "online") == 0) { - wattroff(self->win, COLOUR_ONLINE); + wattroff(window->win, COLOUR_ONLINE); } else if (g_strcmp0(presence, "away") == 0) { - wattroff(self->win, COLOUR_AWAY); + wattroff(window->win, COLOUR_AWAY); } else if (g_strcmp0(presence, "chat") == 0) { - wattroff(self->win, COLOUR_CHAT); + wattroff(window->win, COLOUR_CHAT); } else if (g_strcmp0(presence, "dnd") == 0) { - wattroff(self->win, COLOUR_DND); + wattroff(window->win, COLOUR_DND); } else if (g_strcmp0(presence, "xa") == 0) { - wattroff(self->win, COLOUR_XA); + wattroff(window->win, COLOUR_XA); } else { - wattroff(self->win, COLOUR_OFFLINE); + wattroff(window->win, COLOUR_OFFLINE); } } -static void -_win_show_contact(ProfWin *self, PContact contact) +void +win_show_contact(ProfWin *window, PContact contact) { const char *barejid = p_contact_barejid(contact); const char *name = p_contact_name(contact); @@ -188,43 +177,43 @@ _win_show_contact(ProfWin *self, PContact contact) const char *status = p_contact_status(contact); GDateTime *last_activity = p_contact_last_activity(contact); - _win_print_time(self, '-'); - _win_presence_colour_on(self, presence); + win_print_time(window, '-'); + win_presence_colour_on(window, presence); if (name != NULL) { - wprintw(self->win, "%s", name); + wprintw(window->win, "%s", name); } else { - wprintw(self->win, "%s", barejid); + wprintw(window->win, "%s", barejid); } - wprintw(self->win, " is %s", presence); + wprintw(window->win, " is %s", presence); if (last_activity != NULL) { GDateTime *now = g_date_time_new_now_local(); GTimeSpan span = g_date_time_difference(now, last_activity); - wprintw(self->win, ", idle "); + wprintw(window->win, ", idle "); int hours = span / G_TIME_SPAN_HOUR; span = span - hours * G_TIME_SPAN_HOUR; if (hours > 0) { - wprintw(self->win, "%dh", hours); + wprintw(window->win, "%dh", hours); } int minutes = span / G_TIME_SPAN_MINUTE; span = span - minutes * G_TIME_SPAN_MINUTE; - wprintw(self->win, "%dm", minutes); + wprintw(window->win, "%dm", minutes); int seconds = span / G_TIME_SPAN_SECOND; - wprintw(self->win, "%ds", seconds); + wprintw(window->win, "%ds", seconds); } if (status != NULL) { - wprintw(self->win, ", \"%s\"", p_contact_status(contact)); + wprintw(window->win, ", \"%s\"", p_contact_status(contact)); } - wprintw(self->win, "\n"); - _win_presence_colour_off(self, presence); + wprintw(window->win, "\n"); + win_presence_colour_off(window, presence); } static gboolean @@ -239,7 +228,7 @@ _print_incoming_message(ProfWin *self, GTimeVal *tv_stamp, const char * const from, const char * const message) { if (tv_stamp == NULL) { - self->print_time(self, '-'); + win_print_time(self, '-'); } else { GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); diff --git a/src/ui/window.h b/src/ui/window.h index ccf68dab..bfd9b267 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -52,13 +52,6 @@ typedef struct prof_win_t { int paged; int unread; int history_shown; - void (*print_time)(struct prof_win_t *self, char show_char); - void (*print_line)(struct prof_win_t *self, const char show_char, - int attrs, const char * const msg, ...); - void (*refresh_win)(struct prof_win_t *self); - void (*presence_colour_on)(struct prof_win_t *self, const char * const presence); - void (*presence_colour_off)(struct prof_win_t *self, const char * const presence); - void (*show_contact)(struct prof_win_t *self, PContact contact); gboolean (*handle_error_message)(struct prof_win_t *self, const char * const from, const char * const err_msg); void (*print_incoming_message)(struct prof_win_t *self, GTimeVal *tv_stamp, @@ -69,6 +62,10 @@ ProfWin* win_create(const char * const title, int cols, win_type_t type); void win_free(ProfWin *window); void win_print_line(ProfWin *self, const char show_char, int attrs, const char * const msg, ...); -void win_refresh(ProfWin *self); +void win_refresh(ProfWin *window); +void win_print_time(ProfWin *window, char show_char); +void win_presence_colour_on(ProfWin *window, const char * const presence); +void win_presence_colour_off(ProfWin *window, const char * const presence); +void win_show_contact(ProfWin *window, PContact contact); #endif diff --git a/src/ui/windows.c b/src/ui/windows.c index 0d9d7048..87b61ab1 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -350,7 +350,7 @@ wins_lost_connection(void) while (curr != NULL) { ProfWin *window = curr->data; if (window->type != WIN_CONSOLE) { - window->print_time(window, '-'); + win_print_time(window, '-'); wattron(window->win, COLOUR_ERROR); wprintw(window->win, "%s\n", "Lost connection."); wattroff(window->win, COLOUR_ERROR); From e2161d8acc07e822d473a83a26d0c1077568e1f5 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:25:01 +0100 Subject: [PATCH 09/33] Refactor wins_refresh_current --- src/ui/windows.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ui/windows.c b/src/ui/windows.c index 87b61ab1..88c811e5 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -207,9 +207,7 @@ void wins_refresh_current(void) { ProfWin *window = wins_get_current(); - int rows, cols; - getmaxyx(stdscr, rows, cols); - prefresh(window->win, window->y_pos, 0, 1, 0, rows-3, cols-1); + win_refresh(window); } void From 4c64169d5b56e94185a8722563a52b55fe4b754f Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:28:11 +0100 Subject: [PATCH 10/33] Rename window in ui_handle_error_message --- src/ui/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index d5418cef..eaca033f 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -367,8 +367,8 @@ ui_handle_error_message(const char * const from, const char * const err_msg) if (err_msg == NULL) { cons_show_error("Unknown error received from service."); } else { - ProfWin *win = wins_get_current(); - gboolean handled = win->handle_error_message(win, from, err_msg); + ProfWin *current = wins_get_current(); + gboolean handled = current->handle_error_message(current, from, err_msg); if (handled != TRUE) { cons_show_error("Error received from server: %s", err_msg); } From 7eb5bb6c3d340bd15339962a6b646481c046d69a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:39:27 +0100 Subject: [PATCH 11/33] Moved check for PREF_STATUSES --- src/ui/core.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index eaca033f..af71bb00 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -400,13 +400,15 @@ ui_contact_online(const char * const barejid, const char * const resource, } ProfWin *console = wins_get_console(); - _show_status_string(console, display_str->str, show, status, last_activity, - "++", "online"); - ProfWin *window = wins_get_by_recipient(barejid); - if (window != NULL) { - _show_status_string(window, display_str->str, show, status, - last_activity, "++", "online"); + + if (prefs_get_boolean(PREF_STATUSES)) { + _show_status_string(console, display_str->str, show, status, last_activity, + "++", "online"); + if (window != NULL) { + _show_status_string(window, display_str->str, show, status, + last_activity, "++", "online"); + } } jid_destroy(jid); @@ -442,13 +444,15 @@ ui_contact_offline(const char * const from, const char * const show, } ProfWin *console = wins_get_console(); - _show_status_string(console, display_str->str, show, status, NULL, "--", - "offline"); - ProfWin *window = wins_get_by_recipient(jidp->barejid); - if (window != NULL) { - _show_status_string(window, display_str->str, show, status, NULL, "--", + + if (prefs_get_boolean(PREF_STATUSES)) { + _show_status_string(console, display_str->str, show, status, NULL, "--", "offline"); + if (window != NULL) { + _show_status_string(window, display_str->str, show, status, NULL, "--", + "offline"); + } } jid_destroy(jidp); @@ -922,7 +926,9 @@ ui_new_chat_win(const char * const to) if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - _show_status_string(window, to, show, status, NULL, "--", "offline"); + if (prefs_get_boolean(PREF_STATUSES)) { + _show_status_string(window, to, show, status, NULL, "--", "offline"); + } } } } else { @@ -1030,7 +1036,9 @@ ui_outgoing_msg(const char * const from, const char * const to, if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - _show_status_string(window, to, show, status, NULL, "--", "offline"); + if (prefs_get_boolean(PREF_STATUSES)) { + _show_status_string(window, to, show, status, NULL, "--", "offline"); + } } } @@ -1161,7 +1169,9 @@ ui_room_member_presence(const char * const room, const char * const nick, ProfWin *window = wins_get_by_recipient(room); if (window != NULL) { - _show_status_string(window, nick, show, status, NULL, "++", "online"); + if (prefs_get_boolean(PREF_STATUSES)) { + _show_status_string(window, nick, show, status, NULL, "++", "online"); + } } if (wins_is_current(window)) { @@ -1490,9 +1500,6 @@ _show_status_string(ProfWin *window, const char * const from, { WINDOW *win = window->win; - if (!prefs_get_boolean(PREF_STATUSES)) - return; - win_print_time(window, '-'); if (show != NULL) { From 1d3256ffc7af7a575d692b796930868922a72e85 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:46:35 +0100 Subject: [PATCH 12/33] PREF_STATUSES, moved checks to profanity module Removed checks from muc presence, and when contact is offline on message --- src/profanity.c | 4 ++-- src/ui/core.c | 38 ++++++++++++++------------------------ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 4c621ffa..19613f7d 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -402,7 +402,7 @@ prof_handle_contact_online(char *contact, Resource *resource, { gboolean updated = roster_update_presence(contact, resource, last_activity); - if (updated) { + if (updated && prefs_get_boolean(PREF_STATUSES)) { PContact result = roster_get_contact(contact); if (p_contact_subscription(result) != NULL) { if (strcmp(p_contact_subscription(result), "none") != 0) { @@ -419,7 +419,7 @@ prof_handle_contact_offline(char *contact, char *resource, char *status) { gboolean updated = roster_contact_offline(contact, resource, status); - if (resource != NULL && updated) { + if (resource != NULL && updated && prefs_get_boolean(PREF_STATUSES)) { Jid *jid = jid_create_from_bare_and_resource(contact, resource); PContact result = roster_get_contact(contact); if (p_contact_subscription(result) != NULL) { diff --git a/src/ui/core.c b/src/ui/core.c index af71bb00..682f7a55 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -400,15 +400,13 @@ ui_contact_online(const char * const barejid, const char * const resource, } ProfWin *console = wins_get_console(); - ProfWin *window = wins_get_by_recipient(barejid); + _show_status_string(console, display_str->str, show, status, last_activity, + "++", "online"); - if (prefs_get_boolean(PREF_STATUSES)) { - _show_status_string(console, display_str->str, show, status, last_activity, - "++", "online"); - if (window != NULL) { - _show_status_string(window, display_str->str, show, status, - last_activity, "++", "online"); - } + ProfWin *window = wins_get_by_recipient(barejid); + if (window != NULL) { + _show_status_string(window, display_str->str, show, status, + last_activity, "++", "online"); } jid_destroy(jid); @@ -444,15 +442,13 @@ ui_contact_offline(const char * const from, const char * const show, } ProfWin *console = wins_get_console(); - ProfWin *window = wins_get_by_recipient(jidp->barejid); + _show_status_string(console, display_str->str, show, status, NULL, "--", + "offline"); - if (prefs_get_boolean(PREF_STATUSES)) { - _show_status_string(console, display_str->str, show, status, NULL, "--", + ProfWin *window = wins_get_by_recipient(jidp->barejid); + if (window != NULL) { + _show_status_string(window, display_str->str, show, status, NULL, "--", "offline"); - if (window != NULL) { - _show_status_string(window, display_str->str, show, status, NULL, "--", - "offline"); - } } jid_destroy(jidp); @@ -926,9 +922,7 @@ ui_new_chat_win(const char * const to) if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - if (prefs_get_boolean(PREF_STATUSES)) { - _show_status_string(window, to, show, status, NULL, "--", "offline"); - } + _show_status_string(window, to, show, status, NULL, "--", "offline"); } } } else { @@ -1036,9 +1030,7 @@ ui_outgoing_msg(const char * const from, const char * const to, if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - if (prefs_get_boolean(PREF_STATUSES)) { - _show_status_string(window, to, show, status, NULL, "--", "offline"); - } + _show_status_string(window, to, show, status, NULL, "--", "offline"); } } @@ -1169,9 +1161,7 @@ ui_room_member_presence(const char * const room, const char * const nick, ProfWin *window = wins_get_by_recipient(room); if (window != NULL) { - if (prefs_get_boolean(PREF_STATUSES)) { - _show_status_string(window, nick, show, status, NULL, "++", "online"); - } + _show_status_string(window, nick, show, status, NULL, "++", "online"); } if (wins_is_current(window)) { From 9bea1ce83096bd0a8fe17647f4635adb7bdac904 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 23:52:50 +0100 Subject: [PATCH 13/33] Moved win_show_status function to window module --- src/ui/core.c | 101 ++++-------------------------------------------- src/ui/window.c | 83 +++++++++++++++++++++++++++++++++++++++ src/ui/window.h | 4 ++ 3 files changed, 94 insertions(+), 94 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 682f7a55..43e95ab7 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -64,10 +64,6 @@ static GTimer *ui_idle_time; static void _win_show_user(WINDOW *win, const char * const user, const int colour); static void _win_show_message(WINDOW *win, const char * const message); static void _win_show_error_msg(WINDOW *win, const char * const message); -static void _show_status_string(ProfWin *window, const char * const from, - const char * const show, const char * const status, - GDateTime *last_activity, const char * const pre, - const char * const default_show); static void _win_handle_switch(const wint_t * const ch); static void _win_handle_page(const wint_t * const ch); static void _win_show_history(WINDOW *win, int win_index, @@ -400,12 +396,12 @@ ui_contact_online(const char * const barejid, const char * const resource, } ProfWin *console = wins_get_console(); - _show_status_string(console, display_str->str, show, status, last_activity, + win_show_status_string(console, display_str->str, show, status, last_activity, "++", "online"); ProfWin *window = wins_get_by_recipient(barejid); if (window != NULL) { - _show_status_string(window, display_str->str, show, status, + win_show_status_string(window, display_str->str, show, status, last_activity, "++", "online"); } @@ -442,12 +438,12 @@ ui_contact_offline(const char * const from, const char * const show, } ProfWin *console = wins_get_console(); - _show_status_string(console, display_str->str, show, status, NULL, "--", + win_show_status_string(console, display_str->str, show, status, NULL, "--", "offline"); ProfWin *window = wins_get_by_recipient(jidp->barejid); if (window != NULL) { - _show_status_string(window, display_str->str, show, status, NULL, "--", + win_show_status_string(window, display_str->str, show, status, NULL, "--", "offline"); } @@ -922,7 +918,7 @@ ui_new_chat_win(const char * const to) if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - _show_status_string(window, to, show, status, NULL, "--", "offline"); + win_show_status_string(window, to, show, status, NULL, "--", "offline"); } } } else { @@ -1030,7 +1026,7 @@ ui_outgoing_msg(const char * const from, const char * const to, if (strcmp(p_contact_presence(contact), "offline") == 0) { const char const *show = p_contact_presence(contact); const char const *status = p_contact_status(contact); - _show_status_string(window, to, show, status, NULL, "--", "offline"); + win_show_status_string(window, to, show, status, NULL, "--", "offline"); } } @@ -1161,7 +1157,7 @@ ui_room_member_presence(const char * const room, const char * const nick, ProfWin *window = wins_get_by_recipient(room); if (window != NULL) { - _show_status_string(window, nick, show, status, NULL, "++", "online"); + win_show_status_string(window, nick, show, status, NULL, "++", "online"); } if (wins_is_current(window)) { @@ -1482,89 +1478,6 @@ _win_show_error_msg(WINDOW *win, const char * const message) wattroff(win, COLOUR_ERROR); } -static void -_show_status_string(ProfWin *window, const char * const from, - const char * const show, const char * const status, - GDateTime *last_activity, const char * const pre, - const char * const default_show) -{ - WINDOW *win = window->win; - - win_print_time(window, '-'); - - if (show != NULL) { - if (strcmp(show, "away") == 0) { - wattron(win, COLOUR_AWAY); - } else if (strcmp(show, "chat") == 0) { - wattron(win, COLOUR_CHAT); - } else if (strcmp(show, "dnd") == 0) { - wattron(win, COLOUR_DND); - } else if (strcmp(show, "xa") == 0) { - wattron(win, COLOUR_XA); - } else if (strcmp(show, "online") == 0) { - wattron(win, COLOUR_ONLINE); - } else { - wattron(win, COLOUR_OFFLINE); - } - } else if (strcmp(default_show, "online") == 0) { - wattron(win, COLOUR_ONLINE); - } else { - wattron(win, COLOUR_OFFLINE); - } - - wprintw(win, "%s %s", pre, from); - - if (show != NULL) - wprintw(win, " is %s", show); - else - wprintw(win, " is %s", default_show); - - if (last_activity != NULL) { - GDateTime *now = g_date_time_new_now_local(); - GTimeSpan span = g_date_time_difference(now, last_activity); - - wprintw(win, ", idle "); - - int hours = span / G_TIME_SPAN_HOUR; - span = span - hours * G_TIME_SPAN_HOUR; - if (hours > 0) { - wprintw(win, "%dh", hours); - } - - int minutes = span / G_TIME_SPAN_MINUTE; - span = span - minutes * G_TIME_SPAN_MINUTE; - wprintw(win, "%dm", minutes); - - int seconds = span / G_TIME_SPAN_SECOND; - wprintw(win, "%ds", seconds); - } - - if (status != NULL) - wprintw(win, ", \"%s\"", status); - - wprintw(win, "\n"); - - if (show != NULL) { - if (strcmp(show, "away") == 0) { - wattroff(win, COLOUR_AWAY); - } else if (strcmp(show, "chat") == 0) { - wattroff(win, COLOUR_CHAT); - } else if (strcmp(show, "dnd") == 0) { - wattroff(win, COLOUR_DND); - } else if (strcmp(show, "xa") == 0) { - wattroff(win, COLOUR_XA); - } else if (strcmp(show, "online") == 0) { - wattroff(win, COLOUR_ONLINE); - } else { - wattroff(win, COLOUR_OFFLINE); - } - } else if (strcmp(default_show, "online") == 0) { - wattroff(win, COLOUR_ONLINE); - } else { - wattroff(win, COLOUR_OFFLINE); - } -} - static void _win_handle_switch(const wint_t * const ch) { diff --git a/src/ui/window.c b/src/ui/window.c index d21297a3..caad2531 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -216,6 +216,89 @@ win_show_contact(ProfWin *window, PContact contact) win_presence_colour_off(window, presence); } +void +win_show_status_string(ProfWin *window, const char * const from, + const char * const show, const char * const status, + GDateTime *last_activity, const char * const pre, + const char * const default_show) +{ + WINDOW *win = window->win; + + win_print_time(window, '-'); + + if (show != NULL) { + if (strcmp(show, "away") == 0) { + wattron(win, COLOUR_AWAY); + } else if (strcmp(show, "chat") == 0) { + wattron(win, COLOUR_CHAT); + } else if (strcmp(show, "dnd") == 0) { + wattron(win, COLOUR_DND); + } else if (strcmp(show, "xa") == 0) { + wattron(win, COLOUR_XA); + } else if (strcmp(show, "online") == 0) { + wattron(win, COLOUR_ONLINE); + } else { + wattron(win, COLOUR_OFFLINE); + } + } else if (strcmp(default_show, "online") == 0) { + wattron(win, COLOUR_ONLINE); + } else { + wattron(win, COLOUR_OFFLINE); + } + + wprintw(win, "%s %s", pre, from); + + if (show != NULL) + wprintw(win, " is %s", show); + else + wprintw(win, " is %s", default_show); + + if (last_activity != NULL) { + GDateTime *now = g_date_time_new_now_local(); + GTimeSpan span = g_date_time_difference(now, last_activity); + + wprintw(win, ", idle "); + + int hours = span / G_TIME_SPAN_HOUR; + span = span - hours * G_TIME_SPAN_HOUR; + if (hours > 0) { + wprintw(win, "%dh", hours); + } + + int minutes = span / G_TIME_SPAN_MINUTE; + span = span - minutes * G_TIME_SPAN_MINUTE; + wprintw(win, "%dm", minutes); + + int seconds = span / G_TIME_SPAN_SECOND; + wprintw(win, "%ds", seconds); + } + + if (status != NULL) + wprintw(win, ", \"%s\"", status); + + wprintw(win, "\n"); + + if (show != NULL) { + if (strcmp(show, "away") == 0) { + wattroff(win, COLOUR_AWAY); + } else if (strcmp(show, "chat") == 0) { + wattroff(win, COLOUR_CHAT); + } else if (strcmp(show, "dnd") == 0) { + wattroff(win, COLOUR_DND); + } else if (strcmp(show, "xa") == 0) { + wattroff(win, COLOUR_XA); + } else if (strcmp(show, "online") == 0) { + wattroff(win, COLOUR_ONLINE); + } else { + wattroff(win, COLOUR_OFFLINE); + } + } else if (strcmp(default_show, "online") == 0) { + wattroff(win, COLOUR_ONLINE); + } else { + wattroff(win, COLOUR_OFFLINE); + } +} + static gboolean _default_handle_error_message(ProfWin *self, const char * const from, const char * const err_msg) diff --git a/src/ui/window.h b/src/ui/window.h index bfd9b267..00e905e1 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -67,5 +67,9 @@ void win_print_time(ProfWin *window, char show_char); void win_presence_colour_on(ProfWin *window, const char * const presence); void win_presence_colour_off(ProfWin *window, const char * const presence); void win_show_contact(ProfWin *window, PContact contact); +void win_show_status_string(ProfWin *window, const char * const from, + const char * const show, const char * const status, + GDateTime *last_activity, const char * const pre, + const char * const default_show); #endif From 383d91ec36b7b4afad79dffe2049dabbe2ec93fa Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 00:16:58 +0100 Subject: [PATCH 14/33] Added p_contact_create_display_string --- src/contact.c | 25 +++++++++++++++++++++++++ src/contact.h | 1 + src/ui/core.c | 46 ++++++++-------------------------------------- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/contact.c b/src/contact.c index 9cc992b2..0b955548 100644 --- a/src/contact.c +++ b/src/contact.c @@ -166,6 +166,31 @@ p_contact_name_or_jid(const PContact contact) } } +char * +p_contact_create_display_string(const PContact contact, const char * const resource) +{ + GString *result_str = g_string_new(""); + + // use nickname if exists + if (contact->name != NULL) { + g_string_append(result_str, contact->name); + } else { + g_string_append(result_str, contact->barejid); + } + + // add resource if not default provided by profanity + if (strcmp(resource, "__prof_default") != 0) { + g_string_append(result_str, " ("); + g_string_append(result_str, resource); + g_string_append(result_str, ")"); + } + + char *result = result_str->str; + g_string_free(result_str, FALSE); + + return result; +} + static Resource * _highest_presence(Resource *first, Resource *second) { diff --git a/src/contact.h b/src/contact.h index e71e0f7d..998e5f8b 100644 --- a/src/contact.h +++ b/src/contact.h @@ -55,5 +55,6 @@ void p_contact_set_groups(const PContact contact, GSList *groups); GSList * p_contact_groups(const PContact contact); gboolean p_contact_in_group(const PContact contact, const char * const group); gboolean p_contact_subscribed(const PContact contact); +char * p_contact_create_display_string(const PContact contact, const char * const resource); #endif diff --git a/src/ui/core.c b/src/ui/core.c index 43e95ab7..6a6c15c4 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -377,36 +377,20 @@ void ui_contact_online(const char * const barejid, const char * const resource, const char * const show, const char * const status, GDateTime *last_activity) { - Jid *jid = jid_create_from_bare_and_resource(barejid, resource); PContact contact = roster_get_contact(barejid); - GString *display_str = g_string_new(""); - - // use nickname if exists - if (p_contact_name(contact) != NULL) { - g_string_append(display_str, p_contact_name(contact)); - } else { - g_string_append(display_str, barejid); - } - - // add resource if not default provided by profanity - if (strcmp(jid->resourcepart, "__prof_default") != 0) { - g_string_append(display_str, " ("); - g_string_append(display_str, jid->resourcepart); - g_string_append(display_str, ")"); - } + char *display_str = p_contact_create_display_string(contact, resource); ProfWin *console = wins_get_console(); - win_show_status_string(console, display_str->str, show, status, last_activity, + win_show_status_string(console, display_str, show, status, last_activity, "++", "online"); ProfWin *window = wins_get_by_recipient(barejid); if (window != NULL) { - win_show_status_string(window, display_str->str, show, status, + win_show_status_string(window, display_str, show, status, last_activity, "++", "online"); } - jid_destroy(jid); - g_string_free(display_str, TRUE); + free(display_str); if (wins_is_current(console)) { wins_refresh_current(); @@ -421,34 +405,20 @@ ui_contact_offline(const char * const from, const char * const show, { Jid *jidp = jid_create(from); PContact contact = roster_get_contact(jidp->barejid); - GString *display_str = g_string_new(""); - - // use nickname if exists - if (p_contact_name(contact) != NULL) { - g_string_append(display_str, p_contact_name(contact)); - } else { - g_string_append(display_str, jidp->barejid); - } - - // add resource if not default provided by profanity - if (strcmp(jidp->resourcepart, "__prof_default") != 0) { - g_string_append(display_str, " ("); - g_string_append(display_str, jidp->resourcepart); - g_string_append(display_str, ")"); - } + char *display_str = p_contact_create_display_string(contact, jidp->resourcepart); ProfWin *console = wins_get_console(); - win_show_status_string(console, display_str->str, show, status, NULL, "--", + win_show_status_string(console, display_str, show, status, NULL, "--", "offline"); ProfWin *window = wins_get_by_recipient(jidp->barejid); if (window != NULL) { - win_show_status_string(window, display_str->str, show, status, NULL, "--", + win_show_status_string(window, display_str, show, status, NULL, "--", "offline"); } jid_destroy(jidp); - g_string_free(display_str, TRUE); + free(display_str); if (wins_is_current(console)) { wins_refresh_current(); From acf1afe02538d51b7b613b75334c824e2d314cf4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 00:51:00 +0100 Subject: [PATCH 15/33] Moved idle chat state handling out of ui module --- src/profanity.c | 26 +++++++++++++++++++++++++- src/ui/core.c | 28 +++------------------------- src/ui/ui.h | 2 +- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/profanity.c b/src/profanity.c index 19613f7d..06067d93 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -453,7 +453,31 @@ prof_handle_idle(void) { jabber_conn_status_t status = jabber_get_connection_status(); if (status == JABBER_CONNECTED) { - ui_idle(); + GSList *recipients = ui_get_recipients(); + GSList *curr = recipients; + + while (curr != NULL) { + char *recipient = curr->data; + chat_session_no_activity(recipient); + + if (chat_session_is_gone(recipient) && + !chat_session_get_sent(recipient)) { + message_send_gone(recipient); + } else if (chat_session_is_inactive(recipient) && + !chat_session_get_sent(recipient)) { + message_send_inactive(recipient); + } else if (prefs_get_boolean(PREF_OUTTYPE) && + chat_session_is_paused(recipient) && + !chat_session_get_sent(recipient)) { + message_send_paused(recipient); + } + + curr = g_slist_next(curr); + } + + if (recipients != NULL) { + g_slist_free(recipients); + } } } diff --git a/src/ui/core.c b/src/ui/core.c index 6a6c15c4..24e8b822 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -211,33 +211,11 @@ ui_contact_typing(const char * const barejid) } } -void -ui_idle(void) +GSList * +ui_get_recipients(void) { GSList *recipients = wins_get_chat_recipients(); - GSList *curr = recipients; - while (curr != NULL) { - char *recipient = curr->data; - chat_session_no_activity(recipient); - - if (chat_session_is_gone(recipient) && - !chat_session_get_sent(recipient)) { - message_send_gone(recipient); - } else if (chat_session_is_inactive(recipient) && - !chat_session_get_sent(recipient)) { - message_send_inactive(recipient); - } else if (prefs_get_boolean(PREF_OUTTYPE) && - chat_session_is_paused(recipient) && - !chat_session_get_sent(recipient)) { - message_send_paused(recipient); - } - - curr = g_slist_next(curr); - } - - if (recipients != NULL) { - g_slist_free(recipients); - } + return recipients; } void diff --git a/src/ui/ui.h b/src/ui/ui.h index 7fff0703..9c1cba52 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -48,7 +48,7 @@ void ui_refresh(void); void ui_close(void); void ui_resize(const int ch, const char * const input, const int size); -void ui_idle(void); +GSList* ui_get_recipients(void); void ui_handle_special_keys(const wint_t * const ch, const char * const inp, const int size); void ui_switch_win(const int i); From 2d534fb278e85b9618852a8a4312eda0352666f8 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 00:59:17 +0100 Subject: [PATCH 16/33] Added win_page_off --- src/ui/core.c | 14 ++------------ src/ui/window.c | 15 +++++++++++++++ src/ui/window.h | 1 + 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 24e8b822..6a49bfb4 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -744,18 +744,8 @@ void ui_current_page_off(void) { ProfWin *current = wins_get_current(); - current->paged = 0; - - int rows = getmaxy(stdscr); - int y = getcury(current->win); - int size = rows - 3; - - current->y_pos = y - (size - 1); - if (current->y_pos < 0) { - current->y_pos = 0; - } - - wins_refresh_current(); + win_page_off(current); + win_refresh(current); } void diff --git a/src/ui/window.c b/src/ui/window.c index caad2531..135eac17 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -132,6 +132,21 @@ win_refresh(ProfWin *window) prefresh(window->win, window->y_pos, 0, 1, 0, rows-3, cols-1); } +void +win_page_off(ProfWin *window) +{ + window->paged = 0; + + int rows = getmaxy(stdscr); + int y = getcury(window->win); + int size = rows - 3; + + window->y_pos = y - (size - 1); + if (window->y_pos < 0) { + window->y_pos = 0; + } +} + void win_presence_colour_on(ProfWin *window, const char * const presence) { diff --git a/src/ui/window.h b/src/ui/window.h index 00e905e1..dea6214d 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -63,6 +63,7 @@ void win_free(ProfWin *window); void win_print_line(ProfWin *self, const char show_char, int attrs, const char * const msg, ...); void win_refresh(ProfWin *window); +void win_page_off(ProfWin *window); void win_print_time(ProfWin *window, char show_char); void win_presence_colour_on(ProfWin *window, const char * const presence); void win_presence_colour_off(ProfWin *window, const char * const presence); From 63f594b49493c5f6d697b19f7f627106b90a2f24 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 01:06:19 +0100 Subject: [PATCH 17/33] Refactor ui_recipient_gone --- src/ui/core.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 6a49bfb4..2ce3d0ea 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -815,12 +815,7 @@ ui_recipient_gone(const char * const barejid) ProfWin *window = wins_get_by_recipient(barejid); if (window != NULL) { - win_print_time(window, '!'); - wattron(window->win, COLOUR_GONE); - wprintw(window->win, "<- %s ", display_usr); - wprintw(window->win, "has left the conversation."); - wprintw(window->win, "\n"); - wattroff(window->win, COLOUR_GONE); + win_print_line(window, '!', COLOUR_GONE, "<- %s has left the conversation.", display_usr); if (wins_is_current(window)) { wins_refresh_current(); } From c253d3cd1be5373e73dffd14142c9e4364170395 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 01:08:46 +0100 Subject: [PATCH 18/33] Refactor ui_create_duck_win --- src/ui/core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 2ce3d0ea..539988f0 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -867,8 +867,7 @@ ui_create_duck_win(void) ProfWin *window = wins_new("DuckDuckGo search", WIN_DUCK); int num = wins_get_num(window); ui_switch_win(num); - win_print_time(window, '-'); - wprintw(window->win, "Type ':help' to find out more.\n"); + win_print_line(window, '-', 0, "Type ':help' to find out more."); } void From 4cbfd5f0f989a2b34c00f349074982d4de414c59 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 01:30:20 +0100 Subject: [PATCH 19/33] Removed _win_show_error_msg --- src/ui/core.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 539988f0..57155e03 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -63,7 +63,6 @@ static GTimer *ui_idle_time; static void _win_show_user(WINDOW *win, const char * const user, const int colour); static void _win_show_message(WINDOW *win, const char * const message); -static void _win_show_error_msg(WINDOW *win, const char * const message); static void _win_handle_switch(const wint_t * const ch); static void _win_handle_page(const wint_t * const ch); static void _win_show_history(WINDOW *win, int win_index, @@ -756,8 +755,7 @@ ui_print_error_from_recipient(const char * const from, const char *err_msg) ProfWin *window = wins_get_by_recipient(from); if (window != NULL) { - win_print_time(window, '-'); - _win_show_error_msg(window->win, err_msg); + win_print_line(window, '-', COLOUR_ERROR, "%s", err_msg); if (wins_is_current(window)) { wins_refresh_current(); } @@ -1402,14 +1400,6 @@ _win_show_message(WINDOW *win, const char * const message) wprintw(win, "\n"); } -static void -_win_show_error_msg(WINDOW *win, const char * const message) -{ - wattron(win, COLOUR_ERROR); - wprintw(win, "%s\n", message); - wattroff(win, COLOUR_ERROR); -} - static void _win_handle_switch(const wint_t * const ch) { From 067c26eeea871fabeb2b475b8e6d3c7efe1d5180 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 01:35:20 +0100 Subject: [PATCH 20/33] Tidy _ui_draw_win_title --- src/ui/core.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 57155e03..7374bda7 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -1359,12 +1359,17 @@ _ui_draw_win_title(void) gint unread = ui_unread(); if (unread != 0) { - snprintf(new_win_title, sizeof(new_win_title), "%c]0;%s%s (%d) - %s%c", '\033', "Profanity", version_str->str, unread, jid, '\007'); + snprintf(new_win_title, sizeof(new_win_title), + "%c]0;%s%s (%d) - %s%c", '\033', "Profanity", version_str->str, + unread, jid, '\007'); } else { - snprintf(new_win_title, sizeof(new_win_title), "%c]0;%s%s - %s%c", '\033', "Profanity", version_str->str, jid, '\007'); + snprintf(new_win_title, sizeof(new_win_title), + "%c]0;%s%s - %s%c", '\033', "Profanity", version_str->str, jid, + '\007'); } } else { - snprintf(new_win_title, sizeof(new_win_title), "%c]0;%s%s%c", '\033', "Profanity", version_str->str, '\007'); + snprintf(new_win_title, sizeof(new_win_title), "%c]0;%s%s%c", '\033', + "Profanity", version_str->str, '\007'); } g_string_free(version_str, TRUE); From 3e86d108a314a067589e32b60d2f6374eae501b6 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Oct 2013 01:39:05 +0100 Subject: [PATCH 21/33] Removed usages of ui_current_print_line from ui/core.c --- src/ui/core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 7374bda7..e436a4af 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -1280,7 +1280,7 @@ ui_status(void) if (pcontact != NULL) { win_show_contact(current, pcontact); } else { - ui_current_print_line("Error getting contact info."); + win_print_line(current, '-', 0, "Error getting contact info."); } } @@ -1294,7 +1294,7 @@ ui_status_private(void) if (pcontact != NULL) { win_show_contact(current, pcontact); } else { - ui_current_print_line("Error getting contact info."); + win_print_line(current, '-', 0, "Error getting contact info."); } jid_destroy(jid); @@ -1309,7 +1309,7 @@ ui_status_room(const char * const contact) if (pcontact != NULL) { win_show_contact(current, pcontact); } else { - ui_current_print_line("No such participant \"%s\" in room.", contact); + win_print_line(current, '-', 0, "No such participant \"%s\" in room.", contact); } } From 107fdd355e487793e53799ce3ebed0659263a2f7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 15:34:17 +0000 Subject: [PATCH 22/33] Added simple mock test, refactored roster --- Makefile.am | 52 +- src/command/command.c | 8 +- src/command/command.h | 2 + src/profanity.c | 2 +- src/roster_list.c | 475 +++++++++++++++++ src/roster_list.h | 58 +++ src/ui/console.c | 2 +- src/ui/core.c | 2 +- src/ui/inputwin.c | 1 + src/ui/muc_window.h | 4 +- src/ui/notifier.h | 33 -- src/ui/ui.h | 17 +- src/ui/window.h | 4 +- src/ui/windows.c | 1 + src/ui/windows.h | 4 +- src/xmpp/bookmark.h | 9 +- src/xmpp/capabilities.h | 4 +- src/xmpp/connection.h | 4 +- src/xmpp/iq.c | 1 + src/xmpp/iq.h | 4 +- src/xmpp/message.c | 1 + src/xmpp/message.h | 4 +- src/xmpp/presence.h | 4 +- src/xmpp/roster.c | 535 +++----------------- src/xmpp/roster.h | 4 +- src/xmpp/stanza.h | 4 +- src/xmpp/xmpp.h | 40 +- tests/{test_roster.c => test_roster_list.c} | 6 +- tests/testsuite.c | 2 +- tests/testsuite.h | 2 +- 30 files changed, 705 insertions(+), 584 deletions(-) create mode 100644 src/roster_list.c create mode 100644 src/roster_list.h delete mode 100644 src/ui/notifier.h rename tests/{test_roster.c => test_roster_list.c} (98%) diff --git a/Makefile.am b/Makefile.am index 88bed76f..e001a87e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,7 @@ core_sources = \ src/profanity.h src/chat_session.c \ src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ src/resource.c src/resource.h \ + src/roster_list.c src/roster_list.h \ src/xmpp/xmpp.h src/xmpp/capabilities.c src/xmpp/connection.c \ src/xmpp/iq.c src/xmpp/message.c src/xmpp/presence.c src/xmpp/stanza.c \ src/xmpp/stanza.h src/xmpp/message.h src/xmpp/iq.h src/xmpp/presence.h \ @@ -26,7 +27,7 @@ core_sources = \ src/xmpp/bookmark.c src/xmpp/bookmark.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ - src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ + src/ui/console.c src/ui/notifier.c \ src/ui/windows.c src/ui/windows.h \ src/ui/muc_window.c src/ui/muc_window.h \ src/command/command.h src/command/command.c src/command/history.c \ @@ -39,10 +40,47 @@ core_sources = \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h +#test_sources = \ +# src/contact.c src/contact.h src/log.c src/common.c \ +# src/log.h src/profanity.c src/common.h \ +# src/profanity.h src/chat_session.c \ +# src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ +# src/resource.c src/resource.h \ +# src/roster_list.c src/roster_list.h \ +# src/xmpp/xmpp.h tests/xmpp/mock_xmpp.c \ +# src/ui/ui.h tests/ui/mock_ui.c \ +# src/command/command.h src/command/command.c src/command/history.c \ +# src/command/history.h src/tools/parser.c \ +# src/tools/parser.h \ +# src/tools/autocomplete.c src/tools/autocomplete.h \ +# src/tools/history.c src/tools/history.h \ +# src/tools/tinyurl.c src/tools/tinyurl.h \ +# src/config/accounts.c src/config/accounts.h \ +# src/config/preferences.c src/config/preferences.h \ +# src/config/theme.c src/config/theme.h \ +# tests/test_roster_list.c tests/test_common.c tests/test_history.c \ +# tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \ +# tests/test_jid.c tests/test_command.c + test_sources = \ - tests/test_roster.c tests/test_common.c tests/test_history.c \ - tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \ - tests/test_jid.c + src/contact.c src/contact.h src/log.c src/common.c \ + src/log.h src/profanity.c src/common.h \ + src/profanity.h src/chat_session.c \ + src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ + src/resource.c src/resource.h \ + src/roster_list.c src/roster_list.h \ + src/xmpp/xmpp.h tests/xmpp/mock_xmpp.c \ + src/ui/ui.h tests/ui/mock_ui.c \ + src/command/command.h src/command/command.c src/command/history.c \ + src/command/history.h src/tools/parser.c \ + src/tools/parser.h \ + src/tools/autocomplete.c src/tools/autocomplete.h \ + src/tools/history.c src/tools/history.h \ + src/tools/tinyurl.c src/tools/tinyurl.h \ + src/config/accounts.c src/config/accounts.h \ + src/config/preferences.c src/config/preferences.h \ + src/config/theme.c src/config/theme.h \ + tests/test_command.c main_source = src/main.c @@ -51,8 +89,10 @@ git_sources = \ if INCLUDE_GIT_VERSION with_git_sources = $(git_sources) $(core_sources) +tests_with_git_sources = $(git_sources) $(test_sources) else with_git_sources = $(core_sources) +tests_with_git_sources = $(test_sources) endif bin_PROGRAMS = profanity @@ -60,7 +100,7 @@ profanity_SOURCES = $(with_git_sources) $(main_source) TESTS = tests/testsuite check_PROGRAMS = tests/testsuite -tests_testsuite_SOURCES = $(with_git_sources) $(test_sources) -tests_testsuite_LDADD = -lheadunit -lstdc++ +tests_testsuite_SOURCES = $(tests_with_git_sources) +tests_testsuite_LDADD = -lheadunit -lstdc++ -lcmocka man_MANS = docs/profanity.1 diff --git a/src/command/command.c b/src/command/command.c index a213d141..0ab8b013 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -36,6 +36,7 @@ #include "config/preferences.h" #include "config/theme.h" #include "contact.h" +#include "roster_list.h" #include "jid.h" #include "log.h" #include "muc.h" @@ -133,7 +134,6 @@ static gboolean _cmd_prefs(gchar **args, struct cmd_help_t help); static gboolean _cmd_priority(gchar **args, struct cmd_help_t help); static gboolean _cmd_quit(gchar **args, struct cmd_help_t help); static gboolean _cmd_reconnect(gchar **args, struct cmd_help_t help); -static gboolean _cmd_rooms(gchar **args, struct cmd_help_t help); static gboolean _cmd_bookmark(gchar **args, struct cmd_help_t help); static gboolean _cmd_roster(gchar **args, struct cmd_help_t help); static gboolean _cmd_software(gchar **args, struct cmd_help_t help); @@ -2455,7 +2455,7 @@ _cmd_roster(gchar **args, struct cmd_help_t help) char *jid = args[1]; - roster_remove(jid); + roster_send_remove(jid); return TRUE; } @@ -2916,13 +2916,13 @@ _cmd_decline(gchar **args, struct cmd_help_t help) return TRUE; } -static gboolean +gboolean _cmd_rooms(gchar **args, struct cmd_help_t help) { jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currenlty connected."); + cons_show("You are not currently connected."); return TRUE; } diff --git a/src/command/command.h b/src/command/command.h index c20413c8..8743d662 100644 --- a/src/command/command.h +++ b/src/command/command.h @@ -49,4 +49,6 @@ void cmd_history_append(char *inp); char *cmd_history_previous(char *inp, int *size); char *cmd_history_next(char *inp, int *size); +gboolean _cmd_rooms(gchar **args, struct cmd_help_t help); + #endif diff --git a/src/profanity.c b/src/profanity.c index 6bd7e2f0..d712d426 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -42,10 +42,10 @@ #include "command/command.h" #include "common.h" #include "contact.h" +#include "roster_list.h" #include "log.h" #include "muc.h" #include "resource.h" -#include "ui/notifier.h" #include "ui/ui.h" #include "xmpp/xmpp.h" diff --git a/src/roster_list.c b/src/roster_list.c new file mode 100644 index 00000000..37031ca4 --- /dev/null +++ b/src/roster_list.c @@ -0,0 +1,475 @@ +/* + * roster_list.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + + +#include +#include +#include + +#include "roster_list.h" +#include "resource.h" +#include "contact.h" +#include "jid.h" +#include "tools/autocomplete.h" +#include "xmpp/xmpp.h" +#include "profanity.h" + +// nicknames +static Autocomplete name_ac; + +// barejids +static Autocomplete barejid_ac; + +// fulljids +static Autocomplete fulljid_ac; + +// groups +static Autocomplete groups_ac; + +// contacts, indexed on barejid +static GHashTable *contacts; + +// nickname to jid map +static GHashTable *name_to_barejid; + +static gboolean _key_equals(void *key1, void *key2); +static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2); +static void _replace_name(const char * const current_name, + const char * const new_name, const char * const barejid); +static void _add_name_and_barejid(const char * const name, + const char * const barejid); +static gint _compare_contacts(PContact a, PContact b); + +void +roster_clear(void) +{ + autocomplete_clear(name_ac); + autocomplete_clear(barejid_ac); + autocomplete_clear(fulljid_ac); + autocomplete_clear(groups_ac); + g_hash_table_destroy(contacts); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, + (GDestroyNotify)p_contact_free); + g_hash_table_destroy(name_to_barejid); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + g_free); +} + +gboolean +roster_update_presence(const char * const barejid, Resource *resource, + GDateTime *last_activity) +{ + assert(barejid != NULL); + assert(resource != NULL); + + PContact contact = g_hash_table_lookup(contacts, barejid); + if (contact == NULL) { + return FALSE; + } + if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { + p_contact_set_last_activity(contact, last_activity); + } + p_contact_set_presence(contact, resource); + Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); + autocomplete_add(fulljid_ac, jid->fulljid); + jid_destroy(jid); + + return TRUE; +} + +PContact +roster_get_contact(const char * const barejid) +{ + return g_hash_table_lookup(contacts, barejid); +} + +gboolean +roster_contact_offline(const char * const barejid, + const char * const resource, const char * const status) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact == NULL) { + return FALSE; + } + if (resource == NULL) { + return TRUE; + } else { + gboolean result = p_contact_remove_resource(contact, resource); + if (result == TRUE) { + Jid *jid = jid_create_from_bare_and_resource(barejid, resource); + autocomplete_remove(fulljid_ac, jid->fulljid); + jid_destroy(jid); + } + + return result; + } +} + +void +roster_reset_search_attempts(void) +{ + autocomplete_reset(name_ac); + autocomplete_reset(barejid_ac); + autocomplete_reset(fulljid_ac); + autocomplete_reset(groups_ac); +} + +void +roster_init(void) +{ + name_ac = autocomplete_new(); + barejid_ac = autocomplete_new(); + fulljid_ac = autocomplete_new(); + groups_ac = autocomplete_new(); + contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, + (GDestroyNotify)p_contact_free); + name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + g_free); +} + +void +roster_free(void) +{ + autocomplete_free(name_ac); + autocomplete_free(barejid_ac); + autocomplete_free(fulljid_ac); + autocomplete_free(groups_ac); +} + +void +roster_change_name(const char * const barejid, const char * const new_name) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + const char * current_name = NULL; + if (p_contact_name(contact) != NULL) { + current_name = strdup(p_contact_name(contact)); + } + + if (contact != NULL) { + p_contact_set_name(contact, new_name); + _replace_name(current_name, new_name, barejid); + + GSList *groups = p_contact_groups(contact); + roster_send_name_change(barejid, new_name, groups); + } +} + +void +roster_remove(const char * const name, const char * const barejid) +{ + autocomplete_remove(barejid_ac, barejid); + autocomplete_remove(name_ac, name); + g_hash_table_remove(name_to_barejid, name); + + // remove each fulljid + PContact contact = roster_get_contact(barejid); + if (contact != NULL) { + GList *resources = p_contact_get_available_resources(contact); + while (resources != NULL) { + GString *fulljid = g_string_new(strdup(barejid)); + g_string_append(fulljid, "/"); + g_string_append(fulljid, resources->data); + autocomplete_remove(fulljid_ac, fulljid->str); + g_string_free(fulljid, TRUE); + resources = g_list_next(resources); + } + } + + // remove the contact + g_hash_table_remove(contacts, barejid); +} + +void +roster_update(const char * const barejid, const char * const name, + GSList *groups, const char * const subscription, gboolean pending_out) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact == NULL) { + roster_add(barejid, name, groups, subscription, pending_out, FALSE); + } else { + p_contact_set_subscription(contact, subscription); + p_contact_set_pending_out(contact, pending_out); + + const char * const new_name = name; + const char * current_name = NULL; + if (p_contact_name(contact) != NULL) { + current_name = strdup(p_contact_name(contact)); + } + + p_contact_set_name(contact, new_name); + p_contact_set_groups(contact, groups); + _replace_name(current_name, new_name, barejid); + + // add groups + while (groups != NULL) { + autocomplete_add(groups_ac, groups->data); + groups = g_slist_next(groups); + } + } +} + +gboolean +roster_add(const char * const barejid, const char * const name, GSList *groups, + const char * const subscription, gboolean pending_out, gboolean from_initial) +{ + gboolean added = FALSE; + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact == NULL) { + contact = p_contact_new(barejid, name, groups, subscription, NULL, + pending_out); + + // add groups + while (groups != NULL) { + autocomplete_add(groups_ac, groups->data); + groups = g_slist_next(groups); + } + + g_hash_table_insert(contacts, strdup(barejid), contact); + autocomplete_add(barejid_ac, barejid); + _add_name_and_barejid(name, barejid); + + if (!from_initial) { + prof_handle_roster_add(barejid, name); + } + + added = TRUE; + } + + return added; +} + +char * +roster_barejid_from_name(const char * const name) +{ + return g_hash_table_lookup(name_to_barejid, name); +} + +GSList * +roster_get_contacts(void) +{ + GSList *result = NULL; + GHashTableIter iter; + gpointer key; + gpointer value; + + g_hash_table_iter_init(&iter, contacts); + while (g_hash_table_iter_next(&iter, &key, &value)) { + result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); + } + + // resturn all contact structs + return result; +} + +gboolean +roster_has_pending_subscriptions(void) +{ + GHashTableIter iter; + gpointer key; + gpointer value; + + g_hash_table_iter_init(&iter, contacts); + while (g_hash_table_iter_next(&iter, &key, &value)) { + PContact contact = (PContact) value; + if (p_contact_pending_out(contact)) { + return TRUE; + } + } + + return FALSE; +} + +char * +roster_find_contact(char *search_str) +{ + return autocomplete_complete(name_ac, search_str); +} + +char * +roster_find_resource(char *search_str) +{ + return autocomplete_complete(fulljid_ac, search_str); +} + +GSList * +roster_get_group(const char * const group) +{ + GSList *result = NULL; + GHashTableIter iter; + gpointer key; + gpointer value; + + g_hash_table_iter_init(&iter, contacts); + while (g_hash_table_iter_next(&iter, &key, &value)) { + GSList *groups = p_contact_groups(value); + while (groups != NULL) { + if (strcmp(groups->data, group) == 0) { + result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); + break; + } + groups = g_slist_next(groups); + } + } + + // resturn all contact structs + return result; +} + +void +roster_add_to_group(const char * const group, const char * const barejid) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact != NULL) { + if (p_contact_in_group(contact, group)) { + if (p_contact_name(contact) != NULL) { + prof_handle_already_in_group(p_contact_name(contact), group); + } else { + prof_handle_already_in_group(p_contact_barejid(contact), group); + } + return; + } + + roster_send_add_to_group(group, contact); + + } +} + +void +roster_remove_from_group(const char * const group, const char * const barejid) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact != NULL) { + if (!p_contact_in_group(contact, group)) { + if (p_contact_name(contact) != NULL) { + prof_handle_not_in_group(p_contact_name(contact), group); + } else { + prof_handle_not_in_group(p_contact_barejid(contact), group); + } + return; + } + + roster_send_remove_from_group(group, contact); + } +} + +GSList * +roster_get_groups(void) +{ + return autocomplete_get_list(groups_ac); +} + +char * +roster_find_group(char *search_str) +{ + return autocomplete_complete(groups_ac, search_str); +} + +char * +roster_find_jid(char *search_str) +{ + return autocomplete_complete(barejid_ac, search_str); +} + +static +gboolean _key_equals(void *key1, void *key2) +{ + gchar *str1 = (gchar *) key1; + gchar *str2 = (gchar *) key2; + + return (g_strcmp0(str1, str2) == 0); +} + +static gboolean +_datetimes_equal(GDateTime *dt1, GDateTime *dt2) +{ + if ((dt1 == NULL) && (dt2 == NULL)) { + return TRUE; + } else if ((dt1 == NULL) && (dt2 != NULL)) { + return FALSE; + } else if ((dt1 != NULL) && (dt2 == NULL)) { + return FALSE; + } else { + return g_date_time_equal(dt1, dt2); + } +} + +static void +_replace_name(const char * const current_name, const char * const new_name, + const char * const barejid) +{ + // current handle exists already + if (current_name != NULL) { + autocomplete_remove(name_ac, current_name); + g_hash_table_remove(name_to_barejid, current_name); + _add_name_and_barejid(new_name, barejid); + // no current handle + } else if (new_name != NULL) { + autocomplete_remove(name_ac, barejid); + g_hash_table_remove(name_to_barejid, barejid); + _add_name_and_barejid(new_name, barejid); + } +} + +static void +_add_name_and_barejid(const char * const name, const char * const barejid) +{ + if (name != NULL) { + autocomplete_add(name_ac, name); + g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); + } else { + autocomplete_add(name_ac, barejid); + g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); + } +} + +static +gint _compare_contacts(PContact a, PContact b) +{ + const char * utf8_str_a = NULL; + const char * utf8_str_b = NULL; + + if (p_contact_name(a) != NULL) { + utf8_str_a = p_contact_name(a); + } else { + utf8_str_a = p_contact_barejid(a); + } + if (p_contact_name(b) != NULL) { + utf8_str_b = p_contact_name(b); + } else { + utf8_str_b = p_contact_barejid(b); + } + + gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); + gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); + + gint result = g_strcmp0(key_a, key_b); + + g_free(key_a); + g_free(key_b); + + return result; +} diff --git a/src/roster_list.h b/src/roster_list.h new file mode 100644 index 00000000..9382081b --- /dev/null +++ b/src/roster_list.h @@ -0,0 +1,58 @@ +/* + * roster_list.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#ifndef ROSTER_LIST_H +#define ROSTER_LIST_H + +#include + +#include "resource.h" +#include "contact.h" + +void roster_clear(void); +gboolean roster_update_presence(const char * const barejid, Resource *resource, + GDateTime *last_activity); +PContact roster_get_contact(const char * const barejid); +gboolean roster_contact_offline(const char * const barejid, + const char * const resource, const char * const status); +void roster_reset_search_attempts(void); +void roster_init(void); +void roster_free(void); +void roster_change_name(const char * const barejid, const char * const new_name); +void roster_remove(const char * const name, const char * const barejid); +void roster_update(const char * const barejid, const char * const name, + GSList *groups, const char * const subscription, gboolean pending_out); +gboolean roster_add(const char * const barejid, const char * const name, GSList *groups, + const char * const subscription, gboolean pending_out, gboolean from_initial); +char * roster_barejid_from_name(const char * const name); +GSList * roster_get_contacts(void); +gboolean roster_has_pending_subscriptions(void); +char * roster_find_contact(char *search_str); +char * roster_find_resource(char *search_str); +GSList * roster_get_group(const char * const group); +GSList * roster_get_groups(void); +void roster_add_to_group(const char * const group, const char * const barejid); +void roster_remove_from_group(const char * const group, const char * const barejid); +char * roster_find_group(char *search_str); +char * roster_find_jid(char *search_str); + +#endif diff --git a/src/ui/console.c b/src/ui/console.c index 822da420..00e891db 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -31,9 +31,9 @@ #include "command/command.h" #include "common.h" +#include "roster_list.h" #include "config/preferences.h" #include "config/theme.h" -#include "ui/notifier.h" #include "ui/window.h" #include "ui/windows.h" #include "ui/ui.h" diff --git a/src/ui/core.c b/src/ui/core.c index c5cfe284..507cfb02 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -44,10 +44,10 @@ #include "config/preferences.h" #include "config/theme.h" #include "contact.h" +#include "roster_list.h" #include "jid.h" #include "log.h" #include "muc.h" -#include "ui/notifier.h" #include "ui/ui.h" #include "ui/window.h" #include "ui/windows.h" diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index d5b882d7..3306a246 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -40,6 +40,7 @@ #include "config/theme.h" #include "log.h" #include "profanity.h" +#include "roster_list.h" #include "ui/ui.h" #include "ui/windows.h" #include "xmpp/xmpp.h" diff --git a/src/ui/muc_window.h b/src/ui/muc_window.h index e67954a7..0b1f13ab 100644 --- a/src/ui/muc_window.h +++ b/src/ui/muc_window.h @@ -20,8 +20,8 @@ * */ -#ifndef MUC_WINDOW_H -#define MUC_WINDOW_H +#ifndef UI_MUC_WINDOW_H +#define UI_MUC_WINDOW_H #include diff --git a/src/ui/notifier.h b/src/ui/notifier.h deleted file mode 100644 index 9db789e8..00000000 --- a/src/ui/notifier.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * notifier.h - * - * Copyright (C) 2012, 2013 James Booth - * - * 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 . - * - */ - -void notifier_init(void); -void notifier_uninit(void); - -void notify_typing(const char * const handle); -void notify_message(const char * const handle, int win); -void notify_room_message(const char * const handle, const char * const room, - int win); -void notify_remind(void); -void notify_invite(const char * const from, const char * const room, - const char * const reason); -void notify_subscription(const char * const from); diff --git a/src/ui/ui.h b/src/ui/ui.h index 13e96f27..5ec4debd 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -20,8 +20,8 @@ * */ -#ifndef UI_H -#define UI_H +#ifndef UI_UI_H +#define UI_UI_H #include "config.h" @@ -242,4 +242,17 @@ void inp_block(void); void inp_get_password(char *passwd); void inp_replace_input(char *input, const char * const new_input, int *size); +// desktop notifier actions +void notifier_init(void); +void notifier_uninit(void); + +void notify_typing(const char * const handle); +void notify_message(const char * const handle, int win); +void notify_room_message(const char * const handle, const char * const room, + int win); +void notify_remind(void); +void notify_invite(const char * const from, const char * const room, + const char * const reason); +void notify_subscription(const char * const from); + #endif diff --git a/src/ui/window.h b/src/ui/window.h index dea6214d..69e446fa 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -20,8 +20,8 @@ * */ -#ifndef WINDOW_H -#define WINDOW_H +#ifndef UI_WINDOW_H +#define UI_WINDOW_H #include "config.h" diff --git a/src/ui/windows.c b/src/ui/windows.c index 88c811e5..001f1bfc 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -33,6 +33,7 @@ #endif #include "common.h" +#include "roster_list.h" #include "config/theme.h" #include "ui/ui.h" #include "ui/window.h" diff --git a/src/ui/windows.h b/src/ui/windows.h index 3cf593e8..4467e507 100644 --- a/src/ui/windows.h +++ b/src/ui/windows.h @@ -20,8 +20,8 @@ * */ -#ifndef WINDOWS_H -#define WINDOWS_H +#ifndef UI_WINDOWS_H +#define UI_WINDOWS_H void wins_init(void); ProfWin * wins_get_console(void); diff --git a/src/xmpp/bookmark.h b/src/xmpp/bookmark.h index e15b6eab..8b5418b0 100644 --- a/src/xmpp/bookmark.h +++ b/src/xmpp/bookmark.h @@ -1,6 +1,6 @@ -#ifndef BOOKMARK_H -#define BOOKMARK_H +#ifndef XMPP_BOOKMARK_H +#define XMPP_BOOKMARK_H #include @@ -13,10 +13,5 @@ struct bookmark_t { typedef struct bookmark_t Bookmark; void bookmark_request(void); -void bookmark_add(const char *jid, const char *nick, gboolean autojoin); -void bookmark_remove(const char *jid, gboolean autojoin); -const GList *bookmark_get_list(void); -char *bookmark_find(char *search_str); -void bookmark_autocomplete_reset(void); #endif diff --git a/src/xmpp/capabilities.h b/src/xmpp/capabilities.h index d97b84b2..45d97e0f 100644 --- a/src/xmpp/capabilities.h +++ b/src/xmpp/capabilities.h @@ -20,8 +20,8 @@ * */ -#ifndef CAPABILITIES_H -#define CAPABILITIES_H +#ifndef XMPP_CAPABILITIES_H +#define XMPP_CAPABILITIES_H #include diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index b5701252..f11bc8c8 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -20,8 +20,8 @@ * */ -#ifndef CONNECTION_H -#define CONNECTION_H +#ifndef XMPP_CONNECTION_H +#define XMPP_CONNECTION_H #include diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 1ddfcb34..fdabe771 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -38,6 +38,7 @@ #include "xmpp/capabilities.h" #include "xmpp/connection.h" #include "xmpp/stanza.h" +#include "roster_list.h" #include "xmpp/xmpp.h" #define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_IQ, type, ctx) diff --git a/src/xmpp/iq.h b/src/xmpp/iq.h index 86966e26..64844228 100644 --- a/src/xmpp/iq.h +++ b/src/xmpp/iq.h @@ -20,8 +20,8 @@ * */ -#ifndef IQ_H -#define IQ_H +#ifndef XMPP_IQ_H +#define XMPP_IQ_H void iq_add_handlers(void); void iq_roster_request(void); diff --git a/src/xmpp/message.c b/src/xmpp/message.c index 8a977490..a1644587 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -33,6 +33,7 @@ #include "xmpp/connection.h" #include "xmpp/message.h" #include "xmpp/roster.h" +#include "roster_list.h" #include "xmpp/stanza.h" #include "xmpp/xmpp.h" diff --git a/src/xmpp/message.h b/src/xmpp/message.h index 639ae558..3bfa60ef 100644 --- a/src/xmpp/message.h +++ b/src/xmpp/message.h @@ -20,8 +20,8 @@ * */ -#ifndef MESSAGE_H -#define MESSAGE_H +#ifndef XMPP_MESSAGE_H +#define XMPP_MESSAGE_H void message_add_handlers(void); diff --git a/src/xmpp/presence.h b/src/xmpp/presence.h index fa63d78e..fb13a699 100644 --- a/src/xmpp/presence.h +++ b/src/xmpp/presence.h @@ -20,8 +20,8 @@ * */ -#ifndef PRESENCE_H -#define PRESENCE_H +#ifndef XMPP_PRESENCE_H +#define XMPP_PRESENCE_H void presence_sub_requests_init(void); void presence_add_handlers(void); diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 5fbb7be7..51c34ea1 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -32,30 +32,13 @@ #include "tools/autocomplete.h" #include "xmpp/connection.h" #include "xmpp/roster.h" +#include "roster_list.h" #include "xmpp/stanza.h" #include "xmpp/xmpp.h" #define HANDLE(type, func) xmpp_handler_add(conn, func, XMPP_NS_ROSTER, \ STANZA_NAME_IQ, type, ctx) -// nicknames -static Autocomplete name_ac; - -// barejids -static Autocomplete barejid_ac; - -// fulljids -static Autocomplete fulljid_ac; - -// groups -static Autocomplete groups_ac; - -// contacts, indexed on barejid -static GHashTable *contacts; - -// nickname to jid map -static GHashTable *name_to_barejid; - // callback data for group commands typedef struct _group_data { char *name; @@ -77,14 +60,7 @@ _group_remove_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); // helper functions -static void _add_name_and_barejid(const char * const name, - const char * const barejid); -static void _replace_name(const char * const current_name, - const char * const new_name, const char * const barejid); GSList * _get_groups_from_item(xmpp_stanza_t *item); -static gboolean _key_equals(void *key1, void *key2); -static gboolean _datetimes_equal(GDateTime *dt1, GDateTime *dt2); -static gint _compare_contacts(PContact a, PContact b); void roster_add_handlers(void) @@ -105,52 +81,6 @@ roster_request(void) xmpp_stanza_release(iq); } -void -roster_init(void) -{ - name_ac = autocomplete_new(); - barejid_ac = autocomplete_new(); - fulljid_ac = autocomplete_new(); - groups_ac = autocomplete_new(); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, - g_free); -} - -void -roster_clear(void) -{ - autocomplete_clear(name_ac); - autocomplete_clear(barejid_ac); - autocomplete_clear(fulljid_ac); - autocomplete_clear(groups_ac); - g_hash_table_destroy(contacts); - contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, - (GDestroyNotify)p_contact_free); - g_hash_table_destroy(name_to_barejid); - name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, - g_free); -} - -void -roster_free() -{ - autocomplete_free(name_ac); - autocomplete_free(barejid_ac); - autocomplete_free(fulljid_ac); - autocomplete_free(groups_ac); -} - -void -roster_reset_search_attempts(void) -{ - autocomplete_reset(name_ac); - autocomplete_reset(barejid_ac); - autocomplete_reset(fulljid_ac); - autocomplete_reset(groups_ac); -} - void roster_add_new(const char * const barejid, const char * const name) { @@ -162,7 +92,7 @@ roster_add_new(const char * const barejid, const char * const name) } void -roster_remove(const char * const barejid) +roster_send_remove(const char * const barejid) { xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); @@ -171,180 +101,46 @@ roster_remove(const char * const barejid) xmpp_stanza_release(iq); } -gboolean -roster_add(const char * const barejid, const char * const name, GSList *groups, - const char * const subscription, gboolean pending_out, gboolean from_initial) +void +roster_send_name_change(const char * const barejid, const char * const new_name, GSList *groups) { - gboolean added = FALSE; - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact == NULL) { - contact = p_contact_new(barejid, name, groups, subscription, NULL, - pending_out); - - // add groups - while (groups != NULL) { - autocomplete_add(groups_ac, groups->data); - groups = g_slist_next(groups); - } - - g_hash_table_insert(contacts, strdup(barejid), contact); - autocomplete_add(barejid_ac, barejid); - _add_name_and_barejid(name, barejid); - - if (!from_initial) { - prof_handle_roster_add(barejid, name); - } - - added = TRUE; - } - - return added; + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_roster_set(ctx, NULL, barejid, new_name, + groups); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); } void -roster_update(const char * const barejid, const char * const name, - GSList *groups, const char * const subscription, gboolean pending_out) +roster_send_add_to_group(const char * const group, PContact contact) { - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact == NULL) { - roster_add(barejid, name, groups, subscription, pending_out, FALSE); - } else { - p_contact_set_subscription(contact, subscription); - p_contact_set_pending_out(contact, pending_out); - - const char * const new_name = name; - const char * current_name = NULL; - if (p_contact_name(contact) != NULL) { - current_name = strdup(p_contact_name(contact)); - } - - p_contact_set_name(contact, new_name); - p_contact_set_groups(contact, groups); - _replace_name(current_name, new_name, barejid); - - // add groups - while (groups != NULL) { - autocomplete_add(groups_ac, groups->data); - groups = g_slist_next(groups); - } + GSList *groups = p_contact_groups(contact); + GSList *new_groups = NULL; + while (groups != NULL) { + new_groups = g_slist_append(new_groups, strdup(groups->data)); + groups = g_slist_next(groups); } -} -gboolean -roster_update_presence(const char * const barejid, Resource *resource, - GDateTime *last_activity) -{ - assert(barejid != NULL); - assert(resource != NULL); - - PContact contact = g_hash_table_lookup(contacts, barejid); - if (contact == NULL) { - return FALSE; - } - if (!_datetimes_equal(p_contact_last_activity(contact), last_activity)) { - p_contact_set_last_activity(contact, last_activity); - } - p_contact_set_presence(contact, resource); - Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); - autocomplete_add(fulljid_ac, jid->fulljid); - jid_destroy(jid); - - return TRUE; -} - -gboolean -roster_contact_offline(const char * const barejid, - const char * const resource, const char * const status) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact == NULL) { - return FALSE; - } - if (resource == NULL) { - return TRUE; - } else { - gboolean result = p_contact_remove_resource(contact, resource); - if (result == TRUE) { - Jid *jid = jid_create_from_bare_and_resource(barejid, resource); - autocomplete_remove(fulljid_ac, jid->fulljid); - jid_destroy(jid); - } - - return result; - } -} - -void -roster_change_name(const char * const barejid, const char * const new_name) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - const char * current_name = NULL; + new_groups = g_slist_append(new_groups, strdup(group)); + // add an id handler to handle the response + char *unique_id = get_unique_id(); + GroupData *data = malloc(sizeof(GroupData)); + data->group = strdup(group); if (p_contact_name(contact) != NULL) { - current_name = strdup(p_contact_name(contact)); + data->name = strdup(p_contact_name(contact)); + } else { + data->name = strdup(p_contact_barejid(contact)); } - if (contact != NULL) { - p_contact_set_name(contact, new_name); - _replace_name(current_name, new_name, barejid); - - GSList *groups = p_contact_groups(contact); - - xmpp_conn_t * const conn = connection_get_conn(); - xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_stanza_t *iq = stanza_create_roster_set(ctx, NULL, barejid, new_name, - groups); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); - } -} - -void -roster_add_to_group(const char * const group, const char * const barejid) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact != NULL) { - if (p_contact_in_group(contact, group)) { - if (p_contact_name(contact) != NULL) { - prof_handle_already_in_group(p_contact_name(contact), group); - } else { - prof_handle_already_in_group(p_contact_barejid(contact), group); - } - return; - } - - GSList *groups = p_contact_groups(contact); - GSList *new_groups = NULL; - while (groups != NULL) { - new_groups = g_slist_append(new_groups, strdup(groups->data)); - groups = g_slist_next(groups); - } - - new_groups = g_slist_append(new_groups, strdup(group)); - - xmpp_conn_t * const conn = connection_get_conn(); - xmpp_ctx_t * const ctx = connection_get_ctx(); - - // add an id handler to handle the response - char *unique_id = get_unique_id(); - GroupData *data = malloc(sizeof(GroupData)); - data->group = strdup(group); - if (p_contact_name(contact) != NULL) { - data->name = strdup(p_contact_name(contact)); - } else { - data->name = strdup(p_contact_barejid(contact)); - } - - xmpp_id_handler_add(conn, _group_add_handler, unique_id, data); - xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, barejid, - p_contact_name(contact), new_groups); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); - free(unique_id); - } + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_id_handler_add(conn, _group_add_handler, unique_id, data); + xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), + p_contact_name(contact), new_groups); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); + free(unique_id); } static int @@ -362,50 +158,36 @@ _group_add_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, } void -roster_remove_from_group(const char * const group, const char * const barejid) +roster_send_remove_from_group(const char * const group, PContact contact) { - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact != NULL) { - if (!p_contact_in_group(contact, group)) { - if (p_contact_name(contact) != NULL) { - prof_handle_not_in_group(p_contact_name(contact), group); - } else { - prof_handle_not_in_group(p_contact_barejid(contact), group); - } - return; + GSList *groups = p_contact_groups(contact); + GSList *new_groups = NULL; + while (groups != NULL) { + if (strcmp(groups->data, group) != 0) { + new_groups = g_slist_append(new_groups, strdup(groups->data)); } - - GSList *groups = p_contact_groups(contact); - GSList *new_groups = NULL; - while (groups != NULL) { - if (strcmp(groups->data, group) != 0) { - new_groups = g_slist_append(new_groups, strdup(groups->data)); - } - groups = g_slist_next(groups); - } - - xmpp_conn_t * const conn = connection_get_conn(); - xmpp_ctx_t * const ctx = connection_get_ctx(); - - // add an id handler to handle the response - char *unique_id = get_unique_id(); - GroupData *data = malloc(sizeof(GroupData)); - data->group = strdup(group); - if (p_contact_name(contact) != NULL) { - data->name = strdup(p_contact_name(contact)); - } else { - data->name = strdup(p_contact_barejid(contact)); - } - - xmpp_id_handler_add(conn, _group_remove_handler, unique_id, data); - xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, barejid, - p_contact_name(contact), new_groups); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); - free(unique_id); + groups = g_slist_next(groups); } + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + + // add an id handler to handle the response + char *unique_id = get_unique_id(); + GroupData *data = malloc(sizeof(GroupData)); + data->group = strdup(group); + if (p_contact_name(contact) != NULL) { + data->name = strdup(p_contact_name(contact)); + } else { + data->name = strdup(p_contact_barejid(contact)); + } + + xmpp_id_handler_add(conn, _group_remove_handler, unique_id, data); + xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), + p_contact_name(contact), new_groups); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); + free(unique_id); } static int @@ -422,107 +204,6 @@ _group_remove_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 0; } -gboolean -roster_has_pending_subscriptions(void) -{ - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, contacts); - while (g_hash_table_iter_next(&iter, &key, &value)) { - PContact contact = (PContact) value; - if (p_contact_pending_out(contact)) { - return TRUE; - } - } - - return FALSE; -} - -GSList * -roster_get_contacts(void) -{ - GSList *result = NULL; - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, contacts); - while (g_hash_table_iter_next(&iter, &key, &value)) { - result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); - } - - // resturn all contact structs - return result; -} - -GSList * -roster_get_groups(void) -{ - return autocomplete_get_list(groups_ac); -} - -GSList * -roster_get_group(const char * const group) -{ - GSList *result = NULL; - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, contacts); - while (g_hash_table_iter_next(&iter, &key, &value)) { - GSList *groups = p_contact_groups(value); - while (groups != NULL) { - if (strcmp(groups->data, group) == 0) { - result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts); - break; - } - groups = g_slist_next(groups); - } - } - - // resturn all contact structs - return result; -} - -char * -roster_find_contact(char *search_str) -{ - return autocomplete_complete(name_ac, search_str); -} - -char * -roster_find_jid(char *search_str) -{ - return autocomplete_complete(barejid_ac, search_str); -} - -char * -roster_find_resource(char *search_str) -{ - return autocomplete_complete(fulljid_ac, search_str); -} - -char * -roster_find_group(char *search_str) -{ - return autocomplete_complete(groups_ac, search_str); -} - -char * -roster_barejid_from_name(const char * const name) -{ - return g_hash_table_lookup(name_to_barejid, name); -} - -PContact -roster_get_contact(const char * const barejid) -{ - return g_hash_table_lookup(contacts, barejid); -} - static int _roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -556,26 +237,8 @@ _roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, if (name == NULL) { name = barejid; } - autocomplete_remove(barejid_ac, barejid); - autocomplete_remove(name_ac, name); - g_hash_table_remove(name_to_barejid, name); - // remove each fulljid - PContact contact = roster_get_contact(barejid); - if (contact != NULL) { - GList *resources = p_contact_get_available_resources(contact); - while (resources != NULL) { - GString *fulljid = g_string_new(strdup(barejid)); - g_string_append(fulljid, "/"); - g_string_append(fulljid, resources->data); - autocomplete_remove(fulljid_ac, fulljid->str); - g_string_free(fulljid, TRUE); - resources = g_list_next(resources); - } - } - - // remove the contact - g_hash_table_remove(contacts, barejid); + roster_remove(name, barejid); prof_handle_roster_remove(barejid); @@ -642,35 +305,6 @@ _roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } -static void -_add_name_and_barejid(const char * const name, const char * const barejid) -{ - if (name != NULL) { - autocomplete_add(name_ac, name); - g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); - } else { - autocomplete_add(name_ac, barejid); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } -} - -static void -_replace_name(const char * const current_name, const char * const new_name, - const char * const barejid) -{ - // current handle exists already - if (current_name != NULL) { - autocomplete_remove(name_ac, current_name); - g_hash_table_remove(name_to_barejid, current_name); - _add_name_and_barejid(new_name, barejid); - // no current handle - } else if (new_name != NULL) { - autocomplete_remove(name_ac, barejid); - g_hash_table_remove(name_to_barejid, barejid); - _add_name_and_barejid(new_name, barejid); - } -} - GSList * _get_groups_from_item(xmpp_stanza_t *item) { @@ -689,54 +323,3 @@ _get_groups_from_item(xmpp_stanza_t *item) return groups; } - -static -gboolean _key_equals(void *key1, void *key2) -{ - gchar *str1 = (gchar *) key1; - gchar *str2 = (gchar *) key2; - - return (g_strcmp0(str1, str2) == 0); -} - -static -gint _compare_contacts(PContact a, PContact b) -{ - const char * utf8_str_a = NULL; - const char * utf8_str_b = NULL; - - if (p_contact_name(a) != NULL) { - utf8_str_a = p_contact_name(a); - } else { - utf8_str_a = p_contact_barejid(a); - } - if (p_contact_name(b) != NULL) { - utf8_str_b = p_contact_name(b); - } else { - utf8_str_b = p_contact_barejid(b); - } - - gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); - gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); - - gint result = g_strcmp0(key_a, key_b); - - g_free(key_a); - g_free(key_b); - - return result; -} - -static gboolean -_datetimes_equal(GDateTime *dt1, GDateTime *dt2) -{ - if ((dt1 == NULL) && (dt2 == NULL)) { - return TRUE; - } else if ((dt1 == NULL) && (dt2 != NULL)) { - return FALSE; - } else if ((dt1 != NULL) && (dt2 == NULL)) { - return FALSE; - } else { - return g_date_time_equal(dt1, dt2); - } -} diff --git a/src/xmpp/roster.h b/src/xmpp/roster.h index 8aba28f4..1faaba86 100644 --- a/src/xmpp/roster.h +++ b/src/xmpp/roster.h @@ -20,8 +20,8 @@ * */ -#ifndef ROSTER_H -#define ROSTER_H +#ifndef XMPP_ROSTER_H +#define XMPP_ROSTER_H void roster_add_handlers(void); void roster_request(void); diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index 108b0806..6b647258 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -20,8 +20,8 @@ * */ -#ifndef STANZA_H -#define STANZA_H +#ifndef XMPP_STANZA_H +#define XMPP_STANZA_H #include diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index f1b3ba6c..21c25aa4 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -20,8 +20,8 @@ * */ -#ifndef XMPP_H -#define XMPP_H +#ifndef XMPP_XMPP_H +#define XMPP_XMPP_H #include @@ -124,32 +124,16 @@ void iq_disco_items_request(gchar *jid); Capabilities* caps_get(const char * const caps_str); void caps_close(void); -void roster_clear(void); -gboolean roster_update_presence(const char * const barejid, - Resource *resource, GDateTime *last_activity); -PContact roster_get_contact(const char * const barejid); -gboolean roster_contact_offline(const char * const barejid, - const char * const resource, const char * const status); -void roster_reset_search_attempts(void); -void roster_init(void); -void roster_free(void); -gboolean roster_has_pending_subscriptions(void); -GSList * roster_get_contacts(void); -char * roster_find_contact(char *search_str); -char * roster_find_jid(char *search_str); -char * roster_find_resource(char *search_str); -char * roster_find_group(char *search_str); -gboolean roster_add(const char * const barejid, const char * const name, - GSList *groups, const char * const subscription, gboolean pending_out, - gboolean from_initial); -void roster_change_name(const char * const barejid, const char * const new_name); -char * roster_barejid_from_name(const char * const name); +void bookmark_add(const char *jid, const char *nick, gboolean autojoin); +void bookmark_remove(const char *jid, gboolean autojoin); +const GList *bookmark_get_list(void); +char *bookmark_find(char *search_str); +void bookmark_autocomplete_reset(void); + +void roster_send_name_change(const char * const barejid, const char * const new_name, GSList *groups); +void roster_send_add_to_group(const char * const group, PContact contact); +void roster_send_remove_from_group(const char * const group, PContact contact); void roster_add_new(const char * const barejid, const char * const name); -void roster_remove(const char * const barejid); -GSList * roster_get_group(const char * const group); -void roster_add_to_group(const char * const group, const char * const barejid); -void roster_remove_from_group(const char * const group, - const char * const barejid); -GSList * roster_get_groups(void); +void roster_send_remove(const char * const barejid); #endif diff --git a/tests/test_roster.c b/tests/test_roster_list.c similarity index 98% rename from tests/test_roster.c rename to tests/test_roster_list.c index 0b69dc03..1281beb3 100644 --- a/tests/test_roster.c +++ b/tests/test_roster_list.c @@ -6,7 +6,7 @@ #include #include "contact.h" -#include "xmpp/xmpp.h" +#include "roster_list.h" static void beforetest(void) { @@ -266,9 +266,9 @@ static void find_twice_returns_first_when_two_match_and_reset(void) free(result2); } -void register_roster_tests(void) +void register_roster_list_tests(void) { - TEST_MODULE("roster tests"); + TEST_MODULE("roster_list tests"); BEFORETEST(beforetest); AFTERTEST(aftertest); TEST(empty_list_when_none_added); diff --git a/tests/testsuite.c b/tests/testsuite.c index 888e9bcd..da798b67 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -4,7 +4,7 @@ int main(void) { register_history_tests(); - register_roster_tests(); + register_roster_list_tests(); register_common_tests(); register_autocomplete_tests(); register_parser_tests(); diff --git a/tests/testsuite.h b/tests/testsuite.h index 73d48d23..a13b66c0 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -2,7 +2,7 @@ #define TESTSUITE_H void register_history_tests(void); -void register_roster_tests(void); +void register_roster_list_tests(void); void register_common_tests(void); void register_autocomplete_tests(void); void register_parser_tests(void); From 7f7973f9a72b2b6c7fe39cc39929f779d1662a69 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 16:07:57 +0000 Subject: [PATCH 23/33] Moved common tests to cmocka --- Makefile.am | 6 +- configure.ac | 4 +- tests/test_common.c | 153 +++++++++++++++++--------------------------- tests/testsuite.c | 57 +++++++++++++---- tests/testsuite.h | 11 ---- 5 files changed, 110 insertions(+), 121 deletions(-) delete mode 100644 tests/testsuite.h diff --git a/Makefile.am b/Makefile.am index e001a87e..8ca2123c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,7 +80,9 @@ test_sources = \ src/config/accounts.c src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ - tests/test_command.c + tests/test_common.c \ + tests/test_command.c \ + tests/testsuite.c main_source = src/main.c @@ -101,6 +103,6 @@ profanity_SOURCES = $(with_git_sources) $(main_source) TESTS = tests/testsuite check_PROGRAMS = tests/testsuite tests_testsuite_SOURCES = $(tests_with_git_sources) -tests_testsuite_LDADD = -lheadunit -lstdc++ -lcmocka +tests_testsuite_LDADD = -lcmocka man_MANS = docs/profanity.1 diff --git a/configure.ac b/configure.ac index 8468792c..32b60823 100644 --- a/configure.ac +++ b/configure.ac @@ -78,8 +78,8 @@ AC_CHECK_LIB([glib-2.0], [main], [], [AC_MSG_ERROR([glib-2.0 is required for profanity])]) AC_CHECK_LIB([curl], [main], [], [AC_MSG_ERROR([libcurl is required for profanity])]) -AC_CHECK_LIB([headunit], [main], [], - [AC_MSG_NOTICE([headunit not found, will not be able to run tests])]) +AC_CHECK_LIB([cmocka], [main], [], + [AC_MSG_NOTICE([cmocka not found, will not be able to run tests])]) # Checks for header files. AC_CHECK_HEADERS([stdlib.h string.h]) diff --git a/tests/test_common.c b/tests/test_common.c index 37dd37d2..124a1181 100644 --- a/tests/test_common.c +++ b/tests/test_common.c @@ -1,9 +1,11 @@ -#include -#include -#include #include "common.h" +#include +#include +#include +#include +#include -void replace_one_substr(void) +void replace_one_substr(void **state) { char *string = "it is a string"; char *sub = "is"; @@ -11,10 +13,10 @@ void replace_one_substr(void) char *result = str_replace(string, sub, new); - assert_string_equals("it was a string", result); + assert_string_equal("it was a string", result); } -void replace_one_substr_beginning(void) +void replace_one_substr_beginning(void **state) { char *string = "it is a string"; char *sub = "it"; @@ -22,10 +24,10 @@ void replace_one_substr_beginning(void) char *result = str_replace(string, sub, new); - assert_string_equals("that is a string", result); + assert_string_equal("that is a string", result); } -void replace_one_substr_end(void) +void replace_one_substr_end(void **state) { char *string = "it is a string"; char *sub = "string"; @@ -33,10 +35,10 @@ void replace_one_substr_end(void) char *result = str_replace(string, sub, new); - assert_string_equals("it is a thing", result); + assert_string_equal("it is a thing", result); } -void replace_two_substr(void) +void replace_two_substr(void **state) { char *string = "it is a is string"; char *sub = "is"; @@ -44,10 +46,10 @@ void replace_two_substr(void) char *result = str_replace(string, sub, new); - assert_string_equals("it was a was string", result); + assert_string_equal("it was a was string", result); } -void replace_char(void) +void replace_char(void **state) { char *string = "some & a thing & something else"; char *sub = "&"; @@ -55,10 +57,10 @@ void replace_char(void) char *result = str_replace(string, sub, new); - assert_string_equals("some & a thing & something else", result); + assert_string_equal("some & a thing & something else", result); } -void replace_when_none(void) +void replace_when_none(void **state) { char *string = "its another string"; char *sub = "haha"; @@ -66,10 +68,10 @@ void replace_when_none(void) char *result = str_replace(string, sub, new); - assert_string_equals("its another string", result); + assert_string_equal("its another string", result); } -void replace_when_match(void) +void replace_when_match(void **state) { char *string = "hello"; char *sub = "hello"; @@ -77,10 +79,10 @@ void replace_when_match(void) char *result = str_replace(string, sub, new); - assert_string_equals("goodbye", result); + assert_string_equal("goodbye", result); } -void replace_when_string_empty(void) +void replace_when_string_empty(void **state) { char *string = ""; char *sub = "hello"; @@ -88,10 +90,10 @@ void replace_when_string_empty(void) char *result = str_replace(string, sub, new); - assert_string_equals("", result); + assert_string_equal("", result); } -void replace_when_string_null(void) +void replace_when_string_null(void **state) { char *string = NULL; char *sub = "hello"; @@ -99,10 +101,10 @@ void replace_when_string_null(void) char *result = str_replace(string, sub, new); - assert_is_null(result); + assert_null(result); } -void replace_when_sub_empty(void) +void replace_when_sub_empty(void **state) { char *string = "hello"; char *sub = ""; @@ -110,10 +112,10 @@ void replace_when_sub_empty(void) char *result = str_replace(string, sub, new); - assert_string_equals("hello", result); + assert_string_equal("hello", result); } -void replace_when_sub_null(void) +void replace_when_sub_null(void **state) { char *string = "hello"; char *sub = NULL; @@ -121,10 +123,10 @@ void replace_when_sub_null(void) char *result = str_replace(string, sub, new); - assert_string_equals("hello", result); + assert_string_equal("hello", result); } -void replace_when_new_empty(void) +void replace_when_new_empty(void **state) { char *string = "hello"; char *sub = "hello"; @@ -132,10 +134,10 @@ void replace_when_new_empty(void) char *result = str_replace(string, sub, new); - assert_string_equals("", result); + assert_string_equal("", result); } -void replace_when_new_null(void) +void replace_when_new_null(void **state) { char *string = "hello"; char *sub = "hello"; @@ -143,10 +145,10 @@ void replace_when_new_null(void) char *result = str_replace(string, sub, new); - assert_string_equals("hello", result); + assert_string_equal("hello", result); } -void compare_win_nums_less(void) +void compare_win_nums_less(void **state) { gconstpointer a = GINT_TO_POINTER(2); gconstpointer b = GINT_TO_POINTER(3); @@ -156,7 +158,7 @@ void compare_win_nums_less(void) assert_true(result < 0); } -void compare_win_nums_equal(void) +void compare_win_nums_equal(void **state) { gconstpointer a = GINT_TO_POINTER(5); gconstpointer b = GINT_TO_POINTER(5); @@ -166,7 +168,7 @@ void compare_win_nums_equal(void) assert_true(result == 0); } -void compare_win_nums_greater(void) +void compare_win_nums_greater(void **state) { gconstpointer a = GINT_TO_POINTER(7); gconstpointer b = GINT_TO_POINTER(6); @@ -176,7 +178,7 @@ void compare_win_nums_greater(void) assert_true(result > 0); } -void compare_0s_equal(void) +void compare_0s_equal(void **state) { gconstpointer a = GINT_TO_POINTER(0); gconstpointer b = GINT_TO_POINTER(0); @@ -186,7 +188,7 @@ void compare_0s_equal(void) assert_true(result == 0); } -void compare_0_greater_than_1(void) +void compare_0_greater_than_1(void **state) { gconstpointer a = GINT_TO_POINTER(0); gconstpointer b = GINT_TO_POINTER(1); @@ -196,7 +198,7 @@ void compare_0_greater_than_1(void) assert_true(result > 0); } -void compare_1_less_than_0(void) +void compare_1_less_than_0(void **state) { gconstpointer a = GINT_TO_POINTER(1); gconstpointer b = GINT_TO_POINTER(0); @@ -206,7 +208,7 @@ void compare_1_less_than_0(void) assert_true(result < 0); } -void compare_0_less_than_11(void) +void compare_0_less_than_11(void **state) { gconstpointer a = GINT_TO_POINTER(0); gconstpointer b = GINT_TO_POINTER(11); @@ -216,7 +218,7 @@ void compare_0_less_than_11(void) assert_true(result < 0); } -void compare_11_greater_than_0(void) +void compare_11_greater_than_0(void **state) { gconstpointer a = GINT_TO_POINTER(11); gconstpointer b = GINT_TO_POINTER(0); @@ -226,7 +228,7 @@ void compare_11_greater_than_0(void) assert_true(result > 0); } -void compare_0_greater_than_9(void) +void compare_0_greater_than_9(void **state) { gconstpointer a = GINT_TO_POINTER(0); gconstpointer b = GINT_TO_POINTER(9); @@ -236,7 +238,7 @@ void compare_0_greater_than_9(void) assert_true(result > 0); } -void compare_9_less_than_0(void) +void compare_9_less_than_0(void **state) { gconstpointer a = GINT_TO_POINTER(9); gconstpointer b = GINT_TO_POINTER(0); @@ -246,17 +248,17 @@ void compare_9_less_than_0(void) assert_true(result < 0); } -void next_available_when_only_console(void) +void next_available_when_only_console(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); int result = get_next_available_win_num(used); - assert_int_equals(2, result); + assert_int_equal(2, result); } -void next_available_3_at_end(void) +void next_available_3_at_end(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -264,10 +266,10 @@ void next_available_3_at_end(void) int result = get_next_available_win_num(used); - assert_int_equals(3, result); + assert_int_equal(3, result); } -void next_available_9_at_end(void) +void next_available_9_at_end(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -281,10 +283,10 @@ void next_available_9_at_end(void) int result = get_next_available_win_num(used); - assert_int_equals(9, result); + assert_int_equal(9, result); } -void next_available_0_at_end(void) +void next_available_0_at_end(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -299,10 +301,10 @@ void next_available_0_at_end(void) int result = get_next_available_win_num(used); - assert_int_equals(0, result); + assert_int_equal(0, result); } -void next_available_2_in_first_gap(void) +void next_available_2_in_first_gap(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -314,10 +316,10 @@ void next_available_2_in_first_gap(void) int result = get_next_available_win_num(used); - assert_int_equals(2, result); + assert_int_equal(2, result); } -void next_available_9_in_first_gap(void) +void next_available_9_in_first_gap(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -336,10 +338,10 @@ void next_available_9_in_first_gap(void) int result = get_next_available_win_num(used); - assert_int_equals(9, result); + assert_int_equal(9, result); } -void next_available_0_in_first_gap(void) +void next_available_0_in_first_gap(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -358,10 +360,10 @@ void next_available_0_in_first_gap(void) int result = get_next_available_win_num(used); - assert_int_equals(0, result); + assert_int_equal(0, result); } -void next_available_11_in_first_gap(void) +void next_available_11_in_first_gap(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -380,10 +382,10 @@ void next_available_11_in_first_gap(void) int result = get_next_available_win_num(used); - assert_int_equals(11, result); + assert_int_equal(11, result); } -void next_available_24_first_big_gap(void) +void next_available_24_first_big_gap(void **state) { GList *used = NULL; used = g_list_append(used, GINT_TO_POINTER(1)); @@ -420,42 +422,5 @@ void next_available_24_first_big_gap(void) int result = get_next_available_win_num(used); - assert_int_equals(24, result); -} - -void register_common_tests(void) -{ - TEST_MODULE("common tests"); - TEST(replace_one_substr); - TEST(replace_one_substr_beginning); - TEST(replace_one_substr_end); - TEST(replace_two_substr); - TEST(replace_char); - TEST(replace_when_none); - TEST(replace_when_match); - TEST(replace_when_string_empty); - TEST(replace_when_string_null); - TEST(replace_when_sub_empty); - TEST(replace_when_sub_null); - TEST(replace_when_new_empty); - TEST(replace_when_new_null); - TEST(compare_win_nums_less); - TEST(compare_win_nums_equal); - TEST(compare_win_nums_greater); - TEST(compare_0s_equal); - TEST(compare_0_greater_than_1); - TEST(compare_1_less_than_0); - TEST(compare_0_less_than_11); - TEST(compare_11_greater_than_0); - TEST(compare_0_greater_than_9); - TEST(compare_9_less_than_0); - TEST(next_available_when_only_console); - TEST(next_available_3_at_end); - TEST(next_available_9_at_end); - TEST(next_available_0_at_end); - TEST(next_available_2_in_first_gap); - TEST(next_available_9_in_first_gap); - TEST(next_available_0_in_first_gap); - TEST(next_available_11_in_first_gap); - TEST(next_available_24_first_big_gap); + assert_int_equal(24, result); } diff --git a/tests/testsuite.c b/tests/testsuite.c index da798b67..b12bacd5 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -1,14 +1,47 @@ -#include -#include "testsuite.h" +#include +#include +#include +#include -int main(void) -{ - register_history_tests(); - register_roster_list_tests(); - register_common_tests(); - register_autocomplete_tests(); - register_parser_tests(); - register_jid_tests(); - run_suite(); - return 0; +#include "test_common.h" +#include "test_command.h" + +int main(int argc, char* argv[]) { + const UnitTest tests[] = { + unit_test(cmd_rooms_shows_message_when_not_connected), + + unit_test(replace_one_substr), + unit_test(replace_one_substr_beginning), + unit_test(replace_one_substr_end), + unit_test(replace_two_substr), + unit_test(replace_char), + unit_test(replace_when_none), + unit_test(replace_when_match), + unit_test(replace_when_string_empty), + unit_test(replace_when_string_null), + unit_test(replace_when_sub_empty), + unit_test(replace_when_sub_null), + unit_test(replace_when_new_empty), + unit_test(replace_when_new_null), + unit_test(compare_win_nums_less), + unit_test(compare_win_nums_equal), + unit_test(compare_win_nums_greater), + unit_test(compare_0s_equal), + unit_test(compare_0_greater_than_1), + unit_test(compare_1_less_than_0), + unit_test(compare_0_less_than_11), + unit_test(compare_11_greater_than_0), + unit_test(compare_0_greater_than_9), + unit_test(compare_9_less_than_0), + unit_test(next_available_when_only_console), + unit_test(next_available_3_at_end), + unit_test(next_available_9_at_end), + unit_test(next_available_0_at_end), + unit_test(next_available_2_in_first_gap), + unit_test(next_available_9_in_first_gap), + unit_test(next_available_0_in_first_gap), + unit_test(next_available_11_in_first_gap), + unit_test(next_available_24_first_big_gap) + }; + return run_tests(tests); } diff --git a/tests/testsuite.h b/tests/testsuite.h deleted file mode 100644 index a13b66c0..00000000 --- a/tests/testsuite.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef TESTSUITE_H -#define TESTSUITE_H - -void register_history_tests(void); -void register_roster_list_tests(void); -void register_common_tests(void); -void register_autocomplete_tests(void); -void register_parser_tests(void); -void register_jid_tests(void); - -#endif From 3a403046ff241381e0f9750dfe39f1e05c73fd63 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 16:16:46 +0000 Subject: [PATCH 24/33] Moved autocomplete tests to cmocka --- Makefile.am | 1 + src/roster_list.c | 22 +++++++-------- tests/test_autocomplete.c | 58 +++++++++++++++------------------------ tests/testsuite.c | 16 +++++++++-- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/Makefile.am b/Makefile.am index 8ca2123c..e52fe8bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,6 +80,7 @@ test_sources = \ src/config/accounts.c src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ + tests/test_autocomplete.c \ tests/test_common.c \ tests/test_command.c \ tests/testsuite.c diff --git a/src/roster_list.c b/src/roster_list.c index 37031ca4..fa00437b 100644 --- a/src/roster_list.c +++ b/src/roster_list.c @@ -156,19 +156,19 @@ roster_free(void) autocomplete_free(groups_ac); } -void +void roster_change_name(const char * const barejid, const char * const new_name) { - PContact contact = g_hash_table_lookup(contacts, barejid); - const char * current_name = NULL; - if (p_contact_name(contact) != NULL) { - current_name = strdup(p_contact_name(contact)); - } - - if (contact != NULL) { - p_contact_set_name(contact, new_name); - _replace_name(current_name, new_name, barejid); - + PContact contact = g_hash_table_lookup(contacts, barejid); + const char * current_name = NULL; + if (p_contact_name(contact) != NULL) { + current_name = strdup(p_contact_name(contact)); + } + + if (contact != NULL) { + p_contact_set_name(contact, new_name); + _replace_name(current_name, new_name, barejid); + GSList *groups = p_contact_groups(contact); roster_send_name_change(barejid, new_name, groups); } diff --git a/tests/test_autocomplete.c b/tests/test_autocomplete.c index 58a0e596..3eea639b 100644 --- a/tests/test_autocomplete.c +++ b/tests/test_autocomplete.c @@ -1,66 +1,67 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include "contact.h" #include "tools/autocomplete.h" -static void clear_empty(void) +void clear_empty(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_clear(ac); } -static void reset_after_create(void) +void reset_after_create(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_reset(ac); autocomplete_clear(ac); } -static void find_after_create(void) +void find_after_create(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_complete(ac, "hello"); autocomplete_clear(ac); } -static void get_after_create_returns_null(void) +void get_after_create_returns_null(void **state) { Autocomplete ac = autocomplete_new(); GSList *result = autocomplete_get_list(ac); - assert_is_null(result); + assert_null(result); autocomplete_clear(ac); } -static void add_one_and_complete(void) +void add_one_and_complete(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); char *result = autocomplete_complete(ac, "Hel"); - assert_string_equals("Hello", result); + assert_string_equal("Hello", result); autocomplete_clear(ac); } -static void add_two_and_complete_returns_first(void) +void add_two_and_complete_returns_first(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); autocomplete_add(ac, "Help"); char *result = autocomplete_complete(ac, "Hel"); - assert_string_equals("Hello", result); + assert_string_equal("Hello", result); autocomplete_clear(ac); } -static void add_two_and_complete_returns_second(void) +void add_two_and_complete_returns_second(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); @@ -68,36 +69,36 @@ static void add_two_and_complete_returns_second(void) char *result1 = autocomplete_complete(ac, "Hel"); char *result2 = autocomplete_complete(ac, result1); - assert_string_equals("Help", result2); + assert_string_equal("Help", result2); autocomplete_clear(ac); } -static void add_two_adds_two(void) +void add_two_adds_two(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); autocomplete_add(ac, "Help"); GSList *result = autocomplete_get_list(ac); - assert_int_equals(2, g_slist_length(result)); + assert_int_equal(2, g_slist_length(result)); autocomplete_clear(ac); } -static void add_two_same_adds_one(void) +void add_two_same_adds_one(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); autocomplete_add(ac, "Hello"); GSList *result = autocomplete_get_list(ac); - assert_int_equals(1, g_slist_length(result)); + assert_int_equal(1, g_slist_length(result)); autocomplete_clear(ac); } -static void add_two_same_updates(void) +void add_two_same_updates(void **state) { Autocomplete ac = autocomplete_new(); autocomplete_add(ac, "Hello"); @@ -108,22 +109,7 @@ static void add_two_same_updates(void) char *str = first->data; - assert_string_equals("Hello", str); + assert_string_equal("Hello", str); autocomplete_clear(ac); } - -void register_autocomplete_tests(void) -{ - TEST_MODULE("autocomplete tests"); - TEST(clear_empty); - TEST(reset_after_create); - TEST(find_after_create); - TEST(get_after_create_returns_null); - TEST(add_one_and_complete); - TEST(add_two_and_complete_returns_first); - TEST(add_two_and_complete_returns_second); - TEST(add_two_adds_two); - TEST(add_two_same_adds_one); - TEST(add_two_same_updates); -} diff --git a/tests/testsuite.c b/tests/testsuite.c index b12bacd5..6215b761 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -3,6 +3,7 @@ #include #include +#include "test_autocomplete.h" #include "test_common.h" #include "test_command.h" @@ -41,7 +42,18 @@ int main(int argc, char* argv[]) { unit_test(next_available_9_in_first_gap), unit_test(next_available_0_in_first_gap), unit_test(next_available_11_in_first_gap), - unit_test(next_available_24_first_big_gap) - }; + unit_test(next_available_24_first_big_gap), + + unit_test(clear_empty), + unit_test(reset_after_create), + unit_test(find_after_create), + unit_test(get_after_create_returns_null), + unit_test(add_one_and_complete), + unit_test(add_two_and_complete_returns_first), + unit_test(add_two_and_complete_returns_second), + unit_test(add_two_adds_two), + unit_test(add_two_same_adds_one), + unit_test(add_two_same_updates) + }; return run_tests(tests); } From 71577c1fdd09b1d58710c323264f3bffbf1476ac Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 16:17:53 +0000 Subject: [PATCH 25/33] Added missing files --- tests/test_autocomplete.h | 10 ++ tests/test_command.c | 21 +++ tests/test_command.h | 1 + tests/test_common.h | 32 ++++ tests/ui/mock_ui.c | 302 ++++++++++++++++++++++++++++++++++++++ tests/xmpp/mock_xmpp.c | 150 +++++++++++++++++++ 6 files changed, 516 insertions(+) create mode 100644 tests/test_autocomplete.h create mode 100644 tests/test_command.c create mode 100644 tests/test_command.h create mode 100644 tests/test_common.h create mode 100644 tests/ui/mock_ui.c create mode 100644 tests/xmpp/mock_xmpp.c diff --git a/tests/test_autocomplete.h b/tests/test_autocomplete.h new file mode 100644 index 00000000..4ad327c0 --- /dev/null +++ b/tests/test_autocomplete.h @@ -0,0 +1,10 @@ +void clear_empty(void **state); +void reset_after_create(void **state); +void find_after_create(void **state); +void get_after_create_returns_null(void **state); +void add_one_and_complete(void **state); +void add_two_and_complete_returns_first(void **state); +void add_two_and_complete_returns_second(void **state); +void add_two_adds_two(void **state); +void add_two_same_adds_one(void **state); +void add_two_same_updates(void **state); diff --git a/tests/test_command.c b/tests/test_command.c new file mode 100644 index 00000000..13738032 --- /dev/null +++ b/tests/test_command.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +#include "xmpp/xmpp.h" +#include "ui/ui.h" +#include "command/command.h" + +void cmd_rooms_shows_message_when_not_connected(void **state) +{ + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + expect_string(cons_show, msg, "You are not currently connected."); + CommandHelp *help = malloc(sizeof(CommandHelp)); + + assert_true(_cmd_rooms(NULL, *help)); + + free(help); +} + diff --git a/tests/test_command.h b/tests/test_command.h new file mode 100644 index 00000000..589b589c --- /dev/null +++ b/tests/test_command.h @@ -0,0 +1 @@ +void cmd_rooms_shows_message_when_not_connected(void **state); diff --git a/tests/test_common.h b/tests/test_common.h new file mode 100644 index 00000000..903570e3 --- /dev/null +++ b/tests/test_common.h @@ -0,0 +1,32 @@ +void replace_one_substr(void **state); +void replace_one_substr_beginning(void **state); +void replace_one_substr_end(void **state); +void replace_two_substr(void **state); +void replace_char(void **state); +void replace_when_none(void **state); +void replace_when_match(void **state); +void replace_when_string_empty(void **state); +void replace_when_string_null(void **state); +void replace_when_sub_empty(void **state); +void replace_when_sub_null(void **state); +void replace_when_new_empty(void **state); +void replace_when_new_null(void **state); +void compare_win_nums_less(void **state); +void compare_win_nums_equal(void **state); +void compare_win_nums_greater(void **state); +void compare_0s_equal(void **state); +void compare_0_greater_than_1(void **state); +void compare_1_less_than_0(void **state); +void compare_0_less_than_11(void **state); +void compare_11_greater_than_0(void **state); +void compare_0_greater_than_9(void **state); +void compare_9_less_than_0(void **state); +void next_available_when_only_console(void **state); +void next_available_3_at_end(void **state); +void next_available_9_at_end(void **state); +void next_available_0_at_end(void **state); +void next_available_2_in_first_gap(void **state); +void next_available_9_in_first_gap(void **state); +void next_available_0_in_first_gap(void **state); +void next_available_11_in_first_gap(void **state); +void next_available_24_first_big_gap(void **state); diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c new file mode 100644 index 00000000..bf9f2499 --- /dev/null +++ b/tests/ui/mock_ui.c @@ -0,0 +1,302 @@ +/* + * ui.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include +#include +#include + +#include "ui/ui.h" + +// ui startup and control +void ui_init(void) {} +void ui_load_colours(void) {} +void ui_refresh(void) {} +void ui_close(void) {} +void ui_resize(const int ch, const char * const input, + const int size) {} + +GSList* ui_get_recipients(void) +{ + return (GSList *)mock(); +} + +void ui_handle_special_keys(const wint_t * const ch, const char * const inp, + const int size) {} +void ui_switch_win(const int i) {} +void ui_next_win(void) {} +void ui_previous_win(void) {} + +unsigned long ui_get_idle_time(void) +{ + return (unsigned long)mock(); +} + +void ui_reset_idle_time(void) {} +void ui_new_chat_win(const char * const to) {} +void ui_print_error_from_recipient(const char * const from, const char *err_msg) {} +void ui_print_system_msg_from_recipient(const char * const from, const char *message) {} +void ui_handle_error_message(const char * const from, const char * const err_msg) {} + +gint ui_unread(void) +{ + return (gint)mock(); +} + +void ui_close_connected_win(int index) {} + +int ui_close_all_wins(void) +{ + return (int)mock(); +} + +int ui_close_read_wins(void) +{ + return (int)mock(); +} + +// current window actions +void ui_close_current(void) {} +void ui_clear_current(void) {} + +win_type_t ui_current_win_type(void) +{ + return (win_type_t)mock(); +} + +int ui_current_win_index(void) +{ + return (int)mock(); +} + +char* ui_current_recipient(void) +{ + return (char *)mock(); +} + +void ui_current_print_line(const char * const msg, ...) {} +void ui_current_error_line(const char * const msg) {} +void ui_current_page_off(void) {} + +win_type_t ui_win_type(int index) +{ + return (win_type_t)mock(); +} + +char * ui_recipient(int index) +{ + return (char *)mock(); +} + +void ui_close_win(int index) {} + +gboolean ui_win_exists(int index) +{ + return (gboolean)mock(); +} + +int ui_win_unread(int index) +{ + return (int)mock(); +} + +// ui events +void ui_contact_typing(const char * const from) {} +void ui_incoming_msg(const char * const from, const char * const message, + GTimeVal *tv_stamp, gboolean priv) {} +void ui_contact_online(const char * const barejid, const char * const resource, + const char * const show, const char * const status, GDateTime *last_activity) {} +void ui_contact_offline(const char * const from, const char * const show, + const char * const status) {} +void ui_disconnected(void) {} +void ui_recipient_gone(const char * const barejid) {} +void ui_outgoing_msg(const char * const from, const char * const to, + const char * const message) {} +void ui_room_join(Jid *jid) {} +void ui_room_roster(const char * const room, GList *roster, const char * const presence) {} +void ui_room_history(const char * const room_jid, const char * const nick, + GTimeVal tv_stamp, const char * const message) {} +void ui_room_message(const char * const room_jid, const char * const nick, + const char * const message) {} +void ui_room_subject(const char * const room_jid, + const char * const subject) {} +void ui_room_broadcast(const char * const room_jid, + const char * const message) {} +void ui_room_member_offline(const char * const room, const char * const nick) {} +void ui_room_member_online(const char * const room, + const char * const nick, const char * const show, const char * const status) {} +void ui_room_member_nick_change(const char * const room, + const char * const old_nick, const char * const nick) {} +void ui_room_nick_change(const char * const room, const char * const nick) {} +void ui_room_member_presence(const char * const room, + const char * const nick, const char * const show, const char * const status) {} +void ui_roster_add(const char * const barejid, const char * const name) {} +void ui_roster_remove(const char * const barejid) {} +void ui_contact_already_in_group(const char * const contact, const char * const group) {} +void ui_contact_not_in_group(const char * const contact, const char * const group) {} +void ui_group_added(const char * const contact, const char * const group) {} +void ui_group_removed(const char * const contact, const char * const group) {} + +// contact status functions +void ui_status_room(const char * const contact) {} +void ui_status(void) {} +void ui_status_private(void) {} + +void ui_create_duck_win(void) {} +void ui_open_duck_win(void) {} +void ui_duck(const char * const query) {} +void ui_duck_result(const char * const result) {} + +gboolean ui_duck_exists(void) +{ + return (gboolean)mock(); +} + +void ui_tidy_wins(void) {} +void ui_prune_wins(void) {} + +// create windows +void create_title_bar(void) {} +void create_status_bar(void) {} +void create_input_window(void) {} + +// title bar actions +void title_bar_refresh(void) {} +void title_bar_resize(void) {} +void title_bar_show(const char * const title) {} +void title_bar_title(void) {} +void title_bar_set_status(contact_presence_t status) {} +void title_bar_set_recipient(const char * const from) {} +void title_bar_set_typing(gboolean is_typing) {} +void title_bar_draw(void) {} + +// console window actions +void cons_show(const char * const msg, ...) +{ + check_expected(msg); +} + +void cons_about(void) {} +void cons_help(void) {} +void cons_basic_help(void) {} +void cons_settings_help(void) {} +void cons_presence_help(void) {} +void cons_navigation_help(void) {} +void cons_prefs(void) {} +void cons_show_ui_prefs(void) {} +void cons_show_desktop_prefs(void) {} +void cons_show_chat_prefs(void) {} +void cons_show_log_prefs(void) {} +void cons_show_presence_prefs(void) {} +void cons_show_connection_prefs(void) {} +void cons_show_account(ProfAccount *account) {} +void cons_debug(const char * const msg, ...) {} +void cons_show_time(void) {} +void cons_show_word(const char * const word) {} +void cons_show_error(const char * const cmd, ...) {} +void cons_highlight_show(const char * const cmd) {} +void cons_show_contacts(GSList * list) {} +void cons_show_roster(GSList * list) {} +void cons_show_roster_group(const char * const group, GSList * list) {} +void cons_show_wins(void) {} +void cons_show_status(const char * const barejid) {} +void cons_show_info(PContact pcontact) {} +void cons_show_caps(const char * const contact, Resource *resource) {} +void cons_show_themes(GSList *themes) {} +void cons_show_login_success(ProfAccount *account) {} +void cons_show_software_version(const char * const jid, + const char * const presence, const char * const name, + const char * const version, const char * const os) {} +void cons_show_account_list(gchar **accounts) {} +void cons_show_room_list(GSList *room, const char * const conference_node) {} +void cons_show_bookmarks(const GList *list) {} +void cons_show_disco_items(GSList *items, const char * const jid) {} +void cons_show_disco_info(const char *from, GSList *identities, GSList *features) {} +void cons_show_room_invite(const char * const invitor, const char * const room, + const char * const reason) {} +void cons_check_version(gboolean not_available_msg) {} +void cons_show_typing(const char * const barejid) {} +void cons_show_incoming_message(const char * const short_from, const int win_index) {} +void cons_show_room_invites(GSList *invites) {} +void cons_show_received_subs(void) {} +void cons_show_sent_subs(void) {} +void cons_alert(void) {} +void cons_theme_setting(void) {} +void cons_beep_setting(void) {} +void cons_flash_setting(void) {} +void cons_splash_setting(void) {} +void cons_vercheck_setting(void) {} +void cons_mouse_setting(void) {} +void cons_statuses_setting(void) {} +void cons_titlebar_setting(void) {} +void cons_notify_setting(void) {} +void cons_states_setting(void) {} +void cons_outtype_setting(void) {} +void cons_intype_setting(void) {} +void cons_gone_setting(void) {} +void cons_history_setting(void) {} +void cons_log_setting(void) {} +void cons_chlog_setting(void) {} +void cons_grlog_setting(void) {} +void cons_autoaway_setting(void) {} +void cons_reconnect_setting(void) {} +void cons_autoping_setting(void) {} +void cons_priority_setting(void) {} +void cons_autoconnect_setting(void) {} + +// status bar actions +void status_bar_refresh(void) {} +void status_bar_resize(void) {} +void status_bar_clear(void) {} +void status_bar_clear_message(void) {} +void status_bar_get_password(void) {} +void status_bar_print_message(const char * const msg) {} +void status_bar_inactive(const int win) {} +void status_bar_active(const int win) {} +void status_bar_new(const int win) {} +void status_bar_update_time(void) {} +void status_bar_set_all_inactive(void) {} +void status_bar_current(int i) {} + +// input window actions +wint_t inp_get_char(char *input, int *size) +{ + return (wint_t)mock(); +} +void inp_win_reset(void) {} +void inp_win_resize(const char * input, const int size) {} +void inp_put_back(void) {} +void inp_non_block(void) {} +void inp_block(void) {} +void inp_get_password(char *passwd) {} +void inp_replace_input(char *input, const char * const new_input, int *size) {} + +void notifier_init(void) {} +void notifier_uninit(void) {} + +void notify_typing(const char * const handle) {} +void notify_message(const char * const handle, int win) {} +void notify_room_message(const char * const handle, const char * const room, + int win) {} +void notify_remind(void) {} +void notify_invite(const char * const from, const char * const room, + const char * const reason) {} +void notify_subscription(const char * const from) {} diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c new file mode 100644 index 00000000..0c6bf917 --- /dev/null +++ b/tests/xmpp/mock_xmpp.c @@ -0,0 +1,150 @@ +/* + * xmpp.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include +#include +#include + +#include "xmpp/xmpp.h" + +// connection functions +void jabber_init(const int disable_tls) {} + +jabber_conn_status_t jabber_connect_with_details(const char * const jid, + const char * const passwd, const char * const altdomain) +{ + return (jabber_conn_status_t)mock(); +} + +jabber_conn_status_t jabber_connect_with_account(const ProfAccount * const account) +{ + return (jabber_conn_status_t)mock(); +} + +void jabber_disconnect(void) {} +void jabber_shutdown(void) {} +void jabber_process_events(void) {} +const char * jabber_get_fulljid(void) +{ + return (const char *)mock(); +} +const char * jabber_get_domain(void) +{ + return (const char *)mock(); +} + +jabber_conn_status_t jabber_get_connection_status(void) +{ + return (jabber_conn_status_t)mock(); +} + +char * jabber_get_presence_message(void) +{ + return (char *)mock(); +} +void jabber_set_autoping(int seconds) {} + +char* jabber_get_account_name(void) +{ + return (char *)mock(); +} + +GList * jabber_get_available_resources(void) +{ + return (GList *)mock(); +} + +// message functions +void message_send(const char * const msg, const char * const recipient) {} +void message_send_groupchat(const char * const msg, const char * const recipient) {} +void message_send_inactive(const char * const recipient) {} +void message_send_composing(const char * const recipient) {} +void message_send_paused(const char * const recipient) {} +void message_send_gone(const char * const recipient) {} +void message_send_invite(const char * const room, const char * const contact, + const char * const reason) {} +void message_send_duck(const char * const query) {} + +// presence functions +void presence_subscription(const char * const jid, const jabber_subscr_t action) {} + +GSList* presence_get_subscription_requests(void) +{ + return (GSList *)mock(); +} + +gint presence_sub_request_count(void) +{ + return (gint)mock(); +} + +void presence_reset_sub_request_search(void) {} + +char * presence_sub_request_find(char * search_str) +{ + return (char *)mock(); +} + +void presence_join_room(Jid *jid) {} +void presence_change_room_nick(const char * const room, const char * const nick) {} +void presence_leave_chat_room(const char * const room_jid) {} +void presence_update(resource_presence_t status, const char * const msg, + int idle) {} +gboolean presence_sub_request_exists(const char * const bare_jid) +{ + return (gboolean)mock(); +} + +// iq functions +void iq_send_software_version(const char * const fulljid) {} +void iq_room_list_request(gchar *conferencejid) {} +void iq_disco_info_request(gchar *jid) {} +void iq_disco_items_request(gchar *jid) {} + +// caps functions +Capabilities* caps_get(const char * const caps_str) +{ + return (Capabilities *)mock(); +} + +void caps_close(void) {} + +void bookmark_add(const char *jid, const char *nick, gboolean autojoin) {} +void bookmark_remove(const char *jid, gboolean autojoin) {} + +const GList *bookmark_get_list(void) +{ + return (const GList *)mock(); +} + +char *bookmark_find(char *search_str) +{ + return (char *)mock(); +} + +void bookmark_autocomplete_reset(void) {} + +void roster_send_name_change(const char * const barejid, const char * const new_name, GSList *groups) {} +void roster_send_add_to_group(const char * const group, PContact contact) {} +void roster_send_remove_from_group(const char * const group, PContact contact) {} +void roster_add_new(const char * const barejid, const char * const name) {} +void roster_send_remove(const char * const barejid) {} From 07308673261449c3aab3bbed627a8d61da7a0470 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 16:35:56 +0000 Subject: [PATCH 26/33] Added history and jid tests to cmocka --- Makefile.am | 2 + tests/test_history.c | 116 ++++++++++++++----------------- tests/test_history.h | 13 ++++ tests/test_jid.c | 159 ++++++++++++++++++------------------------- tests/test_jid.h | 23 +++++++ tests/testsuite.c | 44 +++++++++++- 6 files changed, 198 insertions(+), 159 deletions(-) create mode 100644 tests/test_history.h create mode 100644 tests/test_jid.h diff --git a/Makefile.am b/Makefile.am index e52fe8bd..c2c9f53e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,6 +83,8 @@ test_sources = \ tests/test_autocomplete.c \ tests/test_common.c \ tests/test_command.c \ + tests/test_history.c \ + tests/test_jid.c \ tests/testsuite.c main_source = src/main.c diff --git a/tests/test_history.c b/tests/test_history.c index 6af96946..1584b390 100644 --- a/tests/test_history.c +++ b/tests/test_history.c @@ -1,34 +1,38 @@ -#include -#include +#include +#include +#include +#include +#include + #include "tools/history.h" -void previous_on_empty_returns_null(void) +void previous_on_empty_returns_null(void **state) { History history = history_new(10); char *item = history_previous(history, "inp"); - assert_is_null(item); + assert_null(item); } -void next_on_empty_returns_null(void) +void next_on_empty_returns_null(void **state) { History history = history_new(10); char *item = history_next(history, "inp"); - assert_is_null(item); + assert_null(item); } -void previous_once_returns_last(void) +void previous_once_returns_last(void **state) { History history = history_new(10); history_append(history, "Hello"); char *item = history_previous(history, "inp"); - assert_string_equals("Hello", item); + assert_string_equal("Hello", item); } -void previous_twice_when_one_returns_first(void) +void previous_twice_when_one_returns_first(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -36,10 +40,10 @@ void previous_twice_when_one_returns_first(void) char *item1 = history_previous(history, NULL); char *item2 = history_previous(history, item1); - assert_string_equals("Hello", item2); + assert_string_equal("Hello", item2); } -void previous_always_stops_at_first(void) +void previous_always_stops_at_first(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -51,10 +55,10 @@ void previous_always_stops_at_first(void) char *item5 = history_previous(history, item4); char *item6 = history_previous(history, item5); - assert_string_equals("Hello", item6); + assert_string_equal("Hello", item6); } -void previous_goes_to_correct_element(void) +void previous_goes_to_correct_element(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -68,10 +72,10 @@ void previous_goes_to_correct_element(void) char *item2 = history_previous(history, item1); char *item3 = history_previous(history, item2); - assert_string_equals("going", item3); + assert_string_equal("going", item3); } -void prev_then_next_returns_empty(void) +void prev_then_next_returns_empty(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -79,10 +83,10 @@ void prev_then_next_returns_empty(void) char *item1 = history_previous(history, NULL); char *item2 = history_next(history, item1); - assert_string_equals("", item2); + assert_string_equal("", item2); } -void prev_with_val_then_next_returns_val(void) +void prev_with_val_then_next_returns_val(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -90,10 +94,10 @@ void prev_with_val_then_next_returns_val(void) char *item1 = history_previous(history, "Oioi"); char *item2 = history_next(history, item1); - assert_string_equals("Oioi", item2); + assert_string_equal("Oioi", item2); } -void prev_with_val_then_next_twice_returns_null(void) +void prev_with_val_then_next_twice_returns_null(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -102,10 +106,10 @@ void prev_with_val_then_next_twice_returns_null(void) char *item2 = history_next(history, item1); char *item3 = history_next(history, item2); - assert_is_null(item3); + assert_null(item3); } -void navigate_then_append_new(void) +void navigate_then_append_new(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -115,25 +119,25 @@ void navigate_then_append_new(void) history_append(history, "append"); char *item1 = history_previous(history, "new text"); - assert_string_equals("append", item1); + assert_string_equal("append", item1); char *item2 = history_previous(history, item1); - assert_string_equals("history", item2); + assert_string_equal("history", item2); char *item3 = history_previous(history, item2); - assert_string_equals("testing", item3); + assert_string_equal("testing", item3); char *item4 = history_next(history, item3); - assert_string_equals("history", item4); + assert_string_equal("history", item4); char *item5 = history_next(history, item4); - assert_string_equals("append", item5); + assert_string_equal("append", item5); char *item6 = history_next(history, item5); - assert_string_equals("new text", item6); + assert_string_equal("new text", item6); } -void edit_item_mid_history(void) +void edit_item_mid_history(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -143,37 +147,37 @@ void edit_item_mid_history(void) history_append(history, "append"); char *item1 = history_previous(history, "new item"); - assert_string_equals("append", item1); + assert_string_equal("append", item1); char *item2 = history_previous(history, item1); - assert_string_equals("history", item2); + assert_string_equal("history", item2); char *item3 = history_previous(history, item2); - assert_string_equals("testing", item3); + assert_string_equal("testing", item3); char *item4 = history_previous(history, "EDITED"); - assert_string_equals("again", item4); + assert_string_equal("again", item4); char *item5 = history_previous(history, item4); - assert_string_equals("Hello", item5); + assert_string_equal("Hello", item5); char *item6 = history_next(history, item5); - assert_string_equals("again", item6); + assert_string_equal("again", item6); char *item7 = history_next(history, item6); - assert_string_equals("EDITED", item7); + assert_string_equal("EDITED", item7); char *item8 = history_next(history, item7); - assert_string_equals("history", item8); + assert_string_equal("history", item8); char *item9 = history_next(history, item8); - assert_string_equals("append", item9); + assert_string_equal("append", item9); char *item10 = history_next(history, item9); - assert_string_equals("new item", item10); + assert_string_equal("new item", item10); } -void edit_previous_and_append(void) +void edit_previous_and_append(void **state) { History history = history_new(10); history_append(history, "Hello"); @@ -183,51 +187,33 @@ void edit_previous_and_append(void) history_append(history, "append"); char *item1 = history_previous(history, "new item"); - assert_string_equals("append", item1); + assert_string_equal("append", item1); char *item2 = history_previous(history, item1); - assert_string_equals("history", item2); + assert_string_equal("history", item2); char *item3 = history_previous(history, item2); - assert_string_equals("testing", item3); + assert_string_equal("testing", item3); history_append(history, "EDITED"); char *item4 = history_previous(history, NULL); - assert_string_equals("EDITED", item4); + assert_string_equal("EDITED", item4); } -void start_session_add_new_submit_previous(void) +void start_session_add_new_submit_previous(void **state) { History history = history_new(10); history_append(history, "hello"); char *item1 = history_previous(history, NULL); - assert_string_equals("hello", item1); + assert_string_equal("hello", item1); char *item2 = history_next(history, item1); - assert_string_equals("", item2); + assert_string_equal("", item2); char *item3 = history_previous(history, "new text"); - assert_string_equals("hello", item3); + assert_string_equal("hello", item3); history_append(history, item3); } - -void register_history_tests(void) -{ - TEST_MODULE("history tests"); - TEST(previous_on_empty_returns_null); - TEST(next_on_empty_returns_null); - TEST(previous_once_returns_last); - TEST(previous_twice_when_one_returns_first); - TEST(previous_always_stops_at_first); - TEST(previous_goes_to_correct_element); - TEST(prev_then_next_returns_empty); - TEST(prev_with_val_then_next_returns_val); - TEST(prev_with_val_then_next_twice_returns_null); - TEST(navigate_then_append_new); - TEST(edit_item_mid_history); - TEST(edit_previous_and_append); - TEST(start_session_add_new_submit_previous); -} diff --git a/tests/test_history.h b/tests/test_history.h new file mode 100644 index 00000000..e6e8ec3f --- /dev/null +++ b/tests/test_history.h @@ -0,0 +1,13 @@ +void previous_on_empty_returns_null(void **state); +void next_on_empty_returns_null(void **state); +void previous_once_returns_last(void **state); +void previous_twice_when_one_returns_first(void **state); +void previous_always_stops_at_first(void **state); +void previous_goes_to_correct_element(void **state); +void prev_then_next_returns_empty(void **state); +void prev_with_val_then_next_returns_val(void **state); +void prev_with_val_then_next_twice_returns_null(void **state); +void navigate_then_append_new(void **state); +void edit_item_mid_history(void **state); +void edit_previous_and_append(void **state); +void start_session_add_new_submit_previous(void **state); diff --git a/tests/test_jid.c b/tests/test_jid.c index 0b9da5ab..df8096d1 100644 --- a/tests/test_jid.c +++ b/tests/test_jid.c @@ -1,192 +1,167 @@ +#include +#include +#include +#include #include -#include -#include + #include "jid.h" -void create_jid_from_null_returns_null(void) +void create_jid_from_null_returns_null(void **state) { Jid *result = jid_create(NULL); - assert_is_null(result); + assert_null(result); } -void create_jid_from_empty_string_returns_null(void) +void create_jid_from_empty_string_returns_null(void **state) { Jid *result = jid_create(""); - assert_is_null(result); + assert_null(result); } -void create_jid_from_full_returns_full(void) +void create_jid_from_full_returns_full(void **state) { Jid *result = jid_create("myuser@mydomain/laptop"); - assert_string_equals("myuser@mydomain/laptop", result->fulljid); + assert_string_equal("myuser@mydomain/laptop", result->fulljid); } -void create_jid_from_full_returns_bare(void) +void create_jid_from_full_returns_bare(void **state) { Jid *result = jid_create("myuser@mydomain/laptop"); - assert_string_equals("myuser@mydomain", result->barejid); + assert_string_equal("myuser@mydomain", result->barejid); } -void create_jid_from_full_returns_resourcepart(void) +void create_jid_from_full_returns_resourcepart(void **state) { Jid *result = jid_create("myuser@mydomain/laptop"); - assert_string_equals("laptop", result->resourcepart); + assert_string_equal("laptop", result->resourcepart); } -void create_jid_from_full_returns_localpart(void) +void create_jid_from_full_returns_localpart(void **state) { Jid *result = jid_create("myuser@mydomain/laptop"); - assert_string_equals("myuser", result->localpart); + assert_string_equal("myuser", result->localpart); } -void create_jid_from_full_returns_domainpart(void) +void create_jid_from_full_returns_domainpart(void **state) { Jid *result = jid_create("myuser@mydomain/laptop"); - assert_string_equals("mydomain", result->domainpart); + assert_string_equal("mydomain", result->domainpart); } -void create_jid_from_full_nolocal_returns_full(void) +void create_jid_from_full_nolocal_returns_full(void **state) { Jid *result = jid_create("mydomain/laptop"); - assert_string_equals("mydomain/laptop", result->fulljid); + assert_string_equal("mydomain/laptop", result->fulljid); } -void create_jid_from_full_nolocal_returns_bare(void) +void create_jid_from_full_nolocal_returns_bare(void **state) { Jid *result = jid_create("mydomain/laptop"); - assert_string_equals("mydomain", result->barejid); + assert_string_equal("mydomain", result->barejid); } -void create_jid_from_full_nolocal_returns_resourcepart(void) +void create_jid_from_full_nolocal_returns_resourcepart(void **state) { Jid *result = jid_create("mydomain/laptop"); - assert_string_equals("laptop", result->resourcepart); + assert_string_equal("laptop", result->resourcepart); } -void create_jid_from_full_nolocal_returns_domainpart(void) +void create_jid_from_full_nolocal_returns_domainpart(void **state) { Jid *result = jid_create("mydomain/laptop"); - assert_string_equals("mydomain", result->domainpart); + assert_string_equal("mydomain", result->domainpart); } -void create_jid_from_full_nolocal_returns_null_localpart(void) +void create_jid_from_full_nolocal_returns_null_localpart(void **state) { Jid *result = jid_create("mydomain/laptop"); - assert_is_null(result->localpart); + assert_null(result->localpart); } -void create_jid_from_bare_returns_null_full(void) +void create_jid_from_bare_returns_null_full(void **state) { Jid *result = jid_create("myuser@mydomain"); - assert_is_null(result->fulljid); + assert_null(result->fulljid); } -void create_jid_from_bare_returns_null_resource(void) +void create_jid_from_bare_returns_null_resource(void **state) { Jid *result = jid_create("myuser@mydomain"); - assert_is_null(result->resourcepart); + assert_null(result->resourcepart); } -void create_jid_from_bare_returns_bare(void) +void create_jid_from_bare_returns_bare(void **state) { Jid *result = jid_create("myuser@mydomain"); - assert_string_equals("myuser@mydomain", result->barejid); + assert_string_equal("myuser@mydomain", result->barejid); } -void create_jid_from_bare_returns_localpart(void) +void create_jid_from_bare_returns_localpart(void **state) { Jid *result = jid_create("myuser@mydomain"); - assert_string_equals("myuser", result->localpart); + assert_string_equal("myuser", result->localpart); } -void create_jid_from_bare_returns_domainpart(void) +void create_jid_from_bare_returns_domainpart(void **state) { Jid *result = jid_create("myuser@mydomain"); - assert_string_equals("mydomain", result->domainpart); + assert_string_equal("mydomain", result->domainpart); } -void create_room_jid_returns_room(void) +void create_room_jid_returns_room(void **state) { Jid *result = jid_create_from_bare_and_resource("room@conference.domain.org", "myname"); - assert_string_equals("room@conference.domain.org", result->barejid); + assert_string_equal("room@conference.domain.org", result->barejid); } -void create_room_jid_returns_nick(void) +void create_room_jid_returns_nick(void **state) { Jid *result = jid_create_from_bare_and_resource("room@conference.domain.org", "myname"); - assert_string_equals("myname", result->resourcepart); + assert_string_equal("myname", result->resourcepart); } -void create_with_slash_in_resource(void) +void create_with_slash_in_resource(void **state) { Jid *result = jid_create("room@conference.domain.org/my/nick"); - assert_string_equals("room", result->localpart); - assert_string_equals("conference.domain.org", result->domainpart); - assert_string_equals("my/nick", result->resourcepart); - assert_string_equals("room@conference.domain.org", result->barejid); - assert_string_equals("room@conference.domain.org/my/nick", result->fulljid); + assert_string_equal("room", result->localpart); + assert_string_equal("conference.domain.org", result->domainpart); + assert_string_equal("my/nick", result->resourcepart); + assert_string_equal("room@conference.domain.org", result->barejid); + assert_string_equal("room@conference.domain.org/my/nick", result->fulljid); } -void create_with_at_in_resource(void) +void create_with_at_in_resource(void **state) { Jid *result = jid_create("room@conference.domain.org/my@nick"); - assert_string_equals("room", result->localpart); - assert_string_equals("conference.domain.org", result->domainpart); - assert_string_equals("my@nick", result->resourcepart); - assert_string_equals("room@conference.domain.org", result->barejid); - assert_string_equals("room@conference.domain.org/my@nick", result->fulljid); + assert_string_equal("room", result->localpart); + assert_string_equal("conference.domain.org", result->domainpart); + assert_string_equal("my@nick", result->resourcepart); + assert_string_equal("room@conference.domain.org", result->barejid); + assert_string_equal("room@conference.domain.org/my@nick", result->fulljid); } -void create_with_at_and_slash_in_resource(void) +void create_with_at_and_slash_in_resource(void **state) { Jid *result = jid_create("room@conference.domain.org/my@nick/something"); - assert_string_equals("room", result->localpart); - assert_string_equals("conference.domain.org", result->domainpart); - assert_string_equals("my@nick/something", result->resourcepart); - assert_string_equals("room@conference.domain.org", result->barejid); - assert_string_equals("room@conference.domain.org/my@nick/something", result->fulljid); + assert_string_equal("room", result->localpart); + assert_string_equal("conference.domain.org", result->domainpart); + assert_string_equal("my@nick/something", result->resourcepart); + assert_string_equal("room@conference.domain.org", result->barejid); + assert_string_equal("room@conference.domain.org/my@nick/something", result->fulljid); } -void create_full_with_trailing_slash(void) +void create_full_with_trailing_slash(void **state) { Jid *result = jid_create("room@conference.domain.org/nick/"); - assert_string_equals("room", result->localpart); - assert_string_equals("conference.domain.org", result->domainpart); - assert_string_equals("nick/", result->resourcepart); - assert_string_equals("room@conference.domain.org", result->barejid); - assert_string_equals("room@conference.domain.org/nick/", result->fulljid); -} - -void register_jid_tests(void) -{ - TEST_MODULE("jid tests"); - TEST(create_jid_from_null_returns_null); - TEST(create_jid_from_empty_string_returns_null); - TEST(create_jid_from_full_returns_full); - TEST(create_jid_from_full_returns_bare); - TEST(create_jid_from_full_returns_resourcepart); - TEST(create_jid_from_full_returns_localpart); - TEST(create_jid_from_full_returns_domainpart); - TEST(create_jid_from_full_nolocal_returns_full); - TEST(create_jid_from_full_nolocal_returns_bare); - TEST(create_jid_from_full_nolocal_returns_resourcepart); - TEST(create_jid_from_full_nolocal_returns_domainpart); - TEST(create_jid_from_full_nolocal_returns_null_localpart); - TEST(create_jid_from_bare_returns_null_full); - TEST(create_jid_from_bare_returns_null_resource); - TEST(create_jid_from_bare_returns_bare); - TEST(create_jid_from_bare_returns_localpart); - TEST(create_jid_from_bare_returns_domainpart); - TEST(create_room_jid_returns_room); - TEST(create_room_jid_returns_nick); - TEST(create_with_slash_in_resource); - TEST(create_with_at_in_resource); - TEST(create_with_at_and_slash_in_resource); - TEST(create_full_with_trailing_slash); + assert_string_equal("room", result->localpart); + assert_string_equal("conference.domain.org", result->domainpart); + assert_string_equal("nick/", result->resourcepart); + assert_string_equal("room@conference.domain.org", result->barejid); + assert_string_equal("room@conference.domain.org/nick/", result->fulljid); } diff --git a/tests/test_jid.h b/tests/test_jid.h new file mode 100644 index 00000000..95de541f --- /dev/null +++ b/tests/test_jid.h @@ -0,0 +1,23 @@ +void create_jid_from_null_returns_null(void **state); +void create_jid_from_empty_string_returns_null(void **state); +void create_jid_from_full_returns_full(void **state); +void create_jid_from_full_returns_bare(void **state); +void create_jid_from_full_returns_resourcepart(void **state); +void create_jid_from_full_returns_localpart(void **state); +void create_jid_from_full_returns_domainpart(void **state); +void create_jid_from_full_nolocal_returns_full(void **state); +void create_jid_from_full_nolocal_returns_bare(void **state); +void create_jid_from_full_nolocal_returns_resourcepart(void **state); +void create_jid_from_full_nolocal_returns_domainpart(void **state); +void create_jid_from_full_nolocal_returns_null_localpart(void **state); +void create_jid_from_bare_returns_null_full(void **state); +void create_jid_from_bare_returns_null_resource(void **state); +void create_jid_from_bare_returns_bare(void **state); +void create_jid_from_bare_returns_localpart(void **state); +void create_jid_from_bare_returns_domainpart(void **state); +void create_room_jid_returns_room(void **state); +void create_room_jid_returns_nick(void **state); +void create_with_slash_in_resource(void **state); +void create_with_at_in_resource(void **state); +void create_with_at_and_slash_in_resource(void **state); +void create_full_with_trailing_slash(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 6215b761..9e3ddf32 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -6,6 +6,8 @@ #include "test_autocomplete.h" #include "test_common.h" #include "test_command.h" +#include "test_history.h" +#include "test_jid.h" int main(int argc, char* argv[]) { const UnitTest tests[] = { @@ -53,7 +55,45 @@ int main(int argc, char* argv[]) { unit_test(add_two_and_complete_returns_second), unit_test(add_two_adds_two), unit_test(add_two_same_adds_one), - unit_test(add_two_same_updates) - }; + unit_test(add_two_same_updates), + + unit_test(previous_on_empty_returns_null), + unit_test(next_on_empty_returns_null), + unit_test(previous_once_returns_last), + unit_test(previous_twice_when_one_returns_first), + unit_test(previous_always_stops_at_first), + unit_test(previous_goes_to_correct_element), + unit_test(prev_then_next_returns_empty), + unit_test(prev_with_val_then_next_returns_val), + unit_test(prev_with_val_then_next_twice_returns_null), + unit_test(navigate_then_append_new), + unit_test(edit_item_mid_history), + unit_test(edit_previous_and_append), + unit_test(start_session_add_new_submit_previous), + + unit_test(create_jid_from_null_returns_null), + unit_test(create_jid_from_empty_string_returns_null), + unit_test(create_jid_from_full_returns_full), + unit_test(create_jid_from_full_returns_bare), + unit_test(create_jid_from_full_returns_resourcepart), + unit_test(create_jid_from_full_returns_localpart), + unit_test(create_jid_from_full_returns_domainpart), + unit_test(create_jid_from_full_nolocal_returns_full), + unit_test(create_jid_from_full_nolocal_returns_bare), + unit_test(create_jid_from_full_nolocal_returns_resourcepart), + unit_test(create_jid_from_full_nolocal_returns_domainpart), + unit_test(create_jid_from_full_nolocal_returns_null_localpart), + unit_test(create_jid_from_bare_returns_null_full), + unit_test(create_jid_from_bare_returns_null_resource), + unit_test(create_jid_from_bare_returns_bare), + unit_test(create_jid_from_bare_returns_localpart), + unit_test(create_jid_from_bare_returns_domainpart), + unit_test(create_room_jid_returns_room), + unit_test(create_room_jid_returns_nick), + unit_test(create_with_slash_in_resource), + unit_test(create_with_at_in_resource), + unit_test(create_with_at_and_slash_in_resource), + unit_test(create_full_with_trailing_slash) + }; return run_tests(tests); } From 4bb38ac011b823bd8d2a4ba4beb776f757843d10 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 16:48:27 +0000 Subject: [PATCH 27/33] Added parser tests to cmocka --- Makefile.am | 1 + tests/test_parser.c | 314 +++++++++++++++++++------------------------- tests/test_parser.h | 41 ++++++ tests/testsuite.c | 45 ++++++- 4 files changed, 221 insertions(+), 180 deletions(-) create mode 100644 tests/test_parser.h diff --git a/Makefile.am b/Makefile.am index c2c9f53e..7f4ff20b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -85,6 +85,7 @@ test_sources = \ tests/test_command.c \ tests/test_history.c \ tests/test_jid.c \ + tests/test_parser.c \ tests/testsuite.c main_source = src/main.c diff --git a/tests/test_parser.c b/tests/test_parser.c index 6295aafd..49fc4354 100644 --- a/tests/test_parser.c +++ b/tests/test_parser.c @@ -1,493 +1,449 @@ +#include +#include +#include +#include #include -#include -#include + #include "tools/parser.h" void -parse_null_returns_null(void) +parse_null_returns_null(void **state) { char *inp = NULL; gchar **result = parse_args(inp, 1, 2); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_empty_returns_null(void) +parse_empty_returns_null(void **state) { char *inp = ""; gchar **result = parse_args(inp, 1, 2); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_space_returns_null(void) +parse_space_returns_null(void **state) { char *inp = " "; gchar **result = parse_args(inp, 1, 2); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_cmd_no_args_returns_null(void) +parse_cmd_no_args_returns_null(void **state) { char *inp = "/cmd"; gchar **result = parse_args(inp, 1, 2); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_cmd_with_space_returns_null(void) +parse_cmd_with_space_returns_null(void **state) { char *inp = "/cmd "; gchar **result = parse_args(inp, 1, 2); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_cmd_with_too_few_returns_null(void) +parse_cmd_with_too_few_returns_null(void **state) { char *inp = "/cmd arg1"; gchar **result = parse_args(inp, 2, 3); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_cmd_with_too_many_returns_null(void) +parse_cmd_with_too_many_returns_null(void **state) { char *inp = "/cmd arg1 arg2 arg3 arg4"; gchar **result = parse_args(inp, 1, 3); - assert_is_null(result); + assert_null(result); g_strfreev(result); } void -parse_cmd_one_arg(void) +parse_cmd_one_arg(void **state) { char *inp = "/cmd arg1"; gchar **result = parse_args(inp, 1, 2); - assert_int_equals(1, g_strv_length(result)); - assert_string_equals("arg1", result[0]); + assert_int_equal(1, g_strv_length(result)); + assert_string_equal("arg1", result[0]); g_strfreev(result); } void -parse_cmd_two_args(void) +parse_cmd_two_args(void **state) { char *inp = "/cmd arg1 arg2"; gchar **result = parse_args(inp, 1, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); g_strfreev(result); } void -parse_cmd_three_args(void) +parse_cmd_three_args(void **state) { char *inp = "/cmd arg1 arg2 arg3"; gchar **result = parse_args(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("arg3", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("arg3", result[2]); g_strfreev(result); } void -parse_cmd_three_args_with_spaces(void) +parse_cmd_three_args_with_spaces(void **state) { char *inp = " /cmd arg1 arg2 arg3 "; gchar **result = parse_args(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("arg3", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("arg3", result[2]); g_strfreev(result); } void -parse_cmd_with_freetext(void) +parse_cmd_with_freetext(void **state) { char *inp = "/cmd this is some free text"; gchar **result = parse_args_with_freetext(inp, 1, 1); - assert_int_equals(1, g_strv_length(result)); - assert_string_equals("this is some free text", result[0]); + assert_int_equal(1, g_strv_length(result)); + assert_string_equal("this is some free text", result[0]); g_strfreev(result); } void -parse_cmd_one_arg_with_freetext(void) +parse_cmd_one_arg_with_freetext(void **state) { char *inp = "/cmd arg1 this is some free text"; gchar **result = parse_args_with_freetext(inp, 1, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("this is some free text", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("this is some free text", result[1]); g_strfreev(result); } void -parse_cmd_two_args_with_freetext(void) +parse_cmd_two_args_with_freetext(void **state) { char *inp = "/cmd arg1 arg2 this is some free text"; gchar **result = parse_args_with_freetext(inp, 1, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("this is some free text", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("this is some free text", result[2]); g_strfreev(result); } void -parse_cmd_min_zero(void) +parse_cmd_min_zero(void **state) { char *inp = "/cmd"; gchar **result = parse_args(inp, 0, 2); - assert_int_equals(0, g_strv_length(result)); - assert_is_null(result[0]); + assert_int_equal(0, g_strv_length(result)); + assert_null(result[0]); g_strfreev(result); } void -parse_cmd_min_zero_with_freetext(void) +parse_cmd_min_zero_with_freetext(void **state) { char *inp = "/cmd"; gchar **result = parse_args_with_freetext(inp, 0, 2); - assert_int_equals(0, g_strv_length(result)); - assert_is_null(result[0]); + assert_int_equal(0, g_strv_length(result)); + assert_null(result[0]); g_strfreev(result); } void -parse_cmd_with_quoted(void) +parse_cmd_with_quoted(void **state) { char *inp = "/cmd \"arg1\" arg2"; gchar **result = parse_args(inp, 2, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); g_strfreev(result); } void -parse_cmd_with_quoted_and_space(void) +parse_cmd_with_quoted_and_space(void **state) { char *inp = "/cmd \"the arg1\" arg2"; gchar **result = parse_args(inp, 2, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("the arg1", result[0]); - assert_string_equals("arg2", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("the arg1", result[0]); + assert_string_equal("arg2", result[1]); g_strfreev(result); } void -parse_cmd_with_quoted_and_many_spaces(void) +parse_cmd_with_quoted_and_many_spaces(void **state) { char *inp = "/cmd \"the arg1 is here\" arg2"; gchar **result = parse_args(inp, 2, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("the arg1 is here", result[0]); - assert_string_equals("arg2", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("the arg1 is here", result[0]); + assert_string_equal("arg2", result[1]); g_strfreev(result); } void -parse_cmd_with_many_quoted_and_many_spaces(void) +parse_cmd_with_many_quoted_and_many_spaces(void **state) { char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\""; gchar **result = parse_args(inp, 2, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("the arg1 is here", result[0]); - assert_string_equals("and arg2 is right here", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("the arg1 is here", result[0]); + assert_string_equal("and arg2 is right here", result[1]); g_strfreev(result); } void -parse_cmd_freetext_with_quoted(void) +parse_cmd_freetext_with_quoted(void **state) { char *inp = "/cmd \"arg1\" arg2 hello there whats up"; gchar **result = parse_args_with_freetext(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("hello there whats up", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("hello there whats up", result[2]); g_strfreev(result); } void -parse_cmd_freetext_with_quoted_and_space(void) +parse_cmd_freetext_with_quoted_and_space(void **state) { char *inp = "/cmd \"the arg1\" arg2 another bit of freetext"; gchar **result = parse_args_with_freetext(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("the arg1", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("another bit of freetext", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("the arg1", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("another bit of freetext", result[2]); g_strfreev(result); } void -parse_cmd_freetext_with_quoted_and_many_spaces(void) +parse_cmd_freetext_with_quoted_and_many_spaces(void **state) { char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext"; gchar **result = parse_args_with_freetext(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("the arg1 is here", result[0]); - assert_string_equals("arg2", result[1]); - assert_string_equals("some more freetext", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("the arg1 is here", result[0]); + assert_string_equal("arg2", result[1]); + assert_string_equal("some more freetext", result[2]); g_strfreev(result); } void -parse_cmd_freetext_with_many_quoted_and_many_spaces(void) +parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state) { char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text"; gchar **result = parse_args_with_freetext(inp, 3, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("the arg1 is here", result[0]); - assert_string_equals("and arg2 is right here", result[1]); - assert_string_equals("and heres the free text", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("the arg1 is here", result[0]); + assert_string_equal("and arg2 is right here", result[1]); + assert_string_equal("and heres the free text", result[2]); g_strfreev(result); } void -parse_cmd_with_quoted_freetext(void) +parse_cmd_with_quoted_freetext(void **state) { char *inp = "/cmd arg1 here is \"some\" quoted freetext"; gchar **result = parse_args_with_freetext(inp, 1, 2); - assert_int_equals(2, g_strv_length(result)); - assert_string_equals("arg1", result[0]); - assert_string_equals("here is \"some\" quoted freetext", result[1]); + assert_int_equal(2, g_strv_length(result)); + assert_string_equal("arg1", result[0]); + assert_string_equal("here is \"some\" quoted freetext", result[1]); g_strfreev(result); } void -parse_cmd_with_third_arg_quoted_0_min_3_max(void) +parse_cmd_with_third_arg_quoted_0_min_3_max(void **state) { char *inp = "/group add friends \"The User\""; gchar **result = parse_args_with_freetext(inp, 0, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("add", result[0]); - assert_string_equals("friends", result[1]); - assert_string_equals("The User", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("add", result[0]); + assert_string_equal("friends", result[1]); + assert_string_equal("The User", result[2]); } void -parse_cmd_with_second_arg_quoted_0_min_3_max(void) +parse_cmd_with_second_arg_quoted_0_min_3_max(void **state) { char *inp = "/group add \"The Group\" friend"; gchar **result = parse_args_with_freetext(inp, 0, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("add", result[0]); - assert_string_equals("The Group", result[1]); - assert_string_equals("friend", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("add", result[0]); + assert_string_equal("The Group", result[1]); + assert_string_equal("friend", result[2]); } void -parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void) +parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state) { char *inp = "/group add \"The Group\" \"The User\""; gchar **result = parse_args_with_freetext(inp, 0, 3); - assert_int_equals(3, g_strv_length(result)); - assert_string_equals("add", result[0]); - assert_string_equals("The Group", result[1]); - assert_string_equals("The User", result[2]); + assert_int_equal(3, g_strv_length(result)); + assert_string_equal("add", result[0]); + assert_string_equal("The Group", result[1]); + assert_string_equal("The User", result[2]); } void -count_one_token(void) +count_one_token(void **state) { char *inp = "one"; int result = count_tokens(inp); - assert_int_equals(1, result); + assert_int_equal(1, result); } void -count_one_token_quoted_no_whitespace(void) +count_one_token_quoted_no_whitespace(void **state) { char *inp = "\"one\""; int result = count_tokens(inp); - assert_int_equals(1, result); + assert_int_equal(1, result); } void -count_one_token_quoted_with_whitespace(void) +count_one_token_quoted_with_whitespace(void **state) { char *inp = "\"one two\""; int result = count_tokens(inp); - assert_int_equals(1, result); + assert_int_equal(1, result); } void -count_two_tokens(void) +count_two_tokens(void **state) { char *inp = "one two"; int result = count_tokens(inp); - assert_int_equals(2, result); + assert_int_equal(2, result); } void -count_two_tokens_first_quoted(void) +count_two_tokens_first_quoted(void **state) { char *inp = "\"one and\" two"; int result = count_tokens(inp); - assert_int_equals(2, result); + assert_int_equal(2, result); } void -count_two_tokens_second_quoted(void) +count_two_tokens_second_quoted(void **state) { char *inp = "one \"two and\""; int result = count_tokens(inp); - assert_int_equals(2, result); + assert_int_equal(2, result); } void -count_two_tokens_both_quoted(void) +count_two_tokens_both_quoted(void **state) { char *inp = "\"one and then\" \"two and\""; int result = count_tokens(inp); - assert_int_equals(2, result); + assert_int_equal(2, result); } void -get_first_of_one(void) +get_first_of_one(void **state) { char *inp = "one"; char *result = get_start(inp, 2); - assert_string_equals("one", result); + assert_string_equal("one", result); } void -get_first_of_two(void) +get_first_of_two(void **state) { char *inp = "one two"; char *result = get_start(inp, 2); - assert_string_equals("one ", result); + assert_string_equal("one ", result); } void -get_first_two_of_three(void) +get_first_two_of_three(void **state) { char *inp = "one two three"; char *result = get_start(inp, 3); - assert_string_equals("one two ", result); + assert_string_equal("one two ", result); } void -get_first_two_of_three_first_quoted(void) +get_first_two_of_three_first_quoted(void **state) { char *inp = "\"one\" two three"; char *result = get_start(inp, 3); - assert_string_equals("\"one\" two ", result); + assert_string_equal("\"one\" two ", result); } void -get_first_two_of_three_second_quoted(void) +get_first_two_of_three_second_quoted(void **state) { char *inp = "one \"two\" three"; char *result = get_start(inp, 3); - assert_string_equals("one \"two\" ", result); + assert_string_equal("one \"two\" ", result); } void -get_first_two_of_three_first_and_second_quoted(void) +get_first_two_of_three_first_and_second_quoted(void **state) { char *inp = "\"one\" \"two\" three"; char *result = get_start(inp, 3); - assert_string_equals("\"one\" \"two\" ", result); -} - -void -register_parser_tests(void) -{ - TEST_MODULE("parser tests"); - TEST(parse_null_returns_null); - TEST(parse_empty_returns_null); - TEST(parse_space_returns_null); - TEST(parse_cmd_no_args_returns_null); - TEST(parse_cmd_with_space_returns_null); - TEST(parse_cmd_one_arg); - TEST(parse_cmd_two_args); - TEST(parse_cmd_three_args); - TEST(parse_cmd_three_args_with_spaces); - TEST(parse_cmd_with_freetext); - TEST(parse_cmd_one_arg_with_freetext); - TEST(parse_cmd_two_args_with_freetext); - TEST(parse_cmd_with_too_few_returns_null); - TEST(parse_cmd_with_too_many_returns_null); - TEST(parse_cmd_min_zero); - TEST(parse_cmd_min_zero_with_freetext); - TEST(parse_cmd_with_quoted); - TEST(parse_cmd_with_quoted_and_space); - TEST(parse_cmd_with_quoted_and_many_spaces); - TEST(parse_cmd_with_many_quoted_and_many_spaces); - TEST(parse_cmd_freetext_with_quoted); - TEST(parse_cmd_freetext_with_quoted_and_space); - TEST(parse_cmd_freetext_with_quoted_and_many_spaces); - TEST(parse_cmd_freetext_with_many_quoted_and_many_spaces); - TEST(parse_cmd_with_quoted_freetext); - TEST(count_one_token); - TEST(count_one_token_quoted_no_whitespace); - TEST(count_one_token_quoted_with_whitespace); - TEST(count_two_tokens); - TEST(count_two_tokens_first_quoted); - TEST(count_two_tokens_second_quoted); - TEST(count_two_tokens_both_quoted); - TEST(get_first_of_one); - TEST(get_first_of_two); - TEST(get_first_two_of_three); - TEST(get_first_two_of_three_first_quoted); - TEST(get_first_two_of_three_second_quoted); - TEST(get_first_two_of_three_first_and_second_quoted); - TEST(parse_cmd_with_third_arg_quoted_0_min_3_max); - TEST(parse_cmd_with_second_arg_quoted_0_min_3_max); - TEST(parse_cmd_with_second_and_third_arg_quoted_0_min_3_max); + assert_string_equal("\"one\" \"two\" ", result); } diff --git a/tests/test_parser.h b/tests/test_parser.h new file mode 100644 index 00000000..7b178c3d --- /dev/null +++ b/tests/test_parser.h @@ -0,0 +1,41 @@ +void parse_null_returns_null(void **state); +void parse_empty_returns_null(void **state); +void parse_space_returns_null(void **state); +void parse_cmd_no_args_returns_null(void **state); +void parse_cmd_with_space_returns_null(void **state); +void parse_cmd_with_too_few_returns_null(void **state); +void parse_cmd_with_too_many_returns_null(void **state); +void parse_cmd_one_arg(void **state); +void parse_cmd_two_args(void **state); +void parse_cmd_three_args(void **state); +void parse_cmd_three_args_with_spaces(void **state); +void parse_cmd_with_freetext(void **state); +void parse_cmd_one_arg_with_freetext(void **state); +void parse_cmd_two_args_with_freetext(void **state); +void parse_cmd_min_zero(void **state); +void parse_cmd_min_zero_with_freetext(void **state); +void parse_cmd_with_quoted(void **state); +void parse_cmd_with_quoted_and_space(void **state); +void parse_cmd_with_quoted_and_many_spaces(void **state); +void parse_cmd_with_many_quoted_and_many_spaces(void **state); +void parse_cmd_freetext_with_quoted(void **state); +void parse_cmd_freetext_with_quoted_and_space(void **state); +void parse_cmd_freetext_with_quoted_and_many_spaces(void **state); +void parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state); +void parse_cmd_with_quoted_freetext(void **state); +void parse_cmd_with_third_arg_quoted_0_min_3_max(void **state); +void parse_cmd_with_second_arg_quoted_0_min_3_max(void **state); +void parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state); +void count_one_token(void **state); +void count_one_token_quoted_no_whitespace(void **state); +void count_one_token_quoted_with_whitespace(void **state); +void count_two_tokens(void **state); +void count_two_tokens_first_quoted(void **state); +void count_two_tokens_second_quoted(void **state); +void count_two_tokens_both_quoted(void **state); +void get_first_of_one(void **state); +void get_first_of_two(void **state); +void get_first_two_of_three(void **state); +void get_first_two_of_three_first_quoted(void **state); +void get_first_two_of_three_second_quoted(void **state); +void get_first_two_of_three_first_and_second_quoted(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 9e3ddf32..482d71c0 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -8,6 +8,7 @@ #include "test_command.h" #include "test_history.h" #include "test_jid.h" +#include "test_parser.h" int main(int argc, char* argv[]) { const UnitTest tests[] = { @@ -93,7 +94,49 @@ int main(int argc, char* argv[]) { unit_test(create_with_slash_in_resource), unit_test(create_with_at_in_resource), unit_test(create_with_at_and_slash_in_resource), - unit_test(create_full_with_trailing_slash) + unit_test(create_full_with_trailing_slash), + + unit_test(parse_null_returns_null), + unit_test(parse_empty_returns_null), + unit_test(parse_space_returns_null), + unit_test(parse_cmd_no_args_returns_null), + unit_test(parse_cmd_with_space_returns_null), + unit_test(parse_cmd_with_too_few_returns_null), + unit_test(parse_cmd_with_too_many_returns_null), + unit_test(parse_cmd_one_arg), + unit_test(parse_cmd_two_args), + unit_test(parse_cmd_three_args), + unit_test(parse_cmd_three_args_with_spaces), + unit_test(parse_cmd_with_freetext), + unit_test(parse_cmd_one_arg_with_freetext), + unit_test(parse_cmd_two_args_with_freetext), + unit_test(parse_cmd_min_zero), + unit_test(parse_cmd_min_zero_with_freetext), + unit_test(parse_cmd_with_quoted), + unit_test(parse_cmd_with_quoted_and_space), + unit_test(parse_cmd_with_quoted_and_many_spaces), + unit_test(parse_cmd_with_many_quoted_and_many_spaces), + unit_test(parse_cmd_freetext_with_quoted), + unit_test(parse_cmd_freetext_with_quoted_and_space), + unit_test(parse_cmd_freetext_with_quoted_and_many_spaces), + unit_test(parse_cmd_freetext_with_many_quoted_and_many_spaces), + unit_test(parse_cmd_with_quoted_freetext), + unit_test(parse_cmd_with_third_arg_quoted_0_min_3_max), + unit_test(parse_cmd_with_second_arg_quoted_0_min_3_max), + unit_test(parse_cmd_with_second_and_third_arg_quoted_0_min_3_max), + unit_test(count_one_token), + unit_test(count_one_token_quoted_no_whitespace), + unit_test(count_one_token_quoted_with_whitespace), + unit_test(count_two_tokens), + unit_test(count_two_tokens_first_quoted), + unit_test(count_two_tokens_second_quoted), + unit_test(count_two_tokens_both_quoted), + unit_test(get_first_of_one), + unit_test(get_first_of_two), + unit_test(get_first_two_of_three), + unit_test(get_first_two_of_three_first_quoted), + unit_test(get_first_two_of_three_second_quoted), + unit_test(get_first_two_of_three_first_and_second_quoted), }; return run_tests(tests); } From 0b3a9f5785e0e6e09c2760a6c6ab220bd08f80d4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 17:03:39 +0000 Subject: [PATCH 28/33] Added roster_list tests to cmocka --- Makefile.am | 1 + tests/test_roster_list.c | 190 ++++++++++++++++++++------------------- tests/test_roster_list.h | 20 +++++ tests/testsuite.c | 22 +++++ 4 files changed, 140 insertions(+), 93 deletions(-) create mode 100644 tests/test_roster_list.h diff --git a/Makefile.am b/Makefile.am index 7f4ff20b..c54b8256 100644 --- a/Makefile.am +++ b/Makefile.am @@ -86,6 +86,7 @@ test_sources = \ tests/test_history.c \ tests/test_jid.c \ tests/test_parser.c \ + tests/test_roster_list.c \ tests/testsuite.c main_source = src/main.c diff --git a/tests/test_roster_list.c b/tests/test_roster_list.c index 1281beb3..b65d9f83 100644 --- a/tests/test_roster_list.c +++ b/tests/test_roster_list.c @@ -1,60 +1,60 @@ -#include -#include -#include - -#include #include +#include +#include +#include +#include +#include +#include #include "contact.h" #include "roster_list.h" -static void beforetest(void) +void empty_list_when_none_added(void **state) { roster_init(); -} - -static void aftertest(void) -{ + GSList *list = roster_get_contacts(); + assert_null(list); roster_free(); } -static void empty_list_when_none_added(void) -{ - GSList *list = roster_get_contacts(); - assert_is_null(list); -} - -static void contains_one_element(void) +void contains_one_element(void **state) { + roster_init(); printf("0\n"); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); printf("1\n"); GSList *list = roster_get_contacts(); printf("2\n"); - assert_int_equals(1, g_slist_length(list)); + assert_int_equal(1, g_slist_length(list)); printf("3\n"); + roster_free(); } -static void first_element_correct(void) +void first_element_correct(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); PContact james = list->data; - assert_string_equals("James", p_contact_barejid(james)); + assert_string_equal("James", p_contact_barejid(james)); + roster_free(); } -static void contains_two_elements(void) +void contains_two_elements(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); - assert_int_equals(2, g_slist_length(list)); + assert_int_equal(2, g_slist_length(list)); + roster_free(); } -static void first_and_second_elements_correct(void) +void first_and_second_elements_correct(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); @@ -62,22 +62,26 @@ static void first_and_second_elements_correct(void) PContact first = list->data; PContact second = (g_slist_next(list))->data; - assert_string_equals("Dave", p_contact_barejid(first)); - assert_string_equals("James", p_contact_barejid(second)); + assert_string_equal("Dave", p_contact_barejid(first)); + assert_string_equal("James", p_contact_barejid(second)); + roster_free(); } -static void contains_three_elements(void) +void contains_three_elements(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); - assert_int_equals(3, g_slist_length(list)); + assert_int_equal(3, g_slist_length(list)); + roster_free(); } -static void first_three_elements_correct(void) +void first_three_elements_correct(void **state) { + roster_init(); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); @@ -86,13 +90,15 @@ static void first_three_elements_correct(void) PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; - assert_string_equals("James", p_contact_barejid(james)); - assert_string_equals("Dave", p_contact_barejid(dave)); - assert_string_equals("Bob", p_contact_barejid(bob)); + assert_string_equal("James", p_contact_barejid(james)); + assert_string_equal("Dave", p_contact_barejid(dave)); + assert_string_equal("Bob", p_contact_barejid(bob)); + roster_free(); } -static void add_twice_at_beginning_adds_once(void) +void add_twice_at_beginning_adds_once(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); @@ -102,14 +108,16 @@ static void add_twice_at_beginning_adds_once(void) PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data; - assert_int_equals(3, g_slist_length(list)); - assert_string_equals("Bob", p_contact_barejid(first)); - assert_string_equals("Dave", p_contact_barejid(second)); - assert_string_equals("James", p_contact_barejid(third)); + assert_int_equal(3, g_slist_length(list)); + assert_string_equal("Bob", p_contact_barejid(first)); + assert_string_equal("Dave", p_contact_barejid(second)); + assert_string_equal("James", p_contact_barejid(third)); + roster_free(); } -static void add_twice_in_middle_adds_once(void) +void add_twice_in_middle_adds_once(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); @@ -119,14 +127,16 @@ static void add_twice_in_middle_adds_once(void) PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data; - assert_int_equals(3, g_slist_length(list)); - assert_string_equals("Bob", p_contact_barejid(first)); - assert_string_equals("Dave", p_contact_barejid(second)); - assert_string_equals("James", p_contact_barejid(third)); + assert_int_equal(3, g_slist_length(list)); + assert_string_equal("Bob", p_contact_barejid(first)); + assert_string_equal("Dave", p_contact_barejid(second)); + assert_string_equal("James", p_contact_barejid(third)); + roster_free(); } -static void add_twice_at_end_adds_once(void) +void add_twice_at_end_adds_once(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); @@ -136,32 +146,38 @@ static void add_twice_at_end_adds_once(void) PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data; - assert_int_equals(3, g_slist_length(list)); - assert_string_equals("Bob", p_contact_barejid(first)); - assert_string_equals("Dave", p_contact_barejid(second)); - assert_string_equals("James", p_contact_barejid(third)); + assert_int_equal(3, g_slist_length(list)); + assert_string_equal("Bob", p_contact_barejid(first)); + assert_string_equal("Dave", p_contact_barejid(second)); + assert_string_equal("James", p_contact_barejid(third)); + roster_free(); } -static void test_show_online_when_no_value(void) +void test_show_online_when_no_value(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); PContact james = list->data; - assert_string_equals("offline", p_contact_presence(james)); + assert_string_equal("offline", p_contact_presence(james)); + roster_free(); } -static void test_status_when_no_value(void) +void test_status_when_no_value(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); GSList *list = roster_get_contacts(); PContact james = list->data; - assert_is_null(p_contact_status(james)); + assert_null(p_contact_status(james)); + roster_free(); } -static void find_first_exists(void) +void find_first_exists(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); @@ -170,64 +186,76 @@ static void find_first_exists(void) strcpy(search, "B"); char *result = roster_find_contact(search); - assert_string_equals("Bob", result); + assert_string_equal("Bob", result); free(result); free(search); + roster_free(); } -static void find_second_exists(void) +void find_second_exists(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); char *result = roster_find_contact("Dav"); - assert_string_equals("Dave", result); + assert_string_equal("Dave", result); free(result); + roster_free(); } -static void find_third_exists(void) +void find_third_exists(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); char *result = roster_find_contact("Ja"); - assert_string_equals("James", result); + assert_string_equal("James", result); free(result); + roster_free(); } -static void find_returns_null(void) +void find_returns_null(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Dave", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); char *result = roster_find_contact("Mike"); - assert_is_null(result); + assert_null(result); + roster_free(); } -static void find_on_empty_returns_null(void) +void find_on_empty_returns_null(void **state) { + roster_init(); char *result = roster_find_contact("James"); - assert_is_null(result); + assert_null(result); + roster_free(); } -static void find_twice_returns_second_when_two_match(void) +void find_twice_returns_second_when_two_match(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Jamie", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); char *result1 = roster_find_contact("Jam"); char *result2 = roster_find_contact(result1); - assert_string_equals("Jamie", result2); + assert_string_equal("Jamie", result2); free(result1); free(result2); + roster_free(); } -static void find_five_times_finds_fifth(void) +void find_five_times_finds_fifth(void **state) { + roster_init(); roster_add("Jama", NULL, NULL, NULL, FALSE, TRUE); roster_add("Jamb", NULL, NULL, NULL, FALSE, TRUE); roster_add("Mike", NULL, NULL, NULL, FALSE, TRUE); @@ -244,16 +272,18 @@ static void find_five_times_finds_fifth(void) char *result3 = roster_find_contact(result2); char *result4 = roster_find_contact(result3); char *result5 = roster_find_contact(result4); - assert_string_equals("Jamo", result5); + assert_string_equal("Jamo", result5); free(result1); free(result2); free(result3); free(result4); free(result5); + roster_free(); } -static void find_twice_returns_first_when_two_match_and_reset(void) +void find_twice_returns_first_when_two_match_and_reset(void **state) { + roster_init(); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); roster_add("Jamie", NULL, NULL, NULL, FALSE, TRUE); roster_add("Bob", NULL, NULL, NULL, FALSE, TRUE); @@ -261,34 +291,8 @@ static void find_twice_returns_first_when_two_match_and_reset(void) char *result1 = roster_find_contact("Jam"); roster_reset_search_attempts(); char *result2 = roster_find_contact(result1); - assert_string_equals("James", result2); + assert_string_equal("James", result2); free(result1); free(result2); -} - -void register_roster_list_tests(void) -{ - TEST_MODULE("roster_list tests"); - BEFORETEST(beforetest); - AFTERTEST(aftertest); - TEST(empty_list_when_none_added); - TEST(contains_one_element); - TEST(first_element_correct); - TEST(contains_two_elements); - TEST(first_and_second_elements_correct); - TEST(contains_three_elements); - TEST(first_three_elements_correct); - TEST(add_twice_at_beginning_adds_once); - TEST(add_twice_in_middle_adds_once); - TEST(add_twice_at_end_adds_once); - TEST(test_show_online_when_no_value); - TEST(test_status_when_no_value); - TEST(find_first_exists); - TEST(find_second_exists); - TEST(find_third_exists); - TEST(find_returns_null); - TEST(find_on_empty_returns_null); - TEST(find_twice_returns_second_when_two_match); - TEST(find_twice_returns_first_when_two_match_and_reset); - TEST(find_five_times_finds_fifth); + roster_free(); } diff --git a/tests/test_roster_list.h b/tests/test_roster_list.h new file mode 100644 index 00000000..e5ac15f1 --- /dev/null +++ b/tests/test_roster_list.h @@ -0,0 +1,20 @@ +void empty_list_when_none_added(void **state); +void contains_one_element(void **state); +void first_element_correct(void **state); +void contains_two_elements(void **state); +void first_and_second_elements_correct(void **state); +void contains_three_elements(void **state); +void first_three_elements_correct(void **state); +void add_twice_at_beginning_adds_once(void **state); +void add_twice_in_middle_adds_once(void **state); +void add_twice_at_end_adds_once(void **state); +void test_show_online_when_no_value(void **state); +void test_status_when_no_value(void **state); +void find_first_exists(void **state); +void find_second_exists(void **state); +void find_third_exists(void **state); +void find_returns_null(void **state); +void find_on_empty_returns_null(void **state); +void find_twice_returns_second_when_two_match(void **state); +void find_five_times_finds_fifth(void **state); +void find_twice_returns_first_when_two_match_and_reset(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 482d71c0..c67557d1 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -9,6 +9,7 @@ #include "test_history.h" #include "test_jid.h" #include "test_parser.h" +#include "test_roster_list.h" int main(int argc, char* argv[]) { const UnitTest tests[] = { @@ -137,6 +138,27 @@ int main(int argc, char* argv[]) { unit_test(get_first_two_of_three_first_quoted), unit_test(get_first_two_of_three_second_quoted), unit_test(get_first_two_of_three_first_and_second_quoted), + + unit_test(empty_list_when_none_added), + unit_test(contains_one_element), + unit_test(first_element_correct), + unit_test(contains_two_elements), + unit_test(first_and_second_elements_correct), + unit_test(contains_three_elements), + unit_test(first_three_elements_correct), + unit_test(add_twice_at_beginning_adds_once), + unit_test(add_twice_in_middle_adds_once), + unit_test(add_twice_at_end_adds_once), + unit_test(test_show_online_when_no_value), + unit_test(test_status_when_no_value), + unit_test(find_first_exists), + unit_test(find_second_exists), + unit_test(find_third_exists), + unit_test(find_returns_null), + unit_test(find_on_empty_returns_null), + unit_test(find_twice_returns_second_when_two_match), + unit_test(find_five_times_finds_fifth), + unit_test(find_twice_returns_first_when_two_match_and_reset), }; return run_tests(tests); } From 3e3786eb613ec869954e0bb9b5e4e630f15aaa63 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 17:15:43 +0000 Subject: [PATCH 29/33] Removed debug from tests --- tests/test_command.c | 7 +++++-- tests/test_roster_list.c | 4 ---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_command.c b/tests/test_command.c index 13738032..d553f7e5 100644 --- a/tests/test_command.c +++ b/tests/test_command.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "xmpp/xmpp.h" #include "ui/ui.h" @@ -10,11 +11,13 @@ void cmd_rooms_shows_message_when_not_connected(void **state) { + CommandHelp *help = malloc(sizeof(CommandHelp)); will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_string(cons_show, msg, "You are not currently connected."); - CommandHelp *help = malloc(sizeof(CommandHelp)); + + gboolean result = _cmd_rooms(NULL, *help); - assert_true(_cmd_rooms(NULL, *help)); + assert_true(result); free(help); } diff --git a/tests/test_roster_list.c b/tests/test_roster_list.c index b65d9f83..4a78d877 100644 --- a/tests/test_roster_list.c +++ b/tests/test_roster_list.c @@ -20,13 +20,9 @@ void empty_list_when_none_added(void **state) void contains_one_element(void **state) { roster_init(); - printf("0\n"); roster_add("James", NULL, NULL, NULL, FALSE, TRUE); - printf("1\n"); GSList *list = roster_get_contacts(); - printf("2\n"); assert_int_equal(1, g_slist_length(list)); - printf("3\n"); roster_free(); } From b9119b4306ad110931b9ec51127c83a94ea43e6e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 18:06:09 +0000 Subject: [PATCH 30/33] Tidied Makefile.am --- Makefile.am | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index c54b8256..513364ea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -69,8 +69,8 @@ test_sources = \ src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ src/resource.c src/resource.h \ src/roster_list.c src/roster_list.h \ - src/xmpp/xmpp.h tests/xmpp/mock_xmpp.c \ - src/ui/ui.h tests/ui/mock_ui.c \ + src/xmpp/xmpp.h \ + src/ui/ui.h \ src/command/command.h src/command/command.c src/command/history.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ @@ -80,6 +80,8 @@ test_sources = \ src/config/accounts.c src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ + tests/ui/mock_ui.c \ + tests/xmpp/mock_xmpp.c \ tests/test_autocomplete.c \ tests/test_common.c \ tests/test_command.c \ From 7955bc52ab003b26d3ea439c34e7006e6e1f1593 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 18:11:15 +0000 Subject: [PATCH 31/33] Removed commented setion from Makefile.am --- Makefile.am | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/Makefile.am b/Makefile.am index 513364ea..6e95ae15 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,28 +40,6 @@ core_sources = \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h -#test_sources = \ -# src/contact.c src/contact.h src/log.c src/common.c \ -# src/log.h src/profanity.c src/common.h \ -# src/profanity.h src/chat_session.c \ -# src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ -# src/resource.c src/resource.h \ -# src/roster_list.c src/roster_list.h \ -# src/xmpp/xmpp.h tests/xmpp/mock_xmpp.c \ -# src/ui/ui.h tests/ui/mock_ui.c \ -# src/command/command.h src/command/command.c src/command/history.c \ -# src/command/history.h src/tools/parser.c \ -# src/tools/parser.h \ -# src/tools/autocomplete.c src/tools/autocomplete.h \ -# src/tools/history.c src/tools/history.h \ -# src/tools/tinyurl.c src/tools/tinyurl.h \ -# src/config/accounts.c src/config/accounts.h \ -# src/config/preferences.c src/config/preferences.h \ -# src/config/theme.c src/config/theme.h \ -# tests/test_roster_list.c tests/test_common.c tests/test_history.c \ -# tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \ -# tests/test_jid.c tests/test_command.c - test_sources = \ src/contact.c src/contact.h src/log.c src/common.c \ src/log.h src/profanity.c src/common.h \ From 447d235868e9e1554e432aad1e6a3b3db10e7b1d Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 14 Dec 2013 18:43:19 +0000 Subject: [PATCH 32/33] Mocked account preferences and tested cmd_rooms --- Makefile.am | 3 +- tests/config/mock_accounts.c | 112 +++++++++++++++++++++++++++++++++++ tests/test_command.c | 65 +++++++++++++++++++- tests/test_command.h | 8 ++- tests/testsuite.c | 10 +++- tests/ui/mock_ui.c | 2 +- tests/xmpp/mock_xmpp.c | 9 ++- 7 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 tests/config/mock_accounts.c diff --git a/Makefile.am b/Makefile.am index 6e95ae15..4ace2c3c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,11 +55,12 @@ test_sources = \ src/tools/autocomplete.c src/tools/autocomplete.h \ src/tools/history.c src/tools/history.h \ src/tools/tinyurl.c src/tools/tinyurl.h \ - src/config/accounts.c src/config/accounts.h \ + src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ tests/ui/mock_ui.c \ tests/xmpp/mock_xmpp.c \ + tests/config/mock_accounts.c \ tests/test_autocomplete.c \ tests/test_common.c \ tests/test_command.c \ diff --git a/tests/config/mock_accounts.c b/tests/config/mock_accounts.c new file mode 100644 index 00000000..e81aff9b --- /dev/null +++ b/tests/config/mock_accounts.c @@ -0,0 +1,112 @@ +/* + * mock_accounts.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include +#include +#include + +#include "config/accounts.h" + +void accounts_load(void) {} +void accounts_close(void) {} + +char * accounts_find_all(char *prefix) +{ + return (char *)mock(); +} + +char * accounts_find_enabled(char *prefix) +{ + return (char *)mock(); +} + +void accounts_reset_all_search(void) {} +void accounts_reset_enabled_search(void) {} +void accounts_add(const char *jid, const char *altdomain) {} + +gchar** accounts_get_list(void) +{ + return (gchar **)mock(); +} + +ProfAccount* accounts_get_account(const char * const name) +{ + return (ProfAccount *)mock(); +} + +void accounts_free_account(ProfAccount *account) {} + +gboolean accounts_enable(const char * const name) +{ + return (gboolean)mock(); +} + +gboolean accounts_disable(const char * const name) +{ + return (gboolean)mock(); +} + +gboolean accounts_rename(const char * const account_name, + const char * const new_name) +{ + return (gboolean)mock(); +} + +gboolean accounts_account_exists(const char * const account_name) +{ + return (gboolean)mock(); +} + +void accounts_set_jid(const char * const account_name, const char * const value) {} +void accounts_set_server(const char * const account_name, const char * const value) {} +void accounts_set_resource(const char * const account_name, const char * const value) {} +void accounts_set_password(const char * const account_name, const char * const value) {} +void accounts_set_muc_service(const char * const account_name, const char * const value) {} +void accounts_set_muc_nick(const char * const account_name, const char * const value) {} +void accounts_set_last_presence(const char * const account_name, const char * const value) {} +void accounts_set_login_presence(const char * const account_name, const char * const value) {} + +resource_presence_t accounts_get_login_presence(const char * const account_name) +{ + return (resource_presence_t)mock(); +} + +resource_presence_t accounts_get_last_presence(const char * const account_name) +{ + return (resource_presence_t)mock(); +} + +void accounts_set_priority_online(const char * const account_name, const gint value) {} +void accounts_set_priority_chat(const char * const account_name, const gint value) {} +void accounts_set_priority_away(const char * const account_name, const gint value) {} +void accounts_set_priority_xa(const char * const account_name, const gint value) {} +void accounts_set_priority_dnd(const char * const account_name, const gint value) {} +void accounts_set_priority_all(const char * const account_name, const gint value) {} + +gint accounts_get_priority_for_presence_type(const char * const account_name, + resource_presence_t presence_type) +{ + return (gint)mock(); +} + +void accounts_clear_password(const char * const account_name) {} + diff --git a/tests/test_command.c b/tests/test_command.c index d553f7e5..5469cd44 100644 --- a/tests/test_command.c +++ b/tests/test_command.c @@ -9,16 +9,75 @@ #include "ui/ui.h" #include "command/command.h" -void cmd_rooms_shows_message_when_not_connected(void **state) +static void test_with_connection_status(jabber_conn_status_t status) { CommandHelp *help = malloc(sizeof(CommandHelp)); - will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + will_return(jabber_get_connection_status, status); expect_string(cons_show, msg, "You are not currently connected."); gboolean result = _cmd_rooms(NULL, *help); - assert_true(result); free(help); } +void cmd_rooms_shows_message_when_disconnected(void **state) +{ + test_with_connection_status(JABBER_DISCONNECTED); +} + +void cmd_rooms_shows_message_when_disconnecting(void **state) +{ + test_with_connection_status(JABBER_DISCONNECTING); +} + +void cmd_rooms_shows_message_when_connecting(void **state) +{ + test_with_connection_status(JABBER_CONNECTING); +} + +void cmd_rooms_shows_message_when_started(void **state) +{ + test_with_connection_status(JABBER_STARTED); +} + +void cmd_rooms_shows_message_when_undefined(void **state) +{ + test_with_connection_status(JABBER_UNDEFINED); +} + +void cmd_rooms_uses_account_default_when_no_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + ProfAccount *account = malloc(sizeof(ProfAccount)); + account->muc_service = "default_conf_server"; + gchar *args[] = { NULL }; + + will_return(jabber_get_connection_status, JABBER_CONNECTED); + will_return(jabber_get_account_name, "account_name"); + will_return(accounts_get_account, account); + expect_string(iq_room_list_request, conferencejid, "default_conf_server"); + + gboolean result = _cmd_rooms(args, *help); + + assert_true(result); + + free(help); + free(account); +} + +void cmd_arg_used_when_passed(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "conf_server_arg" }; + + will_return(jabber_get_connection_status, JABBER_CONNECTED); + expect_string(iq_room_list_request, conferencejid, "conf_server_arg"); + + gboolean result = _cmd_rooms(args, *help); + + assert_true(result); + + free(help); +} diff --git a/tests/test_command.h b/tests/test_command.h index 589b589c..0f47be84 100644 --- a/tests/test_command.h +++ b/tests/test_command.h @@ -1 +1,7 @@ -void cmd_rooms_shows_message_when_not_connected(void **state); +void cmd_rooms_shows_message_when_disconnected(void **state); +void cmd_rooms_shows_message_when_disconnecting(void **state); +void cmd_rooms_shows_message_when_connecting(void **state); +void cmd_rooms_shows_message_when_started(void **state); +void cmd_rooms_shows_message_when_undefined(void **state); +void cmd_rooms_uses_account_default_when_no_arg(void **state); +void cmd_arg_used_when_passed(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index c67557d1..ee18fe6a 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -13,8 +13,14 @@ int main(int argc, char* argv[]) { const UnitTest tests[] = { - unit_test(cmd_rooms_shows_message_when_not_connected), - + unit_test(cmd_rooms_shows_message_when_disconnected), + unit_test(cmd_rooms_shows_message_when_disconnecting), + unit_test(cmd_rooms_shows_message_when_connecting), + unit_test(cmd_rooms_shows_message_when_started), + unit_test(cmd_rooms_shows_message_when_undefined), + unit_test(cmd_rooms_uses_account_default_when_no_arg), + unit_test(cmd_arg_used_when_passed), + unit_test(replace_one_substr), unit_test(replace_one_substr_beginning), unit_test(replace_one_substr_end), diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index bf9f2499..ed7dd4b1 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -1,5 +1,5 @@ /* - * ui.h + * mock_ui.h * * Copyright (C) 2012, 2013 James Booth * diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index 0c6bf917..8ba03cab 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -1,5 +1,5 @@ /* - * xmpp.h + * mock_xmpp.c * * Copyright (C) 2012, 2013 James Booth * @@ -116,7 +116,12 @@ gboolean presence_sub_request_exists(const char * const bare_jid) // iq functions void iq_send_software_version(const char * const fulljid) {} -void iq_room_list_request(gchar *conferencejid) {} + +void iq_room_list_request(gchar *conferencejid) +{ + check_expected(conferencejid); +} + void iq_disco_info_request(gchar *jid) {} void iq_disco_items_request(gchar *jid) {} From 2490f5b417a13639771211a7862bab88a6a5195e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 15 Dec 2013 16:10:32 +0000 Subject: [PATCH 33/33] Seperated command functions into module --- Makefile.am | 2 + src/command/command.c | 2539 +--------------------------------------- src/command/command.h | 11 +- src/command/commands.c | 2384 +++++++++++++++++++++++++++++++++++++ src/command/commands.h | 112 ++ src/profanity.c | 2 +- tests/test_command.c | 8 +- 7 files changed, 2566 insertions(+), 2492 deletions(-) create mode 100644 src/command/commands.c create mode 100644 src/command/commands.h diff --git a/Makefile.am b/Makefile.am index 4ace2c3c..8531c302 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,6 +31,7 @@ core_sources = \ src/ui/windows.c src/ui/windows.h \ src/ui/muc_window.c src/ui/muc_window.h \ src/command/command.h src/command/command.c src/command/history.c \ + src/command/commands.h src/command/commands.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ src/tools/autocomplete.c src/tools/autocomplete.h \ @@ -50,6 +51,7 @@ test_sources = \ src/xmpp/xmpp.h \ src/ui/ui.h \ src/command/command.h src/command/command.c src/command/history.c \ + src/command/commands.h src/command/commands.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ src/tools/autocomplete.c src/tools/autocomplete.h \ diff --git a/src/command/command.c b/src/command/command.c index 0ab8b013..a13687a3 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -30,6 +30,7 @@ #include "chat_session.h" #include "command/command.h" +#include "command/commands.h" #include "command/history.h" #include "common.h" #include "config/accounts.h" @@ -48,34 +49,8 @@ #include "xmpp/xmpp.h" #include "xmpp/bookmark.h" -/* - * Command structure - * - * cmd - The command string including leading '/' - * func - The function to execute for the command - * parser - The function used to parse arguments - * min_args - Minimum number of arguments - * max_args - Maximum number of arguments - * help - A help struct containing usage info etc - */ -typedef struct cmd_t { - gchar *cmd; - gboolean (*func)(gchar **args, struct cmd_help_t help); - gchar** (*parser)(const char * const inp, int min, int max); - int min_args; - int max_args; - void (*setting_func)(void); - CommandHelp help; -} Command; - typedef char*(*autocompleter)(char*, int*); -static char * _ask_password(void); -static void _update_presence(const resource_presence_t presence, - const char * const show, gchar **args); -static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, - const char * const display, preference_t pref); - static void _cmd_complete_parameters(char *input, int *size); static char * _sub_autocomplete(char *input, int *size); @@ -90,68 +65,7 @@ static char * _roster_autocomplete(char *input, int *size); static char * _group_autocomplete(char *input, int *size); static char * _bookmark_autocomplete(char *input, int *size); -static int _strtoi(char *str, int *saveptr, int min, int max); - -// command prototypes -static gboolean _cmd_about(gchar **args, struct cmd_help_t help); -static gboolean _cmd_account(gchar **args, struct cmd_help_t help); -static gboolean _cmd_autoaway(gchar **args, struct cmd_help_t help); -static gboolean _cmd_autoconnect(gchar **args, struct cmd_help_t help); -static gboolean _cmd_autoping(gchar **args, struct cmd_help_t help); -static gboolean _cmd_away(gchar **args, struct cmd_help_t help); -static gboolean _cmd_beep(gchar **args, struct cmd_help_t help); -static gboolean _cmd_caps(gchar **args, struct cmd_help_t help); -static gboolean _cmd_chat(gchar **args, struct cmd_help_t help); -static gboolean _cmd_chlog(gchar **args, struct cmd_help_t help); -static gboolean _cmd_clear(gchar **args, struct cmd_help_t help); -static gboolean _cmd_close(gchar **args, struct cmd_help_t help); -static gboolean _cmd_connect(gchar **args, struct cmd_help_t help); -static gboolean _cmd_decline(gchar **args, struct cmd_help_t help); -static gboolean _cmd_disco(gchar **args, struct cmd_help_t help); -static gboolean _cmd_disconnect(gchar **args, struct cmd_help_t help); -static gboolean _cmd_dnd(gchar **args, struct cmd_help_t help); -static gboolean _cmd_duck(gchar **args, struct cmd_help_t help); -static gboolean _cmd_flash(gchar **args, struct cmd_help_t help); -static gboolean _cmd_gone(gchar **args, struct cmd_help_t help); -static gboolean _cmd_grlog(gchar **args, struct cmd_help_t help); -static gboolean _cmd_group(gchar **args, struct cmd_help_t help); -static gboolean _cmd_help(gchar **args, struct cmd_help_t help); -static gboolean _cmd_history(gchar **args, struct cmd_help_t help); -static gboolean _cmd_info(gchar **args, struct cmd_help_t help); -static gboolean _cmd_intype(gchar **args, struct cmd_help_t help); -static gboolean _cmd_invite(gchar **args, struct cmd_help_t help); -static gboolean _cmd_invites(gchar **args, struct cmd_help_t help); -static gboolean _cmd_join(gchar **args, struct cmd_help_t help); -static gboolean _cmd_leave(gchar **args, struct cmd_help_t help); -static gboolean _cmd_log(gchar **args, struct cmd_help_t help); -static gboolean _cmd_mouse(gchar **args, struct cmd_help_t help); -static gboolean _cmd_msg(gchar **args, struct cmd_help_t help); -static gboolean _cmd_nick(gchar **args, struct cmd_help_t help); -static gboolean _cmd_notify(gchar **args, struct cmd_help_t help); -static gboolean _cmd_online(gchar **args, struct cmd_help_t help); -static gboolean _cmd_outtype(gchar **args, struct cmd_help_t help); -static gboolean _cmd_prefs(gchar **args, struct cmd_help_t help); -static gboolean _cmd_priority(gchar **args, struct cmd_help_t help); -static gboolean _cmd_quit(gchar **args, struct cmd_help_t help); -static gboolean _cmd_reconnect(gchar **args, struct cmd_help_t help); -static gboolean _cmd_bookmark(gchar **args, struct cmd_help_t help); -static gboolean _cmd_roster(gchar **args, struct cmd_help_t help); -static gboolean _cmd_software(gchar **args, struct cmd_help_t help); -static gboolean _cmd_splash(gchar **args, struct cmd_help_t help); -static gboolean _cmd_states(gchar **args, struct cmd_help_t help); -static gboolean _cmd_status(gchar **args, struct cmd_help_t help); -static gboolean _cmd_statuses(gchar **args, struct cmd_help_t help); -static gboolean _cmd_sub(gchar **args, struct cmd_help_t help); -static gboolean _cmd_theme(gchar **args, struct cmd_help_t help); -static gboolean _cmd_tiny(gchar **args, struct cmd_help_t help); -static gboolean _cmd_titlebar(gchar **args, struct cmd_help_t help); -static gboolean _cmd_vercheck(gchar **args, struct cmd_help_t help); -static gboolean _cmd_who(gchar **args, struct cmd_help_t help); -static gboolean _cmd_win(gchar **args, struct cmd_help_t help); -static gboolean _cmd_wins(gchar **args, struct cmd_help_t help); -static gboolean _cmd_xa(gchar **args, struct cmd_help_t help); - -static GHashTable *commands = NULL; +GHashTable *commands = NULL; /* * Command list @@ -159,7 +73,7 @@ static GHashTable *commands = NULL; static struct cmd_t command_defs[] = { { "/help", - _cmd_help, parse_args, 0, 1, NULL, + cmd_help, parse_args, 0, 1, NULL, { "/help [area|command]", "Get help on using Profanity", { "/help [area|command]", "-------------------------", @@ -175,7 +89,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/about", - _cmd_about, parse_args, 0, 0, NULL, + cmd_about, parse_args, 0, 0, NULL, { "/about", "About Profanity", { "/about", "------", @@ -183,7 +97,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/connect", - _cmd_connect, parse_args, 1, 2, NULL, + cmd_connect, parse_args, 1, 2, NULL, { "/connect account [server]", "Login to a chat service.", { "/connect account [server]", "-------------------------", @@ -196,7 +110,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/disconnect", - _cmd_disconnect, parse_args, 0, 0, NULL, + cmd_disconnect, parse_args, 0, 0, NULL, { "/disconnect", "Logout of current session.", { "/disconnect", "-----------", @@ -204,7 +118,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/msg", - _cmd_msg, parse_args_with_freetext, 1, 2, NULL, + cmd_msg, parse_args_with_freetext, 1, 2, NULL, { "/msg contact|nick [message]", "Start chat with user.", { "/msg contact|nick [message]", "---------------------------", @@ -219,7 +133,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/roster", - _cmd_roster, parse_args_with_freetext, 0, 3, NULL, + cmd_roster, parse_args_with_freetext, 0, 3, NULL, { "/roster [add|remove|nick] [jid] [handle]", "Manage your roster.", { "/roster [add|remove|nick] [jid] [handle]", "----------------------------------------", @@ -239,7 +153,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/group", - _cmd_group, parse_args_with_freetext, 0, 3, NULL, + cmd_group, parse_args_with_freetext, 0, 3, NULL, { "/group [show|add|remove] [group] [contact]", "Manage roster groups.", { "/group [show|add|remove] [group] [contact]", "------------------------------------------", @@ -257,7 +171,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/info", - _cmd_info, parse_args, 0, 1, NULL, + cmd_info, parse_args, 0, 1, NULL, { "/info [contact|nick]", "Show basic information about a contact, or room member.", { "/info [contact|nick]", "--------------------", @@ -269,7 +183,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/caps", - _cmd_caps, parse_args, 0, 1, NULL, + cmd_caps, parse_args, 0, 1, NULL, { "/caps [fulljid|nick]", "Find out a contacts client software capabilities.", { "/caps [fulljid|nick]", "--------------------", @@ -284,7 +198,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/software", - _cmd_software, parse_args, 0, 1, NULL, + cmd_software, parse_args, 0, 1, NULL, { "/software [fulljid|nick]", "Find out software version information about a contacts resource.", { "/software [fulljid|nick]", "------------------------", @@ -300,7 +214,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/status", - _cmd_status, parse_args, 0, 1, NULL, + cmd_status, parse_args, 0, 1, NULL, { "/status [contact|nick]", "Find out a contacts presence information.", { "/status [contact|nick]", "----------------------", @@ -312,7 +226,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/join", - _cmd_join, parse_args_with_freetext, 1, 2, NULL, + cmd_join, parse_args_with_freetext, 1, 2, NULL, { "/join room[@server] [nick]", "Join a chat room.", { "/join room[@server] [nick]", "--------------------------", @@ -328,7 +242,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/leave", - _cmd_leave, parse_args, 0, 0, NULL, + cmd_leave, parse_args, 0, 0, NULL, { "/leave", "Leave a chat room.", { "/leave", "------", @@ -336,7 +250,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/invite", - _cmd_invite, parse_args_with_freetext, 1, 2, NULL, + cmd_invite, parse_args_with_freetext, 1, 2, NULL, { "/invite contact [message]", "Invite contact to chat room.", { "/invite contact [message]", "-------------------------", @@ -345,7 +259,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/invites", - _cmd_invites, parse_args_with_freetext, 0, 0, NULL, + cmd_invites, parse_args_with_freetext, 0, 0, NULL, { "/invites", "Show outstanding chat room invites.", { "/invites", "--------", @@ -355,7 +269,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/decline", - _cmd_decline, parse_args_with_freetext, 1, 1, NULL, + cmd_decline, parse_args_with_freetext, 1, 1, NULL, { "/decline room", "Decline a chat room invite.", { "/decline room", "-------------", @@ -363,7 +277,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/rooms", - _cmd_rooms, parse_args, 0, 1, NULL, + cmd_rooms, parse_args, 0, 1, NULL, { "/rooms [conference-service]", "List chat rooms.", { "/rooms [conference-service]", "---------------------------", @@ -375,7 +289,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/bookmark", - _cmd_bookmark, parse_args, 0, 4, NULL, + cmd_bookmark, parse_args, 0, 4, NULL, { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", "Manage bookmarks.", { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", @@ -384,7 +298,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/disco", - _cmd_disco, parse_args, 1, 2, NULL, + cmd_disco, parse_args, 1, 2, NULL, { "/disco command entity", "Service discovery.", { "/disco command entity", "---------------------", @@ -402,7 +316,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/nick", - _cmd_nick, parse_args_with_freetext, 1, 1, NULL, + cmd_nick, parse_args_with_freetext, 1, 1, NULL, { "/nick nickname", "Change nickname in chat room.", { "/nick nickname", "--------------", @@ -414,7 +328,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/win", - _cmd_win, parse_args, 1, 1, NULL, + cmd_win, parse_args, 1, 1, NULL, { "/win num", "View a window.", { "/win num", "------------------", @@ -422,7 +336,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/wins", - _cmd_wins, parse_args, 0, 1, NULL, + cmd_wins, parse_args, 0, 1, NULL, { "/wins [tidy|prune]", "List or tidy active windows.", { "/wins [tidy|prune]", "------------------", @@ -432,7 +346,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/sub", - _cmd_sub, parse_args, 1, 2, NULL, + cmd_sub, parse_args, 1, 2, NULL, { "/sub command [jid]", "Manage subscriptions.", { "/sub command [jid]", "------------------", @@ -455,7 +369,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/tiny", - _cmd_tiny, parse_args, 1, 1, NULL, + cmd_tiny, parse_args, 1, 1, NULL, { "/tiny url", "Send url as tinyurl in current chat.", { "/tiny url", "---------", @@ -465,7 +379,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/duck", - _cmd_duck, parse_args_with_freetext, 1, 1, NULL, + cmd_duck, parse_args_with_freetext, 1, 1, NULL, { "/duck query", "Perform search using DuckDuckGo chatbot.", { "/duck query", "-----------", @@ -476,7 +390,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/who", - _cmd_who, parse_args, 0, 2, NULL, + cmd_who, parse_args, 0, 2, NULL, { "/who [status] [group]", "Show contacts/room participants with chosen status.", { "/who [status] [group]", "---------------------", @@ -492,7 +406,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/close", - _cmd_close, parse_args, 0, 1, NULL, + cmd_close, parse_args, 0, 1, NULL, { "/close [win|read|all]", "Close windows.", { "/close [win|read|all]", "---------------------", @@ -505,7 +419,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/clear", - _cmd_clear, parse_args, 0, 0, NULL, + cmd_clear, parse_args, 0, 0, NULL, { "/clear", "Clear current window.", { "/clear", "------", @@ -513,7 +427,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/quit", - _cmd_quit, parse_args, 0, 0, NULL, + cmd_quit, parse_args, 0, 0, NULL, { "/quit", "Quit Profanity.", { "/quit", "-----", @@ -521,7 +435,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/beep", - _cmd_beep, parse_args, 1, 1, cons_beep_setting, + cmd_beep, parse_args, 1, 1, cons_beep_setting, { "/beep on|off", "Terminal beep on new messages.", { "/beep on|off", "------------", @@ -531,7 +445,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/notify", - _cmd_notify, parse_args, 2, 2, cons_notify_setting, + cmd_notify, parse_args, 2, 2, cons_notify_setting, { "/notify type value", "Control various desktop noficiations.", { "/notify type value", "------------------", @@ -556,7 +470,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/flash", - _cmd_flash, parse_args, 1, 1, cons_flash_setting, + cmd_flash, parse_args, 1, 1, cons_flash_setting, { "/flash on|off", "Terminal flash on new messages.", { "/flash on|off", "-------------", @@ -566,7 +480,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/intype", - _cmd_intype, parse_args, 1, 1, cons_intype_setting, + cmd_intype, parse_args, 1, 1, cons_intype_setting, { "/intype on|off", "Show when contact is typing.", { "/intype on|off", "--------------", @@ -574,7 +488,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/splash", - _cmd_splash, parse_args, 1, 1, cons_splash_setting, + cmd_splash, parse_args, 1, 1, cons_splash_setting, { "/splash on|off", "Splash logo on startup and /about command.", { "/splash on|off", "--------------", @@ -582,7 +496,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/autoconnect", - _cmd_autoconnect, parse_args, 1, 2, cons_autoconnect_setting, + cmd_autoconnect, parse_args, 1, 2, cons_autoconnect_setting, { "/autoconnect set|off [account]", "Set account to autoconnect with.", { "/autoconnect set|off [account]", "------------------------------", @@ -594,7 +508,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/vercheck", - _cmd_vercheck, parse_args, 0, 1, NULL, + cmd_vercheck, parse_args, 0, 1, NULL, { "/vercheck [on|off]", "Check for a new release.", { "/vercheck [on|off]", "------------------", @@ -603,7 +517,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/titlebar", - _cmd_titlebar, parse_args, 2, 2, cons_titlebar_setting, + cmd_titlebar, parse_args, 2, 2, cons_titlebar_setting, { "/titlebar property on|off", "Show various properties in the window title bar.", { "/titlebar property on|off", "-------------------------", @@ -612,7 +526,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/mouse", - _cmd_mouse, parse_args, 1, 1, cons_mouse_setting, + cmd_mouse, parse_args, 1, 1, cons_mouse_setting, { "/mouse on|off", "Use profanity mouse handling.", { "/mouse on|off", "-------------", @@ -625,7 +539,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/chlog", - _cmd_chlog, parse_args, 1, 1, cons_chlog_setting, + cmd_chlog, parse_args, 1, 1, cons_chlog_setting, { "/chlog on|off", "Chat logging to file", { "/chlog on|off", "-------------", @@ -636,7 +550,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/grlog", - _cmd_grlog, parse_args, 1, 1, cons_grlog_setting, + cmd_grlog, parse_args, 1, 1, cons_grlog_setting, { "/grlog on|off", "Chat logging of chat rooms to file", { "/grlog on|off", "-------------", @@ -645,7 +559,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/states", - _cmd_states, parse_args, 1, 1, cons_states_setting, + cmd_states, parse_args, 1, 1, cons_states_setting, { "/states on|off", "Send chat states during a chat session.", { "/states on|off", "--------------", @@ -654,7 +568,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/outtype", - _cmd_outtype, parse_args, 1, 1, cons_outtype_setting, + cmd_outtype, parse_args, 1, 1, cons_outtype_setting, { "/outtype on|off", "Send typing notification to recipient.", { "/outtype on|off", "---------------", @@ -663,7 +577,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/gone", - _cmd_gone, parse_args, 1, 1, cons_gone_setting, + cmd_gone, parse_args, 1, 1, cons_gone_setting, { "/gone minutes", "Send 'gone' state to recipient after a period.", { "/gone minutes", "-------------", @@ -674,7 +588,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/history", - _cmd_history, parse_args, 1, 1, cons_history_setting, + cmd_history, parse_args, 1, 1, cons_history_setting, { "/history on|off", "Chat history in message windows.", { "/history on|off", "---------------", @@ -683,7 +597,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/log", - _cmd_log, parse_args, 2, 2, cons_log_setting, + cmd_log, parse_args, 2, 2, cons_log_setting, { "/log maxsize value", "Manage system logging settings.", { "/log maxsize value", "------------------", @@ -692,7 +606,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/reconnect", - _cmd_reconnect, parse_args, 1, 1, cons_reconnect_setting, + cmd_reconnect, parse_args, 1, 1, cons_reconnect_setting, { "/reconnect seconds", "Set reconnect interval.", { "/reconnect seconds", "------------------", @@ -701,7 +615,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/autoping", - _cmd_autoping, parse_args, 1, 1, cons_autoping_setting, + cmd_autoping, parse_args, 1, 1, cons_autoping_setting, { "/autoping seconds", "Server ping interval.", { "/autoping seconds", "-----------------", @@ -710,7 +624,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/autoaway", - _cmd_autoaway, parse_args_with_freetext, 2, 2, cons_autoaway_setting, + cmd_autoaway, parse_args_with_freetext, 2, 2, cons_autoaway_setting, { "/autoaway setting value", "Set auto idle/away properties.", { "/autoaway setting value", "-----------------------", @@ -731,7 +645,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/priority", - _cmd_priority, parse_args, 1, 1, cons_priority_setting, + cmd_priority, parse_args, 1, 1, cons_priority_setting, { "/priority value", "Set priority for the current account.", { "/priority value", "---------------", @@ -741,7 +655,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/account", - _cmd_account, parse_args, 0, 4, NULL, + cmd_account, parse_args, 0, 4, NULL, { "/account [command] [account] [property] [value]", "Manage accounts.", { "/account [command] [account] [property] [value]", "-----------------------------------------------", @@ -784,7 +698,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/prefs", - _cmd_prefs, parse_args, 0, 1, NULL, + cmd_prefs, parse_args, 0, 1, NULL, { "/prefs [area]", "Show configuration.", { "/prefs [area]", "-------------", @@ -800,7 +714,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/theme", - _cmd_theme, parse_args, 1, 2, cons_theme_setting, + cmd_theme, parse_args, 1, 2, cons_theme_setting, { "/theme command [theme-name]", "Change colour theme.", { "/theme command [theme-name]", "---------------------------", @@ -816,7 +730,7 @@ static struct cmd_t command_defs[] = { "/statuses", - _cmd_statuses, parse_args, 1, 1, cons_statuses_setting, + cmd_statuses, parse_args, 1, 1, cons_statuses_setting, { "/statuses on|off", "Set notifications for status messages.", { "/statuses on|off", "----------------", @@ -826,7 +740,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/away", - _cmd_away, parse_args_with_freetext, 0, 1, NULL, + cmd_away, parse_args_with_freetext, 0, 1, NULL, { "/away [msg]", "Set status to away.", { "/away [msg]", "-----------", @@ -837,7 +751,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/chat", - _cmd_chat, parse_args_with_freetext, 0, 1, NULL, + cmd_chat, parse_args_with_freetext, 0, 1, NULL, { "/chat [msg]", "Set status to chat (available for chat).", { "/chat [msg]", "-----------", @@ -848,7 +762,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/dnd", - _cmd_dnd, parse_args_with_freetext, 0, 1, NULL, + cmd_dnd, parse_args_with_freetext, 0, 1, NULL, { "/dnd [msg]", "Set status to dnd (do not disturb).", { "/dnd [msg]", "----------", @@ -859,7 +773,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/online", - _cmd_online, parse_args_with_freetext, 0, 1, NULL, + cmd_online, parse_args_with_freetext, 0, 1, NULL, { "/online [msg]", "Set status to online.", { "/online [msg]", "-------------", @@ -870,7 +784,7 @@ static struct cmd_t command_defs[] = NULL } } }, { "/xa", - _cmd_xa, parse_args_with_freetext, 0, 1, NULL, + cmd_xa, parse_args_with_freetext, 0, 1, NULL, { "/xa [msg]", "Set status to xa (extended away).", { "/xa [msg]", "---------", @@ -1059,7 +973,7 @@ cmd_init(void) } void -cmd_close(void) +cmd_uninit(void) { autocomplete_free(commands_ac); autocomplete_free(who_ac); @@ -1382,2317 +1296,6 @@ _cmd_complete_parameters(char *input, int *size) return; } -// The command functions - -static gboolean -_cmd_connect(gchar **args, struct cmd_help_t help) -{ - gboolean result = FALSE; - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { - cons_show("You are either connected already, or a login is in process."); - result = TRUE; - } else { - char *user = args[0]; - char *altdomain = args[1]; - char *lower = g_utf8_strdown(user, -1); - char *jid; - - ProfAccount *account = accounts_get_account(lower); - if (account != NULL) { - if (account->resource != NULL) { - jid = create_fulljid(account->jid, account->resource); - } else { - jid = strdup(account->jid); - } - - if (account->password == NULL) { - account->password = _ask_password(); - } - cons_show("Connecting with account %s as %s", account->name, jid); - conn_status = jabber_connect_with_account(account); - } else { - char *passwd = _ask_password(); - jid = strdup(lower); - cons_show("Connecting as %s", jid); - conn_status = jabber_connect_with_details(jid, passwd, altdomain); - free(passwd); - } - g_free(lower); - - if (conn_status == JABBER_DISCONNECTED) { - cons_show_error("Connection attempt for %s failed.", jid); - log_debug("Connection attempt for %s failed", jid); - } - - accounts_free_account(account); - free(jid); - - result = TRUE; - } - - return result; -} - -static gboolean -_cmd_account(gchar **args, struct cmd_help_t help) -{ - char *command = args[0]; - - if (command == NULL) { - if (jabber_get_connection_status() != JABBER_CONNECTED) { - cons_show("Usage: %s", help.usage); - } else { - ProfAccount *account = accounts_get_account(jabber_get_account_name()); - cons_show_account(account); - accounts_free_account(account); - } - } else if (strcmp(command, "list") == 0) { - gchar **accounts = accounts_get_list(); - cons_show_account_list(accounts); - g_strfreev(accounts); - } else if (strcmp(command, "show") == 0) { - char *account_name = args[1]; - if (account_name == NULL) { - cons_show("Usage: %s", help.usage); - } else { - ProfAccount *account = accounts_get_account(account_name); - if (account == NULL) { - cons_show("No such account."); - cons_show(""); - } else { - cons_show_account(account); - accounts_free_account(account); - } - } - } else if (strcmp(command, "add") == 0) { - char *account_name = args[1]; - if (account_name == NULL) { - cons_show("Usage: %s", help.usage); - } else { - accounts_add(account_name, NULL); - cons_show("Account created."); - cons_show(""); - } - } else if (strcmp(command, "enable") == 0) { - char *account_name = args[1]; - if (account_name == NULL) { - cons_show("Usage: %s", help.usage); - } else { - if (accounts_enable(account_name)) { - cons_show("Account enabled."); - cons_show(""); - } else { - cons_show("No such account: %s", account_name); - cons_show(""); - } - } - } else if (strcmp(command, "disable") == 0) { - char *account_name = args[1]; - if (account_name == NULL) { - cons_show("Usage: %s", help.usage); - } else { - if (accounts_disable(account_name)) { - cons_show("Account disabled."); - cons_show(""); - } else { - cons_show("No such account: %s", account_name); - cons_show(""); - } - } - } else if (strcmp(command, "rename") == 0) { - if (g_strv_length(args) != 3) { - cons_show("Usage: %s", help.usage); - } else { - char *account_name = args[1]; - char *new_name = args[2]; - - if (accounts_rename(account_name, new_name)) { - cons_show("Account renamed."); - cons_show(""); - } else { - cons_show("Either account %s doesn't exist, or account %s already exists.", account_name, new_name); - cons_show(""); - } - } - } else if (strcmp(command, "set") == 0) { - if (g_strv_length(args) != 4) { - cons_show("Usage: %s", help.usage); - } else { - char *account_name = args[1]; - char *property = args[2]; - char *value = args[3]; - - if (!accounts_account_exists(account_name)) { - cons_show("Account %s doesn't exist", account_name); - cons_show(""); - } else { - if (strcmp(property, "jid") == 0) { - Jid *jid = jid_create(args[3]); - if (jid == NULL) { - cons_show("Malformed jid: %s", value); - } else { - accounts_set_jid(account_name, jid->barejid); - cons_show("Updated jid for account %s: %s", account_name, jid->barejid); - if (jid->resourcepart != NULL) { - accounts_set_resource(account_name, jid->resourcepart); - cons_show("Updated resource for account %s: %s", account_name, jid->resourcepart); - } - cons_show(""); - } - jid_destroy(jid); - } else if (strcmp(property, "server") == 0) { - accounts_set_server(account_name, value); - cons_show("Updated server for account %s: %s", account_name, value); - cons_show(""); - } else if (strcmp(property, "resource") == 0) { - accounts_set_resource(account_name, value); - cons_show("Updated resource for account %s: %s", account_name, value); - cons_show(""); - } else if (strcmp(property, "password") == 0) { - accounts_set_password(account_name, value); - cons_show("Updated password for account %s", account_name); - cons_show(""); - } else if (strcmp(property, "muc") == 0) { - accounts_set_muc_service(account_name, value); - cons_show("Updated muc service for account %s: %s", account_name, value); - cons_show(""); - } else if (strcmp(property, "nick") == 0) { - accounts_set_muc_nick(account_name, value); - cons_show("Updated muc nick for account %s: %s", account_name, value); - cons_show(""); - } else if (strcmp(property, "status") == 0) { - if (!valid_resource_presence_string(value) && (strcmp(value, "last") != 0)) { - cons_show("Invalid status: %s", value); - } else { - accounts_set_login_presence(account_name, value); - cons_show("Updated login status for account %s: %s", account_name, value); - } - cons_show(""); - } else if (valid_resource_presence_string(property)) { - int intval; - - if (_strtoi(value, &intval, -128, 127) == 0) { - resource_presence_t presence_type = resource_presence_from_string(property); - switch (presence_type) - { - case (RESOURCE_ONLINE): - accounts_set_priority_online(account_name, intval); - break; - case (RESOURCE_CHAT): - accounts_set_priority_chat(account_name, intval); - break; - case (RESOURCE_AWAY): - accounts_set_priority_away(account_name, intval); - break; - case (RESOURCE_XA): - accounts_set_priority_xa(account_name, intval); - break; - case (RESOURCE_DND): - accounts_set_priority_dnd(account_name, intval); - break; - } - jabber_conn_status_t conn_status = jabber_get_connection_status(); - resource_presence_t last_presence = accounts_get_last_presence(jabber_get_account_name()); - if (conn_status == JABBER_CONNECTED && presence_type == last_presence) { - presence_update(last_presence, jabber_get_presence_message(), 0); - } - cons_show("Updated %s priority for account %s: %s", property, account_name, value); - cons_show(""); - } - } else { - cons_show("Invalid property: %s", property); - cons_show(""); - } - } - } - } else if (strcmp(command, "clear") == 0) { - if (g_strv_length(args) != 3) { - cons_show("Usage: %s", help.usage); - } else { - char *account_name = args[1]; - char *property = args[2]; - - if (!accounts_account_exists(account_name)) { - cons_show("Account %s doesn't exist", account_name); - cons_show(""); - } else { - if (strcmp(property, "password") == 0) { - accounts_clear_password(account_name); - cons_show("Removed password for account %s", account_name); - cons_show(""); - } else { - cons_show("Invalid property: %s", property); - cons_show(""); - } - } - } - } else { - cons_show(""); - } - - return TRUE; -} - -static gboolean -_cmd_sub(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are currently not connected."); - return TRUE; - } - - char *subcmd, *jid; - subcmd = args[0]; - jid = args[1]; - - if (subcmd == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - if (strcmp(subcmd, "sent") == 0) { - cons_show_sent_subs(); - return TRUE; - } - - if (strcmp(subcmd, "received") == 0) { - cons_show_received_subs(); - return TRUE; - } - - if ((win_type != WIN_CHAT) && (jid == NULL)) { - cons_show("You must specify a contact."); - return TRUE; - } - - if (jid == NULL) { - jid = ui_current_recipient(); - } - - Jid *jidp = jid_create(jid); - - if (strcmp(subcmd, "allow") == 0) { - presence_subscription(jidp->barejid, PRESENCE_SUBSCRIBED); - cons_show("Accepted subscription for %s", jidp->barejid); - log_info("Accepted subscription for %s", jidp->barejid); - } else if (strcmp(subcmd, "deny") == 0) { - presence_subscription(jidp->barejid, PRESENCE_UNSUBSCRIBED); - cons_show("Deleted/denied subscription for %s", jidp->barejid); - log_info("Deleted/denied subscription for %s", jidp->barejid); - } else if (strcmp(subcmd, "request") == 0) { - presence_subscription(jidp->barejid, PRESENCE_SUBSCRIBE); - cons_show("Sent subscription request to %s.", jidp->barejid); - log_info("Sent subscription request to %s.", jidp->barejid); - } else if (strcmp(subcmd, "show") == 0) { - PContact contact = roster_get_contact(jidp->barejid); - if ((contact == NULL) || (p_contact_subscription(contact) == NULL)) { - if (win_type == WIN_CHAT) { - ui_current_print_line("No subscription information for %s.", jidp->barejid); - } else { - cons_show("No subscription information for %s.", jidp->barejid); - } - } else { - if (win_type == WIN_CHAT) { - if (p_contact_pending_out(contact)) { - ui_current_print_line("%s subscription status: %s, request pending.", - jidp->barejid, p_contact_subscription(contact)); - } else { - ui_current_print_line("%s subscription status: %s.", jidp->barejid, - p_contact_subscription(contact)); - } - } else { - if (p_contact_pending_out(contact)) { - cons_show("%s subscription status: %s, request pending.", - jidp->barejid, p_contact_subscription(contact)); - } else { - cons_show("%s subscription status: %s.", jidp->barejid, - p_contact_subscription(contact)); - } - } - } - } else { - cons_show("Usage: %s", help.usage); - } - - jid_destroy(jidp); - - return TRUE; -} - -static gboolean -_cmd_disconnect(gchar **args, struct cmd_help_t help) -{ - if (jabber_get_connection_status() == JABBER_CONNECTED) { - char *jid = strdup(jabber_get_fulljid()); - prof_handle_disconnect(jid); - free(jid); - } else { - cons_show("You are not currently connected."); - } - - return TRUE; -} - -static gboolean -_cmd_quit(gchar **args, struct cmd_help_t help) -{ - log_info("Profanity is shutting down..."); - exit(0); - return FALSE; -} - -static gboolean -_cmd_wins(gchar **args, struct cmd_help_t help) -{ - if (args[0] == NULL) { - cons_show_wins(); - } else if (strcmp(args[0], "tidy") == 0) { - ui_tidy_wins(); - } else if (strcmp(args[0], "prune") == 0) { - ui_prune_wins(); - } - return TRUE; -} - -static gboolean -_cmd_win(gchar **args, struct cmd_help_t help) -{ - int num = atoi(args[0]); - if (ui_win_exists(num)) { - ui_switch_win(num); - } else { - cons_show("Window %d does not exist.", num); - } - - return TRUE; -} - -static -gint _compare_commands(Command *a, Command *b) -{ - const char * utf8_str_a = a->cmd; - const char * utf8_str_b = b->cmd; - - gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); - gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); - - gint result = g_strcmp0(key_a, key_b); - - g_free(key_a); - g_free(key_b); - - return result; -} - -static void -_cmd_show_filtered_help(char *heading, gchar *cmd_filter[], int filter_size) -{ - cons_show(""); - cons_show("%s", heading); - cons_show(""); - - GList *ordered_commands = NULL; - int i; - for (i = 0; i < filter_size; i++) { - Command *cmd = g_hash_table_lookup(commands, cmd_filter[i]); - ordered_commands = g_list_insert_sorted(ordered_commands, cmd, (GCompareFunc)_compare_commands); - } - - GList *curr = ordered_commands; - while (curr != NULL) { - Command *cmd = curr->data; - cons_show("%-12s: %s", cmd->cmd, cmd->help.short_help); - curr = g_list_next(curr); - } - g_list_free(ordered_commands); - g_list_free(curr); - - cons_show(""); - cons_show("Use /help [command] without the leading slash, for help on a specific command"); - cons_show(""); -} - -static gboolean -_cmd_help(gchar **args, struct cmd_help_t help) -{ - int num_args = g_strv_length(args); - if (num_args == 0) { - cons_help(); - } else if (strcmp(args[0], "commands") == 0) { - cons_show(""); - cons_show("All commands"); - cons_show(""); - - GList *ordered_commands = NULL; - GHashTableIter iter; - gpointer key; - gpointer value; - - g_hash_table_iter_init(&iter, commands); - while (g_hash_table_iter_next(&iter, &key, &value)) { - ordered_commands = g_list_insert_sorted(ordered_commands, value, (GCompareFunc)_compare_commands); - } - - GList *curr = ordered_commands; - while (curr != NULL) { - Command *cmd = curr->data; - cons_show("%-12s: %s", cmd->cmd, cmd->help.short_help); - curr = g_list_next(curr); - } - g_list_free(ordered_commands); - g_list_free(curr); - - cons_show(""); - cons_show("Use /help [command] without the leading slash, for help on a specific command"); - cons_show(""); - - } else if (strcmp(args[0], "basic") == 0) { - gchar *filter[] = { "/about", "/clear", "/close", "/connect", - "/disconnect", "/help", "/msg", "/join", "/quit", "/vercheck", - "/wins" }; - _cmd_show_filtered_help("Basic commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "chatting") == 0) { - gchar *filter[] = { "/chlog", "/duck", "/gone", "/history", - "/info", "/intype", "/msg", "/notify", "/outtype", "/status", - "/close", "/clear", "/tiny" }; - _cmd_show_filtered_help("Chat commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "groupchat") == 0) { - gchar *filter[] = { "/close", "/clear", "/decline", "/grlog", - "/invite", "/invites", "/join", "/leave", "/notify", "/msg", - "/rooms", "/tiny", "/who", "/nick" }; - _cmd_show_filtered_help("Groupchat commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "presence") == 0) { - gchar *filter[] = { "/autoaway", "/away", "/chat", "/dnd", - "/online", "/priority", "/account", "/status", "/statuses", "/who", - "/xa" }; - _cmd_show_filtered_help("Presence commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "contacts") == 0) { - gchar *filter[] = { "/group", "/roster", "/sub", "/who" }; - _cmd_show_filtered_help("Roster commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "service") == 0) { - gchar *filter[] = { "/caps", "/disco", "/info", "/software", "/rooms" }; - _cmd_show_filtered_help("Service discovery commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "settings") == 0) { - gchar *filter[] = { "/account", "/autoaway", "/autoping", "/autoconnect", "/beep", - "/chlog", "/flash", "/gone", "/grlog", "/history", "/intype", - "/log", "/mouse", "/notify", "/outtype", "/prefs", "/priority", - "/reconnect", "/roster", "/splash", "/states", "/statuses", "/theme", - "/titlebar", "/vercheck" }; - _cmd_show_filtered_help("Settings commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "other") == 0) { - gchar *filter[] = { "/duck", "/vercheck" }; - _cmd_show_filtered_help("Other commands", filter, ARRAY_SIZE(filter)); - - } else if (strcmp(args[0], "navigation") == 0) { - cons_navigation_help(); - } else { - char *cmd = args[0]; - char cmd_with_slash[1 + strlen(cmd) + 1]; - sprintf(cmd_with_slash, "/%s", cmd); - - const gchar **help_text = NULL; - Command *command = g_hash_table_lookup(commands, cmd_with_slash); - - if (command != NULL) { - help_text = command->help.long_help; - } - - cons_show(""); - - if (help_text != NULL) { - int i; - for (i = 0; help_text[i] != NULL; i++) { - cons_show(help_text[i]); - } - } else { - cons_show("No such command."); - } - - cons_show(""); - } - - return TRUE; -} - -static gboolean -_cmd_about(gchar **args, struct cmd_help_t help) -{ - cons_show(""); - cons_about(); - if (ui_current_win_type() != WIN_CONSOLE) { - status_bar_new(1); - } - return TRUE; -} - -static gboolean -_cmd_prefs(gchar **args, struct cmd_help_t help) -{ - if (args[0] == NULL) { - cons_prefs(); - cons_show("Use the /account command for preferences for individual accounts."); - } else if (strcmp(args[0], "ui") == 0) { - cons_show(""); - cons_show_ui_prefs(); - cons_show(""); - } else if (strcmp(args[0], "desktop") == 0) { - cons_show(""); - cons_show_desktop_prefs(); - cons_show(""); - } else if (strcmp(args[0], "chat") == 0) { - cons_show(""); - cons_show_chat_prefs(); - cons_show(""); - } else if (strcmp(args[0], "log") == 0) { - cons_show(""); - cons_show_log_prefs(); - cons_show(""); - } else if (strcmp(args[0], "conn") == 0) { - cons_show(""); - cons_show_connection_prefs(); - cons_show(""); - } else if (strcmp(args[0], "presence") == 0) { - cons_show(""); - cons_show_presence_prefs(); - cons_show(""); - } else { - cons_show("Usage: %s", help.usage); - } - - return TRUE; -} - -static gboolean -_cmd_theme(gchar **args, struct cmd_help_t help) -{ - // list themes - if (strcmp(args[0], "list") == 0) { - GSList *themes = theme_list(); - cons_show_themes(themes); - g_slist_free_full(themes, g_free); - - // load a theme - } else if (strcmp(args[0], "set") == 0) { - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - } else if (theme_load(args[1])) { - ui_load_colours(); - prefs_set_string(PREF_THEME, args[1]); - cons_show("Loaded theme: %s", args[1]); - } else { - cons_show("Couldn't find theme: %s", args[1]); - } - } else { - cons_show("Usage: %s", help.usage); - } - - return TRUE; -} - -static gboolean -_cmd_who(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - } else { - char *presence = args[0]; - char *group = NULL; - if ((g_strv_length(args) == 2) && (args[1] != NULL)) { - group = args[1]; - } - - // bad arg - if ((presence != NULL) - && (strcmp(presence, "online") != 0) - && (strcmp(presence, "available") != 0) - && (strcmp(presence, "unavailable") != 0) - && (strcmp(presence, "offline") != 0) - && (strcmp(presence, "away") != 0) - && (strcmp(presence, "chat") != 0) - && (strcmp(presence, "xa") != 0) - && (strcmp(presence, "dnd") != 0) - && (strcmp(presence, "any") != 0)) { - cons_show("Usage: %s", help.usage); - - // valid arg - } else { - if (win_type == WIN_MUC) { - if (group != NULL) { - cons_show("The group argument is not valid when in a chat room."); - return TRUE; - } - - char *room = ui_current_recipient(); - GList *list = muc_get_roster(room); - - // no arg, show all contacts - if ((presence == NULL) || (g_strcmp0(presence, "any") == 0)) { - ui_room_roster(room, list, NULL); - - // available - } else if (strcmp("available", presence) == 0) { - GList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (p_contact_is_available(contact)) { - filtered = g_list_append(filtered, contact); - } - list = g_list_next(list); - } - - ui_room_roster(room, filtered, "available"); - - // unavailable - } else if (strcmp("unavailable", presence) == 0) { - GList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (!p_contact_is_available(contact)) { - filtered = g_list_append(filtered, contact); - } - list = g_list_next(list); - } - - ui_room_roster(room, filtered, "unavailable"); - - // online, available resources - } else if (strcmp("online", presence) == 0) { - GList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (p_contact_has_available_resource(contact)) { - filtered = g_list_append(filtered, contact); - } - list = g_list_next(list); - } - - ui_room_roster(room, filtered, "online"); - - // offline, no available resources - } else if (strcmp("offline", presence) == 0) { - GList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (!p_contact_has_available_resource(contact)) { - filtered = g_list_append(filtered, contact); - } - list = g_list_next(list); - } - - ui_room_roster(room, filtered, "offline"); - - // show specific status - } else { - GList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (strcmp(p_contact_presence(contact), presence) == 0) { - filtered = g_list_append(filtered, contact); - } - list = g_list_next(list); - } - - ui_room_roster(room, filtered, presence); - } - - // not in groupchat window - } else { - cons_show(""); - GSList *list = NULL; - if (group != NULL) { - list = roster_get_group(group); - } else { - list = roster_get_contacts(); - } - - // no arg, show all contacts - if ((presence == NULL) || (g_strcmp0(presence, "any") == 0)) { - if (group != NULL) { - cons_show("%s:", group); - } else { - cons_show("All contacts:"); - } - cons_show_contacts(list); - - // available - } else if (strcmp("available", presence) == 0) { - if (group != NULL) { - cons_show("%s (%s):", group, presence); - } else { - cons_show("Contacts (%s):", presence); - } - GSList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (p_contact_is_available(contact)) { - filtered = g_slist_append(filtered, contact); - } - list = g_slist_next(list); - } - - cons_show_contacts(filtered); - - // unavailable - } else if (strcmp("unavailable", presence) == 0) { - if (group != NULL) { - cons_show("%s (%s):", group, presence); - } else { - cons_show("Contacts (%s):", presence); - } - GSList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (!p_contact_is_available(contact)) { - filtered = g_slist_append(filtered, contact); - } - list = g_slist_next(list); - } - - cons_show_contacts(filtered); - - // online, available resources - } else if (strcmp("online", presence) == 0) { - if (group != NULL) { - cons_show("%s (%s):", group, presence); - } else { - cons_show("Contacts (%s):", presence); - } - GSList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (p_contact_has_available_resource(contact)) { - filtered = g_slist_append(filtered, contact); - } - list = g_slist_next(list); - } - - cons_show_contacts(filtered); - - // offline, no available resources - } else if (strcmp("offline", presence) == 0) { - if (group != NULL) { - cons_show("%s (%s):", group, presence); - } else { - cons_show("Contacts (%s):", presence); - } - GSList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (!p_contact_has_available_resource(contact)) { - filtered = g_slist_append(filtered, contact); - } - list = g_slist_next(list); - } - - cons_show_contacts(filtered); - - // show specific status - } else { - if (group != NULL) { - cons_show("%s (%s):", group, presence); - } else { - cons_show("Contacts (%s):", presence); - } - GSList *filtered = NULL; - - while (list != NULL) { - PContact contact = list->data; - if (strcmp(p_contact_presence(contact), presence) == 0) { - filtered = g_slist_append(filtered, contact); - } - list = g_slist_next(list); - } - - cons_show_contacts(filtered); - } - } - } - } - - if (win_type != WIN_CONSOLE && win_type != WIN_MUC) { - status_bar_new(1); - } - - return TRUE; -} - -static gboolean -_cmd_msg(gchar **args, struct cmd_help_t help) -{ - char *usr = args[0]; - char *msg = args[1]; - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - if (win_type == WIN_MUC) { - char *room_name = ui_current_recipient(); - if (muc_nick_in_roster(room_name, usr)) { - GString *full_jid = g_string_new(room_name); - g_string_append(full_jid, "/"); - g_string_append(full_jid, usr); - - if (msg != NULL) { - message_send(msg, full_jid->str); - ui_outgoing_msg("me", full_jid->str, msg); - } else { - ui_new_chat_win(full_jid->str); - } - - g_string_free(full_jid, TRUE); - - } else { - ui_current_print_line("No such participant \"%s\" in room.", usr); - } - - return TRUE; - - } else { - char *usr_jid = roster_barejid_from_name(usr); - if (usr_jid == NULL) { - usr_jid = usr; - } - if (msg != NULL) { - message_send(msg, usr_jid); - ui_outgoing_msg("me", usr_jid, msg); - - if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - - return TRUE; - } else { - const char * jid = NULL; - - if (roster_barejid_from_name(usr_jid) != NULL) { - jid = roster_barejid_from_name(usr_jid); - } else { - jid = usr_jid; - } - - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(jid)) { - chat_session_start(jid, TRUE); - } - } - - ui_new_chat_win(usr_jid); - return TRUE; - } - } -} - -static gboolean -_cmd_group(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - // list all groups - if (args[0] == NULL) { - GSList *groups = roster_get_groups(); - GSList *curr = groups; - if (curr != NULL) { - cons_show("Groups:"); - while (curr != NULL) { - cons_show(" %s", curr->data); - curr = g_slist_next(curr); - } - - g_slist_free_full(groups, g_free); - } else { - cons_show("No groups."); - } - return TRUE; - } - - // show contacts in group - if (strcmp(args[0], "show") == 0) { - char *group = args[1]; - if (group == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - GSList *list = roster_get_group(group); - cons_show_roster_group(group, list); - return TRUE; - } - - // add contact to group - if (strcmp(args[0], "add") == 0) { - char *group = args[1]; - char *contact = args[2]; - - if ((group == NULL) || (contact == NULL)) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *barejid = roster_barejid_from_name(contact); - if (barejid == NULL) { - barejid = contact; - } - - PContact pcontact = roster_get_contact(barejid); - if (pcontact == NULL) { - cons_show("Contact not found in roster: %s", barejid); - return TRUE; - } - - roster_add_to_group(group, barejid); - - return TRUE; - } - - // remove contact from group - if (strcmp(args[0], "remove") == 0) { - char *group = args[1]; - char *contact = args[2]; - - if ((group == NULL) || (contact == NULL)) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *barejid = roster_barejid_from_name(contact); - if (barejid == NULL) { - barejid = contact; - } - - PContact pcontact = roster_get_contact(barejid); - if (pcontact == NULL) { - cons_show("Contact not found in roster: %s", barejid); - return TRUE; - } - - roster_remove_from_group(group, barejid); - - return TRUE; - } - - cons_show("Usage: %s", help.usage); - return TRUE; -} - -static gboolean -_cmd_roster(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - // show roster - if (args[0] == NULL) { - GSList *list = roster_get_contacts(); - cons_show_roster(list); - return TRUE; - } - - // add contact - if (strcmp(args[0], "add") == 0) { - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *jid = args[1]; - char *name = args[2]; - - roster_add_new(jid, name); - - return TRUE; - } - - // remove contact - if (strcmp(args[0], "remove") == 0) { - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *jid = args[1]; - - roster_send_remove(jid); - - return TRUE; - } - - // change nickname - if (strcmp(args[0], "nick") == 0) { - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *jid = args[1]; - char *name = args[2]; - - // contact does not exist - PContact contact = roster_get_contact(jid); - if (contact == NULL) { - cons_show("Contact not found in roster: %s", jid); - return TRUE; - } - - roster_change_name(jid, name); - - if (name == NULL) { - cons_show("Nickname for %s removed.", jid); - } else { - cons_show("Nickname for %s set to: %s.", jid, name); - } - - return TRUE; - } - - cons_show("Usage: %s", help.usage); - return TRUE; -} - -static gboolean -_cmd_duck(gchar **args, struct cmd_help_t help) -{ - char *query = args[0]; - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - // if no duck win open, create it and send a help command - if (!ui_duck_exists()) { - ui_create_duck_win(); - - if (query != NULL) { - message_send_duck(query); - ui_duck(query); - } - - // window exists, send query - } else { - ui_open_duck_win(); - - if (query != NULL) { - message_send_duck(query); - ui_duck(query); - } - } - - return TRUE; -} - -static gboolean -_cmd_status(gchar **args, struct cmd_help_t help) -{ - char *usr = args[0]; - char *usr_jid = NULL; - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - switch (win_type) - { - case WIN_MUC: - if (usr != NULL) { - ui_status_room(usr); - } else { - ui_current_print_line("You must specify a nickname."); - } - break; - case WIN_CHAT: - if (usr != NULL) { - ui_current_print_line("No parameter required when in chat."); - } else { - ui_status(); - } - break; - case WIN_PRIVATE: - if (usr != NULL) { - ui_current_print_line("No parameter required when in chat."); - } else { - ui_status_private(); - } - break; - case WIN_CONSOLE: - if (usr != NULL) { - usr_jid = roster_barejid_from_name(usr); - if (usr_jid == NULL) { - usr_jid = usr; - } - cons_show_status(usr_jid); - } else { - cons_show("Usage: %s", help.usage); - } - break; - default: - break; - } - - return TRUE; -} - -static gboolean -_cmd_info(gchar **args, struct cmd_help_t help) -{ - char *usr = args[0]; - char *usr_jid = NULL; - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - PContact pcontact = NULL; - char *recipient; - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - recipient = ui_current_recipient(); - - switch (win_type) - { - case WIN_MUC: - if (usr != NULL) { - pcontact = muc_get_participant(recipient, usr); - if (pcontact != NULL) { - cons_show_info(pcontact); - } else { - cons_show("No such participant \"%s\" in room.", usr); - } - } else { - cons_show("No nickname supplied to /info in chat room."); - } - break; - case WIN_CHAT: - if (usr != NULL) { - cons_show("No parameter required for /info in chat."); - } else { - pcontact = roster_get_contact(recipient); - if (pcontact != NULL) { - cons_show_info(pcontact); - } else { - cons_show("No such contact \"%s\" in roster.", recipient); - } - } - break; - case WIN_PRIVATE: - if (usr != NULL) { - ui_current_print_line("No parameter required when in chat."); - } else { - Jid *jid = jid_create(recipient); - pcontact = muc_get_participant(jid->barejid, jid->resourcepart); - if (pcontact != NULL) { - cons_show_info(pcontact); - } else { - cons_show("No such participant \"%s\" in room.", jid->resourcepart); - } - jid_destroy(jid); - } - break; - case WIN_CONSOLE: - if (usr != NULL) { - usr_jid = roster_barejid_from_name(usr); - if (usr_jid == NULL) { - usr_jid = usr; - } - pcontact = roster_get_contact(usr_jid); - if (pcontact != NULL) { - cons_show_info(pcontact); - } else { - cons_show("No such contact \"%s\" in roster.", usr); - } - } else { - cons_show("Usage: %s", help.usage); - } - break; - default: - break; - } - - return TRUE; -} - -static gboolean -_cmd_caps(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - PContact pcontact = NULL; - char *recipient; - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - switch (win_type) - { - case WIN_MUC: - if (args[0] != NULL) { - recipient = ui_current_recipient(); - pcontact = muc_get_participant(recipient, args[0]); - if (pcontact != NULL) { - Resource *resource = p_contact_get_resource(pcontact, args[0]); - cons_show_caps(args[0], resource); - } else { - cons_show("No such participant \"%s\" in room.", args[0]); - } - } else { - cons_show("No nickname supplied to /caps in chat room."); - } - break; - case WIN_CHAT: - case WIN_CONSOLE: - if (args[0] != NULL) { - Jid *jid = jid_create(args[0]); - - if (jid->fulljid == NULL) { - cons_show("You must provide a full jid to the /caps command."); - } else { - pcontact = roster_get_contact(jid->barejid); - if (pcontact == NULL) { - cons_show("Contact not found in roster: %s", jid->barejid); - } else { - Resource *resource = p_contact_get_resource(pcontact, jid->resourcepart); - if (resource == NULL) { - cons_show("Could not find resource %s, for contact %s", jid->barejid, jid->resourcepart); - } else { - cons_show_caps(jid->fulljid, resource); - } - } - } - } else { - cons_show("You must provide a jid to the /caps command."); - } - break; - case WIN_PRIVATE: - if (args[0] != NULL) { - cons_show("No parameter needed to /caps when in private chat."); - } else { - recipient = ui_current_recipient(); - Jid *jid = jid_create(recipient); - if (jid) { - pcontact = muc_get_participant(jid->barejid, jid->resourcepart); - Resource *resource = p_contact_get_resource(pcontact, jid->resourcepart); - cons_show_caps(jid->resourcepart, resource); - jid_destroy(jid); - } - } - break; - default: - break; - } - - return TRUE; -} - - -static gboolean -_cmd_software(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - PContact pcontact = NULL; - char *recipient; - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - switch (win_type) - { - case WIN_MUC: - if (args[0] != NULL) { - recipient = ui_current_recipient(); - pcontact = muc_get_participant(recipient, args[0]); - if (pcontact != NULL) { - Jid *jid = jid_create_from_bare_and_resource(recipient, args[0]); - iq_send_software_version(jid->fulljid); - jid_destroy(jid); - } else { - cons_show("No such participant \"%s\" in room.", args[0]); - } - } else { - cons_show("No nickname supplied to /software in chat room."); - } - break; - case WIN_CHAT: - case WIN_CONSOLE: - if (args[0] != NULL) { - Jid *jid = jid_create(args[0]); - - if (jid == NULL || jid->fulljid == NULL) { - cons_show("You must provide a full jid to the /software command."); - } else { - iq_send_software_version(jid->fulljid); - } - jid_destroy(jid); - } else { - cons_show("You must provide a jid to the /software command."); - } - break; - case WIN_PRIVATE: - if (args[0] != NULL) { - cons_show("No parameter needed to /software when in private chat."); - } else { - recipient = ui_current_recipient(); - iq_send_software_version(recipient); - } - break; - default: - break; - } - - return TRUE; -} - -static gboolean -_cmd_join(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - ProfAccount *account = accounts_get_account(jabber_get_account_name()); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - Jid *room_arg = jid_create(args[0]); - if (room_arg == NULL) { - cons_show_error("Specified room has incorrect format"); - return TRUE; - } - - int num_args = g_strv_length(args); - char *room = NULL; - char *nick = NULL; - GString *room_str = g_string_new(""); - Jid *my_jid = jid_create(jabber_get_fulljid()); - - // full room jid supplied (room@server) - if (room_arg->localpart != NULL) { - room = args[0]; - - // server not supplied (room), use account preference - } else { - g_string_append(room_str, args[0]); - g_string_append(room_str, "@"); - g_string_append(room_str, account->muc_service); - room = room_str->str; - } - - // nick supplied - if (num_args == 2) { - nick = args[1]; - - // otherwise use account preference - } else { - nick = account->muc_nick; - } - - Jid *room_jid = jid_create_from_bare_and_resource(room, nick); - - if (!muc_room_is_active(room_jid)) { - presence_join_room(room_jid); - } - ui_room_join(room_jid); - muc_remove_invite(room); - - jid_destroy(room_arg); - jid_destroy(room_jid); - jid_destroy(my_jid); - g_string_free(room_str, TRUE); - accounts_free_account(account); - - return TRUE; -} - -static gboolean -_cmd_invite(gchar **args, struct cmd_help_t help) -{ - char *contact = args[0]; - char *reason = args[1]; - char *room = NULL; - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - if (ui_current_win_type() != WIN_MUC) { - cons_show("You must be in a chat room to send an invite."); - return TRUE; - } - - char *usr_jid = roster_barejid_from_name(contact); - if (usr_jid == NULL) { - usr_jid = contact; - } - room = ui_current_recipient(); - message_send_invite(room, usr_jid, reason); - if (reason != NULL) { - cons_show("Room invite sent, contact: %s, room: %s, reason: \"%s\".", - contact, room, reason); - } else { - cons_show("Room invite sent, contact: %s, room: %s.", - contact, room); - } - - return TRUE; -} - -static gboolean -_cmd_invites(gchar **args, struct cmd_help_t help) -{ - GSList *invites = muc_get_invites(); - cons_show_room_invites(invites); - g_slist_free_full(invites, g_free); - return TRUE; -} - -static gboolean -_cmd_decline(gchar **args, struct cmd_help_t help) -{ - if (!muc_invites_include(args[0])) { - cons_show("No such invite exists."); - } else { - muc_remove_invite(args[0]); - cons_show("Declined invite to %s.", args[0]); - } - - return TRUE; -} - -gboolean -_cmd_rooms(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - if (args[0] == NULL) { - ProfAccount *account = accounts_get_account(jabber_get_account_name()); - iq_room_list_request(account->muc_service); - } else { - iq_room_list_request(args[0]); - } - - return TRUE; -} - -static gboolean -_cmd_bookmark(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - gchar *cmd = args[0]; - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currenlty connect."); - return TRUE; - } - - /* TODO: /bookmark list room@server */ - - if (cmd == NULL || strcmp(cmd, "list") == 0) { - cons_show_bookmarks(bookmark_get_list()); - } else { - gboolean autojoin = FALSE; - gchar *jid = NULL; - gchar *nick = NULL; - int idx = 1; - - while (args[idx] != NULL) { - gchar *opt = args[idx]; - - if (strcmp(opt, "autojoin") == 0) { - autojoin = TRUE; - } else if (jid == NULL) { - jid = opt; - } else if (nick == NULL) { - nick = opt; - } else { - cons_show("Usage: %s", help.usage); - } - - ++idx; - } - - if (jid == NULL) { - win_type_t win_type = ui_current_win_type(); - - if (win_type == WIN_MUC) { - jid = ui_current_recipient(); - nick = muc_get_room_nick(jid); - } else { - cons_show("Usage: %s", help.usage); - return TRUE; - } - } - - if (strcmp(cmd, "add") == 0) { - bookmark_add(jid, nick, autojoin); - } else if (strcmp(cmd, "remove") == 0) { - bookmark_remove(jid, autojoin); - } else { - cons_show("Usage: %s", help.usage); - } - } - - return TRUE; -} - -static gboolean -_cmd_disco(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currenlty connected."); - return TRUE; - } - - GString *jid = g_string_new(""); - if (args[1] != NULL) { - jid = g_string_append(jid, args[1]); - } else { - Jid *jidp = jid_create(jabber_get_fulljid()); - jid = g_string_append(jid, jidp->domainpart); - jid_destroy(jidp); - } - - if (g_strcmp0(args[0], "info") == 0) { - iq_disco_info_request(jid->str); - } else { - iq_disco_items_request(jid->str); - } - - g_string_free(jid, TRUE); - - return TRUE; -} - -static gboolean -_cmd_nick(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - if (ui_current_win_type() != WIN_MUC) { - cons_show("You can only change your nickname in a chat room window."); - return TRUE; - } - - char *room = ui_current_recipient(); - char *nick = args[0]; - presence_change_room_nick(room, nick); - - return TRUE; -} - -static gboolean -_cmd_tiny(gchar **args, struct cmd_help_t help) -{ - char *url = args[0]; - win_type_t win_type = ui_current_win_type(); - - if (!tinyurl_valid(url)) { - GString *error = g_string_new("/tiny, badly formed URL: "); - g_string_append(error, url); - cons_show_error(error->str); - if (win_type != WIN_CONSOLE) { - ui_current_error_line(error->str); - } - g_string_free(error, TRUE); - } else if (win_type != WIN_CONSOLE) { - char *tiny = tinyurl_get(url); - - if (tiny != NULL) { - if (win_type == WIN_CHAT) { - char *recipient = ui_current_recipient(); - message_send(tiny, recipient); - - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, recipient, tiny, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - - ui_outgoing_msg("me", recipient, tiny); - } else if (win_type == WIN_PRIVATE) { - char *recipient = ui_current_recipient(); - message_send(tiny, recipient); - ui_outgoing_msg("me", recipient, tiny); - } else { // groupchat - char *recipient = ui_current_recipient(); - message_send_groupchat(tiny, recipient); - } - free(tiny); - } else { - cons_show_error("Couldn't get tinyurl."); - } - } else { - cons_show("/tiny can only be used in chat windows"); - } - - return TRUE; -} - -static gboolean -_cmd_clear(gchar **args, struct cmd_help_t help) -{ - ui_clear_current(); - return TRUE; -} - -static gboolean -_cmd_close(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - int index = 0; - int count = 0; - - if (args[0] == NULL) { - index = ui_current_win_index(); - } else if (strcmp(args[0], "all") == 0) { - count = ui_close_all_wins(); - if (count == 0) { - cons_show("No windows to close."); - } else if (count == 1) { - cons_show("Closed 1 window."); - } else { - cons_show("Closed %d windows.", count); - } - return TRUE; - } else if (strcmp(args[0], "read") == 0) { - count = ui_close_read_wins(); - if (count == 0) { - cons_show("No windows to close."); - } else if (count == 1) { - cons_show("Closed 1 window."); - } else { - cons_show("Closed %d windows.", count); - } - return TRUE; - } else { - index = atoi(args[0]); - } - - if (index < 0 || index == 10) { - cons_show("No such window exists."); - return TRUE; - } - - if (index == 1) { - cons_show("Cannot close console window."); - return TRUE; - } - - if (index == 0) { - index = 10; - } - - if (!ui_win_exists(index)) { - cons_show("Window is not open."); - return TRUE; - } - - // handle leaving rooms, or chat - if (conn_status == JABBER_CONNECTED) { - ui_close_connected_win(index); - } - - // close the window - ui_close_win(index); - cons_show("Closed window %d", index); - - return TRUE; -} - -static gboolean -_cmd_leave(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); - int index = ui_current_win_index(); - - if (win_type != WIN_MUC) { - cons_show("You can only use the /leave command in a chat room."); - cons_alert(); - return TRUE; - } - - // handle leaving rooms, or chat - if (conn_status == JABBER_CONNECTED) { - ui_close_connected_win(index); - } - - // close the window - ui_close_win(index); - - return TRUE; -} - -static gboolean -_cmd_beep(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, "Sound", PREF_BEEP); -} - -static gboolean -_cmd_states(gchar **args, struct cmd_help_t help) -{ - gboolean result = _cmd_set_boolean_preference(args[0], help, "Sending chat states", - PREF_STATES); - - // if disabled, disable outtype and gone - if (result == TRUE && (strcmp(args[0], "off") == 0)) { - prefs_set_boolean(PREF_OUTTYPE, FALSE); - prefs_set_gone(0); - } - - return result; -} - -static gboolean -_cmd_titlebar(gchar **args, struct cmd_help_t help) -{ - if (strcmp(args[0], "version") != 0) { - cons_show("Usage: %s", help.usage); - return TRUE; - } else { - return _cmd_set_boolean_preference(args[1], help, - "Show version in window title", PREF_TITLEBARVERSION); - } -} - -static gboolean -_cmd_outtype(gchar **args, struct cmd_help_t help) -{ - gboolean result = _cmd_set_boolean_preference(args[0], help, - "Sending typing notifications", PREF_OUTTYPE); - - // if enabled, enable states - if (result == TRUE && (strcmp(args[0], "on") == 0)) { - prefs_set_boolean(PREF_STATES, TRUE); - } - - return result; -} - -static gboolean -_cmd_gone(gchar **args, struct cmd_help_t help) -{ - char *value = args[0]; - - gint period = atoi(value); - prefs_set_gone(period); - if (period == 0) { - cons_show("Automatic leaving conversations after period disabled."); - } else if (period == 1) { - cons_show("Leaving conversations after 1 minute of inactivity."); - } else { - cons_show("Leaving conversations after %d minutes of inactivity.", period); - } - - // if enabled, enable states - if (period > 0) { - prefs_set_boolean(PREF_STATES, TRUE); - } - - return TRUE; -} - - -static gboolean -_cmd_notify(gchar **args, struct cmd_help_t help) -{ - char *kind = args[0]; - char *value = args[1]; - - // bad kind - if ((strcmp(kind, "message") != 0) && (strcmp(kind, "typing") != 0) && - (strcmp(kind, "remind") != 0) && (strcmp(kind, "invite") != 0) && - (strcmp(kind, "sub") != 0)) { - cons_show("Usage: %s", help.usage); - - // set message setting - } else if (strcmp(kind, "message") == 0) { - if (strcmp(value, "on") == 0) { - cons_show("Message notifications enabled."); - prefs_set_boolean(PREF_NOTIFY_MESSAGE, TRUE); - } else if (strcmp(value, "off") == 0) { - cons_show("Message notifications disabled."); - prefs_set_boolean(PREF_NOTIFY_MESSAGE, FALSE); - } else { - cons_show("Usage: /notify message on|off"); - } - - // set typing setting - } else if (strcmp(kind, "typing") == 0) { - if (strcmp(value, "on") == 0) { - cons_show("Typing notifications enabled."); - prefs_set_boolean(PREF_NOTIFY_TYPING, TRUE); - } else if (strcmp(value, "off") == 0) { - cons_show("Typing notifications disabled."); - prefs_set_boolean(PREF_NOTIFY_TYPING, FALSE); - } else { - cons_show("Usage: /notify typing on|off"); - } - - // set invite setting - } else if (strcmp(kind, "invite") == 0) { - if (strcmp(value, "on") == 0) { - cons_show("Chat room invite notifications enabled."); - prefs_set_boolean(PREF_NOTIFY_INVITE, TRUE); - } else if (strcmp(value, "off") == 0) { - cons_show("Chat room invite notifications disabled."); - prefs_set_boolean(PREF_NOTIFY_INVITE, FALSE); - } else { - cons_show("Usage: /notify invite on|off"); - } - - // set subscription setting - } else if (strcmp(kind, "sub") == 0) { - if (strcmp(value, "on") == 0) { - cons_show("Subscription notifications enabled."); - prefs_set_boolean(PREF_NOTIFY_SUB, TRUE); - } else if (strcmp(value, "off") == 0) { - cons_show("Subscription notifications disabled."); - prefs_set_boolean(PREF_NOTIFY_SUB, FALSE); - } else { - cons_show("Usage: /notify sub on|off"); - } - - // set remind setting - } else if (strcmp(kind, "remind") == 0) { - gint period = atoi(value); - prefs_set_notify_remind(period); - if (period == 0) { - cons_show("Message reminders disabled."); - } else if (period == 1) { - cons_show("Message reminder period set to 1 second."); - } else { - cons_show("Message reminder period set to %d seconds.", period); - } - - } else { - cons_show("Unknown command: %s.", kind); - } - - return TRUE; -} - -static gboolean -_cmd_log(gchar **args, struct cmd_help_t help) -{ - char *subcmd = args[0]; - char *value = args[1]; - int intval; - - if (strcmp(subcmd, "maxsize") == 0) { - if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) { - prefs_set_max_log_size(intval); - cons_show("Log maxinum size set to %d bytes", intval); - } - } else { - cons_show("Usage: %s", help.usage); - } - - /* TODO: make 'level' subcommand for debug level */ - - return TRUE; -} - -static gboolean -_cmd_reconnect(gchar **args, struct cmd_help_t help) -{ - char *value = args[0]; - int intval; - - if (_strtoi(value, &intval, 0, INT_MAX) == 0) { - prefs_set_reconnect(intval); - if (intval == 0) { - cons_show("Reconnect disabled.", intval); - } else { - cons_show("Reconnect interval set to %d seconds.", intval); - } - } else { - cons_show("Usage: %s", help.usage); - } - - return TRUE; -} - -static gboolean -_cmd_autoping(gchar **args, struct cmd_help_t help) -{ - char *value = args[0]; - int intval; - - if (_strtoi(value, &intval, 0, INT_MAX) == 0) { - prefs_set_autoping(intval); - jabber_set_autoping(intval); - if (intval == 0) { - cons_show("Autoping disabled.", intval); - } else { - cons_show("Autoping interval set to %d seconds.", intval); - } - } else { - cons_show("Usage: %s", help.usage); - } - - return TRUE; -} - -static gboolean -_cmd_autoaway(gchar **args, struct cmd_help_t help) -{ - char *setting = args[0]; - char *value = args[1]; - int minutesval; - - if ((strcmp(setting, "mode") != 0) && (strcmp(setting, "time") != 0) && - (strcmp(setting, "message") != 0) && (strcmp(setting, "check") != 0)) { - cons_show("Setting must be one of 'mode', 'time', 'message' or 'check'"); - return TRUE; - } - - if (strcmp(setting, "mode") == 0) { - if ((strcmp(value, "idle") != 0) && (strcmp(value, "away") != 0) && - (strcmp(value, "off") != 0)) { - cons_show("Mode must be one of 'idle', 'away' or 'off'"); - } else { - prefs_set_string(PREF_AUTOAWAY_MODE, value); - cons_show("Auto away mode set to: %s.", value); - } - - return TRUE; - } - - if (strcmp(setting, "time") == 0) { - if (_strtoi(value, &minutesval, 1, INT_MAX) == 0) { - prefs_set_autoaway_time(minutesval); - cons_show("Auto away time set to: %d minutes.", minutesval); - } - - return TRUE; - } - - if (strcmp(setting, "message") == 0) { - if (strcmp(value, "off") == 0) { - prefs_set_string(PREF_AUTOAWAY_MESSAGE, NULL); - cons_show("Auto away message cleared."); - } else { - prefs_set_string(PREF_AUTOAWAY_MESSAGE, value); - cons_show("Auto away message set to: \"%s\".", value); - } - - return TRUE; - } - - if (strcmp(setting, "check") == 0) { - return _cmd_set_boolean_preference(value, help, "Online check", - PREF_AUTOAWAY_CHECK); - } - - return TRUE; -} - -static gboolean -_cmd_priority(gchar **args, struct cmd_help_t help) -{ - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - return TRUE; - } - - char *value = args[0]; - int intval; - - if (_strtoi(value, &intval, -128, 127) == 0) { - accounts_set_priority_all(jabber_get_account_name(), intval); - resource_presence_t last_presence = accounts_get_last_presence(jabber_get_account_name()); - presence_update(last_presence, jabber_get_presence_message(), 0); - cons_show("Priority set to %d.", intval); - } - - return TRUE; -} - -static gboolean -_cmd_statuses(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, - "Status notifications", PREF_STATUSES); -} - -static gboolean -_cmd_vercheck(gchar **args, struct cmd_help_t help) -{ - int num_args = g_strv_length(args); - - if (num_args == 0) { - cons_check_version(TRUE); - return TRUE; - } else { - return _cmd_set_boolean_preference(args[0], help, - "Version checking", PREF_VERCHECK); - } -} - -static gboolean -_cmd_flash(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, - "Screen flash", PREF_FLASH); -} - -static gboolean -_cmd_intype(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, - "Show contact typing", PREF_INTYPE); -} - -static gboolean -_cmd_splash(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, - "Splash screen", PREF_SPLASH); -} - -static gboolean -_cmd_autoconnect(gchar **args, struct cmd_help_t help) -{ - if (strcmp(args[0], "off") == 0) { - prefs_set_string(PREF_CONNECT_ACCOUNT, NULL); - cons_show("Autoconnect account disabled."); - } else if (strcmp(args[0], "set") == 0) { - prefs_set_string(PREF_CONNECT_ACCOUNT, args[1]); - cons_show("Autoconnect account set to: %s.", args[1]); - } else { - cons_show("Usage: %s", help.usage); - } - return true; -} - -static gboolean -_cmd_chlog(gchar **args, struct cmd_help_t help) -{ - gboolean result = _cmd_set_boolean_preference(args[0], help, - "Chat logging", PREF_CHLOG); - - // if set to off, disable history - if (result == TRUE && (strcmp(args[0], "off") == 0)) { - prefs_set_boolean(PREF_HISTORY, FALSE); - } - - return result; -} - -static gboolean -_cmd_grlog(gchar **args, struct cmd_help_t help) -{ - gboolean result = _cmd_set_boolean_preference(args[0], help, - "Groupchat logging", PREF_GRLOG); - - return result; -} - -static gboolean -_cmd_mouse(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args[0], help, - "Mouse handling", PREF_MOUSE); -} - -static gboolean -_cmd_history(gchar **args, struct cmd_help_t help) -{ - gboolean result = _cmd_set_boolean_preference(args[0], help, - "Chat history", PREF_HISTORY); - - // if set to on, set chlog - if (result == TRUE && (strcmp(args[0], "on") == 0)) { - prefs_set_boolean(PREF_CHLOG, TRUE); - } - - return result; -} - -static gboolean -_cmd_away(gchar **args, struct cmd_help_t help) -{ - _update_presence(RESOURCE_AWAY, "away", args); - return TRUE; -} - -static gboolean -_cmd_online(gchar **args, struct cmd_help_t help) -{ - _update_presence(RESOURCE_ONLINE, "online", args); - return TRUE; -} - -static gboolean -_cmd_dnd(gchar **args, struct cmd_help_t help) -{ - _update_presence(RESOURCE_DND, "dnd", args); - return TRUE; -} - -static gboolean -_cmd_chat(gchar **args, struct cmd_help_t help) -{ - _update_presence(RESOURCE_CHAT, "chat", args); - return TRUE; -} - -static gboolean -_cmd_xa(gchar **args, struct cmd_help_t help) -{ - _update_presence(RESOURCE_XA, "xa", args); - return TRUE; -} - -// helper function that asks the user for a password and saves it in passwd - -static char * -_ask_password(void) { - char *passwd = malloc(sizeof(char) * (MAX_PASSWORD_SIZE + 1)); - status_bar_get_password(); - status_bar_refresh(); - inp_block(); - inp_get_password(passwd); - inp_non_block(); - - return passwd; -} - -// helper function for status change commands - -static void -_update_presence(const resource_presence_t resource_presence, - const char * const show, gchar **args) -{ - char *msg = NULL; - int num_args = g_strv_length(args); - if (num_args == 1) { - msg = args[0]; - } - - jabber_conn_status_t conn_status = jabber_get_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); - } else { - presence_update(resource_presence, msg, 0); - - contact_presence_t contact_presence = contact_presence_from_resource_presence(resource_presence); - title_bar_set_status(contact_presence); - - gint priority = accounts_get_priority_for_presence_type(jabber_get_account_name(), resource_presence); - if (msg != NULL) { - cons_show("Status set to %s (priority %d), \"%s\".", show, priority, msg); - } else { - cons_show("Status set to %s (priority %d).", show, priority); - } - } - -} - -// helper function for boolean preference commands - -static gboolean -_cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, - const char * const display, preference_t pref) -{ - GString *enabled = g_string_new(display); - g_string_append(enabled, " enabled."); - - GString *disabled = g_string_new(display); - g_string_append(disabled, " disabled."); - - if (strcmp(arg, "on") == 0) { - cons_show(enabled->str); - prefs_set_boolean(pref, TRUE); - } else if (strcmp(arg, "off") == 0) { - cons_show(disabled->str); - prefs_set_boolean(pref, FALSE); - } else { - char usage[strlen(help.usage) + 8]; - sprintf(usage, "Usage: %s", help.usage); - cons_show(usage); - } - - g_string_free(enabled, TRUE); - g_string_free(disabled, TRUE); - - return TRUE; -} - static char * _sub_autocomplete(char *input, int *size) { @@ -3977,23 +1580,3 @@ _account_autocomplete(char *input, int *size) return NULL; } -static int -_strtoi(char *str, int *saveptr, int min, int max) -{ - char *ptr; - int val; - - errno = 0; - val = (int)strtol(str, &ptr, 0); - if (*str == '\0' || *ptr != '\0') { - cons_show("Illegal character. Must be a number."); - return -1; - } else if (errno == ERANGE || val < min || val > max) { - cons_show("Value out of range. Must be in %d..%d.", min, max); - return -1; - } - - *saveptr = val; - - return 0; -} diff --git a/src/command/command.h b/src/command/command.h index 8743d662..f958c0d1 100644 --- a/src/command/command.h +++ b/src/command/command.h @@ -25,15 +25,10 @@ #include -// Command help strings -typedef struct cmd_help_t { - const gchar *usage; - const gchar *short_help; - const gchar *long_help[50]; -} CommandHelp; +GHashTable *commands; void cmd_init(void); -void cmd_close(void); +void cmd_uninit(void); void cmd_autocomplete(char *input, int *size); void cmd_reset_autocomplete(void); @@ -49,6 +44,4 @@ void cmd_history_append(char *inp); char *cmd_history_previous(char *inp, int *size); char *cmd_history_next(char *inp, int *size); -gboolean _cmd_rooms(gchar **args, struct cmd_help_t help); - #endif diff --git a/src/command/commands.c b/src/command/commands.c new file mode 100644 index 00000000..4b0c5a5c --- /dev/null +++ b/src/command/commands.c @@ -0,0 +1,2384 @@ +/* + * commands.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include +#include +#include +#include + +#include "chat_session.h" +#include "command/commands.h" +#include "common.h" +#include "config/accounts.h" +#include "config/preferences.h" +#include "config/theme.h" +#include "contact.h" +#include "roster_list.h" +#include "jid.h" +#include "log.h" +#include "muc.h" +#include "profanity.h" +#include "tools/autocomplete.h" +#include "tools/parser.h" +#include "tools/tinyurl.h" +#include "ui/ui.h" +#include "xmpp/xmpp.h" +#include "xmpp/bookmark.h" + +static char * _ask_password(void); +static void _update_presence(const resource_presence_t presence, + const char * const show, gchar **args); +static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, + const char * const display, preference_t pref); +static int _strtoi(char *str, int *saveptr, int min, int max); +static void _cmd_show_filtered_help(char *heading, gchar *cmd_filter[], int filter_size); +static gint _compare_commands(Command *a, Command *b); + +extern GHashTable *commands; + +gboolean +cmd_connect(gchar **args, struct cmd_help_t help) +{ + gboolean result = FALSE; + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { + cons_show("You are either connected already, or a login is in process."); + result = TRUE; + } else { + char *user = args[0]; + char *altdomain = args[1]; + char *lower = g_utf8_strdown(user, -1); + char *jid; + + ProfAccount *account = accounts_get_account(lower); + if (account != NULL) { + if (account->resource != NULL) { + jid = create_fulljid(account->jid, account->resource); + } else { + jid = strdup(account->jid); + } + + if (account->password == NULL) { + account->password = _ask_password(); + } + cons_show("Connecting with account %s as %s", account->name, jid); + conn_status = jabber_connect_with_account(account); + } else { + char *passwd = _ask_password(); + jid = strdup(lower); + cons_show("Connecting as %s", jid); + conn_status = jabber_connect_with_details(jid, passwd, altdomain); + free(passwd); + } + g_free(lower); + + if (conn_status == JABBER_DISCONNECTED) { + cons_show_error("Connection attempt for %s failed.", jid); + log_debug("Connection attempt for %s failed", jid); + } + + accounts_free_account(account); + free(jid); + + result = TRUE; + } + + return result; +} + +gboolean +cmd_account(gchar **args, struct cmd_help_t help) +{ + char *command = args[0]; + + if (command == NULL) { + if (jabber_get_connection_status() != JABBER_CONNECTED) { + cons_show("Usage: %s", help.usage); + } else { + ProfAccount *account = accounts_get_account(jabber_get_account_name()); + cons_show_account(account); + accounts_free_account(account); + } + } else if (strcmp(command, "list") == 0) { + gchar **accounts = accounts_get_list(); + cons_show_account_list(accounts); + g_strfreev(accounts); + } else if (strcmp(command, "show") == 0) { + char *account_name = args[1]; + if (account_name == NULL) { + cons_show("Usage: %s", help.usage); + } else { + ProfAccount *account = accounts_get_account(account_name); + if (account == NULL) { + cons_show("No such account."); + cons_show(""); + } else { + cons_show_account(account); + accounts_free_account(account); + } + } + } else if (strcmp(command, "add") == 0) { + char *account_name = args[1]; + if (account_name == NULL) { + cons_show("Usage: %s", help.usage); + } else { + accounts_add(account_name, NULL); + cons_show("Account created."); + cons_show(""); + } + } else if (strcmp(command, "enable") == 0) { + char *account_name = args[1]; + if (account_name == NULL) { + cons_show("Usage: %s", help.usage); + } else { + if (accounts_enable(account_name)) { + cons_show("Account enabled."); + cons_show(""); + } else { + cons_show("No such account: %s", account_name); + cons_show(""); + } + } + } else if (strcmp(command, "disable") == 0) { + char *account_name = args[1]; + if (account_name == NULL) { + cons_show("Usage: %s", help.usage); + } else { + if (accounts_disable(account_name)) { + cons_show("Account disabled."); + cons_show(""); + } else { + cons_show("No such account: %s", account_name); + cons_show(""); + } + } + } else if (strcmp(command, "rename") == 0) { + if (g_strv_length(args) != 3) { + cons_show("Usage: %s", help.usage); + } else { + char *account_name = args[1]; + char *new_name = args[2]; + + if (accounts_rename(account_name, new_name)) { + cons_show("Account renamed."); + cons_show(""); + } else { + cons_show("Either account %s doesn't exist, or account %s already exists.", account_name, new_name); + cons_show(""); + } + } + } else if (strcmp(command, "set") == 0) { + if (g_strv_length(args) != 4) { + cons_show("Usage: %s", help.usage); + } else { + char *account_name = args[1]; + char *property = args[2]; + char *value = args[3]; + + if (!accounts_account_exists(account_name)) { + cons_show("Account %s doesn't exist", account_name); + cons_show(""); + } else { + if (strcmp(property, "jid") == 0) { + Jid *jid = jid_create(args[3]); + if (jid == NULL) { + cons_show("Malformed jid: %s", value); + } else { + accounts_set_jid(account_name, jid->barejid); + cons_show("Updated jid for account %s: %s", account_name, jid->barejid); + if (jid->resourcepart != NULL) { + accounts_set_resource(account_name, jid->resourcepart); + cons_show("Updated resource for account %s: %s", account_name, jid->resourcepart); + } + cons_show(""); + } + jid_destroy(jid); + } else if (strcmp(property, "server") == 0) { + accounts_set_server(account_name, value); + cons_show("Updated server for account %s: %s", account_name, value); + cons_show(""); + } else if (strcmp(property, "resource") == 0) { + accounts_set_resource(account_name, value); + cons_show("Updated resource for account %s: %s", account_name, value); + cons_show(""); + } else if (strcmp(property, "password") == 0) { + accounts_set_password(account_name, value); + cons_show("Updated password for account %s", account_name); + cons_show(""); + } else if (strcmp(property, "muc") == 0) { + accounts_set_muc_service(account_name, value); + cons_show("Updated muc service for account %s: %s", account_name, value); + cons_show(""); + } else if (strcmp(property, "nick") == 0) { + accounts_set_muc_nick(account_name, value); + cons_show("Updated muc nick for account %s: %s", account_name, value); + cons_show(""); + } else if (strcmp(property, "status") == 0) { + if (!valid_resource_presence_string(value) && (strcmp(value, "last") != 0)) { + cons_show("Invalid status: %s", value); + } else { + accounts_set_login_presence(account_name, value); + cons_show("Updated login status for account %s: %s", account_name, value); + } + cons_show(""); + } else if (valid_resource_presence_string(property)) { + int intval; + + if (_strtoi(value, &intval, -128, 127) == 0) { + resource_presence_t presence_type = resource_presence_from_string(property); + switch (presence_type) + { + case (RESOURCE_ONLINE): + accounts_set_priority_online(account_name, intval); + break; + case (RESOURCE_CHAT): + accounts_set_priority_chat(account_name, intval); + break; + case (RESOURCE_AWAY): + accounts_set_priority_away(account_name, intval); + break; + case (RESOURCE_XA): + accounts_set_priority_xa(account_name, intval); + break; + case (RESOURCE_DND): + accounts_set_priority_dnd(account_name, intval); + break; + } + jabber_conn_status_t conn_status = jabber_get_connection_status(); + resource_presence_t last_presence = accounts_get_last_presence(jabber_get_account_name()); + if (conn_status == JABBER_CONNECTED && presence_type == last_presence) { + presence_update(last_presence, jabber_get_presence_message(), 0); + } + cons_show("Updated %s priority for account %s: %s", property, account_name, value); + cons_show(""); + } + } else { + cons_show("Invalid property: %s", property); + cons_show(""); + } + } + } + } else if (strcmp(command, "clear") == 0) { + if (g_strv_length(args) != 3) { + cons_show("Usage: %s", help.usage); + } else { + char *account_name = args[1]; + char *property = args[2]; + + if (!accounts_account_exists(account_name)) { + cons_show("Account %s doesn't exist", account_name); + cons_show(""); + } else { + if (strcmp(property, "password") == 0) { + accounts_clear_password(account_name); + cons_show("Removed password for account %s", account_name); + cons_show(""); + } else { + cons_show("Invalid property: %s", property); + cons_show(""); + } + } + } + } else { + cons_show(""); + } + + return TRUE; +} + +gboolean +cmd_sub(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are currently not connected."); + return TRUE; + } + + char *subcmd, *jid; + subcmd = args[0]; + jid = args[1]; + + if (subcmd == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + if (strcmp(subcmd, "sent") == 0) { + cons_show_sent_subs(); + return TRUE; + } + + if (strcmp(subcmd, "received") == 0) { + cons_show_received_subs(); + return TRUE; + } + + if ((win_type != WIN_CHAT) && (jid == NULL)) { + cons_show("You must specify a contact."); + return TRUE; + } + + if (jid == NULL) { + jid = ui_current_recipient(); + } + + Jid *jidp = jid_create(jid); + + if (strcmp(subcmd, "allow") == 0) { + presence_subscription(jidp->barejid, PRESENCE_SUBSCRIBED); + cons_show("Accepted subscription for %s", jidp->barejid); + log_info("Accepted subscription for %s", jidp->barejid); + } else if (strcmp(subcmd, "deny") == 0) { + presence_subscription(jidp->barejid, PRESENCE_UNSUBSCRIBED); + cons_show("Deleted/denied subscription for %s", jidp->barejid); + log_info("Deleted/denied subscription for %s", jidp->barejid); + } else if (strcmp(subcmd, "request") == 0) { + presence_subscription(jidp->barejid, PRESENCE_SUBSCRIBE); + cons_show("Sent subscription request to %s.", jidp->barejid); + log_info("Sent subscription request to %s.", jidp->barejid); + } else if (strcmp(subcmd, "show") == 0) { + PContact contact = roster_get_contact(jidp->barejid); + if ((contact == NULL) || (p_contact_subscription(contact) == NULL)) { + if (win_type == WIN_CHAT) { + ui_current_print_line("No subscription information for %s.", jidp->barejid); + } else { + cons_show("No subscription information for %s.", jidp->barejid); + } + } else { + if (win_type == WIN_CHAT) { + if (p_contact_pending_out(contact)) { + ui_current_print_line("%s subscription status: %s, request pending.", + jidp->barejid, p_contact_subscription(contact)); + } else { + ui_current_print_line("%s subscription status: %s.", jidp->barejid, + p_contact_subscription(contact)); + } + } else { + if (p_contact_pending_out(contact)) { + cons_show("%s subscription status: %s, request pending.", + jidp->barejid, p_contact_subscription(contact)); + } else { + cons_show("%s subscription status: %s.", jidp->barejid, + p_contact_subscription(contact)); + } + } + } + } else { + cons_show("Usage: %s", help.usage); + } + + jid_destroy(jidp); + + return TRUE; +} + +gboolean +cmd_disconnect(gchar **args, struct cmd_help_t help) +{ + if (jabber_get_connection_status() == JABBER_CONNECTED) { + char *jid = strdup(jabber_get_fulljid()); + prof_handle_disconnect(jid); + free(jid); + } else { + cons_show("You are not currently connected."); + } + + return TRUE; +} + +gboolean +cmd_quit(gchar **args, struct cmd_help_t help) +{ + log_info("Profanity is shutting down..."); + exit(0); + return FALSE; +} + +gboolean +cmd_wins(gchar **args, struct cmd_help_t help) +{ + if (args[0] == NULL) { + cons_show_wins(); + } else if (strcmp(args[0], "tidy") == 0) { + ui_tidy_wins(); + } else if (strcmp(args[0], "prune") == 0) { + ui_prune_wins(); + } + return TRUE; +} + +gboolean +cmd_win(gchar **args, struct cmd_help_t help) +{ + int num = atoi(args[0]); + if (ui_win_exists(num)) { + ui_switch_win(num); + } else { + cons_show("Window %d does not exist.", num); + } + + return TRUE; +} + +gboolean +cmd_help(gchar **args, struct cmd_help_t help) +{ + int num_args = g_strv_length(args); + if (num_args == 0) { + cons_help(); + } else if (strcmp(args[0], "commands") == 0) { + cons_show(""); + cons_show("All commands"); + cons_show(""); + + GList *ordered_commands = NULL; + GHashTableIter iter; + gpointer key; + gpointer value; + + g_hash_table_iter_init(&iter, commands); + while (g_hash_table_iter_next(&iter, &key, &value)) { + ordered_commands = g_list_insert_sorted(ordered_commands, value, (GCompareFunc)_compare_commands); + } + + GList *curr = ordered_commands; + while (curr != NULL) { + Command *cmd = curr->data; + cons_show("%-12s: %s", cmd->cmd, cmd->help.short_help); + curr = g_list_next(curr); + } + g_list_free(ordered_commands); + g_list_free(curr); + + cons_show(""); + cons_show("Use /help [command] without the leading slash, for help on a specific command"); + cons_show(""); + + } else if (strcmp(args[0], "basic") == 0) { + gchar *filter[] = { "/about", "/clear", "/close", "/connect", + "/disconnect", "/help", "/msg", "/join", "/quit", "/vercheck", + "/wins" }; + _cmd_show_filtered_help("Basic commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "chatting") == 0) { + gchar *filter[] = { "/chlog", "/duck", "/gone", "/history", + "/info", "/intype", "/msg", "/notify", "/outtype", "/status", + "/close", "/clear", "/tiny" }; + _cmd_show_filtered_help("Chat commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "groupchat") == 0) { + gchar *filter[] = { "/close", "/clear", "/decline", "/grlog", + "/invite", "/invites", "/join", "/leave", "/notify", "/msg", + "/rooms", "/tiny", "/who", "/nick" }; + _cmd_show_filtered_help("Groupchat commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "presence") == 0) { + gchar *filter[] = { "/autoaway", "/away", "/chat", "/dnd", + "/online", "/priority", "/account", "/status", "/statuses", "/who", + "/xa" }; + _cmd_show_filtered_help("Presence commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "contacts") == 0) { + gchar *filter[] = { "/group", "/roster", "/sub", "/who" }; + _cmd_show_filtered_help("Roster commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "service") == 0) { + gchar *filter[] = { "/caps", "/disco", "/info", "/software", "/rooms" }; + _cmd_show_filtered_help("Service discovery commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "settings") == 0) { + gchar *filter[] = { "/account", "/autoaway", "/autoping", "/autoconnect", "/beep", + "/chlog", "/flash", "/gone", "/grlog", "/history", "/intype", + "/log", "/mouse", "/notify", "/outtype", "/prefs", "/priority", + "/reconnect", "/roster", "/splash", "/states", "/statuses", "/theme", + "/titlebar", "/vercheck" }; + _cmd_show_filtered_help("Settings commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "other") == 0) { + gchar *filter[] = { "/duck", "/vercheck" }; + _cmd_show_filtered_help("Other commands", filter, ARRAY_SIZE(filter)); + + } else if (strcmp(args[0], "navigation") == 0) { + cons_navigation_help(); + } else { + char *cmd = args[0]; + char cmd_with_slash[1 + strlen(cmd) + 1]; + sprintf(cmd_with_slash, "/%s", cmd); + + const gchar **help_text = NULL; + Command *command = g_hash_table_lookup(commands, cmd_with_slash); + + if (command != NULL) { + help_text = command->help.long_help; + } + + cons_show(""); + + if (help_text != NULL) { + int i; + for (i = 0; help_text[i] != NULL; i++) { + cons_show(help_text[i]); + } + } else { + cons_show("No such command."); + } + + cons_show(""); + } + + return TRUE; +} + +gboolean +cmd_about(gchar **args, struct cmd_help_t help) +{ + cons_show(""); + cons_about(); + if (ui_current_win_type() != WIN_CONSOLE) { + status_bar_new(1); + } + return TRUE; +} + +gboolean +cmd_prefs(gchar **args, struct cmd_help_t help) +{ + if (args[0] == NULL) { + cons_prefs(); + cons_show("Use the /account command for preferences for individual accounts."); + } else if (strcmp(args[0], "ui") == 0) { + cons_show(""); + cons_show_ui_prefs(); + cons_show(""); + } else if (strcmp(args[0], "desktop") == 0) { + cons_show(""); + cons_show_desktop_prefs(); + cons_show(""); + } else if (strcmp(args[0], "chat") == 0) { + cons_show(""); + cons_show_chat_prefs(); + cons_show(""); + } else if (strcmp(args[0], "log") == 0) { + cons_show(""); + cons_show_log_prefs(); + cons_show(""); + } else if (strcmp(args[0], "conn") == 0) { + cons_show(""); + cons_show_connection_prefs(); + cons_show(""); + } else if (strcmp(args[0], "presence") == 0) { + cons_show(""); + cons_show_presence_prefs(); + cons_show(""); + } else { + cons_show("Usage: %s", help.usage); + } + + return TRUE; +} + +gboolean +cmd_theme(gchar **args, struct cmd_help_t help) +{ + // list themes + if (strcmp(args[0], "list") == 0) { + GSList *themes = theme_list(); + cons_show_themes(themes); + g_slist_free_full(themes, g_free); + + // load a theme + } else if (strcmp(args[0], "set") == 0) { + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + } else if (theme_load(args[1])) { + ui_load_colours(); + prefs_set_string(PREF_THEME, args[1]); + cons_show("Loaded theme: %s", args[1]); + } else { + cons_show("Couldn't find theme: %s", args[1]); + } + } else { + cons_show("Usage: %s", help.usage); + } + + return TRUE; +} + +gboolean +cmd_who(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + } else { + char *presence = args[0]; + char *group = NULL; + if ((g_strv_length(args) == 2) && (args[1] != NULL)) { + group = args[1]; + } + + // bad arg + if ((presence != NULL) + && (strcmp(presence, "online") != 0) + && (strcmp(presence, "available") != 0) + && (strcmp(presence, "unavailable") != 0) + && (strcmp(presence, "offline") != 0) + && (strcmp(presence, "away") != 0) + && (strcmp(presence, "chat") != 0) + && (strcmp(presence, "xa") != 0) + && (strcmp(presence, "dnd") != 0) + && (strcmp(presence, "any") != 0)) { + cons_show("Usage: %s", help.usage); + + // valid arg + } else { + if (win_type == WIN_MUC) { + if (group != NULL) { + cons_show("The group argument is not valid when in a chat room."); + return TRUE; + } + + char *room = ui_current_recipient(); + GList *list = muc_get_roster(room); + + // no arg, show all contacts + if ((presence == NULL) || (g_strcmp0(presence, "any") == 0)) { + ui_room_roster(room, list, NULL); + + // available + } else if (strcmp("available", presence) == 0) { + GList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (p_contact_is_available(contact)) { + filtered = g_list_append(filtered, contact); + } + list = g_list_next(list); + } + + ui_room_roster(room, filtered, "available"); + + // unavailable + } else if (strcmp("unavailable", presence) == 0) { + GList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (!p_contact_is_available(contact)) { + filtered = g_list_append(filtered, contact); + } + list = g_list_next(list); + } + + ui_room_roster(room, filtered, "unavailable"); + + // online, available resources + } else if (strcmp("online", presence) == 0) { + GList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (p_contact_has_available_resource(contact)) { + filtered = g_list_append(filtered, contact); + } + list = g_list_next(list); + } + + ui_room_roster(room, filtered, "online"); + + // offline, no available resources + } else if (strcmp("offline", presence) == 0) { + GList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (!p_contact_has_available_resource(contact)) { + filtered = g_list_append(filtered, contact); + } + list = g_list_next(list); + } + + ui_room_roster(room, filtered, "offline"); + + // show specific status + } else { + GList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (strcmp(p_contact_presence(contact), presence) == 0) { + filtered = g_list_append(filtered, contact); + } + list = g_list_next(list); + } + + ui_room_roster(room, filtered, presence); + } + + // not in groupchat window + } else { + cons_show(""); + GSList *list = NULL; + if (group != NULL) { + list = roster_get_group(group); + } else { + list = roster_get_contacts(); + } + + // no arg, show all contacts + if ((presence == NULL) || (g_strcmp0(presence, "any") == 0)) { + if (group != NULL) { + cons_show("%s:", group); + } else { + cons_show("All contacts:"); + } + cons_show_contacts(list); + + // available + } else if (strcmp("available", presence) == 0) { + if (group != NULL) { + cons_show("%s (%s):", group, presence); + } else { + cons_show("Contacts (%s):", presence); + } + GSList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (p_contact_is_available(contact)) { + filtered = g_slist_append(filtered, contact); + } + list = g_slist_next(list); + } + + cons_show_contacts(filtered); + + // unavailable + } else if (strcmp("unavailable", presence) == 0) { + if (group != NULL) { + cons_show("%s (%s):", group, presence); + } else { + cons_show("Contacts (%s):", presence); + } + GSList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (!p_contact_is_available(contact)) { + filtered = g_slist_append(filtered, contact); + } + list = g_slist_next(list); + } + + cons_show_contacts(filtered); + + // online, available resources + } else if (strcmp("online", presence) == 0) { + if (group != NULL) { + cons_show("%s (%s):", group, presence); + } else { + cons_show("Contacts (%s):", presence); + } + GSList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (p_contact_has_available_resource(contact)) { + filtered = g_slist_append(filtered, contact); + } + list = g_slist_next(list); + } + + cons_show_contacts(filtered); + + // offline, no available resources + } else if (strcmp("offline", presence) == 0) { + if (group != NULL) { + cons_show("%s (%s):", group, presence); + } else { + cons_show("Contacts (%s):", presence); + } + GSList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (!p_contact_has_available_resource(contact)) { + filtered = g_slist_append(filtered, contact); + } + list = g_slist_next(list); + } + + cons_show_contacts(filtered); + + // show specific status + } else { + if (group != NULL) { + cons_show("%s (%s):", group, presence); + } else { + cons_show("Contacts (%s):", presence); + } + GSList *filtered = NULL; + + while (list != NULL) { + PContact contact = list->data; + if (strcmp(p_contact_presence(contact), presence) == 0) { + filtered = g_slist_append(filtered, contact); + } + list = g_slist_next(list); + } + + cons_show_contacts(filtered); + } + } + } + } + + if (win_type != WIN_CONSOLE && win_type != WIN_MUC) { + status_bar_new(1); + } + + return TRUE; +} + +gboolean +cmd_msg(gchar **args, struct cmd_help_t help) +{ + char *usr = args[0]; + char *msg = args[1]; + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + if (win_type == WIN_MUC) { + char *room_name = ui_current_recipient(); + if (muc_nick_in_roster(room_name, usr)) { + GString *full_jid = g_string_new(room_name); + g_string_append(full_jid, "/"); + g_string_append(full_jid, usr); + + if (msg != NULL) { + message_send(msg, full_jid->str); + ui_outgoing_msg("me", full_jid->str, msg); + } else { + ui_new_chat_win(full_jid->str); + } + + g_string_free(full_jid, TRUE); + + } else { + ui_current_print_line("No such participant \"%s\" in room.", usr); + } + + return TRUE; + + } else { + char *usr_jid = roster_barejid_from_name(usr); + if (usr_jid == NULL) { + usr_jid = usr; + } + if (msg != NULL) { + message_send(msg, usr_jid); + ui_outgoing_msg("me", usr_jid, msg); + + if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL); + jid_destroy(jidp); + } + + return TRUE; + } else { + const char * jid = NULL; + + if (roster_barejid_from_name(usr_jid) != NULL) { + jid = roster_barejid_from_name(usr_jid); + } else { + jid = usr_jid; + } + + if (prefs_get_boolean(PREF_STATES)) { + if (!chat_session_exists(jid)) { + chat_session_start(jid, TRUE); + } + } + + ui_new_chat_win(usr_jid); + return TRUE; + } + } +} + +gboolean +cmd_group(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + // list all groups + if (args[0] == NULL) { + GSList *groups = roster_get_groups(); + GSList *curr = groups; + if (curr != NULL) { + cons_show("Groups:"); + while (curr != NULL) { + cons_show(" %s", curr->data); + curr = g_slist_next(curr); + } + + g_slist_free_full(groups, g_free); + } else { + cons_show("No groups."); + } + return TRUE; + } + + // show contacts in group + if (strcmp(args[0], "show") == 0) { + char *group = args[1]; + if (group == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + GSList *list = roster_get_group(group); + cons_show_roster_group(group, list); + return TRUE; + } + + // add contact to group + if (strcmp(args[0], "add") == 0) { + char *group = args[1]; + char *contact = args[2]; + + if ((group == NULL) || (contact == NULL)) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *barejid = roster_barejid_from_name(contact); + if (barejid == NULL) { + barejid = contact; + } + + PContact pcontact = roster_get_contact(barejid); + if (pcontact == NULL) { + cons_show("Contact not found in roster: %s", barejid); + return TRUE; + } + + roster_add_to_group(group, barejid); + + return TRUE; + } + + // remove contact from group + if (strcmp(args[0], "remove") == 0) { + char *group = args[1]; + char *contact = args[2]; + + if ((group == NULL) || (contact == NULL)) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *barejid = roster_barejid_from_name(contact); + if (barejid == NULL) { + barejid = contact; + } + + PContact pcontact = roster_get_contact(barejid); + if (pcontact == NULL) { + cons_show("Contact not found in roster: %s", barejid); + return TRUE; + } + + roster_remove_from_group(group, barejid); + + return TRUE; + } + + cons_show("Usage: %s", help.usage); + return TRUE; +} + +gboolean +cmd_roster(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + // show roster + if (args[0] == NULL) { + GSList *list = roster_get_contacts(); + cons_show_roster(list); + return TRUE; + } + + // add contact + if (strcmp(args[0], "add") == 0) { + + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + char *name = args[2]; + + roster_add_new(jid, name); + + return TRUE; + } + + // remove contact + if (strcmp(args[0], "remove") == 0) { + + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + + roster_send_remove(jid); + + return TRUE; + } + + // change nickname + if (strcmp(args[0], "nick") == 0) { + + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + char *name = args[2]; + + // contact does not exist + PContact contact = roster_get_contact(jid); + if (contact == NULL) { + cons_show("Contact not found in roster: %s", jid); + return TRUE; + } + + roster_change_name(jid, name); + + if (name == NULL) { + cons_show("Nickname for %s removed.", jid); + } else { + cons_show("Nickname for %s set to: %s.", jid, name); + } + + return TRUE; + } + + cons_show("Usage: %s", help.usage); + return TRUE; +} + +gboolean +cmd_duck(gchar **args, struct cmd_help_t help) +{ + char *query = args[0]; + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + // if no duck win open, create it and send a help command + if (!ui_duck_exists()) { + ui_create_duck_win(); + + if (query != NULL) { + message_send_duck(query); + ui_duck(query); + } + + // window exists, send query + } else { + ui_open_duck_win(); + + if (query != NULL) { + message_send_duck(query); + ui_duck(query); + } + } + + return TRUE; +} + +gboolean +cmd_status(gchar **args, struct cmd_help_t help) +{ + char *usr = args[0]; + char *usr_jid = NULL; + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + switch (win_type) + { + case WIN_MUC: + if (usr != NULL) { + ui_status_room(usr); + } else { + ui_current_print_line("You must specify a nickname."); + } + break; + case WIN_CHAT: + if (usr != NULL) { + ui_current_print_line("No parameter required when in chat."); + } else { + ui_status(); + } + break; + case WIN_PRIVATE: + if (usr != NULL) { + ui_current_print_line("No parameter required when in chat."); + } else { + ui_status_private(); + } + break; + case WIN_CONSOLE: + if (usr != NULL) { + usr_jid = roster_barejid_from_name(usr); + if (usr_jid == NULL) { + usr_jid = usr; + } + cons_show_status(usr_jid); + } else { + cons_show("Usage: %s", help.usage); + } + break; + default: + break; + } + + return TRUE; +} + +gboolean +cmd_info(gchar **args, struct cmd_help_t help) +{ + char *usr = args[0]; + char *usr_jid = NULL; + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + PContact pcontact = NULL; + char *recipient; + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + recipient = ui_current_recipient(); + + switch (win_type) + { + case WIN_MUC: + if (usr != NULL) { + pcontact = muc_get_participant(recipient, usr); + if (pcontact != NULL) { + cons_show_info(pcontact); + } else { + cons_show("No such participant \"%s\" in room.", usr); + } + } else { + cons_show("No nickname supplied to /info in chat room."); + } + break; + case WIN_CHAT: + if (usr != NULL) { + cons_show("No parameter required for /info in chat."); + } else { + pcontact = roster_get_contact(recipient); + if (pcontact != NULL) { + cons_show_info(pcontact); + } else { + cons_show("No such contact \"%s\" in roster.", recipient); + } + } + break; + case WIN_PRIVATE: + if (usr != NULL) { + ui_current_print_line("No parameter required when in chat."); + } else { + Jid *jid = jid_create(recipient); + pcontact = muc_get_participant(jid->barejid, jid->resourcepart); + if (pcontact != NULL) { + cons_show_info(pcontact); + } else { + cons_show("No such participant \"%s\" in room.", jid->resourcepart); + } + jid_destroy(jid); + } + break; + case WIN_CONSOLE: + if (usr != NULL) { + usr_jid = roster_barejid_from_name(usr); + if (usr_jid == NULL) { + usr_jid = usr; + } + pcontact = roster_get_contact(usr_jid); + if (pcontact != NULL) { + cons_show_info(pcontact); + } else { + cons_show("No such contact \"%s\" in roster.", usr); + } + } else { + cons_show("Usage: %s", help.usage); + } + break; + default: + break; + } + + return TRUE; +} + +gboolean +cmd_caps(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + PContact pcontact = NULL; + char *recipient; + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + switch (win_type) + { + case WIN_MUC: + if (args[0] != NULL) { + recipient = ui_current_recipient(); + pcontact = muc_get_participant(recipient, args[0]); + if (pcontact != NULL) { + Resource *resource = p_contact_get_resource(pcontact, args[0]); + cons_show_caps(args[0], resource); + } else { + cons_show("No such participant \"%s\" in room.", args[0]); + } + } else { + cons_show("No nickname supplied to /caps in chat room."); + } + break; + case WIN_CHAT: + case WIN_CONSOLE: + if (args[0] != NULL) { + Jid *jid = jid_create(args[0]); + + if (jid->fulljid == NULL) { + cons_show("You must provide a full jid to the /caps command."); + } else { + pcontact = roster_get_contact(jid->barejid); + if (pcontact == NULL) { + cons_show("Contact not found in roster: %s", jid->barejid); + } else { + Resource *resource = p_contact_get_resource(pcontact, jid->resourcepart); + if (resource == NULL) { + cons_show("Could not find resource %s, for contact %s", jid->barejid, jid->resourcepart); + } else { + cons_show_caps(jid->fulljid, resource); + } + } + } + } else { + cons_show("You must provide a jid to the /caps command."); + } + break; + case WIN_PRIVATE: + if (args[0] != NULL) { + cons_show("No parameter needed to /caps when in private chat."); + } else { + recipient = ui_current_recipient(); + Jid *jid = jid_create(recipient); + if (jid) { + pcontact = muc_get_participant(jid->barejid, jid->resourcepart); + Resource *resource = p_contact_get_resource(pcontact, jid->resourcepart); + cons_show_caps(jid->resourcepart, resource); + jid_destroy(jid); + } + } + break; + default: + break; + } + + return TRUE; +} + + +gboolean +cmd_software(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + PContact pcontact = NULL; + char *recipient; + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + switch (win_type) + { + case WIN_MUC: + if (args[0] != NULL) { + recipient = ui_current_recipient(); + pcontact = muc_get_participant(recipient, args[0]); + if (pcontact != NULL) { + Jid *jid = jid_create_from_bare_and_resource(recipient, args[0]); + iq_send_software_version(jid->fulljid); + jid_destroy(jid); + } else { + cons_show("No such participant \"%s\" in room.", args[0]); + } + } else { + cons_show("No nickname supplied to /software in chat room."); + } + break; + case WIN_CHAT: + case WIN_CONSOLE: + if (args[0] != NULL) { + Jid *jid = jid_create(args[0]); + + if (jid == NULL || jid->fulljid == NULL) { + cons_show("You must provide a full jid to the /software command."); + } else { + iq_send_software_version(jid->fulljid); + } + jid_destroy(jid); + } else { + cons_show("You must provide a jid to the /software command."); + } + break; + case WIN_PRIVATE: + if (args[0] != NULL) { + cons_show("No parameter needed to /software when in private chat."); + } else { + recipient = ui_current_recipient(); + iq_send_software_version(recipient); + } + break; + default: + break; + } + + return TRUE; +} + +gboolean +cmd_join(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + ProfAccount *account = accounts_get_account(jabber_get_account_name()); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + Jid *room_arg = jid_create(args[0]); + if (room_arg == NULL) { + cons_show_error("Specified room has incorrect format"); + return TRUE; + } + + int num_args = g_strv_length(args); + char *room = NULL; + char *nick = NULL; + GString *room_str = g_string_new(""); + Jid *my_jid = jid_create(jabber_get_fulljid()); + + // full room jid supplied (room@server) + if (room_arg->localpart != NULL) { + room = args[0]; + + // server not supplied (room), use account preference + } else { + g_string_append(room_str, args[0]); + g_string_append(room_str, "@"); + g_string_append(room_str, account->muc_service); + room = room_str->str; + } + + // nick supplied + if (num_args == 2) { + nick = args[1]; + + // otherwise use account preference + } else { + nick = account->muc_nick; + } + + Jid *room_jid = jid_create_from_bare_and_resource(room, nick); + + if (!muc_room_is_active(room_jid)) { + presence_join_room(room_jid); + } + ui_room_join(room_jid); + muc_remove_invite(room); + + jid_destroy(room_arg); + jid_destroy(room_jid); + jid_destroy(my_jid); + g_string_free(room_str, TRUE); + accounts_free_account(account); + + return TRUE; +} + +gboolean +cmd_invite(gchar **args, struct cmd_help_t help) +{ + char *contact = args[0]; + char *reason = args[1]; + char *room = NULL; + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + if (ui_current_win_type() != WIN_MUC) { + cons_show("You must be in a chat room to send an invite."); + return TRUE; + } + + char *usr_jid = roster_barejid_from_name(contact); + if (usr_jid == NULL) { + usr_jid = contact; + } + room = ui_current_recipient(); + message_send_invite(room, usr_jid, reason); + if (reason != NULL) { + cons_show("Room invite sent, contact: %s, room: %s, reason: \"%s\".", + contact, room, reason); + } else { + cons_show("Room invite sent, contact: %s, room: %s.", + contact, room); + } + + return TRUE; +} + +gboolean +cmd_invites(gchar **args, struct cmd_help_t help) +{ + GSList *invites = muc_get_invites(); + cons_show_room_invites(invites); + g_slist_free_full(invites, g_free); + return TRUE; +} + +gboolean +cmd_decline(gchar **args, struct cmd_help_t help) +{ + if (!muc_invites_include(args[0])) { + cons_show("No such invite exists."); + } else { + muc_remove_invite(args[0]); + cons_show("Declined invite to %s.", args[0]); + } + + return TRUE; +} + +gboolean +cmd_rooms(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + if (args[0] == NULL) { + ProfAccount *account = accounts_get_account(jabber_get_account_name()); + iq_room_list_request(account->muc_service); + } else { + iq_room_list_request(args[0]); + } + + return TRUE; +} + +gboolean +cmd_bookmark(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + gchar *cmd = args[0]; + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currenlty connect."); + return TRUE; + } + + /* TODO: /bookmark list room@server */ + + if (cmd == NULL || strcmp(cmd, "list") == 0) { + cons_show_bookmarks(bookmark_get_list()); + } else { + gboolean autojoin = FALSE; + gchar *jid = NULL; + gchar *nick = NULL; + int idx = 1; + + while (args[idx] != NULL) { + gchar *opt = args[idx]; + + if (strcmp(opt, "autojoin") == 0) { + autojoin = TRUE; + } else if (jid == NULL) { + jid = opt; + } else if (nick == NULL) { + nick = opt; + } else { + cons_show("Usage: %s", help.usage); + } + + ++idx; + } + + if (jid == NULL) { + win_type_t win_type = ui_current_win_type(); + + if (win_type == WIN_MUC) { + jid = ui_current_recipient(); + nick = muc_get_room_nick(jid); + } else { + cons_show("Usage: %s", help.usage); + return TRUE; + } + } + + if (strcmp(cmd, "add") == 0) { + bookmark_add(jid, nick, autojoin); + } else if (strcmp(cmd, "remove") == 0) { + bookmark_remove(jid, autojoin); + } else { + cons_show("Usage: %s", help.usage); + } + } + + return TRUE; +} + +gboolean +cmd_disco(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currenlty connected."); + return TRUE; + } + + GString *jid = g_string_new(""); + if (args[1] != NULL) { + jid = g_string_append(jid, args[1]); + } else { + Jid *jidp = jid_create(jabber_get_fulljid()); + jid = g_string_append(jid, jidp->domainpart); + jid_destroy(jidp); + } + + if (g_strcmp0(args[0], "info") == 0) { + iq_disco_info_request(jid->str); + } else { + iq_disco_items_request(jid->str); + } + + g_string_free(jid, TRUE); + + return TRUE; +} + +gboolean +cmd_nick(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + if (ui_current_win_type() != WIN_MUC) { + cons_show("You can only change your nickname in a chat room window."); + return TRUE; + } + + char *room = ui_current_recipient(); + char *nick = args[0]; + presence_change_room_nick(room, nick); + + return TRUE; +} + +gboolean +cmd_tiny(gchar **args, struct cmd_help_t help) +{ + char *url = args[0]; + win_type_t win_type = ui_current_win_type(); + + if (!tinyurl_valid(url)) { + GString *error = g_string_new("/tiny, badly formed URL: "); + g_string_append(error, url); + cons_show_error(error->str); + if (win_type != WIN_CONSOLE) { + ui_current_error_line(error->str); + } + g_string_free(error, TRUE); + } else if (win_type != WIN_CONSOLE) { + char *tiny = tinyurl_get(url); + + if (tiny != NULL) { + if (win_type == WIN_CHAT) { + char *recipient = ui_current_recipient(); + message_send(tiny, recipient); + + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + chat_log_chat(jidp->barejid, recipient, tiny, PROF_OUT_LOG, NULL); + jid_destroy(jidp); + } + + ui_outgoing_msg("me", recipient, tiny); + } else if (win_type == WIN_PRIVATE) { + char *recipient = ui_current_recipient(); + message_send(tiny, recipient); + ui_outgoing_msg("me", recipient, tiny); + } else { // groupchat + char *recipient = ui_current_recipient(); + message_send_groupchat(tiny, recipient); + } + free(tiny); + } else { + cons_show_error("Couldn't get tinyurl."); + } + } else { + cons_show("/tiny can only be used in chat windows"); + } + + return TRUE; +} + +gboolean +cmd_clear(gchar **args, struct cmd_help_t help) +{ + ui_clear_current(); + return TRUE; +} + +gboolean +cmd_close(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + int index = 0; + int count = 0; + + if (args[0] == NULL) { + index = ui_current_win_index(); + } else if (strcmp(args[0], "all") == 0) { + count = ui_close_all_wins(); + if (count == 0) { + cons_show("No windows to close."); + } else if (count == 1) { + cons_show("Closed 1 window."); + } else { + cons_show("Closed %d windows.", count); + } + return TRUE; + } else if (strcmp(args[0], "read") == 0) { + count = ui_close_read_wins(); + if (count == 0) { + cons_show("No windows to close."); + } else if (count == 1) { + cons_show("Closed 1 window."); + } else { + cons_show("Closed %d windows.", count); + } + return TRUE; + } else { + index = atoi(args[0]); + } + + if (index < 0 || index == 10) { + cons_show("No such window exists."); + return TRUE; + } + + if (index == 1) { + cons_show("Cannot close console window."); + return TRUE; + } + + if (index == 0) { + index = 10; + } + + if (!ui_win_exists(index)) { + cons_show("Window is not open."); + return TRUE; + } + + // handle leaving rooms, or chat + if (conn_status == JABBER_CONNECTED) { + ui_close_connected_win(index); + } + + // close the window + ui_close_win(index); + cons_show("Closed window %d", index); + + return TRUE; +} + +gboolean +cmd_leave(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + win_type_t win_type = ui_current_win_type(); + int index = ui_current_win_index(); + + if (win_type != WIN_MUC) { + cons_show("You can only use the /leave command in a chat room."); + cons_alert(); + return TRUE; + } + + // handle leaving rooms, or chat + if (conn_status == JABBER_CONNECTED) { + ui_close_connected_win(index); + } + + // close the window + ui_close_win(index); + + return TRUE; +} + +gboolean +cmd_beep(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, "Sound", PREF_BEEP); +} + +gboolean +cmd_states(gchar **args, struct cmd_help_t help) +{ + gboolean result = _cmd_set_boolean_preference(args[0], help, "Sending chat states", + PREF_STATES); + + // if disabled, disable outtype and gone + if (result == TRUE && (strcmp(args[0], "off") == 0)) { + prefs_set_boolean(PREF_OUTTYPE, FALSE); + prefs_set_gone(0); + } + + return result; +} + +gboolean +cmd_titlebar(gchar **args, struct cmd_help_t help) +{ + if (strcmp(args[0], "version") != 0) { + cons_show("Usage: %s", help.usage); + return TRUE; + } else { + return _cmd_set_boolean_preference(args[1], help, + "Show version in window title", PREF_TITLEBARVERSION); + } +} + +gboolean +cmd_outtype(gchar **args, struct cmd_help_t help) +{ + gboolean result = _cmd_set_boolean_preference(args[0], help, + "Sending typing notifications", PREF_OUTTYPE); + + // if enabled, enable states + if (result == TRUE && (strcmp(args[0], "on") == 0)) { + prefs_set_boolean(PREF_STATES, TRUE); + } + + return result; +} + +gboolean +cmd_gone(gchar **args, struct cmd_help_t help) +{ + char *value = args[0]; + + gint period = atoi(value); + prefs_set_gone(period); + if (period == 0) { + cons_show("Automatic leaving conversations after period disabled."); + } else if (period == 1) { + cons_show("Leaving conversations after 1 minute of inactivity."); + } else { + cons_show("Leaving conversations after %d minutes of inactivity.", period); + } + + // if enabled, enable states + if (period > 0) { + prefs_set_boolean(PREF_STATES, TRUE); + } + + return TRUE; +} + + +gboolean +cmd_notify(gchar **args, struct cmd_help_t help) +{ + char *kind = args[0]; + char *value = args[1]; + + // bad kind + if ((strcmp(kind, "message") != 0) && (strcmp(kind, "typing") != 0) && + (strcmp(kind, "remind") != 0) && (strcmp(kind, "invite") != 0) && + (strcmp(kind, "sub") != 0)) { + cons_show("Usage: %s", help.usage); + + // set message setting + } else if (strcmp(kind, "message") == 0) { + if (strcmp(value, "on") == 0) { + cons_show("Message notifications enabled."); + prefs_set_boolean(PREF_NOTIFY_MESSAGE, TRUE); + } else if (strcmp(value, "off") == 0) { + cons_show("Message notifications disabled."); + prefs_set_boolean(PREF_NOTIFY_MESSAGE, FALSE); + } else { + cons_show("Usage: /notify message on|off"); + } + + // set typing setting + } else if (strcmp(kind, "typing") == 0) { + if (strcmp(value, "on") == 0) { + cons_show("Typing notifications enabled."); + prefs_set_boolean(PREF_NOTIFY_TYPING, TRUE); + } else if (strcmp(value, "off") == 0) { + cons_show("Typing notifications disabled."); + prefs_set_boolean(PREF_NOTIFY_TYPING, FALSE); + } else { + cons_show("Usage: /notify typing on|off"); + } + + // set invite setting + } else if (strcmp(kind, "invite") == 0) { + if (strcmp(value, "on") == 0) { + cons_show("Chat room invite notifications enabled."); + prefs_set_boolean(PREF_NOTIFY_INVITE, TRUE); + } else if (strcmp(value, "off") == 0) { + cons_show("Chat room invite notifications disabled."); + prefs_set_boolean(PREF_NOTIFY_INVITE, FALSE); + } else { + cons_show("Usage: /notify invite on|off"); + } + + // set subscription setting + } else if (strcmp(kind, "sub") == 0) { + if (strcmp(value, "on") == 0) { + cons_show("Subscription notifications enabled."); + prefs_set_boolean(PREF_NOTIFY_SUB, TRUE); + } else if (strcmp(value, "off") == 0) { + cons_show("Subscription notifications disabled."); + prefs_set_boolean(PREF_NOTIFY_SUB, FALSE); + } else { + cons_show("Usage: /notify sub on|off"); + } + + // set remind setting + } else if (strcmp(kind, "remind") == 0) { + gint period = atoi(value); + prefs_set_notify_remind(period); + if (period == 0) { + cons_show("Message reminders disabled."); + } else if (period == 1) { + cons_show("Message reminder period set to 1 second."); + } else { + cons_show("Message reminder period set to %d seconds.", period); + } + + } else { + cons_show("Unknown command: %s.", kind); + } + + return TRUE; +} + +gboolean +cmd_log(gchar **args, struct cmd_help_t help) +{ + char *subcmd = args[0]; + char *value = args[1]; + int intval; + + if (strcmp(subcmd, "maxsize") == 0) { + if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) { + prefs_set_max_log_size(intval); + cons_show("Log maxinum size set to %d bytes", intval); + } + } else { + cons_show("Usage: %s", help.usage); + } + + /* TODO: make 'level' subcommand for debug level */ + + return TRUE; +} + +gboolean +cmd_reconnect(gchar **args, struct cmd_help_t help) +{ + char *value = args[0]; + int intval; + + if (_strtoi(value, &intval, 0, INT_MAX) == 0) { + prefs_set_reconnect(intval); + if (intval == 0) { + cons_show("Reconnect disabled.", intval); + } else { + cons_show("Reconnect interval set to %d seconds.", intval); + } + } else { + cons_show("Usage: %s", help.usage); + } + + return TRUE; +} + +gboolean +cmd_autoping(gchar **args, struct cmd_help_t help) +{ + char *value = args[0]; + int intval; + + if (_strtoi(value, &intval, 0, INT_MAX) == 0) { + prefs_set_autoping(intval); + jabber_set_autoping(intval); + if (intval == 0) { + cons_show("Autoping disabled.", intval); + } else { + cons_show("Autoping interval set to %d seconds.", intval); + } + } else { + cons_show("Usage: %s", help.usage); + } + + return TRUE; +} + +gboolean +cmd_autoaway(gchar **args, struct cmd_help_t help) +{ + char *setting = args[0]; + char *value = args[1]; + int minutesval; + + if ((strcmp(setting, "mode") != 0) && (strcmp(setting, "time") != 0) && + (strcmp(setting, "message") != 0) && (strcmp(setting, "check") != 0)) { + cons_show("Setting must be one of 'mode', 'time', 'message' or 'check'"); + return TRUE; + } + + if (strcmp(setting, "mode") == 0) { + if ((strcmp(value, "idle") != 0) && (strcmp(value, "away") != 0) && + (strcmp(value, "off") != 0)) { + cons_show("Mode must be one of 'idle', 'away' or 'off'"); + } else { + prefs_set_string(PREF_AUTOAWAY_MODE, value); + cons_show("Auto away mode set to: %s.", value); + } + + return TRUE; + } + + if (strcmp(setting, "time") == 0) { + if (_strtoi(value, &minutesval, 1, INT_MAX) == 0) { + prefs_set_autoaway_time(minutesval); + cons_show("Auto away time set to: %d minutes.", minutesval); + } + + return TRUE; + } + + if (strcmp(setting, "message") == 0) { + if (strcmp(value, "off") == 0) { + prefs_set_string(PREF_AUTOAWAY_MESSAGE, NULL); + cons_show("Auto away message cleared."); + } else { + prefs_set_string(PREF_AUTOAWAY_MESSAGE, value); + cons_show("Auto away message set to: \"%s\".", value); + } + + return TRUE; + } + + if (strcmp(setting, "check") == 0) { + return _cmd_set_boolean_preference(value, help, "Online check", + PREF_AUTOAWAY_CHECK); + } + + return TRUE; +} + +gboolean +cmd_priority(gchar **args, struct cmd_help_t help) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + char *value = args[0]; + int intval; + + if (_strtoi(value, &intval, -128, 127) == 0) { + accounts_set_priority_all(jabber_get_account_name(), intval); + resource_presence_t last_presence = accounts_get_last_presence(jabber_get_account_name()); + presence_update(last_presence, jabber_get_presence_message(), 0); + cons_show("Priority set to %d.", intval); + } + + return TRUE; +} + +gboolean +cmd_statuses(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, + "Status notifications", PREF_STATUSES); +} + +gboolean +cmd_vercheck(gchar **args, struct cmd_help_t help) +{ + int num_args = g_strv_length(args); + + if (num_args == 0) { + cons_check_version(TRUE); + return TRUE; + } else { + return _cmd_set_boolean_preference(args[0], help, + "Version checking", PREF_VERCHECK); + } +} + +gboolean +cmd_flash(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, + "Screen flash", PREF_FLASH); +} + +gboolean +cmd_intype(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, + "Show contact typing", PREF_INTYPE); +} + +gboolean +cmd_splash(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, + "Splash screen", PREF_SPLASH); +} + +gboolean +cmd_autoconnect(gchar **args, struct cmd_help_t help) +{ + if (strcmp(args[0], "off") == 0) { + prefs_set_string(PREF_CONNECT_ACCOUNT, NULL); + cons_show("Autoconnect account disabled."); + } else if (strcmp(args[0], "set") == 0) { + prefs_set_string(PREF_CONNECT_ACCOUNT, args[1]); + cons_show("Autoconnect account set to: %s.", args[1]); + } else { + cons_show("Usage: %s", help.usage); + } + return true; +} + +gboolean +cmd_chlog(gchar **args, struct cmd_help_t help) +{ + gboolean result = _cmd_set_boolean_preference(args[0], help, + "Chat logging", PREF_CHLOG); + + // if set to off, disable history + if (result == TRUE && (strcmp(args[0], "off") == 0)) { + prefs_set_boolean(PREF_HISTORY, FALSE); + } + + return result; +} + +gboolean +cmd_grlog(gchar **args, struct cmd_help_t help) +{ + gboolean result = _cmd_set_boolean_preference(args[0], help, + "Groupchat logging", PREF_GRLOG); + + return result; +} + +gboolean +cmd_mouse(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args[0], help, + "Mouse handling", PREF_MOUSE); +} + +gboolean +cmd_history(gchar **args, struct cmd_help_t help) +{ + gboolean result = _cmd_set_boolean_preference(args[0], help, + "Chat history", PREF_HISTORY); + + // if set to on, set chlog + if (result == TRUE && (strcmp(args[0], "on") == 0)) { + prefs_set_boolean(PREF_CHLOG, TRUE); + } + + return result; +} + +gboolean +cmd_away(gchar **args, struct cmd_help_t help) +{ + _update_presence(RESOURCE_AWAY, "away", args); + return TRUE; +} + +gboolean +cmd_online(gchar **args, struct cmd_help_t help) +{ + _update_presence(RESOURCE_ONLINE, "online", args); + return TRUE; +} + +gboolean +cmd_dnd(gchar **args, struct cmd_help_t help) +{ + _update_presence(RESOURCE_DND, "dnd", args); + return TRUE; +} + +gboolean +cmd_chat(gchar **args, struct cmd_help_t help) +{ + _update_presence(RESOURCE_CHAT, "chat", args); + return TRUE; +} + +gboolean +cmd_xa(gchar **args, struct cmd_help_t help) +{ + _update_presence(RESOURCE_XA, "xa", args); + return TRUE; +} + +// helper function that asks the user for a password and saves it in passwd +static char * +_ask_password(void) { + char *passwd = malloc(sizeof(char) * (MAX_PASSWORD_SIZE + 1)); + status_bar_get_password(); + status_bar_refresh(); + inp_block(); + inp_get_password(passwd); + inp_non_block(); + + return passwd; +} + +// helper function for status change commands +static void +_update_presence(const resource_presence_t resource_presence, + const char * const show, gchar **args) +{ + char *msg = NULL; + int num_args = g_strv_length(args); + if (num_args == 1) { + msg = args[0]; + } + + jabber_conn_status_t conn_status = jabber_get_connection_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + } else { + presence_update(resource_presence, msg, 0); + + contact_presence_t contact_presence = contact_presence_from_resource_presence(resource_presence); + title_bar_set_status(contact_presence); + + gint priority = accounts_get_priority_for_presence_type(jabber_get_account_name(), resource_presence); + if (msg != NULL) { + cons_show("Status set to %s (priority %d), \"%s\".", show, priority, msg); + } else { + cons_show("Status set to %s (priority %d).", show, priority); + } + } + +} + +// helper function for boolean preference commands +static gboolean +_cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, + const char * const display, preference_t pref) +{ + GString *enabled = g_string_new(display); + g_string_append(enabled, " enabled."); + + GString *disabled = g_string_new(display); + g_string_append(disabled, " disabled."); + + if (strcmp(arg, "on") == 0) { + cons_show(enabled->str); + prefs_set_boolean(pref, TRUE); + } else if (strcmp(arg, "off") == 0) { + cons_show(disabled->str); + prefs_set_boolean(pref, FALSE); + } else { + char usage[strlen(help.usage) + 8]; + sprintf(usage, "Usage: %s", help.usage); + cons_show(usage); + } + + g_string_free(enabled, TRUE); + g_string_free(disabled, TRUE); + + return TRUE; +} + +static int +_strtoi(char *str, int *saveptr, int min, int max) +{ + char *ptr; + int val; + + errno = 0; + val = (int)strtol(str, &ptr, 0); + if (*str == '\0' || *ptr != '\0') { + cons_show("Illegal character. Must be a number."); + return -1; + } else if (errno == ERANGE || val < min || val > max) { + cons_show("Value out of range. Must be in %d..%d.", min, max); + return -1; + } + + *saveptr = val; + + return 0; +} + +static void +_cmd_show_filtered_help(char *heading, gchar *cmd_filter[], int filter_size) +{ + cons_show(""); + cons_show("%s", heading); + cons_show(""); + + GList *ordered_commands = NULL; + int i; + for (i = 0; i < filter_size; i++) { + Command *cmd = g_hash_table_lookup(commands, cmd_filter[i]); + ordered_commands = g_list_insert_sorted(ordered_commands, cmd, (GCompareFunc)_compare_commands); + } + + GList *curr = ordered_commands; + while (curr != NULL) { + Command *cmd = curr->data; + cons_show("%-12s: %s", cmd->cmd, cmd->help.short_help); + curr = g_list_next(curr); + } + g_list_free(ordered_commands); + g_list_free(curr); + + cons_show(""); + cons_show("Use /help [command] without the leading slash, for help on a specific command"); + cons_show(""); +} + +static +gint _compare_commands(Command *a, Command *b) +{ + const char * utf8_str_a = a->cmd; + const char * utf8_str_b = b->cmd; + + gchar *key_a = g_utf8_collate_key(utf8_str_a, -1); + gchar *key_b = g_utf8_collate_key(utf8_str_b, -1); + + gint result = g_strcmp0(key_a, key_b); + + g_free(key_a); + g_free(key_b); + + return result; +} + diff --git a/src/command/commands.h b/src/command/commands.h new file mode 100644 index 00000000..aee74849 --- /dev/null +++ b/src/command/commands.h @@ -0,0 +1,112 @@ +/* + * commands.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#ifndef COMMANDS_H +#define COMMANDS_H + +// Command help strings +typedef struct cmd_help_t { + const gchar *usage; + const gchar *short_help; + const gchar *long_help[50]; +} CommandHelp; + +/* + * Command structure + * + * cmd - The command string including leading '/' + * func - The function to execute for the command + * parser - The function used to parse arguments + * min_args - Minimum number of arguments + * max_args - Maximum number of arguments + * help - A help struct containing usage info etc + */ +typedef struct cmd_t { + gchar *cmd; + gboolean (*func)(gchar **args, struct cmd_help_t help); + gchar** (*parser)(const char * const inp, int min, int max); + int min_args; + int max_args; + void (*setting_func)(void); + CommandHelp help; +} Command; + +gboolean cmd_about(gchar **args, struct cmd_help_t help); +gboolean cmd_account(gchar **args, struct cmd_help_t help); +gboolean cmd_autoaway(gchar **args, struct cmd_help_t help); +gboolean cmd_autoconnect(gchar **args, struct cmd_help_t help); +gboolean cmd_autoping(gchar **args, struct cmd_help_t help); +gboolean cmd_away(gchar **args, struct cmd_help_t help); +gboolean cmd_beep(gchar **args, struct cmd_help_t help); +gboolean cmd_caps(gchar **args, struct cmd_help_t help); +gboolean cmd_chat(gchar **args, struct cmd_help_t help); +gboolean cmd_chlog(gchar **args, struct cmd_help_t help); +gboolean cmd_clear(gchar **args, struct cmd_help_t help); +gboolean cmd_close(gchar **args, struct cmd_help_t help); +gboolean cmd_connect(gchar **args, struct cmd_help_t help); +gboolean cmd_decline(gchar **args, struct cmd_help_t help); +gboolean cmd_disco(gchar **args, struct cmd_help_t help); +gboolean cmd_disconnect(gchar **args, struct cmd_help_t help); +gboolean cmd_dnd(gchar **args, struct cmd_help_t help); +gboolean cmd_duck(gchar **args, struct cmd_help_t help); +gboolean cmd_flash(gchar **args, struct cmd_help_t help); +gboolean cmd_gone(gchar **args, struct cmd_help_t help); +gboolean cmd_grlog(gchar **args, struct cmd_help_t help); +gboolean cmd_group(gchar **args, struct cmd_help_t help); +gboolean cmd_help(gchar **args, struct cmd_help_t help); +gboolean cmd_history(gchar **args, struct cmd_help_t help); +gboolean cmd_info(gchar **args, struct cmd_help_t help); +gboolean cmd_intype(gchar **args, struct cmd_help_t help); +gboolean cmd_invite(gchar **args, struct cmd_help_t help); +gboolean cmd_invites(gchar **args, struct cmd_help_t help); +gboolean cmd_join(gchar **args, struct cmd_help_t help); +gboolean cmd_leave(gchar **args, struct cmd_help_t help); +gboolean cmd_log(gchar **args, struct cmd_help_t help); +gboolean cmd_mouse(gchar **args, struct cmd_help_t help); +gboolean cmd_msg(gchar **args, struct cmd_help_t help); +gboolean cmd_nick(gchar **args, struct cmd_help_t help); +gboolean cmd_notify(gchar **args, struct cmd_help_t help); +gboolean cmd_online(gchar **args, struct cmd_help_t help); +gboolean cmd_outtype(gchar **args, struct cmd_help_t help); +gboolean cmd_prefs(gchar **args, struct cmd_help_t help); +gboolean cmd_priority(gchar **args, struct cmd_help_t help); +gboolean cmd_quit(gchar **args, struct cmd_help_t help); +gboolean cmd_reconnect(gchar **args, struct cmd_help_t help); +gboolean cmd_rooms(gchar **args, struct cmd_help_t help); +gboolean cmd_bookmark(gchar **args, struct cmd_help_t help); +gboolean cmd_roster(gchar **args, struct cmd_help_t help); +gboolean cmd_software(gchar **args, struct cmd_help_t help); +gboolean cmd_splash(gchar **args, struct cmd_help_t help); +gboolean cmd_states(gchar **args, struct cmd_help_t help); +gboolean cmd_status(gchar **args, struct cmd_help_t help); +gboolean cmd_statuses(gchar **args, struct cmd_help_t help); +gboolean cmd_sub(gchar **args, struct cmd_help_t help); +gboolean cmd_theme(gchar **args, struct cmd_help_t help); +gboolean cmd_tiny(gchar **args, struct cmd_help_t help); +gboolean cmd_titlebar(gchar **args, struct cmd_help_t help); +gboolean cmd_vercheck(gchar **args, struct cmd_help_t help); +gboolean cmd_who(gchar **args, struct cmd_help_t help); +gboolean cmd_win(gchar **args, struct cmd_help_t help); +gboolean cmd_wins(gchar **args, struct cmd_help_t help); +gboolean cmd_xa(gchar **args, struct cmd_help_t help); + +#endif diff --git a/src/profanity.c b/src/profanity.c index d712d426..7b156cd3 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -687,7 +687,7 @@ _shutdown(void) prefs_close(); theme_close(); accounts_close(); - cmd_close(); + cmd_uninit(); log_close(); } diff --git a/tests/test_command.c b/tests/test_command.c index 5469cd44..6dc51ecb 100644 --- a/tests/test_command.c +++ b/tests/test_command.c @@ -7,7 +7,7 @@ #include "xmpp/xmpp.h" #include "ui/ui.h" -#include "command/command.h" +#include "command/commands.h" static void test_with_connection_status(jabber_conn_status_t status) { @@ -16,7 +16,7 @@ static void test_with_connection_status(jabber_conn_status_t status) will_return(jabber_get_connection_status, status); expect_string(cons_show, msg, "You are not currently connected."); - gboolean result = _cmd_rooms(NULL, *help); + gboolean result = cmd_rooms(NULL, *help); assert_true(result); free(help); @@ -59,7 +59,7 @@ void cmd_rooms_uses_account_default_when_no_arg(void **state) will_return(accounts_get_account, account); expect_string(iq_room_list_request, conferencejid, "default_conf_server"); - gboolean result = _cmd_rooms(args, *help); + gboolean result = cmd_rooms(args, *help); assert_true(result); @@ -75,7 +75,7 @@ void cmd_arg_used_when_passed(void **state) will_return(jabber_get_connection_status, JABBER_CONNECTED); expect_string(iq_room_list_request, conferencejid, "conf_server_arg"); - gboolean result = _cmd_rooms(args, *help); + gboolean result = cmd_rooms(args, *help); assert_true(result);