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>
This commit is contained in:
2026-05-26 17:48:14 +00:00
parent 3b673150b4
commit 72f4f186da
303 changed files with 10658 additions and 9857 deletions

View File

@@ -5,33 +5,7 @@
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -69,7 +43,7 @@ aesgcm_file_get(void* userdata)
// and tag stored in the URL fragment.
if (omemo_parse_aesgcm_url(aesgcm_dl->url, &https_url, &fragment) != 0) {
cons_show_error("Download failed: Cannot parse URL '%s'.", aesgcm_dl->url);
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Download failed: Cannot parse URL '%s'.",
aesgcm_dl->url);
return NULL;
@@ -80,7 +54,7 @@ aesgcm_file_get(void* userdata)
auto_gchar gchar* tmpname = NULL;
auto_gfd gint tmpfd = 0;
if ((tmpfd = g_file_open_tmp("profanity.XXXXXX", &tmpname, NULL)) == -1) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to create "
"temporary ciphertext file for writing "
"(%s).",
@@ -91,7 +65,7 @@ aesgcm_file_get(void* userdata)
// Open the target file for storing the cleartext.
auto_FILE FILE* outfh = fopen(aesgcm_dl->filename, "wb");
if (outfh == NULL) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to open "
"output file at '%s' for writing (%s).",
https_url, aesgcm_dl->filename,
@@ -101,14 +75,16 @@ aesgcm_file_get(void* userdata)
// We wrap the HTTPDownload tool and use it for retrieving the ciphertext
// and storing it in the temporary file previously opened.
HTTPDownload* http_dl = calloc(1, sizeof(HTTPDownload));
HTTPDownload* http_dl = g_new0(HTTPDownload, 1);
http_dl->window = aesgcm_dl->window;
http_dl->worker = aesgcm_dl->worker;
http_dl->id = strdup(aesgcm_dl->id);
http_dl->url = strdup(https_url);
http_dl->display_url = strdup(aesgcm_dl->url);
http_dl->filename = strdup(tmpname);
http_dl->cmd_template = NULL;
http_dl->silent = FALSE;
http_dl->silent_done = TRUE;
http_dl->return_bytes_received = TRUE;
aesgcm_dl->http_dl = http_dl;
@@ -121,7 +97,7 @@ aesgcm_file_get(void* userdata)
FILE* tmpfh = fopen(tmpname, "rb");
if (tmpfh == NULL) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to open "
"temporary file at '%s' for reading (%s).",
aesgcm_dl->url, tmpname,
@@ -136,13 +112,15 @@ aesgcm_file_get(void* userdata)
remove(tmpname);
if (crypt_res != GPG_ERR_NO_ERROR) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Failed to decrypt "
"file (%s).",
https_url, gcry_strerror(crypt_res));
} else {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
"Decrypted file saved to '%s'", aesgcm_dl->filename);
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ONLINE, ENTRY_COMPLETED,
"Downloading '%s': done\nSaved to '%s'",
aesgcm_dl->url, aesgcm_dl->filename);
win_mark_received(aesgcm_dl->window, aesgcm_dl->id);
}
if (aesgcm_dl->cmd_template != NULL) {
@@ -152,7 +130,7 @@ aesgcm_file_get(void* userdata)
// TODO: Log the error.
if (!call_external(argv)) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to call "
"command '%s' with file at '%s' (%s).",
aesgcm_dl->url,

View File

@@ -5,33 +5,7 @@
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_AESGCM_DOWNLOAD_H
@@ -50,7 +24,7 @@
typedef struct aesgcm_download_t
{
char* id;
gchar* id;
char* url;
char* filename;
char* cmd_template;

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -62,7 +36,7 @@ static gchar* _search(Autocomplete ac, GList* curr, gboolean quote, search_direc
Autocomplete
autocomplete_new(void)
{
return calloc(1, sizeof(struct autocomplete_t));
return g_new0(struct autocomplete_t, 1);
}
void
@@ -175,7 +149,7 @@ autocomplete_add(Autocomplete ac, const char* item)
void
autocomplete_add_all(Autocomplete ac, char** items)
{
for (int i = 0; i < g_strv_length(items); i++) {
for (guint i = 0; i < g_strv_length(items); i++) {
autocomplete_add(ac, items[i]);
}
}
@@ -205,7 +179,7 @@ autocomplete_remove(Autocomplete ac, const char* const item)
void
autocomplete_remove_all(Autocomplete ac, char** items)
{
for (int i = 0; i < g_strv_length(items); i++) {
for (guint i = 0; i < g_strv_length(items); i++) {
autocomplete_remove(ac, items[i]);
}
}
@@ -260,7 +234,16 @@ autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote,
FREE_SET_NULL(ac->search_str);
}
ac->search_str = g_strdup(search_str);
// unescape search string
GString* unescaped = g_string_new("");
for (const char* p = search_str; *p; p++) {
if (*p == '\\' && (*(p + 1) != '\0')) {
p++;
}
g_string_append_c(unescaped, *p);
}
ac->search_str = g_string_free(unescaped, FALSE);
found = _search(ac, ac->items, quote, NEXT);
return found;
@@ -307,7 +290,7 @@ autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote,
static char*
_autocomplete_param_common(const char* const input, const char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context)
{
int len;
size_t len;
auto_gchar gchar* command_cpy = g_strdup_printf("%s ", command);
if (!command_cpy) {
return NULL;
@@ -316,14 +299,14 @@ _autocomplete_param_common(const char* const input, const char* command, autocom
len = strlen(command_cpy);
if (strncmp(input, command_cpy, len) == 0) {
int inp_len = strlen(input);
size_t inp_len = strlen(input);
auto_char char* found = NULL;
auto_char char* prefix = malloc(inp_len + 1);
if (!prefix) {
return NULL;
}
for (int i = len; i < inp_len; i++) {
for (size_t i = len; i < inp_len; i++) {
prefix[i - len] = input[i];
}
@@ -396,22 +379,47 @@ autocomplete_remove_older_than_max_reverse(Autocomplete ac, int maxsize)
static gchar*
_search(Autocomplete ac, GList* curr, gboolean quote, search_direction direction)
{
auto_gchar gchar* search_str_fold = g_utf8_casefold(ac->search_str, -1);
auto_gchar gchar* search_str_ascii = g_str_to_ascii(ac->search_str, NULL);
auto_gchar gchar* search_str_lower = g_ascii_strdown(search_str_ascii, -1);
auto_gchar gchar* search_str_ascii_lower = g_ascii_strdown(search_str_ascii, -1);
while (curr) {
auto_gchar gchar* curr_ascii = g_str_to_ascii(curr->data, NULL);
auto_gchar gchar* curr_lower = g_ascii_strdown(curr_ascii, -1);
gboolean match = FALSE;
// match found
if (strncmp(curr_lower, search_str_lower, strlen(search_str_lower)) == 0) {
// Try exact UTF-8 case insensitive match
auto_gchar gchar* curr_fold = g_utf8_casefold(curr->data, -1);
if (g_str_has_prefix(curr_fold, search_str_fold)) {
match = TRUE;
}
// Try transliterated match (allow typing 'e' for 'è')
if (!match) {
auto_gchar gchar* curr_ascii = g_str_to_ascii(curr->data, NULL);
auto_gchar gchar* curr_ascii_lower = g_ascii_strdown(curr_ascii, -1);
// We only use transliterated match if the search string conversion didn't result
// in unknown characters ('?'), to avoid false matches between different scripts.
if (strchr(search_str_ascii_lower, '?') == NULL
&& g_str_has_prefix(curr_ascii_lower, search_str_ascii_lower)) {
match = TRUE;
}
}
if (match) {
// set pointer to last found
ac->last_found = curr;
// if contains space, quote before returning
if (quote && g_strrstr(curr->data, " ")) {
return g_strdup_printf("\"%s\"", (gchar*)curr->data);
GString* escaped = g_string_new("\"");
for (const char* p = curr->data; *p; p++) {
if (*p == '"' || *p == '\\') {
g_string_append_c(escaped, '\\');
}
g_string_append_c(escaped, *p);
}
g_string_append_c(escaped, '"');
return g_string_free(escaped, FALSE);
// otherwise just return the string
} else {
return strdup(curr->data);

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_AUTOCOMPLETE_H

View File

@@ -2,35 +2,9 @@
* bookmark_ignore.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2020 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2020 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -73,7 +47,7 @@ bookmark_ignore_on_connect(const char* const barejid)
}
void
bookmark_ignore_on_disconnect()
bookmark_ignore_on_disconnect(void)
{
free_keyfile(&bookmark_ignore_prof_keyfile);
bookmark_ignore_keyfile = NULL;

View File

@@ -2,42 +2,16 @@
* bookmark_ignore.h
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2020 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2020 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef BOOKMARK_IGNORE_H
#define BOOKMARK_IGNORE_H
void bookmark_ignore_on_connect();
void bookmark_ignore_on_disconnect();
void bookmark_ignore_on_connect(const char* const barejid);
void bookmark_ignore_on_disconnect(void);
gboolean bookmark_ignored(Bookmark* bookmark);
gchar** bookmark_ignore_list(gsize* len);
void bookmark_ignore_add(const char* const barejid);

View File

@@ -2,35 +2,9 @@
* clipboard.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2019 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -41,6 +15,7 @@
#include <stdlib.h>
#include "log.h"
#include "ui/tray.h"
/*
For now we rely on tray_init(void)
@@ -53,6 +28,9 @@ void clipboard_init(int argc, char **argv) {
char*
clipboard_get(void)
{
if (!tray_gtk_ready()) {
return NULL;
}
gchar* clip;
GtkClipboard* cl = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);

View File

@@ -2,35 +2,9 @@
* clipboard.h
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2019 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef UI_CLIPBOARD_H

View File

@@ -2,47 +2,19 @@
* editor.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2022 - 2025 Michael Vetter <jubalh@iodoru.org>
* Copyright (C) 2022 - 2026 Michael Vetter <jubalh@iodoru.org>
* Copyright (C) 2022 MarcoPolo PasTonMolo <marcopolopastonmolo@protonmail.com>
* Copyright (C) 2022 Steffen Jaeckel <jaeckel-floss@eyet-services.de>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include <fcntl.h>
#include <glib.h>
#include <errno.h>
#include <stdio.h> // necessary for readline
#include <readline/readline.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include "config/files.h"
#include "config/preferences.h"
@@ -53,194 +25,53 @@
#include "ui/ui.h"
#include "xmpp/xmpp.h"
EditorTask editor_task = { 0, FALSE, NULL, NULL, PTHREAD_MUTEX_INITIALIZER, 0, NULL, NULL };
void*
editor_thread(void* arg)
typedef struct EditorContext
{
EditorTask* task = (EditorTask*)arg;
auto_gcharv gchar** argv = g_strsplit(task->editor_cmd, " ", 0);
gchar* filename;
void (*callback)(gchar* content, void* user_data);
void* user_data;
} EditorContext;
pid_t pid = fork();
if (pid == 0) {
execvp(argv[0], argv);
log_error("[Editor] Failed to exec %s", argv[0]);
_exit(EXIT_FAILURE);
}
pthread_mutex_lock(&task->mutex);
if (pid > 0) {
task->pid = pid;
int status;
// Unlock mutex to avoid deadlock on the main loop
pthread_mutex_unlock(&task->mutex);
waitpid(pid, &status, 0);
pthread_mutex_lock(&task->mutex);
gsize length;
if (!g_file_get_contents(task->filename, &task->result, &length, &task->error)) {
log_error("[Editor] could not read from %s: %s", task->filename,
task->error ? task->error->message : "No GLib error given");
task->result = g_strdup(""); // Ensure valid result
} else {
g_strchomp(task->result);
if (remove(task->filename) != 0) {
log_error("[Editor] error during file deletion of %s", task->filename);
} else {
log_debug("[Editor] deleted file: %s", task->filename);
}
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
task->error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Editor process failed with status %d", WEXITSTATUS(status));
}
} else {
task->error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_FAILED, "Fork failed: %s", strerror(errno));
task->result = g_strdup(""); // Ensure valid result
}
task->active = FALSE;
pthread_mutex_unlock(&task->mutex);
return NULL;
}
// Returns true if an error occurred
gboolean
get_message_from_editor_async(gchar* message)
static void
_editor_exit_cb(GPid pid, gint status, gpointer data)
{
// Check if editor is already running
pthread_mutex_lock(&editor_task.mutex);
if (editor_task.active) {
pthread_mutex_unlock(&editor_task.mutex);
log_error("[Editor] Editor already running");
return TRUE;
}
pthread_mutex_unlock(&editor_task.mutex);
EditorContext* ctx = data;
gchar* contents = NULL;
GError* error = NULL;
// Create temporary file
auto_gchar gchar* filename = NULL;
GError* glib_error = NULL;
const char* jid = connection_get_barejid();
if (jid) {
filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md");
} else {
log_debug("[Editor] could not get JID");
auto_gchar gchar* data_dir = files_get_data_path(DIR_EDITOR);
if (!create_dir(data_dir)) {
log_error("[Editor] could not create directory %s", data_dir);
return TRUE;
}
filename = g_strdup_printf("%s/compose.md", data_dir);
}
if (!filename) {
log_error("[Editor] something went wrong while creating compose file");
return TRUE;
}
// Write message to file
gsize messagelen = message ? strlen(message) : 0;
if (!g_file_set_contents(filename, message ? message : "", messagelen, &glib_error)) {
log_error("[Editor] could not write to %s: %s", filename,
glib_error ? glib_error->message : "No GLib error given");
if (glib_error) {
g_error_free(glib_error);
}
return TRUE;
}
// Prepare editor command
auto_gchar gchar* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
auto_gchar gchar* editor_with_filename = g_strdup_printf("%s %s", editor, filename);
// Suspend NCurses
def_prog_mode();
endwin();
// Initialize thread task
pthread_mutex_lock(&editor_task.mutex);
editor_task.active = TRUE;
editor_task.result = NULL;
editor_task.error = NULL;
g_free(editor_task.filename);
g_free(editor_task.editor_cmd);
editor_task.filename = g_strdup(filename);
editor_task.editor_cmd = g_strdup(editor_with_filename);
pthread_mutex_unlock(&editor_task.mutex);
// Set background mode
background_mode = TRUE;
log_debug("[Editor] Entering background mode");
// Start editor thread
if (pthread_create(&editor_task.thread, NULL, editor_thread, &editor_task) != 0) {
pthread_mutex_lock(&editor_task.mutex);
editor_task.active = FALSE;
editor_task.error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Failed to create editor thread: %s", strerror(errno));
g_free(editor_task.filename);
g_free(editor_task.editor_cmd);
editor_task.filename = NULL;
editor_task.editor_cmd = NULL;
pthread_mutex_unlock(&editor_task.mutex);
background_mode = FALSE;
log_debug("[Editor] Exiting background mode");
reset_prog_mode();
doupdate();
log_error("[Editor] Failed to create editor thread");
return TRUE;
}
return FALSE;
}
void
editor_process(ProfWin* window)
{
pthread_mutex_lock(&editor_task.mutex);
if (editor_task.active) {
pthread_mutex_unlock(&editor_task.mutex);
return;
}
if (!editor_task.result) {
pthread_mutex_unlock(&editor_task.mutex);
return;
}
g_strchomp(editor_task.result);
rl_replace_line(editor_task.result, 0);
rl_point = rl_end;
rl_forced_update_display();
reset_prog_mode();
doupdate();
ui_resume();
ui_resize();
g_free(editor_task.result);
g_free(editor_task.filename);
g_free(editor_task.editor_cmd);
editor_task.result = NULL;
editor_task.filename = NULL;
editor_task.editor_cmd = NULL;
if (editor_task.error) {
log_error("[Editor] %s", editor_task.error->message);
g_error_free(editor_task.error);
editor_task.error = NULL;
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
if (g_file_get_contents(ctx->filename, &contents, NULL, &error)) {
g_strchomp(contents);
ctx->callback(contents, ctx->user_data);
g_free(contents);
} else {
log_error("[Editor] could not read from %s: %s", ctx->filename, error->message);
cons_show_error("Could not read edited content: %s", error->message);
g_error_free(error);
}
} else {
cons_show_error("Editor exited with error status %d", WEXITSTATUS(status));
}
pthread_mutex_unlock(&editor_task.mutex);
background_mode = FALSE;
log_debug("[Editor] Exiting background mode");
if (remove(ctx->filename) != 0) {
log_error("[Editor] error during file deletion of %s", ctx->filename);
}
g_free(ctx->filename);
g_free(ctx);
g_spawn_close_pid(pid);
}
// Deprecated synchronous editor call. Returns a message as returned_message.
// Please avoid using it, since it blocks execution of the message handling and other important functionality until the editor is closed.
// Returns true if an error occurred
// Returns a message via callback once the editor is closed.
// Returns true if an error occurred.
gboolean
get_message_from_editor(gchar* message, gchar** returned_message)
launch_editor(gchar* initial_content, void (*callback)(gchar* content, void* data), void* user_data)
{
/* Make sure that there's no junk in the return-pointer in error cases */
*returned_message = NULL;
auto_gchar gchar* filename = NULL;
GError* glib_error = NULL;
auto_gerror GError* glib_error = NULL;
const char* jid = connection_get_barejid();
if (jid) {
filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md");
@@ -248,64 +79,79 @@ get_message_from_editor(gchar* message, gchar** returned_message)
log_debug("[Editor] could not get JID");
auto_gchar gchar* data_dir = files_get_data_path(DIR_EDITOR);
if (!create_dir(data_dir)) {
cons_show_error("Could not create editor directory.");
return TRUE;
}
filename = g_strdup_printf("%s/compose.md", data_dir);
}
if (!filename) {
log_error("[Editor] something went wrong while creating compose file");
cons_show_error("Could not create compose file.");
return TRUE;
}
gsize messagelen = 0;
if (message != NULL) {
messagelen = strlen(message);
if (initial_content != NULL) {
messagelen = strlen(initial_content);
}
if (!g_file_set_contents(filename, message, messagelen, &glib_error)) {
log_error("[Editor] could not write to %s: %s", filename, glib_error ? glib_error->message : "No GLib error given");
if (glib_error) {
g_error_free(glib_error);
}
if (!g_file_set_contents(filename, initial_content, messagelen, &glib_error)) {
log_error("[Editor] could not write to %s: %s", filename, PROF_GERROR_MESSAGE(glib_error));
cons_show_error("Could not write to compose file: %s", PROF_GERROR_MESSAGE(glib_error));
return TRUE;
}
auto_gchar gchar* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
auto_gchar gchar* editor_with_filename = g_strdup_printf("%s %s", editor, filename);
auto_gcharv gchar** editor_argv = g_strsplit(editor_with_filename, " ", 0);
gchar** editor_argv = NULL;
GError* error = NULL;
// Fork / exec
pid_t pid = fork();
if (pid == 0) {
int x = execvp(editor_argv[0], editor_argv);
if (x == -1) {
log_error("[Editor] Failed to exec %s", editor);
}
_exit(EXIT_FAILURE);
} else {
if (pid == -1) {
return TRUE;
}
waitpid(pid, NULL, 0);
gchar* contents;
gsize length;
if (!g_file_get_contents(filename, &contents, &length, &glib_error)) {
log_error("[Editor] could not read from %s: %s", filename, glib_error ? glib_error->message : "No GLib error given");
if (glib_error) {
g_error_free(glib_error);
}
return TRUE;
}
/* Remove all trailing new-line characters */
g_strchomp(contents);
*returned_message = contents;
if (remove(filename) != 0) {
log_error("[Editor] error during file deletion of %s", filename);
} else {
log_debug("[Editor] deleted file: %s", filename);
}
auto_gchar gchar* full_cmd = g_strdup_printf("%s %s", editor, filename);
if (!g_shell_parse_argv(full_cmd, NULL, &editor_argv, &error)) {
log_error("[Editor] Failed to parse editor command: %s", error->message);
cons_show_error("Failed to parse editor command: %s", error->message);
g_error_free(error);
return TRUE;
}
EditorContext* ctx = g_new0(EditorContext, 1);
ctx->filename = g_steal_pointer(&filename);
ctx->callback = callback;
ctx->user_data = user_data;
ui_suspend();
// Force GLib to install its SIGCHLD handler before forking. Creating a
// GChildWatchSource for our own pid is the documented trigger; we never
// attach or dispatch it, just rely on the side effect.
GSource* sigchld_warmup = g_child_watch_source_new(getpid());
g_source_unref(sigchld_warmup);
pid_t pid = fork();
if (pid == -1) {
log_error("[Editor] Failed to fork: %s", strerror(errno));
ui_resume();
ui_resize();
cons_show_error("Failed to start editor: %s", strerror(errno));
g_strfreev(editor_argv);
g_free(ctx->filename);
g_free(ctx);
return TRUE;
} else if (pid == 0) {
// Child process: Inherits TTY from parent
// Reset signal handlers that profanity sets so the editor doesn't inherit them
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
if (editor_argv && editor_argv[0]) {
execvp(editor_argv[0], editor_argv);
}
_exit(EXIT_FAILURE);
}
// Parent process: Watch the child asynchronously
g_child_watch_add((GPid)pid, _editor_exit_cb, ctx);
g_strfreev(editor_argv);
return FALSE;
}

View File

@@ -2,36 +2,10 @@
* editor.h
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2022 - 2025 Michael Vetter <jubalh@iodoru.org>
* Copyright (C) 2022 - 2026 Michael Vetter <jubalh@iodoru.org>
* Copyright (C) 2022 MarcoPolo PasTonMolo <marcopolopastonmolo@protonmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_EDITOR_H
@@ -39,22 +13,7 @@
#include "ui/win_types.h"
#include <glib.h>
typedef struct
{
pid_t pid; // Editor process PID
gboolean active; // Thread running
gchar* result; // Edited message
GError* error; // Error if any
pthread_mutex_t mutex; // Protect access
pthread_t thread; // Editor thread
gchar* filename; // Temporary file path
gchar* editor_cmd; // Editor command with filename
} EditorTask;
extern EditorTask editor_task;
gboolean get_message_from_editor(gchar* message, gchar** returned_message);
gboolean get_message_from_editor_async(gchar* message);
void editor_process(ProfWin* window);
gboolean launch_editor(gchar* initial_content, void (*callback)(gchar* content, void* data), void* user_data);
#endif

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -45,7 +19,7 @@
#define FALLBACK_MSG ""
void
http_print_transfer_update(ProfWin* window, char* id, const char* fmt, ...)
http_print_transfer_update(ProfWin* window, char* id, theme_item_t theme_item, int flags, const char* fmt, ...)
{
va_list args;
@@ -55,7 +29,7 @@ http_print_transfer_update(ProfWin* window, char* id, const char* fmt, ...)
va_end(args);
if (window->type != WIN_CONSOLE) {
win_update_entry_message(window, id, msg->str);
win_update_entry(window, id, msg->str, theme_item, flags);
} else {
cons_show("%s", msg->str);
}
@@ -64,7 +38,7 @@ http_print_transfer_update(ProfWin* window, char* id, const char* fmt, ...)
}
void
http_print_transfer(ProfWin* window, char* id, const char* fmt, ...)
http_print_transfer(ProfWin* window, char* id, theme_item_t theme_item, const char* fmt, ...)
{
va_list args;
@@ -73,7 +47,7 @@ http_print_transfer(ProfWin* window, char* id, const char* fmt, ...)
g_string_vprintf(msg, fmt, args);
va_end(args);
win_print_http_transfer(window, msg->str, id);
win_print_status_with_id(window, msg->str, id, theme_item, 0);
g_string_free(msg, TRUE);
}

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_HTTP_COMMON_H
@@ -39,9 +13,9 @@
#include <glib.h>
#include "ui/window.h"
G_GNUC_PRINTF(3, 4)
void http_print_transfer(ProfWin* window, char* id, const char* fmt, ...);
G_GNUC_PRINTF(3, 4)
void http_print_transfer_update(ProfWin* window, char* id, const char* fmt, ...);
G_GNUC_PRINTF(4, 5)
void http_print_transfer(ProfWin* window, char* id, theme_item_t theme_item, const char* fmt, ...);
G_GNUC_PRINTF(5, 6)
void http_print_transfer_update(ProfWin* window, char* id, theme_item_t theme_item, int flags, const char* fmt, ...);
#endif

View File

@@ -4,35 +4,9 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2019 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -58,7 +32,6 @@
#include "common.h"
GSList* download_processes = NULL;
gboolean silent = FALSE;
static int
_xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
@@ -81,12 +54,21 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot
unsigned int dlperc = 0;
if (dltotal != 0) {
dlperc = (100 * dlnow) / dltotal;
dlperc = (unsigned int)((100 * dlnow) / dltotal);
}
if (!silent)
http_print_transfer_update(download->window, download->url,
"Downloading '%s': %d%%", download->url, dlperc);
if (!download->silent) {
const char* url = download->display_url ? download->display_url : download->url;
if (dlnow == dltotal && dltotal > 0) {
if (!download->silent_done) {
http_print_transfer_update(download->window, download->id, THEME_ONLINE, ENTRY_COMPLETED,
"Downloading '%s': done", url);
}
} else {
http_print_transfer_update(download->window, download->id, THEME_DEFAULT, 0,
"Downloading '%s': %d%%", url, dlperc);
}
}
pthread_mutex_unlock(&lock);
@@ -111,20 +93,20 @@ http_file_get(void* userdata)
CURL* curl;
CURLcode res;
silent = download->silent;
download->cancel = 0;
download->bytes_received = 0;
pthread_mutex_lock(&lock);
if (!silent) {
http_print_transfer(download->window, download->id,
"Downloading '%s': 0%%", download->url);
const char* display_url = download->display_url ? download->display_url : download->url;
if (!download->silent) {
http_print_transfer(download->window, download->id, THEME_DEFAULT,
"Downloading '%s': 0%%", display_url);
}
FILE* outfh = fopen(download->filename, "wb");
if (outfh == NULL) {
http_print_transfer_update(download->window, download->id,
http_print_transfer_update(download->window, download->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to open "
"output file at '%s' for writing (%s).",
download->url, download->filename,
@@ -177,7 +159,7 @@ http_file_get(void* userdata)
err = strdup(curl_easy_strerror(res));
}
if (ftell(outfh) == 0) {
if (!err && ftell(outfh) == 0) {
err = strdup("Output file is empty.");
}
@@ -185,7 +167,9 @@ http_file_get(void* userdata)
curl_global_cleanup();
if (fclose(outfh) == EOF) {
err = strdup(g_strerror(errno));
if (!err) {
err = strdup(g_strerror(errno));
}
}
pthread_mutex_lock(&lock);
@@ -193,25 +177,29 @@ http_file_get(void* userdata)
g_free(cert_path);
if (err) {
if (download->cancel) {
http_print_transfer_update(download->window, download->id,
http_print_transfer_update(download->window, download->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: "
"Download was canceled",
download->url);
display_url);
} else {
http_print_transfer_update(download->window, download->id,
http_print_transfer_update(download->window, download->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: %s",
download->url, err);
display_url, err);
}
free(err);
} else {
if (!download->cancel && !silent) {
http_print_transfer_update(download->window, download->id,
"Downloading '%s': done\nSaved to '%s'",
download->url, download->filename);
win_mark_received(download->window, download->id);
if (!download->cancel) {
if (!download->silent && !download->silent_done) {
http_print_transfer_update(download->window, download->id, THEME_ONLINE, ENTRY_COMPLETED,
"Downloading '%s': done\nSaved to '%s'",
display_url, download->filename);
win_mark_received(download->window, download->id);
}
if (download->return_bytes_received) {
ret = malloc(sizeof(*ret));
*ret = download->bytes_received;
if (ret) {
*ret = download->bytes_received;
}
}
}
}
@@ -223,10 +211,10 @@ http_file_get(void* userdata)
// TODO: Log the error.
if (!call_external(argv)) {
http_print_transfer_update(download->window, download->id,
http_print_transfer_update(download->window, download->id, THEME_ERROR, ENTRY_ERROR,
"Downloading '%s' failed: Unable to call "
"command '%s' with file at '%s' (%s).",
download->url,
display_url,
download->cmd_template,
download->filename,
"TODO: Log the error");
@@ -243,6 +231,7 @@ out:
free(download->filename);
free(download->url);
free(download->display_url);
free(download->id);
free(download);

View File

@@ -5,33 +5,7 @@
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2020 William Wennerström <william@wstrm.dev>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_HTTP_DOWNLOAD_H
@@ -51,6 +25,7 @@ typedef struct http_download_t
{
char* id;
char* url;
char* display_url;
char* filename;
char* cmd_template;
curl_off_t bytes_received;
@@ -58,6 +33,7 @@ typedef struct http_download_t
pthread_t worker;
int cancel;
gboolean silent;
gboolean silent_done;
gboolean return_bytes_received;
} HTTPDownload;

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -88,14 +62,24 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot
unsigned int ulperc = 0;
if (ultotal != 0) {
ulperc = (100 * ulnow) / ultotal;
ulperc = (unsigned int)((100 * ulnow) / ultotal);
}
gchar* msg = NULL;
theme_item_t theme = THEME_DEFAULT;
int flags = 0;
if (ulnow == ultotal && ultotal > 0) {
msg = g_strdup_printf("Uploading '%s': done", upload->filename);
theme = THEME_ONLINE;
flags = ENTRY_COMPLETED;
} else {
msg = g_strdup_printf("Uploading '%s': %d%%", upload->filename, ulperc);
}
gchar* msg = g_strdup_printf("Uploading '%s': %d%%", upload->filename, ulperc);
if (!msg) {
msg = g_strdup(FALLBACK_MSG);
}
win_update_entry_message(upload->window, upload->put_url, msg);
win_update_entry(upload->window, upload->put_url, msg, theme, flags);
g_free(msg);
pthread_mutex_unlock(&lock);
@@ -176,7 +160,7 @@ http_file_put(void* userdata)
if (!msg) {
msg = g_strdup(FALLBACK_MSG);
}
win_print_http_transfer(upload->window, msg, upload->put_url);
win_print_status_with_id(upload->window, msg, upload->put_url, THEME_DEFAULT, 0);
auto_gchar gchar* cert_path = prefs_get_string(PREF_TLS_CERTPATH);
auto_gchar gchar* cafile = cafile_get_name();
@@ -304,21 +288,22 @@ http_file_put(void* userdata)
if (!err_msg) {
err_msg = g_strdup(FALLBACK_MSG);
}
win_update_entry(upload->window, upload->put_url, err_msg, THEME_ERROR, ENTRY_ERROR);
} else {
err_msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err);
if (!err_msg) {
err_msg = g_strdup(FALLBACK_MSG);
}
win_update_entry_message(upload->window, upload->put_url, err_msg);
win_update_entry(upload->window, upload->put_url, err_msg, THEME_ERROR, ENTRY_ERROR);
}
cons_show_error("%s", err_msg);
} else {
if (!upload->cancel) {
auto_gchar gchar* status_msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename);
auto_gchar gchar* status_msg = g_strdup_printf("Uploading '%s': done", upload->filename);
if (!status_msg) {
status_msg = g_strdup(FALLBACK_MSG);
}
win_update_entry_message(upload->window, upload->put_url, status_msg);
win_update_entry(upload->window, upload->put_url, status_msg, THEME_ONLINE, ENTRY_COMPLETED);
win_mark_received(upload->window, upload->put_url);
char* url = NULL;

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_HTTP_UPLOAD_H

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
@@ -54,20 +28,36 @@ _parse_args_helper(const char* const inp, int min, int max, gboolean* result, gb
auto_char char* copy = strdup(inp);
g_strstrip(copy);
int inp_size = g_utf8_strlen(copy, -1);
glong inp_size = g_utf8_strlen(copy, -1);
gboolean in_token = FALSE;
gboolean in_freetext = FALSE;
gboolean in_quotes = FALSE;
char* token_start = &copy[0];
int token_size = 0;
GString* current_token = g_string_new("");
int num_tokens = 0;
GSList* tokens = NULL;
// add tokens to GSList
for (int i = 0; i < inp_size; i++) {
for (glong i = 0; i < inp_size; i++) {
gchar* curr_ch = g_utf8_offset_to_pointer(copy, i);
gunichar curr_uni = g_utf8_get_char(curr_ch);
if (curr_uni == '\\' && (i + 1 < inp_size)) {
if (!in_token) {
in_token = TRUE;
if (with_freetext) {
num_tokens++;
}
if (with_freetext && (num_tokens == max + 1)) {
in_freetext = TRUE;
}
}
i++;
gchar* next_ch = g_utf8_offset_to_pointer(copy, i);
gunichar next_uni = g_utf8_get_char(next_ch);
g_string_append_unichar(current_token, next_uni);
continue;
}
if (!in_token) {
if (curr_uni == ' ') {
continue;
@@ -81,58 +71,61 @@ _parse_args_helper(const char* const inp, int min, int max, gboolean* result, gb
} else if (curr_uni == '"') {
in_quotes = TRUE;
i++;
gchar* next_ch = g_utf8_next_char(curr_ch);
gunichar next_uni = g_utf8_get_char(next_ch);
if (i < inp_size) {
gchar* next_ch = g_utf8_offset_to_pointer(copy, i);
gunichar next_uni = g_utf8_get_char(next_ch);
if (next_uni == '"') {
tokens = g_slist_append(tokens, g_strndup(curr_ch,
0));
token_size = 0;
if (next_uni == '"') {
tokens = g_slist_append(tokens, g_strdup(""));
in_token = FALSE;
in_quotes = FALSE;
} else if (next_uni == '\\' && (i + 1 < inp_size)) {
i++;
gchar* escaped_ch = g_utf8_offset_to_pointer(copy, i);
gunichar escaped_uni = g_utf8_get_char(escaped_ch);
g_string_append_unichar(current_token, escaped_uni);
} else {
g_string_append_unichar(current_token, next_uni);
}
} else {
// trailing quote
tokens = g_slist_append(tokens, g_strdup(""));
in_token = FALSE;
in_quotes = FALSE;
} else {
token_start = next_ch;
token_size += g_unichar_to_utf8(next_uni, NULL);
}
}
if (curr_uni == '"') {
gchar* next_ch = g_utf8_next_char(curr_ch);
token_start = next_ch;
} else {
token_start = curr_ch;
token_size += g_unichar_to_utf8(curr_uni, NULL);
if (in_token && curr_uni != '"') {
g_string_append_unichar(current_token, curr_uni);
}
}
} else {
if (in_quotes) {
if (curr_uni == '"') {
tokens = g_slist_append(tokens, g_strndup(token_start,
token_size));
token_size = 0;
tokens = g_slist_append(tokens, g_string_free(current_token, FALSE));
current_token = g_string_new("");
in_token = FALSE;
in_quotes = FALSE;
} else {
if (curr_uni != '"') {
token_size += g_unichar_to_utf8(curr_uni, NULL);
}
g_string_append_unichar(current_token, curr_uni);
}
} else {
if (with_freetext && in_freetext) {
token_size += g_unichar_to_utf8(curr_uni, NULL);
g_string_append_unichar(current_token, curr_uni);
} else if (curr_uni == ' ') {
tokens = g_slist_append(tokens, g_strndup(token_start,
token_size));
token_size = 0;
tokens = g_slist_append(tokens, g_string_free(current_token, FALSE));
current_token = g_string_new("");
in_token = FALSE;
} else {
token_size += g_unichar_to_utf8(curr_uni, NULL);
g_string_append_unichar(current_token, curr_uni);
}
}
}
}
if (in_token) {
tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
tokens = g_slist_append(tokens, g_string_free(current_token, FALSE));
} else {
g_string_free(current_token, TRUE);
}
int num = g_slist_length(tokens) - 1;
@@ -238,10 +231,10 @@ gchar**
parse_args_as_one(const char* const inp, int min, int max, gboolean* result)
{
gchar** args = g_malloc0(2 * sizeof(*args));
int length = g_utf8_strlen(inp, -1);
glong length = g_utf8_strlen(inp, -1);
gchar* space = g_utf8_strchr(inp, length, ' ');
if (space) {
int sub_length = g_utf8_strlen(space, -1);
glong sub_length = g_utf8_strlen(space, -1);
if (sub_length > 1) {
args[0] = g_strdup(space + 1);
*result = TRUE;
@@ -260,20 +253,34 @@ parse_args_as_one(const char* const inp, int min, int max, gboolean* result)
int
count_tokens(const char* const string)
{
int length = g_utf8_strlen(string, -1);
glong length = g_utf8_strlen(string, -1);
gboolean in_quotes = FALSE;
int num_tokens = 0;
// include first token
num_tokens++;
for (int i = 0; i < length; i++) {
for (glong i = 0; i < length; i++) {
gchar* curr_ch = g_utf8_offset_to_pointer(string, i);
gunichar curr_uni = g_utf8_get_char(curr_ch);
if (curr_uni == '\\' && (i + 1 < length)) {
i++;
continue;
}
if (curr_uni == ' ') {
if (!in_quotes) {
num_tokens++;
while (i + 1 < length) {
gchar* next_ch = g_utf8_offset_to_pointer(string, i + 1);
gunichar next_uni = g_utf8_get_char(next_ch);
if (next_uni == ' ') {
i++;
} else {
break;
}
}
}
} else if (curr_uni == '"') {
if (in_quotes) {
@@ -291,26 +298,48 @@ char*
get_start(const char* const string, int tokens)
{
GString* result = g_string_new("");
int length = g_utf8_strlen(string, -1);
glong length = g_utf8_strlen(string, -1);
gboolean in_quotes = FALSE;
int num_tokens = 0;
// include first token
num_tokens++;
for (int i = 0; i < length; i++) {
for (glong i = 0; i < length; i++) {
gchar* curr_ch = g_utf8_offset_to_pointer(string, i);
gunichar curr_uni = g_utf8_get_char(curr_ch);
if (curr_uni == '\\' && (i + 1 < length)) {
if (num_tokens < tokens) {
g_string_append_unichar(result, curr_uni);
}
i++;
curr_ch = g_utf8_offset_to_pointer(string, i);
curr_uni = g_utf8_get_char(curr_ch);
if (num_tokens < tokens) {
g_string_append_unichar(result, curr_uni);
}
continue;
}
if (num_tokens < tokens) {
auto_gchar gchar* uni_char = g_malloc(7);
int len = g_unichar_to_utf8(curr_uni, uni_char);
uni_char[len] = '\0';
g_string_append(result, uni_char);
g_string_append_unichar(result, curr_uni);
}
if (curr_uni == ' ') {
if (!in_quotes) {
num_tokens++;
while (i + 1 < length) {
gchar* next_ch = g_utf8_offset_to_pointer(string, i + 1);
gunichar next_uni = g_utf8_get_char(next_ch);
if (next_uni == ' ') {
if (num_tokens <= tokens) {
g_string_append_unichar(result, next_uni);
}
i++;
} else {
break;
}
}
}
} else if (curr_uni == '"') {
if (in_quotes) {
@@ -328,7 +357,7 @@ GHashTable*
parse_options(gchar** args, gchar** opt_keys, gboolean* res)
{
GList* keys = NULL;
for (int i = 0; i < g_strv_length(opt_keys); i++) {
for (guint i = 0; i < g_strv_length(opt_keys); i++) {
keys = g_list_append(keys, opt_keys[i]);
}
@@ -344,7 +373,7 @@ parse_options(gchar** args, gchar** opt_keys, gboolean* res)
// validate options
GList* found_keys = NULL;
for (int curr = 0; curr < g_strv_length(args); curr += 2) {
for (guint curr = 0; curr < g_strv_length(args); curr += 2) {
// check if option valid
if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
*res = FALSE;
@@ -377,7 +406,7 @@ parse_options(gchar** args, gchar** opt_keys, gboolean* res)
// create map
options = g_hash_table_new(g_str_hash, g_str_equal);
*res = TRUE;
for (int curr = 0; curr < g_strv_length(args); curr += 2) {
for (guint curr = 0; curr < g_strv_length(args); curr += 2) {
g_hash_table_insert(options, args[curr], args[curr + 1]);
}

View File

@@ -4,33 +4,7 @@
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_PARSER_H

View File

@@ -3,35 +3,9 @@
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2019 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"

View File

@@ -3,35 +3,9 @@
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
* Copyright (C) 2019 - 2025 Michael Vetter <jubalh@iodoru.org>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
* Copyright (C) 2019 - 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_PLUGIN_DOWNLOAD_H

132
src/tools/spellcheck.c Normal file
View File

@@ -0,0 +1,132 @@
/*
* spellcheck.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
#ifdef HAVE_SPELLCHECK
#include <enchant.h>
#include <string.h>
#include <glib.h>
#include "tools/spellcheck.h"
#include "config/preferences.h"
#include "log.h"
#include "common.h"
static EnchantBroker* broker = NULL;
static EnchantDict* dict = NULL;
static char* current_lang = NULL;
void
spellcheck_init(void)
{
if (broker) {
return;
}
broker = enchant_broker_init();
if (!broker) {
log_error("Failed to initialize Enchant broker");
return;
}
prof_add_shutdown_routine(spellcheck_deinit);
char* lang = prefs_get_string(PREF_SPELLCHECK_LANG);
spellcheck_set_lang(lang);
g_free(lang);
}
void
spellcheck_deinit(void)
{
if (dict) {
enchant_broker_free_dict(broker, dict);
dict = NULL;
}
if (broker) {
enchant_broker_free(broker);
broker = NULL;
}
g_free(current_lang);
current_lang = NULL;
}
gboolean
spellcheck_set_lang(const char* lang)
{
if (!broker || !lang) {
return FALSE;
}
if (current_lang && strcmp(current_lang, lang) == 0 && dict) {
return TRUE;
}
EnchantDict* new_dict = enchant_broker_request_dict(broker, lang);
if (!new_dict) {
log_error("Failed to request Enchant dictionary for language: %s", lang);
return FALSE;
}
if (dict) {
enchant_broker_free_dict(broker, dict);
}
dict = new_dict;
g_free(current_lang);
current_lang = g_strdup(lang);
return TRUE;
}
const char*
spellcheck_get_lang(void)
{
return current_lang;
}
static void
_list_dicts_cb(const char* const lang_tag,
const char* const provider_name,
const char* const provider_desc,
const char* const provider_file,
void* user_data)
{
GList** list = (GList**)user_data;
if (g_list_find_custom(*list, lang_tag, (GCompareFunc)g_strcmp0) == NULL) {
*list = g_list_append(*list, g_strdup(lang_tag));
}
}
GList*
spellcheck_get_available_langs(void)
{
if (!broker) {
return NULL;
}
GList* list = NULL;
enchant_broker_list_dicts(broker, _list_dicts_cb, &list);
return g_list_sort(list, (GCompareFunc)g_strcmp0);
}
gboolean
spellcheck_is_misspelled(const char* word)
{
if (!dict || !word || word[0] == '\0') {
return FALSE;
}
int result = enchant_dict_check(dict, word, strlen(word));
return (result != 0);
}
#endif

58
src/tools/spellcheck.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* spellcheck.h
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2026 Michael Vetter <jubalh@iodoru.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#ifndef TOOLS_SPELLCHECK_H
#define TOOLS_SPELLCHECK_H
#include "config.h"
#include <glib.h>
#ifdef HAVE_SPELLCHECK
void spellcheck_init(void);
void spellcheck_deinit(void);
gboolean spellcheck_is_misspelled(const char* word);
gboolean spellcheck_set_lang(const char* lang);
const char* spellcheck_get_lang(void);
GList* spellcheck_get_available_langs(void);
#else
static inline void
spellcheck_init(void)
{
}
static inline void
spellcheck_deinit(void)
{
}
static inline gboolean
spellcheck_is_misspelled(const char* word)
{
return FALSE;
}
static inline gboolean
spellcheck_set_lang(const char* lang)
{
return FALSE;
}
static inline const char*
spellcheck_get_lang(void)
{
return NULL;
}
static inline GList*
spellcheck_get_available_langs(void)
{
return NULL;
}
#endif
#endif