Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 1 | /* |
simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 2 | * Copyright (C) 2017-2021 Savoir-faire Linux Inc. |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 3 | * |
simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 8 | * |
simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 13 | * |
simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 17 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | 'use strict'; |
Adrien Béraud | 947e879 | 2021-04-15 18:32:44 -0400 | [diff] [blame] | 19 | |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 20 | import { |
| 21 | Account, |
| 22 | AccountConfig, |
| 23 | AccountDetails, |
| 24 | Contact, |
| 25 | Conversation, |
| 26 | Lookup, |
| 27 | LookupResolveValue, |
| 28 | Message, |
| 29 | PromiseExecutor, |
| 30 | RegistrationState, |
| 31 | VolatileDetails, |
| 32 | } from 'jami-web-common'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 33 | import { createRequire } from 'module'; |
| 34 | import path from 'path'; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 35 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 36 | const require = createRequire(import.meta.url); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 37 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 38 | class JamiDaemon { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 39 | private accounts: Account[]; |
| 40 | private readonly lookups: Lookup[]; |
| 41 | private readonly tempAccounts: Record<string, PromiseExecutor<string>>; |
| 42 | private dring: Record<string, any>; |
| 43 | |
| 44 | constructor(onMessage: (account: Account, conversation: Conversation, message: Message) => void) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | this.accounts = []; |
| 46 | this.lookups = []; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 47 | this.tempAccounts = {}; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | this.dring = require(path.join(process.cwd(), 'jamid.node')); |
| 49 | this.dring.init({ |
| 50 | AccountsChanged: () => { |
| 51 | console.log('AccountsChanged'); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 52 | const newAccounts: Account[] = []; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => { |
| 54 | for (const account of this.accounts) { |
| 55 | if (account.getId() === accountId) { |
| 56 | newAccounts.push(account); |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | newAccounts.push( |
| 61 | new Account( |
| 62 | accountId, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 63 | JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails, |
| 64 | JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 65 | ) |
| 66 | ); |
| 67 | }); |
| 68 | this.accounts = newAccounts; |
| 69 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 70 | AccountDetailsChanged: (accountId: string, details: AccountDetails) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | console.log(`AccountDetailsChanged ${accountId}`); |
| 72 | const account = this.getAccount(accountId); |
| 73 | if (!account) { |
| 74 | console.log(`Unknown account ${accountId}`); |
| 75 | return; |
| 76 | } |
| 77 | account.details = details; |
| 78 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 79 | VolatileDetailsChanged: (accountId: string, details: VolatileDetails) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 80 | console.log(`VolatileDetailsChanged ${accountId}`); |
| 81 | const account = this.getAccount(accountId); |
| 82 | if (!account) { |
| 83 | console.log(`Unknown account ${accountId}`); |
| 84 | return; |
| 85 | } |
| 86 | account.volatileDetails = details; |
| 87 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 88 | IncomingAccountMessage: (accountId: string, from: Account, message: Message) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 89 | console.log(`Received message: ${accountId} ${from} ${message['text/plain']}`); |
| 90 | /* |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 91 | if (parser.validate(message["text/plain"]) === true) { |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 92 | console.log(message["text/plain"]) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 93 | } else { |
| 94 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 95 | user = connectedUsers[accountId] |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 96 | console.log(user.socketId) |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 97 | io.to(user.socketId).emit('receivedMessage', message["text/plain"]) |
| 98 | //io.emit('receivedMessage', message["text/plain"]) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 99 | }*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 100 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 101 | RegistrationStateChanged: (accountId: string, state: RegistrationState, code: number, detail: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 102 | console.log('RegistrationStateChanged: ' + accountId + ' ' + state + ' ' + code + ' ' + detail); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 103 | const account: Account | undefined = this.getAccount(accountId); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 104 | if (account) { |
| 105 | account.registrationState = state; |
| 106 | } else { |
| 107 | console.log(`Unknown account ${accountId}`); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 108 | return; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 109 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 110 | const ctx = this.tempAccounts[accountId]; |
| 111 | if (ctx) { |
| 112 | if (state === 'REGISTERED') { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 113 | account.details = JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 114 | ctx.resolve(accountId); |
| 115 | delete this.tempAccounts[accountId]; |
| 116 | } else if (state === 'ERROR_AUTH') { |
| 117 | this.dring.removeAccount(accountId); |
| 118 | ctx.reject(state); |
| 119 | delete this.tempAccounts[accountId]; |
| 120 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 121 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 122 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 123 | RegisteredNameFound: (accountId: string, state: number, address: string, name: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 124 | console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`); |
| 125 | let lookups; |
| 126 | if (accountId) { |
| 127 | const account = this.getAccount(accountId); |
| 128 | if (!account) { |
| 129 | console.log(`Unknown account ${accountId}`); |
| 130 | return; |
| 131 | } |
simon | 73ef58d | 2022-10-27 00:25:55 -0400 | [diff] [blame] | 132 | if (state === 0) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 133 | const contact = account.getContactFromCache(address); |
| 134 | if (!contact.isRegisteredNameResolved()) contact.setRegisteredName(name); |
| 135 | } |
| 136 | lookups = account.lookups; |
| 137 | } else { |
| 138 | lookups = this.lookups; |
| 139 | } |
| 140 | let index = lookups.length - 1; |
| 141 | while (index >= 0) { |
| 142 | const lookup = lookups[index]; |
| 143 | if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) { |
| 144 | lookup.resolve({ address, name, state }); |
| 145 | lookups.splice(index, 1); |
| 146 | } |
| 147 | index -= 1; |
| 148 | } |
| 149 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 150 | NameRegistrationEnded: (accountId: string, state: number, name: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 151 | console.log(`NameRegistrationEnded: ${accountId} ${state} ${name}`); |
| 152 | const account = this.getAccount(accountId); |
| 153 | if (account) { |
| 154 | if (state === 0) account.volatileDetails['Account.registeredName'] = name; |
| 155 | if (account.registeringName) { |
| 156 | account.registeringName.resolve(state); |
| 157 | delete account.registeringName; |
| 158 | } |
| 159 | } else { |
| 160 | console.log(`Unknown account ${accountId}`); |
| 161 | } |
| 162 | }, |
| 163 | // Conversations |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 164 | ConversationReady: (accountId: string, conversationId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 165 | console.log(`conversationReady: ${accountId} ${conversationId}`); |
| 166 | const account = this.getAccount(accountId); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 167 | if (!account) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 168 | console.log(`Unknown account ${accountId}`); |
| 169 | return; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 170 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 171 | let conversation = account.getConversation(conversationId); |
| 172 | if (!conversation) { |
| 173 | const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId)); |
| 174 | members.forEach((member) => (member.contact = account.getContactFromCache(member.uri))); |
| 175 | conversation = new Conversation(conversationId, accountId, members); |
| 176 | account.addConversation(conversation); |
| 177 | } |
| 178 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 179 | ConversationRemoved: (accountId: string, conversationId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 180 | console.log(`conversationRemoved: ${accountId} ${conversationId}`); |
| 181 | const account = this.getAccount(accountId); |
| 182 | if (!account) { |
| 183 | console.log(`Unknown account ${accountId}`); |
| 184 | return; |
| 185 | } |
| 186 | account.removeConversation(conversationId); |
| 187 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 188 | ConversationLoaded: (id: number, accountId: string, conversationId: string, messages: Message[]) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 189 | console.log(`conversationLoaded: ${accountId} ${conversationId}`); |
| 190 | const account = this.getAccount(accountId); |
| 191 | if (!account) { |
| 192 | console.log(`Unknown account ${accountId}`); |
| 193 | return; |
| 194 | } |
| 195 | const conversation = account.getConversation(conversationId); |
| 196 | if (conversation) { |
| 197 | //conversation.addLoadedMessages(messages) |
| 198 | const request = conversation.requests[id]; |
| 199 | if (request) { |
| 200 | request.resolve(messages); |
| 201 | } |
| 202 | } |
| 203 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 204 | MessageReceived: (accountId: string, conversationId: string, message: Message) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 205 | console.log(`messageReceived: ${accountId} ${conversationId}`); |
| 206 | console.log(message); |
| 207 | const account = this.getAccount(accountId); |
| 208 | if (!account) { |
| 209 | console.log(`Unknown account ${accountId}`); |
| 210 | return; |
| 211 | } |
| 212 | const conversation = account.getConversation(conversationId); |
| 213 | if (conversation) { |
| 214 | conversation.addMessage(message); |
| 215 | if (onMessage) onMessage(account, conversation, message); |
| 216 | } |
| 217 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 218 | ConversationRequestReceived: (accountId: string, conversationId: string, message: Message) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 219 | console.log(`conversationRequestReceived: ${accountId} ${conversationId}`); |
| 220 | const account = this.getAccount(accountId); |
| 221 | if (!account) { |
| 222 | console.log(`Unknown account ${accountId}`); |
| 223 | return; |
| 224 | } |
| 225 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 226 | ConversationMemberEvent: (accountId: string, conversationId: string, memberUri: string, event: number) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 227 | console.log(`conversationMemberEvent: ${accountId} ${conversationId}`); |
| 228 | const account = this.getAccount(accountId); |
| 229 | if (!account) { |
| 230 | console.log(`Unknown account ${accountId}`); |
| 231 | return; |
| 232 | } |
| 233 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 234 | OnConversationError: (accountId: string, conversationId: string, code: number, what: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 235 | console.log(`onConversationError: ${accountId} ${conversationId}`); |
| 236 | const account = this.getAccount(accountId); |
| 237 | if (!account) { |
| 238 | console.log(`Unknown account ${accountId}`); |
| 239 | return; |
| 240 | } |
| 241 | }, |
| 242 | // Calls |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 243 | StateChange: (callId: string, state: string, code: number) => { |
| 244 | console.log(`CallStateChange: ${callId} ${state} ${code}`); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 245 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 246 | IncomingCall: (accountId: string, callId: string, peerUri: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 247 | console.log(`IncomingCall: ${accountId} ${callId} ${peerUri}`); |
| 248 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 249 | ConferenceCreated: (confId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 250 | console.log(`ConferenceCreated: ${confId}`); |
| 251 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 252 | ConferenceChanged: (accountId: string, confId: string, state: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 253 | console.log(`ConferenceChanged: ${confId}`); |
| 254 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 255 | ConferenceRemoved: (confId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 256 | console.log(`ConferenceRemoved: ${confId}`); |
| 257 | }, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 258 | OnConferenceInfosUpdated: (confId: string) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 259 | console.log(`onConferenceInfosUpdated: ${confId}`); |
| 260 | }, |
| 261 | }); |
| 262 | |
| 263 | JamiDaemon.vectToJs(this.dring.getAccountList()).forEach((accountId) => { |
| 264 | const account = new Account( |
| 265 | accountId, |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 266 | JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)) as AccountDetails, |
| 267 | JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId)) as VolatileDetails |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 268 | ); |
| 269 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 270 | account.contacts = JamiDaemon.vectMapToJs(this.dring.getContacts(accountId)) as Contact[]; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 271 | |
| 272 | JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach((conversationId) => { |
| 273 | const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId)); |
| 274 | console.log('\n\nXMEMBERS: ', members); |
| 275 | members.forEach((member) => { |
| 276 | member.contact = account.getContactFromCache(member.uri); |
| 277 | if (!member.contact.isRegisteredNameResolved()) { |
| 278 | if (!member.uri) return; |
| 279 | console.log(`lookupAddress ${accountId} ${member.uri}`, member); |
| 280 | member.contact.setRegisteredName( |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 281 | new Promise((resolve: (value: LookupResolveValue) => void, reject) => |
| 282 | account.lookups.push({ address: member.uri, resolve, reject }) |
| 283 | ).then((result) => { |
simon | 73ef58d | 2022-10-27 00:25:55 -0400 | [diff] [blame] | 284 | if (result.state === 0) return result.name; |
| 285 | else if (result.state === 1) return undefined; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 286 | else return null; |
| 287 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 288 | ); |
| 289 | this.dring.lookupAddress(accountId, '', member.uri); |
| 290 | } |
| 291 | }); |
| 292 | const conversation = new Conversation(conversationId, accountId, members); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 293 | conversation.infos = JamiDaemon.mapToJs(this.dring.conversationInfos(accountId, conversationId)); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 294 | account.addConversation(conversation); |
| 295 | }); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 296 | |
| 297 | this.accounts.push(account); |
| 298 | }); |
| 299 | } |
| 300 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 301 | addAccount(accountConfig: AccountConfig) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 302 | const params = this.accountDetailsToNative(accountConfig); |
| 303 | params.set('Account.type', 'RING'); |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 304 | return new Promise<string>((resolve, reject) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 305 | const accountId = this.dring.addAccount(params); |
| 306 | this.tempAccounts[accountId] = { resolve, reject }; |
| 307 | }); |
| 308 | } |
| 309 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 310 | getDevices(accountId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 311 | return JamiDaemon.mapToJs(this.dring.getKnownRingDevices(accountId)); |
| 312 | } |
| 313 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 314 | getAccount(accountId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 315 | for (let i = 0; i < this.accounts.length; i++) { |
| 316 | const account = this.accounts[i]; |
| 317 | if (account.getId() === accountId) return account; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 318 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 319 | return undefined; |
| 320 | } |
| 321 | getAccountList() { |
| 322 | return this.accounts; |
| 323 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 324 | registerName(accountId: string, password: string, name: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 325 | return new Promise((resolve, reject) => { |
| 326 | if (!name) return reject(new Error('Invalid name')); |
| 327 | const account = this.getAccount(accountId); |
| 328 | if (!account) return reject(new Error("Can't find account")); |
| 329 | if (account.registeringName) return reject(new Error('Username already being registered')); |
| 330 | if (this.dring.registerName(accountId, password, name)) { |
| 331 | account.registeringName = { name, resolve, reject }; |
| 332 | } |
| 333 | }); |
| 334 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 335 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 336 | getConversation(accountId: string, conversationId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 337 | const account = this.getAccount(accountId); |
| 338 | if (account) return account.getConversation(conversationId); |
| 339 | return null; |
| 340 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 341 | getAccountDetails(accountId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 342 | return JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)); |
| 343 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 344 | setAccountDetails(accountId: string, details: AccountDetails) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 345 | this.dring.setAccountDetails(accountId, this.mapToNative(details)); |
| 346 | } |
| 347 | getAudioOutputDeviceList() { |
| 348 | return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList()); |
| 349 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 350 | getVolume(deviceName: string): number { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 351 | return this.dring.getVolume(deviceName); |
| 352 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 353 | setVolume(deviceName: string, volume: number) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 354 | return this.dring.setVolume(deviceName, volume); |
| 355 | } |
| 356 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 357 | lookupName(accountId: string, name: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 358 | const p = new Promise((resolve, reject) => { |
| 359 | if (accountId) { |
| 360 | const account = this.getAccount(accountId); |
| 361 | if (!account) { |
| 362 | reject(new Error("Can't find account")); |
| 363 | } else { |
| 364 | account.lookups.push({ name, resolve, reject }); |
| 365 | } |
| 366 | } else { |
| 367 | this.lookups.push({ name, resolve, reject }); |
| 368 | } |
| 369 | }); |
| 370 | this.dring.lookupName(accountId || '', '', name); |
| 371 | return p; |
| 372 | } |
| 373 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 374 | lookupAddress(accountId: string, address: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 375 | console.log(`lookupAddress ${accountId} ${address}`); |
| 376 | const p = new Promise((resolve, reject) => { |
| 377 | if (accountId) { |
| 378 | const account = this.getAccount(accountId); |
| 379 | if (!account) { |
| 380 | reject(new Error("Can't find account")); |
| 381 | } else { |
| 382 | account.lookups.push({ address, resolve, reject }); |
| 383 | } |
| 384 | } else { |
| 385 | this.lookups.push({ address, resolve, reject }); |
| 386 | } |
| 387 | }); |
| 388 | this.dring.lookupAddress(accountId || '', '', address); |
| 389 | return p; |
| 390 | } |
| 391 | |
| 392 | stop() { |
| 393 | this.dring.fini(); |
| 394 | } |
| 395 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 396 | addContact(accountId: string, contactId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 397 | this.dring.addContact(accountId, contactId); |
| 398 | const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId)); |
| 399 | if (details.conversationId) { |
| 400 | const account = this.getAccount(accountId); |
| 401 | if (account) { |
| 402 | let conversation = account.getConversation(details.conversationId); |
| 403 | if (!conversation) { |
| 404 | const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId)); |
| 405 | members.forEach((member) => (member.contact = account.getContactFromCache(member.uri))); |
| 406 | conversation = new Conversation(details.conversationId, accountId, members); |
| 407 | account.addConversation(conversation); |
| 408 | } |
| 409 | } |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 410 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 411 | return details; |
| 412 | } |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 413 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 414 | removeContact(accountId: string, contactId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 415 | //bool ban false |
| 416 | this.dring.removeContact(accountId, contactId, false); |
| 417 | } |
| 418 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 419 | blockContact(accountId: string, contactId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 420 | //bool ban true |
| 421 | this.dring.removeContact(accountId, contactId, true); |
| 422 | } |
| 423 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 424 | getContactDetails(accountId: string, contactId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 425 | return JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId)); |
| 426 | } |
| 427 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 428 | getDefaultModerators(accountId: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 429 | const account = this.getAccount(accountId); |
| 430 | if (!account) { |
| 431 | console.log(`Unknown account ${accountId}`); |
| 432 | return {}; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 433 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 434 | return JamiDaemon.vectToJs(this.dring.getDefaultModerators(accountId)).map((contactId) => |
| 435 | account.getContactFromCache(contactId) |
| 436 | ); |
| 437 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 438 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 439 | addDefaultModerator(accountId: string, uri: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 440 | this.dring.setDefaultModerator(accountId, uri, true); |
| 441 | } |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 442 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 443 | removeDefaultModerator(accountId: string, uri: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 444 | this.dring.setDefaultModerator(accountId, uri, false); |
| 445 | } |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 446 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 447 | sendMessage(accountId: string, conversationId: string, message: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 448 | this.dring.sendMessage(accountId, conversationId, message, ''); |
| 449 | } |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 450 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 451 | loadMessages(accountId: string, conversationId: string, fromMessage?: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 452 | const account = this.getAccount(accountId); |
| 453 | if (!account) throw new Error('Unknown account'); |
| 454 | const conversation = account.getConversation(conversationId); |
| 455 | if (!conversation) throw new Error(`Unknown conversation ${conversationId}`); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 456 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 457 | return new Promise((resolve, reject) => { |
| 458 | if (!conversation.requests) conversation.requests = {}; |
| 459 | const requestId = this.dring.loadConversationMessages(accountId, conversationId, fromMessage || '', 32); |
| 460 | conversation.requests[requestId] = { resolve, reject }; |
| 461 | }); |
| 462 | } |
| 463 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 464 | boolToStr(bool: boolean) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 465 | return bool ? 'true' : 'false'; |
| 466 | } |
| 467 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 468 | accountDetailsToNative(account: AccountConfig) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 469 | const params = new this.dring.StringMap(); |
| 470 | if (account.managerUri) params.set('Account.managerUri', account.managerUri); |
| 471 | if (account.managerUsername) params.set('Account.managerUsername', account.managerUsername); |
| 472 | if (account.archivePassword) { |
| 473 | params.set('Account.archivePassword', account.archivePassword); |
| 474 | } /* else { |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 475 | console.log("archivePassword required") |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 476 | return |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 477 | }*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 478 | if (account.alias) params.set('Account.alias', account.alias); |
| 479 | if (account.displayName) params.set('Account.displayName', account.displayName); |
| 480 | if (account.enable !== undefined) params.set('Account.enable', this.boolToStr(account.enable)); |
| 481 | if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer)); |
| 482 | if (account.autoAnswer !== undefined) params.set('Account.autoAnswer', this.boolToStr(account.autoAnswer)); |
| 483 | if (account.ringtonePath) params.set('Account.ringtonePath', account.ringtonePath); |
| 484 | if (account.ringtoneEnabled !== undefined) |
| 485 | params.set('Account.ringtoneEnabled', this.boolToStr(account.ringtoneEnabled)); |
| 486 | if (account.videoEnabled !== undefined) params.set('Account.videoEnabled', this.boolToStr(account.videoEnabled)); |
| 487 | if (account.useragent) { |
| 488 | params.set('Account.useragent', account.useragent); |
| 489 | params.set('Account.hasCustomUserAgent', 'TRUE'); |
| 490 | } else { |
| 491 | params.set('Account.hasCustomUserAgent', 'FALSE'); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 492 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 493 | if (account.audioPortMin) params.set('Account.audioPortMin', account.audioPortMin); |
| 494 | if (account.audioPortMax) params.set('Account.audioPortMax', account.audioPortMax); |
| 495 | if (account.videoPortMin) params.set('Account.videoPortMin', account.videoPortMin); |
| 496 | if (account.videoPortMax) params.set('Account.videoPortMax', account.videoPortMax); |
| 497 | if (account.localInterface) params.set('Account.localInterface', account.localInterface); |
| 498 | if (account.publishedSameAsLocal !== undefined) |
| 499 | params.set('Account.publishedSameAsLocal', this.boolToStr(account.publishedSameAsLocal)); |
| 500 | if (account.localPort) params.set('Account.localPort', account.localPort); |
| 501 | if (account.publishedPort) params.set('Account.publishedPort', account.publishedPort); |
| 502 | if (account.rendezVous !== undefined) params.set('Account.rendezVous', this.boolToStr(account.rendezVous)); |
| 503 | if (account.upnpEnabled !== undefined) params.set('Account.upnpEnabled', this.boolToStr(account.upnpEnabled)); |
| 504 | return params; |
| 505 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 506 | static vectToJs(vect: any) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 507 | const len = vect.size(); |
| 508 | const outputArr = new Array(len); |
| 509 | for (let i = 0; i < len; i++) outputArr[i] = vect.get(i); |
| 510 | return outputArr; |
| 511 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 512 | static mapToJs(m: any): Record<string, any> { |
| 513 | const outputObj: Record<string, any> = {}; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 514 | JamiDaemon.vectToJs(m.keys()).forEach((k) => (outputObj[k] = m.get(k))); |
| 515 | return outputObj; |
| 516 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 517 | static vectMapToJs(vectMap: any) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 518 | const len = vectMap.size(); |
| 519 | const outputArr = new Array(len); |
| 520 | for (let i = 0; i < len; i++) outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i)); |
| 521 | return outputArr; |
| 522 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 523 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 524 | mapToNative(map: any) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 525 | const ret = new this.dring.StringMap(); |
| 526 | for (const [key, value] of Object.entries(map)) ret.set(key, value); |
| 527 | return ret; |
| 528 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 529 | } |
| 530 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 531 | export default JamiDaemon; |