blob: f60ac5024761906903e47593e68301d809b4a0fd [file] [log] [blame]
Adrien Béraud6ecaa402021-04-06 17:37:25 -04001/*
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 */
Adrien Béraude74741b2021-04-19 13:22:54 -040020"use strict"
Adrien Béraud947e8792021-04-15 18:32:44 -040021
Adrien Béraude74741b2021-04-19 13:22:54 -040022import Account from './model/Account.js'
23import Conversation from './model/Conversation.js'
24import { createRequire } from 'module';
25const require = createRequire(import.meta.url);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040026
Adrien Béraud6ecaa402021-04-06 17:37:25 -040027class JamiDaemon {
28 constructor() {
29 this.accounts = []
30 this.dring = require("./dring.node")
31 this.dring.init({
32 "AccountsChanged": () => {
33 console.log("AccountsChanged")
34 const newAccounts = []
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040035 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach(accountId => {
Adrien Béraud6ecaa402021-04-06 17:37:25 -040036 for (const account in this.accounts) {
37 if (account.id === accountId) {
38 newAccounts.push(account)
39 return
40 }
41 }
42 newAccounts.push(new Account(accountId,
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040043 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)),
44 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId))
Adrien Béraud0cb76c92021-04-07 19:59:08 -040045 ))
Adrien Béraud6ecaa402021-04-06 17:37:25 -040046 })
47 this.accounts = newAccounts
48 },
49 "AccountDetailsChanged": (accountId, details) => {
50 console.log(`AccountDetailsChanged ${accountId}`)
51 const account = this.getAccount(accountId)
52 if (!account) {
53 console.log(`Unknown account ${accountId}`)
54 return
55 }
56 account.details = details
57 },
58 "VolatileDetailsChanged": (accountId, details) => {
59 console.log(`VolatileDetailsChanged ${accountId}`)
60 const account = this.getAccount(accountId)
61 if (!account) {
62 console.log(`Unknown account ${accountId}`)
63 return
64 }
65 account.volatileDetails = details
66 },
67 "IncomingAccountMessage": (accountId, from, message) => {
68 console.log(`Received message: ${accountId} ${from} ${message["text/plain"]}`)
69/*
70 if (parser.validate(message["text/plain"]) === true) {
Adrien Béraude74741b2021-04-19 13:22:54 -040071 console.log(message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040072 } else {
73
Adrien Béraude74741b2021-04-19 13:22:54 -040074 user = connectedUsers[accountId]
Adrien Béraud6ecaa402021-04-06 17:37:25 -040075 console.log(user.socketId)
Adrien Béraude74741b2021-04-19 13:22:54 -040076 io.to(user.socketId).emit('receivedMessage', message["text/plain"])
77 //io.emit('receivedMessage', message["text/plain"])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040078 }*/
79 },
80 "RegistrationStateChanged": (accountId, state, /*int*/ code, detail) => {
81 const account = this.getAccount(accountId)
82 if (!account) {
83 console.log(`Unknown account ${accountId}`)
84 return
85 }
86 account.registrationState = state
87 console.log("RegistrationStateChanged: " + accountId + " " + state + " " + code + " " + detail)
88 if (state === "REGISTERED") {
89 /*if (tempAccounts[accountId]) {
90
91 const ctx = tempAccounts[accountId]
92 ctx.newUser.accountId = accountId
93 ctx.newUser.jamiId = jami.dring.getAccountDetails(accountId).get("Account.username")
94 //connectedUsers[accountId] = ctx.newUser
95 ctx.done(null, ctx.newUser)
96 delete tempAccounts[accountId]
97 }*/
98 } else if (state === "ERROR_AUTH") {
99 //done(null, false)
100 //remove account
101 }
102 },
103 "RegisteredNameFound": (accountId, state, address, name) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400104 console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400105 const account = this.getAccount(accountId)
106 if (!account) {
107 console.log(`Unknown account ${accountId}`)
108 return
109 }
110 if (state == 0) {
111 const contact = account.getContactFromCache(address)
Adrien Béraud150b4782021-04-21 19:40:59 -0400112 if (!contact.isRegisteredNameResolved())
113 contact.setRegisteredName(name)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400114 }
Adrien Béraude74741b2021-04-19 13:22:54 -0400115 let index = account.lookups.length - 1
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400116 while (index >= 0) {
117 const lookup = account.lookups[index]
118 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
119 lookup.resolve({address, name, state})
Adrien Béraude74741b2021-04-19 13:22:54 -0400120 account.lookups.splice(index, 1)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400121 }
Adrien Béraude74741b2021-04-19 13:22:54 -0400122 index -= 1
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400123 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400124 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400125 "ConversationReady": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400126 console.log(`conversationReady: ${accountId} ${conversationId}`)
127 const account = this.getAccount(accountId)
128 if (!account) {
129 console.log(`Unknown account ${accountId}`)
130 return
131 }
132 let conversation = account.getConversation(conversationId)
133 if (!conversation) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400134 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
135 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
136 conversation = new Conversation(conversationId, accountId, members)
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400137 account.addConversation(conversation)
138 }
139 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400140 "ConversationRemoved": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400141 console.log(`conversationRemoved: ${accountId} ${conversationId}`)
142 const account = this.getAccount(accountId)
143 if (!account) {
144 console.log(`Unknown account ${accountId}`)
145 return
146 }
147 account.removeConversation(conversationId)
148 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400149 "ConversationLoaded": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400150 console.log(`conversationLoaded: ${accountId} ${conversationId}`)
151 const account = this.getAccount(accountId)
152 if (!account) {
153 console.log(`Unknown account ${accountId}`)
154 return
155 }
156 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400157 "MessageReceived": (accountId, conversationId, message) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400158 console.log(`messageReceived: ${accountId} ${conversationId}`)
159 const account = this.getAccount(accountId)
160 if (!account) {
161 console.log(`Unknown account ${accountId}`)
162 return
163 }
164 const conversation = account.getConversation(conversationId)
165 if (!conversation) {
166 conversation.addMessage(message)
167 }
168 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400169 "ConversationRequestReceived": (accountId, conversationId, request) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400170 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`)
171 const account = this.getAccount(accountId)
172 if (!account) {
173 console.log(`Unknown account ${accountId}`)
174 return
175 }
176 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400177 "ConversationMemberEvent": (accountId, conversationId, member, event) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400178 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`)
179 const account = this.getAccount(accountId)
180 if (!account) {
181 console.log(`Unknown account ${accountId}`)
182 return
183 }
184 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400185 "OnConversationError": (accountId, conversationId, code, what) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400186 console.log(`onConversationError: ${accountId} ${conversationId}`)
187 const account = this.getAccount(accountId)
188 if (!account) {
189 console.log(`Unknown account ${accountId}`)
190 return
191 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400192 }
193 })
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400194
195 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach(accountId => {
196 const account = new Account(accountId,
197 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)),
198 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId))
199 )
200 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach(conversationId => {
201 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
Adrien Béraud150b4782021-04-21 19:40:59 -0400202 members.forEach(member => {
203 member.contact = account.getContactFromCache(member.uri)
204 if (!member.contact.isRegisteredNameResolved()) {
205 if (!member.uri) return
206 console.log(`lookupAddress ${accountId} ${member.uri}`)
207 member.contact.setRegisteredName(new Promise((resolve, reject) =>
208 account.lookups.push({address: member.uri, resolve, reject})
209 ).then(result => {
210 if (result.state == 0)
211 return result.name
212 else if (result.state == 1)
213 return undefined
214 else
215 return null
216 }))
217 this.dring.lookupAddress(accountId, "", member.uri)
218 }
219 })
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400220 const conversation = new Conversation(conversationId, accountId, members)
221 account.addConversation(conversation)
222 })
223 this.accounts.push(account)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400224 })
225 }
226
227 addAccount(account) {
228 const params = accountDetailsToNative(account)
229 params.set("Account.type", "RING")
230 return this.dring.addAccount(params)
231 }
232 getAccount(accountId) {
233 for (let i = 0; i < this.accounts.length; i++) {
234 const account = this.accounts[i]
235 if (account.getId() === accountId)
236 return account
237 }
238 return undefined
239 }
240 getAccountList() {
241 return this.accounts
242 }
243 /*getAccountDetails(accountId) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400244 return this.mapToJs(this.dring.getAccountDetails(accountId))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400245 }*/
246 setAccountDetails(accountId, details) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400247 this.dring.setAccountDetails(accountId, mapToNative(details))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400248 }
249 getAudioOutputDeviceList() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400250 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList())
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400251 }
252 getVolume(deviceName) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400253 return this.dring.getVolume(deviceName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400254 }
255 setVolume(deviceName, volume) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400256 return this.dring.setVolume(deviceName, volume)
257 }
258
259 lookupName(accountId, name) {
260 const p = new Promise((resolve, reject) => {
261 const account = this.getAccount(accountId)
262 if (!account) {
263 reject(new Error("Can't find account"))
264 } else {
265 account.lookups.push({name, resolve, reject})
266 }
267 })
268 this.dring.lookupName(accountId, "", name)
269 return p
270 }
271
272 lookupAddress(accountId, address) {
273 console.log(`lookupAddress ${accountId} ${address}`)
274 const p = new Promise((resolve, reject) => {
275 const account = this.getAccount(accountId)
276 if (!account) {
277 reject(new Error("Can't find account"))
278 } else {
279 account.lookups.push({address, resolve, reject})
280 }
281 })
282 this.dring.lookupAddress(accountId, "", address)
283 return p
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400284 }
285
286 stop() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400287 this.dring.fini()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400288 }
289
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400290 addContact(accountId, contactId) {
291 this.dring.addContact(accountId, contactId)
292 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId))
293 if (details.conversationId) {
294 const account = this.getAccount(accountId)
295 if (account) {
296 let conversation = account.getConversation(details.conversationId)
297 if (!conversation) {
298 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId))
299 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
300 conversation = new Conversation(details.conversationId, accountId, members)
301 account.addConversation(conversation)
302 }
303 }
304 }
305 return details
306 }
307
Adrien Béraud150b4782021-04-21 19:40:59 -0400308 getDefaultModerators(accountId) {
309 const account = this.getAccount(accountId)
310 if (!account) {
311 console.log(`Unknown account ${accountId}`)
312 return {}
313 }
314 return JamiDaemon.vectToJs(this.dring.getDefaultModerators(accountId))
315 .map(contactId => account.getContactFromCache(contctId))
316 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400317
Adrien Béraud150b4782021-04-21 19:40:59 -0400318 setDefaultModerators(accountId, moderators) {
319
320 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400321
322 boolToStr(bool) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400323 return bool ? "true" : "false"
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400324 }
325
326 accountDetailsToNative(account) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400327 const params = new this.dring.StringMap()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400328 if (account.managerUri)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400329 params.set("Account.managerUri", account.managerUri)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400330 if (account.managerUsername)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400331 params.set("Account.managerUsername", account.managerUsername)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400332 if (account.archivePassword) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400333 params.set("Account.archivePassword", account.archivePassword)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400334 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400335 console.log("archivePassword required")
Adrien Béraude74741b2021-04-19 13:22:54 -0400336 return
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400337 }
338 if (account.alias)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400339 params.set("Account.alias", account.alias)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400340 if (account.displayName)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400341 params.set("Account.displayName", account.displayName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400342 if (account.enable)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400343 params.set("Account.enable", this.boolToStr(account.enable))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400344 if (account.autoAnswer)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400345 params.set("Account.autoAnswer", this.boolToStr(account.autoAnswer))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400346 if (account.ringtonePath)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400347 params.set("Account.ringtonePath", account.ringtonePath)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400348 if (account.ringtoneEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400349 params.set("Account.ringtoneEnabled", this.boolToStr(account.ringtoneEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400350 if (account.videoEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400351 params.set("Account.videoEnabled", this.boolToStr(account.videoEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400352 if (account.useragent) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400353 params.set("Account.useragent", account.useragent)
354 params.set("Account.hasCustomUserAgent", "TRUE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400355 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400356 params.set("Account.hasCustomUserAgent", "FALSE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400357 }
358 if (account.audioPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400359 params.set("Account.audioPortMin", account.audioPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400360 if (account.audioPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400361 params.set("Account.audioPortMax", account.audioPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400362 if (account.videoPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400363 params.set("Account.videoPortMin", account.videoPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400364 if (account.videoPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400365 params.set("Account.videoPortMax", account.videoPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400366 if (account.localInterface)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400367 params.set("Account.localInterface", account.localInterface)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400368 if (account.publishedSameAsLocal)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400369 params.set("Account.publishedSameAsLocal", this.boolToStr(account.publishedSameAsLocal))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400370 if (account.localPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400371 params.set("Account.localPort", account.localPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400372 if (account.publishedPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400373 params.set("Account.publishedPort", account.publishedPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400374 if (account.publishedAddress)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400375 params.set("Account.publishedAddress", account.publishedAddress)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400376 if (account.upnpEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400377 params.set("Account.upnpEnabled", this.boolToStr(account.upnpEnabled))
378 return params
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400379 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400380 static vectToJs(vect) {
381 const len = vect.size()
382 const outputArr = new Array(len)
383 for (let i = 0; i < len; i++)
384 outputArr[i] = vect.get(i)
385 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400386 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400387 static mapToJs(m) {
388 const outputObj = {}
389 JamiDaemon.vectToJs(m.keys())
390 .forEach(k => outputObj[k] = m.get(k))
391 return outputObj
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400392 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400393 static vectMapToJs(vectMap) {
394 const len = vectMap.size()
395 const outputArr = new Array(len)
396 for (let i = 0; i < len; i++)
397 outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i))
398 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400399 }
400
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400401 mapToNative(map){
402 const ret = new this.dring.StringMap()
403 map.forEach((value, key) => ret.set(key, value))
Adrien Béraude74741b2021-04-19 13:22:54 -0400404 return ret
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400405 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400406}
407
Adrien Béraude74741b2021-04-19 13:22:54 -0400408export default JamiDaemon