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.

116 lines
2.5 KiB

6 years ago
6 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. }
  25. // NewEvent makes a new event with Kind, Verb, Time set and Args and Tags initialized.
  26. func NewEvent(kind, verb string) Event {
  27. return Event{
  28. kind: kind,
  29. verb: verb,
  30. name: kind + "." + verb,
  31. Time: time.Now(),
  32. Args: make([]string, 0, 4),
  33. Tags: make(map[string]string),
  34. }
  35. }
  36. // Kind gets the event's kind
  37. func (event *Event) Kind() string {
  38. return event.kind
  39. }
  40. // Verb gets the event's verb
  41. func (event *Event) Verb() string {
  42. return event.verb
  43. }
  44. // Name gets the event name, which is Kind and Verb separated by a dot.
  45. func (event *Event) Name() string {
  46. return event.name
  47. }
  48. // IsEither returns true if the event has the kind and one of the verbs.
  49. func (event *Event) IsEither(kind string, verbs ...string) bool {
  50. if event.kind != kind {
  51. return false
  52. }
  53. for i := range verbs {
  54. if event.verb == verbs[i] {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // Context gets the event's context if it's part of the loop, or `context.Background` otherwise. client.Emit
  61. // will set this context on its copy and return it.
  62. func (event *Event) Context() context.Context {
  63. if event.ctx == nil {
  64. return context.Background()
  65. }
  66. return event.ctx
  67. }
  68. // Kill stops propagation of the event. The context will be killed once
  69. // the current event handler returns.
  70. func (event *Event) Kill() {
  71. event.killed = true
  72. }
  73. // Killed returns true if Kill has been called.
  74. func (event *Event) Killed() bool {
  75. return event.killed
  76. }
  77. // Hide will not stop propagation, but it will allow output handlers to know not to
  78. // render it.
  79. func (event *Event) Hide() {
  80. event.hidden = true
  81. }
  82. // Hidden returns true if Hide has been called.
  83. func (event *Event) Hidden() bool {
  84. return event.hidden
  85. }
  86. // MarshalJSON makes a JSON object from the event.
  87. func (event *Event) MarshalJSON() ([]byte, error) {
  88. return json.Marshal(map[string]interface{}{
  89. "kind": event.kind,
  90. "verb": event.verb,
  91. "text": event.Text,
  92. "args": event.Args,
  93. "tags": event.Tags,
  94. "killed": event.killed,
  95. "hidden": event.hidden,
  96. })
  97. }