199 lines
6.6 KiB
Go
199 lines
6.6 KiB
Go
package extsvc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// hrdlogUploadURL is HRDLog.net's real-time upload endpoint. It accepts a
|
|
// form-encoded POST with the uploader's callsign, the account's secret upload
|
|
// Code (HRDLog account → "My Account" → upload code), an App identifier, and
|
|
// one ADIF record.
|
|
const hrdlogUploadURL = "https://robot.hrdlog.net/NewEntry.aspx"
|
|
|
|
// hrdlogApp is the App identifier sent to HRDLog so uploads are attributed to
|
|
// OpsLog in the user's HRDLog activity.
|
|
const hrdlogApp = "OpsLog"
|
|
|
|
// hrdlogPost performs the form POST to NewEntry.aspx and returns the raw
|
|
// response body. The endpoint replies HTTP 200 with a small XML document even
|
|
// for errors; callers classify it via the markers in classifyHRDLog.
|
|
func hrdlogPost(ctx context.Context, client *http.Client, callsign, code, adif string) (string, error) {
|
|
form := url.Values{}
|
|
form.Set("Callsign", callsign)
|
|
form.Set("Code", code)
|
|
form.Set("App", hrdlogApp)
|
|
form.Set("ADIFData", adif)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, hrdlogUploadURL, strings.NewReader(form.Encode()))
|
|
if err != nil {
|
|
return "", fmt.Errorf("hrdlog: build request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
if client == nil {
|
|
client = &http.Client{Timeout: 20 * time.Second}
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("hrdlog: request failed: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
|
|
msg := strings.TrimSpace(string(body))
|
|
if resp.StatusCode != http.StatusOK {
|
|
if msg == "" {
|
|
msg = fmt.Sprintf("HTTP %d", resp.StatusCode)
|
|
}
|
|
return msg, fmt.Errorf("hrdlog: http %d: %s", resp.StatusCode, msg)
|
|
}
|
|
return msg, nil
|
|
}
|
|
|
|
// authErrHRDLog returns a non-empty, human-readable reason when the response
|
|
// signals a credential problem (wrong upload code or unregistered callsign),
|
|
// or "" otherwise. Markers mirror HRDLog's documented XML replies
|
|
// ("Invalid token</error>" / "Unknown user</error>").
|
|
func authErrHRDLog(body string) string {
|
|
b := strings.ToLower(body)
|
|
switch {
|
|
case strings.Contains(b, "invalid token"):
|
|
return "invalid upload code"
|
|
case strings.Contains(b, "unknown user"):
|
|
return "callsign not registered at HRDLog"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// UploadHRDLog pushes one ADIF record to HRDLog.net. callsign is the station
|
|
// callsign the log belongs to, code is the account's upload code.
|
|
//
|
|
// Form fields (application/x-www-form-urlencoded POST):
|
|
//
|
|
// Callsign=<station call>&Code=<upload code>&App=OpsLog&ADIFData=<one record>
|
|
//
|
|
// HRDLog replies with XML: "<insert>1" on success, "<insert>0" for a duplicate
|
|
// (already logged — treated as success so retries are idempotent), or an
|
|
// "<error>…</error>" payload otherwise.
|
|
func UploadHRDLog(ctx context.Context, client *http.Client, callsign, code, adifRecord string) (UploadResult, error) {
|
|
callsign = strings.ToUpper(strings.TrimSpace(callsign))
|
|
code = strings.TrimSpace(code)
|
|
if callsign == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: station callsign not set")
|
|
}
|
|
if code == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: upload code not set")
|
|
}
|
|
if strings.TrimSpace(adifRecord) == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: empty adif record")
|
|
}
|
|
|
|
body, err := hrdlogPost(ctx, client, callsign, code, adifRecord)
|
|
if err != nil {
|
|
return UploadResult{OK: false, Message: body}, err
|
|
}
|
|
|
|
b := strings.ToLower(body)
|
|
switch {
|
|
case strings.Contains(b, "<insert>1"):
|
|
return UploadResult{OK: true, Message: "uploaded"}, nil
|
|
case strings.Contains(b, "<insert>0"):
|
|
return UploadResult{OK: true, Message: "already in logbook"}, nil
|
|
}
|
|
reason := authErrHRDLog(body)
|
|
if reason == "" {
|
|
reason = body
|
|
if reason == "" {
|
|
reason = "upload rejected"
|
|
}
|
|
}
|
|
return UploadResult{OK: false, Message: reason}, fmt.Errorf("hrdlog: upload failed: %s", reason)
|
|
}
|
|
|
|
// UploadHRDLogADIF pushes a WHOLE ADIF document (header + many records) to
|
|
// HRDLog.net in one NewEntry request — HRDLog parses every <eor> record and
|
|
// replies "<insert>N" with the number actually inserted (duplicates aren't
|
|
// counted but aren't errors). Use this for bulk uploads instead of calling
|
|
// UploadHRDLog once per QSO.
|
|
func UploadHRDLogADIF(ctx context.Context, client *http.Client, callsign, code, adifDoc string) (UploadResult, error) {
|
|
callsign = strings.ToUpper(strings.TrimSpace(callsign))
|
|
code = strings.TrimSpace(code)
|
|
if callsign == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: station callsign not set")
|
|
}
|
|
if code == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: upload code not set")
|
|
}
|
|
if strings.TrimSpace(adifDoc) == "" {
|
|
return UploadResult{}, fmt.Errorf("hrdlog: empty adif")
|
|
}
|
|
|
|
body, err := hrdlogPost(ctx, client, callsign, code, adifDoc)
|
|
if err != nil {
|
|
return UploadResult{OK: false, Message: body}, err
|
|
}
|
|
if reason := authErrHRDLog(body); reason != "" {
|
|
return UploadResult{OK: false, Message: reason}, fmt.Errorf("hrdlog: %s", reason)
|
|
}
|
|
if n, ok := parseHRDLogInsert(body); ok {
|
|
return UploadResult{OK: true, Message: fmt.Sprintf("%d added", n)}, nil
|
|
}
|
|
if strings.Contains(strings.ToLower(body), "<error>") {
|
|
return UploadResult{OK: false, Message: body}, fmt.Errorf("hrdlog: %s", body)
|
|
}
|
|
return UploadResult{OK: true, Message: "uploaded"}, nil
|
|
}
|
|
|
|
// parseHRDLogInsert reads N from "<insert>N" (or "<insert>N</insert>").
|
|
func parseHRDLogInsert(body string) (int, bool) {
|
|
b := strings.ToLower(body)
|
|
i := strings.Index(b, "<insert>")
|
|
if i < 0 {
|
|
return 0, false
|
|
}
|
|
rest := b[i+len("<insert>"):]
|
|
j := 0
|
|
for j < len(rest) && rest[j] >= '0' && rest[j] <= '9' {
|
|
j++
|
|
}
|
|
if j == 0 {
|
|
return 0, false
|
|
}
|
|
n, err := strconv.Atoi(rest[:j])
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
return n, true
|
|
}
|
|
|
|
// TestHRDLog validates the configured HRDLog credentials with a REAL request:
|
|
// it posts an empty ADIF so nothing is inserted, then checks for HRDLog's auth
|
|
// errors. A wrong upload code comes back as "Invalid token", a wrong callsign
|
|
// as "Unknown user"; anything else means the credentials were accepted (HRDLog
|
|
// simply had no QSO to add).
|
|
func TestHRDLog(ctx context.Context, client *http.Client, cfg ServiceConfig) (string, error) {
|
|
callsign := strings.ToUpper(strings.TrimSpace(cfg.Callsign))
|
|
code := strings.TrimSpace(cfg.Code)
|
|
if callsign == "" {
|
|
return "", fmt.Errorf("hrdlog: station callsign not set")
|
|
}
|
|
if code == "" {
|
|
return "", fmt.Errorf("hrdlog: upload code not set")
|
|
}
|
|
body, err := hrdlogPost(ctx, client, callsign, code, "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if reason := authErrHRDLog(body); reason != "" {
|
|
return "", fmt.Errorf("hrdlog: %s", reason)
|
|
}
|
|
return fmt.Sprintf("Credentials accepted — %s", callsign), nil
|
|
}
|