1 // goircd -- minimalistic simple Internet Relay Chat (IRC) server
2 // Copyright (C) 2014-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
35 StateTopicFilename = "topic"
36 StateKeyFilename = "key"
48 type ClientEvent struct {
54 type StateEvent struct {
61 hostname = flag.String("hostname", "localhost", "hostname")
62 bind = flag.String("bind", "[::1]:6667", "address to bind to")
63 cloak = flag.String("cloak", "", "cloak user's host with the given hostname")
64 motd = flag.String("motd", "", "path to MOTD file")
65 logdir = flag.String("logdir", "", "absolute path to directory for logs")
66 statedir = flag.String("statedir", "", "absolute path to directory for states")
67 passwords = flag.String("passwd", "", "optional path to passwords file")
68 tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
69 tlsPEM = flag.String("tlspem", "", "path to TLS certificat+key PEM file")
70 permStateDirS = flag.String("perm-state-dir", "755", "state directory permissions")
71 permStateFileS = flag.String("perm-state-file", "600", "state files permissions")
72 permLogFileS = flag.String("perm-log-file", "644", "log files permissions")
73 timestamped = flag.Bool("timestamped", false, "enable timestamps on stderr messages")
74 verbose = flag.Bool("verbose", false, "enable verbose logging")
75 debug = flag.Bool("debug", false, "enable debug (traffic) logging")
77 permStateDir os.FileMode
78 permStateFile os.FileMode
79 permLogFile os.FileMode
81 stateSink chan StateEvent = make(chan StateEvent)
84 func permParse(s string) os.FileMode {
85 r, err := strconv.ParseUint(s, 8, 16)
92 func listenerLoop(ln net.Listener, events chan ClientEvent) {
94 conn, err := ln.Accept()
96 log.Println("error during accept", err)
99 NewClient(conn, events)
105 permStateDir = permParse(*permStateDirS)
106 permStateFile = permParse(*permStateFileS)
107 permLogFile = permParse(*permLogFileS)
110 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
112 log.SetFlags(log.Lshortfile)
114 log.SetOutput(os.Stdout)
123 if !path.IsAbs(*logdir) {
124 log.Fatalln("need absolute path for logdir")
126 go Logger(*logdir, logSink)
132 for range stateSink {
136 if !path.IsAbs(*statedir) {
137 log.Fatalln("need absolute path for statedir")
139 states, err := filepath.Glob(path.Join(*statedir, "#*"))
141 log.Fatalln("can not read statedir", err)
143 for _, state := range states {
144 buf, err := os.ReadFile(path.Join(state, StateTopicFilename))
147 "can not read state %s/%s: %v",
148 state, StateTopicFilename, err,
151 room := RoomRegister(path.Base(state))
152 room.topic = strings.TrimRight(string(buf), "\n")
153 buf, err = os.ReadFile(path.Join(state, StateKeyFilename))
155 room.key = strings.TrimRight(string(buf), "\n")
157 if !os.IsNotExist(err) {
159 "can not read state %s/%s: %v",
160 state, StateKeyFilename, err,
164 log.Println("loaded state for room:", room.name)
168 for event := range stateSink {
169 statePath := path.Join(*statedir, event.where)
170 if _, err := os.Stat(statePath); os.IsNotExist(err) {
171 if err := os.Mkdir(statePath, permStateDir); err != nil {
172 log.Printf("can not create state %s: %v", statePath, err)
177 topicPath := path.Join(statePath, StateTopicFilename)
178 if err := os.WriteFile(
180 []byte(event.topic+"\n"),
183 log.Printf("can not write statefile %s: %v", topicPath, err)
187 keyPath := path.Join(statePath, StateKeyFilename)
188 if err := os.WriteFile(
190 []byte(event.key+"\n"),
193 log.Printf("can not write statefile %s: %v", keyPath, err)
199 events := make(chan ClientEvent)
201 ln, err := net.Listen("tcp", *bind)
203 log.Fatalf("can not listen on %s: %v", *bind, err)
205 go listenerLoop(ln, events)
208 cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
210 log.Fatalf("can not load TLS keys from %s: %s", *tlsPEM, err)
212 config := tls.Config{Certificates: []tls.Certificate{cert}}
213 ln, err := tls.Listen("tcp", *tlsBind, &config)
215 log.Fatalf("can not listen on %s: %v", *tlsBind, err)
217 go listenerLoop(ln, events)
219 log.Println("goircd", Version, "started")
221 needsShutdown := make(chan os.Signal, 1)
222 signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
225 events <- ClientEvent{eventType: EventTerm}
226 log.Println("goircd shutting down")
229 finished := make(chan struct{})
230 go Processor(events, finished)