blob: 841b979844d624c616ca74fdbd5119a937d55110 [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)
112 contact.setRegisteredName(name)
113 }
Adrien Béraude74741b2021-04-19 13:22:54 -0400114 let index = account.lookups.length - 1
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400115 while (index >= 0) {
116 const lookup = account.lookups[index]
117 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
118 lookup.resolve({address, name, state})
Adrien Béraude74741b2021-04-19 13:22:54 -0400119 account.lookups.splice(index, 1)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400120 }
Adrien Béraude74741b2021-04-19 13:22:54 -0400121 index -= 1
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400122 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400123 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400124 "ConversationReady": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400125 console.log(`conversationReady: ${accountId} ${conversationId}`)
126 const account = this.getAccount(accountId)
127 if (!account) {
128 console.log(`Unknown account ${accountId}`)
129 return
130 }
131 let conversation = account.getConversation(conversationId)
132 if (!conversation) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400133 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
134 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
135 conversation = new Conversation(conversationId, accountId, members)
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400136 account.addConversation(conversation)
137 }
138 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400139 "ConversationRemoved": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400140 console.log(`conversationRemoved: ${accountId} ${conversationId}`)
141 const account = this.getAccount(accountId)
142 if (!account) {
143 console.log(`Unknown account ${accountId}`)
144 return
145 }
146 account.removeConversation(conversationId)
147 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400148 "ConversationLoaded": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400149 console.log(`conversationLoaded: ${accountId} ${conversationId}`)
150 const account = this.getAccount(accountId)
151 if (!account) {
152 console.log(`Unknown account ${accountId}`)
153 return
154 }
155 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400156 "MessageReceived": (accountId, conversationId, message) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400157 console.log(`messageReceived: ${accountId} ${conversationId}`)
158 const account = this.getAccount(accountId)
159 if (!account) {
160 console.log(`Unknown account ${accountId}`)
161 return
162 }
163 const conversation = account.getConversation(conversationId)
164 if (!conversation) {
165 conversation.addMessage(message)
166 }
167 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400168 "ConversationRequestReceived": (accountId, conversationId, request) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400169 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`)
170 const account = this.getAccount(accountId)
171 if (!account) {
172 console.log(`Unknown account ${accountId}`)
173 return
174 }
175 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400176 "ConversationMemberEvent": (accountId, conversationId, member, event) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400177 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`)
178 const account = this.getAccount(accountId)
179 if (!account) {
180 console.log(`Unknown account ${accountId}`)
181 return
182 }
183 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400184 "OnConversationError": (accountId, conversationId, code, what) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400185 console.log(`onConversationError: ${accountId} ${conversationId}`)
186 const account = this.getAccount(accountId)
187 if (!account) {
188 console.log(`Unknown account ${accountId}`)
189 return
190 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400191 }
192 })
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400193
194 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach(accountId => {
195 const account = new Account(accountId,
196 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)),
197 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId))
198 )
199 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach(conversationId => {
200 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
201 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
202 const conversation = new Conversation(conversationId, accountId, members)
203 account.addConversation(conversation)
204 })
205 this.accounts.push(account)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400206 })
207 }
208
209 addAccount(account) {
210 const params = accountDetailsToNative(account)
211 params.set("Account.type", "RING")
212 return this.dring.addAccount(params)
213 }
214 getAccount(accountId) {
215 for (let i = 0; i < this.accounts.length; i++) {
216 const account = this.accounts[i]
217 if (account.getId() === accountId)
218 return account
219 }
220 return undefined
221 }
222 getAccountList() {
223 return this.accounts
224 }
225 /*getAccountDetails(accountId) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400226 return this.mapToJs(this.dring.getAccountDetails(accountId))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400227 }*/
228 setAccountDetails(accountId, details) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400229 this.dring.setAccountDetails(accountId, mapToNative(details))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400230 }
231 getAudioOutputDeviceList() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400232 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList())
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400233 }
234 getVolume(deviceName) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400235 return this.dring.getVolume(deviceName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400236 }
237 setVolume(deviceName, volume) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400238 return this.dring.setVolume(deviceName, volume)
239 }
240
241 lookupName(accountId, name) {
242 const p = new Promise((resolve, reject) => {
243 const account = this.getAccount(accountId)
244 if (!account) {
245 reject(new Error("Can't find account"))
246 } else {
247 account.lookups.push({name, resolve, reject})
248 }
249 })
250 this.dring.lookupName(accountId, "", name)
251 return p
252 }
253
254 lookupAddress(accountId, address) {
255 console.log(`lookupAddress ${accountId} ${address}`)
256 const p = new Promise((resolve, reject) => {
257 const account = this.getAccount(accountId)
258 if (!account) {
259 reject(new Error("Can't find account"))
260 } else {
261 account.lookups.push({address, resolve, reject})
262 }
263 })
264 this.dring.lookupAddress(accountId, "", address)
265 return p
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400266 }
267
268 stop() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400269 this.dring.fini()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400270 }
271
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400272 addContact(accountId, contactId) {
273 this.dring.addContact(accountId, contactId)
274 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId))
275 if (details.conversationId) {
276 const account = this.getAccount(accountId)
277 if (account) {
278 let conversation = account.getConversation(details.conversationId)
279 if (!conversation) {
280 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId))
281 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
282 conversation = new Conversation(details.conversationId, accountId, members)
283 account.addConversation(conversation)
284 }
285 }
286 }
287 return details
288 }
289
290 //getContactDetails
291
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400292// private
293
294 boolToStr(bool) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400295 return bool ? "true" : "false"
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400296 }
297
298 accountDetailsToNative(account) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400299 const params = new this.dring.StringMap()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400300 if (account.managerUri)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400301 params.set("Account.managerUri", account.managerUri)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400302 if (account.managerUsername)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400303 params.set("Account.managerUsername", account.managerUsername)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400304 if (account.archivePassword) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400305 params.set("Account.archivePassword", account.archivePassword)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400306 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400307 console.log("archivePassword required")
Adrien Béraude74741b2021-04-19 13:22:54 -0400308 return
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400309 }
310 if (account.alias)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400311 params.set("Account.alias", account.alias)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400312 if (account.displayName)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400313 params.set("Account.displayName", account.displayName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400314 if (account.enable)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400315 params.set("Account.enable", this.boolToStr(account.enable))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400316 if (account.autoAnswer)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400317 params.set("Account.autoAnswer", this.boolToStr(account.autoAnswer))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400318 if (account.ringtonePath)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400319 params.set("Account.ringtonePath", account.ringtonePath)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400320 if (account.ringtoneEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400321 params.set("Account.ringtoneEnabled", this.boolToStr(account.ringtoneEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400322 if (account.videoEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400323 params.set("Account.videoEnabled", this.boolToStr(account.videoEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400324 if (account.useragent) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400325 params.set("Account.useragent", account.useragent)
326 params.set("Account.hasCustomUserAgent", "TRUE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400327 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400328 params.set("Account.hasCustomUserAgent", "FALSE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400329 }
330 if (account.audioPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400331 params.set("Account.audioPortMin", account.audioPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400332 if (account.audioPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400333 params.set("Account.audioPortMax", account.audioPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400334 if (account.videoPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400335 params.set("Account.videoPortMin", account.videoPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400336 if (account.videoPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400337 params.set("Account.videoPortMax", account.videoPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400338 if (account.localInterface)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400339 params.set("Account.localInterface", account.localInterface)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400340 if (account.publishedSameAsLocal)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400341 params.set("Account.publishedSameAsLocal", this.boolToStr(account.publishedSameAsLocal))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400342 if (account.localPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400343 params.set("Account.localPort", account.localPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400344 if (account.publishedPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400345 params.set("Account.publishedPort", account.publishedPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400346 if (account.publishedAddress)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400347 params.set("Account.publishedAddress", account.publishedAddress)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400348 if (account.upnpEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400349 params.set("Account.upnpEnabled", this.boolToStr(account.upnpEnabled))
350 return params
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400351 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400352 static vectToJs(vect) {
353 const len = vect.size()
354 const outputArr = new Array(len)
355 for (let i = 0; i < len; i++)
356 outputArr[i] = vect.get(i)
357 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400358 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400359 static mapToJs(m) {
360 const outputObj = {}
361 JamiDaemon.vectToJs(m.keys())
362 .forEach(k => outputObj[k] = m.get(k))
363 return outputObj
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400364 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400365 static vectMapToJs(vectMap) {
366 const len = vectMap.size()
367 const outputArr = new Array(len)
368 for (let i = 0; i < len; i++)
369 outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i))
370 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400371 }
372
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400373 mapToNative(map){
374 const ret = new this.dring.StringMap()
375 map.forEach((value, key) => ret.set(key, value))
Adrien Béraude74741b2021-04-19 13:22:54 -0400376 return ret
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400377 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400378}
379
Adrien Béraude74741b2021-04-19 13:22:54 -0400380export default JamiDaemon