anonircd/main.go

110 lines
2.8 KiB
Go
Raw Normal View History

// AnonIRCd - Anonymous IRC daemon
2017-10-26 23:35:18 +00:00
// https://github.com/sageru-6ch/anonircd
2016-09-02 05:54:54 +00:00
//
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// 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/>.
2016-08-26 05:50:24 +00:00
package main
import (
"fmt"
2017-06-08 07:39:01 +00:00
"log"
2016-09-16 01:25:52 +00:00
"math/rand"
"net/http"
2017-06-08 07:39:01 +00:00
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
2016-08-26 05:50:24 +00:00
"time"
2016-09-01 06:35:27 +00:00
"github.com/jessevdk/go-flags"
2017-12-11 11:01:24 +00:00
"github.com/pkg/errors"
"gopkg.in/sorcix/irc.v2"
2016-09-02 05:54:54 +00:00
)
2016-08-26 05:50:24 +00:00
var prefixAnonIRC = irc.Prefix{Name: "AnonIRC"}
2017-12-15 01:39:18 +00:00
var prefixAnonymous = irc.Prefix{Name: "Anonymous", User: "Anon", Host: "IRC"}
2016-08-31 08:44:40 +00:00
const DEFAULT_MOTD = `
2016-08-31 08:44:40 +00:00
_|_| _|_|_| _|_|_| _|_|_|
_| _| _|_|_| _|_| _|_|_| _| _| _| _|
_|_|_|_| _| _| _| _| _| _| _| _|_|_| _|
_| _| _| _| _| _| _| _| _| _| _| _|
_| _| _| _| _|_| _| _| _|_|_| _| _| _|_|_|
`
2016-09-02 05:54:54 +00:00
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const writebuffersize = 10
2016-08-26 05:50:24 +00:00
const (
2017-12-11 11:01:24 +00:00
CHANNEL_LOBBY = "#"
CHANNEL_SERVER = "&"
)
var debugMode = false
var verbose = false
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var opts struct {
ConfigFile string `short:"c" long:"config" description:"Configuration file"`
Debug int `short:"d" long:"debug" description:"Serve pprof data on specified port"`
BareLog bool `short:"b" long:"bare-log" description:"Don't add current date/time to log entries"`
Verbose bool `short:"v" long:"verbose" description:"Log verbosely"`
}
_, err := flags.Parse(&opts)
if err != nil {
2017-12-11 11:01:24 +00:00
log.Panicf("%+v", errors.Wrap(err, "failed to parse flags"))
}
if opts.Debug > 0 {
debugMode = true
log.Printf("WARNING: Running in debug mode. pprof data is available at http://localhost:%d/debug/pprof/", opts.Debug)
go http.ListenAndServe(fmt.Sprintf("localhost:%d", opts.Debug), nil)
2016-08-26 05:50:24 +00:00
}
if opts.BareLog {
log.SetFlags(0)
}
verbose = opts.Verbose
2016-08-31 08:44:40 +00:00
s := NewServer(opts.ConfigFile)
err = s.loadConfig()
if err != nil {
2017-12-11 11:01:24 +00:00
log.Panicf("%+v", errors.Wrap(err, "failed to load configuration file"))
}
s.connectDatabase()
defer s.closeDatabase()
sighup := make(chan os.Signal, 1)
signal.Notify(sighup,
syscall.SIGHUP)
go func() {
2017-09-27 21:21:18 +00:00
<-sighup
err := s.reload()
if err != nil {
log.Println(err)
}
}()
2016-09-02 07:49:36 +00:00
s.odyssey, err = os.Open("ODYSSEY")
2017-06-08 07:39:01 +00:00
if err != nil {
log.Fatal(err)
}
defer s.odyssey.Close()
2017-06-08 07:39:01 +00:00
s.listen()
2016-09-01 06:35:27 +00:00
}