forked from warthog618/sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
171 lines (134 loc) · 4.82 KB
/
options.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
// SPDX-License-Identifier: MIT
//
// Copyright © 2020 Kent Gibson <[email protected]>.
package sms
import "github.com/warthog618/sms/encoding/tpdu"
// EncoderOption is an optional mutator for the Encoder.
type EncoderOption interface {
ApplyEncoderOption(*Encoder)
}
// DecodeOption defines options for Decode.
type DecodeOption interface {
ApplyDecodeOption(*DecodeConfig)
}
// UnmarshalOption defines options for Unmarhsal.
type UnmarshalOption interface {
ApplyUnmarshalOption(*UnmarshalConfig)
}
// WithTemplate specifies the TPDU to be used as the template for encoding.
func WithTemplate(t tpdu.TPDU) EncoderOption {
return tpduTemplate{t}
}
type tpduTemplate struct {
t tpdu.TPDU
}
func (o tpduTemplate) ApplyEncoderOption(e *Encoder) {
e.pdu = o.t
}
type templateOption struct {
tpdu.Option
}
func (o templateOption) ApplyEncoderOption(e *Encoder) {
o.ApplyTPDUOption(&e.pdu)
}
// WithTemplateOption wraps a TPDU option in a TemplateOption so it can be
// applied to an Encoder template PDU.
func WithTemplateOption(option tpdu.Option) EncoderOption {
return templateOption{option}
}
var (
// AsSubmit indicates that generated PDUs will be of type SmsSubmit.
AsSubmit = templateOption{tpdu.SmsSubmit}
// AsDeliver indicates that generated PDUs will be of type SmsDeliver.
AsDeliver = templateOption{tpdu.SmsDeliver}
// As8Bit indicates that generated PDUs encode user data as 8bit.
As8Bit = templateOption{tpdu.Dcs8BitData}
// AsUCS2 indicates that generated PDUs encode user data as UCS2.
AsUCS2 = templateOption{tpdu.DcsUCS2Data}
// AsMO indicates that the TPDU originated from the mobile station.
AsMO = directionOption{tpdu.MO}
// AsMT indicates that the TPDU as destined for the mobile station.
AsMT = directionOption{tpdu.MT}
// WithAllCharsets specifies that all character sets are available for
// encoding or decoding.
//
// This is the default policy for decoding.
WithAllCharsets = AllCharsetsOption{}
// WithDefaultCharset specifies that only the default character set is
// available for encoding or decoding.
//
// This is the default policy for encoding.
WithDefaultCharset = CharsetOption{}
)
// To specifies the DA for a SMS-SUBMIT TPDU.
func To(number string) EncoderOption {
addr := tpdu.NewAddress(tpdu.FromNumber(number))
return templateOption{tpdu.WithDA(addr)}
}
// From specifies the OA for a SMS-DELIVER TPDU.
func From(number string) EncoderOption {
addr := tpdu.NewAddress(tpdu.FromNumber(number))
return templateOption{tpdu.WithOA(addr)}
}
// AllCharsetsOption specifies that all charactersets are available for encoding.
type AllCharsetsOption struct{}
// ApplyEncoderOption applies the AllCharsetsOption to an Encoder.
func (o AllCharsetsOption) ApplyEncoderOption(e *Encoder) {
e.eopts = append(e.eopts, tpdu.WithAllCharsets)
}
// WithCharset creates an CharsetOption.
func WithCharset(nli ...int) CharsetOption {
return CharsetOption{nli}
}
// CharsetOption defines the character sets available for encoding or decoding.
type CharsetOption struct {
nli []int
}
// ApplyEncoderOption applies the CharsetOption to an Encoder.
func (o CharsetOption) ApplyEncoderOption(e *Encoder) {
e.eopts = append(e.eopts, tpdu.WithCharset(o.nli...))
}
// ApplyDecodeOption applies the CharsetOption to decoding.
func (o CharsetOption) ApplyDecodeOption(cc *DecodeConfig) {
cc.dopts = append(cc.dopts, tpdu.WithCharset(o.nli...))
}
// WithLockingCharset creates an LockingCharsetOption.
func WithLockingCharset(nli ...int) LockingCharsetOption {
return LockingCharsetOption{nli}
}
// LockingCharsetOption defines the locking character sets available for
// encoding or decoding.
type LockingCharsetOption struct {
nli []int
}
// ApplyEncoderOption applies the LockingCharsetOption to an Encoder.
func (o LockingCharsetOption) ApplyEncoderOption(e *Encoder) {
e.eopts = append(e.eopts, tpdu.WithLockingCharset(o.nli...))
}
// ApplyDecodeOption applies the LockingCharsetOption to decoding.
func (o LockingCharsetOption) ApplyDecodeOption(cc *DecodeConfig) {
cc.dopts = append(cc.dopts, tpdu.WithLockingCharset(o.nli...))
}
// WithShiftCharset creates an ShiftCharsetOption.
func WithShiftCharset(nli ...int) ShiftCharsetOption {
return ShiftCharsetOption{nli}
}
// ShiftCharsetOption defines the shift character sets available for encoding
// or decoding.
type ShiftCharsetOption struct {
nli []int
}
// ApplyEncoderOption applies the ShiftCharsetOption to an Encoder.
func (o ShiftCharsetOption) ApplyEncoderOption(e *Encoder) {
e.eopts = append(e.eopts, tpdu.WithShiftCharset(o.nli...))
}
// ApplyDecodeOption applies the ShiftCharsetOption to decoding.
func (o ShiftCharsetOption) ApplyDecodeOption(cc *DecodeConfig) {
cc.dopts = append(cc.dopts, tpdu.WithShiftCharset(o.nli...))
}
type directionOption struct {
d tpdu.Direction
}
func (o directionOption) ApplyUnmarshalOption(d *UnmarshalConfig) {
d.dirn = o.d
}