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.

132 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package irc
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. )
  7. // An Event is any thing that passes through the irc client's event loop. It's not thread safe, because it's processed
  8. // in sequence and should not be used off the goroutine that processed it.
  9. type Event struct {
  10. kind string
  11. verb string
  12. name string
  13. Time time.Time
  14. Nick string
  15. User string
  16. Host string
  17. Args []string
  18. Text string
  19. Tags map[string]string
  20. ctx context.Context
  21. cancel context.CancelFunc
  22. killed bool
  23. hidden bool
  24. targets []Target
  25. }
  26. // NewEvent makes a new event with Kind, Verb, Time set and Args and Tags initialized.
  27. func NewEvent(kind, verb string) Event {
  28. return Event{
  29. kind: kind,
  30. verb: verb,
  31. name: kind + "." + verb,
  32. Time: time.Now(),
  33. Args: make([]string, 0, 4),
  34. Tags: make(map[string]string),
  35. }
  36. }
  37. // Kind gets the event's kind
  38. func (event *Event) Kind() string {
  39. return event.kind
  40. }
  41. // Verb gets the event's verb
  42. func (event *Event) Verb() string {
  43. return event.verb
  44. }
  45. // Name gets the event name, which is Kind and Verb separated by a dot.
  46. func (event *Event) Name() string {
  47. return event.name
  48. }
  49. // IsEither returns true if the event has the kind and one of the verbs.
  50. func (event *Event) IsEither(kind string, verbs ...string) bool {
  51. if event.kind != kind {
  52. return false
  53. }
  54. for i := range verbs {
  55. if event.verb == verbs[i] {
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. // Context gets the event's context if it's part of the loop, or `context.Background` otherwise. client.Emit
  62. // will set this context on its copy and return it.
  63. func (event *Event) Context() context.Context {
  64. if event.ctx == nil {
  65. return context.Background()
  66. }
  67. return event.ctx
  68. }
  69. // Kill stops propagation of the event. The context will be killed once
  70. // the current event handler returns.
  71. func (event *Event) Kill() {
  72. event.killed = true
  73. }
  74. // Killed returns true if Kill has been called.
  75. func (event *Event) Killed() bool {
  76. return event.killed
  77. }
  78. // Hide will not stop propagation, but it will allow output handlers to know not to
  79. // render it.
  80. func (event *Event) Hide() {
  81. event.hidden = true
  82. }
  83. // Hidden returns true if Hide has been called.
  84. func (event *Event) Hidden() bool {
  85. return event.hidden
  86. }
  87. // Arg gets the argument by index. The rationale behind it is that some
  88. // servers may use it for the last argument in JOINs and such.
  89. func (event *Event) Arg(index int) string {
  90. if index < 0 || index > len(event.Args) {
  91. return ""
  92. }
  93. if index == len(event.Args) {
  94. return event.Text
  95. }
  96. return event.Args[index]
  97. }
  98. // MarshalJSON makes a JSON object from the event.
  99. func (event *Event) MarshalJSON() ([]byte, error) {
  100. return json.Marshal(map[string]interface{}{
  101. "kind": event.kind,
  102. "verb": event.verb,
  103. "text": event.Text,
  104. "args": event.Args,
  105. "tags": event.Tags,
  106. "killed": event.killed,
  107. "hidden": event.hidden,
  108. })
  109. }