Class: Plugin_manager

Inherits:
Object
  • Object
show all
Defined in:
lib/rirc.rb

Instance Method Summary collapse

Constructor Details

#initialize(plugin_folder) ⇒ Plugin_manager

Returns a new instance of Plugin_manager.



125
126
127
128
129
# File 'lib/rirc.rb', line 125

def initialize(plugin_folder)
  @plugins = []
  @plugin_folder = plugin_folder
  @err_path = "./.errlog"
end

Instance Method Details

#add_chan(plugin_name, channel) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/rirc.rb', line 209

def add_chan(plugin_name, channel)
  if !plugin_loaded(name)
    return "#{plugin_name} is not loaded"
  end

  get_plugin(name).add_chan(channel)

  return "#{channel} added to #{plugin_name}"
end

#add_chan_clear_any(plugin_name, channel) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/rirc.rb', line 219

def add_chan_clear_any(plugin_name, channel)
  if !plugin_loaded(name)
    return "#{plugin_name} is not loaded"
  end

  rm_chan(plugin_name, "any")
  add_chan(plugin_name, channel)

  return "#{channel} added to #{plugin_name}"
end

#check_all(message, admins, backlog) ⇒ Object

regex check function that returns responses for all plugins in an array inputs: - IRC_message object - array of admins [can be an empty array] - backlog array [can be an empty array] output: array of strings



366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/rirc.rb', line 366

def check_all(message, admins, backlog)

  if @plugins.length == 0
    return []
  end

  response = []

  # this is incredibly inneficient but it makes check_plugin flexible
  @plugins.each { |a| response.push(check_plugin(a.name, message, admins, backlog)) }

  return response
end

#check_plugin(name, message, admins, backlog) ⇒ Object

regex check function this function uses the IRC_message object for message input inputs: - name - IRC_message object - array of admins [can be an empty array] - backlog array [can be an empty array] output: string



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/rirc.rb', line 332

def check_plugin(name, message, admins, backlog) #checks an individual plugin's (by name) regex against message

    if @plugins.length == 0
    return ""
  end

  if !plugin_loaded(name)
    return ""
  else
    if message.message.match(get_plugin(name).regex) and (get_plugin(name).chans.include? "any" or get_plugin(name).chans.include? message.channel)
      begin
        return get_plugin(name).script(message, admins, backlog) # plugins use the IRC_message object
      rescue => e
        if File.exist?(@err_path)
          File.write(@err_path, "PLUGIN #{name} FAILED TO RUN", File.size(@err_path), mode: 'a')
          File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
          File.write(@err_path, e.to_s, File.size(@err_path), mode: 'a')
          File.write(@err_path, e.backtrace.to_s, File.size(@err_path), mode: 'a')
          File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
        end
        return "an error occured for plugin: #{name}"
      end
    end
  end

  return ""
end

#get_chans(name) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rirc.rb', line 187

def get_chans

  if @plugins.length == 0
    return []
  end

  names = []

  @plugins.each { |a| names.push(a.chans) }

  return names
end

#get_filesObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rirc.rb', line 174

def get_files

  if @plugins.length == 0
    return []
  end

  names = []

  @plugins.each { |a| names.push(a.file_name) }

  return names
end

#get_helpsObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rirc.rb', line 161

def get_helps

  if @plugins.length == 0
    return []
  end

  names = []

  @plugins.each { |a| names.push(a.help) }

  return names
end

#get_namesObject

search functions



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rirc.rb', line 148

def get_names

  if @plugins.length == 0
    return []
  end

  names = []

  @plugins.each { |a| names.push(a.name) }

  return names
end

#get_plugin(name) ⇒ Object

gets a plugin by name or nil if it is not loaded



253
254
255
256
257
258
259
260
261
262
# File 'lib/rirc.rb', line 253

def get_plugin(name) # gets a plugin by name or nil if it is not loaded

  if @plugins.length == 0
    return nil
  end

  @plugins.each { |a| if a.name == name then return a end }

  return nil
end

#get_regexpsObject



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rirc.rb', line 240

def get_regexps

  if @plugins.length == 0
    return []
  end

  names = []

  @plugins.each { |a| names.push(a.regex) }

  return names
end

#plugin_chans(name) ⇒ Object

gets the array of channels for a plugin



286
287
288
289
290
291
292
293
294
295
# File 'lib/rirc.rb', line 286

def plugin_chans(name) # gets the array of channels for a plugin

  if @plugins.length == 0
    return nil
  end

  @plugins.each { |a| if a.name == name then return a.chans end }

  return nil
end

#plugin_file_name(name) ⇒ Object

gets the file name for a plugin



275
276
277
278
279
280
281
282
283
284
# File 'lib/rirc.rb', line 275

def plugin_file_name(name) # gets the file name for a plugin

  if @plugins.length == 0
    return nil
  end

  @plugins.each { |a| if a.name == name then return a.file_name end }

  return nil
end

#plugin_help(name) ⇒ Object

gets the help for a plugin



264
265
266
267
268
269
270
271
272
273
# File 'lib/rirc.rb', line 264

def plugin_help(name) # gets the help for a plugin

  if @plugins.length == 0
    return nil
  end

  @plugins.each { |a| if a.name == name then return a.help end }

  return nil
end

#plugin_load(name) ⇒ Object

load



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/rirc.rb', line 381

def plugin_load(name)

  $LOAD_PATH << "#{@plugin_folder}"
  response = ""
  temp_plugin = nil
  if name.match(/.rb$/)
    begin
      load "#{name}"
      plugin_loader = Loader.new
      temp_plugin = plugin_loader.get_plugin
      if plugin_loaded(temp_plugin.name)
        temp_plugin = nil
        return "Plugin #{name} is already loaded"
      end
      @plugins.push(temp_plugin)
      temp_plugin = nil
      response = "#{name[0..-4]} loaded"
    rescue => e
      response = "cannot load plugin"
      if File.exist?(@err_path)
        File.write(@err_path, "PLUGIN #{name} FAILED TO LOAD", File.size(@err_path), mode: 'a')
        File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
        File.write(@err_path, e.to_s, File.size(@err_path), mode: 'a')
        File.write(@err_path, e.backtrace.to_s, File.size(@err_path), mode: 'a')
        File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
      end
    end
  else
    begin
      load "#{name}.rb"
      plugin_loader = Loader.new
      temp_plugin = plugin_loader.get_plugin
      if plugin_loaded(temp_plugin.name)
        temp_plugin = nil
        return "Plugin #{name} is already loaded"
      end
      @plugins.push(temp_plugin)
      temp_plugin = nil
      response = "#{name} loaded"
    rescue => e
      response = "cannot load plugin"
      if File.exist?(@err_path)
        File.write(@err_path, "PLUGIN #{name} FAILED TO LOAD", File.size(@err_path), mode: 'a')
        File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
        File.write(@err_path, e.to_s, File.size(@err_path), mode: 'a')
        File.write(@err_path, e.backtrace.to_s, File.size(@err_path), mode: 'a')
        File.write(@err_path, "======================================================", File.size(@err_path), mode: 'a')
      end
    end
  end
  $LOAD_PATH << './'
  return response
end

#plugin_loaded(name) ⇒ Object

check if a plugin is loaded



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/rirc.rb', line 309

def plugin_loaded(name)

  if @plugins.length == 0
    return false
  end

  @plugins.each do |a|
    if a.name == name
      return true
    end
  end

  return false
end

#plugin_regex(name) ⇒ Object

gets the regex for a plugin



297
298
299
300
301
302
303
304
305
306
# File 'lib/rirc.rb', line 297

def plugin_regex(name) # gets the regex for a plugin

  if @plugins.length == 0
    return nil
  end

  @plugins.each { |a| if a.name == name then return a.regex end }

  return nil
end

#pluginsObject

returns all the plugins



138
139
140
141
142
143
144
145
# File 'lib/rirc.rb', line 138

def plugins

  if @plugins.length == 0
    return []
  end

  return @plugins
end

#reload(name) ⇒ Object

reload



449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/rirc.rb', line 449

def reload(name)

  if !plugin_loaded(name)
    return "plugin is not loaded"
  end

  temp_file_name = get_plugin(name).file_name

  unload(name)
  plugin_load(temp_file_name)

  return "plugin #{name} reloaded"
end

#rm_chan(plugin_name, channel) ⇒ Object



230
231
232
233
234
235
236
237
238
# File 'lib/rirc.rb', line 230

def rm_chan(plugin_name, channel)
  if !plugin_loaded(name)
    return "#{plugin_name} is not loaded"
  end

  get_plugin(name).rm_chan(channel)

  return "#{channel} removed from #{plugin_name}"
end

#unload(name) ⇒ Object

unload



436
437
438
439
440
441
442
443
444
445
446
# File 'lib/rirc.rb', line 436

def unload(name)

  if !plugin_loaded(name)
    return "plugin is not loaded"
  end

  get_plugin(name).cleanup
  @plugins.delete_if { |a| a.name == name }

  return "plugin #{name} unloaded"
end