aboutsummaryrefslogtreecommitdiff
path: root/plugin_modchan.go
blob: 1b661cdc8d98cc027fe637d32fa5baa863d3d404 (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package proxy

import (
	"sync"

	"github.com/HimbeerserverDE/mt"
)

var (
	modChanSubscribers  map[string][]*ClientConn
	modChanSubscriberMu sync.RWMutex
)

var (
	onCltModChanMsg     []func(string, *ClientConn, string) bool
	onCltModChanMsgMu   sync.RWMutex
	onCltModChanMsgOnce sync.Once
)

// SendModChanMsg sends a message to all subscribed clients on a modchannel.
var (
	onSrvModChanMsg     []func(*ClientConn, string, string, string) bool
	onSrvModChanMsgMu   sync.RWMutex
	onSrvModChanMsgOnce sync.Once
)

func SendModChanMsg(channel, msg string) {
	modChanSubscriberMu.RLock()
	defer modChanSubscriberMu.RUnlock()

	subs, _ := modChanSubscribers[channel]
	for _, cc := range subs {
		cc.SendCmd(&mt.ToCltModChanMsg{
			Channel: channel,
			Msg:     msg,
		})
	}
}

// JoinModChan attempts to subscribe to a modchannel, returning a channel
// that yields a boolean indicating success.
// The proxy will try to rejoin the channel after switching servers,
// but this is not guaranteed to succeed and errors will not be reported
// to the caller.
// This condition can be checked against using the IsModChanJoined method.
// This method may block indefinitely if the client switches servers
// before a response is received. If this cannot be controlled,
// using a select statement with a timeout is recommended.
func (cc *ClientConn) JoinModChan(channel string) <-chan bool {
	failCh := make(chan bool)
	failCh <- false

	sc := cc.server()
	if sc == nil {
		return failCh
	}

	successCh := make(chan bool)

	sc.modChanJoinChMu.Lock()
	defer sc.modChanJoinChMu.Unlock()

	if sc.modChanJoinChs[channel] == nil {
		sc.modChanJoinChs[channel] = make(map[chan bool]struct{})
	}
	sc.modChanJoinChs[channel][successCh] = struct{}{}

	sc.SendCmd(&mt.ToSrvJoinModChan{Channel: channel})
	return successCh
}

// LeaveModChan attempts to unscribe from a modchannel, returning a channel
// yielding a boolean indicating success.
func (cc *ClientConn) LeaveModChan(channel string) <-chan bool {
	failCh := make(chan bool)
	failCh <- false

	sc := cc.server()
	if sc == nil {
		return failCh
	}

	successCh := make(chan bool)

	sc.modChanLeaveChMu.Lock()
	defer sc.modChanLeaveChMu.Unlock()

	if sc.modChanLeaveChs[channel] == nil {
		sc.modChanLeaveChs[channel] = make(map[chan bool]struct{})
	}
	sc.modChanLeaveChs[channel][successCh] = struct{}{}

	sc.SendCmd(&mt.ToSrvLeaveModChan{Channel: channel})
	return successCh
}

// IsModChanJoined returns whether this client is currently subscribed
// to the specified modchannel. This is mainly useful for tracking success
// across server hops.
func (cc *ClientConn) IsModChanJoined(channel string) bool {
	cc.modChsMu.RLock()
	defer cc.modChsMu.RUnlock()

	_, ok := cc.modChs[channel]
	return ok
}

// SendModChanMsg sends a message to the current upstream server
// and all subscribed clients connected to it on a modchannel.
func (cc *ClientConn) SendModChanMsg(channel, msg string) bool {
	sc := cc.server()
	if sc == nil {
		return false
	}

	sc.SendCmd(&mt.ToSrvMsgModChan{
		Channel: channel,
		Msg:     msg,
	})

	return true
}

// RegisterOnCltModChanMsg registers a handler that is called
// when a client sends a message on a modchannel.
// If any handler returns true, the message is not forwarded
// to the upstream server.
func RegisterOnCltModChanMsg(handler func(string, *ClientConn, string) bool) {
	onCltModChanMsgMu.Lock()
	defer onCltModChanMsgMu.Unlock()

	onCltModChanMsg = append(onCltModChanMsg, handler)
}

// RegisterOnSrvModChanMsg registers a handler that is called
// when another client of the current upstream server or a server mod
// sends a message on a modchannel.
// If any handler returns true, the message is not forwarded to the client.
func RegisterOnSrvModChanMsg(handler func(*ClientConn, string, string, string) bool) {
	onSrvModChanMsgMu.Lock()
	defer onSrvModChanMsgMu.Unlock()

	onSrvModChanMsg = append(onSrvModChanMsg, handler)
}

func cltLeaveModChan(cc *ClientConn, channel string) {
	modChanSubscriberMu.Lock()
	defer modChanSubscriberMu.Unlock()

	for i, sub := range modChanSubscribers[channel] {
		if sub == cc {
			modChanSubscribers[channel] = append(modChanSubscribers[channel][:i], modChanSubscribers[channel][i+1:]...)
			break
		}
	}
}

func cltLeaveModChans(cc *ClientConn) {
	modChanSubscriberMu.Lock()
	defer modChanSubscriberMu.Unlock()

	for ch := range modChanSubscribers {
		for i, sub := range modChanSubscribers[ch] {
			if sub == cc {
				modChanSubscribers[ch] = append(modChanSubscribers[ch][:i], modChanSubscribers[ch][i+1:]...)
				break
			}
		}
	}
}

func handleCltModChanMsg(cc *ClientConn, cmd *mt.ToSrvMsgModChan) bool {
	onCltModChanMsgMu.RLock()
	defer onCltModChanMsgMu.RUnlock()

	subs, _ := modChanSubscribers[cmd.Channel]
	for _, sub := range subs {
		sub.SendCmd(&mt.ToCltModChanMsg{
			Channel: cmd.Channel,
			Sender:  cc.Name(),
			Msg:     cmd.Msg,
		})
	}

	drop := false
	for _, handler := range onCltModChanMsg {
		if handler(cmd.Channel, cc, cmd.Msg) {
			drop = true
		}
	}

	return drop
}

func handleSrvModChanMsg(cc *ClientConn, cmd *mt.ToCltModChanMsg) bool {
	onSrvModChanMsgMu.RLock()
	defer onSrvModChanMsgMu.RUnlock()

	drop := false
	for _, handler := range onSrvModChanMsg {
		if handler(cc, cmd.Channel, cmd.Sender, cmd.Msg) {
			drop = true
		}
	}

	return drop
}

func init() {
	modChanSubscribers = make(map[string][]*ClientConn)
}