Merge pull request #1 from ManuelLR/master

Added multiple masters
This commit is contained in:
pyed 2016-11-30 23:24:45 +03:00 committed by GitHub
commit 3800b83569

View File

@ -102,11 +102,13 @@ const (
` `
) )
type stringslice []string
var ( var (
// flags // flags
BotToken string BotToken string
Master string Masters stringslice
RPCURL string RPCURL string
Username string Username string
Password string Password string
@ -133,11 +135,20 @@ var (
"`", "'") "`", "'")
) )
func (i *stringslice) String() string {
return fmt.Sprintf("%s", *i)
}
func (i *stringslice) Set(value string) error {
*i = append(*i, value)
return nil
}
// init flags // init flags
func init() { func init() {
// define arguments and parse them. // define arguments and parse them.
flag.StringVar(&BotToken, "token", "", "Telegram bot token") flag.StringVar(&BotToken, "token", "", "Telegram bot token")
flag.StringVar(&Master, "master", "", "Your telegram handler, So the bot will only respond to you") flag.Var(&Masters, "master", "Your telegram handler, So the bot will only respond to you. Can specify more than one")
flag.StringVar(&RPCURL, "url", "http://localhost:9091/transmission/rpc", "Transmission RPC URL") flag.StringVar(&RPCURL, "url", "http://localhost:9091/transmission/rpc", "Transmission RPC URL")
flag.StringVar(&Username, "username", "", "Transmission username") flag.StringVar(&Username, "username", "", "Transmission username")
flag.StringVar(&Password, "password", "", "Transmission password") flag.StringVar(&Password, "password", "", "Transmission password")
@ -145,7 +156,7 @@ func init() {
// set the usage message // set the usage message
flag.Usage = func() { flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: transmission-bot -token=<TOKEN> -master=<@tuser> -url=[http://] -username=[user] -password=[pass]\n\n") fmt.Fprint(os.Stderr, "Usage: transmission-bot -token=<TOKEN> -master=<@tuser> -master=<@yuser2> -url=[http://] -username=[user] -password=[pass]\n\n")
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -153,14 +164,17 @@ func init() {
// make sure that we have the two madatory arguments: telegram token & master's handler. // make sure that we have the two madatory arguments: telegram token & master's handler.
if BotToken == "" || if BotToken == "" ||
Master == "" { len(Masters) < 1 {
fmt.Fprintf(os.Stderr, "Error: Mandatory argument missing! (-token or -master)\n\n") fmt.Fprintf(os.Stderr, "Error: Mandatory argument missing! (-token or -master)\n\n")
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
// make sure that the handler doesn't contain @ // make sure that the handler doesn't contain @
Master = strings.Replace(Master, "@", "", -1) for i := range Masters {
Masters[i] = strings.Replace(Masters[i], "@", "", -1)
}
// if we got a log file, log to it // if we got a log file, log to it
if LogFile != "" { if LogFile != "" {
@ -179,8 +193,8 @@ func init() {
} }
// log the flags // log the flags
log.Printf("[INFO] Token=%s\nMaster=%s\nURL=%s\nUSER=%s\nPASS=%s", log.Printf("[INFO] Token=%s\nMasters=%s\nURL=%s\nUSER=%s\nPASS=%s",
BotToken, Master, RPCURL, Username, Password) BotToken, Masters, RPCURL, Username, Password)
} }
// init transmission // init transmission
@ -223,7 +237,7 @@ func main() {
} }
// ignore anyone other than 'master' // ignore anyone other than 'master'
if strings.ToLower(update.Message.From.UserName) != strings.ToLower(Master) { if !inMasters(update.Message.From.UserName) {
log.Printf("[INFO] Ignored a message from: %s", update.Message.From.String()) log.Printf("[INFO] Ignored a message from: %s", update.Message.From.String())
continue continue
} }
@ -1357,3 +1371,15 @@ 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
}