]> Cypherpunks.ru repositories - goircd.git/blob - goircd.go
deb6e3542feca9d32a1f494f3ffefdb5f0415403
[goircd.git] / goircd.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package main
19
20 import (
21         "crypto/tls"
22         "flag"
23         "io/ioutil"
24         "log"
25         "net"
26         "path"
27         "path/filepath"
28         "strings"
29 )
30
31 const Version = "1.8.2"
32
33 var (
34         hostname  = flag.String("hostname", "localhost", "Hostname")
35         bind      = flag.String("bind", ":6667", "Address to bind to")
36         motd      = flag.String("motd", "", "Path to MOTD file")
37         logdir    = flag.String("logdir", "", "Absolute path to directory for logs")
38         statedir  = flag.String("statedir", "", "Absolute path to directory for states")
39         passwords = flag.String("passwords", "", "Optional path to passwords file")
40         tlsBind   = flag.String("tlsbind", "", "TLS address to bind to")
41         tlsPEM    = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
42         verbose   = flag.Bool("v", false, "Enable verbose logging.")
43 )
44
45 func listenerLoop(sock net.Listener, events chan ClientEvent) {
46         for {
47                 conn, err := sock.Accept()
48                 if err != nil {
49                         log.Println("Error during accepting connection", err)
50                         continue
51                 }
52                 client := NewClient(conn)
53                 go client.Processor(events)
54         }
55 }
56
57 func Run() {
58         events := make(chan ClientEvent)
59         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
60
61         if *logdir == "" {
62                 // Dummy logger
63                 go func() {
64                         for _ = range logSink {
65                         }
66                 }()
67         } else {
68                 if !path.IsAbs(*logdir) {
69                         log.Fatalln("Need absolute path for logdir")
70                 }
71                 go Logger(*logdir, logSink)
72                 log.Println(*logdir, "logger initialized")
73         }
74
75         log.Println("goircd " + Version + " is starting")
76         if *statedir == "" {
77                 // Dummy statekeeper
78                 go func() {
79                         for _ = range stateSink {
80                         }
81                 }()
82         } else {
83                 if !path.IsAbs(*statedir) {
84                         log.Fatalln("Need absolute path for statedir")
85                 }
86                 states, err := filepath.Glob(path.Join(*statedir, "#*"))
87                 if err != nil {
88                         log.Fatalln("Can not read statedir", err)
89                 }
90                 for _, state := range states {
91                         buf, err := ioutil.ReadFile(state)
92                         if err != nil {
93                                 log.Fatalf("Can not read state %s: %v", state, err)
94                         }
95                         room, _ := RoomRegister(path.Base(state))
96                         contents := strings.Split(string(buf), "\n")
97                         if len(contents) < 2 {
98                                 log.Printf("State corrupted for %s: %q", *room.name, contents)
99                         } else {
100                                 room.topic = &contents[0]
101                                 room.key = &contents[1]
102                                 log.Println("Loaded state for room", *room.name)
103                         }
104                 }
105                 go StateKeeper(*statedir, stateSink)
106                 log.Println(*statedir, "statekeeper initialized")
107         }
108
109         if *bind != "" {
110                 listener, err := net.Listen("tcp", *bind)
111                 if err != nil {
112                         log.Fatalf("Can not listen on %s: %v", *bind, err)
113                 }
114                 log.Println("Raw listening on", *bind)
115                 go listenerLoop(listener, events)
116         }
117         if *tlsBind != "" {
118                 cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
119                 if err != nil {
120                         log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
121                 }
122                 config := tls.Config{Certificates: []tls.Certificate{cert}}
123                 listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
124                 if err != nil {
125                         log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
126                 }
127                 log.Println("TLS listening on", *tlsBind)
128                 go listenerLoop(listenerTLS, events)
129         }
130         Processor(events, make(chan struct{}))
131 }
132
133 func main() {
134         flag.Parse()
135         Run()
136 }