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

import (
	"strings"
	"sync"
	"time"
)

type State struct {
	LocalPort  int
	RemotePort int
	Username   string
	Nick       string
	RealName   string
	Password   string
	Account    string
	Modes      map[string]string

	channelsMutex sync.Mutex
	Channels      map[string]*StateChannel
	ISupport      *ISupport
}

type StateChannel struct {
	Name   string
	Modes  map[string]string
	Joined time.Time
}

func NewState() *State {
	return &State{
		Channels: make(map[string]*StateChannel),
		ISupport: &ISupport{
			tokens: make(map[string]string),
		},
	}
}

func NewStateChannel(name string) *StateChannel {
	return &StateChannel{
		Name:   name,
		Modes:  make(map[string]string),
		Joined: time.Now(),
	}
}

func (m *State) HasChannel(name string) (ok bool) {
	m.channelsMutex.Lock()
	_, ok = m.Channels[strings.ToLower(name)]
	m.channelsMutex.Unlock()
	return
}

func (m *State) GetChannel(name string) (channel *StateChannel) {
	m.channelsMutex.Lock()
	channel = m.Channels[strings.ToLower(name)]
	m.channelsMutex.Unlock()
	return
}

func (m *State) SetChannel(channel *StateChannel) {
	m.channelsMutex.Lock()
	m.Channels[strings.ToLower(channel.Name)] = channel
	m.channelsMutex.Unlock()
}

func (m *State) RemoveChannel(name string) {
	m.channelsMutex.Lock()
	delete(m.Channels, strings.ToLower(name))
	m.channelsMutex.Unlock()
}

func (m *State) ClearChannels() {
	m.channelsMutex.Lock()
	for i := range m.Channels {
		delete(m.Channels, i)
	}
	m.channelsMutex.Unlock()
}