You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB
C++

3 years ago
// 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/.
2 years ago
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <ctime>
2 years ago
#include <csignal>
#include <exception>
3 years ago
#include <weechat/weechat-plugin.h>
3 years ago
2 years ago
#include "plugin.hh"
2 years ago
#include "config.hh"
#include "account.hh"
#include "connection.hh"
#include "command.hh"
2 years ago
#include "input.hh"
#include "buffer.hh"
#include "completion.hh"
3 years ago
#define WEECHAT_TIMER_INTERVAL_SEC 0.01
#define WEECHAT_TIMER_SECONDS(IVL) (WEECHAT_TIMER_INTERVAL_SEC * IVL)
2 years ago
#pragma GCC visibility push(default)
extern "C" {
3 years ago
WEECHAT_PLUGIN_NAME(WEECHAT_XMPP_PLUGIN_NAME);
3 years ago
WEECHAT_PLUGIN_DESCRIPTION(N_("XMPP client protocol"));
3 years ago
WEECHAT_PLUGIN_AUTHOR("bqv <weechat@fron.io>");
3 years ago
WEECHAT_PLUGIN_VERSION(WEECHAT_XMPP_PLUGIN_VERSION);
3 years ago
WEECHAT_PLUGIN_LICENSE("MPL2");
WEECHAT_PLUGIN_PRIORITY(5500);
}
3 years ago
void (* weechat_signal_handler)(int);
2 years ago
extern "C"
void wrapped_signal_handler(int arg)
{ // wrap weechat's handler
weechat_signal_handler(arg);
2 years ago
__asm__("int3");
}
extern "C"
3 years ago
int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
{
try {
weechat::plugin::instance = std::make_unique<weechat::plugin>(plugin);
weechat::plugin::instance->init(argc, argv);
}
catch (std::exception const& ex) {
return WEECHAT_RC_ERROR;
}
2 years ago
return WEECHAT_RC_OK;
}
3 years ago
extern "C"
int weechat_plugin_end(struct t_weechat_plugin *plugin)
{
try {
if (plugin != *weechat::plugin::instance)
throw std::runtime_error("wrong plugin?");
weechat::plugin::instance->end();
weechat::plugin::instance.reset();
}
catch (std::exception const& ex) {
3 years ago
return WEECHAT_RC_ERROR;
}
return WEECHAT_RC_OK;
}
std::unique_ptr<weechat::plugin> weechat::plugin::instance;
weechat::plugin::plugin(struct t_weechat_plugin *plugin)
: m_plugin_ptr(plugin)
{
}
void weechat::plugin::init(int argc, char *argv[])
{
m_args = std::vector<std::string_view>(argv, argv+argc);
if (std::find(m_args.begin(), m_args.end(), "debug") != m_args.end())
weechat_signal_handler = std::signal(SIGSEGV, wrapped_signal_handler);
if (!weechat::config::init()) // TODO: bool -> exceptions
throw std::runtime_error("Config init failed");
3 years ago
weechat::config::read();
3 years ago
weechat::connection::init();
3 years ago
command__init(); // TODO: port
completion__init(); // TODO: port
3 years ago
m_process_timer = weechat_hook_timer(WEECHAT_TIMER_SECONDS(1000), 0, 0,
&weechat::account::timer_cb,
nullptr, nullptr);
3 years ago
if (!weechat_bar_search(typing_bar_name.data()))
3 years ago
{
weechat_bar_new(typing_bar_name.data(), "off", "400", "window", "${typing}",
3 years ago
"bottom", "horizontal", "vertical",
"1", "1", "default", "default", "default", "default",
"off", typing_bar_item_name.data());
3 years ago
}
m_typing_bar_item = weechat_bar_item_new(typing_bar_item_name.data(),
&buffer__typing_bar_cb, // TODO: port
nullptr, nullptr);
3 years ago
weechat_hook_signal("input_text_changed",
&input__text_changed_cb, // TODO: port
nullptr, nullptr);
3 years ago
}
void weechat::plugin::end() {
if (m_typing_bar_item) // raii?
weechat_bar_item_remove(m_typing_bar_item);
3 years ago
if (m_process_timer) // raii?
weechat_unhook(m_process_timer);
3 years ago
weechat::config::write();
weechat::config::instance.reset();
weechat::account::disconnect_all();
3 years ago
weechat::accounts.clear();
3 years ago
libstrophe::shutdown();
}
3 years ago
weechat::plugin::~plugin()
{
3 years ago
}