-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (123 loc) · 2.97 KB
/
index.js
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
const debug = require('debug')('sms')
const RPDU = require('./lib/rpdu')
const TPDU = require('./lib/tpdu')
const { UD } = require('./lib/ud')
const { UDH } = require('./lib/udh')
const alphabets = require('./lib/alphabets')
const tplib = require('./lib/tplib')
const rplib = require('./lib/rplib')
function ack (message) {
const rpdu = RPDU.ACK.from({
reference: message.reference,
content: message.content
})
return rpdu.encode()
}
function error (message) {
const rpdu = RPDU.ERROR.from({
reference: message.reference,
errorCode: message.errorCode,
content: message.content
})
return rpdu.encode()
}
function submit (message) {
// todo pass message as a buffer, if have from smpp
const reference = message.reference ?? Math.floor(Math.random() * 256)
const tpdu = TPDU.SUBMIT.from({
reference,
destination: message.destination,
report: message.report,
pid: message.pid,
dcs: message.dcs,
coding: message.coding,
udh: message.udh,
content: message.content
})
const rpdu = RPDU.DATA.from({
reference,
source: message.source,
destination: message.smsc,
content: tpdu.encode()
})
const buffer = rpdu.encode()
return buffer
}
function deliver (message) {
const reference = message.reference || Math.floor(Math.random() * 0xff)
// todo pass message as a buffer, if have from smpp
const tpdu = TPDU.DELIVER.from({
source: message.source,
dcs: message.dcs,
coding: message.coding,
udh: message.udh,
content: message.content
})
// debug(tpdu)
const tpBuffer = tpdu.encode()
// debug('built TPDU buffer', tpBuffer)
const rpdu = RPDU.DATA.from({
reference,
source: message.smsc,
content: tpBuffer
}, true)
debug(rpdu)
return rpdu.encode()
}
function smma (message) {
const reference = message.reference || Math.floor(Math.random() * 0xff)
const rpdu = RPDU.SMMA.from({
reference
})
return rpdu.encode()
}
function command (message) {
// todo explain reference usage
const reference = message.reference || Math.floor(Math.random() * 0xff)
debug(message)
// todo pass message as a buffer, if have from smpp
const tpdu = TPDU.COMMAND.from({
reference,
report: message.report,
destination: message.destination,
command: message.command,
message_number: message.message_number,
content: message.content
})
debug(tpdu)
const tpBuffer = tpdu.encode()
debug('TPDU buffer', tpBuffer)
const rpdu = RPDU.DATA.from({
reference,
destination: message.smsc,
content: tpdu.encode()
})
debug(rpdu)
return rpdu.encode()
}
function decode (buffer) {
const rpMessage = RPDU.decode(buffer)
if (!rpMessage) return
const message = rpMessage
if (rpMessage.content) {
const tpMessage = TPDU.decode(rpMessage)
message.tpdu = tpMessage
}
return message
}
module.exports = {
decode,
command,
smma,
deliver,
submit,
error,
ack,
RPDU,
TPDU,
UD,
UDH,
alphabets,
tplib,
rplib
}