Skip to content

Commit

Permalink
Add JsSIP.URI and JsSIP.NameAddrHeader classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Jan 31, 2013
2 parents f4f9cf3 + 7b345bf commit 9fad09b
Show file tree
Hide file tree
Showing 14 changed files with 608 additions and 263 deletions.
2 changes: 2 additions & 0 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = function(grunt) {
"src/Transport.js",
"src/Parser.js",
"src/SIPMessage.js",
"src/URI.js",
"src/NameAddrHeader.js",
"src/Transactions.js",
"src/Dialogs.js",
"src/RequestSender.js",
Expand Down
8 changes: 4 additions & 4 deletions src/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ JsSIP.Message.prototype.send = function(target, body, contentType, options) {
}

// Check target validity
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
if (!target) {
throw new JsSIP.exceptions.InvalidTargetError();
}

// Message parameter initialization
this.direction = 'outgoing';
this.local_identity = this.ua.configuration.user;
this.local_identity = this.ua.configuration.from_uri;
this.remote_identity = target;

this.closed = false;
Expand Down Expand Up @@ -164,8 +164,8 @@ JsSIP.Message.prototype.init_incoming = function(request) {

this.direction = 'incoming';
this.request = request;
this.local_identity = request.s('to').uri;
this.remote_identity = request.s('from').uri;
this.local_identity = request.s('to').uri.toAor();
this.remote_identity = request.s('from').uri.toAor();

if (contentType && (contentType.match(/^text\/plain(\s*;\s*.+)*$/i) || contentType.match(/^text\/html(\s*;\s*.+)*$/i))) {
this.ua.emit('newMessage', this.ua, {
Expand Down
86 changes: 86 additions & 0 deletions src/NameAddrHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @augments JsSIP
* @class Class creating a Name Address SIP header.
*
* @param {JsSIP.URI} uri
* @param {String} [display_name]
* @param {Object} [parameters]
*
*/
JsSIP.NameAddrHeader = function(uri, display_name, parameters) {
var param;

// Checks
if(!uri || !uri instanceof JsSIP.URI) {
console.warn('Missing or invalid "uri" in NameAddrHeader');
throw new JsSIP.exceptions.InvalidValueError();
}

// Initialize parameters
this.uri = uri;
this.parameters = {};

for (param in parameters) {
this.setParam(param, parameters[param]);
}

Object.defineProperties(this, {
display_name: {
get: function() { return display_name; },
set: function(value) {
display_name = value;
}
}
});
};
JsSIP.NameAddrHeader.prototype = {
setParam: function(key, value) {
if (key) {
this.parameters[key.toLowerCase()] = (typeof value === 'undefined' || value === null)? null : value.toString();
}
},

getParam: function(key) {
if(key) {
return this.parameters[key.toLowerCase()];
}
},

hasParam: function(key) {
if(key) {
return this.parameters.hasOwnProperty(key.toLowerCase()) && true || false;
}
},

deleteParam: function(parameter) {
parameter = parameter.toLowerCase();
if (this.parameters.hasOwnProperty(parameter)) {
delete this.parameters[parameter];
}
},

clearParams: function() {
this.parameters = {};
},

clone: function() {
return new JsSIP.NameAddrHeader(
this.uri.clone(),
this.display_name,
window.JSON.parse(window.JSON.stringify(this.parameters)));
},

toString: function() {
var body, parameter;

body = (this.display_name) ? '"' + this.display_name + '" ' : '';
body += (this.display_name) ? '<' + this.uri.toString() + '>' : this.uri.toString();


for (parameter in this.parameters) {
body += ';' + parameter;
body += (this.parameters[parameter] === null)? '' : '=' + this.parameters[parameter];
}
return body;
}
};
6 changes: 3 additions & 3 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ JsSIP.Parser = {
parsed = message.parseHeader('from');
if(parsed) {
message.from = parsed;
message.from_tag = parsed.tag;
message.from_tag = parsed.getParam('tag');
}
break;
case 'to':
Expand All @@ -82,7 +82,7 @@ JsSIP.Parser = {
parsed = message.parseHeader('to');
if(parsed) {
message.to = parsed;
message.to_tag = parsed.tag;
message.to_tag = parsed.getParam('tag');
}
break;
case 'record-route':
Expand Down Expand Up @@ -191,7 +191,7 @@ JsSIP.Parser = {
} else if(!parsed.status_code) {
message = new JsSIP.IncomingRequest();
message.method = parsed.method;
message.ruri = parsed;
message.ruri = parsed.uri;
} else {
message = new JsSIP.IncomingResponse();
message.status_code = parsed.status_code;
Expand Down
10 changes: 5 additions & 5 deletions src/Registrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ JsSIP.Registrator.prototype = {
while(contacts--) {
contact = response.parseHeader('contact', contacts);
if(contact.uri === this.ua.contact.uri) {
expires = contact.params.expires;
expires = contact.getParam('expires');
break;
}
}
Expand All @@ -115,11 +115,11 @@ JsSIP.Registrator.prototype = {
}, (expires * 1000) - 3000);

//Save gruu values
if (contact.params['temp-gruu']) {
this.ua.contact.temp_gruu = contact.params['temp-gruu'].replace(/"/g,'');
if (contact.hasParam('temp-gruu')) {
this.ua.contact.temp_gruu = contact.getParam('temp-gruu').replace(/"/g,'');
}
if (contact.params['pub-gruu']) {
this.ua.contact.pub_gruu = contact.params['pub-gruu'].replace(/"/g,'');
if (contact.hasParam('pub-gruu')) {
this.ua.contact.pub_gruu = contact.getParam('pub-gruu').replace(/"/g,'');
}

this.registered = true;
Expand Down
4 changes: 2 additions & 2 deletions src/SIPMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod

if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
to += ';tag=' + this.to_tag;
}

Expand Down Expand Up @@ -435,7 +435,7 @@ JsSIP.IncomingRequest.prototype.reply_sl = function(code, reason) {

if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
to += ';tag=' + this.to_tag;
}

Expand Down
2 changes: 1 addition & 1 deletion src/SanityCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ JsSIP.sanityCheck = (function() {

// Sanity Check functions for requests
function rfc3261_8_2_2_1() {
if(message.s('to').scheme !== 'sip') {
if(message.s('to').uri.scheme !== 'sip') {
reply(416);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ JsSIP.Session.prototype.connect = function(target, options) {
}

// Check target validity
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
if (!target) {
throw new JsSIP.exceptions.InvalidTargetError();
}
Expand Down Expand Up @@ -746,10 +746,10 @@ JsSIP.Session.prototype.newSession = function(originator, request, target) {
session.direction = (originator === 'local') ? 'outgoing' : 'incoming';

if (originator === 'remote') {
session.local_identity = request.s('to').uri;
session.remote_identity = request.s('from').uri;
session.local_identity = request.s('to').uri.toAor();
session.remote_identity = request.s('from').uri.toAor();
} else if (originator === 'local'){
session.local_identity = session.ua.configuration.user;
session.local_identity = session.ua.configuration.from_uri;
session.remote_identity = target;
}

Expand Down
Loading

0 comments on commit 9fad09b

Please sign in to comment.