-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
122 lines (109 loc) · 2.65 KB
/
state.go
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
package main
type userState struct {
nick string
// other stuff like OPER...
}
const (
CONNECTION_EMPTY = iota
DISCONNECTED
CONNECTING
CONNECTION_ERROR
CONNECTION_START
CONNECTED
)
type serverState struct {
connState int
lastError error
hostname string
port int
ssl bool
networkName string
user *userState
channels map[string]*channelState
privmsgs map[string]*privmsgState
tab *tabServer
channelList *tabChannelList
}
func (servState *serverState) AllTabs() []tabWithTextBuffer {
contexts := tabMan.FindAll(allServerTabsFinder(servState))
ret := []tabWithTextBuffer{}
for _, ctx := range contexts {
if t, ok := ctx.tab.(tabWithTextBuffer); ok {
ret = append(ret, t)
}
}
return ret
}
func (servState *serverState) CurrentTab() tabWithTextBuffer {
ctx := tabMan.Find(currentServerTabFinder(servState))
return ctx.tab.(tabWithTextBuffer)
}
type channelState struct {
channel string
topic string
nickList *nickList
tab *tabChannel
}
type privmsgState struct {
nick string
tab *tabPrivmsg
}
func ensureChanState(servConn *serverConnection, servState *serverState, channel string) *channelState {
chanState, ok := servState.channels[channel]
if !ok {
chanState = &channelState{
channel: channel,
nickList: newNickList(),
}
// TODO(tso): make a finderFunc instead
index := servState.tab.Index()
if servState.channelList != nil {
index = servState.channelList.Index()
}
for _, ch := range servState.channels {
i := ch.tab.Index()
if i > index {
index = i
}
}
index++
servState.channels[channel] = chanState
ctx := tabMan.Create(&tabContext{servConn: servConn, servState: servState, chanState: chanState}, index)
tab := newChannelTab(servConn, servState, chanState, index)
ctx.tab = tab
chanState.tab = tab
}
return chanState
}
func ensurePmState(servConn *serverConnection, servState *serverState, nick string) *privmsgState {
pmState, ok := servState.privmsgs[nick]
if !ok {
pmState = &privmsgState{
nick: nick,
}
// TODO(tso): make a finderFunc instead
index := servState.tab.Index()
if servState.channelList != nil {
index = servState.channelList.Index()
}
for _, ch := range servState.channels {
i := ch.tab.Index()
if i > index {
index = i
}
}
for _, pm := range servState.privmsgs {
i := pm.tab.Index()
if i > index {
index = i
}
}
index++
servState.privmsgs[nick] = pmState
ctx := tabMan.Create(&tabContext{servConn: servConn, servState: servState, pmState: pmState}, index)
tab := newPrivmsgTab(servConn, servState, pmState, index)
ctx.tab = tab
pmState.tab = tab
}
return pmState
}