/
channel.go
180 lines (145 loc) · 3.38 KB
/
channel.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
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
package ircd
import (
"fmt"
"strings"
"sync"
"github.com/PacketFire/go-ircd/parser"
)
type Channel interface {
Name() string
Join(c Client) error
Part(c Client) error
Close() error
Users() []Client
Topic() string
SetTopic(topic string) string
Send(prefix, command string, args ...string) error // send a message to the client
}
// An IRC Channel
type ircChannel struct {
mu sync.Mutex
// server instance
srv Server
srvname string
// Channel name, including #/&
name string
// Users joined to channel
users map[string]Client
// Count of current users
nusers int
// Channel topic
topic string
// Channel modes
modes *Modeset
// Modes of users on the channel
umodes map[string]Modeset
}
// Create a new channel
func NewChannel(srv Server, srvname, name string) Channel {
ch := &ircChannel{
srv: srv,
srvname: srvname,
name: name,
users: make(map[string]Client),
topic: " ", // gross
modes: NewModeset(),
umodes: make(map[string]Modeset),
}
return ch
}
func (ch *ircChannel) Close() error {
return nil
}
// Name returns the channel name, including the leading hash.
func (ch *ircChannel) Name() string {
return ch.name
}
func (ch *ircChannel) Join(c Client) error {
ch.mu.Lock()
defer ch.mu.Unlock()
if _, ok := ch.users[c.Nick()]; ok {
return fmt.Errorf("user %s already in channel %s", c, ch.name)
}
ch.users[c.Nick()] = c
ch.nusers++
// inform user of join
c.Send(c.Prefix(), "JOIN", ch.name)
// topic
if ch.topic != "" {
c.Send(ch.srvname, "332", ch.name, ch.topic)
}
// time created?
//who.Send(ch.serv.Name(), "333", who.nick, ch.name, "0")
//ch.um.RLock()
//defer ch.um.RUnlock()
// nick list, meh
users := make([]string, len(ch.users))
for u := range ch.users {
users = append(users, u)
}
Numeric(ch.srv.Name(), c, "353", "=", ch.name, strings.Join(users, " "))
// end nicks
Numeric(ch.srv.Name(), c, "366", ch.name, "end of /NAMES")
// finally, inform other users of join
cnick := c.Nick()
cprefix := c.Prefix()
for onick, ocl := range ch.users {
if cnick != onick {
ocl.Send(cprefix, "JOIN", ch.name)
}
}
return nil
}
func (ch *ircChannel) Part(c Client) error {
ch.mu.Lock()
defer ch.mu.Unlock()
cnick := c.Nick()
if _, ok := ch.users[cnick]; !ok {
return fmt.Errorf("%s not on channel %s", c, ch.name)
}
cprefix := c.Prefix()
for _, ocl := range ch.users {
go ocl.Send(cprefix, "PART", ch.name, "part")
}
delete(ch.users, cnick)
ch.nusers--
return nil
}
// Generate a list of nicks on the channel
func (ch *ircChannel) Users() []Client {
ch.mu.Lock()
defer ch.mu.Unlock()
out := make([]Client, len(ch.users))
i := 0
for _, c := range ch.users {
out[i] = c
i++
}
return out
}
func (ch *ircChannel) Topic() string {
ch.mu.Lock()
defer ch.mu.Unlock()
return ch.topic
}
func (ch *ircChannel) SetTopic(topic string) string {
ch.mu.Lock()
defer ch.mu.Unlock()
old := ch.topic
ch.topic = topic
return old
}
// Send an IRC Message to all users on a channel
func (ch *ircChannel) Send(prefix, command string, args ...string) error {
m := parser.Message{Prefix: prefix, Command: command, Args: args}
for onick, ocl := range ch.users {
mnick, _, _ := SplitPrefix(m.Prefix)
if mnick != onick {
ocl.Send(m.Prefix, m.Command, m.Args...)
}
}
return nil
}
func (ch *ircChannel) Privmsg(from Client, msg string) error {
return ch.Send(from.Prefix(), "PRIVMSG", ch.Name(), msg)
}