File: mps.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (176 lines) | stat: -rw-r--r-- 4,636 bytes parent folder | download
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
package cli

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/la5nta/pat/app"
	"github.com/la5nta/pat/internal/cmsapi"
)

const (
	MPSUsage = `subcommand [options]

subcommands:
  list [--all]    List message pickup stations for your callsign, or all MPS with --all
  clear           Delete all message pickup stations for your callsign
  add [CALLSIGN]  Add a message pickup station`

	MPSExample = `
  list            List your message pickup stations
  list --all      List all message pickup stations
  clear           Delete all your message pickup stations
  add W1AW        Add W1AW as a message pickup station`
)

func MPSHandle(ctx context.Context, a *app.App, args []string) {
	mycall := a.Options().MyCall
	if mycall == "" {
		fmt.Println("ERROR: MyCall not configured")
		os.Exit(1)
	}

	switch cmd, args := shiftArgs(args); cmd {
	case "list":
		option, _ := shiftArgs(args)
		if option == "--all" {
			err := mpsListAllHandle(ctx, mycall)

			if err != nil {
				fmt.Println("ERROR:", err)
				os.Exit(1)
			}
		} else if err := mpsListMineHandle(ctx, mycall); err != nil {
			fmt.Println("ERROR:", err)
			os.Exit(1)
		}
	case "clear":
		if err := mpsClearHandle(ctx, a, mycall); err != nil {
			fmt.Println("ERROR:", err)
			os.Exit(1)
		}
	case "add":
		addCall, _ := shiftArgs(args)
		if err := mpsAddHandle(ctx, a, mycall, addCall); err != nil {
			fmt.Println("ERROR:", err)
			os.Exit(1)
		}
	default:
		fmt.Println("Missing argument, try 'mps help'.")
	}
}

func mpsListAllHandle(ctx context.Context, mycall string) error {
	mpsList, err := cmsapi.HybridStationList(ctx)

	if err != nil {
		return fmt.Errorf("failed to retrieve MPS list: %w", err)
	}

	if len(mpsList) == 0 {
		fmt.Println("No message pickup stations found.")
		return nil
	}

	fmtStr := "%-12s %-16s\n"

	// Print header
	fmt.Printf(fmtStr, "mps callsign", "forwarding type")

	// Print MPS records
	for _, station := range mpsList {
		fwdType := "unknown"
		if station.AutomaticForwarding {
			fwdType = "automatic"
		} else if station.ManualForwarding {
			fwdType = "manual"
		}

		fmt.Printf(fmtStr, station.Callsign, fwdType)
	}

	return nil
}

func mpsListMineHandle(ctx context.Context, mycall string) error {
	mpsList, err := cmsapi.MPSGet(ctx, mycall, mycall)
	if err != nil {
		return fmt.Errorf("failed to retrieve your MPS records: %w", err)
	}

	if len(mpsList) == 0 {
		fmt.Println("No message pickup stations configured for your callsign.")
		return nil
	}

	fmtStr := "%-12.12s %s\n"

	// Print header
	fmt.Printf(fmtStr, "mps callsign", "timestamp")

	// Print MPS records
	for _, mps := range mpsList {
		fmt.Printf(fmtStr, mps.MpsCallsign, mps.Timestamp.Format("2006-01-02 15:04:05"))
	}
	return nil
}

func mpsClearHandle(ctx context.Context, a *app.App, mycall string) error {
	password := getPasswordForCallsign(ctx, a, mycall)
	if password == "" {
		return fmt.Errorf("password required for clear operation")
	}

	mpsList, err := cmsapi.MPSGet(ctx, mycall, mycall)
	if err != nil {
		return fmt.Errorf("failed to retrieve your MPS records for display before clear: %w", err)
	}

	if err := cmsapi.MPSDelete(ctx, mycall, mycall, password); err != nil {
		return fmt.Errorf("failed to clear MPS records: %w", err)
	}

	fmt.Println("All message pickup stations deleted successfully.")
	fmt.Println("Previous message pickup stations:")
	for _, station := range mpsList {
		fmt.Println(station.MpsCallsign)
	}

	return nil
}

func mpsAddHandle(ctx context.Context, a *app.App, mycall, mpsCallsign string) error {
	// Validate callsign format
	mpsCallsign = strings.ToUpper(strings.TrimSpace(mpsCallsign))
	if mpsCallsign == "" {
		return fmt.Errorf("MPS callsign cannot be empty")
	}

	password := getPasswordForCallsign(ctx, a, mycall)
	if password == "" {
		return fmt.Errorf("password required for add operation")
	}

	// get list to ensure that we don't allow more than
	// 3 stations total, with 2 suggested
	mpsList, err := cmsapi.MPSGet(ctx, mycall, mycall)
	if err != nil {
		return fmt.Errorf("failed to retrieve your MPS records to check if addition is allowed: %w", err)
	}

	numMPS := len(mpsList)
	if numMPS >= 3 {
		return fmt.Errorf("configuring more than 3 message pickup stations is not allowed")
	} else if numMPS == 2 {
		fmt.Println("Warning: You already have 2 message pickup stations configured, more is not recommended. The maximum allowed is 3 stations")
	}

	if err := cmsapi.MPSAdd(ctx, mycall, mycall, password, mpsCallsign); err != nil {
		return fmt.Errorf("failed to add MPS station: %w", err)
	}

	fmt.Printf("Message pickup station %s added successfully.\n", mpsCallsign)
	return nil
}