summaryrefslogtreecommitdiff
path: root/webircgateway/main.go
blob: efd812bf0071c15e7ddcc0a7aff5c9a95879916f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"plugin"
	"sync"
	"syscall"

	"github.com/kiwiirc/webircgateway/pkg/webircgateway"
)

var VERSION = "1.1.0"
var GITCOMMIT = "-"
var BUILTWITHGO = "-"

func init() {
	webircgateway.Version = VERSION
}

func main() {
	printVersion := flag.Bool("version", false, "Print the version")
	configFile := flag.String("config", "config.conf", "Config file location")
	startSection := flag.String("run", "gateway", "What type of server to run")
	flag.Parse()

	if *printVersion {
		fmt.Printf("Version: %s\n", webircgateway.Version)
		fmt.Printf("Git commit: %s\n", GITCOMMIT)
		fmt.Printf("Built with Go version: %s\n", BUILTWITHGO)
		os.Exit(0)
	}

	if *startSection != "gateway" && *startSection != "proxy" {
		fmt.Println("-run can either be 'gateway' or 'proxy'")
		os.Exit(1)
	}

	runGateway(*configFile, *startSection)
}

func runGateway(configFile string, function string) {
	gateway := webircgateway.NewGateway(function)

	log.SetFlags(log.Flags() | log.Lmicroseconds)

	// Print any webircgateway logout to STDOUT
	go printLogOutput(gateway)

	// Listen for process signals
	go watchForSignals(gateway)

	gateway.Config.SetConfigFile(configFile)
	log.Printf("Using config %s", gateway.Config.CurrentConfigFile())

	configErr := gateway.Config.Load()
	if configErr != nil {
		log.Printf("Config file error: %s", configErr.Error())
		os.Exit(1)
	}

	pluginsQuit := &sync.WaitGroup{}
	loadPlugins(gateway, pluginsQuit)

	gateway.Start()

	pluginsQuit.Wait()
	gateway.WaitClose()
}

func watchForSignals(gateway *webircgateway.Gateway) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGINT)

	for {
		switch sig := <-c; sig {
		case syscall.SIGINT:
			fmt.Println("Received SIGINT, shutting down webircgateway")
			gateway.Close()
		case syscall.SIGHUP:
			fmt.Println("Recieved SIGHUP, reloading config file")
			gateway.Config.Load()
		}
	}
}

func printLogOutput(gateway *webircgateway.Gateway) {
	for {
		line, _ := <-gateway.LogOutput
		log.Println(line)
	}
}

func loadPlugins(gateway *webircgateway.Gateway, pluginsQuit *sync.WaitGroup) {
	for _, pluginPath := range gateway.Config.Plugins {
		pluginFullPath := gateway.Config.ResolvePath(pluginPath)

		gateway.Log(2, "Loading plugin "+pluginFullPath)
		p, err := plugin.Open(pluginFullPath)
		if err != nil {
			gateway.Log(3, "Error loading plugin: "+err.Error())
			continue
		}

		startSymbol, err := p.Lookup("Start")
		if err != nil {
			gateway.Log(3, "Plugin does not export a Start function! (%s)", pluginFullPath)
			continue
		}

		startFunc := startSymbol.(func(*webircgateway.Gateway, *sync.WaitGroup))
		pluginsQuit.Add(1)
		startFunc(gateway, pluginsQuit)
	}
}