module.exports = function(RED){ const dav = require('dav') const https = require('https') function NextcloudCardDav (config) { RED.nodes.createNode(this, config) this.server = RED.nodes.getNode(config.server) this.addressBook = config.addressBook const node = this node.on('input', (msg) => { const xhr = new dav.transport.Basic( new dav.Credentials({ username: node.server.credentials.user, password: node.server.credentials.pass }) ) // Server + Basepath let cardDavUri = node.server.address + '/remote.php/dav/addressbooks/users/' // User cardDavUri += node.server.credentials.user + '/' // ToDo Filter ? dav.createAccount({ server: cardDavUri, xhr: xhr, accountType: 'carddav' }) .then(function (account) { if (!account.addressBooks) { node.error('Nextcloud:CardDAV -> no addressbooks found.') return } // account instanceof dav.Account account.addressBooks.forEach(function (addressBook) { // Wenn Adressbuch gesetzt ist, dann nur diesen abrufen let c = msg.addressBook || node.addressBook if (!c || !c.length || (c && c.length && c === addressBook.displayName)) { dav.listVCards(addressBook, { xhr: xhr }) .then(function (addressBookEntries) { let vcfList = { 'payload': { 'name': addressBook.displayName, 'data': [] } } addressBookEntries.forEach(function (addressBookEntry) { const keyValue = addressBookEntry.addressData.split('\n') let vcfJson = {} for (let x = 0; x < keyValue.length; x++) { const temp = keyValue[x].split(':') vcfJson[temp[0]] = temp[1] } vcfList.payload.data.push(vcfJson) }) node.send(vcfList) }, function () { node.error('Nextcloud:CardDAV -> get cards went wrong.') }) } }) }, function () { node.error('Nextcloud:CardDAV -> get addressBooks went wrong.') }) }) } RED.nodes.registerType('nextcloud-carddav', NextcloudCardDav) }