-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoder.go
127 lines (111 loc) · 2.91 KB
/
encoder.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
package pdu
//import "gopkg.in/webnice/debug.v1"
//import "gopkg.in/webnice/log.v2"
import (
"time"
uc "unicode"
"gopkg.in/webnice/pdu.v1/encoders"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// Set Service Centre Address number
func (pdu *impl) setTpSca(m *message, sca string) (err error) {
if len(sca) == 0 {
return
}
if sca[0] == '+' {
sca = sca[1:]
}
m.TpScaType = NumberTypeInternational
m.TpScaNumericPlan = NumericPlanInternational
m.TpScaNumber = sca
if !rexNumeric.MatchString(m.TpScaNumber) && len(m.TpScaNumber) > 0 {
m.TpScaType = NumberTypeAlphanumeric
m.TpScaNumericPlan = NumericPlanAlphanumeric
}
m.TpScaTypeSource = encodeNumberType(m.TpScaType, m.TpScaNumericPlan)
m.TpScaLen = uint8(len(m.TpScaNumber))
return
}
// Set Originating address number
func (pdu *impl) setTpOa(m *message, addr string) (err error) {
if len(addr) == 0 {
err = ErrNoValudRecipientNumber
return
}
if addr[0] == '+' {
addr = addr[1:]
}
m.TpOaNumber = addr
switch rexNumeric.MatchString(m.TpOaNumber) {
case true:
m.TpOaType = NumberTypeInternational
m.TpOaNumericPlan = NumericPlanInternational
if len(m.TpOaNumber) <= 5 {
m.TpOaType = NumberTypeUnknown
}
case false:
m.TpOaType = NumberTypeAlphanumeric
m.TpOaNumericPlan = NumericPlanAlphanumeric
}
m.TpOaTypeSource = encodeNumberType(m.TpOaType, m.TpOaNumericPlan)
m.TpOaLen = uint8(len(m.TpOaNumber))
return
}
// Check all symbols in message
func (pdu *impl) checkUSC2Symbols(message string) (ret bool) {
var sym rune
for _, sym = range message {
if sym > uc.MaxASCII || !uc.IsPrint(sym) {
ret = true
}
}
return
}
// Encoder SMS encoder
func (pdu *impl) Encoder(inp Encode) (ret []string, err error) {
var m *message
m = new(message)
m.Dir = DirectionOutgoing
m.CreateTime = time.Now().In(time.Local)
// Set Service Centre Address number
if err = pdu.setTpSca(m, inp.Sca); err != nil {
return
}
// Set flags
m.DscUSC2 = inp.Ucs2
m.DscFlash = inp.Flash
m.MtiReplyPath = inp.StatusReportRequest
m.TpRdRejectDuplicates = inp.RejectDuplicates
// Set UTF-16 encode if found any non ascii symbol
if !m.DscUSC2 {
m.DscUSC2 = pdu.checkUSC2Symbols(inp.Message)
}
// Set originating address number
if err = pdu.setTpOa(m, inp.Address); err != nil {
return
}
// Encoding body of sms
if m.DscUSC2 {
var enc = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
var tnsf = enc.NewEncoder()
m.SmsDataSource, m.SmsDataLength, err = transform.Bytes(tnsf, []byte(inp.Message))
if err != nil {
return
}
//m.SmsData = hex.EncodeToString(m.SmsDataSource)
m.SmsDataLength = len(m.SmsDataSource)
} else {
m.SmsDataSource = encoders.New7Bit().Encode(inp.Message)
m.SmsDataLength = len(inp.Message)
//m.SmsData = hex.EncodeToString(m.SmsDataSource)
}
// Set multipart message
if m.SmsDataLength > _MaxBytes {
m.MtiUdhiFound = true
}
// Message encode
ret = m.Encode()
err = m.Err
return
}