2018-06-03 03:27:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2018-06-03 04:39:45 +00:00
|
|
|
// NTFConfig keeps track of setup properties as well as runtime state.
|
|
|
|
// FIXME separate compile-time and run-time properties into separate files
|
2018-06-03 03:27:50 +00:00
|
|
|
type NTFConfig struct {
|
2018-06-04 04:46:49 +00:00
|
|
|
HubAddr string
|
|
|
|
HubDescription string
|
2018-06-04 07:20:49 +00:00
|
|
|
HubSecNick string // Nickname of Hub-Security to exclude (e.g. "PtokaX"). Separate multiple with commas
|
2018-06-04 04:46:49 +00:00
|
|
|
HubNickMinChars int
|
|
|
|
BotAPIKey string
|
|
|
|
GroupChatID int64
|
2018-06-03 03:27:50 +00:00
|
|
|
|
|
|
|
// Map of telegram user IDs to NMDC nicks
|
|
|
|
KnownUsers map[int64]string
|
2018-06-03 04:39:45 +00:00
|
|
|
|
|
|
|
// Map of telegram users known to be in the group chat ==> telegram displayname
|
|
|
|
GroupChatMembers map[int64]string
|
2018-06-03 06:24:08 +00:00
|
|
|
|
|
|
|
// Map of telegram users to their direct conversation ID with the bot
|
|
|
|
DirectMessageChats map[int64]int64
|
2018-06-03 03:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(configFile string) (NTFConfig, error) {
|
|
|
|
b, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return NTFConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := NTFConfig{}
|
|
|
|
err = json.Unmarshal(b, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return NTFConfig{}, err
|
|
|
|
}
|
|
|
|
|
2018-06-03 06:24:08 +00:00
|
|
|
if ret.KnownUsers == nil {
|
|
|
|
ret.KnownUsers = make(map[int64]string)
|
|
|
|
}
|
|
|
|
if ret.GroupChatMembers == nil {
|
|
|
|
ret.GroupChatMembers = make(map[int64]string)
|
|
|
|
}
|
|
|
|
if ret.DirectMessageChats == nil {
|
|
|
|
ret.DirectMessageChats = make(map[int64]int64)
|
|
|
|
}
|
|
|
|
|
2018-06-03 03:27:50 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *NTFConfig) Save(configFile string) error {
|
|
|
|
b, err := json.MarshalIndent(this, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(configFile, b, 0644)
|
|
|
|
}
|