2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 hostname = flag.String("hostname", "localhost", "Hostname")
33 bind = flag.String("bind", ":6667", "Address to bind to")
34 motd = flag.String("motd", "", "Path to MOTD file")
35 logdir = flag.String("logdir", "", "Absolute path to directory for logs")
36 statedir = flag.String("statedir", "", "Absolute path to directory for states")
38 ssl = flag.Bool("ssl", false, "Use SSL only.")
39 sslKey = flag.String("ssl_key", "", "SSL keyfile.")
40 sslCert = flag.String("ssl_cert", "", "SSL certificate.")
42 verbose = flag.Bool("v", false, "Enable verbose logging.")
47 events := make(chan ClientEvent)
48 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
50 log_sink := make(chan LogEvent)
54 for _ = range log_sink {
58 if !path.IsAbs(*logdir) {
59 log.Fatalln("Need absolute path for logdir")
62 go Logger(*logdir, log_sink)
63 log.Println(*logdir, "logger initialized")
66 state_sink := make(chan StateEvent)
67 daemon := NewDaemon(*hostname, *motd, log_sink, state_sink)
68 daemon.Verbose = *verbose
72 for _ = range state_sink {
76 if !path.IsAbs(*statedir) {
77 log.Fatalln("Need absolute path for statedir")
79 states, err := filepath.Glob(path.Join(*statedir, "#*"))
81 log.Fatalln("Can not read statedir", err)
83 for _, state := range states {
84 buf, err := ioutil.ReadFile(state)
86 log.Fatalf("Can not read state %s: %v", state, err)
88 room, _ := daemon.RoomRegister(path.Base(state))
89 contents := strings.Split(string(buf), "\n")
90 if len(contents) < 2 {
91 log.Printf("State corrupted for %s: %q", room.name, contents)
93 room.topic = contents[0]
94 room.key = contents[1]
95 log.Println("Loaded state for room", room.name)
98 go StateKeeper(*statedir, state_sink)
99 log.Println(*statedir, "statekeeper initialized")
102 var listener net.Listener
104 cert, err := tls.LoadX509KeyPair(*sslCert, *sslKey)
106 log.Fatalf("Could not load SSL keys from %s and %s: %s", *sslCert, *sslKey, err)
108 config := tls.Config{Certificates: []tls.Certificate{cert}}
109 listener, err = tls.Listen("tcp", *bind, &config)
111 log.Fatalf("Can not listen on %s: %v", *bind, err)
115 listener, err = net.Listen("tcp", *bind)
117 log.Fatalf("Can not listen on %s: %v", *bind, err)
120 log.Println("Listening on", *bind)
122 go daemon.Processor(events)
124 conn, err := listener.Accept()
126 log.Println("Error during accepting connection", err)
129 client = NewClient(*hostname, conn)
130 go client.Processor(events)