Files
cproof/src/xmpp/resource.c
jabber.developer2 4401e817d0
Some checks failed
CI Code / Check spelling (pull_request) Successful in 21s
CI Code / Check coding style (pull_request) Successful in 35s
CI Code / Linux (arch) (pull_request) Failing after 2m52s
CI Code / Code Coverage (pull_request) Successful in 2m56s
CI Code / Linux (debian) (pull_request) Successful in 6m48s
CI Code / Linux (ubuntu) (pull_request) Successful in 7m11s
refactor: address PR #105 review feedback
Apply fixes, refactors and test additions requested by reviewer on PR #105.

Fixes:
- database: warn and notify user on duplicate archive_id instead of
  silently debug-logging it (R05).
- database: add missing '[' in "[DB Migration]" log prefix (R06).
- xmpp/resource: NULL-out name/status after g_free to avoid double-free
  via roster_list.c cleanup path (R09, R23).
- common: widen strtoi_range internal storage from int to long so that
  values in (INT_MAX, LONG_MAX] are rejected as out-of-range instead of
  being silently truncated on 64-bit platforms (R25).

Refactors:
- xmpp/message: extract _receive_omemo helper, removing three copies of
  the OMEMO receive block in groupchat / MUC-PM / chat handlers (R04).
- omemo: flatten deeply nested device-list processing via guard-clause
  continues (R11).
- tools/autocomplete: merge two nested ifs into a single && condition
  (R13).
- ui/titlebar: extract _show_trust_indicator and inline _wprintw_withattr
  wrapper, collapsing three near-identical trust-indicator blocks (R22).
- config/tlscerts: drop _checked_g_strdup wrapper; g_strdup is
  NULL-safe per glib documentation (R19).
- ui/inputwin: use auto_gchar for spellcheck word instead of manual
  g_free (R20).
- tools/editor: drop outdated "Deprecated synchronous" comment that
  no longer matches the callback-based implementation (R28).

Tests:
- tests/command/cmd_ac: rename segfaults_when_empty ->
  no_segfault_when_empty; expand cycling coverage to three files plus
  backward SHIFT-TAB traversal (R16, R17).
- tests/common: add strtoi_range overflow/underflow and strtol-parsing
  consistency tests (R25).
- tests/xmpp/jid: add test for '@' inside resourcepart per RFC 6122
  section 2.4 (R26).

Misc:
- xmpp/omemo: change omemo_error_to_string return type from char* to
  gchar* for glib consistency (R01).
- subprojects/libstrophe: point wrap-git at our fork at
  git.jabber.space/devs/libstrophe-gh (R24).
- RELEASE_GUIDE: drop "Updating website" section referring to an
  upstream site that is not ours (R18).
2026-04-24 15:13:38 +03:00

154 lines
3.7 KiB
C

/*
* resource.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 "xmpp/resource.h"
Resource*
resource_new(const gchar* const name, resource_presence_t presence, const gchar* const status, const int priority)
{
assert(name != NULL);
Resource* new_resource = g_new(Resource, 1);
new_resource->name = g_strdup(name);
new_resource->presence = presence;
if (status) {
new_resource->status = g_strdup(status);
} else {
new_resource->status = NULL;
}
new_resource->priority = priority;
return new_resource;
}
int
resource_compare_availability(Resource* first, Resource* second)
{
if (first->priority > second->priority) {
return -1;
} else if (first->priority < second->priority) {
return 1;
} else { // priorities equal
if (first->presence == RESOURCE_CHAT) {
return -1;
} else if (second->presence == RESOURCE_CHAT) {
return 1;
} else if (first->presence == RESOURCE_ONLINE) {
return -1;
} else if (second->presence == RESOURCE_ONLINE) {
return 1;
} else if (first->presence == RESOURCE_AWAY) {
return -1;
} else if (second->presence == RESOURCE_AWAY) {
return 1;
} else if (first->presence == RESOURCE_XA) {
return -1;
} else if (second->presence == RESOURCE_XA) {
return 1;
} else {
return -1;
}
}
}
void
resource_destroy(Resource* resource)
{
if (resource) {
g_free(resource->name);
resource->name = NULL;
g_free(resource->status);
resource->status = NULL;
g_free(resource);
}
}
Resource*
resource_copy(Resource* resource)
{
if (resource == NULL) {
return NULL;
}
return resource_new(resource->name, resource->presence, resource->status, resource->priority);
}
gboolean
valid_resource_presence_string(const gchar* const str)
{
assert(str != NULL);
if ((strcmp(str, "online") == 0) || (strcmp(str, "chat") == 0) || (strcmp(str, "away") == 0) || (strcmp(str, "xa") == 0) || (strcmp(str, "dnd") == 0)) {
return TRUE;
} else {
return FALSE;
}
}
const gchar*
string_from_resource_presence(resource_presence_t presence)
{
switch (presence) {
case RESOURCE_CHAT:
return "chat";
case RESOURCE_AWAY:
return "away";
case RESOURCE_XA:
return "xa";
case RESOURCE_DND:
return "dnd";
default:
return "online";
}
}
resource_presence_t
resource_presence_from_string(const gchar* const str)
{
if (str == NULL) {
return RESOURCE_ONLINE;
} else if (strcmp(str, "online") == 0) {
return RESOURCE_ONLINE;
} else if (strcmp(str, "chat") == 0) {
return RESOURCE_CHAT;
} else if (strcmp(str, "away") == 0) {
return RESOURCE_AWAY;
} else if (strcmp(str, "xa") == 0) {
return RESOURCE_XA;
} else if (strcmp(str, "dnd") == 0) {
return RESOURCE_DND;
} else {
return RESOURCE_ONLINE;
}
}
contact_presence_t
contact_presence_from_resource_presence(resource_presence_t resource_presence)
{
switch (resource_presence) {
case RESOURCE_CHAT:
return CONTACT_CHAT;
case RESOURCE_AWAY:
return CONTACT_AWAY;
case RESOURCE_XA:
return CONTACT_XA;
case RESOURCE_DND:
return CONTACT_DND;
default:
return CONTACT_ONLINE;
}
}