-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplugin.cpp
140 lines (109 loc) · 3.79 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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/.
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <csignal>
#include <exception>
#include <weechat/weechat-plugin.h>
#include "plugin.hh"
#include "config.hh"
#include "account.hh"
#include "connection.hh"
#include "command.hh"
#include "input.hh"
#include "buffer.hh"
#include "completion.hh"
#define WEECHAT_TIMER_INTERVAL_SEC 0.01
#define WEECHAT_TIMER_SECONDS(IVL) (WEECHAT_TIMER_INTERVAL_SEC * IVL)
#pragma GCC visibility push(default)
extern "C" {
WEECHAT_PLUGIN_NAME(WEECHAT_XMPP_PLUGIN_NAME);
WEECHAT_PLUGIN_DESCRIPTION(N_("XMPP client protocol"));
WEECHAT_PLUGIN_AUTHOR("bqv <[email protected]>");
WEECHAT_PLUGIN_VERSION(WEECHAT_XMPP_PLUGIN_VERSION);
WEECHAT_PLUGIN_LICENSE("MPL2");
WEECHAT_PLUGIN_PRIORITY(5500);
}
void (* weechat_signal_handler)(int);
extern "C"
void wrapped_signal_handler(int arg)
{ // wrap weechat's handler
weechat_signal_handler(arg);
__asm__("int3");
}
extern "C"
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;
}
return WEECHAT_RC_OK;
}
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) {
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");
weechat::config::read();
weechat::connection::init();
command__init(); // TODO: port
completion__init(); // TODO: port
m_process_timer = weechat_hook_timer(WEECHAT_TIMER_SECONDS(1000), 0, 0,
&weechat::account::timer_cb,
nullptr, nullptr);
if (!weechat_bar_search(typing_bar_name.data()))
{
weechat_bar_new(typing_bar_name.data(), "off", "400", "window", "${typing}",
"bottom", "horizontal", "vertical",
"1", "1", "default", "default", "default", "default",
"off", typing_bar_item_name.data());
}
m_typing_bar_item = weechat_bar_item_new(typing_bar_item_name.data(),
&buffer__typing_bar_cb, // TODO: port
nullptr, nullptr);
weechat_hook_signal("input_text_changed",
&input__text_changed_cb, // TODO: port
nullptr, nullptr);
}
void weechat::plugin::end() {
if (m_typing_bar_item) // raii?
weechat_bar_item_remove(m_typing_bar_item);
if (m_process_timer) // raii?
weechat_unhook(m_process_timer);
weechat::config::write();
weechat::config::instance.reset();
weechat::account::disconnect_all();
weechat::accounts.clear();
libstrophe::shutdown();
}
weechat::plugin::~plugin()
{
}