summaryrefslogtreecommitdiff
path: root/webircgateway/pkg/irc/isupport.go
blob: fdb7beea8d9dd407d40c8bbd22cd913b4b62f2dc (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
package irc

import (
	"strings"
	"sync"
)

type ISupport struct {
	Received    bool
	Injected    bool
	Tags        map[string]string
	tokens      map[string]string
	tokensMutex sync.RWMutex
}

func (m *ISupport) ClearTokens() {
	m.tokensMutex.Lock()
	m.tokens = make(map[string]string)
	m.tokensMutex.Unlock()
}

func (m *ISupport) AddToken(tokenPair string) {
	m.tokensMutex.Lock()
	m.addToken(tokenPair)
	m.tokensMutex.Unlock()
}

func (m *ISupport) AddTokens(tokenPairs []string) {
	m.tokensMutex.Lock()
	for _, tp := range tokenPairs {
		m.addToken(tp)
	}
	m.tokensMutex.Unlock()
}

func (m *ISupport) HasToken(key string) (ok bool) {
	m.tokensMutex.RLock()
	_, ok = m.tokens[strings.ToUpper(key)]
	m.tokensMutex.RUnlock()
	return
}

func (m *ISupport) GetToken(key string) (val string) {
	m.tokensMutex.RLock()
	val = m.tokens[strings.ToUpper(key)]
	m.tokensMutex.RUnlock()
	return
}

func (m *ISupport) addToken(tokenPair string) {
	kv := strings.Split(tokenPair, "=")
	if len(kv) == 1 {
		kv = append(kv, "")
	}
	m.tokens[strings.ToUpper(kv[0])] = kv[1]
}