2005-09-15 09:58:31 -04:00
|
|
|
/* Ruby scripting hooks */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2022-01-15 11:56:00 -05:00
|
|
|
#undef _GNU_SOURCE
|
2005-09-15 09:58:31 -04:00
|
|
|
#include <ruby.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2005-12-17 01:32:08 -05:00
|
|
|
#include "cache/cache.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/event.h"
|
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "scripting/ruby/core.h"
|
|
|
|
#include "scripting/ruby/hooks.h"
|
|
|
|
#include "session/location.h"
|
|
|
|
#include "session/session.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* The events that will trigger the functions below and what they are expected
|
|
|
|
* to do is explained in doc/events.txt */
|
|
|
|
|
|
|
|
/* We need to catch and handle errors because, otherwise, Ruby will kill us. */
|
|
|
|
|
|
|
|
struct erb_protect_info {
|
2022-01-30 13:36:06 -05:00
|
|
|
const char *name;
|
2005-09-15 09:58:31 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *args;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
do_erb_protected_method_call(VALUE data)
|
|
|
|
{
|
|
|
|
struct erb_protect_info *info = (struct erb_protect_info *) data;
|
|
|
|
ID method_id;
|
|
|
|
|
|
|
|
assert(info);
|
|
|
|
|
|
|
|
method_id = rb_intern(info->name);
|
|
|
|
|
|
|
|
return rb_funcall3(erb_module, method_id, info->argc, info->args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2022-01-30 13:36:06 -05:00
|
|
|
erb_protected_method_call(const char *name, int argc, VALUE *args, int *error)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct erb_protect_info info = { name, argc, args };
|
|
|
|
|
|
|
|
return rb_protect(do_erb_protected_method_call, (VALUE) &info, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_goto_url(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **url = va_arg(ap, char **);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
|
|
|
int error;
|
|
|
|
VALUE args[2];
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
if (*url == NULL)
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
2006-12-04 17:45:40 -05:00
|
|
|
args[0] = rb_str_new(*url, strlen(*url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!ses || !have_location(ses)) {
|
|
|
|
args[1] = Qnil;
|
|
|
|
} else {
|
2006-12-04 17:45:40 -05:00
|
|
|
args[1] = rb_str_new(struri(cur_loc(ses)->vs.uri), strlen(struri(cur_loc(ses)->vs.uri)));
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result = erb_protected_method_call("goto_url_hook", 2, args, &error);
|
|
|
|
if (error) {
|
|
|
|
erb_report_error(ses, error);
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rb_type(result)) {
|
|
|
|
case T_STRING:
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *new_url;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-03-09 17:31:40 -05:00
|
|
|
new_url = memacpy(RSTRING_PTR(result), RSTRING_LEN(result));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (new_url) {
|
|
|
|
mem_free_set(url, new_url);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_NIL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert_ruby_error(ses, "goto_url_hook must return a string or nil");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_follow_url(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **url = va_arg(ap, char **);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
|
|
|
int error;
|
|
|
|
VALUE args[1];
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
evhook_use_params(url && ses);
|
|
|
|
|
|
|
|
if (*url == NULL)
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
2006-12-04 17:45:40 -05:00
|
|
|
args[0] = rb_str_new(*url, strlen(*url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
result = erb_protected_method_call("follow_url_hook", 1, args, &error);
|
|
|
|
if (error) {
|
|
|
|
erb_report_error(ses, error);
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rb_type(result)) {
|
|
|
|
case T_STRING:
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *new_url;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-03-09 17:31:40 -05:00
|
|
|
new_url = memacpy(RSTRING_PTR(result), RSTRING_LEN(result));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (new_url) {
|
|
|
|
mem_free_set(url, new_url);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_NIL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert_ruby_error(ses, "follow_url_hook must return a string or nil");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_pre_format_html(va_list ap, void *data)
|
|
|
|
{
|
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
2005-12-17 01:32:08 -05:00
|
|
|
struct cache_entry *cached = va_arg(ap, struct cache_entry *);
|
|
|
|
struct fragment *fragment = get_cache_fragment(cached);
|
2021-01-02 10:20:27 -05:00
|
|
|
char *url = struri(cached->uri);
|
2005-09-15 09:58:31 -04:00
|
|
|
int error;
|
|
|
|
VALUE args[2];
|
|
|
|
VALUE result;
|
|
|
|
|
2005-12-17 12:36:46 -05:00
|
|
|
evhook_use_params(ses && cached);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2005-12-17 01:32:08 -05:00
|
|
|
if (!cached->length || !*fragment->data)
|
2005-09-15 09:58:31 -04:00
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
|
|
|
args[0] = rb_str_new2(url);
|
2006-12-04 17:45:40 -05:00
|
|
|
args[1] = rb_str_new(fragment->data, fragment->length);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
result = erb_protected_method_call("pre_format_html_hook", 2, args, &error);
|
|
|
|
if (error) {
|
|
|
|
erb_report_error(ses, error);
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rb_type(result)) {
|
|
|
|
case T_STRING:
|
|
|
|
{
|
2018-03-09 17:31:40 -05:00
|
|
|
int len = RSTRING_LEN(result);
|
2005-12-17 01:32:08 -05:00
|
|
|
|
2018-03-09 17:31:40 -05:00
|
|
|
add_fragment(cached, 0, RSTRING_PTR(result), len);
|
2005-12-17 01:32:08 -05:00
|
|
|
normalize_cache_entry(cached, len);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_NIL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert_ruby_error(ses, "pre_format_html_hook must return a string or nil");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The Ruby function can return:
|
|
|
|
* - "PROXY:PORT" to use the specified proxy
|
|
|
|
* - "" to not use any proxy
|
|
|
|
* - nil to use the default proxies */
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_get_proxy(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **new_proxy_url = va_arg(ap, char **);
|
|
|
|
char *url = va_arg(ap, char *);
|
2005-09-15 09:58:31 -04:00
|
|
|
int error;
|
|
|
|
VALUE args[1];
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
if (!new_proxy_url || !url)
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
2006-12-04 17:45:40 -05:00
|
|
|
args[0] = rb_str_new(url, strlen(url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
result = erb_protected_method_call("proxy_hook", 1, args, &error);
|
|
|
|
if (error) {
|
|
|
|
erb_report_error(NULL, error);
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rb_type(result)) {
|
|
|
|
case T_STRING:
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *proxy;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-03-09 17:31:40 -05:00
|
|
|
proxy = memacpy(RSTRING_PTR(result), RSTRING_LEN(result));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (proxy) {
|
|
|
|
mem_free_set(new_proxy_url, proxy);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_NIL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert_ruby_error(NULL, "proxy_hook must return a string or nil");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_quit(va_list ap, void *data)
|
|
|
|
{
|
|
|
|
VALUE args[1];
|
|
|
|
int error;
|
|
|
|
|
|
|
|
erb_protected_method_call("quit_hook", 0, args, &error);
|
|
|
|
if (error)
|
|
|
|
erb_report_error(NULL, error);
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct event_hook_info ruby_scripting_hooks[] = {
|
2022-01-30 13:19:20 -05:00
|
|
|
{ "goto-url", 0, script_hook_goto_url, {NULL} },
|
|
|
|
{ "follow-url", 0, script_hook_follow_url, {NULL} },
|
|
|
|
{ "pre-format-html", 0, script_hook_pre_format_html, {NULL} },
|
|
|
|
{ "get-proxy", 0, script_hook_get_proxy, {NULL} },
|
|
|
|
{ "quit", 0, script_hook_quit, {NULL} },
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
NULL_EVENT_HOOK_INFO,
|
|
|
|
};
|