~kris/9p

glenda

glenda/x/mux/config.go -rw-r--r-- 4.0 KiB
48661367 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package mux

// General configuration utilities for Glenda

import (
	"encoding/json"
	"os"
	"fmt"
	"strings"
	"errors"
	"strconv"
	"time"
	"github.com/SlyMarbo/rss"
	"github.com/bwmarrin/discordgo"
)


// Global variables are bad
var Config Configuration
var Session *discordgo.Session

// Stores subscription information for channels related to RSS feeds
type Subscription struct {
	ChanID	string
	SubID	int
}

// Stores a Feed and a list of the last n commits; n=3
type Feed struct {
	Feed		rss.Feed
	Recent	[]string
}

type Feeder struct {
	Feeds	[]Feed
	Subs		[]Subscription
}

// Stores config for current state
type Configuration struct {
	Db			map[string]string
	Feeder
	Reminders []Reminder
}

// Initializes current config (called once at start) ­ just .Read()?
func (c *Configuration) Init(s *discordgo.Session) {
	c.Db = map[string]string {
		"owner":		"188698402727526400",			// Henesy
		"name":		"glenda.cfg",					// Config file name
		"dir":			"./cfg",						// Config dir
		"delay":		"20",							// Minutes
		"mansite":		"http://man.postnix.pw/9front/",	// Henesy's
		
		"lookman":	"./x/mux/lookman/lookman",		// Vendored
		"sig":		"./x/mux/man/sig",			// ^
		"gendex":		"./gendex",					// ^
		"bullshit":		"./x/mux/misc/bullshit",			// ^
		
		"extrafortunes":	"../sys/games/lib/fortunes",			// Unix
		"fortunes":		"/usr/share/mirror/plan9front/lib/",	// Plan 9
		"oracledbs": "./oracledbs/",	// Vendored if you want it
		}
		
	// Regularly dump the config
	go func() {
		delay, err := strconv.Atoi(c.Db["delay"])
		if err != nil {
			fmt.Println("bad delay value, using default")
			delay = 20
		}
		
		for {
			select {
			case <-dumpChan:
				dump()

			case <- time.After(time.Duration(delay) * time.Minute):
				dump()
			}
			time.Sleep(5 * time.Millisecond)
		}
	}()

	err := c.Read()
	if err != nil {
		fmt.Println("read cfg failed: -", err)
		
		c.Setup()
	}
	
	Session = s
	
	for id, _ := range Config.Feeds {
		// maybe only do at init step?
		str := Config.Feeds[id].Feed.UpdateURL
		feed, _ := rss.Fetch(str)
		
		if feed != nil {
			Config.Feeds[id].Feed = *feed
		} else {
			fmt.Println("Failed to fetch feed: ", id)
		}
	}
	
	go Listener()
}

// Writes current config to file
func (c *Configuration) Write() error {
	path := c.Db["dir"] + "/" + c.Db["name"]
	
	if path == "/" {
		return errors.New("'dir' and 'name' must be in config")
	}
	
	var f *os.File
	var err error
	
	WRITE:
	f, err = os.OpenFile(path, os.O_RDWR, 0666)
	defer f.Close()
	
	if err != nil {
		if strings.Contains(err.Error(), "no such file or directory") {
			// Create files
			err = Config.Setup()
			if err != nil {
				// We have a creation problem
				return err
			}
			
			// Try again
			goto WRITE
			
		} else {
			fmt.Println("Error opening config (w), see: config.go -", err)
			return err
		}
	} else {
		// Serialize
		e := json.NewEncoder(f)
		err = e.Encode(Config)
		if err != nil {
			fmt.Println("Error writing config (w), see: config.go -", err)
			defer Config.Write()
			
			return err
		}
	}

	return nil
}

// Reads current config into memory
func (c *Configuration) Read() error {
	path := c.Db["dir"] + "/" + c.Db["name"]
	
	if path == "/" {
		return errors.New("'dir' and 'name' must be in config")
	}

	var f *os.File
	var err error
	
	f, err = os.Open(path)
	defer f.Close()
	
	if err != nil {
		return err
	}
	
	// De-serialize
	d := json.NewDecoder(f)
	err = d.Decode(&Config)
	if err != nil {
		fmt.Println("Error reading config (r), see: config.go -", err)
		
		// Overwrite since the file is bad
		defer Config.Write()
		
		return err
	}

	return nil
}

// Set up config for the first time (if one doesn't exist)
func (c *Configuration) Setup() error {
	err := os.Mkdir(c.Db["dir"], 0774)
	if err != nil {
		fmt.Println("Error in making cfg dir, see: config.go -", err)
		
		if !strings.Contains(err.Error(), "exists") {
			return err
		}
	}
	
	_, err = os.Create(c.Db["dir"] + "/" + c.Db["name"])
	if err != nil {
		fmt.Println("Error in making cfg file, see: config.go -", err)
		
		if !strings.Contains(err.Error(), "exists") {
			return err
		}
	}
	
	return nil
}