-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlist.go
52 lines (46 loc) · 1.23 KB
/
list.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
package main
import (
"bytes"
"fmt"
"regexp"
)
// list will form and send a list of all the torrents
// takes an optional argument which is a query to match against trackers
// to list only torrents that has a tracker that matchs.
func list(tokens []string) {
torrents, err := rtorrent.Torrents()
if err != nil {
logger.Print(err)
send("list: "+err.Error(), false)
return
}
buf := new(bytes.Buffer)
// if it gets a query, it will list torrents that has trackers that match the query
if len(tokens) != 0 {
// (?i) for case insensitivity
regx, err := regexp.Compile("(?i)" + tokens[0])
if err != nil {
send("list: "+err.Error(), false)
return
}
for i := range torrents {
if regx.MatchString(torrents[i].Tracker.Hostname()) {
buf.WriteString(fmt.Sprintf("<%d> %s\n", i, torrents[i].Name))
}
}
} else { // if we did not get a query, list all torrents
for i := range torrents {
buf.WriteString(fmt.Sprintf("<%d> %s\n", i, torrents[i].Name))
}
}
if buf.Len() == 0 {
// if we got a tracker query show different message
if len(tokens) != 0 {
send(fmt.Sprintf("list: No tracker matches: *%s*", tokens[0]), true)
return
}
send("list: No torrents", false)
return
}
send(buf.String(), false)
}