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') |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 21 | const Conversation = require('./model/Conversation') |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 22 | |
| 23 | "use strict"; |
| 24 | class JamiDaemon { |
| 25 | constructor() { |
| 26 | this.accounts = [] |
| 27 | this.dring = require("./dring.node") |
| 28 | this.dring.init({ |
| 29 | "AccountsChanged": () => { |
| 30 | console.log("AccountsChanged") |
| 31 | const newAccounts = [] |
| 32 | this.stringVectToArr(this.dring.getAccountList()).forEach(accountId => { |
| 33 | for (const account in this.accounts) { |
| 34 | if (account.id === accountId) { |
| 35 | newAccounts.push(account) |
| 36 | return |
| 37 | } |
| 38 | } |
| 39 | newAccounts.push(new Account(accountId, |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 40 | this.mapToJs(this.dring.getAccountDetails(accountId)), |
| 41 | this.mapToJs(this.dring.getVolatileAccountDetails(accountId)) |
| 42 | )) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 43 | }) |
| 44 | this.accounts = newAccounts |
| 45 | }, |
| 46 | "AccountDetailsChanged": (accountId, details) => { |
| 47 | console.log(`AccountDetailsChanged ${accountId}`) |
| 48 | const account = this.getAccount(accountId) |
| 49 | if (!account) { |
| 50 | console.log(`Unknown account ${accountId}`) |
| 51 | return |
| 52 | } |
| 53 | account.details = details |
| 54 | }, |
| 55 | "VolatileDetailsChanged": (accountId, details) => { |
| 56 | console.log(`VolatileDetailsChanged ${accountId}`) |
| 57 | const account = this.getAccount(accountId) |
| 58 | if (!account) { |
| 59 | console.log(`Unknown account ${accountId}`) |
| 60 | return |
| 61 | } |
| 62 | account.volatileDetails = details |
| 63 | }, |
| 64 | "IncomingAccountMessage": (accountId, from, message) => { |
| 65 | console.log(`Received message: ${accountId} ${from} ${message["text/plain"]}`) |
| 66 | /* |
| 67 | if (parser.validate(message["text/plain"]) === true) { |
| 68 | console.log(message["text/plain"]); |
| 69 | } else { |
| 70 | |
| 71 | user = connectedUsers[accountId]; |
| 72 | console.log(user.socketId) |
| 73 | io.to(user.socketId).emit('receivedMessage', message["text/plain"]); |
| 74 | //io.emit('receivedMessage', message["text/plain"]); |
| 75 | }*/ |
| 76 | }, |
| 77 | "RegistrationStateChanged": (accountId, state, /*int*/ code, detail) => { |
| 78 | const account = this.getAccount(accountId) |
| 79 | if (!account) { |
| 80 | console.log(`Unknown account ${accountId}`) |
| 81 | return |
| 82 | } |
| 83 | account.registrationState = state |
| 84 | console.log("RegistrationStateChanged: " + accountId + " " + state + " " + code + " " + detail) |
| 85 | if (state === "REGISTERED") { |
| 86 | /*if (tempAccounts[accountId]) { |
| 87 | |
| 88 | const ctx = tempAccounts[accountId] |
| 89 | ctx.newUser.accountId = accountId |
| 90 | ctx.newUser.jamiId = jami.dring.getAccountDetails(accountId).get("Account.username") |
| 91 | //connectedUsers[accountId] = ctx.newUser |
| 92 | ctx.done(null, ctx.newUser) |
| 93 | delete tempAccounts[accountId] |
| 94 | }*/ |
| 95 | } else if (state === "ERROR_AUTH") { |
| 96 | //done(null, false) |
| 97 | //remove account |
| 98 | } |
| 99 | }, |
| 100 | "RegisteredNameFound": (accountId, state, address, name) => { |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 101 | console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`) |
| 102 | }, |
| 103 | "conversationReady": (accountId, conversationId) => { |
| 104 | console.log(`conversationReady: ${accountId} ${conversationId}`) |
| 105 | const account = this.getAccount(accountId) |
| 106 | if (!account) { |
| 107 | console.log(`Unknown account ${accountId}`) |
| 108 | return |
| 109 | } |
| 110 | let conversation = account.getConversation(conversationId) |
| 111 | if (!conversation) { |
| 112 | const members = this.dring.getConversationMembers(accountId, conversationId) |
| 113 | console.log(members) |
| 114 | conversation = new Conversation(conversationId, members) |
| 115 | account.addConversation(conversation) |
| 116 | } |
| 117 | }, |
| 118 | "conversationRemoved": (accountId, conversationId) => { |
| 119 | console.log(`conversationRemoved: ${accountId} ${conversationId}`) |
| 120 | const account = this.getAccount(accountId) |
| 121 | if (!account) { |
| 122 | console.log(`Unknown account ${accountId}`) |
| 123 | return |
| 124 | } |
| 125 | account.removeConversation(conversationId) |
| 126 | }, |
| 127 | "conversationLoaded": (accountId, conversationId) => { |
| 128 | console.log(`conversationLoaded: ${accountId} ${conversationId}`) |
| 129 | const account = this.getAccount(accountId) |
| 130 | if (!account) { |
| 131 | console.log(`Unknown account ${accountId}`) |
| 132 | return |
| 133 | } |
| 134 | }, |
| 135 | "messageReceived": (accountId, conversationId, message) => { |
| 136 | console.log(`messageReceived: ${accountId} ${conversationId}`) |
| 137 | const account = this.getAccount(accountId) |
| 138 | if (!account) { |
| 139 | console.log(`Unknown account ${accountId}`) |
| 140 | return |
| 141 | } |
| 142 | const conversation = account.getConversation(conversationId) |
| 143 | if (!conversation) { |
| 144 | conversation.addMessage(message) |
| 145 | } |
| 146 | }, |
| 147 | "conversationRequestReceived": (accountId, conversationId, request) => { |
| 148 | console.log(`conversationRequestReceived: ${accountId} ${conversationId}`) |
| 149 | const account = this.getAccount(accountId) |
| 150 | if (!account) { |
| 151 | console.log(`Unknown account ${accountId}`) |
| 152 | return |
| 153 | } |
| 154 | }, |
| 155 | "conversationMemberEvent": (accountId, conversationId, member, event) => { |
| 156 | console.log(`conversationMemberEvent: ${accountId} ${conversationId}`) |
| 157 | const account = this.getAccount(accountId) |
| 158 | if (!account) { |
| 159 | console.log(`Unknown account ${accountId}`) |
| 160 | return |
| 161 | } |
| 162 | }, |
| 163 | "onConversationError": (accountId, conversationId, code, what) => { |
| 164 | console.log(`onConversationError: ${accountId} ${conversationId}`) |
| 165 | const account = this.getAccount(accountId) |
| 166 | if (!account) { |
| 167 | console.log(`Unknown account ${accountId}`) |
| 168 | return |
| 169 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 170 | } |
| 171 | }) |
| 172 | this.stringVectToArr(this.dring.getAccountList()).forEach(accountId => { |
| 173 | this.accounts.push(new Account(accountId, |
| 174 | this.mapToJs(this.dring.getAccountDetails(accountId)), |
| 175 | this.mapToJs(this.dring.getVolatileAccountDetails(accountId)) |
| 176 | )) |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | addAccount(account) { |
| 181 | const params = accountDetailsToNative(account) |
| 182 | params.set("Account.type", "RING") |
| 183 | return this.dring.addAccount(params) |
| 184 | } |
| 185 | getAccount(accountId) { |
| 186 | for (let i = 0; i < this.accounts.length; i++) { |
| 187 | const account = this.accounts[i] |
| 188 | if (account.getId() === accountId) |
| 189 | return account |
| 190 | } |
| 191 | return undefined |
| 192 | } |
| 193 | getAccountList() { |
| 194 | return this.accounts |
| 195 | } |
| 196 | /*getAccountDetails(accountId) { |
| 197 | return this.mapToJs(this.dring.getAccountDetails(accountId)); |
| 198 | }*/ |
| 199 | setAccountDetails(accountId, details) { |
| 200 | this.dring.setAccountDetails(accountId, mapToNative(details)); |
| 201 | } |
| 202 | getAudioOutputDeviceList() { |
| 203 | return this.stringVectToArr(this.dring.getAudioOutputDeviceList()); |
| 204 | } |
| 205 | getVolume(deviceName) { |
| 206 | return this.dring.getVolume(deviceName); |
| 207 | } |
| 208 | setVolume(deviceName, volume) { |
| 209 | return this.dring.setVolume(deviceName, volume); |
| 210 | } |
| 211 | |
| 212 | stop() { |
| 213 | this.dring.fini(); |
| 214 | } |
| 215 | |
| 216 | // private |
| 217 | |
| 218 | boolToStr(bool) { |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 219 | return bool ? "true" : "false"; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | accountDetailsToNative(account) { |
| 223 | const params = new this.dring.StringMap(); |
| 224 | if (account.managerUri) |
| 225 | params.set("Account.managerUri", account.managerUri); |
| 226 | if (account.managerUsername) |
| 227 | params.set("Account.managerUsername", account.managerUsername); |
| 228 | if (account.archivePassword) { |
| 229 | params.set("Account.archivePassword", account.archivePassword); |
| 230 | } else { |
| 231 | console.log("archivePassword required"); |
| 232 | return; |
| 233 | } |
| 234 | if (account.alias) |
| 235 | params.set("Account.alias", account.alias); |
| 236 | if (account.displayName) |
| 237 | params.set("Account.displayName", account.displayName); |
| 238 | if (account.enable) |
| 239 | params.set("Account.enable", this.boolToStr(account.enable)); |
| 240 | if (account.autoAnswer) |
| 241 | params.set("Account.autoAnswer", this.boolToStr(account.autoAnswer)); |
| 242 | if (account.ringtonePath) |
| 243 | params.set("Account.ringtonePath", account.ringtonePath); |
| 244 | if (account.ringtoneEnabled) |
| 245 | params.set("Account.ringtoneEnabled", this.boolToStr(account.ringtoneEnabled)); |
| 246 | if (account.videoEnabled) |
| 247 | params.set("Account.videoEnabled", this.boolToStr(account.videoEnabled)); |
| 248 | if (account.useragent) { |
| 249 | params.set("Account.useragent", account.useragent); |
| 250 | params.set("Account.hasCustomUserAgent", "TRUE"); |
| 251 | } else { |
| 252 | params.set("Account.hasCustomUserAgent", "FALSE"); |
| 253 | } |
| 254 | if (account.audioPortMin) |
| 255 | params.set("Account.audioPortMin", account.audioPortMin); |
| 256 | if (account.audioPortMax) |
| 257 | params.set("Account.audioPortMax", account.audioPortMax); |
| 258 | if (account.videoPortMin) |
| 259 | params.set("Account.videoPortMin", account.videoPortMin); |
| 260 | if (account.videoPortMax) |
| 261 | params.set("Account.videoPortMax", account.videoPortMax); |
| 262 | if (account.localInterface) |
| 263 | params.set("Account.localInterface", account.localInterface); |
| 264 | if (account.publishedSameAsLocal) |
| 265 | params.set("Account.publishedSameAsLocal", this.boolToStr(account.publishedSameAsLocal)); |
| 266 | if (account.localPort) |
| 267 | params.set("Account.localPort", account.localPort); |
| 268 | if (account.publishedPort) |
| 269 | params.set("Account.publishedPort", account.publishedPort); |
| 270 | if (account.publishedAddress) |
| 271 | params.set("Account.publishedAddress", account.publishedAddress); |
| 272 | if (account.upnpEnabled) |
| 273 | params.set("Account.upnpEnabled", this.boolToStr(account.upnpEnabled)); |
| 274 | return params; |
| 275 | } |
| 276 | stringVectToArr(stringvect) { |
| 277 | const outputArr = []; |
| 278 | for (let i = 0; i < stringvect.size(); i++) |
| 279 | outputArr.push(stringvect.get(i)); |
| 280 | return outputArr; |
| 281 | } |
| 282 | mapToJs(m) { |
| 283 | const outputObj = {}; |
| 284 | this.stringVectToArr(m.keys()) |
| 285 | .forEach(k => outputObj[k] = m.get(k)); |
| 286 | return outputObj; |
| 287 | } |
| 288 | mapToNative(map){ |
| 289 | const ret = new this.dring.StringMap(); |
| 290 | map.forEach((value, key) => ret.set(key, value)); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | module.exports = JamiDaemon; |