changes to support sip REFER

This commit is contained in:
Dave Horton
2021-11-19 15:45:46 -05:00
parent af15449451
commit ec4f23ffec
3 changed files with 86 additions and 0 deletions

73
lib/tasks/sip_refer.js Normal file
View File

@@ -0,0 +1,73 @@
const Task = require('./task');
const {TaskName, TaskPreconditions} = require('../utils/constants');
/**
* sends a sip REFER to transfer the existing call
*/
class TaskSipRefer extends Task {
constructor(logger, opts) {
super(logger, opts);
this.preconditions = TaskPreconditions.StableCall;
this.referTo = this.data.referTo;
this.referredBy = this.data.referredBy;
this.headers = this.data.headers || {};
}
get name() { return TaskName.SipRefer; }
async exec(cs) {
super.exec(cs);
const {dlg} = cs;
const {referTo, referredBy} = this._normalizeReferHeaders(dlg);
try {
this.notifyHandler = this._handleNotify.bind(this, dlg);
dlg.on('notify', this.notifyHandler);
const response = await dlg.request({
method: 'REFER',
headers: {
...this.headers,
'Refer-To': referTo,
'Referred-By': referredBy
}
});
this.referStatus = response.status;
this.logger.info(`TaskSipRefer:exec - received ${this.referStatus} to REFER`);
//if (response.status === 202) this.awaitTaskDone();
} catch (err) {
this.logger.info({err}, 'TaskSipRefer:exec - error sending REFER');
}
}
async kill(cs) {
super.kill(cs);
const {dlg} = cs;
dlg.removeEventListener('notify', this.notifyHandler);
}
async _handleNotify(dlg, req, res) {
const contentType = req.get('Content-Type');
this.logger.info({body: req.body}, `TaskSipRefer:_handleNotify got ${contentType}`);
res.send(200);
}
_normalizeReferHeaders(dlg) {
let {referTo, referredBy} = this;
if (!referTo.startsWith('<') && !referTo.startsWith('sip') && !referTo.startsWith('"')) {
/* they may have only provided a phone number/user */
referTo = `sip:${referTo}@localhost`;
}
if (!referredBy) {
/* default */
referredBy = dlg.local.uri;
}
else if (!referredBy.startsWith('<') && !referredBy.startsWith('sip') && !referredBy.startsWith('"')) {
/* they may have only provided a phone number/user */
referredBy = `sip:${referredBy}@localhost`;
}
}
}
module.exports = TaskSipRefer;

View File

@@ -9,6 +9,18 @@
"status"
]
},
"sip:refer": {
"properties": {
"referTo": "string",
"referredBy": "string",
"headers": "object",
"actionHook": "object|string",
"eventHook": "object|string"
},
"required": [
"referTo"
]
},
"dequeue": {
"properties": {
"name": "string",

View File

@@ -18,6 +18,7 @@
"Redirect": "redirect",
"RestDial": "rest:dial",
"SipDecline": "sip:decline",
"SipRefer": "sip:refer",
"SipNotify": "sip:notify",
"SipRedirect": "sip:redirect",
"Say": "say",