Comparing version 0.0.2 to 0.0.3
@@ -239,10 +239,10 @@ var types = { | ||
} | ||
var result; | ||
if (typeof value == 'string') { | ||
result = value; | ||
if (result.length <= 12) { | ||
result = ('000000000000' + result).substr(-12) + '000R'; | ||
if (value.length <= 12) { | ||
value = ('000000000000' + value).substr(-12) + '000R'; | ||
} | ||
} else if (value instanceof Date) { | ||
result = value.getUTCFullYear().toString().substr(-2); | ||
return value; | ||
} | ||
if (value instanceof Date) { | ||
var result = value.getUTCFullYear().toString().substr(-2); | ||
result += ('0' + (value.getUTCMonth() + 1)).substr(-2); | ||
@@ -255,7 +255,8 @@ result += ('0' + value.getUTCDate()).substr(-2); | ||
result += '00+'; | ||
return result; | ||
} | ||
return result; | ||
return value; | ||
}, | ||
decode: function(value) { | ||
if (!value) { | ||
if (!value || typeof value != 'string') { | ||
return value; | ||
@@ -318,2 +319,5 @@ } | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
var encoding = this.data_coding & 0x0F; | ||
@@ -351,2 +355,5 @@ var ucs2 = encoding == consts.ENCODING.UCS2; | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
return { | ||
@@ -379,2 +386,5 @@ format: value.readUInt8(0), | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
var result = { | ||
@@ -402,2 +412,5 @@ format: value.readUInt8(0), | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
return { | ||
@@ -421,5 +434,8 @@ network: value.readUInt8(0), | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
return { | ||
unit: buffer.readUInt8(0), | ||
interval: buffer.readUInt16BE(1) | ||
unit: value.readUInt8(0), | ||
interval: value.readUInt16BE(1) | ||
}; | ||
@@ -442,2 +458,5 @@ } | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
return { | ||
@@ -466,2 +485,5 @@ digit_mode: value.readUInt8(0), | ||
decode: function(value) { | ||
if (!Buffer.isBuffer(value)) { | ||
return value; | ||
} | ||
return { | ||
@@ -748,3 +770,3 @@ encoding: value.readUInt8(0), | ||
for (var tag in tlvs) if (tlvs.hasOwnProperty(tag)) { | ||
for (var tag in tlvs) { | ||
tlvsById[tlvs[tag].id] = tlvs[tag]; | ||
@@ -1063,3 +1085,3 @@ tlvs[tag].tag = tag; | ||
for (var command in commands) if (commands.hasOwnProperty(command)) { | ||
for (var command in commands) { | ||
commandsById[commands[command].id] = commands[command]; | ||
@@ -1125,2 +1147,3 @@ commands[command].command = command; | ||
ISO_8859_1: 0x03, | ||
BINARY: 0x04, | ||
JIS: 0x05, | ||
@@ -1127,0 +1150,0 @@ X_0208_1990: 0x05, |
@@ -15,4 +15,4 @@ var defs = require('./defs'), | ||
function PDU(command, options) { | ||
if (Buffer.isBuffer(arguments[0])) { | ||
return this.fromBuffer(arguments[0]); | ||
if (Buffer.isBuffer(command)) { | ||
return this.fromBuffer(command); | ||
} | ||
@@ -26,10 +26,6 @@ options = options || {}; | ||
var params = commands[command].params || {}; | ||
for (var key in params) if (params.hasOwnProperty(key)) { | ||
if (key in options) { | ||
this[key] = options[key]; | ||
} else { | ||
this[key] = params[key].default || params[key].type.default; | ||
} | ||
for (var key in params) { | ||
this[key] = options[key] || params[key].default || params[key].type.default; | ||
} | ||
for (var key in options) if (options.hasOwnProperty(key) && key in tlvs) { | ||
for (var key in options) if (key in tlvs) { | ||
this[key] = options[key]; | ||
@@ -54,2 +50,5 @@ } | ||
if (this.command == 'unknown') { | ||
if (!('command_status' in options)) { | ||
options.command_status = defs.errors.ESME_RINVCMDID; | ||
} | ||
return new PDU('generic_nack', options); | ||
@@ -72,4 +71,3 @@ } | ||
} | ||
var filters = {}; | ||
for (var key in params) if (params.hasOwnProperty(key)) { | ||
for (var key in params) { | ||
if (offset >= this.command_length) { | ||
@@ -80,5 +78,2 @@ break; | ||
offset += params[key].type.size(this[key]); | ||
if (params[key].filter) { | ||
filters[key] = params[key].filter; | ||
} | ||
} | ||
@@ -105,28 +100,28 @@ while (offset < this.command_length) { | ||
offset += length; | ||
if (tlv.filter) { | ||
filters[key] = tlv.filter; | ||
} | ||
} | ||
for (var key in filters) if (filters.hasOwnProperty(key)) { | ||
this[key] = filters[key].decode.call(this, this[key]); | ||
} | ||
this._filter('decode'); | ||
}; | ||
PDU.prototype.finalize = function() { | ||
PDU.prototype._filter = function(func) { | ||
var params = commands[this.command].params || {}; | ||
for (var key in this) if (this.hasOwnProperty(key)) { | ||
for (var key in this) { | ||
if (params[key] && params[key].filter) { | ||
this[key] = params[key].filter.encode.call(this, this[key]); | ||
this[key] = params[key].filter[func].call(this, this[key]); | ||
} else if (tlvs[key] && tlvs[key].filter) { | ||
if (tlvs[key].multiple) { | ||
this[key].forEach(function(value, i) { | ||
this[key][i] = tlvs[key].filter.encode.call(this, value); | ||
this[key][i] = tlvs[key].filter[func].call(this, value); | ||
}.bind(this)); | ||
} else { | ||
this[key] = tlvs[key].filter.encode.call(this, this[key]); | ||
this[key] = tlvs[key].filter[func].call(this, this[key]); | ||
} | ||
} | ||
} | ||
}; | ||
PDU.prototype.toBuffer = function() { | ||
this._filter('encode'); | ||
var params = commands[this.command].params || {}; | ||
this.command_length = 16; | ||
for (var key in this) if (this.hasOwnProperty(key)) { | ||
for (var key in this) { | ||
if (params[key]) { | ||
@@ -141,6 +136,2 @@ this.command_length += params[key].type.size(this[key]); | ||
} | ||
}; | ||
PDU.prototype.toBuffer = function() { | ||
this.finalize(); | ||
var buffer = new Buffer(this.command_length); | ||
@@ -151,8 +142,7 @@ pduHeadParams.forEach(function(key, i) { | ||
var offset = 16; | ||
var params = commands[this.command].params || {}; | ||
for (var key in params) if (params.hasOwnProperty(key)) { | ||
for (var key in params) { | ||
params[key].type.write(this[key], buffer, offset); | ||
offset += params[key].type.size(this[key]); | ||
} | ||
for (var key in this) if (this.hasOwnProperty(key) && tlvs[key]) { | ||
for (var key in this) if (tlvs[key]) { | ||
var values = tlvs[key].multiple ? this[key] : [this[key]]; | ||
@@ -159,0 +149,0 @@ values.forEach(function(value) { |
@@ -98,3 +98,3 @@ var net = require('net'), | ||
for (var command in defs.commands) if (defs.commands.hasOwnProperty(command)) { | ||
for (var command in defs.commands) { | ||
Session.prototype[command] = createShortcut(command); | ||
@@ -144,3 +144,3 @@ } | ||
exports.createSession = function(host, port) { | ||
exports.connect = exports.createSession = function(host, port) { | ||
return new Session({ | ||
@@ -168,10 +168,10 @@ host: host || 'localhost', | ||
exports.PDU = PDU; | ||
for (var key in defs) if (defs.hasOwnProperty(key)) { | ||
for (var key in defs) { | ||
exports[key] = defs[key]; | ||
} | ||
for (var error in defs.errors) if (defs.errors.hasOwnProperty(error)) { | ||
for (var error in defs.errors) { | ||
exports[error] = defs.errors[error]; | ||
} | ||
for (var key in defs.consts) if (defs.consts.hasOwnProperty(key)) { | ||
for (var key in defs.consts) { | ||
exports[key] = defs.consts[key]; | ||
} |
{ | ||
"name": "smpp", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "SMPP client and server implementation in node.js", | ||
@@ -5,0 +5,0 @@ "author": "Ali Farhadi <a.farhadi@gmail.com>", |
114
README.md
@@ -5,4 +5,2 @@ node-smpp | ||
[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=farhadi&url=https://github.com/farhadi/node-smpp&title=node-smpp&language=en_GB&tags=github&category=software) | ||
Introduction | ||
@@ -15,3 +13,3 @@ ------------ | ||
the names defined in SMPP specification. So get a copy of | ||
[SMPP v5.0 Specification](http://www.smsforum.net/smppv50.pdf.zip) | ||
[SMPP v5.0 Specification](http://farhadi.ir/downloads/smppv50.pdf) | ||
for a list of available operations and their parameters. | ||
@@ -31,21 +29,21 @@ | ||
``` javascript | ||
var smpp = require('smpp'); | ||
var session = smpp.createSession('example.com', 2775); | ||
session.bind_transceiver({ | ||
system_id: 'YOUR_SYSTEM_ID', | ||
password: 'YOUR_PASSWORD' | ||
}, function(pdu) { | ||
if (pdu.command_status == 0) { | ||
// Successfully bound | ||
session.submit_sm({ | ||
destination_addr: 'DESTINATION NUMBER', | ||
short_message: 'Hello!' | ||
}, function(pdu) { | ||
if (pdu.command_status == 0) { | ||
// Message successfully sent | ||
console.log(pdu.message_id); | ||
} | ||
}); | ||
} | ||
}); | ||
var smpp = require('smpp'); | ||
var session = smpp.connect('example.com', 2775); | ||
session.bind_transceiver({ | ||
system_id: 'YOUR_SYSTEM_ID', | ||
password: 'YOUR_PASSWORD' | ||
}, function(pdu) { | ||
if (pdu.command_status == 0) { | ||
// Successfully bound | ||
session.submit_sm({ | ||
destination_addr: 'DESTINATION NUMBER', | ||
short_message: 'Hello!' | ||
}, function(pdu) { | ||
if (pdu.command_status == 0) { | ||
// Message successfully sent | ||
console.log(pdu.message_id); | ||
} | ||
}); | ||
} | ||
}); | ||
``` | ||
@@ -56,22 +54,22 @@ | ||
``` javascript | ||
var smpp = require('smpp'); | ||
var server = smpp.createServer(function(session) { | ||
session.on('bind_transceiver', function(pdu) { | ||
// we pause the session to prevent further incoming pdu events, | ||
// untill we authorize the session with some async operation. | ||
session.pause(); | ||
checkAsyncUserPass(pdu.system_id, pdu.password, function(err) { | ||
if (err) { | ||
session.send(pdu.response({ | ||
command_status: smpp.ESME_RINVPASWD // (Invaid Password) | ||
})); | ||
session.close(); | ||
return; | ||
} | ||
session.send(pdu.response()); | ||
session.resume(); | ||
}); | ||
}); | ||
}); | ||
server.listen(2775); | ||
var smpp = require('smpp'); | ||
var server = smpp.createServer(function(session) { | ||
session.on('bind_transceiver', function(pdu) { | ||
// we pause the session to prevent further incoming pdu events, | ||
// untill we authorize the session with some async operation. | ||
session.pause(); | ||
checkAsyncUserPass(pdu.system_id, pdu.password, function(err) { | ||
if (err) { | ||
session.send(pdu.response({ | ||
command_status: smpp.ESME_RBINDFAIL | ||
})); | ||
session.close(); | ||
return; | ||
} | ||
session.send(pdu.response()); | ||
session.resume(); | ||
}); | ||
}); | ||
}); | ||
server.listen(2775); | ||
``` | ||
@@ -124,4 +122,4 @@ | ||
``` javascript | ||
var pdu = new smpp.PDU('submit_sm', options); | ||
session.send(pdu, callback); | ||
var pdu = new smpp.PDU('submit_sm', options); | ||
session.send(pdu, callback); | ||
``` | ||
@@ -133,3 +131,3 @@ | ||
#### Event: 'close' | ||
Emitted when the conntion is fully closed. | ||
Emitted when the connection is fully closed. | ||
@@ -232,17 +230,17 @@ #### Event: 'error' `(error)` | ||
``` javascript | ||
session.on('submit_sm', function(pdu) { | ||
var msgid = .... ; // generate a message_id for this message. | ||
session.send(pdu.response({ | ||
message_id: msgid | ||
})); | ||
}); | ||
session.on('submit_sm', function(pdu) { | ||
var msgid = .... ; // generate a message_id for this message. | ||
session.send(pdu.response({ | ||
message_id: msgid | ||
})); | ||
}); | ||
session.on('unbind', function(pdu) { | ||
session.send(pdu.response()); | ||
session.close(); | ||
}); | ||
session.on('unbind', function(pdu) { | ||
session.send(pdu.response()); | ||
session.close(); | ||
}); | ||
session.on('enquire_link', function(pdu) { | ||
session.send(pdu.response()); | ||
}); | ||
session.on('enquire_link', function(pdu) { | ||
session.send(pdu.response()); | ||
}); | ||
``` | ||
@@ -249,0 +247,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
1622
0
51119
249
2