]> Cypherpunks.ru repositories - goircd.git/blob - goircd.go
Additional unittests
[goircd.git] / goircd.go
1 /*
2 goircd -- minimalistic simple Internet Relay Chat (IRC) server
3 Copyright (C) 2014 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, either version 3 of the License, or
8 (at your option) any later version.
9
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.
14
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/>.
17 */
18 package main
19
20 import (
21         "bytes"
22         "flag"
23         "log"
24         "net"
25         "os"
26         "path"
27         "path/filepath"
28         "strings"
29 )
30
31 var (
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")
37 )
38
39 func Run() {
40         var client *Client
41         events := make(chan ClientEvent)
42         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
43
44         log_sink := make(chan LogEvent)
45         if *logdir == "" {
46                 // Dummy logger
47                 go func() {
48                         for {
49                                 <-log_sink
50                         }
51                 }()
52         } else {
53                 if !path.IsAbs(*logdir) {
54                         log.Fatalln("Need absolute path for logdir")
55                         return
56                 }
57                 go Logger(*logdir, log_sink)
58                 log.Println(*logdir, "logger initialized")
59         }
60
61         state_sink := make(chan StateEvent)
62         daemon := NewDaemon(*hostname, *motd, log_sink, state_sink)
63         if *statedir == "" {
64                 // Dummy statekeeper
65                 go func() {
66                         for {
67                                 <-state_sink
68                         }
69                 }()
70         } else {
71                 if !path.IsAbs(*statedir) {
72                         log.Fatalln("Need absolute path for statedir")
73                 }
74                 states, err := filepath.Glob(*statedir + "/#*")
75                 if err != nil {
76                         log.Fatalln("Can not read statedir", err)
77                 }
78                 for _, state := range states {
79                         fd, err := os.Open(state)
80                         if err != nil {
81                                 log.Fatalln("Can not open state", state, err)
82                         }
83                         buf := make([]byte, 1024)
84                         _, err = fd.Read(buf)
85                         fd.Close()
86                         if err != nil {
87                                 log.Fatalln("Can not read state", state, err)
88                         }
89                         room, _ := daemon.RoomRegister(path.Base(state))
90                         buf = bytes.TrimRight(buf, "\x00")
91                         contents := strings.Split(string(buf), "\n")
92                         room.topic = contents[0]
93                         room.key = contents[1]
94                         log.Println("Loaded state for room", room.name)
95                 }
96                 go StateKeeper(*statedir, state_sink)
97                 log.Println(*statedir, "statekeeper initialized")
98         }
99
100         listener, err := net.Listen("tcp", *bind)
101         if err != nil {
102                 log.Fatalln("Can not listen on ", *bind)
103         }
104         log.Println("Listening on", *bind)
105
106         go daemon.Processor(events)
107         for {
108                 conn, err := listener.Accept()
109                 if err != nil {
110                         log.Println("Error during accepting connection", err)
111                         continue
112                 }
113                 client = NewClient(*hostname, conn)
114                 go client.Processor(events)
115         }
116 }
117
118 func main() {
119         flag.Parse()
120         Run()
121 }