Cleanup masters impl

This commit is contained in:
pyed 2017-02-24 23:56:42 +03:00
parent a791b06075
commit 1e4c92acfb

View File

@ -102,13 +102,11 @@ const (
` `
) )
type stringslice []string
var ( var (
// flags // flags
BotToken string BotToken string
Masters stringslice Masters masterSlice
RPCURL string RPCURL string
Username string Username string
Password string Password string
@ -142,15 +140,31 @@ var (
"`", "'") "`", "'")
) )
func (i *stringslice) String() string { // we need a type for masters for the flag package to parse them as a slice
return fmt.Sprintf("%s", *i) type masterSlice []string
// String is mandatory functions for the flag package
func (masters *masterSlice) String() string {
return fmt.Sprintf("%s", *masters)
} }
func (i *stringslice) Set(value string) error { // Set is mandatory functions for the flag package
*i = append(*i, value) func (masters *masterSlice) Set(master string) error {
*masters = append(*masters, strings.ToLower(master))
return nil return nil
} }
// Contains takes a string and return true of masterSlice has it
func (masters masterSlice) Contains(master string) bool {
master = strings.ToLower(master)
for i := range masters {
if masters[i] == master {
return true
}
}
return false
}
// init flags // init flags
func init() { func init() {
// define arguments and parse them. // define arguments and parse them.
@ -234,7 +248,7 @@ func init() {
} }
// log the flags // log the flags
logger.Printf("[INFO] Token=%s\nMasters=%s\nURL=%s\nUSER=%s\nPASS=%s", logger.Printf("[INFO] Token=%s\n\t\tMasters=%s\n\t\tURL=%s\n\t\tUSER=%s\n\t\tPASS=%s",
BotToken, Masters, RPCURL, Username, Password) BotToken, Masters, RPCURL, Username, Password)
} }
@ -277,8 +291,8 @@ func main() {
continue continue
} }
// ignore anyone other than 'master' // ignore non masters
if !inMasters(update.Message.From.UserName) { if !Masters.Contains(update.Message.From.UserName) {
logger.Printf("[INFO] Ignored a message from: %s", update.Message.From.String()) logger.Printf("[INFO] Ignored a message from: %s", update.Message.From.String())
continue continue
} }
@ -1435,15 +1449,3 @@ LenCheck:
return resp.MessageID return resp.MessageID
} }
func inMasters(text string) bool {
lowerCase := strings.ToLower(text)
ret := false
for i := range Masters {
if strings.ToLower(Masters[i]) == lowerCase {
ret = true
break
}
}
return ret
}