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