goircd/daemon.go

505 lines
13 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"
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"
"sync"
2014-05-11 16:18:55 +00:00
"time"
)
const (
// 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
)
var (
RENickname = regexp.MustCompile("^[a-zA-Z0-9-]{1,24}$")
roomsGroup sync.WaitGroup
2014-05-11 16:18:55 +00:00
clients map[*Client]struct{} = make(map[*Client]struct{})
)
2014-05-11 16:18:55 +00:00
func SendLusers(client *Client) {
2014-05-11 16:18:55 +00:00
lusers := 0
for client := range clients {
2014-05-11 16:18:55 +00:00
if client.registered {
lusers++
}
}
client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers))
2014-05-11 16:18: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
}
motdText, err := ioutil.ReadFile(*motd)
2014-06-08 01:16:33 +00:00
if err != nil {
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
}
client.ReplyNicknamed("375", "- "+*hostname+" Message of the day -")
for _, s := range strings.Split(strings.TrimSuffix(string(motdText), "\n"), "\n") {
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
}
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 {
nickname = strings.ToLower(nickname)
for c = range clients {
if strings.ToLower(*c.nickname) == nickname {
goto Found
2014-11-14 20:26:32 +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)
for _, room = range rooms {
for subscriber = range room.members {
if *subscriber.nickname == nickname {
subscriptions = append(subscriptions, *room.name)
2014-05-11 16:18:55 +00:00
}
}
}
sort.Strings(subscriptions)
client.ReplyNicknamed("319", *c.nickname, strings.Join(subscriptions, " "))
client.ReplyNicknamed("318", *c.nickname, "End of /WHOIS list")
2014-05-11 16:18: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] != "") {
rs = strings.Split(strings.Split(cols[1], " ")[0], ",")
2014-05-11 16:18:55 +00:00
} else {
rs = make([]string, 0)
for r = range rooms {
rs = append(rs, r)
2014-05-11 16:18:55 +00:00
}
}
sort.Strings(rs)
var room *Room
var found bool
for _, r = range rs {
if room, found = rooms[r]; found {
client.ReplyNicknamed(
"322",
r,
fmt.Sprintf("%d", len(room.members)),
*room.topic,
)
2014-05-11 16:18:55 +00:00
}
}
client.ReplyNicknamed("323", "End of /LIST")
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.
func ClientRegister(client *Client, cmd string, cols []string) {
switch cmd {
case "PASS":
if len(cols) == 1 || len(cols[1]) < 1 {
client.ReplyNotEnoughParameters("PASS")
return
}
client.password = &cols[1]
2014-05-11 16:18:55 +00:00
case "NICK":
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, ":")
for existingClient := range clients {
if *existingClient.nickname == nickname {
client.ReplyParts("433", "*", nickname, "Nickname is already in use")
return
2014-05-11 16:18:55 +00:00
}
}
2014-08-09 12:42:19 +00:00
if !RENickname.MatchString(nickname) {
2014-05-11 16:18:55 +00:00
client.ReplyParts("432", "*", cols[1], "Erroneous nickname")
return
2014-05-11 16:18:55 +00:00
}
client.nickname = &nickname
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
}
client.username = &args[0]
realname := strings.TrimLeft(args[3], ":")
client.realname = &realname
2014-05-11 16:18:55 +00:00
}
if *client.nickname != "*" && *client.username != "" {
if passwords != nil && *passwords != "" {
if client.password == nil {
client.ReplyParts("462", "You may not register")
client.Close()
return
}
contents, err := ioutil.ReadFile(*passwords)
if err != nil {
log.Fatalf("Can no read passwords file %s: %s", *passwords, err)
return
}
for _, entry := range strings.Split(string(contents), "\n") {
if entry == "" {
continue
}
if lp := strings.Split(entry, ":"); lp[0] == *client.nickname && lp[1] != *client.password {
client.ReplyParts("462", "You may not register")
client.Close()
return
}
}
}
2014-05-11 16:18:55 +00:00
client.registered = true
client.ReplyNicknamed("001", "Hi, welcome to IRC")
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")
client.ReplyNicknamed("004", *hostname+" goircd o o")
SendLusers(client)
SendMotd(client)
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.
func RoomRegister(name string) (*Room, chan ClientEvent) {
roomNew := NewRoom(name)
2014-08-09 12:42:19 +00:00
roomSink := make(chan ClientEvent)
rooms[name] = roomNew
roomSinks[roomNew] = roomSink
2014-08-09 12:42:19 +00:00
go roomNew.Processor(roomSink)
roomsGroup.Add(1)
2014-08-09 12:42:19 +00:00
return roomNew, roomSink
2014-05-11 16:18:55 +00:00
}
func HandlerJoin(client *Client, cmd string) {
2014-05-11 16:18:55 +00:00
args := strings.Split(cmd, " ")
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 {
keys = make([]string, 0)
2014-05-11 16:18:55 +00:00
}
var roomExisting *Room
var roomSink chan ClientEvent
var roomNew *Room
for n, room := range rs {
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 = ""
}
for roomExisting, roomSink = range roomSinks {
if room == *roomExisting.name {
2015-10-13 06:37:03 +00:00
if (*roomExisting.key != "") && (*roomExisting.key != key) {
goto Denied
2014-05-11 16:18:55 +00:00
}
roomSink <- ClientEvent{client, EventNew, ""}
goto Joined
2014-05-11 16:18:55 +00:00
}
}
roomNew, roomSink = RoomRegister(room)
log.Println("Room", roomNew, "created")
2014-05-11 16:18:55 +00:00
if key != "" {
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, ""}
continue
Denied:
client.ReplyNicknamed("475", room, "Cannot join channel (+k) - bad key")
Joined:
2014-05-11 16:18:55 +00:00
}
}
func Processor(events chan ClientEvent, finished chan struct{}) {
2015-10-06 07:59:49 +00:00
var now time.Time
go func() {
for {
time.Sleep(10 * time.Second)
events <- ClientEvent{eventType: EventTick}
}
}()
2014-05-11 16:18:55 +00:00
for event := range events {
2015-10-06 07:59:49 +00:00
now = time.Now()
2014-08-09 11:37:10 +00:00
client := event.client
switch event.eventType {
case EventTick:
for c := range clients {
if c.recvTimestamp.Add(PingTimeout).Before(now) {
2014-05-11 16:18:55 +00:00
log.Println(c, "ping timeout")
c.Close()
2014-05-11 16:18:55 +00:00
continue
}
if c.sendTimestamp.Add(PingThreshold).Before(now) {
2014-05-11 16:18:55 +00:00
if c.registered {
c.Msg("PING :" + *hostname)
c.sendTimestamp = time.Now()
2014-05-11 16:18:55 +00:00
} else {
log.Println(c, "ping timeout")
c.Close()
2014-05-11 16:18:55 +00:00
}
}
}
for rn, r := range rooms {
if *statedir == "" && len(r.members) == 0 {
log.Println(rn, "emptied room")
delete(rooms, rn)
close(roomSinks[r])
delete(roomSinks, r)
}
}
case EventTerm:
for _, sink := range roomSinks {
sink <- ClientEvent{eventType: EventTerm}
2015-05-09 15:27:06 +00:00
}
roomsGroup.Wait()
close(finished)
return
case EventNew:
clients[client] = struct{}{}
2014-08-09 12:42:19 +00:00
case EventDel:
delete(clients, client)
for _, roomSink := range roomSinks {
2014-08-09 12:42:19 +00:00
roomSink <- event
2014-05-11 16:18:55 +00:00
}
2014-08-09 12:42:19 +00:00
case EventMsg:
2014-05-11 16:18:55 +00:00
cols := strings.SplitN(event.text, " ", 2)
cmd := strings.ToUpper(cols[0])
if *verbose {
log.Println(client, "command", cmd)
2014-06-08 00:49:27 +00:00
}
if cmd == "QUIT" {
log.Println(client, "quit")
client.Close()
2014-05-11 16:18:55 +00:00
continue
}
if !client.registered {
ClientRegister(client, cmd, cols)
2014-05-11 16:18:55 +00:00
continue
}
switch cmd {
2014-05-11 16:18:55 +00:00
case "AWAY":
2014-11-14 20:26:32 +00:00
if len(cols) == 1 {
client.away = nil
client.ReplyNicknamed("305", "You are no longer marked as being away")
continue
}
msg := strings.TrimLeft(cols[1], ":")
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
}
HandlerJoin(client, cols[1])
2014-05-11 16:18:55 +00:00
case "LIST":
SendList(client, cols)
2014-05-11 16:18:55 +00:00
case "LUSERS":
SendLusers(client)
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)
if cols[0] == *client.username {
2014-05-11 16:18:55 +00:00
if len(cols) == 1 {
client.Msg("221 " + *client.nickname + " +")
2014-05-11 16:18:55 +00:00
} else {
client.ReplyNicknamed("501", "Unknown MODE flag")
}
continue
}
room := cols[0]
r, found := rooms[room]
2014-05-11 16:18:55 +00:00
if !found {
client.ReplyNoChannel(room)
continue
}
if len(cols) == 1 {
roomSinks[r] <- ClientEvent{client, EventMode, ""}
2014-05-11 16:18:55 +00:00
} else {
roomSinks[r] <- ClientEvent{client, EventMode, cols[1]}
2014-05-11 16:18:55 +00:00
}
case "MOTD":
SendMotd(client)
2014-05-11 16:18:55 +00:00
case "PART":
if len(cols) == 1 || len(cols[1]) < 1 {
client.ReplyNotEnoughParameters("PART")
continue
}
rs := strings.Split(cols[1], " ")[0]
for _, room := range strings.Split(rs, ",") {
if r, found := rooms[room]; found {
roomSinks[r] <- ClientEvent{client, EventDel, ""}
} else {
2014-05-11 16:18:55 +00:00
client.ReplyNoChannel(room)
2014-05-18 11:26:15 +00:00
continue
2014-05-11 16:18:55 +00:00
}
}
case "PING":
if len(cols) == 1 {
client.ReplyNicknamed("409", "No origin specified")
continue
}
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 {
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 := ""
target := strings.ToLower(cols[0])
for c := range clients {
if *c.nickname == target {
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 {
client.ReplyNicknamed("301", *c.nickname, *c.away)
2014-11-14 20:26:32 +00:00
}
2014-05-11 16:18:55 +00:00
break
}
}
if msg != "" {
continue
}
if r, found := rooms[target]; found {
roomSinks[r] <- ClientEvent{
client,
EventMsg,
cmd + " " + strings.TrimLeft(cols[1], ":"),
}
} else {
2014-05-13 19:56:29 +00:00
client.ReplyNoNickChan(target)
2015-05-09 15:27:06 +00:00
}
2014-05-11 16:18:55 +00:00
case "TOPIC":
if len(cols) == 1 {
client.ReplyNotEnoughParameters("TOPIC")
continue
}
cols = strings.SplitN(cols[1], " ", 2)
r, found := rooms[cols[0]]
2014-05-11 16:18:55 +00:00
if !found {
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]
} else {
change = ""
}
roomSinks[r] <- ClientEvent{client, EventTopic, change}
2014-05-11 16:18:55 +00:00
case "WHO":
if len(cols) == 1 || len(cols[1]) < 1 {
client.ReplyNotEnoughParameters("WHO")
continue
}
room := strings.Split(cols[1], " ")[0]
if r, found := rooms[room]; found {
roomSinks[r] <- ClientEvent{client, EventWho, ""}
} else {
2014-05-11 16:18:55 +00:00
client.ReplyNoChannel(room)
}
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], ",")
SendWhois(client, nicknames)
2014-08-14 10:31:34 +00:00
case "VERSION":
var debug string
if *verbose {
2014-08-14 10:31:34 +00:00
debug = "debug"
} else {
debug = ""
}
client.ReplyNicknamed("351", fmt.Sprintf("%s.%s %s :", version, debug, *hostname))
2014-05-11 16:18:55 +00:00
default:
client.ReplyNicknamed("421", cmd, "Unknown command")
2014-05-11 16:18:55 +00:00
}
}
if client != nil {
client.recvTimestamp = now
2014-08-09 11:37:10 +00:00
}
2014-05-11 16:18:55 +00:00
}
}