-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstop.go
53 lines (46 loc) · 1.14 KB
/
stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"strconv"
)
// stop takes id[s] of torrent[s] or 'all' to stop them
func stop(tokens []string) {
// make sure that we got at least one argument
if len(tokens) == 0 {
send("stop: needs an argument", false)
return
}
torrents, err := rtorrent.Torrents()
if err != nil {
logger.Print("stop:", err)
send("stop: "+err.Error(), false)
return
}
// if the first argument is 'all' then stop all torrents
if tokens[0] == "all" {
if err := rtorrent.Stop(torrents...); err != nil {
logger.Print("stop:", err)
send("stop: error occurred while stopping some torrents", false)
return
}
send("stopped all torrents", false)
return
}
for _, i := range tokens {
id, err := strconv.Atoi(i)
if err != nil {
send(fmt.Sprintf("stop: %s is not a number", i), false)
continue
}
if id >= len(torrents) || id < 0 {
send(fmt.Sprintf("stop: No torrent with an ID of '%d'", id), false)
continue
}
if err := rtorrent.Stop(torrents[id]); err != nil {
logger.Print("stop:", err)
send("stop: "+err.Error(), false)
continue
}
send(fmt.Sprintf("Stopped: %s", torrents[id].Name), false)
}
}