Skip to content

Commit

Permalink
fix: fixed parsing of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism authored Jul 27, 2024
1 parent a494940 commit 577da94
Showing 1 changed file with 101 additions and 51 deletions.
152 changes: 101 additions & 51 deletions imap-core/lib/commands/xapplepushservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
module.exports = {
state: ['Authenticated', 'Selected'],

schema: [
/*
Schema: [
{
name: 'aps-version',
type: 'number' // always 2
Expand All @@ -34,63 +35,112 @@ module.exports = {
type: 'string' // e.g. (INBOX Notes)
}
],
*/

handler(command, callback) {
const version = Buffer.from((command.attributes[0] && command.attributes[0].value) || '', 'binary').toString();
if (version !== "2")
return callback(null, {
response: 'NO',
code: 'CLIENTBUG'
});
// it's actually something like this in production
// [
// { type: 'ATOM', value: 'aps-version' },
// { type: 'ATOM', value: '2' },
// { type: 'ATOM', value: 'aps-account-id' },
// { type: 'ATOM', value: 'xxxxxxx' },
// { type: 'ATOM', value: 'aps-device-token' },
// {
// type: 'ATOM',
// value: 'xxxxxx'
// },
// { type: 'ATOM', value: 'aps-subtopic' },
// { type: 'ATOM', value: 'com.apple.mobilemail' },
// { type: 'ATOM', value: 'mailboxes' },
// [
// { type: 'STRING', value: 'Sent Mail' },
// { type: 'STRING', value: 'INBOX' }
// ]
// ]

const accountID = Buffer.from((command.attributes[1] && command.attributes[1].value) || '', 'binary').toString();
const deviceToken = Buffer.from((command.attributes[2] && command.attributes[2].value) || '', 'binary').toString();
const subTopic = Buffer.from((command.attributes[3] && command.attributes[3].value) || '', 'binary').toString();
// disabled for now
schema: false,

if (subTopic !== "com.apple.mobilemail")
return callback(null, {
response: 'NO',
code: 'CLIENTBUG'
});
handler(command, callback) {
// Command = {
// tag: 'I5',
// command: 'XAPPLEPUSHSERVICE',
// attributes: [
// { type: 'ATOM', value: 'aps-version' }, // 0
// { type: 'ATOM', value: '2' }, // 1
// { type: 'ATOM', value: 'aps-account-id' }, // 2
// { type: 'ATOM', value: 'xxxxxx' }, // 3
// { type: 'ATOM', value: 'aps-device-token' }, // 4
// { // 5
// type: 'ATOM',
// value: 'xxxxxx'
// },
// { type: 'ATOM', value: 'aps-subtopic' }, // 6
// { type: 'ATOM', value: 'com.apple.mobilemail' }, // 7
// { type: 'ATOM', value: 'mailboxes' }, // 8
// [ // 9
// { type: 'STRING', value: 'Sent Mail' },
// { type: 'STRING', value: 'INBOX' }
// ]
// ]
// }

// NOTE: mailboxes param is not used at this time (it's a list anyways too)
const mailboxes = Buffer.from((command.attributes[4] && command.attributes[4].value) || '', 'binary').toString();
const version = (command.attributes[1] && command.attributes[1].value) || '';
if (version !== '2') {
return callback(null, {
response: 'NO',
code: 'CLIENTBUG',
});
}

if (typeof this._server.onXAPPLEPUSHSERVICE !== 'function') {
return callback(null, {
response: 'NO',
message: command.command + ' not implemented'
});
}
const accountID = (command.attributes[3] && command.attributes[3].value) || '';
const deviceToken = (command.attributes[5] && command.attributes[5].value) || '';
const subTopic = (command.attributes[7] && command.attributes[7].value) || '';

if (subTopic !== 'com.apple.mobilemail') {
return callback(null, {
response: 'NO',
code: 'CLIENTBUG',
});
}

// NOTE: mailboxes param is not used at this time (it's a list anyways too)
const mailboxes = command.attributes[9] && Array.isArray(command.attributes[9]) && command.attributes[9].length > 0 ? command.attributes[9].map(object => object.value) : [];

if (typeof this._server.onXAPPLEPUSHSERVICE !== 'function') {
return callback(null, {
response: 'NO',
message: command.command + ' not implemented',
});
}

let logdata = {
short_message: '[XAPPLEPUSHSERVICE]',
_mail_action: 'xapplepushservice',
_accountId: accountID,
_deviceToken: deviceToken,
_subTopic: subTopic,
_mailboxes: mailboxes,
_user: this.session.user.id.toString(),
_sess: this.id
};
const logdata = {
short_message: '[XAPPLEPUSHSERVICE]',
_mail_action: 'xapplepushservice',
_accountId: accountID,
_deviceToken: deviceToken,
_subTopic: subTopic,
_mailboxes: mailboxes,
_user: this.session.user.id.toString(),
_sess: this.id,
};

this._server.onXAPPLEPUSHSERVICE(accountID, deviceToken, subTopic, mailboxes, this.session, (err) => {
if (err) {
logdata._error = err.message;
logdata._code = err.code;
logdata._response = err.response;
this._server.loggelf(logdata);
this._server.onXAPPLEPUSHSERVICE(accountID, deviceToken, subTopic, mailboxes, this.session, error => {
if (error) {
logdata._error = error.message;
logdata._code = error.code;
logdata._response = error.response;
this._server.loggelf(logdata);

return callback(null, {
response: 'NO',
code: 'TEMPFAIL'
});
}
return callback(null, {
response: 'NO',
code: 'TEMPFAIL',
});
}

callback(null, {
response: 'OK',
message: 'Success'
});
});
}
callback(null, {
response: 'OK',
message: 'Success',
});
});
},
};

0 comments on commit 577da94

Please sign in to comment.