summaryrefslogtreecommitdiff
path: root/deprecated-webircgateway/pkg/webircgateway/hooks.go
blob: 1bfd56462f2d1b31d1e299bb5dd31c16c20c57e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package webircgateway

import "github.com/kiwiirc/webircgateway/pkg/irc"

var hooksRegistered map[string][]interface{}

func init() {
	hooksRegistered = make(map[string][]interface{})
}

func HookRegister(hookName string, p interface{}) {
	_, exists := hooksRegistered[hookName]
	if !exists {
		hooksRegistered[hookName] = make([]interface{}, 0)
	}

	hooksRegistered[hookName] = append(hooksRegistered[hookName], p)
}

type Hook struct {
	ID   string
	Halt bool
}

func (h *Hook) getCallbacks(eventType string) []interface{} {
	var f []interface{}
	f = make([]interface{}, 0)

	callbacks, exists := hooksRegistered[eventType]
	if exists {
		f = callbacks
	}

	return f
}

/**
 * HookIrcConnectionPre
 * Dispatched just before an IRC connection is attempted
 * Types: irc.connection.pre
 */
type HookIrcConnectionPre struct {
	Hook
	Client         *Client
	UpstreamConfig *ConfigUpstream
}

func (h *HookIrcConnectionPre) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookIrcConnectionPre)); ok {
			f(h)
		}
	}
}

/**
 * HookIrcLine
 * Dispatched when either:
 *   * A line arrives from the IRCd, before sending to the client
 *   * A line arrives from the client, before sending to the IRCd
 * Types: irc.line
 */
type HookIrcLine struct {
	Hook
	Client         *Client
	UpstreamConfig *ConfigUpstream
	Line           string
	Message        *irc.Message
	ToServer       bool
}

func (h *HookIrcLine) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookIrcLine)); ok {
			f(h)
		}
	}
}

/**
 * HookClientState
 * Dispatched after a client connects or disconnects
 * Types: client.state
 */
type HookClientState struct {
	Hook
	Client    *Client
	Connected bool
}

func (h *HookClientState) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookClientState)); ok {
			f(h)
		}
	}
}

/**
 * HookClientInit
 * Dispatched directly after a new Client instance has been created
 * Types: client.init
 */
type HookClientInit struct {
	Hook
	Client    *Client
	Connected bool
}

func (h *HookClientInit) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookClientInit)); ok {
			f(h)
		}
	}
}

/**
 * HookStatus
 * Dispatched for each line output of the _status HTTP request
 * Types: status.client
 */
type HookStatus struct {
	Hook
	Client *Client
	Line   string
}

func (h *HookStatus) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookStatus)); ok {
			f(h)
		}
	}
}

/**
 * HookGatewayClosing
 * Dispatched when the gateway has been told to shutdown
 * Types: gateway.closing
 */
type HookGatewayClosing struct {
	Hook
}

func (h *HookGatewayClosing) Dispatch(eventType string) {
	for _, p := range h.getCallbacks(eventType) {
		if f, ok := p.(func(*HookGatewayClosing)); ok {
			f(h)
		}
	}
}