plugins.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. // Package plugins defines and maintains supported bot plugins.
  4. package plugins
  5. import (
  6. "log"
  7. "notabug.org/mouz/bot/irc"
  8. )
  9. // Plugin defines the interface for a single plugin.
  10. type Plugin interface {
  11. // Load initializes the module and loads any internal resources
  12. // which may be required.
  13. Load(irc.Profile) error
  14. // Unload cleans the module up and unloads any internal resources.
  15. Unload(irc.Profile) error
  16. // Dispatch sends the given, incoming IRC message to the plugin for
  17. // processing as it sees fit.
  18. Dispatch(irc.ResponseWriter, *irc.Request)
  19. }
  20. // List of registered plugins. This is to be filled during
  21. // proigram initialization and is considered read-only from then on.
  22. var plugins []Plugin
  23. // Register registers the given plugin. This is meant to be called during
  24. // program initialization, by imported plugin packages.
  25. func Register(p Plugin) { plugins = append(plugins, p) }
  26. // Load initializes all plugins.
  27. func Load(prof irc.Profile) {
  28. for _, p := range plugins {
  29. log.Printf("[plugins] Loading: %T", p)
  30. err := p.Load(prof)
  31. if err != nil {
  32. log.Printf("[%T] %v", p, err)
  33. }
  34. }
  35. }
  36. // Unload unloads all plugins.
  37. func Unload(prof irc.Profile) {
  38. for _, p := range plugins {
  39. log.Printf("[plugins] Unloading: %T", p)
  40. err := p.Unload(prof)
  41. if err != nil {
  42. log.Printf("[%T] %v", p, err)
  43. }
  44. }
  45. }
  46. // Dispatch sends the given, incoming IRC message to all plugins.
  47. func Dispatch(w irc.ResponseWriter, r *irc.Request) {
  48. for _, p := range plugins {
  49. go p.Dispatch(w, r)
  50. }
  51. }