goircd/goircd.go

137 lines
3.6 KiB
Go
Raw Normal View History

2014-05-11 16:18:55 +00:00
/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
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/>.
*/
package main
import (
2014-06-08 00:08:13 +00:00
"crypto/tls"
2014-05-11 16:18:55 +00:00
"flag"
2014-06-08 01:16:33 +00:00
"io/ioutil"
2014-05-11 16:18:55 +00:00
"log"
"net"
"path"
"path/filepath"
"strings"
)
var (
hostname = flag.String("hostname", "localhost", "Hostname")
bind = flag.String("bind", ":6667", "Address to bind to")
motd = flag.String("motd", "", "Path to MOTD file")
logdir = flag.String("logdir", "", "Absolute path to directory for logs")
statedir = flag.String("statedir", "", "Absolute path to directory for states")
2014-06-08 00:08:13 +00:00
tlsKey = flag.String("tls_key", "", "TLS keyfile")
tlsCert = flag.String("tls_cert", "", "TLS certificate")
2014-06-08 00:49:27 +00:00
verbose = flag.Bool("v", false, "Enable verbose logging.")
2014-05-11 16:18:55 +00:00
)
func Run() {
var client *Client
events := make(chan ClientEvent)
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
2014-08-09 12:42:19 +00:00
logSink := make(chan LogEvent)
2014-05-11 16:18:55 +00:00
if *logdir == "" {
// Dummy logger
go func() {
2014-08-09 12:42:19 +00:00
for _ = range logSink {
2014-05-11 16:18:55 +00:00
}
}()
} else {
if !path.IsAbs(*logdir) {
log.Fatalln("Need absolute path for logdir")
return
}
2014-08-09 12:42:19 +00:00
go Logger(*logdir, logSink)
2014-05-11 16:18:55 +00:00
log.Println(*logdir, "logger initialized")
}
2014-08-09 12:42:19 +00:00
stateSink := make(chan StateEvent)
daemon := NewDaemon(*hostname, *motd, logSink, stateSink)
2014-06-08 00:49:27 +00:00
daemon.Verbose = *verbose
2014-05-11 16:18:55 +00:00
if *statedir == "" {
// Dummy statekeeper
go func() {
2014-08-09 12:42:19 +00:00
for _ = range stateSink {
2014-05-11 16:18:55 +00:00
}
}()
} else {
if !path.IsAbs(*statedir) {
log.Fatalln("Need absolute path for statedir")
}
2014-06-08 01:16:33 +00:00
states, err := filepath.Glob(path.Join(*statedir, "#*"))
2014-05-11 16:18:55 +00:00
if err != nil {
log.Fatalln("Can not read statedir", err)
}
for _, state := range states {
2014-06-08 01:16:33 +00:00
buf, err := ioutil.ReadFile(state)
2014-05-11 16:18:55 +00:00
if err != nil {
2014-06-08 01:16:33 +00:00
log.Fatalf("Can not read state %s: %v", state, err)
2014-05-11 16:18:55 +00:00
}
room, _ := daemon.RoomRegister(path.Base(state))
contents := strings.Split(string(buf), "\n")
2014-06-08 01:16:33 +00:00
if len(contents) < 2 {
log.Printf("State corrupted for %s: %q", room.name, contents)
} else {
room.topic = contents[0]
room.key = contents[1]
log.Println("Loaded state for room", room.name)
}
2014-05-11 16:18:55 +00:00
}
2014-08-09 12:42:19 +00:00
go StateKeeper(*statedir, stateSink)
2014-05-11 16:18:55 +00:00
log.Println(*statedir, "statekeeper initialized")
}
2014-06-08 00:08:13 +00:00
var listener net.Listener
if *tlsKey != "" {
cert, err := tls.LoadX509KeyPair(*tlsCert, *tlsKey)
2014-06-08 00:08:13 +00:00
if err != nil {
log.Fatalf("Could not load TLS keys from %s and %s: %s", *tlsCert, *tlsKey, err)
2014-06-08 00:08:13 +00:00
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
listener, err = tls.Listen("tcp", *bind, &config)
if err != nil {
log.Fatalf("Can not listen on %s: %v", *bind, err)
}
} else {
var err error
listener, err = net.Listen("tcp", *bind)
if err != nil {
log.Fatalf("Can not listen on %s: %v", *bind, err)
}
2014-05-11 16:18:55 +00:00
}
log.Println("Listening on", *bind)
go daemon.Processor(events)
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error during accepting connection", err)
continue
}
client = NewClient(*hostname, conn)
go client.Processor(events)
}
}
func main() {
flag.Parse()
Run()
}