From 2fc588be178b0dbc033f99671cee26842f59553d Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 3 Jan 2014 20:43:07 +0200 Subject: fixed segfault when home directory doesn't exist --- src/log.c | 58 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/log.c b/src/log.c index 4442dcfd..b82905cf 100644 --- a/src/log.c +++ b/src/log.c @@ -131,13 +131,15 @@ void log_close(void) { g_time_zone_unref(tz); - fclose(logp); + if (logp != NULL) { + fclose(logp); + } } void log_msg(log_level_t level, const char * const area, const char * const msg) { - if (level >= level_filter) { + if (level >= level_filter && logp != NULL) { long result; dt = g_date_time_new_now(tz); @@ -238,25 +240,26 @@ chat_log_chat(const gchar * const login, gchar *other, date_fmt = g_date_time_format(dt, "%H:%M:%S"); FILE *logp = fopen(dated_log->filename, "a"); - - if (direction == PROF_IN_LOG) { - if (strncmp(msg, "/me ", 4) == 0) { - fprintf(logp, "%s - *%s %s\n", date_fmt, other, msg + 4); + if (logp != NULL) { + if (direction == PROF_IN_LOG) { + if (strncmp(msg, "/me ", 4) == 0) { + fprintf(logp, "%s - *%s %s\n", date_fmt, other, msg + 4); + } else { + fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg); + } } else { - fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg); + if (strncmp(msg, "/me ", 4) == 0) { + fprintf(logp, "%s - *me %s\n", date_fmt, msg + 4); + } else { + fprintf(logp, "%s - me: %s\n", date_fmt, msg); + } } - } else { - if (strncmp(msg, "/me ", 4) == 0) { - fprintf(logp, "%s - *me %s\n", date_fmt, msg + 4); - } else { - fprintf(logp, "%s - me: %s\n", date_fmt, msg); + fflush(logp); + int result = fclose(logp); + if (result == EOF) { + log_error("Error closing file %s, errno = %d", dated_log->filename, errno); } } - fflush(logp); - int result = fclose(logp); - if (result == EOF) { - log_error("Error closing file %s, errno = %d", dated_log->filename, errno); - } g_free(date_fmt); g_date_time_unref(dt); @@ -285,17 +288,18 @@ groupchat_log_chat(const gchar * const login, const gchar * const room, gchar *date_fmt = g_date_time_format(dt, "%H:%M:%S"); FILE *logp = fopen(dated_log->filename, "a"); + if (logp != NULL) { + if (strncmp(msg, "/me ", 4) == 0) { + fprintf(logp, "%s - *%s %s\n", date_fmt, nick, msg + 4); + } else { + fprintf(logp, "%s - %s: %s\n", date_fmt, nick, msg); + } - if (strncmp(msg, "/me ", 4) == 0) { - fprintf(logp, "%s - *%s %s\n", date_fmt, nick, msg + 4); - } else { - fprintf(logp, "%s - %s: %s\n", date_fmt, nick, msg); - } - - fflush(logp); - int result = fclose(logp); - if (result == EOF) { - log_error("Error closing file %s, errno = %d", dated_log->filename, errno); + fflush(logp); + int result = fclose(logp); + if (result == EOF) { + log_error("Error closing file %s, errno = %d", dated_log->filename, errno); + } } g_free(date_fmt); -- cgit 1.4.1-2-gfad0 From 9da4a6e1b9f13b253de3d029ef5cfb31a7109fa8 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 5 Jan 2014 00:38:45 +0000 Subject: Refactored contact display string --- src/contact.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/contact.c b/src/contact.c index 0b955548..b2bef275 100644 --- a/src/contact.c +++ b/src/contact.c @@ -171,12 +171,9 @@ p_contact_create_display_string(const PContact contact, const char * const resou { GString *result_str = g_string_new(""); - // use nickname if exists - if (contact->name != NULL) { - g_string_append(result_str, contact->name); - } else { - g_string_append(result_str, contact->barejid); - } + // use nickname if exists + const char *display_name = p_contact_name_or_jid(contact); + g_string_append(result_str, display_name); // add resource if not default provided by profanity if (strcmp(resource, "__prof_default") != 0) { -- cgit 1.4.1-2-gfad0 From 80acfdae9acf523b8e1feaeabfb8147d93c38220 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 5 Jan 2014 00:43:37 +0000 Subject: Added contact presence offline test --- src/contact.c | 6 +++--- tests/test_contact.c | 13 +++++++++++++ tests/test_contact.h | 1 + tests/testsuite.c | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/contact.c b/src/contact.c index b2bef275..f2f74349 100644 --- a/src/contact.c +++ b/src/contact.c @@ -171,9 +171,9 @@ p_contact_create_display_string(const PContact contact, const char * const resou { GString *result_str = g_string_new(""); - // use nickname if exists - const char *display_name = p_contact_name_or_jid(contact); - g_string_append(result_str, display_name); + // use nickname if exists + const char *display_name = p_contact_name_or_jid(contact); + g_string_append(result_str, display_name); // add resource if not default provided by profanity if (strcmp(resource, "__prof_default") != 0) { diff --git a/tests/test_contact.c b/tests/test_contact.c index 332d6fd2..4952a271 100644 --- a/tests/test_contact.c +++ b/tests/test_contact.c @@ -100,3 +100,16 @@ void contact_string_when_default_resource(void **state) p_contact_free(contact); free(str); } + +void contact_presence_offline(void **state) +{ + PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both", + "is offline", FALSE); + + const char *presence = p_contact_presence(contact); + + assert_string_equal("offline", presence); + + p_contact_free(contact); + +} diff --git a/tests/test_contact.h b/tests/test_contact.h index f0ba043f..f196ff65 100644 --- a/tests/test_contact.h +++ b/tests/test_contact.h @@ -5,3 +5,4 @@ void contact_jid_when_name_not_exists(void **state); void contact_string_when_name_exists(void **state); void contact_string_when_name_not_exists(void **state); void contact_string_when_default_resource(void **state); +void contact_presence_offline(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 71f2fdf9..6a107dd2 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -265,6 +265,7 @@ int main(int argc, char* argv[]) { unit_test(contact_string_when_name_exists), unit_test(contact_string_when_name_not_exists), unit_test(contact_string_when_default_resource), + unit_test(contact_presence_offline), }; return run_tests(tests); } -- cgit 1.4.1-2-gfad0