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/>.
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")
39 ssl = flag.Bool("ssl", false, "Use SSL only.")
40 sslKey = flag.String("ssl_key", "", "SSL keyfile.")
41 sslCert = flag.String("ssl_cert", "", "SSL certificate.")
46 events := make(chan ClientEvent)
47 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
49 log_sink := make(chan LogEvent)
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)
76 if !path.IsAbs(*statedir) {
77 log.Fatalln("Need absolute path for statedir")
79 states, err := filepath.Glob(*statedir + "/#*")
81 log.Fatalln("Can not read statedir", err)
83 for _, state := range states {
84 fd, err := os.Open(state)
86 log.Fatalln("Can not open state", state, err)
88 buf := make([]byte, 1024)
92 log.Fatalln("Can not read state", state, err)
94 room, _ := daemon.RoomRegister(path.Base(state))
95 buf = bytes.TrimRight(buf, "\x00")
96 contents := strings.Split(string(buf), "\n")
97 room.topic = contents[0]
98 room.key = contents[1]
99 log.Println("Loaded state for room", room.name)
101 go StateKeeper(*statedir, state_sink)
102 log.Println(*statedir, "statekeeper initialized")
105 var listener net.Listener
107 cert, err := tls.LoadX509KeyPair(*sslCert, *sslKey)
109 log.Fatalf("Could not load SSL keys from %s and %s: %s", *sslCert, *sslKey, err)
111 config := tls.Config{Certificates: []tls.Certificate{cert}}
112 listener, err = tls.Listen("tcp", *bind, &config)
114 log.Fatalf("Can not listen on %s: %v", *bind, err)
118 listener, err = net.Listen("tcp", *bind)
120 log.Fatalf("Can not listen on %s: %v", *bind, err)
123 log.Println("Listening on", *bind)
125 go daemon.Processor(events)
127 conn, err := listener.Accept()
129 log.Println("Error during accepting connection", err)
132 client = NewClient(*hostname, conn)
133 go client.Processor(events)