You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dispatcher.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dispatcher
  2. import (
  3. "github.com/juju/loggo"
  4. "github.com/spf13/viper"
  5. "github.com/thoj/go-ircevent"
  6. "strings"
  7. )
  8. // Take a string, parse out the recipients, and send to IRC.
  9. //
  10. // eg:
  11. // hello world [goes to default channel]
  12. // #test hello world [goes to #test, if joined]
  13. // #test,@alice hello world [goes to #test and alice]
  14. // #* hello world [goes to all channels bot is in]
  15. func Send(irc *irc.Connection, msg string, log loggo.Logger, origin string) {
  16. channels := viper.GetStringSlice("irc.channels")
  17. if msg[0] == '#' || msg[0] == '@' {
  18. parts := strings.SplitN(msg, " ", 2)
  19. if parts[0] == "#*" {
  20. for _, channel := range channels {
  21. irc.Privmsg(channel, replaceFormatting(parts[1]))
  22. }
  23. } else {
  24. targets := strings.Split(parts[0], ",")
  25. for _, target := range targets {
  26. if target[0] == '@' {
  27. target = target[1:]
  28. }
  29. irc.Privmsg(target, replaceFormatting(parts[1]))
  30. }
  31. }
  32. log.Infof("from[%s] send[%s] %s", origin, parts[0], parts[1])
  33. } else if len(msg) > 7 && msg[0:6] == "%TOPIC" {
  34. parts := strings.SplitN(msg, " ", 3)
  35. irc.SendRawf("TOPIC %s :%s", parts[1], replaceFormatting(parts[2]))
  36. log.Infof("from[%s] topic[%s] %s", origin, parts[1], parts[2])
  37. } else {
  38. if len(channels) > 0 {
  39. irc.Privmsg(channels[0], replaceFormatting(msg))
  40. log.Infof("from[%s] send_default[%s] %s", origin, channels[0], msg)
  41. }
  42. }
  43. }