forked from Tensai75/nzb-monkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.go
170 lines (148 loc) · 4.76 KB
/
arguments.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"bytes"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
parser "github.com/alexflint/go-arg"
)
// arguments structure
type Args struct {
Nzblnk string `arg:"positional" help:"a qualified NZBLNK URI (nzblnk://?h=...)"`
Header string `arg:"-s,--subject" help:"the header/subject to search for"`
Title string `arg:"-t,--title" help:"the title/tag for the NZB file"`
Password string `arg:"-p,--password" help:"the password to extract the download"`
Groups []string `arg:"-g,--group" help:"the group(s) to search in (several groups seperated with space)"`
Date string `arg:"-d,--date" help:"the date the upload was posted to Usenet (either in the format DD.MM.YYYY or as a Unix timestamp)"`
Category string `arg:"-c,--category" help:"the category to use for the target (if supportet by the target)"`
UnixDate int64 `arg:"-"` // will hold the parsed Unix timestamp
IsTimestamp bool `arg:"-"` // will indicate if exact timestamp was passed as date
Config string `arg:"--config" help:"path to the config file"`
Debug bool `arg:"--debug" help:"logs output to log file"`
}
// version information
func (Args) Version() string {
return " "
}
// additional description
func (Args) Epilogue() string {
return " Parameters that are passed as arguments have precedence over the parameters of the NZBLNK.\n\n For more information visit github.com/Tensai75/nzb-monkey-go\n"
}
// global arguments variable
var args struct {
Args
}
// parser variable
var argParser *parser.Parser
func parseArguments() {
parserConfig := parser.Config{
IgnoreEnv: true,
}
// parse flags
argParser, _ = parser.NewParser(parserConfig, &args)
if err := parser.Parse(&args); err != nil {
if err.Error() == "help requested by user" {
writeHelp(argParser)
fmt.Println(args.Epilogue())
exit(0)
} else if err.Error() == "version requested by user" {
fmt.Println(args.Version())
exit(0)
}
writeUsage(argParser)
Log.Error(err.Error())
exit(1)
}
}
func checkArguments() {
if args.Header == "" && args.Nzblnk == "" {
writeUsage(argParser)
Log.Error("You must provide either --subject or a NZBLNK URI")
exit(1)
}
// parse nzblink if provided
isNzblnk := false
if args.Nzblnk != "" {
if nzblnk, err := url.Parse(args.Nzblnk); err == nil {
if query, err := url.ParseQuery(nzblnk.RawQuery); err == nil {
if h := query.Get("h"); h != "" && args.Header == "" {
args.Header = strings.TrimSpace(h)
} else {
writeUsage(argParser)
Log.Error("Invalid NZBLNK URI: missing 'h' parameter")
exit(1)
}
if t := query.Get("t"); t != "" && args.Title == "" {
args.Title = strings.TrimSpace(t)
}
if p := query.Get("p"); p != "" && args.Password == "" {
args.Password = strings.TrimSpace(p)
}
if query.Get("g") != "" && args.Groups == nil {
for _, group := range query["g"] {
args.Groups = append(args.Groups, strings.TrimSpace(group))
}
}
if d := query.Get("d"); d != "" && args.Date == "" {
isNzblnk = true
args.Date = strings.TrimSpace(d)
}
}
}
}
// date argument needs to be parsed because it can have two formats
if args.Date != "" {
dateRegexDate := regexp.MustCompile(`^[0-3]\d\.[0-1]\d\.(?:19|20)\d\d$`)
dateRegexTimestamp := regexp.MustCompile(`^[1-9]\d{9}$`) // starting from Sep 09 2001 01:46:40 GMT+0000
var parseError error
if match := dateRegexDate.FindStringIndex(args.Date); match != nil {
var date time.Time
if date, parseError = time.Parse("02.01.2006", args.Date); parseError == nil {
args.UnixDate = date.Unix() + (60 * 60 * 23) - 1 // add a day minus 1 sec so it is 23:59:59
}
} else if match := dateRegexTimestamp.FindStringIndex(args.Date); match != nil {
args.UnixDate, parseError = strconv.ParseInt(args.Date, 10, 64)
args.UnixDate += (60 * 60) // add 1 hour buffer
args.IsTimestamp = true
} else {
parseError = fmt.Errorf("ERROR")
}
if parseError != nil || args.UnixDate == 0 {
if isNzblnk {
Log.Warn("Invalid NZBLNK URI: invalid input for parameter 'd'")
} else {
writeUsage(argParser)
Log.Error("Invalid input for --date")
exit(1)
}
}
}
// set title to header if empty
if args.Title == "" {
args.Title = args.Header
}
// replace a.b. in groups
for i, group := range args.Groups {
args.Groups[i] = strings.Replace(group, "a.b.", "alt.binaries.", 1)
}
}
func writeUsage(parser *parser.Parser) {
var buf bytes.Buffer
parser.WriteUsage(&buf)
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
fmt.Println(" " + scanner.Text())
}
}
func writeHelp(parser *parser.Parser) {
var buf bytes.Buffer
parser.WriteHelp(&buf)
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
fmt.Println(" " + scanner.Text())
}
}