blob: 404915babc3b10199d8796acdcbb30ea9499c1ce [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éraud947e8792021-04-15 18:32:44 -040020"use strict";
21
Adrien Béraud6ecaa402021-04-06 17:37:25 -040022const Account = require('./model/Account')
Adrien Béraud0cb76c92021-04-07 19:59:08 -040023const Conversation = require('./model/Conversation')
Adrien Béraud6ecaa402021-04-06 17:37:25 -040024
Adrien Béraud6ecaa402021-04-06 17:37:25 -040025class JamiDaemon {
26 constructor() {
27 this.accounts = []
28 this.dring = require("./dring.node")
29 this.dring.init({
30 "AccountsChanged": () => {
31 console.log("AccountsChanged")
32 const newAccounts = []
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040033 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach(accountId => {
Adrien Béraud6ecaa402021-04-06 17:37:25 -040034 for (const account in this.accounts) {
35 if (account.id === accountId) {
36 newAccounts.push(account)
37 return
38 }
39 }
40 newAccounts.push(new Account(accountId,
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040041 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)),
42 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId))
Adrien Béraud0cb76c92021-04-07 19:59:08 -040043 ))
Adrien Béraud6ecaa402021-04-06 17:37:25 -040044 })
45 this.accounts = newAccounts
46 },
47 "AccountDetailsChanged": (accountId, details) => {
48 console.log(`AccountDetailsChanged ${accountId}`)
49 const account = this.getAccount(accountId)
50 if (!account) {
51 console.log(`Unknown account ${accountId}`)
52 return
53 }
54 account.details = details
55 },
56 "VolatileDetailsChanged": (accountId, details) => {
57 console.log(`VolatileDetailsChanged ${accountId}`)
58 const account = this.getAccount(accountId)
59 if (!account) {
60 console.log(`Unknown account ${accountId}`)
61 return
62 }
63 account.volatileDetails = details
64 },
65 "IncomingAccountMessage": (accountId, from, message) => {
66 console.log(`Received message: ${accountId} ${from} ${message["text/plain"]}`)
67/*
68 if (parser.validate(message["text/plain"]) === true) {
69 console.log(message["text/plain"]);
70 } else {
71
72 user = connectedUsers[accountId];
73 console.log(user.socketId)
74 io.to(user.socketId).emit('receivedMessage', message["text/plain"]);
75 //io.emit('receivedMessage', message["text/plain"]);
76 }*/
77 },
78 "RegistrationStateChanged": (accountId, state, /*int*/ code, detail) => {
79 const account = this.getAccount(accountId)
80 if (!account) {
81 console.log(`Unknown account ${accountId}`)
82 return
83 }
84 account.registrationState = state
85 console.log("RegistrationStateChanged: " + accountId + " " + state + " " + code + " " + detail)
86 if (state === "REGISTERED") {
87 /*if (tempAccounts[accountId]) {
88
89 const ctx = tempAccounts[accountId]
90 ctx.newUser.accountId = accountId
91 ctx.newUser.jamiId = jami.dring.getAccountDetails(accountId).get("Account.username")
92 //connectedUsers[accountId] = ctx.newUser
93 ctx.done(null, ctx.newUser)
94 delete tempAccounts[accountId]
95 }*/
96 } else if (state === "ERROR_AUTH") {
97 //done(null, false)
98 //remove account
99 }
100 },
101 "RegisteredNameFound": (accountId, state, address, name) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400102 console.log(`RegisteredNameFound: ${accountId} ${state} ${address} ${name}`)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400103 const account = this.getAccount(accountId)
104 if (!account) {
105 console.log(`Unknown account ${accountId}`)
106 return
107 }
108 if (state == 0) {
109 const contact = account.getContactFromCache(address)
110 contact.setRegisteredName(name)
111 }
112 let index = account.lookups.length - 1;
113 while (index >= 0) {
114 const lookup = account.lookups[index]
115 if ((lookup.address && lookup.address === address) || (lookup.name && lookup.name === name)) {
116 lookup.resolve({address, name, state})
117 account.lookups.splice(index, 1);
118 }
119 index -= 1;
120 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400121 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400122 "ConversationReady": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400123 console.log(`conversationReady: ${accountId} ${conversationId}`)
124 const account = this.getAccount(accountId)
125 if (!account) {
126 console.log(`Unknown account ${accountId}`)
127 return
128 }
129 let conversation = account.getConversation(conversationId)
130 if (!conversation) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400131 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
132 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
133 conversation = new Conversation(conversationId, accountId, members)
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400134 account.addConversation(conversation)
135 }
136 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400137 "ConversationRemoved": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400138 console.log(`conversationRemoved: ${accountId} ${conversationId}`)
139 const account = this.getAccount(accountId)
140 if (!account) {
141 console.log(`Unknown account ${accountId}`)
142 return
143 }
144 account.removeConversation(conversationId)
145 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400146 "ConversationLoaded": (accountId, conversationId) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400147 console.log(`conversationLoaded: ${accountId} ${conversationId}`)
148 const account = this.getAccount(accountId)
149 if (!account) {
150 console.log(`Unknown account ${accountId}`)
151 return
152 }
153 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400154 "MessageReceived": (accountId, conversationId, message) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400155 console.log(`messageReceived: ${accountId} ${conversationId}`)
156 const account = this.getAccount(accountId)
157 if (!account) {
158 console.log(`Unknown account ${accountId}`)
159 return
160 }
161 const conversation = account.getConversation(conversationId)
162 if (!conversation) {
163 conversation.addMessage(message)
164 }
165 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400166 "ConversationRequestReceived": (accountId, conversationId, request) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400167 console.log(`conversationRequestReceived: ${accountId} ${conversationId}`)
168 const account = this.getAccount(accountId)
169 if (!account) {
170 console.log(`Unknown account ${accountId}`)
171 return
172 }
173 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400174 "ConversationMemberEvent": (accountId, conversationId, member, event) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400175 console.log(`conversationMemberEvent: ${accountId} ${conversationId}`)
176 const account = this.getAccount(accountId)
177 if (!account) {
178 console.log(`Unknown account ${accountId}`)
179 return
180 }
181 },
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400182 "OnConversationError": (accountId, conversationId, code, what) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -0400183 console.log(`onConversationError: ${accountId} ${conversationId}`)
184 const account = this.getAccount(accountId)
185 if (!account) {
186 console.log(`Unknown account ${accountId}`)
187 return
188 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400189 }
190 })
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400191
192 JamiDaemon.vectToJs(this.dring.getAccountList()).forEach(accountId => {
193 const account = new Account(accountId,
194 JamiDaemon.mapToJs(this.dring.getAccountDetails(accountId)),
195 JamiDaemon.mapToJs(this.dring.getVolatileAccountDetails(accountId))
196 )
197 JamiDaemon.vectToJs(this.dring.getConversations(accountId)).forEach(conversationId => {
198 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, conversationId))
199 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
200 const conversation = new Conversation(conversationId, accountId, members)
201 account.addConversation(conversation)
202 })
203 this.accounts.push(account)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400204 })
205 }
206
207 addAccount(account) {
208 const params = accountDetailsToNative(account)
209 params.set("Account.type", "RING")
210 return this.dring.addAccount(params)
211 }
212 getAccount(accountId) {
213 for (let i = 0; i < this.accounts.length; i++) {
214 const account = this.accounts[i]
215 if (account.getId() === accountId)
216 return account
217 }
218 return undefined
219 }
220 getAccountList() {
221 return this.accounts
222 }
223 /*getAccountDetails(accountId) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400224 return this.mapToJs(this.dring.getAccountDetails(accountId))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400225 }*/
226 setAccountDetails(accountId, details) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400227 this.dring.setAccountDetails(accountId, mapToNative(details))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400228 }
229 getAudioOutputDeviceList() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400230 return JamiDaemon.vectToJs(this.dring.getAudioOutputDeviceList())
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400231 }
232 getVolume(deviceName) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400233 return this.dring.getVolume(deviceName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400234 }
235 setVolume(deviceName, volume) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400236 return this.dring.setVolume(deviceName, volume)
237 }
238
239 lookupName(accountId, name) {
240 const p = new Promise((resolve, reject) => {
241 const account = this.getAccount(accountId)
242 if (!account) {
243 reject(new Error("Can't find account"))
244 } else {
245 account.lookups.push({name, resolve, reject})
246 }
247 })
248 this.dring.lookupName(accountId, "", name)
249 return p
250 }
251
252 lookupAddress(accountId, address) {
253 console.log(`lookupAddress ${accountId} ${address}`)
254 const p = new Promise((resolve, reject) => {
255 const account = this.getAccount(accountId)
256 if (!account) {
257 reject(new Error("Can't find account"))
258 } else {
259 account.lookups.push({address, resolve, reject})
260 }
261 })
262 this.dring.lookupAddress(accountId, "", address)
263 return p
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400264 }
265
266 stop() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400267 this.dring.fini()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400268 }
269
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400270 addContact(accountId, contactId) {
271 this.dring.addContact(accountId, contactId)
272 const details = JamiDaemon.mapToJs(this.dring.getContactDetails(accountId, contactId))
273 if (details.conversationId) {
274 const account = this.getAccount(accountId)
275 if (account) {
276 let conversation = account.getConversation(details.conversationId)
277 if (!conversation) {
278 const members = JamiDaemon.vectMapToJs(this.dring.getConversationMembers(accountId, details.conversationId))
279 members.forEach(member => member.contact = account.getContactFromCache(member.uri))
280 conversation = new Conversation(details.conversationId, accountId, members)
281 account.addConversation(conversation)
282 }
283 }
284 }
285 return details
286 }
287
288 //getContactDetails
289
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400290// private
291
292 boolToStr(bool) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400293 return bool ? "true" : "false"
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400294 }
295
296 accountDetailsToNative(account) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400297 const params = new this.dring.StringMap()
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400298 if (account.managerUri)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400299 params.set("Account.managerUri", account.managerUri)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400300 if (account.managerUsername)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400301 params.set("Account.managerUsername", account.managerUsername)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400302 if (account.archivePassword) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400303 params.set("Account.archivePassword", account.archivePassword)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400304 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400305 console.log("archivePassword required")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400306 return;
307 }
308 if (account.alias)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400309 params.set("Account.alias", account.alias)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400310 if (account.displayName)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400311 params.set("Account.displayName", account.displayName)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400312 if (account.enable)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400313 params.set("Account.enable", this.boolToStr(account.enable))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400314 if (account.autoAnswer)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400315 params.set("Account.autoAnswer", this.boolToStr(account.autoAnswer))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400316 if (account.ringtonePath)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400317 params.set("Account.ringtonePath", account.ringtonePath)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400318 if (account.ringtoneEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400319 params.set("Account.ringtoneEnabled", this.boolToStr(account.ringtoneEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400320 if (account.videoEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400321 params.set("Account.videoEnabled", this.boolToStr(account.videoEnabled))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400322 if (account.useragent) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400323 params.set("Account.useragent", account.useragent)
324 params.set("Account.hasCustomUserAgent", "TRUE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400325 } else {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400326 params.set("Account.hasCustomUserAgent", "FALSE")
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400327 }
328 if (account.audioPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400329 params.set("Account.audioPortMin", account.audioPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400330 if (account.audioPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400331 params.set("Account.audioPortMax", account.audioPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400332 if (account.videoPortMin)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400333 params.set("Account.videoPortMin", account.videoPortMin)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400334 if (account.videoPortMax)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400335 params.set("Account.videoPortMax", account.videoPortMax)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400336 if (account.localInterface)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400337 params.set("Account.localInterface", account.localInterface)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400338 if (account.publishedSameAsLocal)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400339 params.set("Account.publishedSameAsLocal", this.boolToStr(account.publishedSameAsLocal))
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400340 if (account.localPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400341 params.set("Account.localPort", account.localPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400342 if (account.publishedPort)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400343 params.set("Account.publishedPort", account.publishedPort)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400344 if (account.publishedAddress)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400345 params.set("Account.publishedAddress", account.publishedAddress)
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400346 if (account.upnpEnabled)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400347 params.set("Account.upnpEnabled", this.boolToStr(account.upnpEnabled))
348 return params
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400349 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400350 static vectToJs(vect) {
351 const len = vect.size()
352 const outputArr = new Array(len)
353 for (let i = 0; i < len; i++)
354 outputArr[i] = vect.get(i)
355 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400356 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400357 static mapToJs(m) {
358 const outputObj = {}
359 JamiDaemon.vectToJs(m.keys())
360 .forEach(k => outputObj[k] = m.get(k))
361 return outputObj
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400362 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400363 static vectMapToJs(vectMap) {
364 const len = vectMap.size()
365 const outputArr = new Array(len)
366 for (let i = 0; i < len; i++)
367 outputArr[i] = JamiDaemon.mapToJs(vectMap.get(i))
368 return outputArr
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400369 }
370
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400371 mapToNative(map){
372 const ret = new this.dring.StringMap()
373 map.forEach((value, key) => ret.set(key, value))
374 return ret;
375 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400376}
377
378module.exports = JamiDaemon;