summaryrefslogtreecommitdiff
path: root/goircd.go
blob: 6991e8cb8ad901dd29a9fd25d0ab7f4a0d0f7e66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
goircd -- minimalistic simple Internet Relay Chat (IRC) server
Copyright (C) 2014-2018 | wn 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 (
	"crypto/tls"
	"flag"
	"io/ioutil"
	"log"
	"net"
	"path"
	"path/filepath"
	"strings"
)

var (
	version   string
	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")
	passwords = flag.String("passwords", "", "Optional path to passwords file")
	tlsBind   = flag.String("tlsbind", "", "TLS address to bind to")
	tlsPEM    = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
	verbose   = flag.Bool("v", false, "Enable verbose logging.")
)

func listenerLoop(sock net.Listener, events chan ClientEvent) {
	for {
		conn, err := sock.Accept()
		if err != nil {
			log.Println("Error during accepting connection", err)
			continue
		}
		client := NewClient(conn)
		go client.Processor(events)
	}
}

func Run() {
	events := make(chan ClientEvent)
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)

	if *logdir == "" {
		// Dummy logger
		go func() {
			for _ = range logSink {
			}
		}()
	} else {
		if !path.IsAbs(*logdir) {
			log.Fatalln("Need absolute path for logdir")
		}
		go Logger(*logdir, logSink)
		log.Println(*logdir, "logger initialized")
	}

	log.Println("goircd " + version + " is starting")
	if *statedir == "" {
		// Dummy statekeeper
		go func() {
			for _ = range stateSink {
			}
		}()
	} else {
		if !path.IsAbs(*statedir) {
			log.Fatalln("Need absolute path for statedir")
		}
		states, err := filepath.Glob(path.Join(*statedir, "#*"))
		if err != nil {
			log.Fatalln("Can not read statedir", err)
		}
		for _, state := range states {
			buf, err := ioutil.ReadFile(state)
			if err != nil {
				log.Fatalf("Can not read state %s: %v", state, err)
			}
			room, _ := RoomRegister(path.Base(state))
			contents := strings.Split(string(buf), "\n")
			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)
			}
		}
		go StateKeeper(*statedir, stateSink)
		log.Println(*statedir, "statekeeper initialized")
	}

	if *bind != "" {
		listener, err := net.Listen("tcp", *bind)
		if err != nil {
			log.Fatalf("Can not listen on %s: %v", *bind, err)
		}
		log.Println("Raw listening on", *bind)
		go listenerLoop(listener, events)
	}
	if *tlsBind != "" {
		cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
		if err != nil {
			log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
		}
		config := tls.Config{Certificates: []tls.Certificate{cert}}
		listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
		if err != nil {
			log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
		}
		log.Println("TLS listening on", *tlsBind)
		go listenerLoop(listenerTLS, events)
	}
	Processor(events, make(chan struct{}))
}

func main() {
	flag.Parse()
	Run()
}