some raii

master
Tony Olagbaiye 2 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;
char *account_options[ACCOUNT_NUM_OPTIONS][2] =
{ { "jid", "" },
{ "password", "" },
{ "tls", "normal" },
{ "nickname", "" },
{ "autoconnect", "" },
{ "resource", "" },
{ "status", "probably about to segfault" },
{ "pgp_pubring_path", "${weechat_data_dir}/pubring.gpg" },
{ "pgp_secring_path", "${weechat_data_dir}/secring.gpg" },
{ "pgp_keyid", "" },
{ { (char*)"jid", (char*)"" },
{ (char*)"password", (char*)"" },
{ (char*)"tls", (char*)"normal" },
{ (char*)"nickname", (char*)"" },
{ (char*)"autoconnect", (char*)"" },
{ (char*)"resource", (char*)"" },
{ (char*)"status", (char*)"probably about to segfault" },
{ (char*)"pgp_pubring_path", (char*)"${weechat_data_dir}/pubring.gpg" },
{ (char*)"pgp_secring_path", (char*)"${weechat_data_dir}/secring.gpg" },
{ (char*)"pgp_keyid", (char*)"" },
};
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->id = account->omemo->device_id;
device->id = account->omemo.device_id;
snprintf(id, sizeof(id), "%u", device->id);
device->name = strdup(id);
device->label = strdup("weechat");
@ -195,7 +195,7 @@ xmpp_stanza_t *account__get_devicelist(struct t_account *account)
for (device = account->devices; 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(
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_as_string = NULL;
new_account->omemo = NULL;
new_account->devices = NULL;
new_account->last_device = NULL;
new_account->mam_queries = NULL;
@ -543,9 +541,6 @@ void account__free_data(struct t_account *account)
if (account->buffer_as_string)
free(account->buffer_as_string);
if (account->omemo)
omemo__free(account->omemo);
//channel__free_all(account);
//user__free_all(account);
}

@ -4,11 +4,12 @@
#pragma once
#ifdef __cplusplus
#include <ctime>
#include <cstdint>
#include <memory>
#include <strophe.h>
#endif
#include "omemo.hh"
extern struct t_account *accounts;
extern struct t_account *last_account;
@ -113,7 +114,7 @@ struct t_account
struct t_gui_buffer *buffer;
char *buffer_as_string;
struct t_omemo *omemo;
weechat::xmpp::omemo omemo;
struct t_pgp *pgp;
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)
{
xmpp_stanza_t *encrypted = omemo__encode(account, to, body);
xmpp_stanza_t *encrypted = account->omemo.encode(account, to, body);
if (!encrypted)
{
weechat_printf_date_tags(channel->buffer, 0, "notify_none", "%s%s",

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

@ -395,8 +395,8 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
{
if (account->omemo)
{
omemo__handle_devicelist(account->omemo,
from ? from : account_jid(account), items);
account->omemo.handle_devicelist(
from ? from : account_jid(account), items);
}
children = (xmpp_stanza_t**)malloc(sizeof(*children) * (3 + 1));
@ -533,7 +533,7 @@ int connection__message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *
"eu.siacs.conversations.axolotl");
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");
intext = xmpp_stanza_get_text(body);
@ -979,8 +979,8 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
item, "list", "eu.siacs.conversations.axolotl");
if (list && account->omemo)
{
omemo__handle_devicelist(account->omemo,
from ? from : account_jid(account), items);
account->omemo.handle_devicelist(
from ? from : account_jid(account), items);
xmpp_stanza_t *children[2] = {NULL};
for (device = xmpp_stanza_get_children(list);
@ -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->id = account->omemo->device_id;
dev->id = account->omemo.device_id;
snprintf(id, sizeof(id), "%d", dev->id);
dev->name = strdup(id);
dev->label = strdup("weechat");
@ -1082,8 +1082,8 @@ int connection__iq_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userd
strlen("eu.siacs.conversations.axolotl.bundles:");
if (account->omemo && strlen(items_node) > node_prefix)
{
omemo__handle_bundle(account->omemo,
from ? from : account_jid(account),
account->omemo.handle_bundle(
from ? from : account_jid(account),
strtol(items_node+node_prefix,
NULL, 10),
items);
@ -1314,13 +1314,13 @@ void connection__handler(xmpp_conn_t *conn, xmpp_conn_event_t status,
xmpp_send(conn, 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)
{
children[0] =
omemo__get_bundle(account->context,
strdup(account_jid(account)), NULL, account->omemo);
account->omemo.get_bundle(account->context,
strdup(account_jid(account)), NULL);
xmpp_send(conn, children[0]);
xmpp_stanza_release(children[0]);
}

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

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

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

@ -7,4 +7,4 @@
#define MESSAGE_MAX_LENGTH 40000
char *message__decode(struct t_account *account,
const char *text);
const char *text);

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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef _WEECHAT_XMPP_OMEMO_H_
#define _WEECHAT_XMPP_OMEMO_H_
#pragma once
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
{
struct signal_context *context;
struct signal_protocol_store_context *store_context;
#include "signal.hh"
struct t_account;
struct t_omemo_db *db;
char *db_path;
extern const char *OMEMO_ADVICE;
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;
char *jid;
char *device;
char *message_text;
};
libsignal::context context;
libsignal::store_context store_context;
struct t_omemo_devicelist_req
{
char *id;
struct t_omemo_bundle_req bundle_req;
};
struct {
lmdb::env env;
lmdb::dbi dbi_omemo;
} db;
std::string db_path;
xmpp_stanza_t *omemo__get_bundle(xmpp_ctx_t *context, char *from, char *to,
struct t_omemo *omemo);
libsignal::identity_key_pair identity;
void omemo__init(struct t_gui_buffer *buffer, struct t_omemo **omemo,
const char *account_name);
std::uint32_t device_id;
void omemo__handle_devicelist(struct t_omemo *omemo, const char *jid,
xmpp_stanza_t *items);
class bundle_request
{
public:
std::string id;
std::string jid;
std::string device;
std::string message_text;
};
void omemo__handle_bundle(struct t_omemo *omemo, const char *jid,
uint32_t device_id, xmpp_stanza_t *items);
class devicelist_request
{
public:
std::string id;
bundle_request bundle_req;
};
char *omemo__decode(struct t_account *account, const char *jid,
xmpp_stanza_t *encrypted);
~omemo();
xmpp_stanza_t *omemo__encode(struct t_account *account, const char *jid,
const char *unencrypted);
inline operator bool() { return this->context && this->store_context &&
this->identity && this->device_id != 0; }
void omemo__free(struct t_omemo *omemo);
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 *encode(struct t_account *account, const char *jid,
const char *unencrypted);
};
#endif /*WEECHAT_XMPP_OMEMO_H*/
}

@ -2,8 +2,7 @@
// 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/.
#ifndef _WEECHAT_XMPP_PGP_H_
#define _WEECHAT_XMPP_PGP_H_
#pragma once
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__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 "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" {
#pragma GCC visibility push(default)
WEECHAT_PLUGIN_NAME(WEECHAT_XMPP_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(N_("XMPP client protocol"));
WEECHAT_PLUGIN_AUTHOR("bqv <weechat@fron.io>");
@ -27,12 +34,6 @@ WEECHAT_PLUGIN_LICENSE("MPL2");
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"
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;
char *group = "...";
if (weechat_strcasecmp(user->profile.affiliation, "outcast") == 0)
group = "!";
if (weechat_strcasecmp(user->profile.role, "visitor") == 0)
group = "?";
if (weechat_strcasecmp(user->profile.role, "participant") == 0)
group = "+";
if (weechat_strcasecmp(user->profile.affiliation, "member") == 0)
group = "%";
if (weechat_strcasecmp(user->profile.role, "moderator") == 0)
group = "@";
if (weechat_strcasecmp(user->profile.affiliation, "admin") == 0)
group = "&";
if (weechat_strcasecmp(user->profile.affiliation, "owner") == 0)
group = "~";
char *group = (char*)"...";
if (weechat_strcasecmp(user->profile.affiliation, (char*)"outcast") == 0)
group = (char*)"!";
if (weechat_strcasecmp(user->profile.role, (char*)"visitor") == 0)
group = (char*)"?";
if (weechat_strcasecmp(user->profile.role, (char*)"participant") == 0)
group = (char*)"+";
if (weechat_strcasecmp(user->profile.affiliation, (char*)"member") == 0)
group = (char*)"%";
if (weechat_strcasecmp(user->profile.role, (char*)"moderator") == 0)
group = (char*)"@";
if (weechat_strcasecmp(user->profile.affiliation, (char*)"admin") == 0)
group = (char*)"&";
if (weechat_strcasecmp(user->profile.affiliation, (char*)"owner") == 0)
group = (char*)"~";
ptr_group = weechat_nicklist_search_group(ptr_buffer, NULL, group);
weechat_nicklist_add_nick(ptr_buffer, ptr_group,
name,

Loading…
Cancel
Save