Files
cproof/src/xmpp/contact.c
Jabber Developer 72f4f186da
All checks were successful
CI Code / Check spelling (push) Successful in 18s
CI Code / Check coding style (push) Successful in 34s
CI Code / Code Coverage (push) Successful in 2m36s
CI Code / Linux (debian) (push) Successful in 4m41s
CI Code / Linux (ubuntu) (push) Successful in 4m52s
CI Code / Linux (arch) (push) Successful in 5m40s
merge: sync upstream profanity-im/profanity
Sync with upstream profanity-im/profanity.

Major upstream changes incorporated:

Memory management
  - Replace malloc+memset with g_new0 throughout codebase
  - Adopt auto_gchar / auto_gcharv / auto_gerror / auto_jid cleanup macros
  - Replace free() with g_free() for GAlloc'd memory

Editor rewrite
  - Remove pthread-based async editor; use GChildWatch callback API
  - New launch_editor(initial_content, callback, user_data) interface
  - Proper signal handling (SIGINT, SIGTSTP, SIGPIPE reset in child)
  - ui_suspend()/ui_resume() integration for TTY management

OMEMO improvements
  - Dual backend support: libsignal-protocol-c and libomemo-c
  - Proper pre-key removal after use (XEP-0384 compliance)
  - Automatic pre-key regeneration when store drops below threshold
  - New functions: omemo_is_device_active(), omemo_is_jid_trusted()
  - omemo_get_jid_untrusted_fingerprints() for better error messages
  - Fingerprint notifications on new device identity discovery
  - Deterministic pre-key ID generation tracking max_pre_key_id
  - omemo_trust_changed() UI updates on trust state changes

JID validation
  - RFC 6122-compliant validation in jid_is_valid()
  - Character-level checks (RFC 6122 forbidden chars: & ' / : < > @)
  - Length limits: 1023 per component, 3071 total
  - New jid_is_valid_user_jid() for user vs. service JID distinction

Database
  - Schema migration v3: UNIQUE constraint on archive_id for deduplication
  - Triggers for corrected message tracking (replaces_db_id / replaced_by_db_id)
  - db_history_result_t return type, _truncate_datetime_suffix()

UI / console
  - win_warn_needed() / win_warn_sent() warning deduplication hash table
  - PAD_MIN_HEIGHT dynamic pad sizing with PAD_THRESHOLD auto-cleanup
  - Spellcheck integration in input field with Unicode word detection
  - cons_spellcheck_setting() for /settings ui output
  - /[command]? shortcut for command help

Account config
  - Account name sanitization for GKeyFile special chars ([ ] = # \n \r)
  - Replace popen() with g_spawn_sync() for eval_password
  - TLS policy: add "direct" option alongside legacy

Connection
  - Port validation with g_assert (0–65535)
  - SHA-256 certificate fingerprint support (XMPP_CERT_PUBKEY_FINGERPRINT_SHA256)
  - "direct" TLS policy alias for legacy SSL

Common utilities
  - str_xml_sanitize() for XML 1.0 illegal character removal
  - string_matches_one_of() with formatted error messages
  - valid_tls_policy_option() helper
  - prof_date_time_format_iso8601() utility
  - Improved strip_arg_quotes() with backslash unescaping
  - prof_occurrences() uses g_slist_prepend + reverse for performance

PGP / OX
  - Proper GPGME resource cleanup with goto-cleanup pattern
  - g_string_free(xmppuri) leak fix in _ox_key_lookup

CSV export
  - Use GString + g_file_set_contents instead of raw write() syscalls

Tests
  - Restructured into subdirectories: command/, config/, xmpp/, ui/, omemo/, otr/, pgp/
  - New test_cmd_ac.c for autocompleter unit tests
  - Updated stubs for new UI suspend/resume functions

License headers
  - Migrate to SPDX-3.0 identifiers (GPL-3.0-or-later WITH OpenSSL-exception)

────────────────────────────────────────────────────
cproof-specific preservations:

  - XEP-0308 LMC: replace_id ?: id logic in message/stanza/omemo
  - Force encryption: cmd_force_encryption, test_forced_encryption
  - CWE-134: format string protection (cons_show("%s", ...))
  - y_start_pos-based paging in window.c
  - db_history_result_t return type, _truncate_datetime_suffix()

Merge-time fixes:

  - common.c: format-security (-Werror) — cons_show(errmsg) → cons_show("%s", errmsg)
  - database.c: null-deref guard — !msg->timestamp → msg && !msg->timestamp
  - console.c: implicit size_t → int cast — (int)(maxlen + 1)
  - tlscerts.c: %d for size_t — %zu

Build system:
  - Kept autotools (Makefile.am, configure.ac); upstream uses Meson
  - Restored deleted files: bootstrap.sh, autogen.sh, ax_valgrind_check.m4, configure-debug
  - Updated Makefile.am test paths for subdirectory structure
  - Added test_cmd_ac, test_forced_encryption to test sources

Functional tests:
  - Use cproof version; upstream requires stbbr_for_xmlns from updated stabber
  - Not yet available in devs/stabber fork

Closes #64

Merge author: jabber.developer2
Commits authors:
 Michael Vetter <jubalh@iodoru.org>
& Steffen Jaeckel <s@jaeckel.eu>
2026-05-26 17:48:14 +00:00

422 lines
9.9 KiB
C

/*
* contact.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "common.h"
#include "tools/autocomplete.h"
#include "xmpp/resource.h"
#include "xmpp/contact.h"
struct p_contact_t
{
char* barejid;
gchar* barejid_collate_key;
char* name;
gchar* name_collate_key;
GSList* groups;
char* subscription;
char* offline_message;
gboolean pending_out;
GDateTime* last_activity;
GHashTable* available_resources;
Autocomplete resource_ac;
};
PContact
p_contact_new(const char* const barejid, const char* const name,
GSList* groups, const char* const subscription,
const char* const offline_message, gboolean pending_out)
{
PContact contact = g_new0(struct p_contact_t, 1);
contact->barejid = strdup(barejid);
contact->barejid_collate_key = g_utf8_collate_key(contact->barejid, -1);
if (name) {
contact->name = strdup(name);
contact->name_collate_key = g_utf8_collate_key(contact->name, -1);
} else {
contact->name = NULL;
contact->name_collate_key = NULL;
}
contact->groups = groups;
if (subscription)
contact->subscription = strdup(subscription);
else
contact->subscription = strdup("none");
if (offline_message)
contact->offline_message = strdup(offline_message);
else
contact->offline_message = NULL;
contact->pending_out = pending_out;
contact->last_activity = NULL;
contact->available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free,
(GDestroyNotify)resource_destroy);
contact->resource_ac = autocomplete_new();
return contact;
}
void
p_contact_set_name(const PContact contact, const char* const name)
{
FREE_SET_NULL(contact->name);
FREE_SET_NULL(contact->name_collate_key);
if (name) {
contact->name = strdup(name);
contact->name_collate_key = g_utf8_collate_key(contact->name, -1);
}
}
void
p_contact_set_groups(const PContact contact, GSList* groups)
{
if (contact->groups) {
g_slist_free_full(contact->groups, g_free);
contact->groups = NULL;
}
contact->groups = groups;
}
gboolean
p_contact_in_group(const PContact contact, const char* const group)
{
GSList* groups = contact->groups;
while (groups) {
if (strcmp(groups->data, group) == 0) {
return TRUE;
}
groups = g_slist_next(groups);
}
return FALSE;
}
GSList*
p_contact_groups(const PContact contact)
{
return contact->groups;
}
gboolean
p_contact_remove_resource(PContact contact, const char* const resource)
{
gboolean result = g_hash_table_remove(contact->available_resources, resource);
autocomplete_remove(contact->resource_ac, resource);
return result;
}
void
p_contact_free(PContact contact)
{
if (contact) {
free(contact->barejid);
free(contact->barejid_collate_key);
free(contact->name);
free(contact->name_collate_key);
free(contact->subscription);
free(contact->offline_message);
if (contact->groups) {
g_slist_free_full(contact->groups, g_free);
}
if (contact->last_activity) {
g_date_time_unref(contact->last_activity);
}
g_hash_table_destroy(contact->available_resources);
autocomplete_free(contact->resource_ac);
free(contact);
}
}
const char*
p_contact_barejid(const PContact contact)
{
return contact->barejid;
}
const char*
p_contact_barejid_collate_key(const PContact contact)
{
return contact->barejid_collate_key;
}
const char*
p_contact_name(const PContact contact)
{
if (!contact)
return NULL;
return contact->name;
}
const char*
p_contact_name_collate_key(const PContact contact)
{
return contact->name_collate_key;
}
const char*
p_contact_name_or_jid(const PContact contact)
{
if (contact->name) {
return contact->name;
} else {
return contact->barejid;
}
}
char*
p_contact_create_display_string(const PContact contact, const char* const resource)
{
gchar* result;
// use nickname if exists
const char* display_name = p_contact_name_or_jid(contact);
// add resource if not default provided by profanity
if (strcmp(resource, "__prof_default") != 0) {
result = g_strdup_printf("%s (%s)", display_name, resource);
} else {
result = g_strdup_printf("%s", display_name);
}
return result;
}
static Resource*
_highest_presence(Resource* first, Resource* second)
{
if (first->presence == RESOURCE_CHAT) {
return first;
} else if (second->presence == RESOURCE_CHAT) {
return second;
} else if (first->presence == RESOURCE_ONLINE) {
return first;
} else if (second->presence == RESOURCE_ONLINE) {
return second;
} else if (first->presence == RESOURCE_AWAY) {
return first;
} else if (second->presence == RESOURCE_AWAY) {
return second;
} else if (first->presence == RESOURCE_XA) {
return first;
} else if (second->presence == RESOURCE_XA) {
return second;
} else {
return first;
}
}
Resource*
_get_most_available_resource(PContact contact)
{
// find resource with highest priority, if more than one,
// use highest availability, in the following order:
// chat
// online
// away
// xa
// dnd
GList* resources = g_hash_table_get_values(contact->available_resources);
GList* curr = resources;
Resource* current = curr->data;
Resource* highest = current;
curr = g_list_next(curr);
while (curr) {
current = curr->data;
// priority is same as current highest, choose presence
if (current->priority == highest->priority) {
highest = _highest_presence(highest, current);
// priority higher than current highest, set new presence
} else if (current->priority > highest->priority) {
highest = current;
}
curr = g_list_next(curr);
}
g_list_free(resources);
return highest;
}
const char*
p_contact_presence(const PContact contact)
{
assert(contact != NULL);
// no available resources, offline
if (g_hash_table_size(contact->available_resources) == 0) {
return "offline";
}
Resource* resource = _get_most_available_resource(contact);
return string_from_resource_presence(resource->presence);
}
const char*
p_contact_status(const PContact contact)
{
assert(contact != NULL);
// no available resources, use offline message
if (g_hash_table_size(contact->available_resources) == 0) {
return contact->offline_message;
}
Resource* resource = _get_most_available_resource(contact);
return resource->status;
}
const char*
p_contact_subscription(const PContact contact)
{
return contact->subscription;
}
gboolean
p_contact_subscribed(const PContact contact)
{
if (contact->subscription == NULL) {
return FALSE;
} else if (strcmp(contact->subscription, "to") == 0) {
return TRUE;
} else if (strcmp(contact->subscription, "both") == 0) {
return TRUE;
} else {
return FALSE;
}
}
Resource*
p_contact_get_resource(const PContact contact, const char* const resource)
{
return g_hash_table_lookup(contact->available_resources, resource);
}
gboolean
p_contact_pending_out(const PContact contact)
{
return contact->pending_out;
}
GDateTime*
p_contact_last_activity(const PContact contact)
{
return contact->last_activity;
}
GList*
p_contact_get_available_resources(const PContact contact)
{
assert(contact != NULL);
GList* resources = g_hash_table_get_values(contact->available_resources);
GList* ordered = NULL;
GList* curr_resource = resources;
while (curr_resource) {
Resource* resource = curr_resource->data;
ordered = g_list_insert_sorted(ordered, resource, (GCompareFunc)resource_compare_availability);
curr_resource = g_list_next(curr_resource);
}
g_list_free(resources);
return ordered;
}
gboolean
p_contact_is_available(const PContact contact)
{
// no available resources, unavailable
if (g_hash_table_size(contact->available_resources) == 0) {
return FALSE;
}
// if most available resource is CHAT or ONLINE, available
Resource* most_available = _get_most_available_resource(contact);
if ((most_available->presence == RESOURCE_ONLINE) || (most_available->presence == RESOURCE_CHAT)) {
return TRUE;
} else {
return FALSE;
}
}
gboolean
p_contact_has_available_resource(const PContact contact)
{
return (g_hash_table_size(contact->available_resources) > 0);
}
void
p_contact_set_presence(const PContact contact, Resource* resource)
{
g_hash_table_replace(contact->available_resources, strdup(resource->name), resource);
autocomplete_add(contact->resource_ac, resource->name);
}
void
p_contact_set_subscription(const PContact contact, const char* const subscription)
{
FREE_SET_NULL(contact->subscription);
if (subscription) {
contact->subscription = strdup(subscription);
}
}
void
p_contact_set_pending_out(const PContact contact, gboolean pending_out)
{
contact->pending_out = pending_out;
}
void
p_contact_set_last_activity(const PContact contact, GDateTime* last_activity)
{
if (contact->last_activity) {
g_date_time_unref(contact->last_activity);
contact->last_activity = NULL;
}
if (last_activity) {
contact->last_activity = g_date_time_ref(last_activity);
}
}
Autocomplete
p_contact_resource_ac(const PContact contact)
{
return contact->resource_ac;
}
void
p_contact_resource_ac_reset(const PContact contact)
{
autocomplete_reset(contact->resource_ac);
}