2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2021 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/>.
38 StateTopicFilename = "topic"
39 StateKeyFilename = "key"
51 type ClientEvent struct {
57 type StateEvent struct {
64 hostname = flag.String("hostname", "localhost", "hostname")
65 bind = flag.String("bind", "[::1]:6667", "address to bind to")
66 cloak = flag.String("cloak", "", "cloak user's host with the given hostname")
67 motd = flag.String("motd", "", "path to MOTD file")
68 logdir = flag.String("logdir", "", "absolute path to directory for logs")
69 statedir = flag.String("statedir", "", "absolute path to directory for states")
70 passwords = flag.String("passwd", "", "optional path to passwords file")
71 tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
72 tlsPEM = flag.String("tlspem", "", "path to TLS certificat+key PEM file")
73 permStateDirS = flag.String("perm-state-dir", "755", "state directory permissions")
74 permStateFileS = flag.String("perm-state-file", "600", "state files permissions")
75 permLogFileS = flag.String("perm-log-file", "644", "log files permissions")
76 timestamped = flag.Bool("timestamped", false, "enable timestamps on stderr messages")
77 verbose = flag.Bool("verbose", false, "enable verbose logging")
78 debug = flag.Bool("debug", false, "enable debug (traffic) logging")
80 permStateDir os.FileMode
81 permStateFile os.FileMode
82 permLogFile os.FileMode
84 stateSink chan StateEvent = make(chan StateEvent)
87 func permParse(s string) os.FileMode {
88 r, err := strconv.ParseUint(s, 8, 16)
95 func listenerLoop(ln net.Listener, events chan ClientEvent) {
97 conn, err := ln.Accept()
99 log.Println("error during accept", err)
102 NewClient(conn, events)
108 permStateDir = permParse(*permStateDirS)
109 permStateFile = permParse(*permStateFileS)
110 permLogFile = permParse(*permLogFileS)
113 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
115 log.SetFlags(log.Lshortfile)
117 log.SetOutput(os.Stdout)
126 if !path.IsAbs(*logdir) {
127 log.Fatalln("need absolute path for logdir")
129 go Logger(*logdir, logSink)
135 for range stateSink {
139 if !path.IsAbs(*statedir) {
140 log.Fatalln("need absolute path for statedir")
142 states, err := filepath.Glob(path.Join(*statedir, "#*"))
144 log.Fatalln("can not read statedir", err)
146 for _, state := range states {
147 buf, err := ioutil.ReadFile(path.Join(state, StateTopicFilename))
150 "can not read state %s/%s: %v",
151 state, StateTopicFilename, err,
154 room := RoomRegister(path.Base(state))
155 room.topic = strings.TrimRight(string(buf), "\n")
156 buf, err = ioutil.ReadFile(path.Join(state, StateKeyFilename))
158 room.key = strings.TrimRight(string(buf), "\n")
160 if !os.IsNotExist(err) {
162 "can not read state %s/%s: %v",
163 state, StateKeyFilename, err,
167 log.Println("loaded state for room:", room.name)
171 for event := range stateSink {
172 statePath := path.Join(*statedir, event.where)
173 if _, err := os.Stat(statePath); os.IsNotExist(err) {
174 if err := os.Mkdir(statePath, permStateDir); err != nil {
175 log.Printf("can not create state %s: %v", statePath, err)
180 topicPath := path.Join(statePath, StateTopicFilename)
181 if err := ioutil.WriteFile(
183 []byte(event.topic+"\n"),
186 log.Printf("can not write statefile %s: %v", topicPath, err)
190 keyPath := path.Join(statePath, StateKeyFilename)
191 if err := ioutil.WriteFile(
193 []byte(event.key+"\n"),
196 log.Printf("can not write statefile %s: %v", keyPath, err)
202 events := make(chan ClientEvent)
204 ln, err := net.Listen("tcp", *bind)
206 log.Fatalf("can not listen on %s: %v", *bind, err)
208 go listenerLoop(ln, events)
211 cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
213 log.Fatalf("can not load TLS keys from %s: %s", *tlsPEM, err)
215 config := tls.Config{Certificates: []tls.Certificate{cert}}
216 ln, err := tls.Listen("tcp", *tlsBind, &config)
218 log.Fatalf("can not listen on %s: %v", *tlsBind, err)
220 go listenerLoop(ln, events)
222 log.Println("goircd", Version, "started")
224 needsShutdown := make(chan os.Signal, 0)
225 signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
228 events <- ClientEvent{eventType: EventTerm}
229 log.Println("goircd shutting down")
232 finished := make(chan struct{})
233 go Processor(events, finished)