some raii

master
Tony Olagbaiye 3 years ago
parent 43f3a3ffe4
commit bb375918ee
No known key found for this signature in database
GPG Key ID: 9E2FF3BDEBDFC910

@ -26,16 +26,16 @@ struct t_account *accounts = NULL;
struct t_account *last_account = NULL; struct t_account *last_account = NULL;
char *account_options[ACCOUNT_NUM_OPTIONS][2] = char *account_options[ACCOUNT_NUM_OPTIONS][2] =
{ { "jid", "" }, { { (char*)"jid", (char*)"" },
{ "password", "" }, { (char*)"password", (char*)"" },
{ "tls", "normal" }, { (char*)"tls", (char*)"normal" },
{ "nickname", "" }, { (char*)"nickname", (char*)"" },
{ "autoconnect", "" }, { (char*)"autoconnect", (char*)"" },
{ "resource", "" }, { (char*)"resource", (char*)"" },
{ "status", "probably about to segfault" }, { (char*)"status", (char*)"probably about to segfault" },
{ "pgp_pubring_path", "${weechat_data_dir}/pubring.gpg" }, { (char*)"pgp_pubring_path", (char*)"${weechat_data_dir}/pubring.gpg" },
{ "pgp_secring_path", "${weechat_data_dir}/secring.gpg" }, { (char*)"pgp_secring_path", (char*)"${weechat_data_dir}/secring.gpg" },
{ "pgp_keyid", "" }, { (char*)"pgp_keyid", (char*)"" },
}; };
struct t_account *account__search(const char *name) struct t_account *account__search(const char *name)
@ -179,7 +179,7 @@ xmpp_stanza_t *account__get_devicelist(struct t_account *account)
device = (struct t_account_device*)malloc(sizeof(struct t_account_device)); device = (struct t_account_device*)malloc(sizeof(struct t_account_device));
device->id = account->omemo->device_id; device->id = account->omemo.device_id;
snprintf(id, sizeof(id), "%u", device->id); snprintf(id, sizeof(id), "%u", device->id);
device->name = strdup(id); device->name = strdup(id);
device->label = strdup("weechat"); device->label = strdup("weechat");
@ -195,7 +195,7 @@ xmpp_stanza_t *account__get_devicelist(struct t_account *account)
for (device = account->devices; device; for (device = account->devices; device;
device = device->next_device) device = device->next_device)
{ {
if (device->id != account->omemo->device_id) if (device->id != account->omemo.device_id)
children[i++] = stanza__iq_pubsub_publish_item_list_device( children[i++] = stanza__iq_pubsub_publish_item_list_device(
account->context, NULL, with_noop(device->name), NULL); account->context, NULL, with_noop(device->name), NULL);
} }
@ -446,8 +446,6 @@ struct t_account *account__alloc(const char *name)
new_account->buffer = NULL; new_account->buffer = NULL;
new_account->buffer_as_string = NULL; new_account->buffer_as_string = NULL;
new_account->omemo = NULL;
new_account->devices = NULL; new_account->devices = NULL;
new_account->last_device = NULL; new_account->last_device = NULL;
new_account->mam_queries = NULL; new_account->mam_queries = NULL;
@ -543,9 +541,6 @@ void account__free_data(struct t_account *account)
if (account->buffer_as_string) if (account->buffer_as_string)
free(account->buffer_as_string); free(account->buffer_as_string);
if (account->omemo)
omemo__free(account->omemo);
//channel__free_all(account); //channel__free_all(account);
//user__free_all(account); //user__free_all(account);
} }

@ -4,11 +4,12 @@
#pragma once #pragma once
#ifdef __cplusplus
#include <ctime> #include <ctime>
#include <cstdint> #include <cstdint>
#include <memory>
#include <strophe.h> #include <strophe.h>
#endif
#include "omemo.hh"
extern struct t_account *accounts; extern struct t_account *accounts;
extern struct t_account *last_account; extern struct t_account *last_account;
@ -113,7 +114,7 @@ struct t_account
struct t_gui_buffer *buffer; struct t_gui_buffer *buffer;
char *buffer_as_string; char *buffer_as_string;
struct t_omemo *omemo; weechat::xmpp::omemo omemo;
struct t_pgp *pgp; struct t_pgp *pgp;
struct t_account_device *devices; struct t_account_device *devices;

@ -1044,7 +1044,7 @@ int channel__send_message(struct t_account *account, struct t_channel *channel,
if (account->omemo && channel->omemo.enabled) if (account->omemo && channel->omemo.enabled)
{ {
xmpp_stanza_t *encrypted = omemo__encode(account, to, body); xmpp_stanza_t *encrypted = account->omemo.encode(account, to, body);
if (!encrypted) if (!encrypted)
{ {
weechat_printf_date_tags(channel->buffer, 0, "notify_none", "%s%s", weechat_printf_date_tags(channel->buffer, 0, "notify_none", "%s%s",

@ -4,9 +4,7 @@
#pragma once #pragma once
#ifdef __cplusplus
#include <ctime> #include <ctime>
#endif
#define CHANNEL_MEMBERS_SPEAKING_LIMIT 128 #define CHANNEL_MEMBERS_SPEAKING_LIMIT 128

@ -395,7 +395,7 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
{ {
if (account->omemo) if (account->omemo)
{ {
omemo__handle_devicelist(account->omemo, account->omemo.handle_devicelist(
from ? from : account_jid(account), items); from ? from : account_jid(account), items);
} }
@ -533,7 +533,7 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
"eu.siacs.conversations.axolotl"); "eu.siacs.conversations.axolotl");
if (encrypted && account->omemo) if (encrypted && account->omemo)
{ {
cleartext = omemo__decode(account, from_bare, encrypted); cleartext = account->omemo.decode(account, from_bare, encrypted);
} }
x = xmpp_stanza_get_child_by_name_and_ns(stanza, "x", "jabber:x:encrypted"); x = xmpp_stanza_get_child_by_name_and_ns(stanza, "x", "jabber:x:encrypted");
intext = xmpp_stanza_get_text(body); intext = xmpp_stanza_get_text(body);
@ -979,7 +979,7 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
item, "list", "eu.siacs.conversations.axolotl"); item, "list", "eu.siacs.conversations.axolotl");
if (list && account->omemo) if (list && account->omemo)
{ {
omemo__handle_devicelist(account->omemo, account->omemo.handle_devicelist(
from ? from : account_jid(account), items); from ? from : account_jid(account), items);
xmpp_stanza_t *children[2] = {NULL}; xmpp_stanza_t *children[2] = {NULL};
@ -1024,7 +1024,7 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
dev = (t_account_device*)malloc(sizeof(struct t_account_device)); dev = (t_account_device*)malloc(sizeof(struct t_account_device));
dev->id = account->omemo->device_id; dev->id = account->omemo.device_id;
snprintf(id, sizeof(id), "%d", dev->id); snprintf(id, sizeof(id), "%d", dev->id);
dev->name = strdup(id); dev->name = strdup(id);
dev->label = strdup("weechat"); dev->label = strdup("weechat");
@ -1082,7 +1082,7 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
strlen("eu.siacs.conversations.axolotl.bundles:"); strlen("eu.siacs.conversations.axolotl.bundles:");
if (account->omemo && strlen(items_node) > node_prefix) if (account->omemo && strlen(items_node) > node_prefix)
{ {
omemo__handle_bundle(account->omemo, account->omemo.handle_bundle(
from ? from : account_jid(account), from ? from : account_jid(account),
strtol(items_node+node_prefix, strtol(items_node+node_prefix,
NULL, 10), NULL, 10),
@ -1314,13 +1314,13 @@ void connection__handler(xmpp_conn_t *conn, xmpp_conn_event_t status,
xmpp_send(conn, children[0]); xmpp_send(conn, children[0]);
xmpp_stanza_release(children[0]); xmpp_stanza_release(children[0]);
omemo__init(account->buffer, &account->omemo, account->name); account->omemo.init(account->buffer, account->name);
if (account->omemo) if (account->omemo)
{ {
children[0] = children[0] =
omemo__get_bundle(account->context, account->omemo.get_bundle(account->context,
strdup(account_jid(account)), NULL, account->omemo); strdup(account_jid(account)), NULL);
xmpp_send(conn, children[0]); xmpp_send(conn, children[0]);
xmpp_stanza_release(children[0]); xmpp_stanza_release(children[0]);
} }

@ -4,9 +4,7 @@
#pragma once #pragma once
#ifdef __cplusplus
#include <strophe.h> #include <strophe.h>
#endif
void connection__init(); void connection__init();

@ -12,6 +12,7 @@ INCLUDES=-Ilibstrophe -Ideps -Ideps/fmt/include \
$(shell pkg-config --cflags libomemo-c) $(shell pkg-config --cflags libomemo-c)
CFLAGS+=$(DBGCFLAGS) \ CFLAGS+=$(DBGCFLAGS) \
-fno-omit-frame-pointer -fPIC \ -fno-omit-frame-pointer -fPIC \
-fvisibility=hidden -fvisibility-inlines-hidden \
-std=gnu99 -gdwarf-4 \ -std=gnu99 -gdwarf-4 \
-Wall -Wextra -pedantic \ -Wall -Wextra -pedantic \
-Werror-implicit-function-declaration \ -Werror-implicit-function-declaration \
@ -20,6 +21,7 @@ CFLAGS+=$(DBGCFLAGS) \
$(INCLUDES) $(INCLUDES)
CPPFLAGS+=$(DBGCFLAGS) \ CPPFLAGS+=$(DBGCFLAGS) \
-fno-omit-frame-pointer -fPIC \ -fno-omit-frame-pointer -fPIC \
-fvisibility=hidden -fvisibility-inlines-hidden \
-std=c++20 -gdwarf-4 \ -std=c++20 -gdwarf-4 \
-Wall -Wextra -pedantic \ -Wall -Wextra -pedantic \
-Wno-missing-field-initializers \ -Wno-missing-field-initializers \

@ -37,7 +37,7 @@ char *message__translate_code(struct t_account *account,
case '#': /* channel */ case '#': /* channel */
if (alttext) if (alttext)
{ {
prefix = "#"; prefix = (char*)"#";
symbol = strdup(alttext); symbol = strdup(alttext);
} }
else else
@ -45,12 +45,12 @@ char *message__translate_code(struct t_account *account,
channel = channel__search(account, identifier+1); channel = channel__search(account, identifier+1);
if (channel) if (channel)
{ {
prefix = "#"; prefix = (char*)"#";
symbol = strdup(channel->name); symbol = strdup(channel->name);
} }
else else
{ {
prefix = "Channel:"; prefix = (char*)"Channel:";
symbol = strdup(identifier+1); symbol = strdup(identifier+1);
} }
} }
@ -58,7 +58,7 @@ char *message__translate_code(struct t_account *account,
case '@': /* user */ case '@': /* user */
if (alttext) if (alttext)
{ {
prefix = "@"; prefix = (char*)"@";
symbol = strdup(alttext); symbol = strdup(alttext);
} }
else else
@ -66,12 +66,12 @@ char *message__translate_code(struct t_account *account,
user = user__search(account, identifier+1); user = user__search(account, identifier+1);
if (user) if (user)
{ {
prefix = "@"; prefix = (char*)"@";
symbol = strdup(user->profile.display_name); symbol = strdup(user->profile.display_name);
} }
else else
{ {
prefix = "User:"; prefix = (char*)"User:";
symbol = strdup(identifier+1); symbol = strdup(identifier+1);
} }
} }
@ -79,17 +79,17 @@ char *message__translate_code(struct t_account *account,
case '!': /* special */ case '!': /* special */
if (alttext) if (alttext)
{ {
prefix = "@"; prefix = (char*)"@";
symbol = strdup(alttext); symbol = strdup(alttext);
} }
else else
{ {
prefix = "@"; prefix = (char*)"@";
symbol = strdup(identifier+1); symbol = strdup(identifier+1);
} }
break; break;
default: /* url */ default: /* url */
prefix = ""; prefix = (char*)"";
symbol = strdup(code); symbol = strdup(code);
break; break;
} }

File diff suppressed because it is too large Load Diff

@ -2,56 +2,78 @@
// License, version 2.0. If a copy of the MPL was not distributed with this // License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef _WEECHAT_XMPP_OMEMO_H_ #pragma once
#define _WEECHAT_XMPP_OMEMO_H_
extern const char *OMEMO_ADVICE; #include <memory>
#include <functional>
#include <cstdint>
#include <string>
#include <strophe.h>
#include <lmdb++.h>
#include <signal_protocol.h>
struct t_omemo #include "signal.hh"
{ struct t_account;
struct signal_context *context;
struct signal_protocol_store_context *store_context;
struct t_omemo_db *db; extern const char *OMEMO_ADVICE;
char *db_path;
struct ratchet_identity_key_pair *identity; namespace weechat::xmpp {
uint32_t device_id; struct t_pre_key {
const char *id;
const char *public_key;
}; };
struct t_omemo_bundle_req struct omemo
{ {
char *id; libsignal::context context;
char *jid; libsignal::store_context store_context;
char *device;
char *message_text;
};
struct t_omemo_devicelist_req struct {
{ lmdb::env env;
char *id; lmdb::dbi dbi_omemo;
struct t_omemo_bundle_req bundle_req; } db;
}; std::string db_path;
xmpp_stanza_t *omemo__get_bundle(xmpp_ctx_t *context, char *from, char *to, libsignal::identity_key_pair identity;
struct t_omemo *omemo);
void omemo__init(struct t_gui_buffer *buffer, struct t_omemo **omemo, std::uint32_t device_id;
const char *account_name);
void omemo__handle_devicelist(struct t_omemo *omemo, const char *jid, class bundle_request
xmpp_stanza_t *items); {
public:
std::string id;
std::string jid;
std::string device;
std::string message_text;
};
class devicelist_request
{
public:
std::string id;
bundle_request bundle_req;
};
void omemo__handle_bundle(struct t_omemo *omemo, const char *jid, ~omemo();
uint32_t device_id, xmpp_stanza_t *items);
char *omemo__decode(struct t_account *account, const char *jid, inline operator bool() { return this->context && this->store_context &&
this->identity && this->device_id != 0; }
xmpp_stanza_t *get_bundle(xmpp_ctx_t *context, char *from, char *to);
void init(struct t_gui_buffer *buffer, const char *account_name);
void handle_devicelist(const char *jid, xmpp_stanza_t *items);
void handle_bundle(const char *jid, std::uint32_t device_id,
xmpp_stanza_t *items);
char *decode(struct t_account *account, const char *jid,
xmpp_stanza_t *encrypted); xmpp_stanza_t *encrypted);
xmpp_stanza_t *omemo__encode(struct t_account *account, const char *jid, xmpp_stanza_t *encode(struct t_account *account, const char *jid,
const char *unencrypted); const char *unencrypted);
};
void omemo__free(struct t_omemo *omemo); }
#endif /*WEECHAT_XMPP_OMEMO_H*/

@ -2,8 +2,7 @@
// License, version 2.0. If a copy of the MPL was not distributed with this // License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef _WEECHAT_XMPP_PGP_H_ #pragma once
#define _WEECHAT_XMPP_PGP_H_
extern const char *PGP_ADVICE; extern const char *PGP_ADVICE;
@ -24,5 +23,3 @@ char *pgp__encrypt(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *s
char *pgp__verify(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *certificate); char *pgp__verify(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *certificate);
char *pgp__sign(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *source, const char *message); char *pgp__sign(struct t_gui_buffer *buffer, struct t_pgp *pgp, const char *source, const char *message);
#endif /*WEECHAT_XMPP_PGP_H*/

@ -18,7 +18,14 @@
#include "buffer.hh" #include "buffer.hh"
#include "completion.hh" #include "completion.hh"
struct t_weechat_plugin *weechat_xmpp_plugin = NULL;
struct t_hook *weechat_xmpp_process_timer = NULL;
struct t_gui_bar_item *weechat_xmpp_typing_bar_item = NULL;
extern "C" { extern "C" {
#pragma GCC visibility push(default)
WEECHAT_PLUGIN_NAME(WEECHAT_XMPP_PLUGIN_NAME); WEECHAT_PLUGIN_NAME(WEECHAT_XMPP_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(N_("XMPP client protocol")); WEECHAT_PLUGIN_DESCRIPTION(N_("XMPP client protocol"));
WEECHAT_PLUGIN_AUTHOR("bqv <weechat@fron.io>"); WEECHAT_PLUGIN_AUTHOR("bqv <weechat@fron.io>");
@ -27,12 +34,6 @@ WEECHAT_PLUGIN_LICENSE("MPL2");
WEECHAT_PLUGIN_PRIORITY(5500); WEECHAT_PLUGIN_PRIORITY(5500);
} }
struct t_weechat_plugin *weechat_xmpp_plugin = NULL;
struct t_hook *weechat_xmpp_process_timer = NULL;
struct t_gui_bar_item *weechat_xmpp_typing_bar_item = NULL;
extern "C" extern "C"
int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[]) int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
{ {

@ -0,0 +1,57 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <memory>
#include <functional>
#include <signal_protocol.h>
namespace libsignal {
template<typename T>
struct deleter {
void operator() (T *ptr) { SIGNAL_UNREF(ptr); }
};
template<>
struct deleter<struct signal_context> {
void operator() (struct signal_context *ptr)
{ signal_context_destroy(ptr); }
};
template<>
struct deleter<struct signal_protocol_store_context> {
void operator() (struct signal_protocol_store_context *ptr)
{ signal_protocol_store_context_destroy(ptr); }
};
template<>
struct deleter<struct ratchet_identity_key_pair> {
void operator() (struct ratchet_identity_key_pair *ptr)
{ ratchet_identity_key_pair_destroy(
reinterpret_cast<signal_type_base*>(ptr)); }
};
template<typename T>
using object = std::unique_ptr<T, deleter<T>>;
template<typename T, typename... Args>
object<T> make(int (*fun)(T**,Args...), Args... args) {
T *result;
fun(&result, args...);
return object<T>(result);
};
typedef object<struct signal_context> context;
typedef object<struct signal_protocol_store_context> store_context;
typedef object<struct ratchet_identity_key_pair> identity_key_pair;
typedef object<struct ec_public_key> public_key;
typedef object<struct ec_private_key> private_key;
}

@ -0,0 +1,13 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <memory>
#include <functional>
#include <strophe.h>
namespace strophe {
}

@ -103,21 +103,21 @@ void user__nicklist_add(struct t_account *account,
ptr_buffer = channel ? channel->buffer : account->buffer; ptr_buffer = channel ? channel->buffer : account->buffer;
char *group = "..."; char *group = (char*)"...";
if (weechat_strcasecmp(user->profile.affiliation, "outcast") == 0) if (weechat_strcasecmp(user->profile.affiliation, (char*)"outcast") == 0)
group = "!"; group = (char*)"!";
if (weechat_strcasecmp(user->profile.role, "visitor") == 0) if (weechat_strcasecmp(user->profile.role, (char*)"visitor") == 0)
group = "?"; group = (char*)"?";
if (weechat_strcasecmp(user->profile.role, "participant") == 0) if (weechat_strcasecmp(user->profile.role, (char*)"participant") == 0)
group = "+"; group = (char*)"+";
if (weechat_strcasecmp(user->profile.affiliation, "member") == 0) if (weechat_strcasecmp(user->profile.affiliation, (char*)"member") == 0)
group = "%"; group = (char*)"%";
if (weechat_strcasecmp(user->profile.role, "moderator") == 0) if (weechat_strcasecmp(user->profile.role, (char*)"moderator") == 0)
group = "@"; group = (char*)"@";
if (weechat_strcasecmp(user->profile.affiliation, "admin") == 0) if (weechat_strcasecmp(user->profile.affiliation, (char*)"admin") == 0)
group = "&"; group = (char*)"&";
if (weechat_strcasecmp(user->profile.affiliation, "owner") == 0) if (weechat_strcasecmp(user->profile.affiliation, (char*)"owner") == 0)
group = "~"; group = (char*)"~";
ptr_group = weechat_nicklist_search_group(ptr_buffer, NULL, group); ptr_group = weechat_nicklist_search_group(ptr_buffer, NULL, group);
weechat_nicklist_add_nick(ptr_buffer, ptr_group, weechat_nicklist_add_nick(ptr_buffer, ptr_group,
name, name,

Loading…
Cancel
Save