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