Skip to content

Commit

Permalink
fix 'head', 'tail' updating crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdulelah Alfuntukh committed Dec 7, 2016
1 parent 747e16b commit da6712b
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions transmission-telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var (

// flags
BotToken string
Masters stringslice
Masters stringslice
RPCURL string
Username string
Password string
Expand Down Expand Up @@ -136,12 +136,12 @@ var (
)

func (i *stringslice) String() string {
return fmt.Sprintf("%s", *i)
return fmt.Sprintf("%s", *i)
}

func (i *stringslice) Set(value string) error {
*i = append(*i, value)
return nil
*i = append(*i, value)
return nil
}

// init flags
Expand All @@ -156,7 +156,7 @@ func init() {

// set the usage message
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: transmission-bot -token=<TOKEN> -master=<@tuser> -master=<@yuser2> -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()
}

Expand All @@ -171,10 +171,9 @@ func init() {
}

// make sure that the handler doesn't contain @
for i := range Masters {
Masters[i] = strings.Replace(Masters[i], "@", "", -1)
}

for i := range Masters {
Masters[i] = strings.Replace(Masters[i], "@", "", -1)
}

// if we got a log file, log to it
if LogFile != "" {
Expand Down Expand Up @@ -430,12 +429,21 @@ func head(ud tgbotapi.Update, tokens []string) {
continue // try again if some error heppened
}

for i := range torrents[:n] {
torrentName := mdReplacer.Replace(torrents[i].Name) // escape markdown
if len(torrents) < 1 {
continue
}

// make sure that we stay in the boundaries
if n <= 0 || n > len(torrents) {
n = len(torrents)
}

for _, torrent := range torrents[:n] {
torrentName := mdReplacer.Replace(torrent.Name) // escape markdown
buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *%s* ↑ *%s* R: *%s*\n\n",
torrents[i].ID, torrentName, torrents[i].TorrentStatus(), humanize.Bytes(torrents[i].Have()),
humanize.Bytes(torrents[i].SizeWhenDone), torrents[i].PercentDone*100, humanize.Bytes(torrents[i].RateDownload),
humanize.Bytes(torrents[i].RateUpload), torrents[i].Ratio()))
torrent.ID, torrentName, torrent.TorrentStatus(), humanize.Bytes(torrent.Have()),
humanize.Bytes(torrent.SizeWhenDone), torrent.PercentDone*100, humanize.Bytes(torrent.RateDownload),
humanize.Bytes(torrent.RateUpload), torrent.Ratio()))
}

// no need to check if it is empty, as if the buffer is empty telegram won't change the message
Expand Down Expand Up @@ -498,6 +506,15 @@ func tail(ud tgbotapi.Update, tokens []string) {
continue // try again if some error heppened
}

if len(torrents) < 1 {
continue
}

// make sure that we stay in the boundaries
if n <= 0 || n > len(torrents) {
n = len(torrents)
}

for _, torrent := range torrents[len(torrents)-n:] {
torrentName := mdReplacer.Replace(torrent.Name) // escape markdown
buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* of *%s* (*%.1f%%*) ↓ *%s* ↑ *%s* R: *%s*\n\n",
Expand Down Expand Up @@ -1372,14 +1389,14 @@ LenCheck:
return resp.MessageID
}

func inMasters(text string) bool{
lowerCase := strings.ToLower(text)
ret := false
for i := range Masters {
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
}
return ret
}

0 comments on commit da6712b

Please sign in to comment.