Class: Admin::Plugins

Inherits:
Object
  • Object
show all
Includes:
Cinch::Helpers, Cinch::Plugin
Defined in:
lib/Zeta/admin/plugins.rb

Instance Method Summary collapse

Methods included from Cinch::Plugin

#check?, #log2chan

Instance Method Details

#load_plugin(m, plugin, mapping) ⇒ Object

Methods



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
# File 'lib/Zeta/admin/plugins.rb', line 21

def load_plugin(m, plugin, mapping)
  mapping ||= plugin.gsub(/(.)([A-Z])/) { |_|
    $1 + "_" + $2
  }.downcase # we downcase here to also catch the first letter

  #file_name = "lib/plugins/#{mapping}.rb"
  stdlib_plugin = File.join('Zeta', 'plugins', p.to_s)
  custom_plugin = File.join(Dir.home, '.zeta', 'plugins', p.to_s, "#{p.to_s}.rb")

  unless File.exist?(custom_plugin)
    m.reply "Could not load #{plugin} because #{file_name} does not exist."
    return
  end

  begin
    load(custom_plugin)
  rescue
    m.reply "Could not load #{plugin}."
    raise
  end

  begin
    const = Plugins.const_get(plugin)
  rescue NameError
    m.reply "Could not load #{plugin} because no matching class was found."
    return
  end

  @bot.plugins.register_plugin(const)
  m.reply "Successfully loaded #{plugin}"
end

#reload_plugin(m, plugin, mapping) ⇒ Object



89
90
91
92
# File 'lib/Zeta/admin/plugins.rb', line 89

def reload_plugin(m, plugin, mapping)
  unload_plugin(m, plugin)
  load_plugin(m, plugin, mapping)
end

#set_option(m, plugin, option, value) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/Zeta/admin/plugins.rb', line 94

def set_option(m, plugin, option, value)
  begin
    const = Plugins.const_get(plugin)
  rescue NameError
    m.reply "Could not set plugin option for #{plugin} because no matching class was found."
    return
  end
  @bot.config.plugins.options[const][option.to_sym] = eval(value)
end

#unload_plugin(m, plugin) ⇒ Object



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
# File 'lib/Zeta/admin/plugins.rb', line 53

def unload_plugin(m, plugin)
  begin
    plugin_class = Plugins.const_get(plugin)
  rescue NameError
    m.reply "Could not unload #{plugin} because no matching class was found."
    return
  end

  @bot.plugins.select { |p| p.class == plugin_class }.each do |p|
    @bot.plugins.unregister_plugin(p)
  end

  ## FIXME not doing this at the moment because it'll break
  ## plugin options. This means, however, that reloading a
  ## plugin is relatively dirty: old methods will not be removed
  ## but only overwritten by new ones. You will also not be able
  ## to change a classes superclass this way.
  # Cinch::Plugins.__send__(:remove_const, plugin)

  # Because we're not completely removing the plugin class,
  # reset everything to the starting values.
  plugin_class.hooks.clear
  plugin_class.matchers.clear
  plugin_class.listeners.clear
  plugin_class.timers.clear
  plugin_class.ctcps.clear
  plugin_class.react_on = :message
  plugin_class.plugin_name = nil
  plugin_class.help = nil
  plugin_class.prefix = nil
  plugin_class.suffix = nil
  plugin_class.required_options.clear

  m.reply "Successfully unloaded #{plugin}"
end