Skip to content

Commit

Permalink
Adding multiple masters
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelLR committed Nov 30, 2016
1 parent 851f83a commit e50af1a
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions transmission-telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ const (
`
)

type stringslice []string

var (

// flags
BotToken string
Master string
Masters stringslice
RPCURL string
Username string
Password string
Expand All @@ -133,34 +135,46 @@ 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
func init() {
// define arguments and parse them.
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(&Username, "username", "", "Transmission username")
flag.StringVar(&Password, "password", "", "Transmission password")
flag.StringVar(&LogFile, "logfile", "", "Send logs to a file")

// set the usage message
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.Parse()

// make sure that we have the two madatory arguments: telegram token & master's handler.
if BotToken == "" ||
Master == "" {
len(Masters) < 1 {
fmt.Fprintf(os.Stderr, "Error: Mandatory argument missing! (-token or -master)\n\n")
flag.Usage()
os.Exit(1)
}

// 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 LogFile != "" {
Expand All @@ -179,8 +193,8 @@ func init() {
}

// log the flags
log.Printf("[INFO] Token=%s\nMaster=%s\nURL=%s\nUSER=%s\nPASS=%s",
BotToken, Master, RPCURL, Username, Password)
log.Printf("[INFO] Token=%s\nMasters=%s\nURL=%s\nUSER=%s\nPASS=%s",
BotToken, Masters, RPCURL, Username, Password)
}

// init transmission
Expand Down Expand Up @@ -223,7 +237,7 @@ func main() {
}

// 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())
continue
}
Expand Down Expand Up @@ -1357,3 +1371,15 @@ LenCheck:

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
}

0 comments on commit e50af1a

Please sign in to comment.