plugin.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. // Package url defines a plugin, which finds and extracts URLs from
  4. // incoming chat data. It performs a HTTP lookup to the found URL and
  5. // attempts to determine the page title of the link. This title is then
  6. // returned to the channel from which the message came.
  7. package url
  8. import (
  9. "notabug.org/mouz/bot/app/util"
  10. "notabug.org/mouz/bot/irc"
  11. "notabug.org/mouz/bot/plugins"
  12. )
  13. func init() { plugins.Register(&plugin{}) }
  14. type plugin struct {
  15. data struct {
  16. YoutubeAPIKey string
  17. }
  18. }
  19. // Load initializes the module and loads any internal resources
  20. // which may be required.
  21. func (p *plugin) Load(prof irc.Profile) error {
  22. return util.ReadFile("url.cfg", &p.data, false)
  23. }
  24. // Unload cleans the module up and unloads any internal resources.
  25. func (p *plugin) Unload(prof irc.Profile) error {
  26. p.data.YoutubeAPIKey = ""
  27. return nil
  28. }
  29. // Dispatch sends the given, incoming IRC message to the plugin for
  30. // processing as it sees fit.
  31. func (p *plugin) Dispatch(w irc.ResponseWriter, r *irc.Request) {
  32. if !r.IsPrivMsg() {
  33. return
  34. }
  35. // Find all URLs in the message body.
  36. list := regURL.FindAllString(r.Data, -1)
  37. if len(list) == 0 {
  38. return
  39. }
  40. // Fetch title data for each of them.
  41. for _, url := range list {
  42. go fetchDescription(w, r, url, p.data.YoutubeAPIKey)
  43. }
  44. }