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.

87 lines
2.4 KiB

  1. package handlers
  2. import (
  3. "strings"
  4. "git.aiterp.net/gisle/irc"
  5. "git.aiterp.net/gisle/irc/ircutil"
  6. )
  7. // MRoleplay is a handler that adds commands for cutting NPC commands, as well as cleaning up
  8. // the input from the server. It's named after Charybdis IRCd's m_roleplay module.
  9. func MRoleplay(event *irc.Event, client *irc.Client) {
  10. switch event.Name() {
  11. case "packet.privmsg", "ctcp.action":
  12. {
  13. // Detect m_roleplay
  14. if strings.HasPrefix(event.Nick, "\x1F") {
  15. event.Nick = event.Nick[1 : len(event.Nick)-2]
  16. if event.Verb() == "PRIVMSG" {
  17. event.RenderTags["mRoleplay"] = "npc"
  18. } else {
  19. event.RenderTags["mRoleplay"] = "npca"
  20. }
  21. } else if strings.HasPrefix(event.Nick, "=") {
  22. event.RenderTags["mRoleplay"] = "scene"
  23. } else {
  24. break
  25. }
  26. lastSpace := strings.LastIndex(event.Text, " ")
  27. lastParanthesis := strings.LastIndex(event.Text, "(")
  28. if lastParanthesis != -1 && lastSpace != -1 && lastParanthesis == lastSpace+1 {
  29. event.Text = event.Text[:lastSpace]
  30. }
  31. }
  32. case "input.npcc", "input.npcac":
  33. {
  34. isAction := event.Verb() == "npcac"
  35. nick, text := ircutil.ParseArgAndText(event.Text)
  36. if nick == "" || text == "" {
  37. client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /"+event.Verb()+" <nick> <text...>"))
  38. break
  39. }
  40. channel := event.ChannelTarget()
  41. if channel == nil {
  42. client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel"))
  43. break
  44. }
  45. overhead := ircutil.MessageOverhead("\x1f"+nick+"\x1f", client.Nick(), "npc.fakeuser.invalid", channel.Name(), isAction)
  46. cuts := ircutil.CutMessage(text, overhead)
  47. for _, cut := range cuts {
  48. npcCommand := "NPCA"
  49. if event.Verb() == "npcc" {
  50. npcCommand = "NPC"
  51. }
  52. client.SendQueuedf("%s %s %s :%s", npcCommand, channel.Name(), nick, cut)
  53. }
  54. event.PreventDefault()
  55. }
  56. case "input.scenec":
  57. {
  58. if event.Text == "" {
  59. client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /scenec <text...>"))
  60. break
  61. }
  62. channel := event.ChannelTarget()
  63. if channel == nil {
  64. client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel"))
  65. break
  66. }
  67. overhead := ircutil.MessageOverhead("=Scene=", client.Nick(), "npc.fakeuser.invalid", channel.Name(), false)
  68. cuts := ircutil.CutMessage(event.Text, overhead)
  69. for _, cut := range cuts {
  70. client.SendQueuedf("SCENE %s :%s", channel.Name(), cut)
  71. }
  72. event.PreventDefault()
  73. }
  74. }
  75. }