2007-01-25 07:40:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2004-2005 Lee Hardy <lee -at- leeh.co.uk>
|
|
|
|
* Copyright (C) 2004-2005 ircd-ratbox development team
|
2016-09-23 01:59:22 +02:00
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2007-01-25 07:40:21 +01:00
|
|
|
*/
|
2016-08-13 05:05:54 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_HOOK_H
|
|
|
|
|
2016-09-23 01:59:22 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace hook {
|
|
|
|
|
|
|
|
using ctx::future;
|
|
|
|
|
|
|
|
// Function returns an empty future for serialization; valid for asynchronous
|
|
|
|
template<class state> using closure = std::function<future<> (state)>;
|
|
|
|
|
|
|
|
// Gives name of event required to happen.first (before), and required to happen.second (after)
|
|
|
|
using relationship = std::pair<std::string, std::string>;
|
|
|
|
|
|
|
|
// Returns true if A happens before B (for sorting)
|
|
|
|
bool happens_before(const std::string &a_name, const relationship &a_happens,
|
|
|
|
const std::string &b_name, const relationship &b_happens);
|
|
|
|
|
|
|
|
|
|
|
|
template<class state = void>
|
|
|
|
struct phase
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
relationship happens;
|
|
|
|
closure<state> function;
|
|
|
|
|
|
|
|
phase(const std::string &name, const relationship &, closure<state>&&);
|
|
|
|
phase(const std::string &name, closure<state>&&);
|
|
|
|
phase() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
phase<state>::phase(const std::string &name,
|
|
|
|
closure<state>&& function)
|
|
|
|
:phase{name, {}, std::forward<closure<state>>(function)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
phase<state>::phase(const std::string &name,
|
|
|
|
const relationship &happens,
|
|
|
|
closure<state>&& function)
|
|
|
|
:name{name}
|
|
|
|
,happens{happens}
|
|
|
|
,function{std::forward<closure<state>>(function)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
struct execution
|
|
|
|
{
|
|
|
|
std::map<std::string, future<>> pending; // phase.name => future
|
|
|
|
std::multimap<std::string, std::string> fences; // happens.second = > phase name
|
|
|
|
|
|
|
|
void fences_wait(const std::string &name);
|
|
|
|
void pending_wait(const std::string &name);
|
|
|
|
void pending_wait();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
execution<state>::pending_wait()
|
|
|
|
{
|
|
|
|
for(const auto &pit : pending)
|
|
|
|
{
|
|
|
|
const auto &future(pit.second);
|
|
|
|
future.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
execution<state>::pending_wait(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto pit(pending.find(name));
|
|
|
|
if(pit == end(pending))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const scope erase([this, &pit]
|
|
|
|
{
|
|
|
|
pending.erase(pit);
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto &future(pit->second);
|
|
|
|
future.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
execution<state>::fences_wait(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto ppit(fences.equal_range(name));
|
|
|
|
const scope erase([this, &ppit]
|
|
|
|
{
|
|
|
|
fences.erase(ppit.first, ppit.second);
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto pit(ppit.first); pit != ppit.second; ++pit)
|
|
|
|
{
|
|
|
|
const auto &name(pit->second);
|
|
|
|
pending_wait(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class state = void>
|
|
|
|
struct sequence
|
|
|
|
{
|
|
|
|
std::list<phase<state>> space;
|
|
|
|
|
|
|
|
void sort();
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<class... phase> void add(phase&&...);
|
|
|
|
void del(const std::string &name);
|
|
|
|
|
|
|
|
void operator()(state);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
sequence<state>::operator()(state s)
|
|
|
|
{
|
|
|
|
execution<state> e;
|
|
|
|
for(const auto &phase : space)
|
|
|
|
{
|
|
|
|
e.fences_wait(phase.name);
|
|
|
|
e.pending_wait(phase.happens.first);
|
|
|
|
|
|
|
|
auto future(phase.function(s));
|
|
|
|
if(!future.valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!phase.happens.second.empty())
|
|
|
|
e.fences.emplace(phase.happens.second, phase.name);
|
|
|
|
|
|
|
|
e.pending.emplace(phase.name, std::move(future));
|
|
|
|
}
|
|
|
|
|
|
|
|
e.pending_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
sequence<state>::del(const std::string &name)
|
|
|
|
{
|
|
|
|
space.remove_if([&name]
|
|
|
|
(const auto &phase)
|
|
|
|
{
|
|
|
|
return phase.name == name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
template<class... phase>
|
|
|
|
void
|
|
|
|
sequence<state>::add(phase&&... p)
|
|
|
|
{
|
|
|
|
space.emplace_back(std::forward<phase>(p)...);
|
|
|
|
sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
void
|
|
|
|
sequence<state>::sort()
|
|
|
|
{
|
|
|
|
space.sort([]
|
|
|
|
(const auto &a, const auto &b)
|
|
|
|
{
|
|
|
|
return happens_before(a.name, a.happens, b.name, b.happens);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class state = void>
|
|
|
|
class lambda
|
|
|
|
{
|
|
|
|
sequence<state> *s;
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
public:
|
|
|
|
lambda(sequence<state> &, const std::string &name, const relationship &, closure<state>);
|
|
|
|
lambda(sequence<state> &, const std::string &name, closure<state>);
|
|
|
|
~lambda() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
lambda<state>::lambda(sequence<state> &s,
|
|
|
|
const std::string &name,
|
|
|
|
const relationship &relationship,
|
|
|
|
closure<state> closure)
|
|
|
|
:s{&s}
|
|
|
|
,name{name}
|
|
|
|
{
|
|
|
|
s.add(name, relationship, std::move(closure));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
lambda<state>::lambda(sequence<state> &s,
|
|
|
|
const std::string &name,
|
|
|
|
closure<state> closure)
|
|
|
|
:lambda{s, name, {}, std::move(closure)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
lambda<state>::~lambda()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
s->del(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class state = void>
|
|
|
|
class function
|
|
|
|
:lambda<state>
|
|
|
|
{
|
|
|
|
virtual future<> operator()(state) const = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
function(sequence<state> &, const std::string &name, const relationship & = {});
|
|
|
|
virtual ~function() noexcept = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class state>
|
|
|
|
function<state>::function(sequence<state> &s,
|
|
|
|
const std::string &name,
|
|
|
|
const relationship &relationship)
|
|
|
|
:lambda<state>{s, name, relationship, [this]
|
|
|
|
(state&& st)
|
|
|
|
{
|
|
|
|
return this->operator()(std::forward<state>(st));
|
|
|
|
}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hook
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-08-13 05:05:54 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace ircd {
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *name;
|
2008-04-01 22:18:48 +02:00
|
|
|
rb_dlink_list hooks;
|
2007-01-25 07:40:21 +01:00
|
|
|
} hook;
|
|
|
|
|
|
|
|
typedef void (*hookfn) (void *data);
|
|
|
|
|
2008-04-20 15:20:10 +02:00
|
|
|
extern int h_iosend_id;
|
|
|
|
extern int h_iorecv_id;
|
|
|
|
extern int h_iorecvctrl_id;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2008-04-20 15:20:10 +02:00
|
|
|
extern int h_burst_client;
|
|
|
|
extern int h_burst_channel;
|
|
|
|
extern int h_burst_finished;
|
|
|
|
extern int h_server_introduced;
|
|
|
|
extern int h_server_eob;
|
|
|
|
extern int h_client_exit;
|
|
|
|
extern int h_umode_changed;
|
|
|
|
extern int h_new_local_user;
|
|
|
|
extern int h_new_remote_user;
|
|
|
|
extern int h_introduce_client;
|
2009-04-20 16:20:11 +02:00
|
|
|
extern int h_can_kick;
|
2012-04-01 04:20:02 +02:00
|
|
|
extern int h_privmsg_channel;
|
|
|
|
extern int h_privmsg_user;
|
2012-05-21 22:22:07 +02:00
|
|
|
extern int h_conf_read_start;
|
|
|
|
extern int h_conf_read_end;
|
2016-02-20 22:49:41 +01:00
|
|
|
extern int h_outbound_msgbuf;
|
2016-04-06 12:43:54 +02:00
|
|
|
extern int h_rehash;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
void init_hook(void);
|
|
|
|
int register_hook(const char *name);
|
|
|
|
void add_hook(const char *name, hookfn fn);
|
|
|
|
void remove_hook(const char *name, hookfn fn);
|
|
|
|
void call_hook(int id, void *arg);
|
|
|
|
|
2016-02-21 00:44:13 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2016-02-21 00:44:13 +01:00
|
|
|
void *arg1;
|
|
|
|
void *arg2;
|
|
|
|
} hook_data;
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2007-01-25 07:40:21 +01:00
|
|
|
const void *arg1;
|
|
|
|
const void *arg2;
|
2016-02-21 00:44:13 +01:00
|
|
|
} hook_cdata;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2007-01-25 07:40:21 +01:00
|
|
|
const void *arg1;
|
|
|
|
int arg2;
|
2016-01-14 13:57:04 +01:00
|
|
|
int result;
|
2007-01-25 07:40:21 +01:00
|
|
|
} hook_data_int;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
|
|
|
client::client *target;
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr;
|
2016-01-06 01:37:42 +01:00
|
|
|
int approved;
|
2007-01-25 07:40:21 +01:00
|
|
|
} hook_data_client;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
int approved;
|
|
|
|
} hook_data_channel;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr;
|
2007-01-25 07:40:21 +01:00
|
|
|
char *key;
|
|
|
|
} hook_data_channel_activity;
|
|
|
|
|
2009-04-20 16:20:11 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr;
|
|
|
|
chan::membership *msptr;
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *target;
|
2009-04-20 16:20:11 +02:00
|
|
|
int approved;
|
2015-12-10 08:00:32 +01:00
|
|
|
int dir;
|
2016-01-12 05:40:32 +01:00
|
|
|
const char *modestr;
|
2009-04-20 16:20:11 +02:00
|
|
|
} hook_data_channel_approval;
|
|
|
|
|
2007-01-25 07:40:21 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
|
|
|
client::client *target;
|
2007-01-25 07:40:21 +01:00
|
|
|
int approved;
|
|
|
|
} hook_data_client_approval;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-09-23 01:59:22 +02:00
|
|
|
client::client *local_link; // local client originating this, or NULL
|
|
|
|
client::client *target; // dying client
|
|
|
|
client::client *from; // causing client (could be &me or target)
|
2007-01-25 07:40:21 +01:00
|
|
|
const char *comment;
|
|
|
|
} hook_data_client_exit;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client;
|
2007-01-25 07:40:21 +01:00
|
|
|
unsigned int oldumodes;
|
|
|
|
unsigned int oldsnomask;
|
|
|
|
} hook_data_umode_changed;
|
|
|
|
|
2012-04-01 04:20:02 +02:00
|
|
|
enum message_type {
|
|
|
|
MESSAGE_TYPE_NOTICE,
|
|
|
|
MESSAGE_TYPE_PRIVMSG,
|
2012-04-07 05:03:07 +02:00
|
|
|
MESSAGE_TYPE_PART,
|
2012-04-01 04:20:02 +02:00
|
|
|
MESSAGE_TYPE_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
enum message_type msgtype;
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *source_p;
|
2016-08-18 07:33:38 +02:00
|
|
|
chan::chan *chptr;
|
2012-04-01 04:20:02 +02:00
|
|
|
const char *text;
|
|
|
|
int approved;
|
2016-01-29 18:02:56 +01:00
|
|
|
const char *reason;
|
2012-04-01 04:20:02 +02:00
|
|
|
} hook_data_privmsg_channel;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
enum message_type msgtype;
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *source_p;
|
|
|
|
client::client *target_p;
|
2012-04-01 04:20:02 +02:00
|
|
|
const char *text;
|
|
|
|
int approved;
|
|
|
|
} hook_data_privmsg_user;
|
|
|
|
|
2016-04-06 12:43:54 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool signal;
|
|
|
|
} hook_data_rehash;
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
} // namespace ircd
|
|
|
|
#endif // __cplusplus
|
2016-09-23 01:59:22 +02:00
|
|
|
|
|
|
|
*/
|