feat: implemented HRDLog upload
This commit is contained in:
@@ -31,6 +31,7 @@ const (
|
||||
ServiceQRZ Service = "qrz" // QRZ.com Logbook
|
||||
ServiceClublog Service = "clublog" // Club Log real-time upload
|
||||
ServiceLoTW Service = "lotw" // ARRL Logbook of The World (via TQSL)
|
||||
ServiceHRDLog Service = "hrdlog" // HRDLog.net real-time upload
|
||||
)
|
||||
|
||||
// UploadMode selects when an auto-upload fires after a QSO is saved.
|
||||
@@ -64,7 +65,8 @@ type ServiceConfig struct {
|
||||
Email string `json:"email"` // Club Log account email
|
||||
Username string `json:"username"` // LoTW website login (for confirmation download)
|
||||
Password string `json:"password"` // Club Log account / LoTW website password
|
||||
Callsign string `json:"callsign"` // Club Log logbook (owner) callsign
|
||||
Callsign string `json:"callsign"` // Club Log / HRDLog logbook (owner) callsign
|
||||
Code string `json:"code"` // HRDLog: account upload code
|
||||
ForceStationCallsign string `json:"force_station_callsign"` // QRZ + LoTW: override STATION_CALLSIGN
|
||||
TQSLPath string `json:"tqsl_path"` // LoTW: path to tqsl.exe
|
||||
StationLocation string `json:"station_location"` // LoTW: TQSL Station Location name
|
||||
@@ -81,6 +83,7 @@ func (c ServiceConfig) normalised() ServiceConfig {
|
||||
c.APIKey = strings.TrimSpace(c.APIKey)
|
||||
c.Email = strings.TrimSpace(c.Email)
|
||||
c.Callsign = strings.ToUpper(strings.TrimSpace(c.Callsign))
|
||||
c.Code = strings.TrimSpace(c.Code)
|
||||
c.ForceStationCallsign = strings.ToUpper(strings.TrimSpace(c.ForceStationCallsign))
|
||||
c.TQSLPath = strings.TrimSpace(c.TQSLPath)
|
||||
c.StationLocation = strings.TrimSpace(c.StationLocation)
|
||||
@@ -111,6 +114,7 @@ type ExternalServices struct {
|
||||
QRZ ServiceConfig `json:"qrz"`
|
||||
Clublog ServiceConfig `json:"clublog"`
|
||||
LoTW ServiceConfig `json:"lotw"`
|
||||
HRDLog ServiceConfig `json:"hrdlog"`
|
||||
}
|
||||
|
||||
// UploadResult is the outcome of a single upload attempt.
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package extsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -115,6 +115,7 @@ func (m *Manager) SetConfig(cfg ExternalServices) {
|
||||
cfg.QRZ = cfg.QRZ.normalised()
|
||||
cfg.Clublog = cfg.Clublog.normalised()
|
||||
cfg.LoTW = cfg.LoTW.normalised()
|
||||
cfg.HRDLog = cfg.HRDLog.normalised()
|
||||
m.cfg = cfg
|
||||
}
|
||||
|
||||
@@ -151,6 +152,10 @@ func (m *Manager) OnQSOLogged(id int64) {
|
||||
if lt := cfg.LoTW; lt.AutoUpload && lt.TQSLPath != "" && lt.StationLocation != "" {
|
||||
m.route(ServiceLoTW, id, lt)
|
||||
}
|
||||
// HRDLog — needs the station callsign + the account upload code.
|
||||
if h := cfg.HRDLog; h.AutoUpload && h.Callsign != "" && h.Code != "" {
|
||||
m.route(ServiceHRDLog, id, h)
|
||||
}
|
||||
}
|
||||
|
||||
// route sends a logged QSO down the configured timing path: queue it for the
|
||||
@@ -190,6 +195,9 @@ func (m *Manager) onCloseServices() []Service {
|
||||
if l := cfg.LoTW; l.AutoUpload && l.UploadMode == ModeOnClose && l.TQSLPath != "" && l.StationLocation != "" {
|
||||
out = append(out, ServiceLoTW)
|
||||
}
|
||||
if h := cfg.HRDLog; h.AutoUpload && h.UploadMode == ModeOnClose && h.Callsign != "" && h.Code != "" {
|
||||
out = append(out, ServiceHRDLog)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -237,6 +245,12 @@ func (m *Manager) FlushOnClose() int {
|
||||
uploaded++
|
||||
}
|
||||
}
|
||||
case ServiceHRDLog:
|
||||
for _, id := range ids {
|
||||
if m.upload(svc, id, cfg.HRDLog) {
|
||||
uploaded++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return uploaded
|
||||
@@ -303,7 +317,7 @@ func (m *Manager) upload(svc Service, id int64, cfg ServiceConfig) bool {
|
||||
switch svc {
|
||||
case ServiceQRZ, ServiceLoTW:
|
||||
owner = cfg.ForceStationCallsign
|
||||
case ServiceClublog:
|
||||
case ServiceClublog, ServiceHRDLog:
|
||||
owner = cfg.Callsign
|
||||
}
|
||||
if owner != "" && m.deps.StationCallOf != nil {
|
||||
@@ -351,6 +365,15 @@ func (m *Manager) upload(svc Service, id int64, cfg ServiceConfig) bool {
|
||||
return false
|
||||
}
|
||||
res, err = UploadLoTW(ctx, cfg, "", record)
|
||||
case ServiceHRDLog:
|
||||
// HRDLog takes the station callsign as a separate param, so the ADIF
|
||||
// keeps the QSO's own station call (no override), like Club Log.
|
||||
record, ok := m.deps.BuildADIF(id, "")
|
||||
if !ok {
|
||||
m.logf("extsvc: %s upload of QSO %d skipped (no record)", svc, id)
|
||||
return false
|
||||
}
|
||||
res, err = UploadHRDLog(ctx, m.deps.Client, cfg.Callsign, cfg.Code, record)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user