-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
227 lines (204 loc) · 4.98 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"github.com/reiver/finger/arg"
failure "github.com/reiver/finger/srv/failure"
verbose "github.com/reiver/finger/srv/verbose"
"github.com/reiver/go-finger"
"io"
"net"
"os"
)
func main() {
// This ('arguments') holds what was on the command-line when the flags (and program-name) are removed.
//
// For example, if this program was called with the following command-line:
//
// finger -v /W [email protected]
//
// Then logically 'arguments' would be:
//
// arguments == []string{"/W", "[email protected]"}
//
// And also, for another example, if this program called with the following command-line:
//
// finger -v [email protected]
//
// Then logically 'arguments' would be:
//
// arguments == []string{"[email protected]"}
var arguments []string = arg.Values
// Here we see if there is a finger-protocol switch (as a string) from the command-line
// which this program was called with.
//
// For example:
//
// finger /W
// # switch -> "/W"
//
// And, for example:
//
// finger /W [email protected]
// # switch -> "/W"
//
// Or, for example:
//
// finger /W [email protected]@example.com
// # switch -> "/W"
//
// Or, alternatively, for example:
//
// finger /W [email protected]:1971
// # switch -> "/W"
//
// Or also, for example:
//
// finger /W janedoe
// # switch -> "/W"
//
// Or even, for example:
//
// finger /W @example.com
// # switch -> "/W"
//
// Etc.
//
// Although the switch could be something other than "/W".
// (For example — "/PULL", "/ECHO", "/path/to/a/file/ext", etc.)
// It just has to become with a "/" character.
//
// Also, there might not be a switch. For example:
//
// finger [email protected]
// # no switch
var swtch finger.Switch
func(){
if len(arguments) < 1 {
return
}
argument0 := arguments[0]
var err error
swtch, err = finger.ParseSwitch(argument0)
if nil != err {
return
}
arguments = arguments[1:]
}()
verbose.Publish("SWITCH: %q", swtch)
// Get the finger-protocol query (as a string) from the command-line
// which this program was called with.
//
// For example:
//
// finger [email protected]
// # query -> "[email protected]"
//
// Or, for example:
//
// finger [email protected]@example.com
// # query -> "[email protected]@example.com"
//
// Or, alternatively, for example:
//
// finger [email protected]:1971
// # query -> "[email protected]:1971"
//
// Or also, for example:
//
// finger janedoe
// # query -> "janedoe"
//
// Or even, for example:
//
// finger @example.com
// # query -> "@example.com"
//
// Etc.
//
// Note that in this type of finger-protocol request:
//
// "/W [email protected]"
//
// The "/W" is NOT part of the query.
//
// Only the "[email protected]" is the query.
var q string
{
if 1 <= len(arguments) {
q = arguments[0]
}
}
verbose.Publish("QUERY: %q", q)
// Here we parse the finger-protocol query string,
// and if valid, turn it into a finger.Query.
var query finger.Query
{
var err error
query, err = finger.ParseQuery(q)
if nil != err {
failure.Publish("bad query: %s", err)
////////////////////////// EXIT
os.Exit(1)
return
}
}
verbose.Publish("COMPILED QUERY: %q", q)
// Here we infer what TCP address we should make a TCP connection to.
// (That we will later make a finger-protocol request from.)
//
// And what the finger-protocol query we sent will be.
var address finger.Address
var target finger.Target
{
var subquery finger.Query
address, subquery = query.ClientParameters()
target = subquery.Target()
}
verbose.Publish("ADDRESS: %q", address)
verbose.Publish("RESOLVED-ADDRESS: %q", address.Resolve())
verbose.Publish("TARGET: %#v", target)
// Create the finger-protocol request.
//
// We aren't sending it to the server yet.
//
// We are just preparing the request (and will use it a little later).
var request finger.Request = finger.AssembleRequest(swtch, target)
verbose.Publish("REQUEST: %q", request)
// Connect to the finger-protocol server.
var conn net.Conn
{
var err error
conn, err = net.Dial("tcp", address.Resolve())
if nil != err {
failure.Publish("problem connecting to %q: %s", address, err)
////////////////////////// EXIT
os.Exit(1)
return
}
}
defer conn.Close()
// Bind the finger-protocol client to the TCP connection to the finger-protocol server.
var client finger.Client = finger.AssembleClient(conn)
// Send the finger-protocol request,
// and receive finger-protocol response.
var responseReader finger.ResponseReader
{
var err error
responseReader, err = client.Do(request)
if nil != err {
failure.Publish("problem with doing request: %s", err)
////////////////////////// EXIT
os.Exit(1)
return
}
}
// Send the contents of the finger-protocol response,
// from the finger-protocol server, to STDOUT.
//
// This way the user can see it.
// Or it can be piped to another program.
// Or saved to a file.
// Or whatever.
{
io.Copy(os.Stdout, responseReader)
}
}