goircd/room.go

214 lines
5.6 KiB
Go
Raw Normal View History

2014-05-11 16:18:55 +00:00
/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
2015-05-09 15:27:06 +00:00
Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
2014-05-11 16:18:55 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-05-09 15:27:06 +00:00
2014-05-11 16:18:55 +00:00
package main
import (
"fmt"
"log"
"regexp"
"sort"
"strings"
)
var (
2014-08-09 12:42:19 +00:00
RERoom = regexp.MustCompile("^#[^\x00\x07\x0a\x0d ,:/]{1,200}$")
rooms map[string]*Room = make(map[string]*Room)
roomSinks map[*Room]chan ClientEvent = make(map[*Room]chan ClientEvent)
)
2014-05-11 16:18:55 +00:00
// Sanitize room's name. It can consist of 1 to 50 ASCII symbols
// with some exclusions. All room names will have "#" prefix.
func RoomNameValid(name string) bool {
2014-08-09 12:42:19 +00:00
return RERoom.MatchString(name)
2014-05-11 16:18:55 +00:00
}
type Room struct {
name *string
topic *string
key *string
members map[*Client]struct{}
2014-05-11 16:18:55 +00:00
}
func (room Room) String() string {
return *room.name
}
func NewRoom(name string) *Room {
topic := ""
2015-10-13 06:37:03 +00:00
key := ""
return &Room{
name: &name,
topic: &topic,
2015-10-13 06:37:03 +00:00
key: &key,
members: make(map[*Client]struct{}),
}
2014-05-11 16:18:55 +00:00
}
func (room *Room) SendTopic(client *Client) {
if *room.topic == "" {
client.ReplyNicknamed("331", *room.name, "No topic is set")
2014-05-11 16:18:55 +00:00
} else {
client.ReplyNicknamed("332", *room.name, *room.topic)
2014-05-11 16:18:55 +00:00
}
}
// Send message to all room's subscribers, possibly excluding someone.
2014-08-09 12:42:19 +00:00
func (room *Room) Broadcast(msg string, clientToIgnore ...*Client) {
2014-05-11 16:18:55 +00:00
for member := range room.members {
2014-08-09 12:42:19 +00:00
if (len(clientToIgnore) > 0) && member == clientToIgnore[0] {
2014-05-11 16:18:55 +00:00
continue
}
member.Msg(msg)
}
}
func (room *Room) StateSave() {
2015-10-13 06:37:03 +00:00
stateSink <- StateEvent{*room.name, *room.topic, *room.key}
2014-05-11 16:18:55 +00:00
}
2014-06-08 00:21:06 +00:00
func (room *Room) Processor(events <-chan ClientEvent) {
2014-05-11 16:18:55 +00:00
var client *Client
for event := range events {
client = event.client
2014-08-09 12:42:19 +00:00
switch event.eventType {
case EventTerm:
roomsGroup.Done()
return
2014-08-09 12:42:19 +00:00
case EventNew:
room.members[client] = struct{}{}
if *verbose {
2014-06-08 00:49:27 +00:00
log.Println(client, "joined", room.name)
}
2014-05-11 16:18:55 +00:00
room.SendTopic(client)
room.Broadcast(fmt.Sprintf(":%s JOIN %s", client, *room.name))
logSink <- LogEvent{*room.name, *client.nickname, "joined", true}
nicknames := make([]string, 0)
2014-05-11 16:18:55 +00:00
for member := range room.members {
nicknames = append(nicknames, *member.nickname)
2014-05-11 16:18:55 +00:00
}
sort.Strings(nicknames)
client.ReplyNicknamed("353", "=", *room.name, strings.Join(nicknames, " "))
client.ReplyNicknamed("366", *room.name, "End of NAMES list")
2014-08-09 12:42:19 +00:00
case EventDel:
2014-05-11 16:18:55 +00:00
if _, subscribed := room.members[client]; !subscribed {
client.ReplyNicknamed("442", *room.name, "You are not on that channel")
2014-05-11 16:18:55 +00:00
continue
}
delete(room.members, client)
msg := fmt.Sprintf(":%s PART %s :%s", client, *room.name, *client.nickname)
2014-08-09 11:37:10 +00:00
room.Broadcast(msg)
logSink <- LogEvent{*room.name, *client.nickname, "left", true}
2014-08-09 12:42:19 +00:00
case EventTopic:
2014-05-11 16:18:55 +00:00
if _, subscribed := room.members[client]; !subscribed {
client.ReplyParts("442", *room.name, "You are not on that channel")
2014-05-11 16:18:55 +00:00
continue
}
if event.text == "" {
room.SendTopic(client)
2014-05-11 16:18:55 +00:00
continue
}
topic := strings.TrimLeft(event.text, ":")
room.topic = &topic
msg := fmt.Sprintf(":%s TOPIC %s :%s", client, *room.name, *room.topic)
room.Broadcast(msg)
logSink <- LogEvent{
*room.name,
*client.nickname,
"set topic to " + *room.topic,
2015-10-05 20:57:31 +00:00
true,
}
2014-05-11 16:18:55 +00:00
room.StateSave()
2014-08-09 12:42:19 +00:00
case EventWho:
2014-05-11 16:18:55 +00:00
for m := range room.members {
2015-10-05 20:57:31 +00:00
client.ReplyNicknamed(
"352",
*room.name,
*m.username,
2015-10-05 20:57:31 +00:00
m.conn.RemoteAddr().String(),
*hostname,
*m.nickname,
2015-10-05 20:57:31 +00:00
"H",
"0 "+*m.realname,
2015-10-05 20:57:31 +00:00
)
2014-05-11 16:18:55 +00:00
}
client.ReplyNicknamed("315", *room.name, "End of /WHO list")
2014-08-09 12:42:19 +00:00
case EventMode:
2014-05-11 16:18:55 +00:00
if event.text == "" {
mode := "+"
2015-10-13 06:37:03 +00:00
if *room.key != "" {
2014-05-11 16:18:55 +00:00
mode = mode + "k"
}
client.Msg(fmt.Sprintf("324 %s %s %s", *client.nickname, *room.name, mode))
2014-05-11 16:18:55 +00:00
continue
}
if strings.HasPrefix(event.text, "b") {
client.ReplyNicknamed("368", *room.name, "End of channel ban list")
continue
}
2014-05-11 16:18:55 +00:00
if strings.HasPrefix(event.text, "-k") || strings.HasPrefix(event.text, "+k") {
if _, subscribed := room.members[client]; !subscribed {
client.ReplyParts("442", *room.name, "You are not on that channel")
2014-05-11 16:18:55 +00:00
continue
}
} else {
client.ReplyNicknamed("472", event.text, "Unknown MODE flag")
continue
}
var msg string
2014-08-09 12:42:19 +00:00
var msgLog string
2014-05-11 16:18:55 +00:00
if strings.HasPrefix(event.text, "+k") {
cols := strings.Split(event.text, " ")
if len(cols) == 1 {
client.ReplyNotEnoughParameters("MODE")
continue
}
room.key = &cols[1]
msg = fmt.Sprintf(":%s MODE %s +k %s", client, *room.name, *room.key)
msgLog = "set channel key to " + *room.key
} else {
2015-10-13 06:37:03 +00:00
key := ""
room.key = &key
msg = fmt.Sprintf(":%s MODE %s -k", client, *room.name)
2014-08-09 12:42:19 +00:00
msgLog = "removed channel key"
2014-05-11 16:18:55 +00:00
}
room.Broadcast(msg)
logSink <- LogEvent{*room.name, *client.nickname, msgLog, true}
2014-05-11 16:18:55 +00:00
room.StateSave()
2014-08-09 12:42:19 +00:00
case EventMsg:
2014-05-11 16:18:55 +00:00
sep := strings.Index(event.text, " ")
2015-10-05 20:57:31 +00:00
room.Broadcast(fmt.Sprintf(
":%s %s %s :%s",
client,
event.text[:sep],
*room.name,
2015-10-05 20:57:31 +00:00
event.text[sep+1:]),
client,
)
logSink <- LogEvent{
*room.name,
*client.nickname,
2015-10-05 20:57:31 +00:00
event.text[sep+1:],
false,
}
2014-05-11 16:18:55 +00:00
}
}
}