2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2019 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/>.
33 hostname = flag.String("hostname", "localhost", "Hostname")
34 bind = flag.String("bind", ":6667", "Address to bind to")
35 motd = flag.String("motd", "", "Path to MOTD file")
36 logdir = flag.String("logdir", "", "Absolute path to directory for logs")
37 statedir = flag.String("statedir", "", "Absolute path to directory for states")
38 passwords = flag.String("passwords", "", "Optional path to passwords file")
39 tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
40 tlsPEM = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
41 verbose = flag.Bool("v", false, "Enable verbose logging.")
44 func listenerLoop(sock net.Listener, events chan ClientEvent) {
46 conn, err := sock.Accept()
48 log.Println("Error during accepting connection", err)
51 client := NewClient(conn)
52 go client.Processor(events)
57 events := make(chan ClientEvent)
58 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
63 for _ = range logSink {
67 if !path.IsAbs(*logdir) {
68 log.Fatalln("Need absolute path for logdir")
70 go Logger(*logdir, logSink)
71 log.Println(*logdir, "logger initialized")
74 log.Println("goircd " + version + " is starting")
78 for _ = range stateSink {
82 if !path.IsAbs(*statedir) {
83 log.Fatalln("Need absolute path for statedir")
85 states, err := filepath.Glob(path.Join(*statedir, "#*"))
87 log.Fatalln("Can not read statedir", err)
89 for _, state := range states {
90 buf, err := ioutil.ReadFile(state)
92 log.Fatalf("Can not read state %s: %v", state, err)
94 room, _ := RoomRegister(path.Base(state))
95 contents := strings.Split(string(buf), "\n")
96 if len(contents) < 2 {
97 log.Printf("State corrupted for %s: %q", *room.name, contents)
99 room.topic = &contents[0]
100 room.key = &contents[1]
101 log.Println("Loaded state for room", *room.name)
104 go StateKeeper(*statedir, stateSink)
105 log.Println(*statedir, "statekeeper initialized")
109 listener, err := net.Listen("tcp", *bind)
111 log.Fatalf("Can not listen on %s: %v", *bind, err)
113 log.Println("Raw listening on", *bind)
114 go listenerLoop(listener, events)
117 cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
119 log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
121 config := tls.Config{Certificates: []tls.Certificate{cert}}
122 listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
124 log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
126 log.Println("TLS listening on", *tlsBind)
127 go listenerLoop(listenerTLS, events)
129 Processor(events, make(chan struct{}))