2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
const (
|
|
|
|
DEBUG_CHANNEL = true
|
|
|
|
)
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
type Channel struct {
|
2013-05-25 06:39:53 +02:00
|
|
|
id *RowId
|
2012-12-12 08:04:03 +01:00
|
|
|
server *Server
|
2012-12-15 23:34:20 +01:00
|
|
|
commands chan<- ChannelCommand
|
2012-12-17 04:13:53 +01:00
|
|
|
replies chan<- Reply
|
2012-12-12 08:04:03 +01:00
|
|
|
name string
|
|
|
|
key string
|
|
|
|
topic string
|
2012-12-15 23:34:20 +01:00
|
|
|
members UserSet
|
2012-12-12 08:04:03 +01:00
|
|
|
noOutside bool
|
|
|
|
password string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (set ChannelSet) Add(channel *Channel) {
|
|
|
|
set[channel] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set ChannelSet) Remove(channel *Channel) {
|
|
|
|
delete(set, channel)
|
|
|
|
}
|
|
|
|
|
2013-05-25 06:39:53 +02:00
|
|
|
func (set ChannelSet) Ids() (ids []RowId) {
|
|
|
|
ids = []RowId{}
|
|
|
|
for channel := range set {
|
|
|
|
if channel.id != nil {
|
|
|
|
ids = append(ids, *channel.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type ChannelCommand interface {
|
|
|
|
Command
|
|
|
|
HandleChannel(channel *Channel)
|
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
// NewChannel creates a new channel from a `Server` and a `name` string, which
|
|
|
|
// must be unique on the server.
|
2012-12-09 07:54:58 +01:00
|
|
|
func NewChannel(s *Server, name string) *Channel {
|
2013-05-26 22:28:22 +02:00
|
|
|
commands := make(chan ChannelCommand, 1)
|
|
|
|
replies := make(chan Reply, 1)
|
2012-12-15 23:34:20 +01:00
|
|
|
channel := &Channel{
|
|
|
|
name: name,
|
|
|
|
members: make(UserSet),
|
|
|
|
server: s,
|
|
|
|
commands: commands,
|
|
|
|
replies: replies,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
go channel.receiveCommands(commands)
|
2013-05-26 22:28:22 +02:00
|
|
|
go channel.receiveReplies(replies)
|
2013-06-03 01:32:22 +02:00
|
|
|
Save(s.db, channel)
|
2012-12-15 23:34:20 +01:00
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2013-05-25 06:39:53 +02:00
|
|
|
func (channel *Channel) Save(q Queryable) bool {
|
|
|
|
if channel.id == nil {
|
|
|
|
if err := InsertChannel(q, channel); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
channelId, err := FindChannelIdByName(q, channel.name)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
channel.id = &channelId
|
|
|
|
} else {
|
|
|
|
if err := UpdateChannel(q, channel); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
|
|
|
|
for command := range commands {
|
|
|
|
if DEBUG_CHANNEL {
|
|
|
|
log.Printf("%s → %s : %s", command.Source(), channel, command)
|
|
|
|
}
|
|
|
|
command.HandleChannel(channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 20:20:55 +02:00
|
|
|
func (channel *Channel) receiveReplies(replies <-chan Reply) {
|
2012-12-15 23:34:20 +01:00
|
|
|
for reply := range replies {
|
2013-05-12 03:28:18 +02:00
|
|
|
if DEBUG_CHANNEL {
|
2013-05-26 22:28:22 +02:00
|
|
|
log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
|
2013-05-12 03:28:18 +02:00
|
|
|
}
|
2013-05-12 20:20:55 +02:00
|
|
|
for user := range channel.members {
|
2012-12-17 04:13:53 +01:00
|
|
|
if user != reply.Source() {
|
2013-05-26 22:28:22 +02:00
|
|
|
user.Replies() <- reply
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-12 20:20:55 +02:00
|
|
|
func (channel *Channel) Nicks() []string {
|
|
|
|
return channel.members.Nicks()
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:20:55 +02:00
|
|
|
func (channel *Channel) IsEmpty() bool {
|
|
|
|
return len(channel.members) == 0
|
2012-12-10 06:46:22 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (channel *Channel) GetTopic(replier Replier) {
|
|
|
|
if channel.topic == "" {
|
|
|
|
replier.Replies() <- RplNoTopic(channel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replier.Replies() <- RplTopic(channel)
|
|
|
|
}
|
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
func (channel *Channel) Replies() chan<- Reply {
|
|
|
|
return channel.replies
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) Id() string {
|
2012-12-17 04:13:53 +01:00
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) PublicId() string {
|
2012-12-17 04:13:53 +01:00
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) Commands() chan<- ChannelCommand {
|
2013-05-09 20:05:10 +02:00
|
|
|
return channel.commands
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) String() string {
|
|
|
|
return channel.Id()
|
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
//
|
2012-12-15 23:34:20 +01:00
|
|
|
// commands
|
2012-12-09 21:51:50 +01:00
|
|
|
//
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *JoinCommand) HandleChannel(channel *Channel) {
|
2012-12-15 23:34:20 +01:00
|
|
|
client := m.Client()
|
|
|
|
user := client.user
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
if channel.key != m.channels[channel.name] {
|
2013-05-26 22:28:22 +02:00
|
|
|
client.user.Replies() <- ErrBadChannelKey(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel.members.Add(user)
|
|
|
|
user.channels.Add(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
channel.Replies() <- RplJoin(channel, user)
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.GetTopic(user)
|
2013-05-26 22:28:22 +02:00
|
|
|
user.Replies() <- RplNamReply(channel)
|
|
|
|
user.Replies() <- RplEndOfNames(channel.server)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PartCommand) HandleChannel(channel *Channel) {
|
2012-12-15 23:34:20 +01:00
|
|
|
user := m.Client().user
|
|
|
|
|
|
|
|
if !channel.members[user] {
|
|
|
|
user.replies <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := m.message
|
|
|
|
if msg == "" {
|
|
|
|
msg = user.Nick()
|
2012-12-09 19:15:01 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
channel.Replies() <- RplPart(channel, user, msg)
|
2012-12-09 19:15:01 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.members.Remove(user)
|
|
|
|
user.channels.Remove(channel)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2013-05-12 20:20:55 +02:00
|
|
|
if channel.IsEmpty() {
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.server.DeleteChannel(channel)
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *TopicCommand) HandleChannel(channel *Channel) {
|
2013-05-12 20:37:35 +02:00
|
|
|
user := m.User()
|
2012-12-17 04:13:53 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if !channel.members[user] {
|
2013-05-26 22:28:22 +02:00
|
|
|
user.Replies() <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
if m.topic == "" {
|
|
|
|
channel.GetTopic(user)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.topic = m.topic
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if channel.topic == "" {
|
2013-05-26 22:28:22 +02:00
|
|
|
channel.Replies() <- RplNoTopic(channel)
|
2012-12-15 23:34:20 +01:00
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2013-05-26 22:28:22 +02:00
|
|
|
channel.Replies() <- RplTopic(channel)
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
|
2013-05-12 20:37:35 +02:00
|
|
|
channel.Replies() <- RplPrivMsgChannel(channel, m.User(), m.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|