2014-05-11 16:18:55 +00:00
|
|
|
/*
|
|
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
2021-01-05 17:48:29 +00:00
|
|
|
Copyright (C) 2014-2021 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
|
2019-09-27 08:32:53 +00:00
|
|
|
the Free Software Foundation, version 3 of the License.
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
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"
|
2014-06-08 01:16:33 +00:00
|
|
|
"io/ioutil"
|
2014-05-11 16:18:55 +00:00
|
|
|
"log"
|
2014-06-09 14:03:11 +00:00
|
|
|
"net"
|
2014-05-11 16:18:55 +00:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-10-11 08:12:55 +00:00
|
|
|
"sync"
|
2014-05-11 16:18:55 +00:00
|
|
|
"time"
|
2018-03-08 02:40:26 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-10-11 08:12:55 +00:00
|
|
|
// Max deadline time of client's unresponsiveness
|
2015-05-09 15:27:06 +00:00
|
|
|
PingTimeout = time.Second * 180
|
|
|
|
// Max idle client's time before PING are sent
|
|
|
|
PingThreshold = time.Second * 90
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2014-05-13 19:59:09 +00:00
|
|
|
var (
|
2018-03-04 17:20:25 +00:00
|
|
|
RENickname = regexp.MustCompile("^[^\\x00\\x0D\\x0A\\x20\\x3A]{1,64}$") // any octet except NUL, CR, LF, " " and ":"
|
2014-05-13 19:59:09 +00:00
|
|
|
|
2016-03-26 14:10:04 +00:00
|
|
|
clients map[*Client]struct{} = make(map[*Client]struct{})
|
|
|
|
clientsM sync.RWMutex
|
|
|
|
rooms map[string]*Room = make(map[string]*Room)
|
|
|
|
roomsM sync.RWMutex
|
2015-10-11 08:12:55 +00:00
|
|
|
roomsGroup sync.WaitGroup
|
2016-03-26 14:10:04 +00:00
|
|
|
roomSinks map[*Room]chan ClientEvent = make(map[*Room]chan ClientEvent)
|
2015-10-11 08:12:55 +00:00
|
|
|
)
|
2014-05-11 16:18:55 +00:00
|
|
|
|
2018-03-04 17:20:25 +00:00
|
|
|
func GetRoom(name string) (r *Room, found bool) {
|
|
|
|
var room string
|
|
|
|
if strings.HasPrefix(name, "#") {
|
|
|
|
room = strings.ToLower(name)
|
|
|
|
} else {
|
|
|
|
room = "#" + strings.ToLower(name)
|
|
|
|
}
|
|
|
|
r, found = rooms[room]
|
|
|
|
|
|
|
|
return r, found
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:19:22 +00:00
|
|
|
func GetNumberOfRegisteredUsers(client *Client) (nusers float64) {
|
2018-03-08 02:40:26 +00:00
|
|
|
nusers = 0
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for client := range clients {
|
2014-05-11 16:18:55 +00:00
|
|
|
if client.registered {
|
2018-03-08 02:40:26 +00:00
|
|
|
nusers++
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RUnlock()
|
2018-03-08 02:40:26 +00:00
|
|
|
return nusers
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendLusers(client *Client) {
|
2018-03-08 17:11:58 +00:00
|
|
|
lusers := int(GetNumberOfRegisteredUsers(client))
|
2014-05-13 19:57:45 +00:00
|
|
|
client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers))
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func SendMotd(client *Client) {
|
|
|
|
if motd == nil {
|
2014-06-08 01:16:33 +00:00
|
|
|
client.ReplyNicknamed("422", "MOTD File is missing")
|
|
|
|
return
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
motdText, err := ioutil.ReadFile(*motd)
|
2014-06-08 01:16:33 +00:00
|
|
|
if err != nil {
|
2015-10-11 08:12:55 +00:00
|
|
|
log.Printf("Can not read motd file %s: %v", *motd, err)
|
2014-06-08 01:16:33 +00:00
|
|
|
client.ReplyNicknamed("422", "Error reading MOTD File")
|
|
|
|
return
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("375", "- "+*hostname+" Message of the day -")
|
2015-10-18 21:47:50 +00:00
|
|
|
for _, s := range strings.Split(strings.TrimSuffix(string(motdText), "\n"), "\n") {
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("372", "- "+s)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-06-08 01:16:33 +00:00
|
|
|
client.ReplyNicknamed("376", "End of /MOTD command")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func SendWhois(client *Client, nicknames []string) {
|
|
|
|
var c *Client
|
|
|
|
var hostPort string
|
|
|
|
var err error
|
|
|
|
var subscriptions []string
|
|
|
|
var room *Room
|
|
|
|
var subscriber *Client
|
2014-05-11 16:18:55 +00:00
|
|
|
for _, nickname := range nicknames {
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for c = range clients {
|
2018-03-04 17:20:25 +00:00
|
|
|
if c.Match(nickname) {
|
2015-10-11 08:12:55 +00:00
|
|
|
goto Found
|
2014-11-14 20:26:32 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RUnlock()
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNoNickChan(nickname)
|
|
|
|
continue
|
|
|
|
Found:
|
|
|
|
hostPort, _, err = net.SplitHostPort(c.conn.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can't parse RemoteAddr %q: %v", hostPort, err)
|
|
|
|
hostPort = "Unknown"
|
|
|
|
}
|
|
|
|
client.ReplyNicknamed("311", *c.nickname, *c.username, hostPort, "*", *c.realname)
|
|
|
|
client.ReplyNicknamed("312", *c.nickname, *hostname, *hostname)
|
|
|
|
if c.away != nil {
|
|
|
|
client.ReplyNicknamed("301", *c.nickname, *c.away)
|
|
|
|
}
|
|
|
|
subscriptions = make([]string, 0)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for _, room = range rooms {
|
|
|
|
for subscriber = range room.members {
|
2018-03-04 17:20:25 +00:00
|
|
|
if subscriber.Match(nickname) {
|
2015-10-11 08:12:55 +00:00
|
|
|
subscriptions = append(subscriptions, *room.name)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2015-10-11 08:12:55 +00:00
|
|
|
sort.Strings(subscriptions)
|
|
|
|
client.ReplyNicknamed("319", *c.nickname, strings.Join(subscriptions, " "))
|
|
|
|
client.ReplyNicknamed("318", *c.nickname, "End of /WHOIS list")
|
2018-03-04 17:20:25 +00:00
|
|
|
clientsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func SendList(client *Client, cols []string) {
|
|
|
|
var rs []string
|
|
|
|
var r string
|
2014-05-11 16:18:55 +00:00
|
|
|
if (len(cols) > 1) && (cols[1] != "") {
|
2015-10-11 08:12:55 +00:00
|
|
|
rs = strings.Split(strings.Split(cols[1], " ")[0], ",")
|
2014-05-11 16:18:55 +00:00
|
|
|
} else {
|
2015-10-11 08:12:55 +00:00
|
|
|
rs = make([]string, 0)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for r = range rooms {
|
|
|
|
rs = append(rs, r)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
sort.Strings(rs)
|
|
|
|
var room *Room
|
|
|
|
var found bool
|
|
|
|
for _, r = range rs {
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
if room, found = rooms[r]; found {
|
|
|
|
client.ReplyNicknamed(
|
|
|
|
"322",
|
2018-03-04 17:20:25 +00:00
|
|
|
*room.name,
|
2015-10-11 08:12:55 +00:00
|
|
|
fmt.Sprintf("%d", len(room.members)),
|
|
|
|
*room.topic,
|
|
|
|
)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-05-13 19:57:45 +00:00
|
|
|
client.ReplyNicknamed("323", "End of /LIST")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 17:20:25 +00:00
|
|
|
func ClientNick(client *Client, cols []string) {
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyParts("431", "No nickname given")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nickname := cols[1]
|
|
|
|
// Compatibility with some clients prepending colons to nickname
|
|
|
|
nickname = strings.TrimPrefix(nickname, ":")
|
|
|
|
rename := false
|
|
|
|
clientsM.RLock()
|
|
|
|
for existingClient := range clients {
|
|
|
|
if existingClient == client {
|
|
|
|
rename = true
|
|
|
|
} else if existingClient.Match(nickname) {
|
|
|
|
clientsM.RUnlock()
|
|
|
|
client.ReplyParts("433", "*", nickname, "Nickname is already in use")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clientsM.RUnlock()
|
|
|
|
if !RENickname.MatchString(nickname) {
|
|
|
|
client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rename {
|
|
|
|
// find all rooms the client has subscribed,
|
|
|
|
// then gather all clients in those rooms
|
|
|
|
cs := make(map[*Client]struct{})
|
|
|
|
clientsM.RLock() // first clients, then rooms,
|
2021-12-29 15:19:22 +00:00
|
|
|
roomsM.RLock() // to avoid deadlock with SendWhois
|
2018-03-04 17:20:25 +00:00
|
|
|
for _, r := range rooms {
|
|
|
|
if _, subscribed := r.members[client]; subscribed {
|
|
|
|
for c := range r.members {
|
|
|
|
cs[c] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then notify those clients of the nick change
|
|
|
|
message := ":" + client.String() + " NICK " + nickname
|
|
|
|
for c := range cs {
|
|
|
|
c.Msg(message)
|
|
|
|
}
|
|
|
|
roomsM.RUnlock()
|
|
|
|
clientsM.RUnlock()
|
|
|
|
}
|
|
|
|
client.nickname = &nickname
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
// Unregistered client workflow processor. Unregistered client:
|
|
|
|
// * is not PINGed
|
|
|
|
// * only QUIT, NICK and USER commands are processed
|
|
|
|
// * other commands are quietly ignored
|
|
|
|
// When client finishes NICK/USER workflow, then MOTD and LUSERS are send to him.
|
2015-10-11 08:12:55 +00:00
|
|
|
func ClientRegister(client *Client, cmd string, cols []string) {
|
|
|
|
switch cmd {
|
2014-08-14 10:01:54 +00:00
|
|
|
case "PASS":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("PASS")
|
|
|
|
return
|
|
|
|
}
|
2016-04-07 15:33:27 +00:00
|
|
|
password := strings.TrimPrefix(cols[1], ":")
|
|
|
|
client.password = &password
|
2014-05-11 16:18:55 +00:00
|
|
|
case "NICK":
|
2018-03-04 17:20:25 +00:00
|
|
|
ClientNick(client, cols)
|
2014-05-11 16:18:55 +00:00
|
|
|
case "USER":
|
|
|
|
if len(cols) == 1 {
|
|
|
|
client.ReplyNotEnoughParameters("USER")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
args := strings.SplitN(cols[1], " ", 4)
|
|
|
|
if len(args) < 4 {
|
|
|
|
client.ReplyNotEnoughParameters("USER")
|
|
|
|
return
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
client.username = &args[0]
|
|
|
|
realname := strings.TrimLeft(args[3], ":")
|
|
|
|
client.realname = &realname
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if *client.nickname != "*" && *client.username != "" {
|
|
|
|
if passwords != nil && *passwords != "" {
|
|
|
|
if client.password == nil {
|
2014-08-19 14:11:56 +00:00
|
|
|
client.ReplyParts("462", "You may not register")
|
2018-03-04 17:20:25 +00:00
|
|
|
client.Close("462")
|
2014-08-19 14:11:56 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
contents, err := ioutil.ReadFile(*passwords)
|
2014-08-19 14:11:56 +00:00
|
|
|
if err != nil {
|
2015-10-11 08:12:55 +00:00
|
|
|
log.Fatalf("Can no read passwords file %s: %s", *passwords, err)
|
2014-08-19 14:11:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, entry := range strings.Split(string(contents), "\n") {
|
|
|
|
if entry == "" {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if lp := strings.Split(entry, ":"); lp[0] == *client.nickname && lp[1] != *client.password {
|
2014-08-19 14:11:56 +00:00
|
|
|
client.ReplyParts("462", "You may not register")
|
2018-03-04 17:20:25 +00:00
|
|
|
client.Close("462")
|
2014-08-19 14:11:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-08-14 10:01:54 +00:00
|
|
|
}
|
2014-05-11 16:18:55 +00:00
|
|
|
client.registered = true
|
2018-03-08 02:40:26 +00:00
|
|
|
clients_irc_total.Inc()
|
|
|
|
clients_connected.Set(GetNumberOfRegisteredUsers(client))
|
2014-05-11 16:18:55 +00:00
|
|
|
client.ReplyNicknamed("001", "Hi, welcome to IRC")
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("002", "Your host is "+*hostname+", running goircd "+version)
|
2014-05-11 16:18:55 +00:00
|
|
|
client.ReplyNicknamed("003", "This server was created sometime")
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("004", *hostname+" goircd o o")
|
|
|
|
SendLusers(client)
|
|
|
|
SendMotd(client)
|
2014-08-14 10:01:54 +00:00
|
|
|
log.Println(client, "logged in")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register new room in Daemon. Create an object, events sink, save pointers
|
|
|
|
// to corresponding daemon's places and start room's processor goroutine.
|
2015-10-11 08:12:55 +00:00
|
|
|
func RoomRegister(name string) (*Room, chan ClientEvent) {
|
|
|
|
roomNew := NewRoom(name)
|
2014-08-09 12:42:19 +00:00
|
|
|
roomSink := make(chan ClientEvent)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.Lock()
|
2018-03-04 17:20:25 +00:00
|
|
|
rooms[strings.ToLower(name)] = roomNew
|
2015-10-11 08:12:55 +00:00
|
|
|
roomSinks[roomNew] = roomSink
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.Unlock()
|
2014-08-09 12:42:19 +00:00
|
|
|
go roomNew.Processor(roomSink)
|
2015-10-11 08:12:55 +00:00
|
|
|
roomsGroup.Add(1)
|
2014-08-09 12:42:19 +00:00
|
|
|
return roomNew, roomSink
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func HandlerJoin(client *Client, cmd string) {
|
2014-05-11 16:18:55 +00:00
|
|
|
args := strings.Split(cmd, " ")
|
2015-10-11 08:12:55 +00:00
|
|
|
rs := strings.Split(args[0], ",")
|
2014-05-11 16:18:55 +00:00
|
|
|
var keys []string
|
|
|
|
if len(args) > 1 {
|
|
|
|
keys = strings.Split(args[1], ",")
|
|
|
|
} else {
|
2015-10-11 08:12:55 +00:00
|
|
|
keys = make([]string, 0)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
var roomExisting *Room
|
|
|
|
var roomSink chan ClientEvent
|
|
|
|
var roomNew *Room
|
|
|
|
for n, room := range rs {
|
2014-05-13 19:59:09 +00:00
|
|
|
if !RoomNameValid(room) {
|
2014-05-11 16:18:55 +00:00
|
|
|
client.ReplyNoChannel(room)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var key string
|
|
|
|
if (n < len(keys)) && (keys[n] != "") {
|
|
|
|
key = keys[n]
|
|
|
|
} else {
|
|
|
|
key = ""
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for roomExisting, roomSink = range roomSinks {
|
2018-03-04 17:20:25 +00:00
|
|
|
if roomExisting.Match(room) {
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2015-10-13 06:37:03 +00:00
|
|
|
if (*roomExisting.key != "") && (*roomExisting.key != key) {
|
2015-10-11 08:12:55 +00:00
|
|
|
goto Denied
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
roomSink <- ClientEvent{client, EventNew, ""}
|
|
|
|
goto Joined
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2015-10-11 08:12:55 +00:00
|
|
|
roomNew, roomSink = RoomRegister(room)
|
2014-08-14 10:01:54 +00:00
|
|
|
log.Println("Room", roomNew, "created")
|
2014-05-11 16:18:55 +00:00
|
|
|
if key != "" {
|
2015-10-11 08:12:55 +00:00
|
|
|
roomNew.key = &key
|
2014-08-09 12:42:19 +00:00
|
|
|
roomNew.StateSave()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-08-09 12:42:19 +00:00
|
|
|
roomSink <- ClientEvent{client, EventNew, ""}
|
2015-10-11 08:12:55 +00:00
|
|
|
continue
|
|
|
|
Denied:
|
|
|
|
client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
|
|
|
|
Joined:
|
2021-12-29 15:19:22 +00:00
|
|
|
clients_irc_rooms_total.With(prometheus.Labels{"room": "all"}).Inc()
|
|
|
|
clients_irc_rooms_total.With(prometheus.Labels{"room": room}).Inc()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func Processor(events chan ClientEvent, finished chan struct{}) {
|
2015-10-06 07:59:49 +00:00
|
|
|
var now time.Time
|
2018-03-08 02:40:26 +00:00
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
go func() {
|
2020-11-06 17:10:06 +00:00
|
|
|
for range ticker.C {
|
2015-10-11 08:12:55 +00:00
|
|
|
events <- ClientEvent{eventType: EventTick}
|
|
|
|
}
|
|
|
|
}()
|
2020-11-06 17:10:06 +00:00
|
|
|
for e := range events {
|
2015-10-06 07:59:49 +00:00
|
|
|
now = time.Now()
|
2020-11-06 17:10:06 +00:00
|
|
|
client := e.client
|
|
|
|
switch e.eventType {
|
2015-10-11 08:12:55 +00:00
|
|
|
case EventTick:
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for c := range clients {
|
|
|
|
if c.recvTimestamp.Add(PingTimeout).Before(now) {
|
2020-11-06 17:10:06 +00:00
|
|
|
if *verbose {
|
|
|
|
log.Println(c, "ping timeout")
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
c.Close()
|
2014-05-11 16:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if c.sendTimestamp.Add(PingThreshold).Before(now) {
|
2014-05-11 16:18:55 +00:00
|
|
|
if c.registered {
|
2015-10-11 08:12:55 +00:00
|
|
|
c.Msg("PING :" + *hostname)
|
|
|
|
c.sendTimestamp = time.Now()
|
2014-05-11 16:18:55 +00:00
|
|
|
} else {
|
2020-11-06 17:10:06 +00:00
|
|
|
if *verbose {
|
|
|
|
log.Println(c, "ping timeout")
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
c.Close()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.RUnlock()
|
|
|
|
roomsLock.Lock()
|
2015-11-04 14:53:20 +00:00
|
|
|
for rn, r := range rooms {
|
|
|
|
if *statedir == "" && len(r.members) == 0 {
|
2020-11-06 17:10:06 +00:00
|
|
|
if *verbose {
|
|
|
|
log.Println(rn, "emptied room")
|
|
|
|
}
|
2015-11-04 14:53:20 +00:00
|
|
|
delete(rooms, rn)
|
2020-12-04 10:28:53 +00:00
|
|
|
r.events <- ClientEvent{eventType: EventTerm}
|
2020-11-06 17:10:06 +00:00
|
|
|
close(r.events)
|
2015-11-04 14:53:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
roomsLock.Unlock()
|
2015-10-11 08:12:55 +00:00
|
|
|
case EventTerm:
|
2020-11-06 17:10:06 +00:00
|
|
|
break EventsCycle
|
2015-10-11 08:12:55 +00:00
|
|
|
case EventNew:
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.Lock()
|
2015-10-11 08:12:55 +00:00
|
|
|
clients[client] = struct{}{}
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.Unlock()
|
2014-08-09 12:42:19 +00:00
|
|
|
case EventDel:
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.Lock()
|
2015-10-11 08:12:55 +00:00
|
|
|
delete(clients, client)
|
2020-11-06 17:10:06 +00:00
|
|
|
clientsLock.Unlock()
|
|
|
|
roomsLock.RLock()
|
|
|
|
for _, r := range rooms {
|
|
|
|
r.events <- e
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
roomsLock.RUnlock()
|
2014-08-09 12:42:19 +00:00
|
|
|
case EventMsg:
|
2020-11-06 17:10:06 +00:00
|
|
|
cols := strings.SplitN(e.text, " ", 2)
|
2015-10-11 08:12:55 +00:00
|
|
|
cmd := strings.ToUpper(cols[0])
|
|
|
|
if *verbose {
|
|
|
|
log.Println(client, "command", cmd)
|
2014-06-08 00:49:27 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
if cmd == "QUIT" {
|
2014-08-14 10:01:54 +00:00
|
|
|
log.Println(client, "quit")
|
2018-03-04 17:20:25 +00:00
|
|
|
var quitMsg string
|
|
|
|
if len(cols) >= 2 {
|
|
|
|
quitMsg = strings.TrimPrefix(cols[1], ":")
|
|
|
|
} else {
|
|
|
|
quitMsg = *client.nickname
|
2020-11-06 17:10:06 +00:00
|
|
|
}
|
2018-03-04 17:20:25 +00:00
|
|
|
client.Close(quitMsg)
|
2014-05-11 16:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !client.registered {
|
2020-11-06 17:10:06 +00:00
|
|
|
client.Register(cmd, cols)
|
2014-05-11 16:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-10 08:27:14 +00:00
|
|
|
if client != nil {
|
|
|
|
client.recvTimestamp = now
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
switch cmd {
|
2014-05-11 16:18:55 +00:00
|
|
|
case "AWAY":
|
2014-11-14 20:26:32 +00:00
|
|
|
if len(cols) == 1 {
|
2020-11-06 17:10:06 +00:00
|
|
|
client.away = ""
|
2014-11-14 20:26:32 +00:00
|
|
|
client.ReplyNicknamed("305", "You are no longer marked as being away")
|
|
|
|
continue
|
|
|
|
}
|
2018-03-04 19:50:32 +00:00
|
|
|
msg := cols[1]
|
2014-11-14 20:26:32 +00:00
|
|
|
client.away = &msg
|
|
|
|
client.ReplyNicknamed("306", "You have been marked as being away")
|
2014-05-11 16:18:55 +00:00
|
|
|
case "JOIN":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("JOIN")
|
|
|
|
continue
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
HandlerJoin(client, cols[1])
|
2018-03-04 17:20:25 +00:00
|
|
|
case "NICK":
|
|
|
|
ClientNick(client, cols)
|
2014-05-11 16:18:55 +00:00
|
|
|
case "LIST":
|
2020-11-06 17:10:06 +00:00
|
|
|
client.SendList(cols)
|
2014-05-11 16:18:55 +00:00
|
|
|
case "LUSERS":
|
2020-11-06 17:10:06 +00:00
|
|
|
client.SendLusers()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "MODE":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("MODE")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cols = strings.SplitN(cols[1], " ", 2)
|
2018-03-04 17:20:25 +00:00
|
|
|
if strings.ToLower(cols[0]) == strings.ToLower(*client.username) {
|
2014-05-11 16:18:55 +00:00
|
|
|
if len(cols) == 1 {
|
2015-10-11 08:12:55 +00:00
|
|
|
client.Msg("221 " + *client.nickname + " +")
|
2014-05-11 16:18:55 +00:00
|
|
|
} else {
|
|
|
|
client.ReplyNicknamed("501", "Unknown MODE flag")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2014-05-13 19:59:09 +00:00
|
|
|
room := cols[0]
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2018-03-04 17:20:25 +00:00
|
|
|
r, found := GetRoom(room)
|
2014-05-11 16:18:55 +00:00
|
|
|
if !found {
|
|
|
|
client.ReplyNoChannel(room)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(cols) == 1 {
|
2020-11-06 17:10:06 +00:00
|
|
|
r.events <- ClientEvent{client, EventMode, ""}
|
2014-05-11 16:18:55 +00:00
|
|
|
} else {
|
2020-11-06 17:10:06 +00:00
|
|
|
r.events <- ClientEvent{client, EventMode, cols[1]}
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "MOTD":
|
2020-11-06 17:10:06 +00:00
|
|
|
client.SendMotd()
|
|
|
|
case "NAMES":
|
|
|
|
rs := make([]*Room, len(cols))
|
|
|
|
roomsLock.RLock()
|
|
|
|
if len(cols) == 0 {
|
|
|
|
for _, r := range rooms {
|
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
needed := make(map[string]struct{}, len(rs))
|
|
|
|
for _, r := range cols {
|
|
|
|
needed[r] = struct{}{}
|
|
|
|
}
|
|
|
|
for rn, r := range rooms {
|
|
|
|
if _, found := needed[rn]; found {
|
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomsLock.RUnlock()
|
|
|
|
for _, r := range rs {
|
|
|
|
r.SendNames(client)
|
|
|
|
}
|
2014-05-11 16:18:55 +00:00
|
|
|
case "PART":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("PART")
|
|
|
|
continue
|
|
|
|
}
|
2018-03-04 17:20:25 +00:00
|
|
|
rs := strings.SplitN(cols[1], " ", 2)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2018-03-04 17:20:25 +00:00
|
|
|
for _, room := range strings.Split(rs[0], ",") {
|
|
|
|
if r, found := GetRoom(room); found {
|
|
|
|
var partMsg string
|
|
|
|
if len(rs) >= 2 {
|
|
|
|
partMsg = strings.TrimPrefix(rs[1], ":")
|
|
|
|
} else {
|
|
|
|
partMsg = *client.nickname
|
|
|
|
}
|
|
|
|
roomSinks[r] <- ClientEvent{client, EventDel, partMsg}
|
2015-10-11 08:12:55 +00:00
|
|
|
} else {
|
2014-05-11 16:18:55 +00:00
|
|
|
client.ReplyNoChannel(room)
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "PING":
|
|
|
|
if len(cols) == 1 {
|
|
|
|
client.ReplyNicknamed("409", "No origin specified")
|
|
|
|
continue
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
client.Reply(fmt.Sprintf("PONG %s :%s", *hostname, cols[1]))
|
2014-05-11 16:18:55 +00:00
|
|
|
case "PONG":
|
|
|
|
continue
|
|
|
|
case "NOTICE", "PRIVMSG":
|
|
|
|
if len(cols) == 1 {
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("411", "No recipient given ("+cmd+")")
|
2014-05-11 16:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cols = strings.SplitN(cols[1], " ", 2)
|
|
|
|
if len(cols) == 1 {
|
|
|
|
client.ReplyNicknamed("412", "No text to send")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg := ""
|
2018-03-04 17:20:25 +00:00
|
|
|
target := cols[0]
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
for c := range clients {
|
2018-03-04 17:20:25 +00:00
|
|
|
if c.Match(target) {
|
2015-10-11 08:12:55 +00:00
|
|
|
msg = fmt.Sprintf(":%s %s %s %s", client, cmd, *c.nickname, cols[1])
|
2014-05-11 16:18:55 +00:00
|
|
|
c.Msg(msg)
|
2014-11-14 20:26:32 +00:00
|
|
|
if c.away != nil {
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("301", *c.nickname, *c.away)
|
2014-11-14 20:26:32 +00:00
|
|
|
}
|
2014-05-11 16:18:55 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
if msg != "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2018-03-04 17:20:25 +00:00
|
|
|
if r, found := rooms[strings.ToLower(target)]; found {
|
2015-10-11 08:12:55 +00:00
|
|
|
roomSinks[r] <- ClientEvent{
|
|
|
|
client,
|
|
|
|
EventMsg,
|
|
|
|
cmd + " " + strings.TrimLeft(cols[1], ":"),
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
roomsLock.RUnlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
roomsLock.RUnlock()
|
|
|
|
var msg string
|
|
|
|
clientsLock.RLock()
|
|
|
|
for c := range clients {
|
|
|
|
if c.nickname != target {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg = fmt.Sprintf(":%s %s %s %s", client, cmd, c.nickname, cols[1])
|
|
|
|
c.Msg(msg)
|
|
|
|
if c.away != "" {
|
|
|
|
client.ReplyNicknamed("301", c.nickname, c.away)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
clientsLock.RUnlock()
|
|
|
|
if msg != "" {
|
|
|
|
continue
|
2015-05-09 15:27:06 +00:00
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
client.ReplyNoNickChan(target)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "TOPIC":
|
|
|
|
if len(cols) == 1 {
|
|
|
|
client.ReplyNotEnoughParameters("TOPIC")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cols = strings.SplitN(cols[1], " ", 2)
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2018-03-04 17:20:25 +00:00
|
|
|
r, found := GetRoom(cols[0])
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
if !found {
|
2014-05-13 19:59:09 +00:00
|
|
|
client.ReplyNoChannel(cols[0])
|
2014-05-18 11:26:15 +00:00
|
|
|
continue
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
var change string
|
|
|
|
if len(cols) > 1 {
|
|
|
|
change = cols[1]
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RLock()
|
2015-10-11 08:12:55 +00:00
|
|
|
roomSinks[r] <- ClientEvent{client, EventTopic, change}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "WHO":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("WHO")
|
|
|
|
continue
|
|
|
|
}
|
2014-05-13 19:59:09 +00:00
|
|
|
room := strings.Split(cols[1], " ")[0]
|
2021-12-29 15:19:22 +00:00
|
|
|
roomsM.RLock()
|
2018-03-04 17:20:25 +00:00
|
|
|
if r, found := GetRoom(room); found {
|
2015-10-11 08:12:55 +00:00
|
|
|
roomSinks[r] <- ClientEvent{client, EventWho, ""}
|
|
|
|
} else {
|
2014-05-11 16:18:55 +00:00
|
|
|
client.ReplyNoChannel(room)
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
roomsM.RUnlock()
|
2014-05-11 16:18:55 +00:00
|
|
|
case "WHOIS":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("WHOIS")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cols := strings.Split(cols[1], " ")
|
|
|
|
nicknames := strings.Split(cols[len(cols)-1], ",")
|
2015-10-11 08:12:55 +00:00
|
|
|
SendWhois(client, nicknames)
|
2015-11-14 15:54:34 +00:00
|
|
|
case "ISON":
|
|
|
|
if len(cols) == 1 || len(cols[1]) < 1 {
|
|
|
|
client.ReplyNotEnoughParameters("ISON")
|
|
|
|
continue
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
nicknamesList := strings.Split(cols[1], " ")
|
|
|
|
nicknames := make(map[string]bool, len(nicknamesList))
|
|
|
|
for _, nickname := range nicknamesList {
|
|
|
|
nicknames[nickname] = false
|
|
|
|
}
|
2021-12-29 15:19:22 +00:00
|
|
|
clientsM.RLock()
|
2015-11-14 15:54:34 +00:00
|
|
|
for c := range clients {
|
2020-11-06 17:10:06 +00:00
|
|
|
if _, exists := nicknames[c.nickname]; exists {
|
|
|
|
nicknames[c.nickname] = true
|
|
|
|
}
|
2015-11-14 15:54:34 +00:00
|
|
|
}
|
2021-12-29 15:19:22 +00:00
|
|
|
clientsM.RUnlock()
|
2020-11-06 17:10:06 +00:00
|
|
|
nicknamesList = nicknamesList[:0]
|
|
|
|
for n, exists := range nicknames {
|
|
|
|
if exists {
|
|
|
|
nicknamesList = append(nicknamesList, n)
|
2015-11-14 15:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
client.ReplyNicknamed("303", strings.Join(nicknamesList, " "))
|
|
|
|
case "WALLOPS":
|
|
|
|
if len(cols) == 1 {
|
|
|
|
client.ReplyNotEnoughParameters("WALLOPS")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cs := make([]*Client, 0, len(clients))
|
|
|
|
clientsLock.RLock()
|
|
|
|
for c := range clients {
|
|
|
|
if c != client {
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clientsLock.RUnlock()
|
|
|
|
for _, c := range cs {
|
|
|
|
c.Msg(fmt.Sprintf(":%s NOTICE %s %s", client, c.nickname, cols[1]))
|
|
|
|
}
|
2016-03-26 14:10:04 +00:00
|
|
|
clientsM.RLock()
|
2015-11-14 15:54:34 +00:00
|
|
|
var nicksExists []string
|
|
|
|
for _, nickname := range strings.Split(cols[1], " ") {
|
2018-03-04 17:20:25 +00:00
|
|
|
for c := range clients {
|
|
|
|
if c.Match(nickname) {
|
|
|
|
nicksExists = append(nicksExists, nickname)
|
|
|
|
}
|
2015-11-14 15:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-04 17:20:25 +00:00
|
|
|
clientsM.RUnlock()
|
2015-11-14 15:54:34 +00:00
|
|
|
client.ReplyNicknamed("303", strings.Join(nicksExists, " "))
|
2014-08-14 10:31:34 +00:00
|
|
|
case "VERSION":
|
|
|
|
var debug string
|
2015-10-11 08:12:55 +00:00
|
|
|
if *verbose {
|
2014-08-14 10:31:34 +00:00
|
|
|
debug = "debug"
|
|
|
|
}
|
2020-09-05 08:30:25 +00:00
|
|
|
client.ReplyNicknamed("351", fmt.Sprintf("%s.%s %s :", Version, debug, *hostname))
|
2014-05-11 16:18:55 +00:00
|
|
|
default:
|
2015-10-11 08:12:55 +00:00
|
|
|
client.ReplyNicknamed("421", cmd, "Unknown command")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
ticker.Stop()
|
|
|
|
|
|
|
|
// Notify all clients about shutdown
|
|
|
|
clientsLock.RLock()
|
|
|
|
for c := range clients {
|
|
|
|
c.Msg(fmt.Sprintf(
|
|
|
|
":%s NOTICE %s %s", *hostname, c.nickname,
|
|
|
|
":Server is shutting down",
|
|
|
|
))
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
clientsLock.RUnlock()
|
|
|
|
|
|
|
|
// Read their EventDel
|
|
|
|
go func() {
|
|
|
|
for range events {
|
2018-03-08 02:40:26 +00:00
|
|
|
clients_connected.Set(GetNumberOfRegisteredUsers(client))
|
2020-11-06 17:10:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Stop room processors
|
|
|
|
roomsLock.RLock()
|
|
|
|
for _, r := range rooms {
|
|
|
|
r.events <- ClientEvent{eventType: EventTerm}
|
|
|
|
}
|
|
|
|
roomsLock.RUnlock()
|
|
|
|
roomsWG.Wait()
|
|
|
|
|
|
|
|
// Wait for either 5sec or all clients quitting
|
|
|
|
t := time.NewTimer(5 * time.Second)
|
|
|
|
clientsDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
clientsWG.Wait()
|
|
|
|
close(clientsDone)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
case <-clientsDone:
|
|
|
|
}
|
|
|
|
if !t.Stop() {
|
|
|
|
<-t.C
|
|
|
|
}
|
|
|
|
close(events)
|
|
|
|
close(finished)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|