]> Cypherpunks.ru repositories - goircd.git/blobdiff - goircd.go
Forbid any later GNU GPL versions autousage
[goircd.git] / goircd.go
index 984b47b94144c134e7bfc57158b52de13dddbf84..240f0e6eaf49431bde8d00cfb587dca1a48ae0ae 100644 (file)
--- a/goircd.go
+++ b/goircd.go
@@ -1,11 +1,10 @@
 /*
 goircd -- minimalistic simple Internet Relay Chat (IRC) server
-Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, version 3 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,6 +14,7 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
+
 package main
 
 import (
@@ -23,34 +23,40 @@ import (
        "io/ioutil"
        "log"
        "net"
-       "os"
-       "os/signal"
        "path"
        "path/filepath"
        "strings"
-       "syscall"
 )
 
 var (
+       version   string
        hostname  = flag.String("hostname", "localhost", "Hostname")
        bind      = flag.String("bind", ":6667", "Address to bind to")
        motd      = flag.String("motd", "", "Path to MOTD file")
        logdir    = flag.String("logdir", "", "Absolute path to directory for logs")
        statedir  = flag.String("statedir", "", "Absolute path to directory for states")
        passwords = flag.String("passwords", "", "Optional path to passwords file")
-
-       tlsKey  = flag.String("tls_key", "", "TLS keyfile")
-       tlsCert = flag.String("tls_cert", "", "TLS certificate")
-
-       verbose = flag.Bool("v", false, "Enable verbose logging.")
+       tlsBind   = flag.String("tlsbind", "", "TLS address to bind to")
+       tlsPEM    = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
+       verbose   = flag.Bool("v", false, "Enable verbose logging.")
 )
 
+func listenerLoop(sock net.Listener, events chan ClientEvent) {
+       for {
+               conn, err := sock.Accept()
+               if err != nil {
+                       log.Println("Error during accepting connection", err)
+                       continue
+               }
+               client := NewClient(conn)
+               go client.Processor(events)
+       }
+}
+
 func Run() {
-       var client *Client
        events := make(chan ClientEvent)
        log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
 
-       logSink := make(chan LogEvent)
        if *logdir == "" {
                // Dummy logger
                go func() {
@@ -60,15 +66,12 @@ func Run() {
        } else {
                if !path.IsAbs(*logdir) {
                        log.Fatalln("Need absolute path for logdir")
-                       return
                }
                go Logger(*logdir, logSink)
                log.Println(*logdir, "logger initialized")
        }
 
-       stateSink := make(chan StateEvent)
-       daemon := NewDaemon(*hostname, *motd, logSink, stateSink)
-       daemon.Verbose = *verbose
+       log.Println("goircd " + version + " is starting")
        if *statedir == "" {
                // Dummy statekeeper
                go func() {
@@ -88,62 +91,42 @@ func Run() {
                        if err != nil {
                                log.Fatalf("Can not read state %s: %v", state, err)
                        }
-                       room, _ := daemon.RoomRegister(path.Base(state))
+                       room, _ := RoomRegister(path.Base(state))
                        contents := strings.Split(string(buf), "\n")
                        if len(contents) < 2 {
-                               log.Printf("State corrupted for %s: %q", room.name, contents)
+                               log.Printf("State corrupted for %s: %q", *room.name, contents)
                        } else {
-                               room.topic = contents[0]
-                               room.key = contents[1]
-                               log.Println("Loaded state for room", room.name)
+                               room.topic = &contents[0]
+                               room.key = &contents[1]
+                               log.Println("Loaded state for room", *room.name)
                        }
                }
                go StateKeeper(*statedir, stateSink)
                log.Println(*statedir, "statekeeper initialized")
        }
 
-       var listener net.Listener
-       if *tlsKey != "" {
-               cert, err := tls.LoadX509KeyPair(*tlsCert, *tlsKey)
-               if err != nil {
-                       log.Fatalf("Could not load TLS keys from %s and %s: %s", *tlsCert, *tlsKey, err)
-               }
-               config := tls.Config{Certificates: []tls.Certificate{cert}}
-               listener, err = tls.Listen("tcp", *bind, &config)
+       if *bind != "" {
+               listener, err := net.Listen("tcp", *bind)
                if err != nil {
                        log.Fatalf("Can not listen on %s: %v", *bind, err)
                }
-       } else {
-               var err error
-               listener, err = net.Listen("tcp", *bind)
+               log.Println("Raw listening on", *bind)
+               go listenerLoop(listener, events)
+       }
+       if *tlsBind != "" {
+               cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
                if err != nil {
-                       log.Fatalf("Can not listen on %s: %v", *bind, err)
+                       log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
                }
-       }
-       log.Println("Listening on", *bind)
-
-       if *passwords != "" {
-               daemon.PasswordsRefresh()
-               hups := make(chan os.Signal)
-               signal.Notify(hups, syscall.SIGHUP)
-               go func() {
-                       for {
-                               <-hups
-                               daemon.PasswordsRefresh()
-                       }
-               }()
-       }
-
-       go daemon.Processor(events)
-       for {
-               conn, err := listener.Accept()
+               config := tls.Config{Certificates: []tls.Certificate{cert}}
+               listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
                if err != nil {
-                       log.Println("Error during accepting connection", err)
-                       continue
+                       log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
                }
-               client = NewClient(*hostname, conn)
-               go client.Processor(events)
+               log.Println("TLS listening on", *tlsBind)
+               go listenerLoop(listenerTLS, events)
        }
+       Processor(events, make(chan struct{}))
 }
 
 func main() {