2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 const Version = "1.8.2"
34 hostname = flag.String("hostname", "localhost", "Hostname")
35 bind = flag.String("bind", ":6667", "Address to bind to")
36 motd = flag.String("motd", "", "Path to MOTD file")
37 logdir = flag.String("logdir", "", "Absolute path to directory for logs")
38 statedir = flag.String("statedir", "", "Absolute path to directory for states")
39 passwords = flag.String("passwords", "", "Optional path to passwords file")
40 tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
41 tlsPEM = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
42 verbose = flag.Bool("v", false, "Enable verbose logging.")
45 func listenerLoop(sock net.Listener, events chan ClientEvent) {
47 conn, err := sock.Accept()
49 log.Println("Error during accepting connection", err)
52 client := NewClient(conn)
53 go client.Processor(events)
58 events := make(chan ClientEvent)
59 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
64 for _ = range logSink {
68 if !path.IsAbs(*logdir) {
69 log.Fatalln("Need absolute path for logdir")
71 go Logger(*logdir, logSink)
72 log.Println(*logdir, "logger initialized")
75 log.Println("goircd " + Version + " is starting")
79 for _ = range stateSink {
83 if !path.IsAbs(*statedir) {
84 log.Fatalln("Need absolute path for statedir")
86 states, err := filepath.Glob(path.Join(*statedir, "#*"))
88 log.Fatalln("Can not read statedir", err)
90 for _, state := range states {
91 buf, err := ioutil.ReadFile(state)
93 log.Fatalf("Can not read state %s: %v", state, err)
95 room, _ := RoomRegister(path.Base(state))
96 contents := strings.Split(string(buf), "\n")
97 if len(contents) < 2 {
98 log.Printf("State corrupted for %s: %q", *room.name, contents)
100 room.topic = &contents[0]
101 room.key = &contents[1]
102 log.Println("Loaded state for room", *room.name)
105 go StateKeeper(*statedir, stateSink)
106 log.Println(*statedir, "statekeeper initialized")
110 listener, err := net.Listen("tcp", *bind)
112 log.Fatalf("Can not listen on %s: %v", *bind, err)
114 log.Println("Raw listening on", *bind)
115 go listenerLoop(listener, events)
118 cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
120 log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
122 config := tls.Config{Certificates: []tls.Certificate{cert}}
123 listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
125 log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
127 log.Println("TLS listening on", *tlsBind)
128 go listenerLoop(listenerTLS, events)
130 Processor(events, make(chan struct{}))