Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2017-2021 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
| 5 | * Author: Asad Salman <me@asad.co> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | const Account = require('./model/Account') |
| 21 | |
| 22 | "use strict"; |
| 23 | class JamiDaemon { |
| 24 | constructor() { |
| 25 | this.accounts = [] |
| 26 | this.dring = require("./dring.node") |
| 27 | this.dring.init({ |
| 28 | "AccountsChanged": () => { |
| 29 | console.log("AccountsChanged") |
| 30 | const newAccounts = [] |
| 31 | this.stringVectToArr(this.dring.getAccountList()).forEach(accountId => { |
| 32 | for (const account in this.accounts) { |
| 33 | if (account.id === accountId) { |
| 34 | newAccounts.push(account) |
| 35 | return |
| 36 | } |
| 37 | } |
| 38 | newAccounts.push(new Account(accountId, |
| 39 | this.mapToJs(this.dring.getAccountDetails(accountId)) |
| 40 | //this.mapToJs(this.dring.getVolatileDetails(accountId)), |
| 41 | )) |
| 42 | }) |
| 43 | this.accounts = newAccounts |
| 44 | }, |
| 45 | "AccountDetailsChanged": (accountId, details) => { |
| 46 | console.log(`AccountDetailsChanged ${accountId}`) |
| 47 | const account = this.getAccount(accountId) |
| 48 | if (!account) { |
| 49 | console.log(`Unknown account ${accountId}`) |
| 50 | return |
| 51 | } |
| 52 | account.details = details |
| 53 | }, |
| 54 | "VolatileDetailsChanged": (accountId, details) => { |
| 55 | console.log(`VolatileDetailsChanged ${accountId}`) |
| 56 | const account = this.getAccount(accountId) |
| 57 | if (!account) { |
| 58 | console.log(`Unknown account ${accountId}`) |
| 59 | return |
| 60 | } |
| 61 | account.volatileDetails = details |
| 62 | }, |
| 63 | "IncomingAccountMessage": (accountId, from, message) => { |
| 64 | console.log(`Received message: ${accountId} ${from} ${message["text/plain"]}`) |
| 65 | /* |
| 66 | if (parser.validate(message["text/plain"]) === true) { |
| 67 | console.log(message["text/plain"]); |
| 68 | } else { |
| 69 | |
| 70 | user = connectedUsers[accountId]; |
| 71 | console.log(user.socketId) |
| 72 | io.to(user.socketId).emit('receivedMessage', message["text/plain"]); |
| 73 | //io.emit('receivedMessage', message["text/plain"]); |
| 74 | }*/ |
| 75 | }, |
| 76 | "RegistrationStateChanged": (accountId, state, /*int*/ code, detail) => { |
| 77 | const account = this.getAccount(accountId) |
| 78 | if (!account) { |
| 79 | console.log(`Unknown account ${accountId}`) |
| 80 | return |
| 81 | } |
| 82 | account.registrationState = state |
| 83 | console.log("RegistrationStateChanged: " + accountId + " " + state + " " + code + " " + detail) |
| 84 | if (state === "REGISTERED") { |
| 85 | /*if (tempAccounts[accountId]) { |
| 86 | |
| 87 | const ctx = tempAccounts[accountId] |
| 88 | ctx.newUser.accountId = accountId |
| 89 | ctx.newUser.jamiId = jami.dring.getAccountDetails(accountId).get("Account.username") |
| 90 | //connectedUsers[accountId] = ctx.newUser |
| 91 | ctx.done(null, ctx.newUser) |
| 92 | delete tempAccounts[accountId] |
| 93 | }*/ |
| 94 | } else if (state === "ERROR_AUTH") { |
| 95 | //done(null, false) |
| 96 | //remove account |
| 97 | } |
| 98 | }, |
| 99 | "RegisteredNameFound": (accountId, state, address, name) => { |
| 100 | console.log("RegistrationStateChanged: " + accountId + " " + state + " " + address + " " + name) |
| 101 | } |
| 102 | }) |
| 103 | this.stringVectToArr(this.dring.getAccountList()).forEach(accountId => { |
| 104 | this.accounts.push(new Account(accountId, |
| 105 | this.mapToJs(this.dring.getAccountDetails(accountId)), |
| 106 | this.mapToJs(this.dring.getVolatileAccountDetails(accountId)) |
| 107 | )) |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | addAccount(account) { |
| 112 | const params = accountDetailsToNative(account) |
| 113 | params.set("Account.type", "RING") |
| 114 | return this.dring.addAccount(params) |
| 115 | } |
| 116 | getAccount(accountId) { |
| 117 | for (let i = 0; i < this.accounts.length; i++) { |
| 118 | const account = this.accounts[i] |
| 119 | if (account.getId() === accountId) |
| 120 | return account |
| 121 | } |
| 122 | return undefined |
| 123 | } |
| 124 | getAccountList() { |
| 125 | return this.accounts |
| 126 | } |
| 127 | /*getAccountDetails(accountId) { |
| 128 | return this.mapToJs(this.dring.getAccountDetails(accountId)); |
| 129 | }*/ |
| 130 | setAccountDetails(accountId, details) { |
| 131 | this.dring.setAccountDetails(accountId, mapToNative(details)); |
| 132 | } |
| 133 | getAudioOutputDeviceList() { |
| 134 | return this.stringVectToArr(this.dring.getAudioOutputDeviceList()); |
| 135 | } |
| 136 | getVolume(deviceName) { |
| 137 | return this.dring.getVolume(deviceName); |
| 138 | } |
| 139 | setVolume(deviceName, volume) { |
| 140 | return this.dring.setVolume(deviceName, volume); |
| 141 | } |
| 142 | |
| 143 | stop() { |
| 144 | this.dring.fini(); |
| 145 | } |
| 146 | |
| 147 | // private |
| 148 | |
| 149 | boolToStr(bool) { |
| 150 | return bool ? "TRUE" : "FALSE"; |
| 151 | } |
| 152 | |
| 153 | accountDetailsToNative(account) { |
| 154 | const params = new this.dring.StringMap(); |
| 155 | if (account.managerUri) |
| 156 | params.set("Account.managerUri", account.managerUri); |
| 157 | if (account.managerUsername) |
| 158 | params.set("Account.managerUsername", account.managerUsername); |
| 159 | if (account.archivePassword) { |
| 160 | params.set("Account.archivePassword", account.archivePassword); |
| 161 | } else { |
| 162 | console.log("archivePassword required"); |
| 163 | return; |
| 164 | } |
| 165 | if (account.alias) |
| 166 | params.set("Account.alias", account.alias); |
| 167 | if (account.displayName) |
| 168 | params.set("Account.displayName", account.displayName); |
| 169 | if (account.enable) |
| 170 | params.set("Account.enable", this.boolToStr(account.enable)); |
| 171 | if (account.autoAnswer) |
| 172 | params.set("Account.autoAnswer", this.boolToStr(account.autoAnswer)); |
| 173 | if (account.ringtonePath) |
| 174 | params.set("Account.ringtonePath", account.ringtonePath); |
| 175 | if (account.ringtoneEnabled) |
| 176 | params.set("Account.ringtoneEnabled", this.boolToStr(account.ringtoneEnabled)); |
| 177 | if (account.videoEnabled) |
| 178 | params.set("Account.videoEnabled", this.boolToStr(account.videoEnabled)); |
| 179 | if (account.useragent) { |
| 180 | params.set("Account.useragent", account.useragent); |
| 181 | params.set("Account.hasCustomUserAgent", "TRUE"); |
| 182 | } else { |
| 183 | params.set("Account.hasCustomUserAgent", "FALSE"); |
| 184 | } |
| 185 | if (account.audioPortMin) |
| 186 | params.set("Account.audioPortMin", account.audioPortMin); |
| 187 | if (account.audioPortMax) |
| 188 | params.set("Account.audioPortMax", account.audioPortMax); |
| 189 | if (account.videoPortMin) |
| 190 | params.set("Account.videoPortMin", account.videoPortMin); |
| 191 | if (account.videoPortMax) |
| 192 | params.set("Account.videoPortMax", account.videoPortMax); |
| 193 | if (account.localInterface) |
| 194 | params.set("Account.localInterface", account.localInterface); |
| 195 | if (account.publishedSameAsLocal) |
| 196 | params.set("Account.publishedSameAsLocal", this.boolToStr(account.publishedSameAsLocal)); |
| 197 | if (account.localPort) |
| 198 | params.set("Account.localPort", account.localPort); |
| 199 | if (account.publishedPort) |
| 200 | params.set("Account.publishedPort", account.publishedPort); |
| 201 | if (account.publishedAddress) |
| 202 | params.set("Account.publishedAddress", account.publishedAddress); |
| 203 | if (account.upnpEnabled) |
| 204 | params.set("Account.upnpEnabled", this.boolToStr(account.upnpEnabled)); |
| 205 | return params; |
| 206 | } |
| 207 | stringVectToArr(stringvect) { |
| 208 | const outputArr = []; |
| 209 | for (let i = 0; i < stringvect.size(); i++) |
| 210 | outputArr.push(stringvect.get(i)); |
| 211 | return outputArr; |
| 212 | } |
| 213 | mapToJs(m) { |
| 214 | const outputObj = {}; |
| 215 | this.stringVectToArr(m.keys()) |
| 216 | .forEach(k => outputObj[k] = m.get(k)); |
| 217 | return outputObj; |
| 218 | } |
| 219 | mapToNative(map){ |
| 220 | const ret = new this.dring.StringMap(); |
| 221 | map.forEach((value, key) => ret.set(key, value)); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | |
| 227 | module.exports = JamiDaemon; |