Beispiel #1
0
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
    struct t_plugin_script_init init;
    int ruby_error;
    char *weechat_ruby_code = {
        "$stdout = WeechatOutputs\n"
        "$stderr = WeechatOutputs\n"
        "begin"
        "  if RUBY_VERSION.split('.')[0] == '1' and RUBY_VERSION.split('.')[1] <= '8'\n"
        "    require 'rubygems'\n"
        "  else\n"
        "    require 'enc/encdb.so'\n"
        "    require 'enc/trans/transdb.so'\n"
        "\n"
        "    require 'thread'\n"
        "    class ::Mutex\n"
        "      def synchronize(*args)\n"
        "        yield\n"
        "      end\n"
        "    end\n"
        "    require 'rubygems'\n"
        "  end\n"
        "rescue LoadError\n"
        "end\n"
        "\n"
        "class Module\n"
        "\n"
        "  def load_eval_file (file)\n"
        "    lines = ''\n"
        "    begin\n"
        "      lines = File.read(file)\n"
        "    rescue => e\n"
        "      return 1\n"
        "    end\n"
        "\n"
        "    begin\n"
        "      module_eval(lines)\n"
        "    rescue Exception => e\n"
        "      @load_eval_file_error = e\n"
        "      return 2\n"
        "    end\n"
        "\n"
        "    has_init = false\n"
        "\n"
        "    instance_methods.each do |meth|\n"
        "      if meth.to_s == 'weechat_init'\n"
        "        has_init = true\n"
        "      end\n"
        "      module_eval('module_function :' + meth.to_s)\n"
        "    end\n"
        "\n"
        "    unless has_init\n"
        "      return 3\n"
        "    end\n"
        "\n"
        "    return 0\n"
        "  end\n"
        "end\n"
    };

    weechat_ruby_plugin = plugin;

    ruby_error = 0;

    /* init stdout/stderr buffer */
    ruby_buffer_output[0] = '\0';

#if (defined(RUBY_API_VERSION_MAJOR) && defined(RUBY_API_VERSION_MINOR)) && (RUBY_API_VERSION_MAJOR >= 2 || (RUBY_API_VERSION_MAJOR == 1 && RUBY_API_VERSION_MINOR >= 9))
    RUBY_INIT_STACK;
#endif

    ruby_hide_errors = 1;
    ruby_init ();
    ruby_init_loadpath ();
    ruby_script ("__weechat_plugin__");

    ruby_mWeechat = rb_define_module("Weechat");
    weechat_ruby_api_init (ruby_mWeechat);

    /* redirect stdin and stdout */
    ruby_mWeechatOutputs = rb_define_module("WeechatOutputs");
    rb_define_singleton_method(ruby_mWeechatOutputs, "write",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "puts",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "p",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "flush",
                               weechat_ruby_output_flush, 0);
    ruby_hide_errors = 0;

    rb_eval_string_protect(weechat_ruby_code, &ruby_error);
    if (ruby_error)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to eval WeeChat ruby "
                                         "internal code"),
                        weechat_prefix ("error"), RUBY_PLUGIN_NAME);
        VALUE err = rb_gv_get("$!");
        weechat_ruby_print_exception(err);
        return WEECHAT_RC_ERROR;
    }

    init.callback_command = &weechat_ruby_command_cb;
    init.callback_completion = &weechat_ruby_completion_cb;
    init.callback_hdata = &weechat_ruby_hdata_cb;
    init.callback_infolist = &weechat_ruby_infolist_cb;
    init.callback_signal_debug_dump = &weechat_ruby_signal_debug_dump_cb;
    init.callback_signal_debug_libs = &weechat_ruby_signal_debug_libs_cb;
    init.callback_signal_buffer_closed = &weechat_ruby_signal_buffer_closed_cb;
    init.callback_signal_script_action = &weechat_ruby_signal_script_action_cb;
    init.callback_load_file = &weechat_ruby_load_cb;

    ruby_quiet = 1;
    plugin_script_init (weechat_ruby_plugin, argc, argv, &init);
    ruby_quiet = 0;

    plugin_script_display_short_list (weechat_ruby_plugin,
                                      ruby_scripts);

    /* init OK */
    return WEECHAT_RC_OK;
}
Beispiel #2
0
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
    int ruby_error;
    char *weechat_ruby_code =
        {
            "$stdout = WeechatOutputs\n"
            "$stderr = WeechatOutputs\n"
            "\n"
            "class Module\n"
            "  @load_eval_file_error = ''\n"
            "\n"
            "  def load_eval_file (file)\n"
            "    lines = ''\n"
	    "    begin\n"
	    "      f = File.open(file, 'r')\n"
	    "      lines = f.readlines.join\n"
	    "    rescue => e\n"
	    "      @load_eval_file_error = e\n"
	    "      return 1\n"
	    "    end\n"
	    "\n"	   
	    "    begin\n"
	    "      module_eval(lines)\n"
	    "    rescue => e\n"
	    "      @load_eval_file_error = e\n"
	    "      return 2\n"
	    "    end\n"
	    "\n"
	    "    has_init = false\n"
	    "\n"
	    "    instance_methods.each do |meth|\n"
	    "      if meth == 'weechat_init'\n"
	    "        has_init = true\n"
	    "      end\n"
	    "      module_eval('module_function :' + meth)\n"
	    "    end\n"
	    "\n"
	    "    unless has_init\n"
	    "      return 3\n"
	    "    end\n"
	    "\n"
	    "    return 0\n"
	    "  end\n"
	    "end\n"
	};
    
    weechat_ruby_plugin = plugin;
    
    ruby_error = 0;
    
    /* init stdout/stderr buffer */
    ruby_buffer_output[0] = '\0';
    
    ruby_init ();
    ruby_init_loadpath ();
    ruby_script ("__weechat_plugin__");
    
    ruby_mWeechat = rb_define_module("Weechat");
    weechat_ruby_api_init (ruby_mWeechat);
    
    /* redirect stdin and stdout */
    ruby_mWeechatOutputs = rb_define_module("WeechatOutputs");
    rb_define_singleton_method(ruby_mWeechatOutputs, "write",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "puts",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "p",
                               weechat_ruby_output, 1);
    rb_define_singleton_method(ruby_mWeechatOutputs, "flush",
                               weechat_ruby_output_flush, 0);

    rb_eval_string_protect(weechat_ruby_code, &ruby_error);
    if (ruby_error)
    {
	VALUE ruby_error_info = rb_inspect(ruby_errinfo);
	weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to eval WeeChat ruby "
                                         "internal code"),
                        weechat_prefix ("error"), RUBY_PLUGIN_NAME);
	weechat_printf (NULL,
                        weechat_gettext ("%s%s: error: %s"),
                        weechat_prefix ("error"), RUBY_PLUGIN_NAME,
                        STR2CSTR(ruby_error_info));
	return WEECHAT_RC_ERROR;
    }
    
    ruby_quiet = 1;
    script_init (weechat_ruby_plugin,
                 argc,
                 argv,
                 &ruby_scripts,
                 &weechat_ruby_command_cb,
                 &weechat_ruby_completion_cb,
                 &weechat_ruby_infolist_cb,
                 &weechat_ruby_signal_debug_dump_cb,
                 &weechat_ruby_signal_buffer_closed_cb,
                 &weechat_ruby_signal_script_action_cb,
                 &weechat_ruby_load_cb,
                 &weechat_ruby_api_buffer_input_data_cb,
                 &weechat_ruby_api_buffer_close_cb);
    ruby_quiet = 0;
    
    script_display_short_list (weechat_ruby_plugin,
                               ruby_scripts);
    
    /* init ok */
    return WEECHAT_RC_OK;
}