diff --git a/cygwin b/cygwin deleted file mode 100644 index f6f2c02a..00000000 --- a/cygwin +++ /dev/null @@ -1,40 +0,0 @@ -Install cygwin: - -http://www.cygwin.com/ -http://cygwin.com/setup.exe - -Choose git and wget - -wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg -chmod +x apt-cyg -mv apt-cyg /usr/local/bin/ - -apt-cyg install make gcc automake autoconf pkg-config openssl-devel expat zlib-devel libncurses-devel libncurses-devel libxml2-devel libglib2.0-devel libcurl-devel libidn-devel libssh2-devel libkrb5-devel openldap-devel -ln -s /usr/bin/gcc-3.exe /usr/bin/gcc.exe -ln -s /usr/bin/g++-3.exe /usr/bin/g++.exe - -mkdir projects-git -git clone git://github.com/boothj5/profanity.git -cd profanity - -git clone git://github.com/boothj5/head-unit.git -cd head-unit -make -make install - -cd .. - -git clone git://github.com/metajack/libstrophe.git -cd libstrophe -./bootstrap.sh -./bootstrap.sh -./configure -make -make install - -cd .. - -./bootstrap.sh -./configure -make -make install diff --git a/docs/profanity.1 b/docs/profanity.1 index 9f53c36a..f570b3fc 100644 --- a/docs/profanity.1 +++ b/docs/profanity.1 @@ -155,3 +155,5 @@ with contributions from: Dolan O'Toole .br Colin Bradley +.br +Dmitry Podgorny diff --git a/maketags.sh b/maketags.sh deleted file mode 100755 index 679c7134..00000000 --- a/maketags.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -rm -f tags -rm -f cscope.out -ctags -R . -cscope -R -b diff --git a/runvalgrind.sh b/runvalgrind.sh deleted file mode 100755 index 985d69a6..00000000 --- a/runvalgrind.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -rm -f valgrind.out -#valgrind --log-file=valgrind.out --leak-check=full --track-origins=yes --show-reachable=yes ./profanity -valgrind --log-file=valgrind.out --leak-check=full --track-origins=yes ./profanity -l DEBUG diff --git a/src/chat_log.c b/src/chat_log.c index 5d15d8e4..2e53dfc7 100644 --- a/src/chat_log.c +++ b/src/chat_log.c @@ -1,8 +1,8 @@ -/* +/* * chat_log.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -44,25 +44,25 @@ static gboolean _log_roll_needed(struct dated_chat_log *dated_log); static struct dated_chat_log *_create_log(char *other, const char * const login); static void _free_chat_log(struct dated_chat_log *dated_log); static gboolean _key_equals(void *key1, void *key2); -static char * _get_log_filename(const char * const other, const char * const login, +static char * _get_log_filename(const char * const other, const char * const login, GDateTime *dt, gboolean create); void chat_log_init(void) -{ +{ session_started = g_date_time_new_now_local(); log_info("Initialising chat logs"); - logs = g_hash_table_new_full(g_str_hash, (GEqualFunc) _key_equals, g_free, + logs = g_hash_table_new_full(g_str_hash, (GEqualFunc) _key_equals, g_free, (GDestroyNotify)_free_chat_log); } void -chat_log_chat(const gchar * const login, gchar *other, +chat_log_chat(const gchar * const login, gchar *other, const gchar * const msg, chat_log_direction_t direction) { gchar *other_copy = strdup(other); struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other_copy); - + // no log for user if (dated_log == NULL) { dated_log = _create_log(other_copy, login); @@ -94,29 +94,29 @@ chat_log_chat(const gchar * const login, gchar *other, } GSList * -chat_log_get_previous(const gchar * const login, const gchar * const recipient, +chat_log_get_previous(const gchar * const login, const gchar * const recipient, GSList *history) { GTimeZone *tz = g_time_zone_new_local(); - + GDateTime *now = g_date_time_new_now_local(); - GDateTime *log_date = g_date_time_new(tz, + GDateTime *log_date = g_date_time_new(tz, g_date_time_get_year(session_started), - g_date_time_get_month(session_started), + g_date_time_get_month(session_started), g_date_time_get_day_of_month(session_started), g_date_time_get_hour(session_started), g_date_time_get_minute(session_started), g_date_time_get_second(session_started)); - + // get data from all logs from the day the session was started to today while (g_date_time_get_day_of_year(log_date) <= g_date_time_get_day_of_year(now)) { char *filename = _get_log_filename(recipient, login, log_date, FALSE); - + FILE *logp = fopen(filename, "r"); char *line = NULL; size_t read = 0; if (logp != NULL) { - GString *gs_header = g_string_new("Log "); + GString *gs_header = g_string_new(""); g_string_append_printf(gs_header, "%d/%d/%d:", g_date_time_get_day_of_month(log_date), g_date_time_get_month(log_date), @@ -136,10 +136,10 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient, read = 0; length = getline(&line, &read, logp); } - + fclose(logp); } - + free(filename); GDateTime *next = g_date_time_add_days(log_date, 1); g_date_time_unref(log_date); @@ -208,12 +208,12 @@ gboolean _key_equals(void *key1, void *key2) { gchar *str1 = (gchar *) key1; gchar *str2 = (gchar *) key2; - + return (g_strcmp0(str1, str2) == 0); } static char * -_get_log_filename(const char * const other, const char * const login, +_get_log_filename(const char * const other, const char * const login, GDateTime *dt, gboolean create) { GString *log_file = g_string_new(getenv("HOME")); diff --git a/src/chat_log.h b/src/chat_log.h index 7071da55..238e087f 100644 --- a/src/chat_log.h +++ b/src/chat_log.h @@ -1,8 +1,8 @@ -/* +/* * chat_log.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -31,10 +31,10 @@ typedef enum { } chat_log_direction_t; void chat_log_init(void); -void chat_log_chat(const gchar * const login, gchar *other, +void chat_log_chat(const gchar * const login, gchar *other, const gchar * const msg, chat_log_direction_t direction); void chat_log_close(void); -GSList * chat_log_get_previous(const gchar * const login, +GSList * chat_log_get_previous(const gchar * const login, const gchar * const recipient, GSList *history); #endif diff --git a/src/command.c b/src/command.c index 5c8e30fa..79b92ffc 100644 --- a/src/command.c +++ b/src/command.c @@ -1,8 +1,8 @@ -/* +/* * command.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -38,7 +38,7 @@ #include "tinyurl.h" #include "ui.h" -/* +/* * Command structure * * cmd - The command string including leading '/' @@ -52,11 +52,10 @@ struct cmd_t { }; static struct cmd_t * _cmd_get_command(const char * const command); -static void _update_presence(const jabber_presence_t presence, +static void _update_presence(const jabber_presence_t presence, const char * const show, const char * const inp); -static gboolean -_cmd_set_boolean_preference(const char * const inp, struct cmd_help_t help, - const char * const cmd_str, const char * const display, +static gboolean _cmd_set_boolean_preference(const char * const inp, + struct cmd_help_t help, const char * const cmd_str, const char * const display, void (*set_func)(gboolean)); // command prototypes @@ -88,10 +87,10 @@ static gboolean _cmd_xa(const char * const inp, struct cmd_help_t help); * Commands to change preferences * Commands to change users status */ -static struct cmd_t main_commands[] = +static struct cmd_t main_commands[] = { - { "/help", - _cmd_help, + { "/help", + _cmd_help, { "/help [command]", "Show help summary, or help on a specific command", { "/help [command]", "---------------", @@ -101,8 +100,8 @@ static struct cmd_t main_commands[] = "Example : /help connect", NULL } } }, - { "/connect", - _cmd_connect, + { "/connect", + _cmd_connect, { "/connect user@host", "Login to jabber.", { "/connect user@host", "------------------", @@ -110,11 +109,11 @@ static struct cmd_t main_commands[] = "Profanity should work with any XMPP (Jabber) compliant chat host.", "You can use tab completion to autocomplete any logins you have used before.", "", - "Example: /connect myuser@gmail.com", + "Example: /connect myuser@gmail.com", NULL } } }, - - { "/prefs", - _cmd_prefs, + + { "/prefs", + _cmd_prefs, { "/prefs", "Show current preferences.", { "/prefs", "------", @@ -127,8 +126,8 @@ static struct cmd_t main_commands[] = "you will need to restart Profanity for config file edits to take effect.", NULL } } }, - { "/msg", - _cmd_msg, + { "/msg", + _cmd_msg, { "/msg user@host mesg", "Send mesg to user.", { "/msg user@host mesg", "-------------------", @@ -142,8 +141,8 @@ static struct cmd_t main_commands[] = "Example : /msg boothj5@gmail.com Hey, here's a message!", NULL } } }, - { "/tiny", - _cmd_tiny, + { "/tiny", + _cmd_tiny, { "/tiny url", "Send url as tinyurl in current chat.", { "/tiny url", "---------", @@ -154,7 +153,7 @@ static struct cmd_t main_commands[] = "Example : /tiny http://www.google.com", NULL } } }, - { "/who", + { "/who", _cmd_who, { "/who [status]", "Show contacts with chosen status.", { "/who [status]", @@ -164,8 +163,8 @@ static struct cmd_t main_commands[] = "online includes: chat, dnd, away, xa.", NULL } } }, - { "/close", - _cmd_close, + { "/close", + _cmd_close, { "/close", "Close current chat window.", { "/close", "------", @@ -173,8 +172,8 @@ static struct cmd_t main_commands[] = "The chat window will become available for new chats.", NULL } } }, - { "/quit", - _cmd_quit, + { "/quit", + _cmd_quit, { "/quit", "Quit Profanity.", { "/quit", "-----", @@ -182,7 +181,7 @@ static struct cmd_t main_commands[] = NULL } } } }; -static struct cmd_t setting_commands[] = +static struct cmd_t setting_commands[] = { { "/beep", _cmd_set_beep, @@ -237,7 +236,7 @@ static struct cmd_t setting_commands[] = "Config file value : remind=seconds", NULL } } }, - { "/flash", + { "/flash", _cmd_set_flash, { "/flash on|off", "Enable/disable screen flash notifications.", { "/flash on|off", @@ -251,8 +250,8 @@ static struct cmd_t setting_commands[] = "Config file value : flash=true|false", NULL } } }, - { "/showsplash", - _cmd_set_showsplash, + { "/showsplash", + _cmd_set_showsplash, { "/showsplash on|off", "Enable/disable splash logo on startup.", { "/showsplash on|off", "------------------", @@ -262,7 +261,7 @@ static struct cmd_t setting_commands[] = "Config file value : showsplash=true|false", NULL } } }, - { "/chlog", + { "/chlog", _cmd_set_chlog, { "/chlog on|off", "Enable/disable chat logging.", { "/chlog on|off", @@ -278,7 +277,7 @@ static struct cmd_t setting_commands[] = " ~/.profanity/log/someuser_at_chatserv.com/myfriend_at_chatserv.com", NULL } } }, - { "/history", + { "/history", _cmd_set_history, { "/history on|off", "Enable/disable chat history.", { "/history on|off", @@ -291,7 +290,7 @@ static struct cmd_t setting_commands[] = NULL } } } }; -static struct cmd_t status_commands[] = +static struct cmd_t status_commands[] = { { "/away", _cmd_away, @@ -328,7 +327,7 @@ static struct cmd_t status_commands[] = "Example : /dnd I'm in the zone", NULL } } }, - { "/online", + { "/online", _cmd_online, { "/online [msg]", "Set status to online.", { "/online [msg]", @@ -351,8 +350,9 @@ static struct cmd_t status_commands[] = "Example : /xa This meeting is going to be a long one", NULL } } }, }; - + static PAutocomplete commands_ac; +static PAutocomplete help_ac; /* * Initialise command autocompleter and history @@ -362,26 +362,37 @@ cmd_init(void) { log_info("Initialising commands"); commands_ac = p_autocomplete_new(); + help_ac = p_autocomplete_new(); unsigned int i; for (i = 0; i < ARRAY_SIZE(main_commands); i++) { struct cmd_t *pcmd = main_commands+i; - p_autocomplete_add(commands_ac, (gchar *)pcmd->cmd); + p_autocomplete_add(commands_ac, (gchar *)strdup(pcmd->cmd)); + p_autocomplete_add(help_ac, (gchar *)strdup(pcmd->cmd+1)); } for (i = 0; i < ARRAY_SIZE(setting_commands); i++) { struct cmd_t *pcmd = setting_commands+i; - p_autocomplete_add(commands_ac, (gchar *)pcmd->cmd); + p_autocomplete_add(commands_ac, (gchar *)strdup(pcmd->cmd)); + p_autocomplete_add(help_ac, (gchar *)strdup(pcmd->cmd+1)); } for (i = 0; i < ARRAY_SIZE(status_commands); i++) { struct cmd_t *pcmd = status_commands+i; - p_autocomplete_add(commands_ac, (gchar *)pcmd->cmd); + p_autocomplete_add(commands_ac, (gchar *)strdup(pcmd->cmd)); + p_autocomplete_add(help_ac, (gchar *)strdup(pcmd->cmd+1)); } history_init(); } +void +cmd_close(void) +{ + p_autocomplete_clear(commands_ac); + p_autocomplete_clear(help_ac); +} + // Command autocompletion functions char * @@ -397,6 +408,17 @@ cmd_reset_completer(void) } // Command help +char * +cmd_help_complete(char *inp) +{ + return p_autocomplete_complete(help_ac, inp); +} + +void +cmd_help_reset_completer(void) +{ + p_autocomplete_reset(help_ac); +} GSList * cmd_get_basic_help(void) @@ -443,7 +465,7 @@ gboolean cmd_execute(const char * const command, const char * const inp) { struct cmd_t *cmd = _cmd_get_command(command); - + if (cmd != NULL) { return (cmd->func(inp, cmd->help)); } else { @@ -457,7 +479,7 @@ cmd_execute_default(const char * const inp) if (win_in_chat()) { char *recipient = win_get_recipient(); jabber_send(inp, recipient); - + if (prefs_get_chlog()) { const char *jid = jabber_get_jid(); chat_log_chat(jid, recipient, inp, OUT); @@ -499,7 +521,7 @@ _cmd_connect(const char * const inp, struct cmd_help_t help) inp_non_block(); log_debug("Connecting as %s", lower); - + conn_status = jabber_connect(lower, passwd); if (conn_status == JABBER_CONNECTING) { cons_show("Connecting..."); @@ -512,7 +534,7 @@ _cmd_connect(const char * const inp, struct cmd_help_t help) result = TRUE; } - + return result; } @@ -540,7 +562,7 @@ _cmd_help(const char * const inp, struct cmd_help_t help) if (command != NULL) { help_text = command->help.long_help; } - + cons_show(""); if (help_text != NULL) { @@ -574,7 +596,7 @@ _cmd_who(const char * const inp, struct cmd_help_t help) if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); } else { - // copy input + // copy input char inp_cpy[strlen(inp) + 1]; strcpy(inp_cpy, inp); @@ -595,7 +617,7 @@ _cmd_who(const char * const inp, struct cmd_help_t help) // valid arg } else { GSList *list = get_contact_list(); - + // no arg, show all contacts if (show == NULL) { cons_show("All contacts:"); @@ -653,7 +675,7 @@ _cmd_msg(const char * const inp, struct cmd_help_t help) if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); } else { - // copy input + // copy input char inp_cpy[strlen(inp) + 1]; strcpy(inp_cpy, inp); @@ -719,7 +741,7 @@ _cmd_tiny(const char * const inp, struct cmd_help_t help) } } else { cons_show("Usage: %s", help.usage); - + if (win_in_chat()) { char usage[strlen(help.usage + 8)]; sprintf(usage, "Usage: %s", help.usage); @@ -735,7 +757,7 @@ _cmd_close(const char * const inp, struct cmd_help_t help) { if (!win_close_win()) cons_bad_command(inp); - + return TRUE; } @@ -749,42 +771,42 @@ _cmd_set_beep(const char * const inp, struct cmd_help_t help) static gboolean _cmd_set_notify(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/notify", + return _cmd_set_boolean_preference(inp, help, "/notify", "Desktop notifications", prefs_set_notify); } static gboolean _cmd_set_typing(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/typing", + return _cmd_set_boolean_preference(inp, help, "/typing", "Incoming typing notifications", prefs_set_typing); } static gboolean _cmd_set_flash(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/flash", + return _cmd_set_boolean_preference(inp, help, "/flash", "Screen flash", prefs_set_flash); } static gboolean _cmd_set_showsplash(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/showsplash", + return _cmd_set_boolean_preference(inp, help, "/showsplash", "Splash screen", prefs_set_showsplash); } static gboolean _cmd_set_chlog(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/chlog", + return _cmd_set_boolean_preference(inp, help, "/chlog", "Chat logging", prefs_set_chlog); } static gboolean _cmd_set_history(const char * const inp, struct cmd_help_t help) { - return _cmd_set_boolean_preference(inp, help, "/history", + return _cmd_set_boolean_preference(inp, help, "/history", "Chat history", prefs_set_history); } @@ -794,7 +816,7 @@ _cmd_set_remind(const char * const inp, struct cmd_help_t help) if ((strncmp(inp, "/remind ", 8) != 0) || (strlen(inp) < 9)) { cons_show("Usage: %s", help.usage); } else { - // copy input + // copy input char inp_cpy[strlen(inp) + 1]; strcpy(inp_cpy, inp); @@ -813,7 +835,7 @@ _cmd_set_remind(const char * const inp, struct cmd_help_t help) } } - return TRUE; + return TRUE; } static gboolean @@ -854,7 +876,7 @@ _cmd_xa(const char * const inp, struct cmd_help_t help) // helper function for status change commands static void -_update_presence(const jabber_presence_t presence, +_update_presence(const jabber_presence_t presence, const char * const show, const char * const inp) { char *msg; @@ -865,7 +887,7 @@ _update_presence(const jabber_presence_t presence, } jabber_conn_status_t conn_status = jabber_get_connection_status(); - + if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); } else { @@ -879,18 +901,18 @@ _update_presence(const jabber_presence_t presence, } } -} +} // helper function for boolean preference commands static gboolean _cmd_set_boolean_preference(const char * const inp, struct cmd_help_t help, - const char * const cmd_str, const char * const display, + const char * const cmd_str, const char * const display, void (*set_func)(gboolean)) { GString *on = g_string_new(cmd_str); g_string_append(on, " on"); - + GString *off = g_string_new(cmd_str); g_string_append(off, " off"); @@ -910,7 +932,7 @@ _cmd_set_boolean_preference(const char * const inp, struct cmd_help_t help, char usage[strlen(help.usage + 8)]; sprintf(usage, "Usage: %s", help.usage); cons_show(usage); - } + } g_string_free(on, TRUE); g_string_free(off, TRUE); diff --git a/src/command.h b/src/command.h index 9f27debf..c15b6bd5 100644 --- a/src/command.h +++ b/src/command.h @@ -1,8 +1,8 @@ -/* +/* * command.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -33,12 +33,15 @@ struct cmd_help_t { }; void cmd_init(void); +void cmd_close(void); char * cmd_complete(char *inp); void cmd_reset_completer(void); gboolean cmd_execute(const char * const command, const char * const inp); gboolean cmd_execute_default(const char * const inp); // command help +char * cmd_help_complete(char *inp); +void cmd_help_reset_completer(void); GSList * cmd_get_basic_help(void); GSList * cmd_get_settings_help(void); GSList * cmd_get_status_help(void); diff --git a/src/common.c b/src/common.c index 078e8ae4..c9a5612e 100644 --- a/src/common.c +++ b/src/common.c @@ -1,8 +1,8 @@ -/* +/* * common.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -50,20 +50,20 @@ create_dir(char *name) } char * -str_replace(const char *string, const char *substr, - const char *replacement) +str_replace(const char *string, const char *substr, + const char *replacement) { char *tok = NULL; char *newstr = NULL; char *oldstr = NULL; char *head = NULL; - + if (string == NULL) return NULL; - if ( substr == NULL || - replacement == NULL || - (strcmp(substr, "") == 0)) + if ( substr == NULL || + replacement == NULL || + (strcmp(substr, "") == 0)) return strdup (string); newstr = strdup (string); @@ -71,26 +71,26 @@ str_replace(const char *string, const char *substr, while ( (tok = strstr ( head, substr ))) { oldstr = newstr; - newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + + newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 ); - - if ( newstr == NULL ) { + + if ( newstr == NULL ) { free (oldstr); return NULL; } - + memcpy ( newstr, oldstr, tok - oldstr ); memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) ); - memcpy ( newstr + (tok - oldstr) + strlen( replacement ), - tok + strlen ( substr ), + memcpy ( newstr + (tok - oldstr) + strlen( replacement ), + tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) ); - memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + + memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 ); - + head = newstr + (tok - oldstr) + strlen( replacement ); free (oldstr); } - + return newstr; } diff --git a/src/common.h b/src/common.h index 2515e065..d683ae16 100644 --- a/src/common.h +++ b/src/common.h @@ -1,8 +1,8 @@ -/* +/* * common.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -37,7 +37,7 @@ void p_slist_free_full(GSList *items, GDestroyNotify free_func); void create_dir(char *name); -char * str_replace(const char *string, const char *substr, +char * str_replace(const char *string, const char *substr, const char *replacement); int str_contains(char str[], int size, char ch); diff --git a/src/contact.c b/src/contact.c index 1becabc6..12a2e500 100644 --- a/src/contact.c +++ b/src/contact.c @@ -1,8 +1,8 @@ -/* +/* * contact.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -34,7 +34,7 @@ struct p_contact_t { }; PContact -p_contact_new(const char * const name, const char * const show, +p_contact_new(const char * const name, const char * const show, const char * const status) { PContact contact = malloc(sizeof(struct p_contact_t)); @@ -59,12 +59,12 @@ p_contact_copy(PContact contact) PContact copy = malloc(sizeof(struct p_contact_t)); copy->name = strdup(contact->name); copy->show = strdup(contact->show); - + if (contact->status != NULL) copy->status = strdup(contact->status); else copy->status = NULL; - + return copy; } diff --git a/src/contact.h b/src/contact.h index a5f96b97..8f778ca9 100644 --- a/src/contact.h +++ b/src/contact.h @@ -1,8 +1,8 @@ -/* +/* * contact.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -25,7 +25,7 @@ typedef struct p_contact_t *PContact; -PContact p_contact_new(const char * const name, const char * const show, +PContact p_contact_new(const char * const name, const char * const show, const char * const status); PContact p_contact_copy(PContact contact); void p_contact_free(PContact contact); diff --git a/src/contact_list.c b/src/contact_list.c index 22a52659..e266e25e 100644 --- a/src/contact_list.c +++ b/src/contact_list.c @@ -1,8 +1,8 @@ -/* +/* * contact_list.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -30,7 +30,7 @@ static PAutocomplete ac; void contact_list_init(void) { - ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, + ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, (GDestroyNotify)p_contact_free); @@ -43,7 +43,7 @@ contact_list_clear(void) } void -reset_search_attempts(void) +contact_list_reset_search_attempts(void) { p_autocomplete_reset(ac); } @@ -55,7 +55,7 @@ contact_list_remove(const char * const name) } gboolean -contact_list_add(const char * const name, const char * const show, +contact_list_add(const char * const name, const char * const show, const char * const status) { return p_autocomplete_add(ac, p_contact_new(name, show, status)); @@ -68,7 +68,7 @@ get_contact_list(void) } char * -find_contact(char *search_str) +contact_list_find_contact(char *search_str) { return p_autocomplete_complete(ac, search_str); } @@ -77,7 +77,7 @@ PContact contact_list_get_contact(const char const *jid) { GSList *contacts = get_contact_list(); - + while (contacts != NULL) { PContact contact = contacts->data; if (strcmp(p_contact_name(contact), jid) == 0) { diff --git a/src/contact_list.h b/src/contact_list.h index 7d89b88c..bc239111 100644 --- a/src/contact_list.h +++ b/src/contact_list.h @@ -1,8 +1,8 @@ -/* +/* * contact_list.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -29,12 +29,12 @@ void contact_list_init(void); void contact_list_clear(void); -void reset_search_attempts(void); +void contact_list_reset_search_attempts(void); gboolean contact_list_add(const char * const name, const char * const show, const char * const status); gboolean contact_list_remove(const char * const name); GSList * get_contact_list(void); -char * find_contact(char *search_str); +char * contact_list_find_contact(char *search_str); PContact contact_list_get_contact(const char const *jid); #endif diff --git a/src/history.c b/src/history.c index 444ca210..8805beb5 100644 --- a/src/history.c +++ b/src/history.c @@ -1,8 +1,8 @@ -/* +/* * history.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -45,7 +45,7 @@ history_previous(char *inp, int *size) { char inp_str[*size + 1]; _stringify_input(inp, *size, inp_str); - + return p_history_previous(history, inp_str); } diff --git a/src/history.h b/src/history.h index 982278a3..3c9a4463 100644 --- a/src/history.h +++ b/src/history.h @@ -1,8 +1,8 @@ -/* +/* * history.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify diff --git a/src/input_win.c b/src/input_win.c index 308d4046..107d6985 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -1,8 +1,8 @@ -/* - * input_win.c +/* + * input_win.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -26,11 +26,11 @@ * *size - holds the current size of input * *input - holds the current input string, NOT null terminated at this point * *ch - getch will put a charater here if there was any input - * + * * The example below shows the values of size, input, a call to wgetyx to * find the current cursor location, and the index of the input string. * - * view : |mple| + * view : |mple| * input : "example te" * index : "0123456789" * inp_x : "0123456789" @@ -58,12 +58,16 @@ #include "preferences.h" #include "ui.h" +typedef char*(*autocomplete_func)(char *); + static WINDOW *inp_win; static int pad_start = 0; static int _handle_edit(const int ch, char *input, int *size); static int _printable(const int ch); static void _replace_input(char *input, const char * const new_input, int *size); +static void _parameter_autocomplete(char *input, int *size, char *command, + autocomplete_func func); void create_input_window(void) @@ -84,7 +88,7 @@ inp_win_resize(const char * const input, const int size) int rows, cols, inp_x; getmaxyx(stdscr, rows, cols); inp_x = getcurx(inp_win); - + // if lost cursor off screen, move contents to show it if (inp_x >= pad_start + cols) { pad_start = inp_x - (cols / 2); @@ -125,7 +129,7 @@ inp_get_char(int *ch, char *input, int *size) int inp_y = 0; int inp_x = 0; int i; - + // echo off, and get some more input noecho(); *ch = wgetch(inp_win); @@ -134,7 +138,7 @@ inp_get_char(int *ch, char *input, int *size) if (!_handle_edit(*ch, input, size)) { if (_printable(*ch)) { getyx(inp_win, inp_y, inp_x); - + // handle insert if not at end of input if (inp_x < *size) { winsch(inp_win, *ch); @@ -150,7 +154,7 @@ inp_get_char(int *ch, char *input, int *size) } else { waddch(inp_win, *ch); input[(*size)++] = *ch; - + // if gone over screen size follow input int rows, cols; getmaxyx(stdscr, rows, cols); @@ -160,8 +164,10 @@ inp_get_char(int *ch, char *input, int *size) } } - reset_search_attempts(); - reset_login_search(); + contact_list_reset_search_attempts(); + prefs_reset_login_search(); + prefs_reset_boolean_choice(); + cmd_help_reset_completer(); cmd_reset_completer(); } } @@ -174,7 +180,7 @@ inp_get_password(char *passwd) { int rows, cols; getmaxyx(stdscr, rows, cols); - + wclear(inp_win); prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); noecho(); @@ -208,15 +214,15 @@ _handle_edit(const int ch, char *input, int *size) int inp_y = 0; int inp_x = 0; char inp_cpy[*size]; - + getmaxyx(stdscr, rows, cols); getyx(inp_win, inp_y, inp_x); switch(ch) { - + case 127: case KEY_BACKSPACE: - reset_search_attempts(); + contact_list_reset_search_attempts(); if (*size > 0) { // if at end, delete last char @@ -252,12 +258,12 @@ _handle_edit(const int ch, char *input, int *size) case KEY_DC: // DEL if (inp_x < *size) { wdelch(inp_win); - + // if not last char, shift chars left if (inp_x < *size - 1) for (i = inp_x; i < *size; i++) input[i] = input[i+1]; - + (*size)--; } return 1; @@ -265,7 +271,7 @@ _handle_edit(const int ch, char *input, int *size) case KEY_LEFT: if (inp_x > 0) wmove(inp_win, inp_y, inp_x-1); - + // current position off screen to left if (inp_x - 1 < pad_start) { pad_start--; @@ -276,7 +282,7 @@ _handle_edit(const int ch, char *input, int *size) case KEY_RIGHT: if (inp_x < *size) { wmove(inp_win, inp_y, inp_x+1); - + // current position off screen to right if ((inp_x + 1 - pad_start) >= cols) { pad_start++; @@ -319,7 +325,7 @@ _handle_edit(const int ch, char *input, int *size) case 9: // tab - // autocomplete commands + // autocomplete command if ((strncmp(input, "/", 1) == 0) && (!str_contains(input, *size, ' '))) { for(i = 0; i < *size; i++) { inp_cpy[i] = input[i]; @@ -333,41 +339,31 @@ _handle_edit(const int ch, char *input, int *size) free(auto_msg); free(found); } - - // autcomplete /msg recipient - } else if ((strncmp(input, "/msg ", 5) == 0) && (*size > 5)) { - for(i = 5; i < *size; i++) { - inp_cpy[i-5] = input[i]; - } - inp_cpy[(*size) - 5] = '\0'; - found = find_contact(inp_cpy); - if (found != NULL) { - auto_msg = (char *) malloc((5 + (strlen(found) + 1)) * sizeof(char)); - strcpy(auto_msg, "/msg "); - strcat(auto_msg, found); - _replace_input(input, auto_msg, size); - free(auto_msg); - free(found); - } - - // autocomplete /connect username - } else if ((strncmp(input, "/connect ", 9) == 0) && (*size > 9)) { - for(i = 9; i < *size; i++) { - inp_cpy[i-9] = input[i]; - } - inp_cpy[(*size) - 9] = '\0'; - found = find_login(inp_cpy); - if (found != NULL) { - auto_msg = (char *) malloc((9 + (strlen(found) + 1)) * sizeof(char)); - strcpy(auto_msg, "/connect "); - strcat(auto_msg, found); - _replace_input(input, auto_msg, size); - free(auto_msg); - free(found); - } } + + _parameter_autocomplete(input, size, "/msg", + contact_list_find_contact); + _parameter_autocomplete(input, size, "/connect", + prefs_find_login); + _parameter_autocomplete(input, size, "/help", + cmd_help_complete); + _parameter_autocomplete(input, size, "/beep", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/notify", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/typing", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/flash", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/showsplash", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/chlog", + prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/history", + prefs_autocomplete_boolean_choice); + return 1; - + default: return 0; } @@ -376,9 +372,9 @@ _handle_edit(const int ch, char *input, int *size) static int _printable(const int ch) { - return (ch != ERR && ch != '\n' && + return (ch != ERR && ch != '\n' && ch != KEY_PPAGE && ch != KEY_NPAGE && - ch != KEY_F(1) && ch != KEY_F(2) && ch != KEY_F(3) && + ch != KEY_F(1) && ch != KEY_F(2) && ch != KEY_F(3) && ch != KEY_F(4) && ch != KEY_F(5) && ch != KEY_F(6) && ch != KEY_F(7) && ch != KEY_F(8) && ch != KEY_F(9) && ch != KEY_F(10) && ch!= KEY_F(11) && ch != KEY_F(12) && @@ -396,3 +392,32 @@ _replace_input(char *input, const char * const new_input, int *size) for (i = 0; i < *size; i++) waddch(inp_win, input[i]); } + +static void +_parameter_autocomplete(char *input, int *size, char *command, + autocomplete_func func) +{ + char *found = NULL; + char *auto_msg = NULL; + char inp_cpy[*size]; + int i; + char *command_cpy = malloc(strlen(command) + 2); + sprintf(command_cpy, "%s ", command); + int len = strlen(command_cpy); + if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) { + for(i = len; i < *size; i++) { + inp_cpy[i-len] = input[i]; + } + inp_cpy[(*size) - len] = '\0'; + found = func(inp_cpy); + if (found != NULL) { + auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char)); + strcpy(auto_msg, command_cpy); + strcat(auto_msg, found); + _replace_input(input, auto_msg, size); + free(auto_msg); + free(found); + } + } + free(command_cpy); +} diff --git a/src/jabber.c b/src/jabber.c index da606132..ea7e38c4 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -1,8 +1,8 @@ -/* +/* * jabber.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -44,20 +44,20 @@ static struct _jabber_conn_t { static log_level_t _get_log_level(xmpp_log_level_t xmpp_level); static xmpp_log_level_t _get_xmpp_log_level(); -static void _xmpp_file_logger(void * const userdata, - const xmpp_log_level_t level, const char * const area, +static void _xmpp_file_logger(void * const userdata, + const xmpp_log_level_t level, const char * const area, const char * const msg); static xmpp_log_t * _xmpp_get_file_logger(); // XMPP event handlers -static void _connection_handler(xmpp_conn_t * const conn, - const xmpp_conn_event_t status, const int error, +static void _connection_handler(xmpp_conn_t * const conn, + const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata); -static int _message_handler(xmpp_conn_t * const conn, +static int _message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -static int _roster_handler(xmpp_conn_t * const conn, +static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -static int _presence_handler(xmpp_conn_t * const conn, +static int _presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); @@ -71,7 +71,7 @@ jabber_init(const int disable_tls) } jabber_conn_status_t -jabber_connect(const char * const user, +jabber_connect(const char * const user, const char * const passwd) { log_info("Connecting as %s", user); @@ -87,12 +87,12 @@ jabber_connect(const char * const user, if (jabber_conn.tls_disabled) xmpp_conn_disable_tls(jabber_conn.conn); - int connect_status = xmpp_connect_client(jabber_conn.conn, NULL, 0, + int connect_status = xmpp_connect_client(jabber_conn.conn, NULL, 0, _connection_handler, jabber_conn.ctx); if (connect_status == 0) jabber_conn.conn_status = JABBER_CONNECTING; - else + else jabber_conn.conn_status = JABBER_DISCONNECTED; return jabber_conn.conn_status; @@ -122,7 +122,7 @@ jabber_disconnect(void) void jabber_process_events(void) { - if (jabber_conn.conn_status == JABBER_CONNECTED + if (jabber_conn.conn_status == JABBER_CONNECTED || jabber_conn.conn_status == JABBER_CONNECTING || jabber_conn.conn_status == JABBER_DISCONNECTING) xmpp_run_once(jabber_conn.ctx, 10); @@ -136,7 +136,7 @@ jabber_send(const char * const msg, const char * const recipient) char *coded_msg3 = str_replace(coded_msg2, ">", ">"); xmpp_stanza_t *reply, *body, *text, *active; - + active = xmpp_stanza_new(jabber_conn.ctx); xmpp_stanza_set_name(active, "active"); xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates"); @@ -191,7 +191,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg) pres = xmpp_stanza_new(jabber_conn.ctx); xmpp_stanza_set_name(pres, "presence"); - + if (status != PRESENCE_ONLINE) { show = xmpp_stanza_new(jabber_conn.ctx); xmpp_stanza_set_name(show, "show"); @@ -205,7 +205,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg) xmpp_stanza_set_text(text, "chat"); else if (status == PRESENCE_XA) xmpp_stanza_set_text(text, "xa"); - else + else xmpp_stanza_set_text(text, "online"); xmpp_stanza_add_child(show, text); @@ -243,10 +243,46 @@ jabber_get_jid(void) return xmpp_conn_get_jid(jabber_conn.conn); } +void +jabber_free_resources(void) +{ + xmpp_conn_release(jabber_conn.conn); + xmpp_ctx_free(jabber_conn.ctx); + xmpp_shutdown(); +} + static int -_message_handler(xmpp_conn_t * const conn, +_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + char *type = NULL; + char *from = NULL; + + type = xmpp_stanza_get_attribute(stanza, "type"); + from = xmpp_stanza_get_attribute(stanza, "from"); + + if (type != NULL) { + if (strcmp(type, "error") == 0) { + char *err_msg = NULL; + xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, "error"); + if (error == NULL) { + log_debug("error message without received"); + return 1; + } else { + xmpp_stanza_t *err_cond = xmpp_stanza_get_children(error); + if (err_cond == NULL) { + log_debug("error message without received"); + return 1; + } else { + err_msg = xmpp_stanza_get_name(err_cond); + } + // TODO: process 'type' attribute from [RFC6120, 8.3.2] + } + prof_handle_error_message(from, err_msg); + return 1; + } + } + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body"); // if no message, check for chatstates @@ -257,36 +293,30 @@ _message_handler(xmpp_conn_t * const conn, // active } else if (xmpp_stanza_get_child_by_name(stanza, "composing") != NULL) { // composing - char *from = xmpp_stanza_get_attribute(stanza, "from"); prof_handle_typing(from); } } - + return 1; } // message body recieved - char *type = xmpp_stanza_get_attribute(stanza, "type"); - if(strcmp(type, "error") == 0) - return 1; - char *message = xmpp_stanza_get_text(body); - char *from = xmpp_stanza_get_attribute(stanza, "from"); prof_handle_incoming_message(from, message); return 1; } static void -_connection_handler(xmpp_conn_t * const conn, - const xmpp_conn_event_t status, const int error, +_connection_handler(xmpp_conn_t * const conn, + const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; if (status == XMPP_CONN_CONNECT) { const char *jid = xmpp_conn_get_jid(conn); - prof_handle_login_success(jid); + prof_handle_login_success(jid); xmpp_stanza_t* pres; xmpp_handler_add(conn, _message_handler, NULL, "message", NULL, ctx); @@ -303,22 +333,15 @@ _connection_handler(xmpp_conn_t * const conn, jabber_conn.presence = PRESENCE_ONLINE; jabber_roster_request(); } else { - + // received close stream response from server after disconnect if (jabber_conn.conn_status == JABBER_DISCONNECTING) { - // free memory for connection object and context - xmpp_conn_release(jabber_conn.conn); - xmpp_ctx_free(jabber_conn.ctx); - - // shutdown libstrophe - xmpp_shutdown(); - jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; - + // lost connection for unkown reason } else if (jabber_conn.conn_status == JABBER_CONNECTED) { - prof_handle_lost_connection(); + prof_handle_lost_connection(); xmpp_stop(ctx); jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; @@ -334,19 +357,19 @@ _connection_handler(xmpp_conn_t * const conn, } static int -_roster_handler(xmpp_conn_t * const conn, +_roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *query, *item; char *type = xmpp_stanza_get_type(stanza); - + if (strcmp(type, "error") == 0) log_error("Roster query failed"); else { query = xmpp_stanza_get_child_by_name(stanza, "query"); GSList *roster = NULL; item = xmpp_stanza_get_children(query); - + while (item != NULL) { const char *name = xmpp_stanza_get_attribute(item, "name"); const char *jid = xmpp_stanza_get_attribute(item, "jid"); @@ -366,7 +389,7 @@ _roster_handler(xmpp_conn_t * const conn, prof_handle_roster(roster); } - + return 1; } @@ -375,7 +398,7 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) { if (jabber_conn.conn_status == JABBER_CONNECTED) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; - + xmpp_stanza_t *iq, *ping; iq = xmpp_stanza_new(ctx); @@ -398,20 +421,20 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) } static int -_presence_handler(xmpp_conn_t * const conn, +_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { const char *jid = xmpp_conn_get_jid(jabber_conn.conn); char jid_cpy[strlen(jid) + 1]; strcpy(jid_cpy, jid); char *short_jid = strtok(jid_cpy, "/"); - + char *from = xmpp_stanza_get_attribute(stanza, "from"); char *short_from = strtok(from, "/"); char *type = xmpp_stanza_get_attribute(stanza, "type"); char *show_str, *status_str; - + xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, "show"); if (show != NULL) show_str = xmpp_stanza_get_text(show); @@ -419,9 +442,9 @@ _presence_handler(xmpp_conn_t * const conn, show_str = NULL; xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, "status"); - if (status != NULL) + if (status != NULL) status_str = xmpp_stanza_get_text(status); - else + else status_str = NULL; if (strcmp(short_jid, short_from) !=0) { diff --git a/src/jabber.h b/src/jabber.h index 7e04d0bb..b7e3f87c 100644 --- a/src/jabber.h +++ b/src/jabber.h @@ -1,8 +1,8 @@ -/* +/* * jabber.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -41,7 +41,7 @@ typedef enum { } jabber_presence_t; void jabber_init(const int disable_tls); -jabber_conn_status_t jabber_connect(const char * const user, +jabber_conn_status_t jabber_connect(const char * const user, const char * const passwd); gboolean jabber_disconnect(void); void jabber_roster_request(void); @@ -50,5 +50,6 @@ void jabber_send(const char * const msg, const char * const recipient); void jabber_update_presence(jabber_presence_t status, const char * const msg); const char * jabber_get_jid(void); jabber_conn_status_t jabber_get_connection_status(void); +void jabber_free_resources(void); #endif diff --git a/src/log.c b/src/log.c index 20dc0b4e..5265e247 100644 --- a/src/log.c +++ b/src/log.c @@ -1,8 +1,8 @@ -/* +/* * log.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -37,7 +37,7 @@ static GDateTime *dt; static log_level_t level_filter; void -log_debug(const char * const msg, ...) +log_debug(const char * const msg, ...) { va_list arg; va_start(arg, msg); @@ -49,7 +49,7 @@ log_debug(const char * const msg, ...) } void -log_info(const char * const msg, ...) +log_info(const char * const msg, ...) { va_list arg; va_start(arg, msg); @@ -61,7 +61,7 @@ log_info(const char * const msg, ...) } void -log_warning(const char * const msg, ...) +log_warning(const char * const msg, ...) { va_list arg; va_start(arg, msg); @@ -73,7 +73,7 @@ log_warning(const char * const msg, ...) } void -log_error(const char * const msg, ...) +log_error(const char * const msg, ...) { va_list arg; va_start(arg, msg); @@ -116,10 +116,10 @@ log_msg(log_level_t level, const char * const area, const char * const msg) if (level >= level_filter) { dt = g_date_time_new_now(tz); - gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); + gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg); g_date_time_unref(dt); - + fflush(logp); g_free(date_fmt); } diff --git a/src/log.h b/src/log.h index abf4b927..12dd880c 100644 --- a/src/log.h +++ b/src/log.h @@ -1,8 +1,8 @@ -/* +/* * log.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -38,7 +38,7 @@ void log_debug(const char * const msg, ...); void log_info(const char * const msg, ...); void log_warning(const char * const msg, ...); void log_error(const char * const msg, ...); -void log_msg(log_level_t level, const char * const area, +void log_msg(log_level_t level, const char * const area, const char * const msg); #endif diff --git a/src/main.c b/src/main.c index 9e42d17d..fa84cf7b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ -/* +/* * main.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -32,15 +32,15 @@ static char *log = "INFO"; int main(int argc, char **argv) -{ - static GOptionEntry entries[] = +{ + static GOptionEntry entries[] = { { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Show version information", NULL }, { "disable-tls", 'd', 0, G_OPTION_ARG_NONE, &disable_tls, "Disable TLS", NULL }, { "log",'l', 0, G_OPTION_ARG_STRING, &log, "Set logging levels, DEBUG, INFO (default), WARN, ERROR", "LEVEL" }, { NULL } }; - + GError *error = NULL; GOptionContext *context; diff --git a/src/preferences.c b/src/preferences.c index 847b1817..67f9345a 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -1,8 +1,8 @@ -/* +/* * preferences.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -39,8 +39,8 @@ static GString *prefs_loc; static GKeyFile *prefs; -// search logins list -static PAutocomplete ac; +static PAutocomplete login_ac; +static PAutocomplete boolean_choice_ac; struct colour_string_t { char *str; @@ -78,7 +78,7 @@ static struct colours_t { } colour_prefs; static NCURSES_COLOR_T _lookup_colour(const char * const colour); -static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, +static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, NCURSES_COLOR_T def); static void _load_colours(void); static void _save_prefs(void); @@ -87,12 +87,12 @@ void prefs_load(void) { log_info("Loading preferences"); - ac = p_autocomplete_new(); + login_ac = p_autocomplete_new(); prefs_loc = g_string_new(getenv("HOME")); g_string_append(prefs_loc, "/.profanity/config"); prefs = g_key_file_new(); - g_key_file_load_from_file(prefs, prefs_loc->str, G_KEY_FILE_KEEP_COMMENTS, + g_key_file_load_from_file(prefs, prefs_loc->str, G_KEY_FILE_KEEP_COMMENTS, NULL); // create the logins searchable list for autocompletion @@ -102,7 +102,7 @@ prefs_load(void) gsize i; for (i = 0; i < njids; i++) { - p_autocomplete_add(ac, strdup(jids[i])); + p_autocomplete_add(login_ac, strdup(jids[i])); } for (i = 0; i < njids; i++) { @@ -110,13 +110,19 @@ prefs_load(void) } free(jids); - _load_colours(); + + boolean_choice_ac = p_autocomplete_new(); + p_autocomplete_add(boolean_choice_ac, strdup("on")); + p_autocomplete_add(boolean_choice_ac, strdup("off")); + } void prefs_close(void) { + p_autocomplete_clear(login_ac); + p_autocomplete_clear(boolean_choice_ac); g_key_file_free(prefs); } @@ -134,7 +140,7 @@ _lookup_colour(const char * const colour) } static void -_set_colour(gchar *val, NCURSES_COLOR_T *pref, +_set_colour(gchar *val, NCURSES_COLOR_T *pref, NCURSES_COLOR_T def) { if(!val) { @@ -144,7 +150,7 @@ _set_colour(gchar *val, NCURSES_COLOR_T *pref, if (col == -99) { *pref = def; } else { - *pref = col; + *pref = col; } } } @@ -160,48 +166,60 @@ _load_colours(void) gchar *online_val = g_key_file_get_string(prefs, "colours", "online", NULL); _set_colour(online_val, &colour_prefs.online, COLOR_GREEN); - + gchar *away_val = g_key_file_get_string(prefs, "colours", "away", NULL); _set_colour(away_val, &colour_prefs.away, COLOR_GREEN); - + gchar *chat_val = g_key_file_get_string(prefs, "colours", "chat", NULL); _set_colour(chat_val, &colour_prefs.chat, COLOR_GREEN); - + gchar *dnd_val = g_key_file_get_string(prefs, "colours", "dnd", NULL); _set_colour(dnd_val, &colour_prefs.dnd, COLOR_GREEN); - + gchar *xa_val = g_key_file_get_string(prefs, "colours", "xa", NULL); _set_colour(xa_val, &colour_prefs.xa, COLOR_GREEN); - + gchar *offline_val = g_key_file_get_string(prefs, "colours", "offline", NULL); _set_colour(offline_val, &colour_prefs.offline, COLOR_CYAN); - + gchar *err_val = g_key_file_get_string(prefs, "colours", "err", NULL); _set_colour(err_val, &colour_prefs.err, COLOR_RED); gchar *inc_val = g_key_file_get_string(prefs, "colours", "inc", NULL); _set_colour(inc_val, &colour_prefs.inc, COLOR_YELLOW); - + gchar *bar_val = g_key_file_get_string(prefs, "colours", "bar", NULL); _set_colour(bar_val, &colour_prefs.bar, COLOR_BLUE); - + gchar *bar_draw_val = g_key_file_get_string(prefs, "colours", "bar_draw", NULL); _set_colour(bar_draw_val, &colour_prefs.bar_draw, COLOR_CYAN); - + gchar *bar_text_val = g_key_file_get_string(prefs, "colours", "bar_text", NULL); _set_colour(bar_text_val, &colour_prefs.bar_text, COLOR_WHITE); } char * -find_login(char *prefix) +prefs_find_login(char *prefix) { - return p_autocomplete_complete(ac, prefix); + return p_autocomplete_complete(login_ac, prefix); } void -reset_login_search(void) +prefs_reset_login_search(void) { - p_autocomplete_reset(ac); + p_autocomplete_reset(login_ac); +} + +char * +prefs_autocomplete_boolean_choice(char *prefix) +{ + return p_autocomplete_complete(boolean_choice_ac, prefix); +} + +void +prefs_reset_boolean_choice(void) +{ + p_autocomplete_reset(boolean_choice_ac); } gboolean @@ -299,7 +317,7 @@ void prefs_add_login(const char *jid) { gsize njids; - gchar **jids = + gchar **jids = g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL); // no logins remembered yet @@ -308,11 +326,11 @@ prefs_add_login(const char *jid) jids = (gchar**) g_malloc(sizeof(gchar *) * 2); jids[0] = g_strdup(jid); jids[1] = NULL; - g_key_file_set_string_list(prefs, "connections", "logins", + g_key_file_set_string_list(prefs, "connections", "logins", (const gchar * const *)jids, njids); _save_prefs(); g_strfreev(jids); - + return; } else { gsize i; @@ -322,7 +340,7 @@ prefs_add_login(const char *jid) return; } } - + // jid not found, add to the list jids = (gchar **) g_realloc(jids, (sizeof(gchar *) * (njids+2))); jids[njids] = g_strdup(jid); @@ -359,79 +377,79 @@ _save_prefs(void) } NCURSES_COLOR_T -prefs_get_bkgnd() +prefs_get_bkgnd() { return colour_prefs.bkgnd; } NCURSES_COLOR_T -prefs_get_text() +prefs_get_text() { return colour_prefs.text; } NCURSES_COLOR_T -prefs_get_online() +prefs_get_online() { return colour_prefs.online; } NCURSES_COLOR_T -prefs_get_away() +prefs_get_away() { return colour_prefs.away; } NCURSES_COLOR_T -prefs_get_chat() +prefs_get_chat() { return colour_prefs.chat; } NCURSES_COLOR_T -prefs_get_dnd() +prefs_get_dnd() { return colour_prefs.dnd; } NCURSES_COLOR_T -prefs_get_xa() +prefs_get_xa() { return colour_prefs.xa; } NCURSES_COLOR_T -prefs_get_offline() +prefs_get_offline() { return colour_prefs.offline; } NCURSES_COLOR_T -prefs_get_err() +prefs_get_err() { return colour_prefs.err; } NCURSES_COLOR_T -prefs_get_inc() +prefs_get_inc() { return colour_prefs.inc; } NCURSES_COLOR_T -prefs_get_bar() +prefs_get_bar() { return colour_prefs.bar; } NCURSES_COLOR_T -prefs_get_bar_draw() +prefs_get_bar_draw() { return colour_prefs.bar_draw; } NCURSES_COLOR_T -prefs_get_bar_text() +prefs_get_bar_text() { return colour_prefs.bar_text; } diff --git a/src/preferences.h b/src/preferences.h index 8b4f5e68..e816b7c0 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -1,8 +1,8 @@ -/* +/* * preferences.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -37,8 +37,10 @@ void prefs_load(void); void prefs_close(void); -char * find_login(char *prefix); -void reset_login_search(void); +char * prefs_find_login(char *prefix); +void prefs_reset_login_search(void); +char * prefs_autocomplete_boolean_choice(char *prefix); +void prefs_reset_boolean_choice(void); gboolean prefs_get_beep(void); void prefs_set_beep(gboolean value); diff --git a/src/prof_autocomplete.c b/src/prof_autocomplete.c index c12e2df0..79d44498 100644 --- a/src/prof_autocomplete.c +++ b/src/prof_autocomplete.c @@ -1,8 +1,8 @@ -/* +/* * prof_autocomplete.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -47,7 +47,7 @@ p_autocomplete_new(void) } PAutocomplete -p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, +p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, PEqualDeepFunc equal_deep_func, GDestroyNotify free_func) { PAutocomplete new = malloc(sizeof(struct p_autocomplete_t)); @@ -105,15 +105,15 @@ p_autocomplete_add(PAutocomplete ac, void *item) return TRUE; } else { GSList *curr = ac->items; - + while(curr) { - + // insert if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) > 0) { ac->items = g_slist_insert_before(ac->items, curr, item); return TRUE; - + // update } else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) { // only update if data different @@ -125,7 +125,7 @@ p_autocomplete_add(PAutocomplete ac, void *item) return FALSE; } } - + curr = g_slist_next(curr); } @@ -148,13 +148,13 @@ p_autocomplete_remove(PAutocomplete ac, const char * const item) return FALSE; } else { GSList *curr = ac->items; - + while(curr) { if (g_strcmp0(ac->str_func(curr->data), item) == 0) { void *current_item = curr->data; ac->items = g_slist_remove(ac->items, curr->data); ac->free_func(current_item); - + return TRUE; } @@ -190,14 +190,14 @@ p_autocomplete_complete(PAutocomplete ac, gchar *search_str) // first search attempt if (ac->last_found == NULL) { - ac->search_str = + ac->search_str = (gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar)); strcpy(ac->search_str, search_str); found = _search_from(ac, ac->items); return found; - // subsequent search attempt + // subsequent search attempt } else { // search from here+1 tp end found = _search_from(ac, g_slist_next(ac->last_found)); @@ -208,7 +208,7 @@ p_autocomplete_complete(PAutocomplete ac, gchar *search_str) found = _search_from(ac, ac->items); if (found != NULL) return found; - + // we found nothing, reset search p_autocomplete_reset(ac); return NULL; @@ -219,14 +219,14 @@ static gchar * _search_from(PAutocomplete ac, GSList *curr) { while(curr) { - + // match found if (strncmp(ac->str_func(curr->data), ac->search_str, strlen(ac->search_str)) == 0) { - gchar *result = + gchar *result = (gchar *) malloc((strlen(ac->str_func(curr->data)) + 1) * sizeof(gchar)); - + // set pointer to last found ac->last_found = curr; @@ -242,7 +242,7 @@ _search_from(PAutocomplete ac, GSList *curr) } static const char * -_str_func_default(const char *orig) +_str_func_default(const char *orig) { return orig; } diff --git a/src/prof_autocomplete.h b/src/prof_autocomplete.h index 43dc8ac9..50b9e30c 100644 --- a/src/prof_autocomplete.h +++ b/src/prof_autocomplete.h @@ -1,8 +1,8 @@ -/* +/* * prof_autocomplete.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -32,7 +32,7 @@ typedef int (*PEqualFunc)(const void *o1, const void *o2); typedef int (*PEqualDeepFunc)(const void *o1, const void *o2); PAutocomplete p_autocomplete_new(void); -PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, +PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func, PEqualDeepFunc equal_deep_func, GDestroyNotify free_func); void p_autocomplete_clear(PAutocomplete ac); void p_autocomplete_reset(PAutocomplete ac); diff --git a/src/prof_history.c b/src/prof_history.c index e5667d86..218a1887 100644 --- a/src/prof_history.c +++ b/src/prof_history.c @@ -1,8 +1,8 @@ -/* +/* * prof_history.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -59,7 +59,7 @@ p_history_new(unsigned int size) PHistory new_history = malloc(sizeof(struct p_history_t)); new_history->items = NULL; new_history->max_size = size; - + _reset_session(new_history); return new_history; @@ -77,12 +77,12 @@ p_history_append(PHistory history, char *item) _add_to_history(history, copied); return; } - + if (!_has_session(history)) { if (g_list_length(history->items) == history->max_size) { _remove_first(history); } - + _add_to_history(history, copied); } else { @@ -92,15 +92,15 @@ p_history_append(PHistory history, char *item) if (strcmp(history->session.sess_curr->data, "") == 0) { _remove_last_session_item(history); } - + _replace_history_with_session(history); } else { _remove_last_session_item(history); - + char *new = strdup(history->session.sess_curr->data); history->session.items = g_list_append(history->session.items, new); - + _replace_current_with_original(history); _replace_history_with_session(history); } @@ -119,10 +119,10 @@ p_history_previous(PHistory history, char *item) if (item != NULL) { copied = strdup(item); } - + if (!_has_session(history)) { _create_session(history); - + // add the new item history->session.items = g_list_append(history->session.items, copied); history->session.sess_new = g_list_last(history->session.items); @@ -153,7 +153,7 @@ p_history_next(PHistory history, char *item) _update_current_session_item(history, copied); _session_next(history); - + char *result = strdup(history->session.sess_curr->data); return result; } @@ -163,9 +163,9 @@ _replace_history_with_session(PHistory history) { g_list_free(history->items); history->items = g_list_copy(history->session.items); - + if (g_list_length(history->items) > history->max_size) { - _remove_first(history); + _remove_first(history); } _reset_session(history); @@ -174,7 +174,7 @@ _replace_history_with_session(PHistory history) static gboolean _adding_new(PHistory history) { - return (history->session.sess_curr == + return (history->session.sess_curr == g_list_last(history->session.items)); } @@ -218,7 +218,7 @@ _remove_last_session_item(PHistory history) { history->session.items = g_list_reverse(history->session.items); GList *first = g_list_first(history->session.items); - history->session.items = + history->session.items = g_list_remove(history->session.items, first->data); history->session.items = g_list_reverse(history->session.items); } @@ -240,12 +240,12 @@ _create_session(PHistory history) static void _session_previous(PHistory history) { - history->session.sess_curr = + history->session.sess_curr = g_list_previous(history->session.sess_curr); if (history->session.orig_curr == NULL) history->session.orig_curr = g_list_last(history->items); - else - history->session.orig_curr = + else + history->session.orig_curr = g_list_previous(history->session.orig_curr); if (history->session.sess_curr == NULL) { diff --git a/src/prof_history.h b/src/prof_history.h index dce777d5..b3d379cf 100644 --- a/src/prof_history.h +++ b/src/prof_history.h @@ -1,8 +1,8 @@ -/* +/* * prof_history.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify diff --git a/src/profanity.c b/src/profanity.c index cdb084bf..4e240b21 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -1,8 +1,8 @@ -/* +/* * profanity.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -65,7 +65,7 @@ prof_run(const int disable_tls, char *log_level) while(ch != '\n') { gdouble elapsed = g_timer_elapsed(timer, NULL); - + gint remind_period = prefs_get_remind(); // 0 means to not remind @@ -79,10 +79,10 @@ prof_run(const int disable_tls, char *log_level) if (ch == KEY_RESIZE) { gui_resize(ch, inp, size); } - + gui_refresh(); jabber_process_events(); - + inp_get_char(&ch, inp, &size); } @@ -105,15 +105,33 @@ prof_handle_incoming_message(char *from, char *message) { win_show_incomming_msg(from, message); win_page_off(); - - if (prefs_get_chlog()) { - char from_cpy[strlen(from) + 1]; - strcpy(from_cpy, from); - char *short_from = strtok(from_cpy, "/"); - const char *jid = jabber_get_jid(); - - chat_log_chat(jid, short_from, message, IN); - } + + if (prefs_get_chlog()) { + char from_cpy[strlen(from) + 1]; + strcpy(from_cpy, from); + char *short_from = strtok(from_cpy, "/"); + const char *jid = jabber_get_jid(); + + chat_log_chat(jid, short_from, message, IN); + } +} + +void +prof_handle_error_message(const char *from, const char *err_msg) +{ + char *msg, *fmt; + + if (err_msg != NULL) { + fmt = "Error received from server: %s"; + msg = (char *)malloc(strlen(err_msg) + strlen(fmt) - 1); + if (msg == NULL) + goto loop_out; + sprintf(msg, fmt, err_msg); + cons_bad_show(msg); + free(msg); + } +loop_out: + win_show_error_msg(from, err_msg); } void @@ -173,16 +191,16 @@ prof_handle_roster(GSList *roster) { while (roster != NULL) { jabber_roster_entry *entry = roster->data; - + // if contact not in contact list add them as offline - if (find_contact(entry->jid) == NULL) { + if (contact_list_find_contact(entry->jid) == NULL) { contact_list_add(entry->jid, "offline", NULL); } - + roster = g_slist_next(roster); } - g_slist_free_full(roster, (GDestroyNotify)_free_roster_entry); + g_slist_free_full(roster, (GDestroyNotify)_free_roster_entry); } static void @@ -204,7 +222,7 @@ _free_roster_entry(jabber_roster_entry *entry) free(entry->jid); } -static log_level_t +static log_level_t _get_log_level(char *log_level) { if (strcmp(log_level, "DEBUG") == 0) { @@ -218,8 +236,8 @@ _get_log_level(char *log_level) } } -/* - * Take a line of input and process it, return TRUE if profanity is to +/* + * Take a line of input and process it, return TRUE if profanity is to * continue, FALSE otherwise */ static gboolean @@ -228,7 +246,7 @@ _process_input(char *inp) log_debug("Input recieved: %s", inp); gboolean result = FALSE; g_strstrip(inp); - + // add line to history if something typed if (strlen(inp) > 0) { history_append(inp); @@ -251,7 +269,7 @@ _process_input(char *inp) } inp_clear(); - reset_search_attempts(); + contact_list_reset_search_attempts(); win_page_off(); return result; @@ -283,6 +301,7 @@ _shutdown_init(void) while (jabber_get_connection_status() == JABBER_DISCONNECTING) { jabber_process_events(); } + jabber_free_resources(); } _shutdown(); @@ -291,8 +310,10 @@ _shutdown_init(void) static void _shutdown(void) { - gui_close(); - chat_log_close(); - prefs_close(); - log_close(); + contact_list_clear(); + gui_close(); + chat_log_close(); + prefs_close(); + cmd_close(); + log_close(); } diff --git a/src/profanity.h b/src/profanity.h index 3731f2c1..430148e2 100644 --- a/src/profanity.h +++ b/src/profanity.h @@ -1,8 +1,8 @@ -/* +/* * profanity.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -37,6 +37,7 @@ void prof_handle_typing(char *from); void prof_handle_contact_online(char *contact, char *show, char *status); void prof_handle_contact_offline(char *contact, char *show, char *status); void prof_handle_incoming_message(char *from, char *message); +void prof_handle_error_message(const char *from, const char *err_msg); void prof_handle_roster(GSList *roster); #endif diff --git a/src/status_bar.c b/src/status_bar.c index d9ae4aee..8d79fe5d 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -1,8 +1,8 @@ -/* +/* * status_bar.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -86,7 +86,7 @@ status_bar_refresh(void) g_date_time_unref(now_time); } - + void status_bar_resize(void) { @@ -124,7 +124,7 @@ status_bar_inactive(const int win) int active_pos = 1 + ((win -1) * 3); int cols = getmaxx(stdscr); - + mvwaddch(status_bar, 0, cols - 29 + active_pos, ' '); if (win == 9) mvwaddch(status_bar, 0, cols - 29 + active_pos + 1, ' '); @@ -141,7 +141,7 @@ status_bar_active(const int win) int active_pos = 1 + ((win -1) * 3); int cols = getmaxx(stdscr); - + wattron(status_bar, COLOUR_BAR_DRAW); if (win < 9) mvwprintw(status_bar, 0, cols - 29 + active_pos, "%d", win+1); @@ -161,7 +161,7 @@ status_bar_new(const int win) int active_pos = 1 + ((win -1) * 3); int cols = getmaxx(stdscr); - + wattron(status_bar, COLOUR_BAR_TEXT); wattron(status_bar, A_BLINK); if (win < 9) @@ -187,7 +187,7 @@ status_bar_print_message(const char * const msg) if (message != NULL) free(message); - message = (char *) malloc((strlen(msg) + 1) * sizeof(char)); + message = (char *) malloc((strlen(msg) + 1) * sizeof(char)); strcpy(message, msg); mvwprintw(status_bar, 0, 9, message); @@ -201,7 +201,7 @@ status_bar_clear(void) free(message); message = NULL; } - + int i; for (i = 0; i < 9; i++) { is_active[i] = FALSE; diff --git a/src/tinyurl.c b/src/tinyurl.c index 48987cf3..9b572319 100644 --- a/src/tinyurl.c +++ b/src/tinyurl.c @@ -1,8 +1,8 @@ -/* +/* * tinyurl.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -38,7 +38,7 @@ static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data); gboolean tinyurl_valid(char *url) { - return (g_str_has_prefix(url, "http://") || + return (g_str_has_prefix(url, "http://") || g_str_has_prefix(url, "https://")); } @@ -49,14 +49,14 @@ tinyurl_get(char *url) g_string_append(full_url, url); CURL *handle = curl_easy_init(); - struct curl_data_t output; + struct curl_data_t output; output.buffer = NULL; output.size = 0; curl_easy_setopt(handle, CURLOPT_URL, full_url->str); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, _data_callback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&output); - + curl_easy_perform(handle); curl_easy_cleanup(handle); @@ -66,7 +66,7 @@ tinyurl_get(char *url) return output.buffer; } -static size_t +static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data) { size_t realsize = size * nmemb; @@ -79,6 +79,6 @@ _data_callback(void *ptr, size_t size, size_t nmemb, void *data) mem->size += realsize; mem->buffer[ mem->size ] = 0; } - + return realsize; } diff --git a/src/tinyurl.h b/src/tinyurl.h index a36f2b51..0df9865b 100644 --- a/src/tinyurl.h +++ b/src/tinyurl.h @@ -1,8 +1,8 @@ -/* +/* * tinyurl.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify diff --git a/src/title_bar.c b/src/title_bar.c index 2e067d59..0bfed5ba 100644 --- a/src/title_bar.c +++ b/src/title_bar.c @@ -1,8 +1,8 @@ -/* +/* * title_bar.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -79,7 +79,7 @@ title_bar_refresh(void) gdouble seconds = g_timer_elapsed(typing_elapsed, NULL); if (seconds >= 10) { - + if (current_title != NULL) { free(current_title); } @@ -88,7 +88,7 @@ title_bar_refresh(void) strcpy(current_title, recipient); title_bar_draw(); - + dirty = TRUE; } } @@ -124,7 +124,7 @@ title_bar_set_recipient(char *from) { typing_elapsed = NULL; recipient = from; - + if (current_title != NULL) { free(current_title); } @@ -157,7 +157,7 @@ title_bar_set_typing(gboolean is_typing) current_title = (char *) malloc((strlen(recipient) + 1) * sizeof(char)); strcpy(current_title, recipient); } - + dirty = TRUE; } @@ -190,11 +190,11 @@ _title_bar_draw_status(void) } else { mvwprintw(title_bar, 0, cols - 13, " ..offline "); } - + wattron(title_bar, COLOUR_BAR_DRAW); mvwaddch(title_bar, 0, cols - 2, ']'); wattroff(title_bar, COLOUR_BAR_DRAW); - + dirty = TRUE; } @@ -206,6 +206,6 @@ _title_bar_draw_title(void) for (i = 0; i < 45; i++) waddch(title_bar, ' '); mvwprintw(title_bar, 0, 0, " %s", current_title); - + dirty = TRUE; } diff --git a/src/ui.h b/src/ui.h index 66a41e07..671ca703 100644 --- a/src/ui.h +++ b/src/ui.h @@ -1,8 +1,8 @@ -/* +/* * ui.h * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -88,13 +88,14 @@ int win_in_chat(void); char *win_get_recipient(void); void win_show_typing(const char * const from); void win_show_incomming_msg(const char * const from, const char * const message); -void win_show_outgoing_msg(const char * const from, const char * const to, +void win_show_error_msg(const char * const from, const char *err_msg); +void win_show_outgoing_msg(const char * const from, const char * const to, const char * const message); void win_handle_special_keys(const int * const ch); void win_page_off(void); -void win_contact_online(const char * const from, const char * const show, +void win_contact_online(const char * const from, const char * const show, const char * const status); -void win_contact_offline(const char * const from, const char * const show, +void win_contact_offline(const char * const from, const char * const show, const char * const status); void win_disconnected(void); void win_show(const char * const msg); diff --git a/src/windows.c b/src/windows.c index 74e51a0f..8fe78365 100644 --- a/src/windows.c +++ b/src/windows.c @@ -1,8 +1,8 @@ -/* +/* * windows.c * * Copyright (C) 2012 James Booth - * + * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify @@ -73,19 +73,22 @@ static void _win_switch_if_active(const int i); static void _win_show_time(WINDOW *win); 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 _show_status_string(WINDOW *win, const char * const from, - const char * const show, const char * const status, const char * const pre, +static void _win_show_error_msg(WINDOW *win, const char * const message); +static void _show_status_string(WINDOW *win, const char * const from, + const char * const show, const char * const status, const char * const pre, const char * const default_show); static void _cons_show_typing(const char * const short_from); -static void _cons_show_incoming_message(const char * const short_from, +static void _cons_show_incoming_message(const char * const short_from, const int win_index); static void _win_handle_switch(const int * const ch); static void _win_handle_page(const int * const ch); static void _win_resize_all(void); static gint _win_get_unread(void); +static void _win_show_history(WINDOW *win, int win_index, + const char * const contact); #ifdef HAVE_LIBNOTIFY -static void _win_notify(const char * const message, int timeout, +static void _win_notify(const char * const message, int timeout, const char * const category); static void _win_notify_remind(gint unread); static void _win_notify_message(char * short_from); @@ -100,7 +103,7 @@ gui_init(void) cbreak(); keypad(stdscr, TRUE); - if (has_colors()) { + if (has_colors()) { use_default_colors(); start_color(); @@ -177,13 +180,13 @@ win_close_win(void) // set it as inactive in the status bar status_bar_inactive(_curr_prof_win); - + // go back to console window _curr_prof_win = 0; title_bar_title(); dirty = TRUE; - + // success return 1; } else { @@ -195,7 +198,7 @@ win_close_win(void) int win_in_chat(void) { - return ((_curr_prof_win != 0) && + return ((_curr_prof_win != 0) && (strcmp(_wins[_curr_prof_win].from, "") != 0)); } @@ -208,7 +211,7 @@ win_get_recipient(void) return recipient; } -void +void win_show_typing(const char * const from) { char from_cpy[strlen(from) + 1]; @@ -253,7 +256,7 @@ win_remind(void) } void -win_show_incomming_msg(const char * const from, const char * const message) +win_show_incomming_msg(const char * const from, const char * const message) { char from_cpy[strlen(from) + 1]; strcpy(from_cpy, from); @@ -264,7 +267,7 @@ win_show_incomming_msg(const char * const from, const char * const message) win_index = _new_prof_win(short_from); WINDOW *win = _wins[win_index].win; - + // currently viewing chat window with sender if (win_index == _curr_prof_win) { _win_show_time(win); @@ -284,17 +287,9 @@ win_show_incomming_msg(const char * const from, const char * const message) _wins[win_index].unread++; if (prefs_get_chlog() && prefs_get_history()) { - if (!_wins[win_index].history_shown) { - GSList *history = NULL; - history = chat_log_get_previous(jabber_get_jid(), short_from, history); - while (history != NULL) { - wprintw(win, "%s\n", history->data); - history = g_slist_next(history); - } - _wins[win_index].history_shown = 1; - } + _win_show_history(win, win_index, short_from); } - + _win_show_time(win); _win_show_user(win, short_from, 1); _win_show_message(win, message); @@ -308,9 +303,30 @@ win_show_incomming_msg(const char * const from, const char * const message) #endif } +void +win_show_error_msg(const char * const from, const char *err_msg) +{ + int win_index; + WINDOW *win; + + if (from == NULL || err_msg == NULL) + return; + + win_index = _find_prof_win_index(from); + // chat window exists + if (win_index < NUM_WINS) { + win = _wins[win_index].win; + _win_show_time(win); + _win_show_error_msg(win, err_msg); + if (win_index == _curr_prof_win) { + dirty = TRUE; + } + } +} + #ifdef HAVE_LIBNOTIFY static void -_win_notify(const char * const message, int timeout, +_win_notify(const char * const message, int timeout, const char * const category) { gboolean notify_initted = notify_is_initted(); @@ -348,7 +364,7 @@ _win_notify_remind(gint unread) } else { sprintf(message, "%d unread messages", unread); } - + _win_notify(message, 5000, "Incoming message"); } @@ -357,8 +373,8 @@ _win_notify_message(char * short_from) { char message[strlen(short_from) + 1 + 10]; sprintf(message, "%s: message.", short_from); - - _win_notify(message, 10000, "Incoming message"); + + _win_notify(message, 10000, "Incoming message"); } static void @@ -366,57 +382,46 @@ _win_notify_typing(char * short_from) { char message[strlen(short_from) + 1 + 11]; sprintf(message, "%s: typing...", short_from); - + _win_notify(message, 10000, "Incoming message"); } #endif void -win_show_outgoing_msg(const char * const from, const char * const to, +win_show_outgoing_msg(const char * const from, const char * const to, const char * const message) { // if the contact is offline, show a message PContact contact = contact_list_get_contact(to); - - if (contact == NULL) { - cons_show("%s is not one of your contacts."); - } else { - int win_index = _find_prof_win_index(to); - WINDOW *win = NULL; + int win_index = _find_prof_win_index(to); + WINDOW *win = NULL; - // create new window - if (win_index == NUM_WINS) { - win_index = _new_prof_win(to); - win = _wins[win_index].win; - - if (prefs_get_chlog() && prefs_get_history()) { - if (!_wins[win_index].history_shown) { - GSList *history = NULL; - history = chat_log_get_previous(jabber_get_jid(), to, history); - while (history != NULL) { - wprintw(win, "%s\n", history->data); - history = g_slist_next(history); - } - _wins[win_index].history_shown = 1; - } - } + // create new window + if (win_index == NUM_WINS) { + win_index = _new_prof_win(to); + win = _wins[win_index].win; + if (prefs_get_chlog() && prefs_get_history()) { + _win_show_history(win, win_index, to); + } + + if (contact != NULL) { if (strcmp(p_contact_show(contact), "offline") == 0) { const char const *show = p_contact_show(contact); const char const *status = p_contact_status(contact); _show_status_string(win, to, show, status, "--", "offline"); } - - // use existing window - } else { - win = _wins[win_index].win; } - _win_show_time(win); - _win_show_user(win, from, 0); - _win_show_message(win, message); - _win_switch_if_active(win_index); + // use existing window + } else { + win = _wins[win_index].win; } + + _win_show_time(win); + _win_show_user(win, from, 0); + _win_show_message(win, message); + _win_switch_if_active(win_index); } void @@ -424,7 +429,7 @@ win_show(const char * const msg) { WINDOW *win = _wins[_curr_prof_win].win; _win_show_time(win); - wprintw(win, "%s\n", msg); + wprintw(win, "%s\n", msg); dirty = TRUE; } @@ -437,12 +442,12 @@ win_bad_show(const char * const msg) wattron(win, COLOUR_ERR); wprintw(win, "%s\n", msg); wattroff(win, COLOUR_ERR); - + dirty = TRUE; } void -win_contact_online(const char * const from, const char * const show, +win_contact_online(const char * const from, const char * const show, const char * const status) { _show_status_string(_cons_win, from, show, status, "++", "online"); @@ -452,13 +457,13 @@ win_contact_online(const char * const from, const char * const show, WINDOW *win = _wins[win_index].win; _show_status_string(win, from, show, status, "++", "online"); } - + if (win_index == _curr_prof_win) dirty = TRUE; } void -win_contact_offline(const char * const from, const char * const show, +win_contact_offline(const char * const from, const char * const show, const char * const status) { _show_status_string(_cons_win, from, show, status, "--", "offline"); @@ -468,7 +473,7 @@ win_contact_offline(const char * const from, const char * const show, WINDOW *win = _wins[win_index].win; _show_status_string(win, from, show, status, "--", "offline"); } - + if (win_index == _curr_prof_win) dirty = TRUE; } @@ -485,7 +490,7 @@ win_disconnected(void) wattron(win, COLOUR_ERR); wprintw(win, "%s\n", "Lost connection."); wattroff(win, COLOUR_ERR); - + // if current win, set dirty if (i == _curr_prof_win) { dirty = TRUE; @@ -500,41 +505,41 @@ cons_prefs(void) cons_show(""); cons_show("Current preferences:"); cons_show(""); - + if (prefs_get_beep()) cons_show("Terminal beep : ON"); else - cons_show("Terminal beep : OFF"); - + cons_show("Terminal beep : OFF"); + if (prefs_get_flash()) cons_show("Terminal flash : ON"); else - cons_show("Terminal flash : OFF"); - + cons_show("Terminal flash : OFF"); + if (prefs_get_notify()) cons_show("Message notifications : ON"); else - cons_show("Message notifications : OFF"); + cons_show("Message notifications : OFF"); if (prefs_get_typing()) cons_show("Typing notifications : ON"); else - cons_show("Typing notifications : OFF"); + cons_show("Typing notifications : OFF"); if (prefs_get_showsplash()) cons_show("Splash screen : ON"); else - cons_show("Splash screen : OFF"); - + cons_show("Splash screen : OFF"); + if (prefs_get_chlog()) cons_show("Chat logging : ON"); else - cons_show("Chat logging : OFF"); + cons_show("Chat logging : OFF"); if (prefs_get_history()) cons_show("Chat history : ON"); else - cons_show("Chat history : OFF"); + cons_show("Chat history : OFF"); gint remind_period = prefs_get_remind(); if (remind_period == 0) { @@ -616,8 +621,8 @@ cons_show_contacts(GSList *list) while(curr) { PContact contact = curr->data; - const char *show = p_contact_show(contact); - + const char *show = p_contact_show(contact); + _win_show_time(_cons_win); if (strcmp(show, "online") == 0) { @@ -636,10 +641,10 @@ cons_show_contacts(GSList *list) wprintw(_cons_win, "%s", p_contact_name(contact)); wprintw(_cons_win, " is %s", show); - + if (p_contact_status(contact)) wprintw(_cons_win, ", \"%s\"", p_contact_status(contact)); - + wprintw(_cons_win, "\n"); if (strcmp(show, "online") == 0) { @@ -667,7 +672,7 @@ cons_bad_show(const char * const msg) wattron(_cons_win, COLOUR_ERR); wprintw(_cons_win, "%s\n", msg); wattroff(_cons_win, COLOUR_ERR); - + if (_curr_prof_win == 0) dirty = TRUE; } @@ -680,10 +685,10 @@ cons_show(const char * const msg, ...) GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); _win_show_time(_cons_win); - wprintw(_cons_win, "%s\n", fmt_msg->str); + wprintw(_cons_win, "%s\n", fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); - + if (_curr_prof_win == 0) dirty = TRUE; } @@ -693,7 +698,7 @@ cons_bad_command(const char * const cmd) { _win_show_time(_cons_win); wprintw(_cons_win, "Unknown command: %s\n", cmd); - + if (_curr_prof_win == 0) dirty = TRUE; } @@ -710,7 +715,7 @@ win_page_off(void) { int rows = getmaxy(stdscr); _wins[_curr_prof_win].paged = 0; - + int y = getcury(_wins[_curr_prof_win].win); int size = rows - 3; @@ -742,7 +747,7 @@ _create_windows(void) _wins[0] = cons; _cons_win = _wins[0].win; - + if (prefs_get_showsplash()) { _cons_splash_logo(); } else { @@ -766,11 +771,11 @@ _create_windows(void) wprintw(_cons_win, "Type '/help' to show complete help.\n"); _win_show_time(_cons_win); wprintw(_cons_win, "\n"); - + prefresh(_cons_win, 0, 0, 1, 0, rows-3, cols-1); dirty = TRUE; - + // create the chat windows int i; for (i = 1; i < NUM_WINS; i++) { @@ -784,7 +789,7 @@ _create_windows(void) chat.history_shown = 0; scrollok(chat.win, TRUE); _wins[i] = chat; - } + } } static void @@ -870,7 +875,7 @@ _win_switch_if_active(const int i) if (strcmp(_wins[i].from, "") != 0) { _curr_prof_win = i; win_page_off(); - + _wins[i].unread = 0; if (i == 0) { @@ -888,7 +893,7 @@ _win_switch_if_active(const int i) static void _win_show_time(WINDOW *win) { - GDateTime *time = g_date_time_new_now_local(); + GDateTime *time = g_date_time_new_now_local(); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); wprintw(win, "%s - ", date_fmt); g_date_time_unref(time); @@ -915,6 +920,14 @@ _win_show_message(WINDOW *win, const char * const message) wprintw(win, "%s\n", message); } +static void +_win_show_error_msg(WINDOW *win, const char * const message) +{ + wattron(win, COLOUR_ERR); + wprintw(win, "%s\n", message); + wattroff(win, COLOUR_ERR); +} + static void _current_window_refresh(void) { @@ -940,18 +953,18 @@ _win_resize_all(void) wresize(_wins[i].win, PAD_SIZE, cols); } } - + WINDOW *current = _wins[_curr_prof_win].win; prefresh(current, _wins[_curr_prof_win].y_pos, 0, 1, 0, rows-3, cols-1); } static void -_show_status_string(WINDOW *win, const char * const from, - const char * const show, const char * const status, const char * const pre, +_show_status_string(WINDOW *win, const char * const from, + const char * const show, const char * const status, const char * const pre, const char * const default_show) { - _win_show_time(win); - + _win_show_time(win); + if (show != NULL) { if (strcmp(show, "away") == 0) { wattron(win, COLOUR_AWAY); @@ -974,17 +987,17 @@ _show_status_string(WINDOW *win, const char * const from, wprintw(win, "%s %s", pre, from); - if (show != NULL) + if (show != NULL) wprintw(win, " is %s", show); else wprintw(win, " is %s", default_show); - + if (status != NULL) wprintw(win, ", \"%s\"", status); - + wprintw(win, "\n"); - - if (show != NULL) { + + if (show != NULL) { if (strcmp(show, "away") == 0) { wattroff(win, COLOUR_AWAY); } else if (strcmp(show, "chat") == 0) { @@ -1057,15 +1070,15 @@ _win_handle_page(const int * const ch) int page_space = rows - 4; int *page_start = &_wins[_curr_prof_win].y_pos; - + // page up if (*ch == KEY_PPAGE) { *page_start -= page_space; - + // went past beginning, show first page if (*page_start < 0) *page_start = 0; - + _wins[_curr_prof_win].paged = 1; dirty = TRUE; @@ -1080,7 +1093,7 @@ _win_handle_page(const int * const ch) // went past end, show full screen else if (*page_start >= y) *page_start = y - page_space; - + _wins[_curr_prof_win].paged = 1; dirty = TRUE; } @@ -1097,3 +1110,19 @@ _win_get_unread(void) return result; } +static void +_win_show_history(WINDOW *win, int win_index, const char * const contact) +{ + if (!_wins[win_index].history_shown) { + GSList *history = NULL; + history = chat_log_get_previous(jabber_get_jid(), contact, history); + while (history != NULL) { + wprintw(win, "%s\n", history->data); + history = g_slist_next(history); + } + _wins[win_index].history_shown = 1; + + g_slist_free_full(history, free); + } +} + diff --git a/tests/test_contact_list.c b/tests/test_contact_list.c index 599c8ca1..18d5a6e4 100644 --- a/tests/test_contact_list.c +++ b/tests/test_contact_list.c @@ -59,10 +59,10 @@ static void first_and_second_elements_correct(void) contact_list_add("James", NULL, NULL); contact_list_add("Dave", NULL, NULL); GSList *list = get_contact_list(); - + PContact dave = list->data; PContact james = (g_slist_next(list))->data; - + assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); } @@ -73,7 +73,7 @@ static void contains_three_elements(void) contact_list_add("Bob", NULL, NULL); contact_list_add("Dave", NULL, NULL); GSList *list = get_contact_list(); - + assert_int_equals(3, g_slist_length(list)); } @@ -86,7 +86,7 @@ static void first_three_elements_correct(void) PContact bob = list->data; PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; - + assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); assert_string_equals("Bob", p_contact_name(bob)); @@ -102,8 +102,8 @@ static void add_twice_at_beginning_adds_once(void) PContact bob = list->data; PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; - - assert_int_equals(3, g_slist_length(list)); + + assert_int_equals(3, g_slist_length(list)); assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); assert_string_equals("Bob", p_contact_name(bob)); @@ -119,8 +119,8 @@ static void add_twice_in_middle_adds_once(void) PContact bob = list->data; PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; - - assert_int_equals(3, g_slist_length(list)); + + assert_int_equals(3, g_slist_length(list)); assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); assert_string_equals("Bob", p_contact_name(bob)); @@ -136,8 +136,8 @@ static void add_twice_at_end_adds_once(void) PContact bob = list->data; PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; - - assert_int_equals(3, g_slist_length(list)); + + assert_int_equals(3, g_slist_length(list)); assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); assert_string_equals("Bob", p_contact_name(bob)); @@ -156,7 +156,7 @@ static void remove_when_one_removes(void) contact_list_add("James", NULL, NULL); contact_list_remove("James"); GSList *list = get_contact_list(); - + assert_int_equals(0, g_slist_length(list)); } @@ -167,7 +167,7 @@ static void remove_first_when_two(void) contact_list_remove("James"); GSList *list = get_contact_list(); - + assert_int_equals(1, g_slist_length(list)); PContact dave = list->data; assert_string_equals("Dave", p_contact_name(dave)); @@ -180,7 +180,7 @@ static void remove_second_when_two(void) contact_list_remove("Dave"); GSList *list = get_contact_list(); - + assert_int_equals(1, g_slist_length(list)); PContact james = list->data; assert_string_equals("James", p_contact_name(james)); @@ -194,11 +194,11 @@ static void remove_first_when_three(void) contact_list_remove("James"); GSList *list = get_contact_list(); - + assert_int_equals(2, g_slist_length(list)); PContact bob = list->data; PContact dave = (g_slist_next(list))->data; - + assert_string_equals("Dave", p_contact_name(dave)); assert_string_equals("Bob", p_contact_name(bob)); } @@ -211,11 +211,11 @@ static void remove_second_when_three(void) contact_list_remove("Dave"); GSList *list = get_contact_list(); - + assert_int_equals(2, g_slist_length(list)); PContact bob = list->data; PContact james = (g_slist_next(list))->data; - + assert_string_equals("James", p_contact_name(james)); assert_string_equals("Bob", p_contact_name(bob)); } @@ -228,11 +228,11 @@ static void remove_third_when_three(void) contact_list_remove("Bob"); GSList *list = get_contact_list(); - + assert_int_equals(2, g_slist_length(list)); PContact dave = list->data; PContact james = (g_slist_next(list))->data; - + assert_string_equals("James", p_contact_name(james)); assert_string_equals("Dave", p_contact_name(dave)); } @@ -242,7 +242,7 @@ static void test_show_when_value(void) contact_list_add("James", "away", NULL); GSList *list = get_contact_list(); PContact james = list->data; - + assert_string_equals("away", p_contact_show(james)); } @@ -251,7 +251,7 @@ static void test_show_online_when_no_value(void) contact_list_add("James", NULL, NULL); GSList *list = get_contact_list(); PContact james = list->data; - + assert_string_equals("online", p_contact_show(james)); } @@ -260,7 +260,7 @@ static void test_show_online_when_empty_string(void) contact_list_add("James", "", NULL); GSList *list = get_contact_list(); PContact james = list->data; - + assert_string_equals("online", p_contact_show(james)); } @@ -269,7 +269,7 @@ static void test_status_when_value(void) contact_list_add("James", NULL, "I'm not here right now"); GSList *list = get_contact_list(); PContact james = list->data; - + assert_string_equals("I'm not here right now", p_contact_status(james)); } @@ -278,7 +278,7 @@ static void test_status_when_no_value(void) contact_list_add("James", NULL, NULL); GSList *list = get_contact_list(); PContact james = list->data; - + assert_is_null(p_contact_status(james)); } @@ -339,7 +339,7 @@ static void find_first_exists(void) char *search = (char *) malloc(2 * sizeof(char)); strcpy(search, "B"); - char *result = find_contact(search); + char *result = contact_list_find_contact(search); assert_string_equals("Bob", result); free(result); free(search); @@ -351,7 +351,7 @@ static void find_second_exists(void) contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); - char *result = find_contact("Dav"); + char *result = contact_list_find_contact("Dav"); assert_string_equals("Dave", result); free(result); } @@ -362,7 +362,7 @@ static void find_third_exists(void) contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); - char *result = find_contact("Ja"); + char *result = contact_list_find_contact("Ja"); assert_string_equals("James", result); free(result); } @@ -373,13 +373,13 @@ static void find_returns_null(void) contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); - char *result = find_contact("Mike"); + char *result = contact_list_find_contact("Mike"); assert_is_null(result); } static void find_on_empty_returns_null(void) { - char *result = find_contact("James"); + char *result = contact_list_find_contact("James"); assert_is_null(result); } @@ -389,8 +389,8 @@ static void find_twice_returns_second_when_two_match(void) contact_list_add("Jamie", NULL, NULL); contact_list_add("Bob", NULL, NULL); - char *result1 = find_contact("Jam"); - char *result2 = find_contact(result1); + char *result1 = contact_list_find_contact("Jam"); + char *result2 = contact_list_find_contact(result1); assert_string_equals("Jamie", result2); free(result1); free(result2); @@ -409,11 +409,11 @@ static void find_five_times_finds_fifth(void) contact_list_add("Jamy", NULL, NULL); contact_list_add("Jamz", NULL, NULL); - char *result1 = find_contact("Jam"); - char *result2 = find_contact(result1); - char *result3 = find_contact(result2); - char *result4 = find_contact(result3); - char *result5 = find_contact(result4); + char *result1 = contact_list_find_contact("Jam"); + char *result2 = contact_list_find_contact(result1); + char *result3 = contact_list_find_contact(result2); + char *result4 = contact_list_find_contact(result3); + char *result5 = contact_list_find_contact(result4); assert_string_equals("Jamo", result5); free(result1); free(result2); @@ -428,9 +428,9 @@ static void find_twice_returns_first_when_two_match_and_reset(void) contact_list_add("Jamie", NULL, NULL); contact_list_add("Bob", NULL, NULL); - char *result1 = find_contact("Jam"); - reset_search_attempts(); - char *result2 = find_contact(result1); + char *result1 = contact_list_find_contact("Jam"); + contact_list_reset_search_attempts(); + char *result2 = contact_list_find_contact(result1); assert_string_equals("James", result2); free(result1); free(result2); @@ -444,10 +444,10 @@ static void removed_contact_not_in_search(void) contact_list_add("James", NULL, NULL); contact_list_add("Jamie", NULL, NULL); - char *result1 = find_contact("Jam"); // Jamatron - char *result2 = find_contact(result1); // Jambo + char *result1 = contact_list_find_contact("Jam"); // Jamatron + char *result2 = contact_list_find_contact(result1); // Jambo contact_list_remove("James"); - char *result3 = find_contact(result2); + char *result3 = contact_list_find_contact(result2); assert_string_equals("Jamie", result3); free(result1); free(result2); diff --git a/tests/test_prof_autocomplete.c b/tests/test_prof_autocomplete.c index 45717abd..895a4a44 100644 --- a/tests/test_prof_autocomplete.c +++ b/tests/test_prof_autocomplete.c @@ -15,7 +15,7 @@ static void clear_empty(void) static void clear_empty_with_free_func(void) { - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -49,7 +49,7 @@ static void get_after_create_returns_null(void) static void get_after_create_with_copy_func_returns_null(void) { - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -57,7 +57,7 @@ static void get_after_create_with_copy_func_returns_null(void) GSList *result = p_autocomplete_get_list(ac); assert_is_null(result); - + p_autocomplete_clear(ac); } @@ -76,7 +76,7 @@ static void add_one_and_complete(void) static void add_one_and_complete_with_funcs(void) { PContact contact = p_contact_new("James", "Online", "I'm here"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -85,7 +85,7 @@ static void add_one_and_complete_with_funcs(void) char *result = p_autocomplete_complete(ac, "Jam"); assert_string_equals("James", result); - + p_autocomplete_clear(ac); } @@ -99,7 +99,7 @@ static void add_two_and_complete_returns_first(void) char *result = p_autocomplete_complete(ac, "Hel"); assert_string_equals("Hello", result); - + p_autocomplete_clear(ac); } @@ -107,7 +107,7 @@ static void add_two_and_complete_returns_first_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -117,7 +117,7 @@ static void add_two_and_complete_returns_first_with_funcs(void) char *result = p_autocomplete_complete(ac, "Jam"); assert_string_equals("James", result); - + p_autocomplete_clear(ac); } @@ -132,7 +132,7 @@ static void add_two_and_complete_returns_second(void) char *result2 = p_autocomplete_complete(ac, result1); assert_string_equals("Help", result2); - + p_autocomplete_clear(ac); } @@ -140,7 +140,7 @@ static void add_two_and_complete_returns_second_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -151,7 +151,7 @@ static void add_two_and_complete_returns_second_with_funcs(void) char *result2 = p_autocomplete_complete(ac, result1); assert_string_equals("Jamie", result2); - + p_autocomplete_clear(ac); } @@ -173,7 +173,7 @@ static void add_two_adds_two_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -183,7 +183,7 @@ static void add_two_adds_two_with_funcs(void) GSList *result = p_autocomplete_get_list(ac); assert_int_equals(2, g_slist_length(result)); - + p_autocomplete_clear(ac); } @@ -205,7 +205,7 @@ static void add_two_same_adds_one_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -215,7 +215,7 @@ static void add_two_same_adds_one_with_funcs(void) GSList *result = p_autocomplete_get_list(ac); assert_int_equals(1, g_slist_length(result)); - + p_autocomplete_clear(ac); } @@ -241,7 +241,7 @@ static void add_two_same_updates_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -274,7 +274,7 @@ static void add_one_returns_true(void) static void add_one_returns_true_with_funcs(void) { PContact contact = p_contact_new("James", "Online", "I'm here"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -282,7 +282,7 @@ static void add_one_returns_true_with_funcs(void) int result = p_autocomplete_add(ac, contact); assert_true(result); - + p_autocomplete_clear(ac); } @@ -304,7 +304,7 @@ static void add_two_different_returns_true_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("JamesB", "Away", "Out to lunch"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -336,7 +336,7 @@ static void add_two_same_returns_false_with_funcs(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Online", "I'm here"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, @@ -354,7 +354,7 @@ static void add_two_same_different_data_returns_true(void) { PContact contact1 = p_contact_new("James", "Online", "I'm here"); PContact contact2 = p_contact_new("James", "Away", "I'm not here right now"); - PAutocomplete ac = + PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, (PCopyFunc)p_contact_copy, (PEqualDeepFunc)p_contacts_equal_deep, diff --git a/tests/test_prof_history.c b/tests/test_prof_history.c index 5f08c26c..c58c5957 100644 --- a/tests/test_prof_history.c +++ b/tests/test_prof_history.c @@ -67,8 +67,8 @@ void previous_goes_to_correct_element(void) char *item1 = p_history_previous(history, NULL); char *item2 = p_history_previous(history, item1); char *item3 = p_history_previous(history, item2); - - assert_string_equals("going", item3); + + assert_string_equals("going", item3); } void prev_then_next_returns_empty(void) @@ -128,7 +128,7 @@ void navigate_then_append_new(void) char *item5 = p_history_next(history, item4); assert_string_equals("append", item5); - + char *item6 = p_history_next(history, item5); assert_string_equals("new text", item6); } @@ -144,22 +144,22 @@ void edit_item_mid_history(void) char *item1 = p_history_previous(history, "new item"); assert_string_equals("append", item1); - + char *item2 = p_history_previous(history, item1); assert_string_equals("history", item2); char *item3 = p_history_previous(history, item2); assert_string_equals("testing", item3); - + char *item4 = p_history_previous(history, "EDITED"); - assert_string_equals("again", item4); - + assert_string_equals("again", item4); + char *item5 = p_history_previous(history, item4); assert_string_equals("Hello", item5); char *item6 = p_history_next(history, item5); assert_string_equals("again", item6); - + char *item7 = p_history_next(history, item6); assert_string_equals("EDITED", item7); @@ -184,13 +184,13 @@ void edit_previous_and_append(void) char *item1 = p_history_previous(history, "new item"); assert_string_equals("append", item1); - + char *item2 = p_history_previous(history, item1); assert_string_equals("history", item2); char *item3 = p_history_previous(history, item2); assert_string_equals("testing", item3); - + p_history_append(history, "EDITED"); char *item4 = p_history_previous(history, NULL); @@ -204,7 +204,7 @@ void start_session_add_new_submit_previous(void) char *item1 = p_history_previous(history, NULL); assert_string_equals("hello", item1); - + char *item2 = p_history_next(history, item1); assert_string_equals("", item2);