Skip to content

Commit

Permalink
* Important: js-controller 3.0+ required!
Browse files Browse the repository at this point in the history
* (Apollon77) Remove some legacy code
* (Apollon77) Prevent potential crash case when error occurs
  • Loading branch information
Apollon77 committed Feb 20, 2022
1 parent 66f5ec1 commit 4aa89d2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
node-version: [12.x]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2021 bluefox <[email protected]>
Copyright (c) 2014-2022 bluefox <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ When the adapter crashes or an other Code error happens, this error message that
-->

## Changelog

### __WORK IN PROGRESS__
* Important: js-controller 3.0+ required!
* (Apollon77) Remove some legacy code
* (Apollon77) Prevent potential crash case when error occurs

### 2.0.5 (2021-06-29)
* (bluefox) Corrected error with token

Expand Down Expand Up @@ -167,7 +173,7 @@ When the adapter crashes or an other Code error happens, this error message that

The MIT License (MIT)

Copyright (c) 2014-2021 bluefox <[email protected]>
Copyright (c) 2014-2022 bluefox <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 10 additions & 2 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@
},
"dependencies": [
{
"admin": ">=3.0.0"
"js-controller": ">=3.0.0"
}
],
"globalDependencies": [
{
"admin": ">=4.0.9"
}
],
"extIcon": "https://raw.githubusercontent.com/ioBroker/ioBroker.pushover/master/admin/pushover.png",
Expand All @@ -252,6 +257,9 @@
"encryptedNative": [
"token"
],
"protectedNative": [
"token"
],
"native": {
"user": "",
"token": "",
Expand All @@ -260,4 +268,4 @@
"priority": 0
},
"objects": []
}
}
126 changes: 6 additions & 120 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ function startAdapter(options) {
Object.assign(options, {name: adapterName});
adapter = new utils.Adapter(options);

adapter.getEncryptedConfig = adapter.getEncryptedConfig || getEncryptedConfig;

try {
adapter.tools = adapter.tools || require(utils.controllerDir + '/lib/tools');
adapter.tools.migrateEncodedAttributes = adapter.tools.migrateEncodedAttributes || migrateEncodedAttributes;
} catch (e) {
adapter.tools = {decrypt, migrateEncodedAttributes};
}

adapter.on('message', obj => {
if (obj && obj.command === 'send' && obj.message) {
obj.message && processMessage(adapter, obj);
Expand All @@ -44,19 +35,7 @@ function startAdapter(options) {
});

adapter.on('ready', () => {
if (!adapter.supportsFeature || !adapter.supportsFeature('ADAPTER_AUTO_DECRYPT_NATIVE')) {
adapter.getEncryptedConfig('token')
.then(value => {
adapter.config.token = value;
main(adapter);
})
.catch(e => {
adapter.config.token = '';
main(adapter);
});
} else {
main(adapter);
}
main(adapter);
});

return adapter;
Expand Down Expand Up @@ -97,89 +76,6 @@ function processMessage(adapter, obj) {
}
}

function migrateEncodedAttributes(adapter, attrs, onlyRename) {
if (typeof attrs === 'string') {
attrs = [attrs];
}
const toMigrate = [];
attrs.forEach(attr =>
adapter.config[attr] !== undefined && adapter.config['enc_' + attr] === undefined && toMigrate.push(attr));

if (toMigrate.length) {
return new Promise((resolve, reject) => {
// read system secret
adapter.getForeignObject('system.config', null, (err, data) => {
let systemSecret;
if (data && data.native) {
systemSecret = data.native.secret;
}
if (systemSecret) {
// read instance configuration
adapter.getForeignObject('system.adapter.' + adapter.namespace, (err, obj) => {
if (obj && obj.native) {
toMigrate.forEach(attr => {
if (obj.native[attr]) {
if (onlyRename) {
obj.native['enc_' + attr] = obj.native[attr];
} else {
obj.native['enc_' + attr] = adapter.tools.encrypt(systemSecret, obj.native[attr]);
}
} else {
obj.native['enc_' + attr] = '';
}
delete obj.native[attr];
});
adapter.setForeignObject('system.adapter.' + adapter.namespace, obj, err => {
err && adapter.log.error(`Cannot write system.adapter.${adapter.namespace}: ${err}`);
!err && adapter.log.info('Attributes are migrated and adapter will be restarted');
err ? reject(err) : resolve(true);
});
} else {
adapter.log.error(`system.adapter.${adapter.namespace} not found!`);
reject(`system.adapter.${adapter.namespace} not found!`);
}
});
} else {
adapter.log.error('No system secret found!');
reject('No system secret found!');
}
});
})
} else {
return Promise.resolve(false);
}
}

function getEncryptedConfig(attribute, callback) {
if (adapter.config.hasOwnProperty(attribute)) {
if (typeof callback !== 'function') {
return new Promise((resolve, reject) => {
getEncryptedConfig(attribute, (err, encrypted) => {
if (err) {
reject(err);
} else {
resolve(encrypted);
}
});
});
} else {
adapter.getForeignObject('system.config', null, (err, data) => {
let systemSecret;
if (data && data.native) {
systemSecret = data.native.secret;
}
callback(null, adapter.tools.decrypt(systemSecret, adapter.config[attribute]));
});
}
} else {
if (typeof callback === 'function') {
callback('Attribute not found');
} else {
return Promise.reject('Attribute not found');
}
}
}

function sendGlances(adapter, obj) {
const json = JSON.stringify(obj.message);
if (lastMessageTime && lastMessageText === JSON.stringify(obj.message) && Date.now() - lastMessageTime < 1000) {
Expand Down Expand Up @@ -231,20 +127,6 @@ function sendGlances(adapter, obj) {
});
}

/**
* Decrypt the password/value with given key
* @param {string} key - Secret key
* @param {string} value - value to decript
* @returns {string}
*/
function decrypt(key, value) {
let result = '';
for(let i = 0; i < value.length; i++) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}

function main(adapter) {
// do nothing. Only answer on messages.
if (!adapter.config.user || !adapter.config.token) {
Expand Down Expand Up @@ -306,7 +188,11 @@ function sendNotification(adapter, message, callback) {

pushover.send(message, (err, result) => {
if (err) {
adapter.log.error('Cannot send notification: ' + JSON.stringify(err));
try {
adapter.log.error('Cannot send notification: ' + JSON.stringify(err));
} catch (err) {
adapter.log.error('Cannot send notification: Error');
}
callback && callback(err);
return false;
} else {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"dependencies": {
"pushover-notifications": "^1.2.2",
"@iobroker/adapter-core": "^2.5.1",
"axios": "^0.24.0"
"axios": "^0.26.0"
},
"devDependencies": {
"@alcalzone/release-script": "^2.2.1",
"@alcalzone/release-script": "^2.2.2",
"gulp": "^4.0.2",
"mocha": "^9.2.0",
"mocha": "^9.2.1",
"chai": "^4.3.6",
"@iobroker/testing": "^2.5.3"
"@iobroker/testing": "^2.5.4"
},
"bugs": {
"url": "https://github.com/ioBroker/ioBroker.pushover/issues"
Expand Down

0 comments on commit 4aa89d2

Please sign in to comment.