Mirror of github.com/gissleh/irc
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.

62 lines
1.7 KiB

  1. package handlers
  2. import (
  3. "github.com/gissleh/irc"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // CTCP implements the widely used CTCP commands (CLIENTINFO, VERSION, TIME, and PING), as well as the /ping command.
  9. // It does not implement DCC.
  10. //
  11. // For every other CTCP command supported, you should expand the `ctcp.clientinfo.reply` client value like above.
  12. func CTCP(event *irc.Event, client *irc.Client) {
  13. switch event.Name() {
  14. case "client.create":
  15. if r, ok := client.Value("ctcp.clientinfo.reply").(string); ok {
  16. if !strings.Contains(r, "ACTION PING TIME VERSION") {
  17. client.SetValue("ctcp.clientinfo.reply", r+" ACTION PING TIME VERSION")
  18. }
  19. } else {
  20. client.SetValue("ctcp.clientinfo.reply", "ACTION PING TIME VERSION")
  21. }
  22. case "ctcp.clientinfo":
  23. {
  24. response, ok := client.Value("ctcp.clientinfo.reply").(string)
  25. if !ok {
  26. response = "ACTION PING TIME VERSION"
  27. }
  28. client.SendCTCP("CLIENTINFO", event.Nick, true, response)
  29. }
  30. case "ctcp.version":
  31. {
  32. version := "github.com/gissleh/irc v1.0"
  33. if v, ok := client.Value("ctcp.version.reply").(string); ok {
  34. version = v
  35. }
  36. client.SendCTCP("VERSION", event.Nick, true, version)
  37. }
  38. case "ctcp.time":
  39. {
  40. client.SendCTCP("TIME", event.Nick, true, time.Now().Local().Format(time.RFC1123))
  41. }
  42. case "ctcp.ping":
  43. {
  44. client.SendCTCP("PING", event.Nick, true, event.Text)
  45. }
  46. case "input.ping":
  47. {
  48. args := strings.SplitN(event.Text, " ", 2)
  49. targetName := args[0]
  50. if targetName == "" {
  51. client.EmitNonBlocking(irc.NewErrorEvent("ctcp.pingarg", "/ping needs an argument"))
  52. break
  53. }
  54. client.SendCTCP("PING", targetName, false, strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
  55. }
  56. }
  57. }