diff --git a/node/.gitattributes b/node/.gitattributes new file mode 100644 index 000000000..937b84ad7 --- /dev/null +++ b/node/.gitattributes @@ -0,0 +1 @@ +*.ts text eol=lf \ No newline at end of file diff --git a/node/.github/workflows/build-and-test-integration-linux.yml b/node/.github/workflows/build-and-test-integration-linux.yml new file mode 100644 index 000000000..909fad350 --- /dev/null +++ b/node/.github/workflows/build-and-test-integration-linux.yml @@ -0,0 +1,51 @@ +name: 'Linux' +on: push + +jobs: + build-and-test: + name: "Build & Test integration" + runs-on: ubuntu-18.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: System info + run: | + rustc -vV + rustup -vV + cargo -vV + npm --version + node --version + + - name: Pull submodule + run: npm run submodule + + - name: Cache node modules + uses: actions/cache@v1 + with: + path: ${{ env.APPDATA }}/npm-cache # npm cache files are stored in `~/.npm` on Linux/macOS + key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} + + - name: Cache cargo index + uses: actions/cache@v1 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo build + uses: actions/cache@v1 + with: + path: deltachat-core-rust/target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + + - name: Install dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: npm install --ignore-scripts --verbose + + - name: Build deltachat-core-rust & bindings + if: steps.cache.outputs.cache-hit != 'true' + run: npm run build + + - name: Tests (+ Integration tests) + run: npm run test + env: + DCC_NEW_TMP_EMAIL: ${{secrets.DCC_NEW_TMP_EMAIL}} diff --git a/node/.github/workflows/build-and-test.yml b/node/.github/workflows/build-and-test.yml new file mode 100644 index 000000000..6221f6076 --- /dev/null +++ b/node/.github/workflows/build-and-test.yml @@ -0,0 +1,52 @@ +name: 'Build & Test' +on: push + +jobs: + build-and-test: + name: 'Build & Test' + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-18.04, macos-latest, windows-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: '16' + - name: System info + run: | + rustc -vV + rustup -vV + cargo -vV + npm --version + node --version + + - name: Pull submodule + run: npm run submodule + + - name: Cache node modules + uses: actions/cache@v1 + with: + path: ${{ env.APPDATA }}/npm-cache # npm cache files are stored in `~/.npm` on Linux/macOS + key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} + + - name: Cache cargo index + uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry/ + ~/.cargo/git + deltachat-core-rust/target + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}-2 + + - name: Install dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: npm install --ignore-scripts --verbose + + - name: Build deltachat-core-rust & bindings + if: steps.cache.outputs.cache-hit != 'true' + run: npm run build + + - name: Test + run: npm run test diff --git a/node/.github/workflows/make-package.yml b/node/.github/workflows/make-package.yml new file mode 100644 index 000000000..0fe25f4ce --- /dev/null +++ b/node/.github/workflows/make-package.yml @@ -0,0 +1,124 @@ +name: 'Make Package' +on: push + +jobs: + prebuild: + name: 'Prebuild' + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-18.04, macos-latest, windows-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: '16' + - name: System info + run: | + rustc -vV + rustup -vV + cargo -vV + npm --version + node --version + + - name: Pull submodule + run: npm run submodule + + - name: Cache node modules + uses: actions/cache@v2 + with: + path: | + ${{ env.APPDATA }}/npm-cache + ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} + + - name: Cache cargo index + uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry/ + ~/.cargo/git + deltachat-core-rust/target + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}-2 + + - name: Install dependencies & build + if: steps.cache.outputs.cache-hit != 'true' + run: npm install --verbose + + - name: Build Prebuild + run: | + npm run prebuildify + tar -zcvf "${{ matrix.os }}.tar.gz" -C prebuilds . + + - name: Upload Prebuild + uses: actions/upload-artifact@v1 + with: + name: ${{ matrix.os }} + path: ${{ matrix.os }}.tar.gz + + pack-module: + needs: prebuild + name: 'Package the node_module and upload as artifact for testing' + runs-on: ubuntu-18.04 + steps: + - name: install tree + run: sudo apt install tree + - name: Checkout + uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: '16' + - name: System info + run: | + rustc -vV + rustup -vV + cargo -vV + npm --version + node --version + - name: npm run submodule + run: npm run submodule + - name: Download ubuntu prebuild + uses: actions/download-artifact@v1 + with: + name: ubuntu-18.04 + - name: Download macos prebuild + uses: actions/download-artifact@v1 + with: + name: macos-latest + - name: Download windows prebuild + uses: actions/download-artifact@v1 + with: + name: windows-latest + - shell: bash + run: | + ls -lah + mkdir prebuilds + tar -xvzf ubuntu-18.04/ubuntu-18.04.tar.gz -C prebuilds + tar -xvzf macos-latest/macos-latest.tar.gz -C prebuilds + tar -xvzf windows-latest/windows-latest.tar.gz -C prebuilds + tree prebuilds + - name: install dependencies without running scripts + run: npm install --ignore-scripts + - name: build typescript part + run: npm run build:bindings:ts + - name: package + shell: bash + run: | + npm pack . + ls -lah + mv $(find deltachat-node-*) deltachat-node.tgz + - name: Upload Prebuild + uses: actions/upload-artifact@v1 + with: + name: deltachat-node.tgz + path: deltachat-node.tgz + - name: Upload to release + if: contains(github.ref, 'refs/tags/') + uses: svenstaro/upload-release-action@v1-release + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: deltachat-node.tgz + asset_name: deltachat-node.tgz + tag: ${{ github.ref }} + overwrite: true diff --git a/node/.github/workflows/upload-docs.yaml b/node/.github/workflows/upload-docs.yaml new file mode 100644 index 000000000..bff047f45 --- /dev/null +++ b/node/.github/workflows/upload-docs.yaml @@ -0,0 +1,33 @@ +name: Generate & upload documentation + +on: + push: + branches: + - master + +jobs: + generate: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + + - name: Use Node.js 16.x + uses: actions/setup-node@v1 + with: + node-version: 16.x + + - name: npm install and generate documentation + run: | + npm i --ignore-scripts + npx typedoc + mv docs js + + - name: Upload + uses: horochx/deploy-via-scp@v1.0.1 + with: + user: ${{ secrets.USERNAME }} + key: ${{ secrets.KEY }} + host: "delta.chat" + port: 22 + local: "js" + remote: "/var/www/html/" diff --git a/node/.gitignore b/node/.gitignore new file mode 100644 index 000000000..b6a5650bc --- /dev/null +++ b/node/.gitignore @@ -0,0 +1,13 @@ +build/ +package-lock.json +node_modules/ +test.sqlite +db.sqlite +db.sqlite-journal +.nyc_output/ +coverage/ +prebuilds/ +*.tar.gz +dist/ +docs/ +.DS_Store diff --git a/node/.gitmodules b/node/.gitmodules new file mode 100644 index 000000000..0bb85c95e --- /dev/null +++ b/node/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deltachat-core-rust"] + path = deltachat-core-rust + url = ../deltachat-core-rust diff --git a/node/.npmignore b/node/.npmignore new file mode 100644 index 000000000..d7dc06465 --- /dev/null +++ b/node/.npmignore @@ -0,0 +1,40 @@ +.circleci/ +.gitmodules +.nyc_output/ +.travis.yml +appveyor.yml +build/ +build2/ +node_modules +.git +ci_scripts/ +coverage/ +deltachat-core-rust/.circleci +deltachat-core-rust/.git +deltachat-core-rust/.github +deltachat-core-rust/.gitattributes +deltachat-core-rust/appveyor.yml +deltachat-core-rust/ci/ +deltachat-core-rust/ci_scripts/ +deltachat-core-rust/examples/ +deltachat-core-rust/python/ +deltachat-core-rust/tests/ +deltachat-core-rust/target +deltachat-core-rust/test-data +deltachat-core-rust/proptest-regressions +deltachat-core-rust/deltachat-ffi/Doxyfile +deltachat-core-rust/scripts +deltachat-core-rust/benches +deltachat-core-rust/webxdc.md +deltachat-core-rust/standards.md +deltachat-core-rust/draft/ +examples/ +# deltachat-core-rust/assets # don't exclude assets, otherwise it won't build +images/ +test/ +windows.md +*.tar.gz +old_docs.md +.vscode/ +.github/ +.prettierrc.yml diff --git a/node/.npmrc b/node/.npmrc new file mode 100644 index 000000000..43c97e719 --- /dev/null +++ b/node/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/node/.prettierrc.yml b/node/.prettierrc.yml new file mode 100644 index 000000000..5ca635d59 --- /dev/null +++ b/node/.prettierrc.yml @@ -0,0 +1,6 @@ +# .prettierrc +trailingComma: es5 +tabWidth: 2 +semi: false +singleQuote: true +jsxSingleQuote: true diff --git a/node/CHANGELOG.md b/node/CHANGELOG.md new file mode 100644 index 000000000..35fe0e5fb --- /dev/null +++ b/node/CHANGELOG.md @@ -0,0 +1,1699 @@ +# Changelog + +## [Unreleased][unreleased] + +## [1.77.1] - 2022-04-26 + +### BREAKING: we now use node 16 +we now use node version 16. +Please update if you use an older version! ([`nvm`](https://github.com/nvm-sh/nvm) or [`fnm`](https://github.com/Schniz/fnm) are helpful for this). + +### Changed +- update node version from `14` to `16` +- move `Context.getSystemInfo()` to `AccountManager.getSystemInfo()` + +### Fixed +- fix that context was not usable standalone without account managrer anymore. + +## [1.77.0] - 2022-04-14 + +## Removed +- `createTempUser()` + +### Changed +- new webxdc api signature for `Context.getWebxdcStatusUpdates` +- Upgrade to deltachat-core version `1.77.0` + +## [1.76.0] - 2022-03-04 + +## Changed + +- Upgrade to deltachat-core version `1.76.0` + +## [1.75.3] - 2022-03-04 + +## Changed + +- upgrade `node-gyp` to version `9.0.0` to fix the github build action not finding Visual Studio on windows + +## [1.75.2] - 2022-03-03 + +## Changed + +- remove dependency on `lodash.pick` + +## Fixed + +- fix export backup +- fix `Context.setDraft(chatId: number, msg: Message | null)` msg argument type to allow for removing drafts + +## [1.75.1] - 2022-02-03 + +### Changed +- Fix version in package.json +- remove some unused files from the npm package + +## [1.75.0] - 2022-02-03 + +### Changed + +- Upgrade to deltachat-core version `1.75.0` +- `message.setQuote(quotedMessage: Message | null)` - you can now remove quotes from draft messages + +### Added + +- Add `message.forcePlaintext()` +- Add `AccountManager.addClosedAccount()` +- Add `Context.is_open` +- Add `Context.open(passphrase?: string)` +- Add `Message.webxdcInfo` +- Add `Message.parent` +- Add `Context.sendWebxdcStatusUpdate(msgId:number, json: WebxdcSendingStatusUpdate, descr: string)` +- Add `Context.getWebxdcStatusUpdates(msgId: number, statusUpdateId = 0):WebxdcReceivedStatusUpdate[]` +- Add `getWebxdcBlob(message: Message, filename: string):string` + +### Removed + +- removed config value `inbox_watch` +- removed config value `mvbox_watch` + +## [1.70.0] - 2021-12-09 + +### Changed + +- Upgrade to core 1.68.0 + +## [1.68.0] - 2021-11-29 + +### Changed + +- Upgrade to core 1.68.0 + +### Added + +- Add `contact.lastSeen` + +### Changed + +- Upgrade to core 1.68.0 + +## [1.67.0] - 2021-11-25 + +### Added + +- Add `context.getSecurejoinQrCodeSVG(chatId)` + +### Changed + +- Upgrade to core 1.67 + +## [1.65.0] - 2021-11-21 + +### Changed + +- Upgrade to core 1.65 ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`4e71cd6`](https://github.com/deltachat/deltachat-node/commit/4e71cd6)) (Simon Laux) + +### Added + +- Add `message.downloadState` ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`92a90c8`](https://github.com/deltachat/deltachat-node/commit/92a90c8)) (Simon Laux) +- Add missing `chat.canSend` ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`d4dabc4`](https://github.com/deltachat/deltachat-node/commit/d4dabc4)) (Simon Laux) +- Add missing command to readme ([`135836e`](https://github.com/deltachat/deltachat-node/commit/135836e)) (Simon Laux) + + ``` + followup #526 + ``` +- Add setConfigFromQr ([`e89da5c`](https://github.com/deltachat/deltachat-node/commit/e89da5c)) (Simon Laux) +- Add workarounds to build on m1 to readme ([`9297787`](https://github.com/deltachat/deltachat-node/commit/9297787)) (Simon Laux) +- Add link to gyp format docs ([#524](https://github.com/deltachat/deltachat-node/issues/524)) ([`6db2d13`](https://github.com/deltachat/deltachat-node/commit/6db2d13)) (Simon Laux) + +### Fixed + +- Fix tests ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`fb03ba8`](https://github.com/deltachat/deltachat-node/commit/fb03ba8)) (jikstra) +- Fix configure test not quitting/shutting down deltachat properly ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`2f666aa`](https://github.com/deltachat/deltachat-node/commit/2f666aa)) (jikstra) +- Fix segmentation fault in DeltaChat.getProviderFromEmail() ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`96c515b`](https://github.com/deltachat/deltachat-node/commit/96c515b)) (jikstra) + +### Uncategorized + +- Update CHANGELOG ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`d1fd42a`](https://github.com/deltachat/deltachat-node/commit/d1fd42a)) (jikstra) +- Let prettier format test/test.js too ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`1c08330`](https://github.com/deltachat/deltachat-node/commit/1c08330)) (jikstra) +- Update deltachat-core-rust to 1.65.0 ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`6e85063`](https://github.com/deltachat/deltachat-node/commit/6e85063)) (jikstra) +- Update deltachat-core-rust to 1.64.0 ([#528](https://github.com/deltachat/deltachat-node/issues/528)) ([`6c46980`](https://github.com/deltachat/deltachat-node/commit/6c46980)) (Simon Laux) +- format binding.gyp that it is easier to read ([#524](https://github.com/deltachat/deltachat-node/issues/524)) ([`72c2c63`](https://github.com/deltachat/deltachat-node/commit/72c2c63)) (Simon Laux) +- tsconfig ignore core folder ([#524](https://github.com/deltachat/deltachat-node/issues/524)) ([`f19dff2`](https://github.com/deltachat/deltachat-node/commit/f19dff2)) (Simon Laux) + +## [1.60.1] - 2021-08-10 + +### Fixed + +- Fix formatting ([`1860832`](https://github.com/deltachat/deltachat-node/commit/1860832)) (Simon Laux) + +### Uncategorized + +- Prepare for v1.60.1 ([`1f186e4`](https://github.com/deltachat/deltachat-node/commit/1f186e4)) (jikstra) +- Update typescript to fix that some of the generated types did not have the explicit null case included ([`d08de3f`](https://github.com/deltachat/deltachat-node/commit/d08de3f)) (Simon Laux) + + ``` + enable typescript declaration map + ``` +- strict typescript ([`673c276`](https://github.com/deltachat/deltachat-node/commit/673c276)) (Simon Laux) + + ``` + and remove old unused imports + ``` +- provide an integrated version of getProviderFromEmail ([#522](https://github.com/deltachat/deltachat-node/issues/522)) ([`d6dc0a4`](https://github.com/deltachat/deltachat-node/commit/d6dc0a4)) (Simon Laux) + + ``` + that uses the context + ``` +- make package workflow shouldn't run tests ([`808e5cc`](https://github.com/deltachat/deltachat-node/commit/808e5cc)) (jikstra) + +## [1.60.0] - 2021-01-09 + +**ATTENTION**: This release includes many breaking changes + +## [1.56.2] - 2021-23-07 + +### Fixed + +- Fix tsc and tests + +## [1.56.1] - 2021-23-07 + +### Fixed + +- fix set_config null handling: empty string "" value can now be saved (fixes [#505](https://github.com/deltachat/deltachat-node/issues/505)) +- env variable `USE_SYSTEM_LIBDELTACHAT="true"`allows to use system wide libdeltachat + +### Changed + +- drop fs-extra and tempy [**@dotlambda**](https://github.com/dotlambda) +- update to node 14 [**@dotlambda**](https://github.com/dotlambda) + +## [1.56.0] - 2021-27-06 + +### Changed + +- Update deltachat-core-rust to `1.56.0` + +## [1.55.0] - 2021-18-05 + +### Added + +- add option to open context without event handler: `async open(cwd: string, start_event_handler:boolean = true)` + +### Fixed + +- fix return type of deltchat.lookupContactIdByAddr (returned a boolean instead of the chatId) +- fix closing of context -> this should solve the event emitter problem leading to crashs + +### Changed + +- Update deltachat-core-rust to `1.55.0` + +## [1.54.0] - 2021-06-05 + +### Changed + +- Update deltachat-core-rust to `1.54.0` + +## [1.51.1] - 2021-28-03 + +### Fixed + +- Fix package.json version + +## [1.51.0] - 2021-28-03 + +### Added + +- `deltachat.getChatEncrytionInfo(chatId: number)` +- `Contact.authName` and `Contact.status` +- `deltachat.decideOnContactRequest(messageId, decision)` +- `Message.hasHTML`, `Message.setHTML(html: string)` and `deltachat.getMessageHTML(messageId: number)` +- `Message.realChatId` +- `Message.overrideSenderName` and `Message.setOverrideSenderName(senderName: string)` +- `Message.subject` + +### Changed + +- Update deltachat-core-rust to `1.51.0` + +Breaking changes: + +- `deltachat.isIORunning()` removed +- `contact.getFirstName()` removed + +### Fixed + +- Fix `integerToHexColor(colorInt)` function to work correctly with new color generation. + +## [1.50.0] - 2020-22-11 + +### Changed + +- Update deltachat-core-rust to 1.50.0 + +## [1.49.0] - 2020-15-11 + +### Changed + +- Update deltachat-core-rust to 1.49.0 +- changed tests to use mocha and fixed them + +## [1.47.0] - 2020-5-11 + +### Changed + +- Update deltachat-core-rust to 1.47.0 + +Breaking changes: + +- `deltachat.updateDeviceChats()` removed + this is now done automatically during configure + unless the new config-option `bot` is set deltachat-core-rust/[#1957](https://github.com/deltachat/deltachat-node/issues/1957) +- `deltachat.markNoticedAllChats()` removed +- `chat.isVerified()` is replaced by `chat.isProtected()` +- `deltachat.createUnverifiedGroupChat(chatName)` and `deltachat.createVerifiedGroupChat(chatName)` are replaced by a single function `deltachat.createGroupChat(chatName, is_protected)` +- if an message has a quote to another message you need to use `message.getQuotedText()` to get the quoted message + +New: + +- `deltachat.setChatProtection(chatId:number, protect:boolean)` +- `message.setQuote(quotedMessage:Message)` +- `message.getQuotedText():string` +- `message.getQuotedMessage():Message` + +## [1.46.0] - 2020-01-10 + +### Changed + +- Update deltachat-core-rust to 1.46.0 +- BREAKING: There is no `serverFlags` option anymore, use `mail_security` and `send_security` +- BREAKING: Remove `DCN_EVENT_CONFIGURE_FAILED` and `DCN_EVENT_CONFIGURE_SUCCESSFUL` +- BREAKING: Removed deprecated dcn_chat_get_archived and dcn_archive_chat + +## [1.45.0] - 2020-10-09 + +### Changed + +- Update deltachat-core-rust to 1.45.0 + +## [1.44.0] - 2020-08-09 + +### Changed + +- Update deltachat-core-rust to 1.44.0 + +## [1.42.1] - 2020-07-30 + +### Changed + +- Update deltachat-core-rust to [`4e79703`](https://github.com/deltachat/deltachat-node/commit/4e797037c48461284441d43a963ff7810c908f88) + +## [1.42.0] - 2020-07-30 + +### Changed + +- Update deltachat-core-rust to 1.42.0 + +## [1.41.0] - 2020-07-11 + +### Changed + +- add invite call apis +- add getChatlistItemSummary function +- updated dcc to `1.41.0` + +## [1.40.0] - 2020-07-11 + +### Changed + +- Update deltachat-core-rust to v1.40.0 +- Implement ephermeral methods + +## [1.39.0] - 2019-06-24 + +### Changed + +- Update deltachat-core-rust to v1.39.0 +- convert all colors to be hex-string instead of int ([#450](https://github.com/deltachat/deltachat-node/issues/450)) +- We asyncified the joinSecurejoin() function, it now returns a Promise + +### Fixed + +- Fix configure not throwing a real error on `DC_EVENT_ERROR` +- Fixed postinstall script on windows (should now build on windows again) + +## [1.35.0] - 2019-06-12 + +- Update deltachat-core-rust +- We changed the api quite a bit, all methods are now async/await. Please + check the typedocs for changes. + +## [1.34.0] - 2019-06-08 + +### Changed + +- Update deltachat-core-rust to [`2ad014f`](https://github.com/deltachat/deltachat-node/commit/2ad014faf41e530cecfe6020e7a548726b5ae699) +- We're now using the async version of the core, this means that some api has + changed. Please see the example in the README and the typescript function + documentation on what has changed. + +## [1.33.0] - 2019-05-18 + +### Changed + +- Update deltachat-core-rust to 1.33.0 + +## [1.32.1] - 2019-05-16 + +### Changed + +- Implement initiateKeyTransfer2 and continueKeyTransfer2 methods which just + pass through the result from the core. Deprecated initiateKeyTransfer and + continueKeyTransfer methods +- Expose stopOngoingProcess +- expose mute chat functions +- add a docstring to searchMessages explaining it's parameters + +## [1.32.0] - 2019-05-11 + +### Changed + +- Update deltachat-core-rust to 1.32.0 + +### Fixed + +- Fixed `npm run build` not doing anything if a build or prebuild was present + +## [1.29.2] - 2019-04-28 + +### Changed + +- switch from standard to prettier +- Update deltachat-core-rust to 1.29.0 + +## [1.29.1] - 2019-04-23 + +### Changed + +- Fix npm install script + +## [1.29.0] - 2019-04-23 + +### Added + +- Add chat visibility methods [**@Simon-Laux**](https://github.com/Simon-Laux) +- Add method for join_secure [**@nicodh**](https://github.com/nicodh) + +### Changed + +- Update deltachat-core-rust to [`979d7c5`](https://github.com/deltachat/deltachat-node/commit/979d7c562515da2a30983993048cd5184889059c) [**@link2xt**](https://github.com/link2xt) +- Add id and convert the checkQrCode return to json [**@nicodh**](https://github.com/nicodh) +- Clean up building & ci [**@jikstra**](https://github.com/jikstra) + +### Removed + +- removed get_subtitle method [**@link2xt**](https://github.com/link2xt) + +## [1.28.0] - 2019-03-28 + +### Changed + +- Update deltachat-core-rust to v1.28.0 +- Some typefixes [**@Simon-Laux**](https://github.com/Simon-Laux) + +## [1.27.0] - 2019-03-14 + +### Changed + +- Update deltachat-core-rust to v1.27.0 + +## [1.26.0] - 2019-03-03 + +### Added + +- Introduce typedoc + +### Changed + +- Update deltachat-core-rust to v1.26.0 + +## [1.25.0] - 2019-02-21 + +### Added + +- Added `chat.isSingle()` and `chat.isGroup()` methods [**@pabzm**](https://github.com/pabzm) + +### Changed + +- Update deltachat-core-rust to v1.25.0 + +## [1.0.0-beta.26] - 2019-02-07 + +### Changed + +- Switched to Typescript + +### Breaking changes: + +You need to change your imports from + +```js +const DeltaChat = require('deltachat-node') +const C = require('deltachat-node/constants') +``` + +to + +```js +const DeltaChat = require('deltachat-node').default +const { C } = require('deltachat-node') +``` + +## [1.0.0-beta.25] - 2019-02-07 + +### Added + +- Add provider db + +### Changed + +- Update deltachat-core-rust to [`fc0292b`](https://github.com/deltachat/deltachat-node/commit/fc0292bf8ac1af59aaee97cd0c0e286db3a7e4f1) + +## [1.0.0-beta.23.1] - 2019-01-29 + +### Changed + +- Fix windows bug where invoking node-gyp-build on install step failed + +## [1.0.0-beta.23] - 2019-01-24 + +### Added + +- Add typescript support [**@nicodh**](https://github.com/nicodh) [#406](https://github.com/deltachat/deltachat-node/issues/406) + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.23` [**@jikstra**](https://github.com/jikstra) +- Update release instructions [**@lefherz**](https://github.com/lefherz) [**@jikstra**](https://github.com/jikstra) +- Run node-gyp and node-gyp build via npx [**@link2xt**](https://github.com/link2xt) + +## [1.0.0-beta.22] - 2019-12-25 + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.22` + +## [1.0.0-beta.21] - 2019-12-20 + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.21` + +## [1.0.0-beta.18] - 2019-12-16 + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.18` + +## [1.0.0-beta.15] - 2019-12-10 + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.15` + +### Added + +- Implement message dimensions [**@Simon-Laux**](https://github.com/Simon-Laux) + +## [1.0.0-beta.11] - 2019-12-10 + +### Changed + +- Update deltachat-core-rust `301852fd87140790f606f12280064ee9db80d978` + +### Added + +- Implement new device chat methods + +## [1.0.0-beta.10] - 2019-12-05 + +### Changed + +- Update deltachat-core-rust to `1.0.0-beta.10` + +## [1.0.0-alpha.12] - 2019-11-29 + +### Changed + +- Update deltachat-core-rust to `430d4e5f6ebddacac24f4cceebda769a8f367af7` + +### Added + +- Add `addDeviceMessage(label, msg)` and `chat.isDeviceTalk()` methods + +## [1.0.0-alpha.11] - 2019-11-04 + +### Changed + +- Update deltachat-core-rust to Update deltachat-core-rust to `67e2e4d415824f7698488abf609ae9f91c7c92b9` [**@jikstra**](https://github.com/jikstra) [**@simon-laux**](https://github.com/simon-laux) +- Replace setStringTable with setStockTranslation ([#389](https://github.com/deltachat/deltachat-node/issues/389)) [**@link2xt**](https://github.com/link2xt) +- Make it possible to configure {imap,smtp}\_certificate_checks ([#388](https://github.com/deltachat/deltachat-node/issues/388)) [**@link2xt**](https://github.com/link2xt) +- Enable update of DC configurations [**@nicodh**](https://github.com/nicodh) + +## [1.0.0-alpha.10] - 2019-10-08 + +### Changed + +- Updated deltachat-core-rust to `c23e98ff83e02f8d53d21ab42c5be952fcd19fbc` [**@Simon-Laux**](https://github.com/Simon-Laux) [**@Jikstra**](https://github.com/Jikstra) +- Update node-gyp [**@ralphtheninja**](https://github.com/ralphtheninja) [#383](https://github.com/deltachat/deltachat-node/issues/383) +- Implement new introduced core constants [**@hpk42**](https://github.com/hpk42) [#385](https://github.com/deltachat/deltachat-node/issues/385) + +## [1.0.0-alpha.9] - 2019-10-01 + +### Changed + +- Update deltachat-core-rust to `75f41bcb90dd0399c0110b52a7f2b53632477934` + +## [1.0.0-alpha.7] - 2019-09-26 + +### Changed + +- Update deltachat-core-rust to commit `1ed543b0e8535d5d1004b77e0d2a1475123bb18a` [**@jikstra**](https://github.com/jikstra) [#379](https://github.com/deltachat/deltachat-node/issues/379) +- Fix tests by adjusting them to minor rust core changes [**@jikstra**](https://github.com/jikstra) [#379](https://github.com/deltachat/deltachat-node/issues/379) +- Upgrade napi-macros [**@ralphtheninja**](https://github.com/ralphtheninja) [#378](https://github.com/deltachat/deltachat-node/issues/378) + +## [1.0.0-alpha.6] - 2019-09-25 + +**Note**: Between 1.0.0-alpha.3 and this version alpha.4 and alpha.5 versions happend but were pre releases. Changelog for core-rust updates (as they mainly were) are skipped, all other changes are condensed into this release + +### Changed + +- Upgrade `deltachat-core-rust` submodule to `efc563f5fff808e968e8672d1654223f03613dca` [**@jikstra**](https://github.com/jikstra) +- Change License to GPL-3.0-or-later ([#375](https://github.com/deltachat/deltachat-node/issues/375)) [**@ralphtheninja**](https://github.com/ralphtheninja) +- Refactor test/index.js, use default tape coding style ([#368](https://github.com/deltachat/deltachat-node/issues/368)) [**@jikstra**](https://github.com/jikstra) + +## [1.0.0-alpha.3] - 2019-07-22 + +### Changed + +- Upgrade `deltachat-core-rust` submodule to `6f798008244304d1c7dc499fd8cb00b38ca2cddc` [**@jikstra**](https://github.com/jikstra) +- Upgrade hallmark dependency to version 1.0.0 [**@nicodh**](https://github.com/nicodh) [#363](https://github.com/deltachat/deltachat-node/issues/363) +- Upgrade standard to version 13.0.1 [**@ralptheninja**](https://github.com/ralptheninja) [#362](https://github.com/deltachat/deltachat-node/issues/362) + +### Fixed + +- Fix `call_threadsafe_function` blocking/producing deadlock [**@jikstra**](https://github.com/jikstra) [#364](https://github.com/deltachat/deltachat-node/issues/364) + +### Added + +- Expose `dcn_perform_imap_jobs` [**@jikstra**](https://github.com/jikstra) [#364](https://github.com/deltachat/deltachat-node/issues/364) + +## [1.0.0-alpha.2] - 2019-07-09 + +### Changed + +- Upgrade `deltachat-core-rust` submodule to `816fa1df9b23ed0b30815e80873258432618d7f0` ([**@jikstra**](https://github.com/jikstra)) + +## [1.0.0-alpha.1] - 2019-07-02 + +### Changed + +- Upgrade `deltachat-core-rust` submodule to `v1.0.0-alpha.1` ([**@ralptheninja**](https://github.com/ralptheninja)) + +### Fixed + +- Fix defaults for mailbox flags ([**@nicodh**](https://github.com/nicodh)) + +### Removed + +- Remove `compile_date` from info ([**@ralptheninja**](https://github.com/ralptheninja)) + +## [1.0.0-alpha.0] - 2019-06-07 + +### Changed + +- Make options to `dc.configure()` 1-1 with core ([**@ralptheninja**](https://github.com/ralptheninja)) +- Bring back prebuilt binaries using `node-gyp-build`, `prebuildify` and `prebuildify-ci` ([**@ralptheninja**](https://github.com/ralptheninja)) +- Run coverage on Travis ([**@ralptheninja**](https://github.com/ralptheninja)) + +### Added + +- Add `deltachat-core-rust` submodule ([**@ralptheninja**](https://github.com/ralptheninja)) +- Add windows to `bindings.gyp` ([**@ralptheninja**](https://github.com/ralptheninja)) +- Add AppVeyor ([**@ralptheninja**](https://github.com/ralptheninja)) +- Add `.inbox_watch`, `.sentbox_watch`, `.mvbox_watch`, `.mvbox_move` and `.show_emails` to options ([**@ralptheninja**](https://github.com/ralptheninja)) + +### Removed + +- Remove `deltachat-core` submodule ([**@ralptheninja**](https://github.com/ralptheninja)) +- Remove http get (handled by core) ([**@ralptheninja**](https://github.com/ralptheninja)) +- Remove code related to `dc_check_password()` ([**@ralptheninja**](https://github.com/ralptheninja)) +- Remove deprecated `.imap_folder` option ([**@ralptheninja**](https://github.com/ralptheninja)) + +### Fixed + +- Make `dc.close()` async ([**@ralptheninja**](https://github.com/ralptheninja)) +- Fix spawn npm on windows ([**@Simon-Laux**](https://github.com/Simon-Laux)) +- Call `dc_str_unref()` to free strings allocated in core ([**@ralptheninja**](https://github.com/ralptheninja)) +- Use mutex from `libuv` for windows support ([**@ralptheninja**](https://github.com/ralptheninja)) + +## [0.44.0] - 2019-05-14 + +### Changed + +- Remove --verbose from npm install [**@ralptheninja**](https://github.com/ralptheninja) +- Add section in README about making releases [**@ralphtheninja**](https://github.com/ralphtheninja) +- Update nyc [**@ralphtheninja**](https://github.com/ralphtheninja) [#305](https://github.com/deltachat/deltachat-node/issues/305) + +### Added + +- Allow the bindings to build against the installed version of libdeltachat.so using pkg-config. Also fixes a lot of ci related issues. [**@flub**](https://github.com/flub) [**@jikstra**](https://github.com/jikstra) [#257](https://github.com/deltachat/deltachat-node/issues/257) +- Implement bundle dependencies script for mac os x [**@nicodh**](https://github.com/nicodh) [**@jikstra**](https://github.com/jikstra) [#276](https://github.com/deltachat/deltachat-node/issues/276) +- Add marker methods for location streaming [**@ralphtheninja**](https://github.com/ralphtheninja) [#301](https://github.com/deltachat/deltachat-node/issues/301) +- Build deltachat-core with rpgp [**@flub**](https://github.com/flub) [#301](https://github.com/deltachat/deltachat-node/issues/301) + +### Fixed + +- Fix scripts, refactor and split out helper methods for scripts [**@jikstra**](https://github.com/jikstra) [#284](https://github.com/deltachat/deltachat-node/issues/284) +- Set variable to point to core root and fix include dirs [**@ralphtheninja**](https://github.com/ralphtheninja) [#285](https://github.com/deltachat/deltachat-node/issues/285) +- Build on node v12 and v11 [**@jikstra**](https://github.com/jikstra) [#293](https://github.com/deltachat/deltachat-node/issues/293) +- Fix docker integration tests [**@ralphtheninja**](https://github.com/ralphtheninja) [#299](https://github.com/deltachat/deltachat-node/issues/299) +- Fix broken http get for autoconfigure [**@ralphtheninja**](https://github.com/ralphtheninja) [#303](https://github.com/deltachat/deltachat-node/issues/303) +- Fix mac ci [**@jikstra**](https://github.com/jikstra) [#313](https://github.com/deltachat/deltachat-node/issues/313) +- Fix bundle dependency scripts [**@jikstra**](https://github.com/jikstra) [#314](https://github.com/deltachat/deltachat-node/issues/314) + +## [0.43.0] - 2019-04-26 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.43.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Handle `DC_EVENT_LOCATION_CHANGED` event ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `msg.setLocation()` and `msg.hasLocation()` ([**@nicodh**](https://github.com/nicodh)) + +## [0.42.0] - 2019-04-25 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.42.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Adding gnu99 cstd for CentOS 7 build ([**@chrries**](https://github.com/chrries)) +- Tweak preinstall script for `node-gyp-build` to work on Windows ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Switch to using `rpgp` for Mac ([**@nicodh**](https://github.com/nicodh)) +- Make events push based ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Update dependencies ([**@jikstra**](https://github.com/jikstra)) +- Update to node 10 on Travis for Mac ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Implement location streaming ([**@nicodh**](https://github.com/nicodh)) + +### Removed + +- Skip integration tests for PR from forked repositories ([**@nicodh**](https://github.com/nicodh)) +- Remove prebuildify ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove Jenkins ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.40.2] - 2019-02-12 + +### Fixed + +- Fix Jenkins vs Travis race ([**@ralptheninja**](https://github.com/ralphtheninja)) + +## [0.40.1] - 2019-02-12 + +### Changed + +- Upgrade core to v0.40.0 ([#240](https://github.com/deltachat/deltachat-node/issues/240)) ([`29bb00c`](https://github.com/deltachat/deltachat-node/commit/29bb00c)) (jikstra) + +### Uncategorized + +- Update CHANGELOG ([#240](https://github.com/deltachat/deltachat-node/issues/240)) ([`ffc4ed9`](https://github.com/deltachat/deltachat-node/commit/ffc4ed9)) (jikstra) + +## [0.40.0] - 2019-02-12 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.40.0` ([**@jikstra**](https://github.com/jikstra)) + +## [0.39.0] - 2019-01-17 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.39.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.38.0] - 2019-01-15 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.38.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `Message#getSortTimestamp()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `Message#hasDeviatingTimestamp()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.36.0] - 2019-01-08 + +### Changed + +- Allow passing in string to `dc.sendMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Upgrade `deltachat-core` dependency to `v0.36.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Make example in README more useful ([**@Simon-Laux**](https://github.com/Simon-Laux)) +- Prebuild for `node@8.6.0` and `electron@3.0.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Make `deltachat-node` build on mac ([**@jikstra**](https://github.com/jikstra)) +- Tweak integration tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `hallmark` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.35.0] - 2019-01-05 + +### Changed + +- Uncomment `mvbox` and `sentbox` code ([**@jikstra**](https://github.com/jikstra)) +- Upgrade `deltachat-core` dependency to `v0.35.0` (changes to `dc_get_chat_media()` and `dc_get_next_media()`) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +**Historical Note** We started following the core version from this point to make it easier to identify what we're using in e.g. the desktop application. + +## [0.30.1] - 2018-12-28 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.34.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.30.0] - 2018-12-24 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.32.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +**Historical Note** This release temporarily disables `mvbox` and `sentbox` threads, i.e. only one thread for IMAP and SMTP will be running. + +## [0.29.0] - 2018-12-20 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.31.1` (core sends simultaneously to the INBOX) ([**@karissa**](https://github.com/karissa), [**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.28.2] - 2018-12-18 + +### Fixed + +- Pass in empty string instead of `null` as `param2` in `dc.importExport()` ([**@jikstra**](https://github.com/jikstra)) + +### Changed + +- Change minimum node version to `v8.6.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.28.1] - 2018-12-10 + +### Fixed + +- Pass in empty string instead of `null` in `dc.setChatProfileImage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.28.0] - 2018-12-10 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.29.0` (colors for chat and contact) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `chat.getColor()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `contact.getColor()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.27.0] - 2018-12-05 + +### Changed + +- Rewrite tests to be pure unit tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Enable chaining in `Message` class for all .set methods ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Document workflow for prebuilt binaries ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `contact.getProfileImage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `docker.bash` script ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dc.getDraft()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dc.setDraft()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `dc.setTextDraft()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `chat.getDraftTimestamp()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `chat.getTextDraft()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.26.1] - 2018-11-28 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.27.0` (chat profile image bug fix) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.26.0] - 2018-11-27 + +### Changed + +- Rewrite tests to be completely based on environment variables ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `isInfo` and `isForwarded` to `message.toJson()` ([**@karissa**](https://github.com/karissa)) +- Add `prebuildify` for making prebuilt binaries ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add Jenkins ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +**Historical Note** From this version and onward we deliver prebuilt binaries, starting with linux x64. + +## [0.25.0] - 2018-11-23 + +### Changed + +- Upgrade `deltachat-core` dependency to `v0.26.1` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- README: `libetpan-dev` _can_ be installed on the system without breaking compile ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- `events.js` is now automatically generated based on `deltachat-core/src/deltachat.h` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `DC_EVENT_ERROR_NETWORK` and `DC_EVENT_ERROR_SELF_NOT_IN_GROUP` events ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `DC_ERROR_NO_NETWORK` and `DC_STR_NONETWORK` constants ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- `dc.setChatProfileImage()` accepts `null` image ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.24.0] - 2018-11-16 + +### Changed + +- Make `dc.configure()` take camel case options ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Update troubleshooting section in README ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Upgrade `deltachat-core` dependency to `v0.25.1` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Upgrade `opn-cli` devDependency to `^4.0.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `.imapFolder` option ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dc.maybeNetwork()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `DC_EVENT_IS_OFFLINE` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Make compile work even if `libetpan-dev` is installed on the system ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +**Historical Note** From this version and onwards `deltachat-core` is now in single folder mode. + +## [0.23.1] - 2018-11-02 + +### Changed + +- Allow passing `NULL` to `dc_set_chat_profile_image()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.23.0] - 2018-10-30 + +### Changed + +- Allow users to remove config options with `dc.configure()` ([**@karissa**](https://github.com/karissa)) +- Upgrade `deltachat-core` to `v0.24.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `DeltaChat#setStringTable()` and `DeltaChat#clearStringTable()` ([**@r10s**](https://github.com/r10s), [**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `Message#getMediainfo()` and `Message#setMediainfo()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.22.1] - 2018-10-25 + +### Added + +- Add `.showPadlock` to `Message#toJson()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.22.0] - 2018-10-23 + +### Added + +- Add static method `DeltaChat#getSystemInfo()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Changed + +- README: Update docs on `dc.getInfo()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.21.0] - 2018-10-17 + +### Changed + +- Upgrade `deltachat-core` to `v0.23.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add static method `DeltaChat#maybeValidAddr()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dc.lookupContactIdByAddr()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dc.getMimeHeaders()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `message.getReceivedTimestamp()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `selfavatar` option ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `mdns_enabled` option ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `DC_EVENT_FILE_COPIED` event ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.20.0] - 2018-10-11 + +### Changed + +- Upgrade `deltachat-core` for improved speed when sending messages ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove default value parameter from `DeltaChat#getConfig()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendAudioMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendFileMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendImageMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendTextMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendVcardMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendVideoMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DeltaChat#sendVoiceMessage()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.19.2] - 2018-10-08 + +### Fixed + +- Use path to db file in `DeltaChat#getConfig(path, cb)` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.19.1] - 2018-10-08 + +### Fixed + +- Remove all listeners in `DeltaChat#close()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.19.0] - 2018-10-08 + +### Changed + +- `DeltaChat#getInfo()` returns an object based on parsed data from `dc_get_info()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rename `Message#getType()` to `Message#getViewType()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rename `MessageType` to `MessageViewType` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- `DeltaChat#messageNew()` accepts an optional `viewType` parameter (defaults to `DC_MSG_TEXT`) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add static method `DeltaChat#getConfig(path, cb)` for retrieving configuration given a folder ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `UPGRADING.md` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `DeltaChat#setConfigInt()` and `DeltaChat#getConfigInt()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `Message#setType()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DC_MSG_UNDEFINED` view type ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.18.2] - 2018-10-06 + +### Removed + +- Remove redundant `DC_EVENT_GET_STRING` and `DC_EVENT_GET_QUANTITY_STRING` events ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.18.1] - 2018-10-05 + +### Changed + +- Fix broken link to core build instructions ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Print ut error message in test autoconfig test ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Link against global `-lsasl2` instead and fallback to bundled version ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.18.0] - 2018-09-27 + +### Changed + +- Build against system version of openssl ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Update links to c documentation ([**@r10s**](https://github.com/r10s)) +- Upgrade `deltachat-core` for hex-literals fix ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Print out env and openssl version on Travis ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Test `DC_EVENT_CONFIGURE_PROGRESS` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.17.1] - 2018-09-25 + +### Changed + +- Close context database in `dc.close()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.17.0] - 2018-09-25 + +### Changed + +- Tweak `binding.gyp` to prepare for windows ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Implement `dc.isOpen()` to check for open context database ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove redundant include path for `libetpan` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.16.0] - 2018-09-24 + +### Changed + +- Make `dc.initiateKeyTransfer()` and `dc.continueKeyTransfer()` async ([**@jikstra**](https://github.com/jikstra)) +- Rewrite rebuild script in JavaScript ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Make JavaScript event handler private ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Document `toJson()` methods ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Use `NULL` instead of `0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- `npm install` is silent and builds in `Release` mode by default ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Upgrade `deltachat-core` for message changed event fixes ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Callback is not optional in `dc.open()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.15.0] - 2018-09-20 + +### Changed + +- Upgrade `deltachat-core` for `DC_EVENT_HTTP_GET` empty string fix ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Change official support to node 8 (and electron 2) ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Refactor tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Test autoconfig on merlinux server ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Reset `dc_event_http_done` to 0 after lock is released ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.14.0] - 2018-09-19 + +### Changed + +- Upgrade `deltachat-core` to latest master ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add support for `DC_EVENT_HTTP_GET` ([**@ralphtheninja**](https://github.com/ralphtheninja), [**@r10s**](https://github.com/r10s)) + +### Fixed + +- Fix incorrect link order causing missing `RSA_check_key` symbol ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.13.1] - 2018-09-18 + +### Fixed + +- Fix symlink problems in `deltachat-core` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.13.0] - 2018-09-17 + +### Changed + +- Use `deltachat-core#flub-openssl` so we can build on mac ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.12.0] - 2018-09-17 + +### Changed + +- Upgrade `debug` devDependency from `^3.1.0` to `^4.0.0` ([**@greenkeeper**](https://github.com/greenkeeper)) +- Upgrade `deltachat-core` for `dc_marknoticed_all_chats()`, new constants and new events ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `dc.markNoticedAllChats()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add events `DC_EVENT_SMTP_CONNECTED`, `DC_EVENT_IMAP_CONNECTED` and `DC_EVENT_SMTP_MESSAGE_SENT` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add constants `DC_CHAT_ID_ALLDONE_HINT`, `DC_EVENT_IMAP_CONNECTED`, `DC_EVENT_SMTP_CONNECTED`, `DC_EVENT_SMTP_MESSAGE_SENT` and `DC_GCL_ADD_ALLDONE_HINT` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.11.0] - 2018-09-10 + +### Changed + +- Upgrade `deltachat-core` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rewrite open and configure workflow ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Document tests, coverage and npm scripts ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.10.0] - 2018-09-03 + +### Changed + +- Sort keys in `constants.js` alphabetically ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Upgrade `deltachat-core` for new `DC_EVENT_FILE_COPIED` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Test `DC_EVENT_FILE_COPIED` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Fix profile image tests (image moved to blob dir) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.9.4] - 2018-08-30 + +### Added + +- Add `.id` and `.isSetupmessage` to `Message#toJson()` ([**@karissa**](https://github.com/karissa)) + +### Removed + +- Remove `.id` from `Lot#toJson()` ([**@karissa**](https://github.com/karissa)) + +## [0.9.3] - 2018-08-29 + +### Fixed + +- Fix typo ([**@karissa**](https://github.com/karissa)) + +## [0.9.2] - 2018-08-29 + +### Added + +- Add `.state` and `.mediaInfo` to `Message#toJson()` ([**@karissa**](https://github.com/karissa)) + +## [0.9.1] - 2018-08-29 + +### Changed + +- Upgrade `deltachat-core` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Forward strings from `data1` in the same way as for `data2` ([**@r10s**](https://github.com/r10s)) +- Upgrade `standard` devDependency to `^12.0.0` ([**@greenkeeper**](https://github.com/greenkeeper)) +- Full coverage of `message.js` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Test `dc.getBlobdir()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Test `displayname`, `selfstatus` and `e2ee_enabled` config ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `.file` to `Message#toJson()` ([**@karissa**](https://github.com/karissa)) + +## [0.9.0] - 2018-08-28 + +### Changed + +- Upgrade `split2` from `^2.2.0` to `^3.0.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Run tests using `greenmail` server ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Test `dc.getInfo()`, `dc.getConfig()` and `dc.getConfigInt()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add dependency badge ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `toJson()` methods ([**@karissa**](https://github.com/karissa)) + +## [0.8.0] - 2018-08-26 + +### Changed + +- Refactor classes into separate files ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rename `dev` script to `rebuild` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add support for all configuration parameters ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `coverage-html-report` script ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `start` script ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Don't check for bits in the message type ([**@r10s**](https://github.com/r10s)) + +## [0.7.0] - 2018-08-26 + +### Changed + +- Upgrade `deltachat-core` for `secure_delete` and fixes to `libetpan` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Test events ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `MessageType` class ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `dependency-check` module ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Fix `message.getFilebytes()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.6.2] - 2018-08-24 + +### Changed + +- Prefer async `mkdirp` ([**@karissa**](https://github.com/karissa)) + +## [0.6.1] - 2018-08-24 + +### Fixed + +- Add `mkdirp.sync()` in `dc.open()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.6.0] - 2018-08-22 + +### Changed + +- Implement a temporary polling mechanism for events to relax the requirements for `node` and `electron` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.5.1] - 2018-08-20 + +### Changed + +- Put back `'ready'` event ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Fix broken tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.5.0] - 2018-08-20 + +### Changed + +- Bump `deltachat-core` for updated constants and fallbacks to `cyrussasl`, `iconv` and `openssl` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Refactor event handler code (constructor doing too much) ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Use `.addr` instead of `.email` for consistency with `deltachat-core` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rename `.root` to `.cwd` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `DeltaChat#open(cb)` (constructor doing too much) ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove `'ready'` event because `.open()` calls back when done ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `DEBUG` output from tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.4.1] - 2018-08-14 + +### Changed + +- Bump `deltachat-core` to version without symlinks, which removes the need of having `libetpan-dev` installed system wide ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.4.0] - 2018-08-12 + +### Changed + +- Change `MessageState#_state` to a public property ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Allow passing in non array to `DeltaChat#starMessages` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Link to classes in README ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Make all methods taking some form of id to accept strings as well as numbers ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `DeltaChat#getStarredMessages()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `DeltaChat#getChats()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `Message#isDeadDrop()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add npm package version badge ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.3.0] - 2018-08-09 + +### Changed + +- Upgrade `napi-macros` to `^1.7.0` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Rename `NAPI_UTF8()` to `NAPI_UTF8_MALLOC()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Pass 1 or 0 to `dcn_set_offline()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Use `NAPI_ARGV_*` macros ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Wait with configure until db is open and emit `'ready'` when done configuring ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Start threads after `open()` is finished ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Emit `ALL` and individual events ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add reverse lookup of events from `int` to `string` in `events.js` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `MessageState` class ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Fix :bug: with wrong index for query in `dcn_search_msg()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.2.0] - 2018-08-03 + +### Changed + +- Tweak license description ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Handle `NULL` strings in `NAPI_RETURN_AND_FREE_STRING()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Refactor array code in `src/module.c` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Split `dc.createGroupChat()` into `dc.createUnverifiedGroupChat()` and `dc.createVerifiedGroupChat()` ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Update minimal node version in README ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Added + +- Add `nyc` and `coveralls` for code coverage ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Add `constants.js` by parsing `deltachat-core/src/deltachat.h` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Removed + +- Remove magic numbers from tests ([**@ralphtheninja**](https://github.com/ralphtheninja)) +- Remove `console.log()` from `binding.js` ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +### Fixed + +- Throw helpful error if tests are missing credentials ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.1.1] - 2018-08-02 + +### Fixed + +- Fix issues when installing from npm ([**@ralphtheninja**](https://github.com/ralphtheninja)) + +## [0.1.0] - 2018-08-01 + +:seedling: Initial release. + +## [Added] - YYYY-MM-DD + +- `context.createBroadcastList(): number` +- `context.downloadFullMessage(messageId: number)` +- `message.downloadState` +- added missing `chat.canSend` +- added missing `context.setConfigFromQr(qrcodeContent:string)` + +## [Added] - YYYY-MM-DD + +- `context.getProviderFromEmail(email:string)` + +## [Added] - YYYY-MM-DD + +- `dcn_chat_is_contact_request()` and `chat.isContactRequest()` +- `dcn_accept_chat` and `context.acceptChat()` +- `dcn_block_chat` and `context.blockChat()` +- `dcn_accounts_get_all` and `DeltaChat.accounts()` +- `dcn_accounts_add_account` and `DeltaChat.addAccount()` +- `dcn_accounts_get_selected_account` and `DeltaChat.selectAccount()` +- `dcn_accounts_new` and `DeltaChat constructor` +- `dcn_accounts_remove_account` and `DeltaChat.removeAccount` +- `dcn_accounts_get_account` and `DeltaChat.accountContext` +- `dcn_accounts_start_event_handler` and `DeltaChat.startEvents` + +## [Changed] - YYYY-MM-DD + +- BREAKING: `joinSecurejoin(qrCode: string): number` returns immediately now (no promise anymore): On errors, 0 is returned, however, most errors will happen during handshake later on. +- Update deltachat-core-rust to 1.65.0 + +## [Changed] - YYYY-MM-DD + +- rename `DeltaChat` to `AccountManager` +- rename `DeltaChat.accounts()` to `AccountManager.getAllAccountIds()` +- enable strict typescript +- update typescript to version `3.9.10` +- enable typescript declaration map to help IDEs to jump to the ts source files, not the declaration files. + +## [Changed] - YYYY-MM-DD + +- constructor of DeltaChat changed, it directly opens a accounts object now +- all api calls got moved from DeltaChat class to Context. You can retrieve it through `DeltaChat.accountContext()` +- `DeltaChat.newTempContext()` is now called `DeltaChat.newTemporary()` +- Update deltachat-core-rust to 1.60.0 + +## [Deprecated] - YYYY-MM-DD + +- `DeltaChat.getProviderFromEmail(email:string)`, please use `context.getProviderFromEmail(email:string)` instead + +## [Fixed] - YYYY-MM-DD + +- Fix documentation comment of `AccountManager` +- Fix configure for multiple accounts at once + +## [Removed] - YYYY-MM-DD + +- `dcn_create_chat_by_msg_id()` +- `dcn_decide_on_contact_request()` +- `dcn_marknoticed_contact()` +- `dcn_msg_get_real_chat_id()` and `chat.getRealChatId()`, as deaddrops got removed, use `chat.getChatId()` instead + +## Removed + +- Remove `dc_msg_has_deviating_timestamp` prototype [**@link2xt**](https://github.com/link2xt) + +[unreleased]: https://github.com/deltachat/deltachat-node/compare/v1.77.1...HEAD + +[1.77.1]: https://github.com/deltachat/deltachat-node/compare/v1.76.0...v1.77.1 + +[1.77.0]: https://github.com/deltachat/deltachat-node/compare/v1.76.0...v1.77.0 + +[1.76.0]: https://github.com/deltachat/deltachat-node/compare/v1.75.3...v1.76.0 + +[1.75.3]: https://github.com/deltachat/deltachat-node/compare/v1.75.2...v1.75.3 + +[1.75.2]: https://github.com/deltachat/deltachat-node/compare/v1.75.1...v1.75.2 + +[1.75.1]: https://github.com/deltachat/deltachat-node/compare/v1.75.0...v1.75.1 + +[1.75.0]: https://github.com/deltachat/deltachat-node/compare/v1.70.0...v1.75.0 + +[1.70.0]: https://github.com/deltachat/deltachat-node/compare/v1.68.0...v1.70.0 + +[1.68.0]: https://github.com/deltachat/deltachat-node/compare/v1.67.0...v1.68.0 + +[1.67.0]: https://github.com/deltachat/deltachat-node/compare/v1.65.0...v1.67.0 + +[1.65.0]: https://github.com/deltachat/deltachat-node/compare/v1.60.1...v1.65.0 + +[1.60.1]: https://github.com/deltachat/deltachat-node/compare/v1.60.0...v1.60.1 + +[1.60.0]: https://github.com/deltachat/deltachat-node/compare/v1.56.2...v1.60.0 + +[1.56.2]: https://github.com/deltachat/deltachat-node/compare/v1.56.1...v1.56.2 + +[1.56.1]: https://github.com/deltachat/deltachat-node/compare/v1.56.0...v1.56.1 + +[1.56.0]: https://github.com/deltachat/deltachat-node/compare/v1.55.0...v1.56.0 + +[1.55.1]: https://github.com/deltachat/deltachat-node/compare/v1.55.0...v1.55.1 + +[1.55.0]: https://github.com/deltachat/deltachat-node/compare/v1.54.0...v1.55.0 + +[1.54.0]: https://github.com/deltachat/deltachat-node/compare/v1.51.1...v1.54.0 + +[1.51.1]: https://github.com/deltachat/deltachat-node/compare/v1.51.0...v1.51.1 + +[1.51.0]: https://github.com/deltachat/deltachat-node/compare/v1.50.0...v1.51.0 + +[1.50.0]: https://github.com/deltachat/deltachat-node/compare/v1.49.0...v1.50.0 + +[1.49.0]: https://github.com/deltachat/deltachat-node/compare/v1.47.0...v1.49.0 + +[1.47.0]: https://github.com/deltachat/deltachat-node/compare/v1.46.0...v1.47.0 + +[1.46.0]: https://github.com/deltachat/deltachat-node/compare/v1.45.0...v1.46.0 + +[1.45.0]: https://github.com/deltachat/deltachat-node/compare/v1.44.0...v1.45.0 + +[1.44.0]: https://github.com/deltachat/deltachat-node/compare/v1.42.1...v1.44.0 + +[1.43.0]: https://github.com/deltachat/deltachat-node/compare/v1.42.0...v1.43.0 + +[1.42.1]: https://github.com/deltachat/deltachat-node/compare/v1.42.0...v1.42.1 + +[1.42.0]: https://github.com/deltachat/deltachat-node/compare/v1.41.0...v1.42.0 + +[1.41.0]: https://github.com/deltachat/deltachat-node/compare/v1.40.0...v1.41.0 + +[1.40.0]: https://github.com/deltachat/deltachat-node/compare/v1.39.0...v1.40.0 + +[1.39.0]: https://github.com/deltachat/deltachat-node/compare/v1.35.0...v1.39.0 + +[1.35.0]: https://github.com/deltachat/deltachat-node/compare/v1.34.0...v1.35.0 + +[1.34.0]: https://github.com/deltachat/deltachat-node/compare/v1.33.0...v1.34.0 + +[1.33.0]: https://github.com/deltachat/deltachat-node/compare/v1.32.1...v1.33.0 + +[1.32.1]: https://github.com/deltachat/deltachat-node/compare/v1.32.0...v1.32.1 + +[1.32.0]: https://github.com/deltachat/deltachat-node/compare/v1.29.2...v1.32.0 + +[1.29.2]: https://github.com/deltachat/deltachat-node/compare/v1.29.1...v1.29.2 + +[1.29.1]: https://github.com/deltachat/deltachat-node/compare/v1.29.0...v1.29.1 + +[1.29.0]: https://github.com/deltachat/deltachat-node/compare/v1.28.0...v1.29.0 + +[1.28.0]: https://github.com/deltachat/deltachat-node/compare/v1.27.0...v1.28.0 + +[1.27.0]: https://github.com/deltachat/deltachat-node/compare/v1.26.0...v1.27.0 + +[1.26.0]: https://github.com/deltachat/deltachat-node/compare/v1.25.0...v1.26.0 + +[1.25.0]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.26...v1.25.0 + +[1.0.0-beta.26]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.25...v1.0.0-beta.26 + +[1.0.0-beta.25]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.23.1...v1.0.0-beta.25 + +[1.0.0-beta.23.1]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.23...v1.0.0-beta.23.1 + +[1.0.0-beta.23]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.22...v1.0.0-beta.23 + +[1.0.0-beta.22]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.21...v1.0.0-beta.22 + +[1.0.0-beta.21]: https://github.com/deltachat/deltachat-node/compare/1.0.0-beta.18...v1.0.0-beta.21 + +[1.0.0-beta.20]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-beta.18...1.0.0-beta.20 + +[1.0.0-beta.18]: https://github.com/deltachat/deltachat-node/compare/1.0.0-beta.15...1.0.0-beta.18 + +[1.0.0-beta.15]: https://github.com/deltachat/deltachat-node/compare/1.0.0-beta.11...1.0.0-beta.15 + +[1.0.0-beta.11]: https://github.com/deltachat/deltachat-node/compare/1.0.0-beta.10...1.0.0-beta.11 + +[1.0.0-beta.10]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.12...1.0.0-beta.10 + +[1.0.0-alpha.12]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.11...v1.0.0-alpha.12 + +[1.0.0-alpha.11]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.10...v1.0.0-alpha.11 + +[1.0.0-alpha.10]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.9...v1.0.0-alpha.10 + +[1.0.0-alpha.9]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.7...v1.0.0-alpha.9 + +[1.0.0-alpha.7]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.6...v1.0.0-alpha.7 + +[1.0.0-alpha.6]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.3...v1.0.0-alpha.6 + +[1.0.0-alpha.3]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.2...v1.0.0-alpha.3 + +[1.0.0-alpha.2]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.1...v1.0.0-alpha.2 + +[1.0.0-alpha.1]: https://github.com/deltachat/deltachat-node/compare/v1.0.0-alpha.0...v1.0.0-alpha.1 + +[1.0.0-alpha.0]: https://github.com/deltachat/deltachat-node/compare/v0.44.0...v1.0.0-alpha.0 + +[0.44.0]: https://github.com/deltachat/deltachat-node/compare/v0.43.0...v0.44.0 + +[0.43.0]: https://github.com/deltachat/deltachat-node/compare/v0.42.0...v0.43.0 + +[0.42.0]: https://github.com/deltachat/deltachat-node/compare/v0.40.2...v0.42.0 + +[0.40.2]: https://github.com/deltachat/deltachat-node/compare/v0.40.1...v0.40.2 + +[0.40.1]: https://github.com/deltachat/deltachat-node/compare/v0.40.0...v0.40.1 + +[0.40.0]: https://github.com/deltachat/deltachat-node/compare/v0.39.0...v0.40.0 + +[0.39.0]: https://github.com/deltachat/deltachat-node/compare/v0.38.0...v0.39.0 + +[0.38.0]: https://github.com/deltachat/deltachat-node/compare/v0.36.0...v0.38.0 + +[0.36.0]: https://github.com/deltachat/deltachat-node/compare/v0.35.0...v0.36.0 + +[0.35.0]: https://github.com/deltachat/deltachat-node/compare/v0.30.1...v0.35.0 + +[0.30.1]: https://github.com/deltachat/deltachat-node/compare/v0.30.0...v0.30.1 + +[0.30.0]: https://github.com/deltachat/deltachat-node/compare/v0.29.0...v0.30.0 + +[0.29.0]: https://github.com/deltachat/deltachat-node/compare/v0.28.2...v0.29.0 + +[0.28.2]: https://github.com/deltachat/deltachat-node/compare/v0.28.1...v0.28.2 + +[0.28.1]: https://github.com/deltachat/deltachat-node/compare/v0.28.0...v0.28.1 + +[0.28.0]: https://github.com/deltachat/deltachat-node/compare/v0.27.0...v0.28.0 + +[0.27.0]: https://github.com/deltachat/deltachat-node/compare/v0.26.1...v0.27.0 + +[0.26.1]: https://github.com/deltachat/deltachat-node/compare/v0.26.0...v0.26.1 + +[0.26.0]: https://github.com/deltachat/deltachat-node/compare/v0.25.0...v0.26.0 + +[0.25.0]: https://github.com/deltachat/deltachat-node/compare/v0.24.0...v0.25.0 + +[0.24.0]: https://github.com/deltachat/deltachat-node/compare/v0.23.1...v0.24.0 + +[0.23.1]: https://github.com/deltachat/deltachat-node/compare/v0.23.0...v0.23.1 + +[0.23.0]: https://github.com/deltachat/deltachat-node/compare/v0.22.1...v0.23.0 + +[0.22.1]: https://github.com/deltachat/deltachat-node/compare/v0.22.0...v0.22.1 + +[0.22.0]: https://github.com/deltachat/deltachat-node/compare/v0.21.0...v0.22.0 + +[0.21.0]: https://github.com/deltachat/deltachat-node/compare/v0.20.0...v0.21.0 + +[0.20.0]: https://github.com/deltachat/deltachat-node/compare/v0.19.2...v0.20.0 + +[0.19.2]: https://github.com/deltachat/deltachat-node/compare/v0.19.1...v0.19.2 + +[0.19.1]: https://github.com/deltachat/deltachat-node/compare/v0.19.0...v0.19.1 + +[0.19.0]: https://github.com/deltachat/deltachat-node/compare/v0.18.2...v0.19.0 + +[0.18.2]: https://github.com/deltachat/deltachat-node/compare/v0.18.1...v0.18.2 + +[0.18.1]: https://github.com/deltachat/deltachat-node/compare/v0.18.0...v0.18.1 + +[0.18.0]: https://github.com/deltachat/deltachat-node/compare/v0.17.1...v0.18.0 + +[0.17.1]: https://github.com/deltachat/deltachat-node/compare/v0.17.0...v0.17.1 + +[0.17.0]: https://github.com/deltachat/deltachat-node/compare/v0.16.0...v0.17.0 + +[0.16.0]: https://github.com/deltachat/deltachat-node/compare/v0.15.0...v0.16.0 + +[0.15.0]: https://github.com/deltachat/deltachat-node/compare/v0.14.0...v0.15.0 + +[0.14.0]: https://github.com/deltachat/deltachat-node/compare/v0.13.1...v0.14.0 + +[0.13.1]: https://github.com/deltachat/deltachat-node/compare/v0.13.0...v0.13.1 + +[0.13.0]: https://github.com/deltachat/deltachat-node/compare/v0.12.0...v0.13.0 + +[0.12.0]: https://github.com/deltachat/deltachat-node/compare/v0.11.0...v0.12.0 + +[0.11.0]: https://github.com/deltachat/deltachat-node/compare/v0.10.0...v0.11.0 + +[0.10.0]: https://github.com/deltachat/deltachat-node/compare/v0.9.4...v0.10.0 + +[0.9.4]: https://github.com/deltachat/deltachat-node/compare/v0.9.3...v0.9.4 + +[0.9.3]: https://github.com/deltachat/deltachat-node/compare/v0.9.2...v0.9.3 + +[0.9.2]: https://github.com/deltachat/deltachat-node/compare/v0.9.1...v0.9.2 + +[0.9.1]: https://github.com/deltachat/deltachat-node/compare/v0.9.0...v0.9.1 + +[0.9.0]: https://github.com/deltachat/deltachat-node/compare/v0.8.0...v0.9.0 + +[0.8.0]: https://github.com/deltachat/deltachat-node/compare/v0.7.0...v0.8.0 + +[0.7.0]: https://github.com/deltachat/deltachat-node/compare/v0.6.2...v0.7.0 + +[0.6.2]: https://github.com/deltachat/deltachat-node/compare/v0.6.1...v0.6.2 + +[0.6.1]: https://github.com/deltachat/deltachat-node/compare/v0.6.0...v0.6.1 + +[0.6.0]: https://github.com/deltachat/deltachat-node/compare/v0.5.1...v0.6.0 + +[0.5.1]: https://github.com/deltachat/deltachat-node/compare/v0.5.0...v0.5.1 + +[0.5.0]: https://github.com/deltachat/deltachat-node/compare/v0.4.1...v0.5.0 + +[0.4.1]: https://github.com/deltachat/deltachat-node/compare/v0.4.0...v0.4.1 + +[0.4.0]: https://github.com/deltachat/deltachat-node/compare/v0.3.0...v0.4.0 + +[0.3.0]: https://github.com/deltachat/deltachat-node/compare/v0.2.0...v0.3.0 + +[0.2.0]: https://github.com/deltachat/deltachat-node/compare/v0.1.1...v0.2.0 + +[0.1.1]: https://github.com/deltachat/deltachat-node/compare/v0.1.0...v0.1.1 + +[0.1.0]: https://github.com/deltachat/deltachat-node/compare/vAdded...v0.1.0 + +[added]: https://github.com/deltachat/deltachat-node/compare/vChanged...vAdded + +[changed]: https://github.com/deltachat/deltachat-node/compare/vDeprecated...vChanged + +[deprecated]: https://github.com/deltachat/deltachat-node/compare/vFixed...vDeprecated + +[fixed]: https://github.com/deltachat/deltachat-node/compare/vRemoved...vFixed + +[removed]: https://github.com/deltachat/deltachat-node/compare/vRemoved...vRemoved diff --git a/node/CONTRIBUTORS.md b/node/CONTRIBUTORS.md new file mode 100644 index 000000000..1c81d4add --- /dev/null +++ b/node/CONTRIBUTORS.md @@ -0,0 +1,21 @@ +# Contributors + +| Name | GitHub | +| :-------------------- | :----------------------------------------------- | +| **Lars-Magnus Skog** | | +| **jikstra** | | +| **Simon Laux** | [**@Simon-Laux**](https://github.com/Simon-Laux) | +| **Jikstra** | [**@Jikstra**](https://github.com/Jikstra) | +| **Nico de Haen** | | +| **B. Petersen** | | +| **Karissa McKelvey** | [**@karissa**](https://github.com/karissa) | +| **developer** | | +| **Alexander Krotov** | | +| **Floris Bruynooghe** | | +| **lefherz** | | +| **Pablo** | [**@pabzm**](https://github.com/pabzm) | +| **pabzm** | | +| **holger krekel** | | +| **Robert Schütz** | | +| **bb** | | +| **Charles Paul** | | diff --git a/node/LICENSE b/node/LICENSE new file mode 100644 index 000000000..94a9ed024 --- /dev/null +++ b/node/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/node/README.md b/node/README.md new file mode 100644 index 000000000..f423732e7 --- /dev/null +++ b/node/README.md @@ -0,0 +1,220 @@ +# deltachat-node + +> node.js bindings for [`deltachat-core-rust`][deltachat-core-rust] + +[![npm](https://img.shields.io/npm/v/deltachat-node.svg)](https://www.npmjs.com/package/deltachat-node) +![Node version](https://img.shields.io/node/v/deltachat-node.svg) +[![Coverage Status](https://coveralls.io/repos/github/deltachat/deltachat-node/badge.svg)](https://coveralls.io/github/deltachat/deltachat-node) +[![dependencies](https://david-dm.org/deltachat/deltachat-node.svg)](https://david-dm.org/deltachat/deltachat-node) +[![JavaScript Style Guide](https://img.shields.io/badge/code_style-prettier-brightgreen.svg)](https://prettier.io) + +**If you are upgrading:** please see [`UPGRADING.md`](UPGRADING.md). + +`deltachat-node` primarily aims to offer two things: + +- A high level JavaScript api with syntactic sugar +- A low level c binding api around [`deltachat-core-rust`][deltachat-core-rust] + +## Table of Contents + +
Click to expand + +- [Install](#install) +- [Dependencies](#dependencies) +- [Build from source](#build-from-source) +- [Usage](#usage) +- [Developing](#developing) +- [License](#license) + +
+ +## Install + +By default the installation will build try to use the bundled prebuilds in the +npm package. If this fails it falls back to compile the bundled +`deltachat-core-rust` from the submodule using `scripts/rebuild-core.js`. +To install from npm use: + +``` +npm install deltchat-node +``` + +## Dependencies + +- Nodejs >= `v16.0.0` +- rustup (optional if you can't use the prebuilds) + +> On Windows, you may need to also install **Perl** to be able to compile deltachat-core. + +## Build from source + +If you want to build from source, make sure that you have `rustup` installed. +You can either use `npm install deltachat-node --build-from-source` to force +building from source or clone this repository and follow this steps: + +1. `git clone https://github.com/deltachat/deltachat-node.git` +2. `cd deltachat-node` +3. `npm i` + +### Workaround to build for x86_64 on Apple's M1 + +deltachat doesn't support universal (fat) binaries (that contain builds for both cpu architectures) yet, until it does you can use the following workaround to get x86_64 builds: + +``` +$ fnm install 17 --arch x64 +$ fnm use 17 +$ node -p process.arch +# result should be x64 +$ cd deltachat-core-rust && rustup target add x86_64-apple-darwin && cd - +$ git apply patches/m1_build_use_x86_64.patch +$ CARGO_BUILD_TARGET=x86_64-apple-darwin npm run build +$ npm run test +``` + +(when using [fnm](https://github.com/Schniz/fnm) instead of nvm, you can select the architecture) +If your node and electron are already build for arm64 you can also try bulding for arm: + +``` +$ fnm install 16 --arch arm64 +$ fnm use 16 +$ node -p process.arch +# result should be arm64 +$ npm_config_arch=arm64 npm run build +$ npm run test +``` + +## Usage + +```js +const { Context } = require('deltachat-node') + +const opts = { + addr: '[email]', + mail_pw: '[password]', +} + +const contact = '[email]' + +async function main() { + const dc = Context.open('./') + dc.on('ALL', console.log.bind(null, 'core |')) + + try { + await dc.configure(opts) + } catch (err) { + console.error('Failed to configure because of: ', err) + dc.unref() + return + } + + dc.startIO() + console.log('fully configured') + + const contactId = dc.createContact('Test', contact) + const chatId = dc.createChatByContactId(contactId) + dc.sendMessage(chatId, 'Hi!') + + console.log('sent message') + + dc.once('DC_EVENT_SMTP_MESSAGE_SENT', async () => { + console.log('Message sent, shutting down...') + dc.stopIO() + console.log('stopped io') + dc.unref() + }) +} + +main() +``` +this example can also be found in the examples folder [examples/send_message.js](./examples/send_message.js) + +### Generating Docs + +We are curently migrating to automaticaly generated documentation. +You can find the old documentation at [old_docs](./old_docs). + +to generate the documentation, run: + +``` +npx typedoc +``` + +The resulting documentation can be found in the `docs/` folder. +An online version can be found under [js.delta.chat](https://js.delta.chat). + +## Developing + +### Tests and Coverage + +Running `npm test` ends with showing a code coverage report, which is produced by [`nyc`](https://github.com/istanbuljs/nyc#readme). + +![test output](images/tests.png) + +The coverage report from `nyc` in the console is rather limited. To get a more detailed coverage report you can run `npm run coverage-html-report`. This will produce a html report from the `nyc` data and display it in a browser on your local machine. + +To run the integration tests you need to set the `DCC_NEW_TMP_EMAIL` environment variables. E.g.: + +``` +$ export DCC_NEW_TMP_EMAIL=https://testrun.org/new_email?t=[token] +$ npm run test +``` + +### Scripts + +We have the following scripts for building, testing and coverage: + +- `npm run coverage` Creates a coverage report and passes it to `coveralls`. Only done by `Travis`. +- `npm run coverage-html-report` Generates a html report from the coverage data and opens it in a browser on the local machine. +- `npm run generate-constants` Generates `constants.js` and `events.js` based on the `deltachat-core-rust/deltachat-ffi/deltachat.h` header file. +- `npm install` After dependencies are installed, runs `node-gyp-build` to see if the native code needs to be rebuilt. +- `npm run build` Rebuilds all code. +- `npm run build:core` Rebuilds code in `deltachat-core-rust`. +- `npm run build:bindings` Rebuilds the bindings and links with `deltachat-core-rust`. +- `ǹpm run clean` Removes all built code +- `npm run prebuildify` Builds prebuilt binary to `prebuilds/$PLATFORM-$ARCH`. Copies `deltachat.dll` from `deltachat-core-rust` for windows. +- `npm run download-prebuilds` Downloads all prebuilt binaries from github before `npm publish`. +- `npm run submodule` Updates the `deltachat-core-rust` submodule. +- `npm test` Runs `standard` and then the tests in `test/index.js`. +- `npm run test-integration` Runs the integration tests. +- `npm run hallmark` Runs `hallmark` on all markdown files. + +### Releases + +The following steps are needed to make a release: + +1. Update `CHANGELOG.md` (and run `npm run hallmark` to adjust markdown) + +- Add release changelog in top section +- Also adjust links to github prepare links at the end of the file + +2. Bump version number in package.json +3. Commit the changed files, commit message should be similiar to `Prepare for v1.0.0-foo.number` +4. Tag the release with `git tag -a v1.0.0-foo.number` +5. Push to github with `git push origin master --tags` +6. Wait until `Make Package` github action is completed +7. Download `deltachat-node.tgz` from the github release and run `npm publish deltachat-node.tgz` to publish it to npm. You probably need write rights to npm. + +## License + +Licensed under `GPL-3.0-or-later`, see [LICENSE](./LICENSE) file for details. + +> Copyright © 2018 `DeltaChat` contributors. +> +> This program is free software: you can redistribute it and/or modify +> it under the terms of the GNU General Public License as published by +> the Free Software Foundation, either version 3 of the License, or +> (at your option) any later version. +> +> This program is distributed in the hope that it will be useful, +> but WITHOUT ANY WARRANTY; without even the implied warranty of +> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +> GNU General Public License for more details. +> +> You should have received a copy of the GNU General Public License +> along with this program. If not, see . + +[deltachat-core-rust]: https://github.com/deltachat/deltachat-core-rust + +[appveyor-shield]: https://ci.appveyor.com/api/projects/status/t0narp672wpbl6pd?svg=true + +[appveyor]: https://ci.appveyor.com/project/ralphtheninja/deltachat-node-d4bf8 diff --git a/node/binding.gyp b/node/binding.gyp new file mode 100644 index 000000000..b0d92eae9 --- /dev/null +++ b/node/binding.gyp @@ -0,0 +1,78 @@ +{ + # documentation about the format of this file can be found under https://gyp.gsrc.io/docs/InputFormatReference.md + # Variables can be specified when calling node-gyp as so: + # node-gyp configure -- -Dvarname=value + "variables": { + # Whether to use a system-wide installation of deltachat-core + # using pkg-config. Set to either "true" or "false". + "USE_SYSTEM_LIBDELTACHAT%": " { + console.log('Message sent, shutting down...') + dc.stopIO() + console.log('stopped io') + dc.unref() + }) +} + +main() diff --git a/node/images/tests.png b/node/images/tests.png new file mode 100644 index 000000000..829295b38 Binary files /dev/null and b/node/images/tests.png differ diff --git a/node/lib/binding.ts b/node/lib/binding.ts new file mode 100644 index 000000000..72875aa4f --- /dev/null +++ b/node/lib/binding.ts @@ -0,0 +1,9 @@ +import { join } from 'path' + +/** + * bindings are not typed yet. + * if the availible function names are required they can be found inside of `../src/module.c` + */ +export const bindings: any = require('node-gyp-build')(join(__dirname, '../')) + +export default bindings diff --git a/node/lib/chat.ts b/node/lib/chat.ts new file mode 100644 index 000000000..4a8ba6807 --- /dev/null +++ b/node/lib/chat.ts @@ -0,0 +1,103 @@ +/* eslint-disable camelcase */ + +import binding from './binding' +import rawDebug from 'debug' +const debug = rawDebug('deltachat:node:chat') +import { C } from './constants' +import { integerToHexColor } from './util' +import { ChatJSON } from './types' + +interface NativeChat {} +/** + * Wrapper around dc_chat_t* + */ + +export class Chat { + constructor(public dc_chat: NativeChat) { + debug('Chat constructor') + } + + getVisibility(): + | C.DC_CHAT_VISIBILITY_NORMAL + | C.DC_CHAT_VISIBILITY_ARCHIVED + | C.DC_CHAT_VISIBILITY_PINNED { + return binding.dcn_chat_get_visibility(this.dc_chat) + } + + get color(): string { + return integerToHexColor(binding.dcn_chat_get_color(this.dc_chat)) + } + + getId(): number { + return binding.dcn_chat_get_id(this.dc_chat) + } + + getName(): string { + return binding.dcn_chat_get_name(this.dc_chat) + } + + getProfileImage(): string { + return binding.dcn_chat_get_profile_image(this.dc_chat) + } + + getType(): number { + return binding.dcn_chat_get_type(this.dc_chat) + } + + isSelfTalk(): boolean { + return Boolean(binding.dcn_chat_is_self_talk(this.dc_chat)) + } + + isContactRequest(): boolean { + return Boolean(binding.dcn_chat_is_contact_request(this.dc_chat)) + } + + isUnpromoted(): boolean { + return Boolean(binding.dcn_chat_is_unpromoted(this.dc_chat)) + } + + isProtected(): boolean { + return Boolean(binding.dcn_chat_is_protected(this.dc_chat)) + } + + get canSend(): boolean { + return Boolean(binding.dcn_chat_can_send(this.dc_chat)) + } + + isDeviceTalk(): boolean { + return Boolean(binding.dcn_chat_is_device_talk(this.dc_chat)) + } + + isSingle(): boolean { + return this.getType() === C.DC_CHAT_TYPE_SINGLE + } + + isGroup(): boolean { + return this.getType() === C.DC_CHAT_TYPE_GROUP + } + + isMuted(): boolean { + return Boolean(binding.dcn_chat_is_muted(this.dc_chat)) + } + + toJson(): ChatJSON { + debug('toJson') + const visibility = this.getVisibility() + return { + archived: visibility === C.DC_CHAT_VISIBILITY_ARCHIVED, + pinned: visibility === C.DC_CHAT_VISIBILITY_PINNED, + color: this.color, + id: this.getId(), + name: this.getName(), + profileImage: this.getProfileImage(), + type: this.getType(), + isSelfTalk: this.isSelfTalk(), + isUnpromoted: this.isUnpromoted(), + isProtected: this.isProtected(), + canSend: this.canSend, + isDeviceTalk: this.isDeviceTalk(), + isContactRequest: this.isContactRequest(), + muted: this.isMuted(), + } + } +} diff --git a/node/lib/chatlist.ts b/node/lib/chatlist.ts new file mode 100644 index 000000000..92d022fc2 --- /dev/null +++ b/node/lib/chatlist.ts @@ -0,0 +1,39 @@ +/* eslint-disable camelcase */ + +import binding from './binding' +import { Lot } from './lot' +import { Chat } from './chat' +const debug = require('debug')('deltachat:node:chatlist') + +interface NativeChatList {} +/** + * Wrapper around dc_chatlist_t* + */ +export class ChatList { + constructor(private dc_chatlist: NativeChatList) { + debug('ChatList constructor') + } + + getChatId(index: number): number { + debug(`getChatId ${index}`) + return binding.dcn_chatlist_get_chat_id(this.dc_chatlist, index) + } + + getCount(): number { + debug('getCount') + return binding.dcn_chatlist_get_cnt(this.dc_chatlist) + } + + getMessageId(index: number): number { + debug(`getMessageId ${index}`) + return binding.dcn_chatlist_get_msg_id(this.dc_chatlist, index) + } + + getSummary(index: number, chat?: Chat): Lot { + debug(`getSummary ${index}`) + const dc_chat = (chat && chat.dc_chat) || null + return new Lot( + binding.dcn_chatlist_get_summary(this.dc_chatlist, index, dc_chat) + ) + } +} diff --git a/node/lib/constants.ts b/node/lib/constants.ts new file mode 100644 index 000000000..61bbbe28e --- /dev/null +++ b/node/lib/constants.ts @@ -0,0 +1,260 @@ +// Generated! + +export enum C { + DC_CERTCK_ACCEPT_INVALID_CERTIFICATES = 3, + DC_CERTCK_AUTO = 0, + DC_CERTCK_STRICT = 1, + DC_CHAT_ID_ALLDONE_HINT = 7, + DC_CHAT_ID_ARCHIVED_LINK = 6, + DC_CHAT_ID_LAST_SPECIAL = 9, + DC_CHAT_ID_TRASH = 3, + DC_CHAT_TYPE_BROADCAST = 160, + DC_CHAT_TYPE_GROUP = 120, + DC_CHAT_TYPE_MAILINGLIST = 140, + DC_CHAT_TYPE_SINGLE = 100, + DC_CHAT_TYPE_UNDEFINED = 0, + DC_CHAT_VISIBILITY_ARCHIVED = 1, + DC_CHAT_VISIBILITY_NORMAL = 0, + DC_CHAT_VISIBILITY_PINNED = 2, + DC_CONNECTIVITY_CONNECTED = 4000, + DC_CONNECTIVITY_CONNECTING = 2000, + DC_CONNECTIVITY_NOT_CONNECTED = 1000, + DC_CONNECTIVITY_WORKING = 3000, + DC_CONTACT_ID_DEVICE = 5, + DC_CONTACT_ID_INFO = 2, + DC_CONTACT_ID_LAST_SPECIAL = 9, + DC_CONTACT_ID_SELF = 1, + DC_DOWNLOAD_AVAILABLE = 10, + DC_DOWNLOAD_DONE = 0, + DC_DOWNLOAD_FAILURE = 20, + DC_DOWNLOAD_IN_PROGRESS = 1000, + DC_EVENT_CHAT_EPHEMERAL_TIMER_MODIFIED = 2021, + DC_EVENT_CHAT_MODIFIED = 2020, + DC_EVENT_CONFIGURE_PROGRESS = 2041, + DC_EVENT_CONNECTIVITY_CHANGED = 2100, + DC_EVENT_CONTACTS_CHANGED = 2030, + DC_EVENT_DELETED_BLOB_FILE = 151, + DC_EVENT_ERROR = 400, + DC_EVENT_ERROR_SELF_NOT_IN_GROUP = 410, + DC_EVENT_IMAP_CONNECTED = 102, + DC_EVENT_IMAP_MESSAGE_DELETED = 104, + DC_EVENT_IMAP_MESSAGE_MOVED = 105, + DC_EVENT_IMEX_FILE_WRITTEN = 2052, + DC_EVENT_IMEX_PROGRESS = 2051, + DC_EVENT_INCOMING_MSG = 2005, + DC_EVENT_INFO = 100, + DC_EVENT_LOCATION_CHANGED = 2035, + DC_EVENT_MSGS_CHANGED = 2000, + DC_EVENT_MSGS_NOTICED = 2008, + DC_EVENT_MSG_DELIVERED = 2010, + DC_EVENT_MSG_FAILED = 2012, + DC_EVENT_MSG_READ = 2015, + DC_EVENT_NEW_BLOB_FILE = 150, + DC_EVENT_SECUREJOIN_INVITER_PROGRESS = 2060, + DC_EVENT_SECUREJOIN_JOINER_PROGRESS = 2061, + DC_EVENT_SELFAVATAR_CHANGED = 2110, + DC_EVENT_SMTP_CONNECTED = 101, + DC_EVENT_SMTP_MESSAGE_SENT = 103, + DC_EVENT_WARNING = 300, + DC_EVENT_WEBXDC_STATUS_UPDATE = 2120, + DC_GCL_ADD_ALLDONE_HINT = 4, + DC_GCL_ADD_SELF = 2, + DC_GCL_ARCHIVED_ONLY = 1, + DC_GCL_FOR_FORWARDING = 8, + DC_GCL_NO_SPECIALS = 2, + DC_GCL_VERIFIED_ONLY = 1, + DC_GCM_ADDDAYMARKER = 1, + DC_GCM_INFO_ONLY = 2, + DC_IMEX_EXPORT_BACKUP = 11, + DC_IMEX_EXPORT_SELF_KEYS = 1, + DC_IMEX_IMPORT_BACKUP = 12, + DC_IMEX_IMPORT_SELF_KEYS = 2, + DC_INFO_PROTECTION_DISABLED = 12, + DC_INFO_PROTECTION_ENABLED = 11, + DC_KEY_GEN_DEFAULT = 0, + DC_KEY_GEN_ED25519 = 2, + DC_KEY_GEN_RSA2048 = 1, + DC_LP_AUTH_NORMAL = 4, + DC_LP_AUTH_OAUTH2 = 2, + DC_MEDIA_QUALITY_BALANCED = 0, + DC_MEDIA_QUALITY_WORSE = 1, + DC_MSG_AUDIO = 40, + DC_MSG_FILE = 60, + DC_MSG_GIF = 21, + DC_MSG_ID_DAYMARKER = 9, + DC_MSG_ID_LAST_SPECIAL = 9, + DC_MSG_ID_MARKER1 = 1, + DC_MSG_IMAGE = 20, + DC_MSG_STICKER = 23, + DC_MSG_TEXT = 10, + DC_MSG_VIDEO = 50, + DC_MSG_VIDEOCHAT_INVITATION = 70, + DC_MSG_VOICE = 41, + DC_MSG_WEBXDC = 80, + DC_PROVIDER_STATUS_BROKEN = 3, + DC_PROVIDER_STATUS_OK = 1, + DC_PROVIDER_STATUS_PREPARATION = 2, + DC_QR_ACCOUNT = 250, + DC_QR_ADDR = 320, + DC_QR_ASK_VERIFYCONTACT = 200, + DC_QR_ASK_VERIFYGROUP = 202, + DC_QR_ERROR = 400, + DC_QR_FPR_MISMATCH = 220, + DC_QR_FPR_OK = 210, + DC_QR_FPR_WITHOUT_ADDR = 230, + DC_QR_REVIVE_VERIFYCONTACT = 510, + DC_QR_REVIVE_VERIFYGROUP = 512, + DC_QR_TEXT = 330, + DC_QR_URL = 332, + DC_QR_WEBRTC_INSTANCE = 260, + DC_QR_WITHDRAW_VERIFYCONTACT = 500, + DC_QR_WITHDRAW_VERIFYGROUP = 502, + DC_SHOW_EMAILS_ACCEPTED_CONTACTS = 1, + DC_SHOW_EMAILS_ALL = 2, + DC_SHOW_EMAILS_OFF = 0, + DC_SOCKET_AUTO = 0, + DC_SOCKET_PLAIN = 3, + DC_SOCKET_SSL = 1, + DC_SOCKET_STARTTLS = 2, + DC_STATE_IN_FRESH = 10, + DC_STATE_IN_NOTICED = 13, + DC_STATE_IN_SEEN = 16, + DC_STATE_OUT_DELIVERED = 26, + DC_STATE_OUT_DRAFT = 19, + DC_STATE_OUT_FAILED = 24, + DC_STATE_OUT_MDN_RCVD = 28, + DC_STATE_OUT_PENDING = 20, + DC_STATE_OUT_PREPARING = 18, + DC_STATE_UNDEFINED = 0, + DC_STR_AC_SETUP_MSG_BODY = 43, + DC_STR_AC_SETUP_MSG_SUBJECT = 42, + DC_STR_ARCHIVEDCHATS = 40, + DC_STR_AUDIO = 11, + DC_STR_BAD_TIME_MSG_BODY = 85, + DC_STR_BROADCAST_LIST = 115, + DC_STR_CANNOT_LOGIN = 60, + DC_STR_CANTDECRYPT_MSG_BODY = 29, + DC_STR_CONFIGURATION_FAILED = 84, + DC_STR_CONNECTED = 107, + DC_STR_CONNTECTING = 108, + DC_STR_CONTACT_NOT_VERIFIED = 36, + DC_STR_CONTACT_SETUP_CHANGED = 37, + DC_STR_CONTACT_VERIFIED = 35, + DC_STR_DEVICE_MESSAGES = 68, + DC_STR_DEVICE_MESSAGES_HINT = 70, + DC_STR_DOWNLOAD_AVAILABILITY = 100, + DC_STR_DRAFT = 3, + DC_STR_E2E_AVAILABLE = 25, + DC_STR_E2E_PREFERRED = 34, + DC_STR_ENCRYPTEDMSG = 24, + DC_STR_ENCR_NONE = 28, + DC_STR_ENCR_TRANSP = 27, + DC_STR_EPHEMERAL_DAY = 79, + DC_STR_EPHEMERAL_DAYS = 95, + DC_STR_EPHEMERAL_DISABLED = 75, + DC_STR_EPHEMERAL_FOUR_WEEKS = 81, + DC_STR_EPHEMERAL_HOUR = 78, + DC_STR_EPHEMERAL_HOURS = 94, + DC_STR_EPHEMERAL_MINUTE = 77, + DC_STR_EPHEMERAL_MINUTES = 93, + DC_STR_EPHEMERAL_SECONDS = 76, + DC_STR_EPHEMERAL_WEEK = 80, + DC_STR_EPHEMERAL_WEEKS = 96, + DC_STR_ERROR = 112, + DC_STR_ERROR_NO_NETWORK = 87, + DC_STR_FAILED_SENDING_TO = 74, + DC_STR_FILE = 12, + DC_STR_FINGERPRINTS = 30, + DC_STR_FORWARDED = 97, + DC_STR_GIF = 23, + DC_STR_IMAGE = 9, + DC_STR_INCOMING_MESSAGES = 103, + DC_STR_LAST_MSG_SENT_SUCCESSFULLY = 111, + DC_STR_LOCATION = 66, + DC_STR_MESSAGES = 114, + DC_STR_MSGACTIONBYME = 63, + DC_STR_MSGACTIONBYUSER = 62, + DC_STR_MSGADDMEMBER = 17, + DC_STR_MSGDELMEMBER = 18, + DC_STR_MSGGROUPLEFT = 19, + DC_STR_MSGGRPIMGCHANGED = 16, + DC_STR_MSGGRPIMGDELETED = 33, + DC_STR_MSGGRPNAME = 15, + DC_STR_MSGLOCATIONDISABLED = 65, + DC_STR_MSGLOCATIONENABLED = 64, + DC_STR_NOMESSAGES = 1, + DC_STR_NOT_CONNECTED = 121, + DC_STR_NOT_SUPPORTED_BY_PROVIDER = 113, + DC_STR_ONE_MOMENT = 106, + DC_STR_OUTGOING_MESSAGES = 104, + DC_STR_PARTIAL_DOWNLOAD_MSG_BODY = 99, + DC_STR_PART_OF_TOTAL_USED = 116, + DC_STR_PROTECTION_DISABLED = 89, + DC_STR_PROTECTION_ENABLED = 88, + DC_STR_QUOTA_EXCEEDING_MSG_BODY = 98, + DC_STR_READRCPT = 31, + DC_STR_READRCPT_MAILBODY = 32, + DC_STR_REPLY_NOUN = 90, + DC_STR_SAVED_MESSAGES = 69, + DC_STR_SECURE_JOIN_GROUP_QR_DESC = 120, + DC_STR_SECURE_JOIN_REPLIES = 118, + DC_STR_SECURE_JOIN_STARTED = 117, + DC_STR_SELF = 2, + DC_STR_SELF_DELETED_MSG_BODY = 91, + DC_STR_SENDING = 110, + DC_STR_SERVER_TURNED_OFF = 92, + DC_STR_SETUP_CONTACT_QR_DESC = 119, + DC_STR_STICKER = 67, + DC_STR_STORAGE_ON_DOMAIN = 105, + DC_STR_SUBJECT_FOR_NEW_CONTACT = 73, + DC_STR_SYNC_MSG_BODY = 102, + DC_STR_SYNC_MSG_SUBJECT = 101, + DC_STR_UNKNOWN_SENDER_FOR_CHAT = 72, + DC_STR_UPDATE_REMINDER_MSG_BODY = 86, + DC_STR_UPDATING = 109, + DC_STR_VIDEO = 10, + DC_STR_VIDEOCHAT_INVITATION = 82, + DC_STR_VIDEOCHAT_INVITE_MSG_BODY = 83, + DC_STR_VOICEMESSAGE = 7, + DC_STR_WELCOME_MESSAGE = 71, + DC_TEXT1_DRAFT = 1, + DC_TEXT1_SELF = 3, + DC_TEXT1_USERNAME = 2, + DC_VIDEOCHATTYPE_BASICWEBRTC = 1, + DC_VIDEOCHATTYPE_JITSI = 2, + DC_VIDEOCHATTYPE_UNKNOWN = 0, +} + +// Generated! + +export const EventId2EventName: { [key: number]: string } = { + 100: 'DC_EVENT_INFO', + 101: 'DC_EVENT_SMTP_CONNECTED', + 102: 'DC_EVENT_IMAP_CONNECTED', + 103: 'DC_EVENT_SMTP_MESSAGE_SENT', + 104: 'DC_EVENT_IMAP_MESSAGE_DELETED', + 105: 'DC_EVENT_IMAP_MESSAGE_MOVED', + 150: 'DC_EVENT_NEW_BLOB_FILE', + 151: 'DC_EVENT_DELETED_BLOB_FILE', + 300: 'DC_EVENT_WARNING', + 400: 'DC_EVENT_ERROR', + 410: 'DC_EVENT_ERROR_SELF_NOT_IN_GROUP', + 2000: 'DC_EVENT_MSGS_CHANGED', + 2005: 'DC_EVENT_INCOMING_MSG', + 2008: 'DC_EVENT_MSGS_NOTICED', + 2010: 'DC_EVENT_MSG_DELIVERED', + 2012: 'DC_EVENT_MSG_FAILED', + 2015: 'DC_EVENT_MSG_READ', + 2020: 'DC_EVENT_CHAT_MODIFIED', + 2021: 'DC_EVENT_CHAT_EPHEMERAL_TIMER_MODIFIED', + 2030: 'DC_EVENT_CONTACTS_CHANGED', + 2035: 'DC_EVENT_LOCATION_CHANGED', + 2041: 'DC_EVENT_CONFIGURE_PROGRESS', + 2051: 'DC_EVENT_IMEX_PROGRESS', + 2052: 'DC_EVENT_IMEX_FILE_WRITTEN', + 2060: 'DC_EVENT_SECUREJOIN_INVITER_PROGRESS', + 2061: 'DC_EVENT_SECUREJOIN_JOINER_PROGRESS', + 2100: 'DC_EVENT_CONNECTIVITY_CHANGED', + 2110: 'DC_EVENT_SELFAVATAR_CHANGED', + 2120: 'DC_EVENT_WEBXDC_STATUS_UPDATE', +} diff --git a/node/lib/contact.ts b/node/lib/contact.ts new file mode 100644 index 000000000..be4e2b8c9 --- /dev/null +++ b/node/lib/contact.ts @@ -0,0 +1,91 @@ +import { integerToHexColor } from './util' + +/* eslint-disable camelcase */ + +import binding from './binding' +const debug = require('debug')('deltachat:node:contact') + +interface NativeContact {} +/** + * Wrapper around dc_contact_t* + */ +export class Contact { + constructor(public dc_contact: NativeContact) { + debug('Contact constructor') + } + + toJson() { + debug('toJson') + return { + address: this.getAddress(), + color: this.color, + authName: this.authName, + status: this.status, + displayName: this.getDisplayName(), + id: this.getId(), + lastSeen: this.lastSeen, + name: this.getName(), + profileImage: this.getProfileImage(), + nameAndAddr: this.getNameAndAddress(), + isBlocked: this.isBlocked(), + isVerified: this.isVerified(), + } + } + + getAddress(): string { + return binding.dcn_contact_get_addr(this.dc_contact) + } + + /** Get original contact name. + * This is the name of the contact as defined by the contact themself. + * If the contact themself does not define such a name, + * an empty string is returned. */ + get authName(): string { + return binding.dcn_contact_get_auth_name(this.dc_contact) + } + + get color(): string { + return integerToHexColor(binding.dcn_contact_get_color(this.dc_contact)) + } + + /** + * contact's status + * + * Status is the last signature received in a message from this contact. + */ + get status(): string { + return binding.dcn_contact_get_status(this.dc_contact) + } + + getDisplayName(): string { + return binding.dcn_contact_get_display_name(this.dc_contact) + } + + getId(): number { + return binding.dcn_contact_get_id(this.dc_contact) + } + + get lastSeen(): number { + return binding.dcn_contact_get_last_seen(this.dc_contact) + } + + getName(): string { + return binding.dcn_contact_get_name(this.dc_contact) + } + + getNameAndAddress(): string { + return binding.dcn_contact_get_name_n_addr(this.dc_contact) + } + + getProfileImage(): string { + return binding.dcn_contact_get_profile_image(this.dc_contact) + } + + isBlocked() { + return Boolean(binding.dcn_contact_is_blocked(this.dc_contact)) + } + + isVerified() { + return Boolean(binding.dcn_contact_is_verified(this.dc_contact)) + } +} diff --git a/node/lib/context.ts b/node/lib/context.ts new file mode 100644 index 000000000..9a672b0a8 --- /dev/null +++ b/node/lib/context.ts @@ -0,0 +1,928 @@ +/* eslint-disable camelcase */ + +import binding from './binding' +import { C, EventId2EventName } from './constants' +import { Chat } from './chat' +import { ChatList } from './chatlist' +import { Contact } from './contact' +import { Message } from './message' +import { Lot } from './lot' +import { Locations } from './locations' +import rawDebug from 'debug' +import { AccountManager } from './deltachat' +import { join } from 'path' +import { EventEmitter } from 'stream' +const debug = rawDebug('deltachat:node:index') + +const noop = function () {} +interface NativeContext {} + +/** + * Wrapper around dcn_context_t* + * + * only acts as event emitter when created in standalone mode (without account manager) + * with `Context.open` + */ +export class Context extends EventEmitter { + constructor( + readonly manager: AccountManager | null, + private inner_dcn_context: NativeContext, + readonly account_id: number + ) { + super() + debug('DeltaChat constructor') + } + + /** Opens a stanalone context (without an account manager) + * automatically starts the event handler */ + static open(cwd: string): Context { + const dbFile = join(cwd, 'db.sqlite') + const context = new Context(null, binding.dcn_context_new(dbFile), 42) + debug('Opened context') + function handleCoreEvent( + eventId: number, + data1: number, + data2: number | string + ) { + const eventString = EventId2EventName[eventId] + debug(eventString, data1, data2) + if (!context.emit) { + console.log('Received an event but EventEmitter is already destroyed.') + console.log(eventString, 42, data1, data2) + return + } + context.emit(eventString, 42, data1, data2) + } + binding.dcn_start_event_handler( + context.dcn_context, + handleCoreEvent.bind(this) + ) + debug('Started event handler') + return context + } + + get dcn_context() { + return this.inner_dcn_context + } + + get is_open() { + return Boolean(binding.dcn_context_is_open()) + } + + open(passphrase?: string) { + return Boolean( + binding.dcn_context_open(this.dcn_context, passphrase ? passphrase : '') + ) + } + + unref() { + binding.dcn_context_unref(this.dcn_context) + ;(this.inner_dcn_context as any) = null + } + + acceptChat(chatId: number) { + binding.dcn_accept_chat(this.dcn_context, chatId) + } + + blockChat(chatId: number) { + binding.dcn_block_chat(this.dcn_context, chatId) + } + + addAddressBook(addressBook: string) { + debug(`addAddressBook ${addressBook}`) + return binding.dcn_add_address_book(this.dcn_context, addressBook) + } + + addContactToChat(chatId: number, contactId: number) { + debug(`addContactToChat ${chatId} ${contactId}`) + return Boolean( + binding.dcn_add_contact_to_chat( + this.dcn_context, + Number(chatId), + Number(contactId) + ) + ) + } + + addDeviceMessage(label: string, msg: Message | string) { + debug(`addDeviceMessage ${label} ${msg}`) + if (!msg) { + throw new Error('invalid msg parameter') + } + if (typeof label !== 'string') { + throw new Error('invalid label parameter, must be a string') + } + if (typeof msg === 'string') { + const msgObj = this.messageNew() + msgObj.setText(msg) + msg = msgObj + } + if (!msg.dc_msg) { + throw new Error('invalid msg object') + } + return binding.dcn_add_device_msg(this.dcn_context, label, msg.dc_msg) + } + + setChatVisibility( + chatId: number, + visibility: + | C.DC_CHAT_VISIBILITY_NORMAL + | C.DC_CHAT_VISIBILITY_ARCHIVED + | C.DC_CHAT_VISIBILITY_PINNED + ) { + debug(`setChatVisibility ${chatId} ${visibility}`) + binding.dcn_set_chat_visibility( + this.dcn_context, + Number(chatId), + visibility + ) + } + + blockContact(contactId: number, block: boolean) { + debug(`blockContact ${contactId} ${block}`) + binding.dcn_block_contact( + this.dcn_context, + Number(contactId), + block ? 1 : 0 + ) + } + + checkQrCode(qrCode: string) { + debug(`checkQrCode ${qrCode}`) + const dc_lot = binding.dcn_check_qr(this.dcn_context, qrCode) + let result = dc_lot ? new Lot(dc_lot) : null + if (result) { + return { id: result.getId(), ...result.toJson() } + } + return result + } + + configure(opts: any): Promise { + return new Promise((resolve, reject) => { + debug('configure') + + const onSuccess = () => { + removeListeners() + resolve() + } + const onFail = (error: string) => { + removeListeners() + reject(new Error(error)) + } + + const onConfigure = (accountId: number, data1: number, data2: string) => { + if (this.account_id !== accountId) { + return + } + if (data1 === 0) return onFail(data2) + else if (data1 === 1000) return onSuccess() + } + + const removeListeners = () => { + ;(this.manager || this).removeListener( + 'DC_EVENT_CONFIGURE_PROGRESS', + onConfigure + ) + } + + const registerListeners = () => { + ;(this.manager || this).on('DC_EVENT_CONFIGURE_PROGRESS', onConfigure) + } + + registerListeners() + + if (!opts) opts = {} + Object.keys(opts).forEach((key) => { + const value = opts[key] + this.setConfig(key, value) + }) + + binding.dcn_configure(this.dcn_context) + }) + } + + continueKeyTransfer(messageId: number, setupCode: string) { + debug(`continueKeyTransfer ${messageId}`) + return new Promise((resolve, reject) => { + binding.dcn_continue_key_transfer( + this.dcn_context, + Number(messageId), + setupCode, + (result: number) => resolve(result === 1) + ) + }) + } + /** @returns chatId */ + createBroadcastList(): number { + debug(`createBroadcastList`) + return binding.dcn_create_broadcast_list(this.dcn_context) + } + + /** @returns chatId */ + createChatByContactId(contactId: number): number { + debug(`createChatByContactId ${contactId}`) + return binding.dcn_create_chat_by_contact_id( + this.dcn_context, + Number(contactId) + ) + } + + /** @returns contactId */ + createContact(name: string, addr: string): number { + debug(`createContact ${name} ${addr}`) + return binding.dcn_create_contact(this.dcn_context, name, addr) + } + + /** + * + * @param chatName The name of the chat that should be created + * @param is_protected Whether the chat should be protected at creation time + * @returns chatId + */ + createGroupChat(chatName: string, is_protected: boolean = false): number { + debug(`createGroupChat ${chatName} [protected:${is_protected}]`) + return binding.dcn_create_group_chat( + this.dcn_context, + is_protected ? 1 : 0, + chatName + ) + } + + deleteChat(chatId: number) { + debug(`deleteChat ${chatId}`) + binding.dcn_delete_chat(this.dcn_context, Number(chatId)) + } + + deleteContact(contactId: number) { + debug(`deleteContact ${contactId}`) + return Boolean( + binding.dcn_delete_contact(this.dcn_context, Number(contactId)) + ) + } + + deleteMessages(messageIds: number[]) { + if (!Array.isArray(messageIds)) { + messageIds = [messageIds] + } + messageIds = messageIds.map((id) => Number(id)) + debug('deleteMessages', messageIds) + binding.dcn_delete_msgs(this.dcn_context, messageIds) + } + + forwardMessages(messageIds: number[], chatId: number) { + if (!Array.isArray(messageIds)) { + messageIds = [messageIds] + } + messageIds = messageIds.map((id) => Number(id)) + debug('forwardMessages', messageIds) + binding.dcn_forward_msgs(this.dcn_context, messageIds, chatId) + } + + getBlobdir(): string { + debug('getBlobdir') + return binding.dcn_get_blobdir(this.dcn_context) + } + + getBlockedCount(): number { + debug('getBlockedCount') + return binding.dcn_get_blocked_cnt(this.dcn_context) + } + + getBlockedContacts(): number[] { + debug('getBlockedContacts') + return binding.dcn_get_blocked_contacts(this.dcn_context) + } + + getChat(chatId: number) { + debug(`getChat ${chatId}`) + const dc_chat = binding.dcn_get_chat(this.dcn_context, Number(chatId)) + return dc_chat ? new Chat(dc_chat) : null + } + + getChatContacts(chatId: number): number[] { + debug(`getChatContacts ${chatId}`) + return binding.dcn_get_chat_contacts(this.dcn_context, Number(chatId)) + } + + getChatIdByContactId(contactId: number): number { + debug(`getChatIdByContactId ${contactId}`) + return binding.dcn_get_chat_id_by_contact_id( + this.dcn_context, + Number(contactId) + ) + } + + getChatMedia( + chatId: number, + msgType1: number, + msgType2: number, + msgType3: number + ): number[] { + debug(`getChatMedia ${chatId}`) + return binding.dcn_get_chat_media( + this.dcn_context, + Number(chatId), + msgType1, + msgType2 || 0, + msgType3 || 0 + ) + } + + getMimeHeaders(messageId: number): string { + debug(`getMimeHeaders ${messageId}`) + return binding.dcn_get_mime_headers(this.dcn_context, Number(messageId)) + } + + getChatlistItemSummary(chatId: number, messageId: number) { + debug(`getChatlistItemSummary ${chatId} ${messageId}`) + return new Lot( + binding.dcn_chatlist_get_summary2(this.dcn_context, chatId, messageId) + ) + } + + getChatMessages(chatId: number, flags: number, marker1before: number) { + debug(`getChatMessages ${chatId} ${flags} ${marker1before}`) + return binding.dcn_get_chat_msgs( + this.dcn_context, + Number(chatId), + flags, + marker1before + ) + } + + /** + * Get encryption info for a chat. + * Get a multi-line encryption info, containing encryption preferences of all members. + * Can be used to find out why messages sent to group are not encrypted. + * + * @param chatId ID of the chat to get the encryption info for. + * @return Multi-line text, must be released using dc_str_unref() after usage. + */ + getChatEncrytionInfo(chatId: number): string { + return binding.dcn_get_chat_encrinfo(this.dcn_context, chatId) + } + + getChats(listFlags: number, queryStr: string, queryContactId: number) { + debug('getChats') + const result = [] + const list = this.getChatList(listFlags, queryStr, queryContactId) + const count = list.getCount() + for (let i = 0; i < count; i++) { + result.push(list.getChatId(i)) + } + return result + } + + getChatList(listFlags: number, queryStr: string, queryContactId: number) { + listFlags = listFlags || 0 + queryStr = queryStr || '' + queryContactId = queryContactId || 0 + debug(`getChatList ${listFlags} ${queryStr} ${queryContactId}`) + return new ChatList( + binding.dcn_get_chatlist( + this.dcn_context, + listFlags, + queryStr, + Number(queryContactId) + ) + ) + } + + getConfig(key: string): string { + debug(`getConfig ${key}`) + return binding.dcn_get_config(this.dcn_context, key) + } + + getContact(contactId: number) { + debug(`getContact ${contactId}`) + const dc_contact = binding.dcn_get_contact( + this.dcn_context, + Number(contactId) + ) + return dc_contact ? new Contact(dc_contact) : null + } + + getContactEncryptionInfo(contactId: number) { + debug(`getContactEncryptionInfo ${contactId}`) + return binding.dcn_get_contact_encrinfo(this.dcn_context, Number(contactId)) + } + + getContacts(listFlags: number, query: string) { + listFlags = listFlags || 0 + query = query || '' + debug(`getContacts ${listFlags} ${query}`) + return binding.dcn_get_contacts(this.dcn_context, listFlags, query) + } + + wasDeviceMessageEverAdded(label: string) { + debug(`wasDeviceMessageEverAdded ${label}`) + const added = binding.dcn_was_device_msg_ever_added(this.dcn_context, label) + return added === 1 + } + + getDraft(chatId: number) { + debug(`getDraft ${chatId}`) + const dc_msg = binding.dcn_get_draft(this.dcn_context, Number(chatId)) + return dc_msg ? new Message(dc_msg) : null + } + + getFreshMessageCount(chatId: number): number { + debug(`getFreshMessageCount ${chatId}`) + return binding.dcn_get_fresh_msg_cnt(this.dcn_context, Number(chatId)) + } + + getFreshMessages() { + debug('getFreshMessages') + return binding.dcn_get_fresh_msgs(this.dcn_context) + } + + getInfo() { + debug('getInfo') + const info = binding.dcn_get_info(this.dcn_context) + return AccountManager.parseGetInfo(info) + } + + getMessage(messageId: number) { + debug(`getMessage ${messageId}`) + const dc_msg = binding.dcn_get_msg(this.dcn_context, Number(messageId)) + return dc_msg ? new Message(dc_msg) : null + } + + getMessageCount(chatId: number): number { + debug(`getMessageCount ${chatId}`) + return binding.dcn_get_msg_cnt(this.dcn_context, Number(chatId)) + } + + getMessageInfo(messageId: number): string { + debug(`getMessageInfo ${messageId}`) + return binding.dcn_get_msg_info(this.dcn_context, Number(messageId)) + } + + getMessageHTML(messageId: number): string { + debug(`getMessageHTML ${messageId}`) + return binding.dcn_get_msg_html(this.dcn_context, Number(messageId)) + } + + getNextMediaMessage( + messageId: number, + msgType1: number, + msgType2: number, + msgType3: number + ) { + debug( + `getNextMediaMessage ${messageId} ${msgType1} ${msgType2} ${msgType3}` + ) + return this._getNextMedia(messageId, 1, msgType1, msgType2, msgType3) + } + + getPreviousMediaMessage( + messageId: number, + msgType1: number, + msgType2: number, + msgType3: number + ) { + debug( + `getPreviousMediaMessage ${messageId} ${msgType1} ${msgType2} ${msgType3}` + ) + return this._getNextMedia(messageId, -1, msgType1, msgType2, msgType3) + } + + _getNextMedia( + messageId: number, + dir: number, + msgType1: number, + msgType2: number, + msgType3: number + ): number { + return binding.dcn_get_next_media( + this.dcn_context, + Number(messageId), + dir, + msgType1 || 0, + msgType2 || 0, + msgType3 || 0 + ) + } + + getSecurejoinQrCode(chatId: number): string { + debug(`getSecurejoinQrCode ${chatId}`) + return binding.dcn_get_securejoin_qr(this.dcn_context, Number(chatId)) + } + + getSecurejoinQrCodeSVG(chatId: number): string { + debug(`getSecurejoinQrCodeSVG ${chatId}`) + return binding.dcn_get_securejoin_qr_svg(this.dcn_context, chatId) + } + + startIO(): void { + debug(`startIO`) + binding.dcn_start_io(this.dcn_context) + } + + stopIO(): void { + debug(`stopIO`) + binding.dcn_stop_io(this.dcn_context) + } + + stopOngoingProcess(): void { + debug(`stopOngoingProcess`) + binding.dcn_stop_ongoing_process(this.dcn_context) + } + + /** + * + * @deprectated please use `AccountManager.getSystemInfo()` instead + */ + static getSystemInfo() { + return AccountManager.getSystemInfo() + } + + getConnectivity(): number { + return binding.dcn_get_connectivity(this.dcn_context) + } + + getConnectivityHTML(): String { + return binding.dcn_get_connectivity_html(this.dcn_context) + } + + importExport(what: number, param1: string, param2 = '') { + debug(`importExport ${what} ${param1} ${param2}`) + binding.dcn_imex(this.dcn_context, what, param1, param2) + } + + importExportHasBackup(dir: string) { + debug(`importExportHasBackup ${dir}`) + return binding.dcn_imex_has_backup(this.dcn_context, dir) + } + + initiateKeyTransfer(): Promise { + return new Promise((resolve, reject) => { + debug('initiateKeyTransfer2') + binding.dcn_initiate_key_transfer(this.dcn_context, resolve) + }) + } + + isConfigured() { + debug('isConfigured') + return Boolean(binding.dcn_is_configured(this.dcn_context)) + } + + isContactInChat(chatId: number, contactId: number) { + debug(`isContactInChat ${chatId} ${contactId}`) + return Boolean( + binding.dcn_is_contact_in_chat( + this.dcn_context, + Number(chatId), + Number(contactId) + ) + ) + } + + /** + * + * @returns resulting chat id or 0 on error + */ + joinSecurejoin(qrCode: string): number { + debug(`joinSecurejoin ${qrCode}`) + return binding.dcn_join_securejoin(this.dcn_context, qrCode) + } + + lookupContactIdByAddr(addr: string): number { + debug(`lookupContactIdByAddr ${addr}`) + return binding.dcn_lookup_contact_id_by_addr(this.dcn_context, addr) + } + + markNoticedChat(chatId: number) { + debug(`markNoticedChat ${chatId}`) + binding.dcn_marknoticed_chat(this.dcn_context, Number(chatId)) + } + + markSeenMessages(messageIds: number[]) { + if (!Array.isArray(messageIds)) { + messageIds = [messageIds] + } + messageIds = messageIds.map((id) => Number(id)) + debug('markSeenMessages', messageIds) + binding.dcn_markseen_msgs(this.dcn_context, messageIds) + } + + maybeNetwork() { + debug('maybeNetwork') + binding.dcn_maybe_network(this.dcn_context) + } + + messageNew(viewType = C.DC_MSG_TEXT) { + debug(`messageNew ${viewType}`) + return new Message(binding.dcn_msg_new(this.dcn_context, viewType)) + } + + removeContactFromChat(chatId: number, contactId: number) { + debug(`removeContactFromChat ${chatId} ${contactId}`) + return Boolean( + binding.dcn_remove_contact_from_chat( + this.dcn_context, + Number(chatId), + Number(contactId) + ) + ) + } + + /** + * + * @param chatId ID of the chat to search messages in. Set this to 0 for a global search. + * @param query The query to search for. + */ + searchMessages(chatId: number, query: string): number[] { + debug(`searchMessages ${chatId} ${query}`) + return binding.dcn_search_msgs(this.dcn_context, Number(chatId), query) + } + + sendMessage(chatId: number, msg: string | Message) { + debug(`sendMessage ${chatId}`) + if (!msg) { + throw new Error('invalid msg parameter') + } + if (typeof msg === 'string') { + const msgObj = this.messageNew() + msgObj.setText(msg) + msg = msgObj + } + if (!msg.dc_msg) { + throw new Error('invalid msg object') + } + return binding.dcn_send_msg(this.dcn_context, Number(chatId), msg.dc_msg) + } + + downloadFullMessage(messageId: number) { + binding.dcn_download_full_msg(this.dcn_context, messageId) + } + + /** + * + * @returns {Promise} Promise that resolves into the resulting message id + */ + sendVideochatInvitation(chatId: number): Promise { + debug(`sendVideochatInvitation ${chatId}`) + return new Promise((resolve, reject) => { + binding.dcn_send_videochat_invitation( + this.dcn_context, + chatId, + (result: number) => { + if (result !== 0) { + resolve(result) + } else { + reject( + 'Videochatinvitation failed to send, see error events for detailed info' + ) + } + } + ) + }) + } + + setChatName(chatId: number, name: string) { + debug(`setChatName ${chatId} ${name}`) + return Boolean( + binding.dcn_set_chat_name(this.dcn_context, Number(chatId), name) + ) + } + + /** + * + * @param chatId + * @param protect + * @returns success boolean + */ + setChatProtection(chatId: number, protect: boolean) { + debug(`setChatProtection ${chatId} ${protect}`) + return Boolean( + binding.dcn_set_chat_protection( + this.dcn_context, + Number(chatId), + protect ? 1 : 0 + ) + ) + } + + getChatEphemeralTimer(chatId: number): number { + debug(`getChatEphemeralTimer ${chatId}`) + return binding.dcn_get_chat_ephemeral_timer( + this.dcn_context, + Number(chatId) + ) + } + + setChatEphemeralTimer(chatId: number, timer: number) { + debug(`setChatEphemeralTimer ${chatId} ${timer}`) + return Boolean( + binding.dcn_set_chat_ephemeral_timer( + this.dcn_context, + Number(chatId), + Number(timer) + ) + ) + } + + setChatProfileImage(chatId: number, image: string) { + debug(`setChatProfileImage ${chatId} ${image}`) + return Boolean( + binding.dcn_set_chat_profile_image( + this.dcn_context, + Number(chatId), + image || '' + ) + ) + } + + setConfig(key: string, value: string | boolean | number): number { + debug(`setConfig (string) ${key} ${value}`) + if (value === null) { + return binding.dcn_set_config_null(this.dcn_context, key) + } else { + if (typeof value === 'boolean') { + value = value === true ? '1' : '0' + } else if (typeof value === 'number') { + value = String(value) + } + return binding.dcn_set_config(this.dcn_context, key, value) + } + } + + setConfigFromQr(qrcodeContent: string): boolean { + return Boolean( + binding.dcn_set_config_from_qr(this.dcn_context, qrcodeContent) + ) + } + + estimateDeletionCount(fromServer: boolean, seconds: number): number { + debug(`estimateDeletionCount fromServer: ${fromServer} seconds: ${seconds}`) + return binding.dcn_estimate_deletion_cnt( + this.dcn_context, + fromServer === true ? 1 : 0, + seconds + ) + } + + setStockTranslation(stockId: number, stockMsg: string) { + debug(`setStockTranslation ${stockId} ${stockMsg}`) + return Boolean( + binding.dcn_set_stock_translation( + this.dcn_context, + Number(stockId), + stockMsg + ) + ) + } + + setDraft(chatId: number, msg: Message | null) { + debug(`setDraft ${chatId}`) + binding.dcn_set_draft( + this.dcn_context, + Number(chatId), + msg ? msg.dc_msg : null + ) + } + + setLocation(latitude: number, longitude: number, accuracy: number) { + debug(`setLocation ${latitude}`) + binding.dcn_set_location( + this.dcn_context, + Number(latitude), + Number(longitude), + Number(accuracy) + ) + } + + /* + * @param chatId Chat-id to get location information for. + * 0 to get locations independently of the chat. + * @param contactId Contact id to get location information for. + * If also a chat-id is given, this should be a member of the given chat. + * 0 to get locations independently of the contact. + * @param timestampFrom Start of timespan to return. + * Must be given in number of seconds since 00:00 hours, Jan 1, 1970 UTC. + * 0 for "start from the beginning". + * @param timestampTo End of timespan to return. + * Must be given in number of seconds since 00:00 hours, Jan 1, 1970 UTC. + * 0 for "all up to now". + * @return Array of locations, NULL is never returned. + * The array is sorted decending; + * the first entry in the array is the location with the newest timestamp. + * + * Examples: + * // get locations from the last hour for a global map + * getLocations(0, 0, time(NULL)-60*60, 0); + * + * // get locations from a contact for a global map + * getLocations(0, contact_id, 0, 0); + * + * // get all locations known for a given chat + * getLocations(chat_id, 0, 0, 0); + * + * // get locations from a single contact for a given chat + * getLocations(chat_id, contact_id, 0, 0); + */ + + getLocations( + chatId: number, + contactId: number, + timestampFrom = 0, + timestampTo = 0 + ) { + const locations = new Locations( + binding.dcn_get_locations( + this.dcn_context, + Number(chatId), + Number(contactId), + timestampFrom, + timestampTo + ) + ) + return locations.toJson() + } + + /** + * + * @param duration The duration (0 for no mute, -1 for forever mute, everything else is is the relative mute duration from now in seconds) + */ + setChatMuteDuration(chatId: number, duration: number) { + return Boolean( + binding.dcn_set_chat_mute_duration(this.dcn_context, chatId, duration) + ) + } + + /** get information about the provider */ + getProviderFromEmail(email: string) { + debug('DeltaChat.getProviderFromEmail') + const provider = binding.dcn_provider_new_from_email( + this.dcn_context, + email + ) + if (!provider) { + return undefined + } + return { + before_login_hint: binding.dcn_provider_get_before_login_hint(provider), + overview_page: binding.dcn_provider_get_overview_page(provider), + status: binding.dcn_provider_get_status(provider), + } + } + + sendWebxdcStatusUpdate( + msgId: number, + json: WebxdcSendingStatusUpdate, + descr: string + ) { + return Boolean( + binding.dcn_send_webxdc_status_update( + this.dcn_context, + msgId, + JSON.stringify(json), + descr + ) + ) + } + + getWebxdcStatusUpdates( + msgId: number, + serial = 0 + ): WebxdcReceivedStatusUpdate[] { + return JSON.parse( + binding.dcn_get_webxdc_status_updates(this.dcn_context, msgId, serial) + ) + } + + /** the string contains the binary data, it is an "u8 string", maybe we will use a more efficient type in the future. */ + getWebxdcBlob(message: Message, filename: string): Buffer | null { + return binding.dcn_msg_get_webxdc_blob(message.dc_msg, filename) + } +} + +type WebxdcSendingStatusUpdate = { + /** the payload, deserialized json: + * any javascript primitive, array or object. */ + payload: T + /** optional, short, informational message that will be added to the chat, + * eg. "Alice voted" or "Bob scored 123 in MyGame"; + * usually only one line of text is shown, + * use this option sparingly to not spam the chat. */ + info?: string + /** optional, short text, shown beside app icon; + * it is recommended to use some aggregated value, + * eg. "8 votes", "Highscore: 123" */ + summary?: string +} + +type WebxdcReceivedStatusUpdate = { + /** the payload, deserialized json */ + payload: T + /** the serial number of this update. Serials are larger `0` and newer serials have higher numbers. */ + serial: number + /** the maximum serial currently known. + * If `max_serial` equals `serial` this update is the last update (until new network messages arrive). */ + max_serial: number + /** optional, short, informational message. */ + info?: string + /** optional, short text, shown beside app icon. If there are no updates, an empty JSON-array is returned. */ + summary?: string +} diff --git a/node/lib/deltachat.ts b/node/lib/deltachat.ts new file mode 100644 index 000000000..860fe41d3 --- /dev/null +++ b/node/lib/deltachat.ts @@ -0,0 +1,200 @@ +/* eslint-disable camelcase */ + +import binding from './binding' +import { EventId2EventName } from './constants' +import { EventEmitter } from 'events' +import { existsSync } from 'fs' +import rawDebug from 'debug' +import { tmpdir } from 'os' +import { join } from 'path' +import { Context } from './context' +const debug = rawDebug('deltachat:node:index') + +const noop = function () {} +interface NativeAccount {} + +/** + * Wrapper around dcn_account_t* + */ +export class AccountManager extends EventEmitter { + dcn_accounts: NativeAccount + accountDir: string + + constructor(cwd: string, os = 'deltachat-node') { + debug('DeltaChat constructor') + super() + + this.accountDir = cwd + this.dcn_accounts = binding.dcn_accounts_new(os, this.accountDir) + } + + getAllAccountIds() { + return binding.dcn_accounts_get_all(this.dcn_accounts) + } + + selectAccount(account_id: number) { + return binding.dcn_accounts_select_account(this.dcn_accounts, account_id) + } + + selectedAccount(): number { + return binding.dcn_accounts_get_selected_account(this.dcn_accounts) + } + + addAccount(): number { + return binding.dcn_accounts_add_account(this.dcn_accounts) + } + + addClosedAccount(): number { + return binding.dcn_accounts_add_closed_account(this.dcn_accounts) + } + + removeAccount(account_id: number) { + return binding.dcn_accounts_remove_account(this.dcn_accounts, account_id) + } + + accountContext(account_id: number) { + const native_context = binding.dcn_accounts_get_account( + this.dcn_accounts, + account_id + ) + return new Context(this, native_context, account_id) + } + + migrateAccount(dbfile: string): number { + return binding.dcn_accounts_migrate_account(this.dcn_accounts, dbfile) + } + + close() { + this.stopIO() + debug('unrefing context') + binding.dcn_accounts_unref(this.dcn_accounts) + debug('Unref end') + } + + emit( + event: string | symbol, + account_id: number, + data1: any, + data2: any + ): boolean { + super.emit('ALL', event, account_id, data1, data2) + return super.emit(event, account_id, data1, data2) + } + + handleCoreEvent( + eventId: number, + accountId: number, + data1: number, + data2: number | string + ) { + const eventString = EventId2EventName[eventId] + debug('event', eventString, accountId, data1, data2) + debug(eventString, data1, data2) + if (!this.emit) { + console.log('Received an event but EventEmitter is already destroyed.') + console.log(eventString, data1, data2) + return + } + this.emit(eventString, accountId, data1, data2) + } + + startEvents() { + if (this.dcn_accounts === null) { + throw new Error('dcn_account is null') + } + binding.dcn_accounts_start_event_handler( + this.dcn_accounts, + this.handleCoreEvent.bind(this) + ) + debug('Started event handler') + } + + startIO() { + binding.dcn_accounts_start_io(this.dcn_accounts) + } + + stopIO() { + binding.dcn_accounts_stop_io(this.dcn_accounts) + } + + static maybeValidAddr(addr: string) { + debug('DeltaChat.maybeValidAddr') + if (addr === null) return false + return Boolean(binding.dcn_maybe_valid_addr(addr)) + } + + static parseGetInfo(info: string) { + debug('static _getInfo') + const result: { [key: string]: string } = {} + + const regex = /^(\w+)=(.*)$/i + info + .split('\n') + .filter(Boolean) + .forEach((line) => { + const match = regex.exec(line) + if (match) { + result[match[1]] = match[2] + } + }) + + return result + } + + static newTemporary() { + let directory = null + while (true) { + const randomString = Math.random().toString(36).substr(2, 5) + directory = join(tmpdir(), 'deltachat-' + randomString) + if (!existsSync(directory)) break + } + const dc = new AccountManager(directory) + const accountId = dc.addAccount() + const context = dc.accountContext(accountId) + return { dc, context, accountId, directory } + } + + static getSystemInfo() { + debug('DeltaChat.getSystemInfo') + const { dc, context } = AccountManager.newTemporary() + const info = AccountManager.parseGetInfo( + binding.dcn_get_info(context.dcn_context) + ) + const { + deltachat_core_version, + sqlite_version, + sqlite_thread_safe, + libetpan_version, + openssl_version, + compile_date, + arch, + } = info + const result = { + deltachat_core_version, + sqlite_version, + sqlite_thread_safe, + libetpan_version, + openssl_version, + compile_date, + arch, + } + context.unref() + dc.close() + return result + } + + /** get information about the provider + * + * This function creates a temporary context to be standalone, + * if posible use `Context.getProviderFromEmail` instead. (otherwise potential proxy settings are not used) + * @deprecated + */ + static getProviderFromEmail(email: string) { + debug('DeltaChat.getProviderFromEmail') + const { dc, context } = AccountManager.newTemporary() + const provider = context.getProviderFromEmail(email) + context.unref() + dc.close() + return provider + } +} diff --git a/node/lib/index.ts b/node/lib/index.ts new file mode 100644 index 000000000..6b179dbeb --- /dev/null +++ b/node/lib/index.ts @@ -0,0 +1,20 @@ +import { AccountManager } from './deltachat' + +export default AccountManager + +export { Context } from './context' +export { Chat } from './chat' +export { ChatList } from './chatlist' +export { C } from './constants' +export { Contact } from './contact' +export { AccountManager as DeltaChat } +export { Locations } from './locations' +export { Lot } from './lot' +export { + Message, + MessageState, + MessageViewType, + MessageDownloadState, +} from './message' + +export * from './types' diff --git a/node/lib/locations.ts b/node/lib/locations.ts new file mode 100644 index 000000000..1be2bea65 --- /dev/null +++ b/node/lib/locations.ts @@ -0,0 +1,79 @@ +/* eslint-disable camelcase */ + +const binding = require('../binding') +const debug = require('debug')('deltachat:node:locations') + +interface NativeLocations {} +/** + * Wrapper around dc_location_t* + */ +export class Locations { + constructor(public dc_locations: NativeLocations) { + debug('Locations constructor') + } + + locationToJson(index: number) { + debug('locationToJson') + return { + accuracy: this.getAccuracy(index), + latitude: this.getLatitude(index), + longitude: this.getLongitude(index), + timestamp: this.getTimestamp(index), + contactId: this.getContactId(index), + msgId: this.getMsgId(index), + chatId: this.getChatId(index), + isIndependent: this.isIndependent(index), + marker: this.getMarker(index), + } + } + + toJson(): ReturnType[] { + debug('toJson') + const locations = [] + const count = this.getCount() + for (let index = 0; index < count; index++) { + locations.push(this.locationToJson(index)) + } + return locations + } + + getCount(): number { + return binding.dcn_array_get_cnt(this.dc_locations) + } + + getAccuracy(index: number): number { + return binding.dcn_array_get_accuracy(this.dc_locations, index) + } + + getLatitude(index: number): number { + return binding.dcn_array_get_latitude(this.dc_locations, index) + } + + getLongitude(index: number): number { + return binding.dcn_array_get_longitude(this.dc_locations, index) + } + + getTimestamp(index: number): number { + return binding.dcn_array_get_timestamp(this.dc_locations, index) + } + + getMsgId(index: number): number { + return binding.dcn_array_get_msg_id(this.dc_locations, index) + } + + getContactId(index: number): number { + return binding.dcn_array_get_contact_id(this.dc_locations, index) + } + + getChatId(index: number): number { + return binding.dcn_array_get_chat_id(this.dc_locations, index) + } + + isIndependent(index: number): boolean { + return binding.dcn_array_is_independent(this.dc_locations, index) + } + + getMarker(index: number): string { + return binding.dcn_array_get_marker(this.dc_locations, index) + } +} diff --git a/node/lib/lot.ts b/node/lib/lot.ts new file mode 100644 index 000000000..0370fbc7e --- /dev/null +++ b/node/lib/lot.ts @@ -0,0 +1,49 @@ +/* eslint-disable camelcase */ + +const binding = require('../binding') +const debug = require('debug')('deltachat:node:lot') + +interface NativeLot {} +/** + * Wrapper around dc_lot_t* + */ +export class Lot { + constructor(public dc_lot: NativeLot) { + debug('Lot constructor') + } + + toJson() { + debug('toJson') + return { + state: this.getState(), + text1: this.getText1(), + text1Meaning: this.getText1Meaning(), + text2: this.getText2(), + timestamp: this.getTimestamp(), + } + } + + getId(): number { + return binding.dcn_lot_get_id(this.dc_lot) + } + + getState(): number { + return binding.dcn_lot_get_state(this.dc_lot) + } + + getText1(): string { + return binding.dcn_lot_get_text1(this.dc_lot) + } + + getText1Meaning(): string { + return binding.dcn_lot_get_text1_meaning(this.dc_lot) + } + + getText2(): string { + return binding.dcn_lot_get_text2(this.dc_lot) + } + + getTimestamp(): number { + return binding.dcn_lot_get_timestamp(this.dc_lot) + } +} diff --git a/node/lib/message.ts b/node/lib/message.ts new file mode 100644 index 000000000..4817365f0 --- /dev/null +++ b/node/lib/message.ts @@ -0,0 +1,366 @@ +/* eslint-disable camelcase */ + +import binding from './binding' +import { C } from './constants' +import { Lot } from './lot' +import { Chat } from './chat' +const debug = require('debug')('deltachat:node:message') + +export enum MessageDownloadState { + Available = C.DC_DOWNLOAD_AVAILABLE, + Done = C.DC_DOWNLOAD_DONE, + Failure = C.DC_DOWNLOAD_FAILURE, + InProgress = C.DC_DOWNLOAD_IN_PROGRESS, +} + +/** + * Helper class for message states so you can do e.g. + * + * if (msg.getState().isPending()) { .. } + * + */ +export class MessageState { + constructor(public state: number) { + debug(`MessageState constructor ${state}`) + } + + isUndefined() { + return this.state === C.DC_STATE_UNDEFINED + } + + isFresh() { + return this.state === C.DC_STATE_IN_FRESH + } + + isNoticed() { + return this.state === C.DC_STATE_IN_NOTICED + } + + isSeen() { + return this.state === C.DC_STATE_IN_SEEN + } + + isPending() { + return this.state === C.DC_STATE_OUT_PENDING + } + + isFailed() { + return this.state === C.DC_STATE_OUT_FAILED + } + + isDelivered() { + return this.state === C.DC_STATE_OUT_DELIVERED + } + + isReceived() { + return this.state === C.DC_STATE_OUT_MDN_RCVD + } +} + +/** + * Helper class for message types so you can do e.g. + * + * if (msg.getViewType().isVideo()) { .. } + * + */ +export class MessageViewType { + constructor(public viewType: number) { + debug(`MessageViewType constructor ${viewType}`) + } + + isText() { + return this.viewType === C.DC_MSG_TEXT + } + + isImage() { + return this.viewType === C.DC_MSG_IMAGE || this.viewType === C.DC_MSG_GIF + } + + isGif() { + return this.viewType === C.DC_MSG_GIF + } + + isAudio() { + return this.viewType === C.DC_MSG_AUDIO || this.viewType === C.DC_MSG_VOICE + } + + isVoice() { + return this.viewType === C.DC_MSG_VOICE + } + + isVideo() { + return this.viewType === C.DC_MSG_VIDEO + } + + isFile() { + return this.viewType === C.DC_MSG_FILE + } + + isVideochatInvitation() { + return this.viewType === C.DC_MSG_VIDEOCHAT_INVITATION + } +} + +interface NativeMessage {} +/** + * Wrapper around dc_msg_t* + */ +export class Message { + constructor(public dc_msg: NativeMessage) { + debug('Message constructor') + } + + toJson() { + debug('toJson') + const quotedMessage = this.getQuotedMessage() + const viewType = binding.dcn_msg_get_viewtype(this.dc_msg) + return { + chatId: this.getChatId(), + webxdcInfo: viewType == C.DC_MSG_WEBXDC ? this.webxdcInfo : null, + downloadState: this.downloadState, + duration: this.getDuration(), + file: this.getFile(), + fromId: this.getFromId(), + id: this.getId(), + quotedText: this.getQuotedText(), + quotedMessageId: quotedMessage ? quotedMessage.getId() : null, + receivedTimestamp: this.getReceivedTimestamp(), + sortTimestamp: this.getSortTimestamp(), + text: this.getText(), + timestamp: this.getTimestamp(), + hasLocation: this.hasLocation(), + hasHTML: this.hasHTML, + viewType, + state: binding.dcn_msg_get_state(this.dc_msg), + hasDeviatingTimestamp: this.hasDeviatingTimestamp(), + showPadlock: this.getShowpadlock(), + summary: this.getSummary().toJson(), + subject: this.subject, + isSetupmessage: this.isSetupmessage(), + isInfo: this.isInfo(), + isForwarded: this.isForwarded(), + dimensions: { + height: this.getHeight(), + width: this.getWidth(), + }, + videochatType: this.getVideochatType(), + videochatUrl: this.getVideochatUrl(), + overrideSenderName: this.overrideSenderName, + parentId: this.parent?.getId(), + } + } + + getChatId(): number { + return binding.dcn_msg_get_chat_id(this.dc_msg) + } + + get webxdcInfo(): { name: string; icon: string; summary: string } | null { + let info = binding.dcn_msg_get_webxdc_info(this.dc_msg) + return info + ? JSON.parse(binding.dcn_msg_get_webxdc_info(this.dc_msg)) + : null + } + + get downloadState(): MessageDownloadState { + return binding.dcn_msg_get_download_state(this.dc_msg) + } + + get parent(): Message | null { + let msg = binding.dcn_msg_get_parent(this.dc_msg) + return msg ? new Message(msg) : null + } + + getDuration(): number { + return binding.dcn_msg_get_duration(this.dc_msg) + } + + getFile(): string { + return binding.dcn_msg_get_file(this.dc_msg) + } + + getFilebytes(): number { + return binding.dcn_msg_get_filebytes(this.dc_msg) + } + + getFilemime(): string { + return binding.dcn_msg_get_filemime(this.dc_msg) + } + + getFilename(): string { + return binding.dcn_msg_get_filename(this.dc_msg) + } + + getFromId(): number { + return binding.dcn_msg_get_from_id(this.dc_msg) + } + + getHeight(): number { + return binding.dcn_msg_get_height(this.dc_msg) + } + + getId(): number { + return binding.dcn_msg_get_id(this.dc_msg) + } + + getQuotedText(): string { + return binding.dcn_msg_get_quoted_text(this.dc_msg) + } + + getQuotedMessage(): Message | null { + const dc_msg = binding.dcn_msg_get_quoted_msg(this.dc_msg) + return dc_msg ? new Message(dc_msg) : null + } + + getReceivedTimestamp(): number { + return binding.dcn_msg_get_received_timestamp(this.dc_msg) + } + + getSetupcodebegin() { + return binding.dcn_msg_get_setupcodebegin(this.dc_msg) + } + + getShowpadlock() { + return Boolean(binding.dcn_msg_get_showpadlock(this.dc_msg)) + } + + getSortTimestamp(): number { + return binding.dcn_msg_get_sort_timestamp(this.dc_msg) + } + + getState() { + return new MessageState(binding.dcn_msg_get_state(this.dc_msg)) + } + + getSummary(chat?: Chat) { + const dc_chat = (chat && chat.dc_chat) || null + return new Lot(binding.dcn_msg_get_summary(this.dc_msg, dc_chat)) + } + + get subject(): string { + return binding.dcn_msg_get_subject(this.dc_msg) + } + + getSummarytext(approxCharacters: number): string { + approxCharacters = approxCharacters || 0 + return binding.dcn_msg_get_summarytext(this.dc_msg, approxCharacters) + } + + getText(): string { + return binding.dcn_msg_get_text(this.dc_msg) + } + + getTimestamp(): number { + return binding.dcn_msg_get_timestamp(this.dc_msg) + } + + getViewType() { + return new MessageViewType(binding.dcn_msg_get_viewtype(this.dc_msg)) + } + + getVideochatType(): number { + return binding.dcn_msg_get_videochat_type(this.dc_msg) + } + + getVideochatUrl(): string { + return binding.dcn_msg_get_videochat_url(this.dc_msg) + } + + getWidth(): number { + return binding.dcn_msg_get_width(this.dc_msg) + } + + get overrideSenderName(): string { + return binding.dcn_msg_get_override_sender_name(this.dc_msg) + } + + hasDeviatingTimestamp() { + return binding.dcn_msg_has_deviating_timestamp(this.dc_msg) + } + + hasLocation() { + return Boolean(binding.dcn_msg_has_location(this.dc_msg)) + } + + get hasHTML() { + return Boolean(binding.dcn_msg_has_html(this.dc_msg)) + } + + isDeadDrop() { + // TODO: Fix + //return this.getChatId() === C.DC_CHAT_ID_DEADDROP + return false + } + + isForwarded() { + return Boolean(binding.dcn_msg_is_forwarded(this.dc_msg)) + } + + isIncreation() { + return Boolean(binding.dcn_msg_is_increation(this.dc_msg)) + } + + isInfo() { + return Boolean(binding.dcn_msg_is_info(this.dc_msg)) + } + + isSent() { + return Boolean(binding.dcn_msg_is_sent(this.dc_msg)) + } + + isSetupmessage() { + return Boolean(binding.dcn_msg_is_setupmessage(this.dc_msg)) + } + + latefilingMediasize(width: number, height: number, duration: number) { + binding.dcn_msg_latefiling_mediasize(this.dc_msg, width, height, duration) + } + + setDimension(width: number, height: number) { + binding.dcn_msg_set_dimension(this.dc_msg, width, height) + return this + } + + setDuration(duration: number) { + binding.dcn_msg_set_duration(this.dc_msg, duration) + return this + } + + setFile(file: string, mime?: string) { + if (typeof file !== 'string') throw new Error('Missing filename') + binding.dcn_msg_set_file(this.dc_msg, file, mime || '') + return this + } + + setLocation(longitude: number, latitude: number) { + binding.dcn_msg_set_location(this.dc_msg, longitude, latitude) + return this + } + + setQuote(quotedMessage: Message | null) { + binding.dcn_msg_set_quote(this.dc_msg, quotedMessage?.dc_msg) + return this + } + + setText(text: string) { + binding.dcn_msg_set_text(this.dc_msg, text) + return this + } + + setHTML(html: string) { + binding.dcn_msg_set_html(this.dc_msg, html) + return this + } + + setOverrideSenderName(senderName: string) { + binding.dcn_msg_set_override_sender_name(this.dc_msg, senderName) + return this + } + + /** Force the message to be sent in plain text. + * + * This API is for bots, there is no need to expose it in the UI. + */ + forcePlaintext() { + binding.dcn_msg_force_plaintext(this.dc_msg) + } +} diff --git a/node/lib/types.ts b/node/lib/types.ts new file mode 100644 index 000000000..feb6e981e --- /dev/null +++ b/node/lib/types.ts @@ -0,0 +1,24 @@ +import { C } from './constants' + +export type ChatTypes = + | C.DC_CHAT_TYPE_GROUP + | C.DC_CHAT_TYPE_MAILINGLIST + | C.DC_CHAT_TYPE_SINGLE + | C.DC_CHAT_TYPE_UNDEFINED + +export interface ChatJSON { + archived: boolean + pinned: boolean + color: string + id: number + name: string + profileImage: string + type: number + isSelfTalk: boolean + isUnpromoted: boolean + isProtected: boolean + canSend: boolean + isDeviceTalk: boolean + isContactRequest: boolean + muted: boolean +} diff --git a/node/lib/util.ts b/node/lib/util.ts new file mode 100644 index 000000000..7a8c4ba6b --- /dev/null +++ b/node/lib/util.ts @@ -0,0 +1,6 @@ +/** + * @param integerColor expects a 24bit rgb integer (left to right: 8bits red, 8bits green, 8bits blue) + */ +export function integerToHexColor(integerColor: number) { + return '#' + (integerColor + 16777216).toString(16).substring(1) +} diff --git a/node/package.json b/node/package.json new file mode 100644 index 000000000..11b03c731 --- /dev/null +++ b/node/package.json @@ -0,0 +1,67 @@ +{ + "name": "deltachat-node", + "version": "1.77.1", + "description": "node.js bindings for deltachat-core", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "coverage": "nyc report --reporter=text-lcov | coveralls", + "coverage-html-report": "rm -rf coverage/ && nyc report --reporter=html && xdg-open coverage/index.html", + "install": "node scripts/install.js", + "install:prebuilds": "node-gyp-build \"npm run build:core\" \"npm run build:bindings:c:postinstall\"", + "clean": "rm -rf ./dist ./build ./prebuilds ./deltachat-core-rust/target", + "build": "npm run build:core && npm run build:bindings", + "build:core": "npm run build:core:rust && npm run build:core:constants", + "build:core:rust": "node scripts/rebuild-core.js", + "build:core:constants": "node scripts/generate-constants.js", + "build:bindings": "npm run build:bindings:c && npm run build:bindings:ts", + "build:bindings:c": "npm run build:bindings:c:c && npm run build:bindings:c:postinstall", + "build:bindings:c:c": "npx node-gyp rebuild", + "build:bindings:c:postinstall": "node scripts/postinstall.js", + "build:bindings:ts": "tsc", + "prebuildify": "prebuildify -t 16.13.0 --napi --strip --postinstall \"node scripts/postinstall.js --prebuild\"", + "download-prebuilds": "prebuildify-ci download", + "submodule": "git submodule update --recursive --init", + "test": "npm run test:lint && npm run test:mocha", + "test:mocha": "mocha -r esm test/test.js --growl --reporter=spec", + "test:lint": "npm run lint", + "testCoverage": "nyc mocha -r esm test/test.js --growl --reporter=spec", + "hallmark": "hallmark --fix", + "lint": "prettier --check \"lib/**/*.{ts,tsx}\"", + "lint-fix": "prettier --write \"lib/**/*.{ts,tsx}\" \"test/**/*.js\"" + }, + "homepage": "https://github.com/deltachat/deltachat-node", + "repository": { + "type": "git", + "url": "https://github.com/deltachat/deltachat-node.git" + }, + "engines": { + "node": ">=16.0.0" + }, + "license": "GPL-3.0-or-later", + "dependencies": { + "debug": "^4.1.1", + "napi-macros": "^2.0.0", + "node-gyp-build": "^4.1.0", + "split2": "^3.1.1" + }, + "devDependencies": { + "@types/debug": "^4.1.7", + "@types/node": "^16.11.26", + "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", + "coveralls": "^3.0.3", + "esm": "^3.2.25", + "hallmark": "^2.0.0", + "mocha": "^8.2.1", + "node-fetch": "^2.6.7", + "node-gyp": "^9.0.0", + "nyc": "^15.0.0", + "opn-cli": "^5.0.0", + "prebuildify": "^3.0.0", + "prebuildify-ci": "^1.0.4", + "prettier": "^2.0.5", + "typedoc": "^0.17.0", + "typescript": "^3.9.10" + } +} diff --git a/node/patches/m1_build_use_x86_64.patch b/node/patches/m1_build_use_x86_64.patch new file mode 100644 index 000000000..b404f3c69 --- /dev/null +++ b/node/patches/m1_build_use_x86_64.patch @@ -0,0 +1,13 @@ +diff --git a/binding.gyp b/binding.gyp +index 199f4d3..15b47f8 100644 +--- a/binding.gyp ++++ b/binding.gyp +@@ -42,7 +42,7 @@ + { + "include_dirs": ["deltachat-core-rust/deltachat-ffi"], + "libraries": [ +- "../deltachat-core-rust/target/release/libdeltachat.a", ++ "../deltachat-core-rust/target/x86_64-apple-darwin/release/libdeltachat.a", + "-ldl", + ], + "conditions": [], diff --git a/node/scripts/common.js b/node/scripts/common.js new file mode 100644 index 000000000..af085cb59 --- /dev/null +++ b/node/scripts/common.js @@ -0,0 +1,26 @@ +const spawnSync = require('child_process').spawnSync + +const verbose = isVerbose() + +function spawn (cmd, args, opts) { + log(`>> spawn: ${cmd} ${args.join(' ')}`) + const result = spawnSync(cmd, args, opts) + if (result.status === null) { + console.error(`Could not find ${cmd}`) + process.exit(1) + } else if (result.status !== 0) { + console.error(`${cmd} failed with code ${result.status}`) + process.exit(1) + } +} + +function log (...args) { + if (verbose) console.log(...args) +} + +function isVerbose () { + const loglevel = process.env.npm_config_loglevel + return loglevel === 'verbose' || process.env.CI === 'true' +} + +module.exports = { spawn, log, isVerbose, verbose } diff --git a/node/scripts/generate-constants.js b/node/scripts/generate-constants.js new file mode 100755 index 000000000..fc3de2d97 --- /dev/null +++ b/node/scripts/generate-constants.js @@ -0,0 +1,73 @@ +#!/usr/bin/env node + +const fs = require('fs') +const path = require('path') +const split = require('split2') + +const data = [] +const regex = /^#define\s+(\w+)\s+(\w+)/i +const header = path.resolve( + __dirname, + '../deltachat-core-rust/deltachat-ffi/deltachat.h' +) + +console.log('Generating constants...') + +fs.createReadStream(header) + .pipe(split()) + .on('data', (line) => { + const match = regex.exec(line) + if (match) { + const key = match[1] + const value = parseInt(match[2]) + if (isNaN(value)) return + + data.push({ key, value }) + } + }) + .on('end', () => { + const constants = data + .filter( + ({ key }) => key.toUpperCase()[0] === key[0] // check if define name is uppercase + ) + .sort((lhs, rhs) => { + if (lhs.key < rhs.key) return -1 + else if (lhs.key > rhs.key) return 1 + return 0 + }) + .map((row) => { + return ` ${row.key}: ${row.value}` + }) + .join(',\n') + + const events = data + .sort((lhs, rhs) => { + if (lhs.value < rhs.value) return -1 + else if (lhs.value > rhs.value) return 1 + return 0 + }) + .filter((i) => { + return i.key.startsWith('DC_EVENT_') + }) + .map((i) => { + return ` ${i.value}: '${i.key}'` + }) + .join(',\n') + + // backwards compat + fs.writeFileSync( + path.resolve(__dirname, '../constants.js'), + `// Generated!\n\nmodule.exports = {\n${constants}\n}\n` + ) + // backwards compat + fs.writeFileSync( + path.resolve(__dirname, '../events.js'), + `/* eslint-disable quotes */\n// Generated!\n\nmodule.exports = {\n${events}\n}\n` + ) + + fs.writeFileSync( + path.resolve(__dirname, '../lib/constants.ts'), + `// Generated!\n\nexport enum C {\n${constants.replace(/:/g, ' =')},\n}\n +// Generated!\n\nexport const EventId2EventName: { [key: number]: string } = {\n${events},\n}\n` + ) + }) diff --git a/node/scripts/install.js b/node/scripts/install.js new file mode 100644 index 000000000..c1cdca2e5 --- /dev/null +++ b/node/scripts/install.js @@ -0,0 +1,27 @@ +const {execSync} = require('child_process') +const {existsSync} = require('fs') +const {join} = require('path') + +const run = (cmd) => { + console.log('[i] running `' + cmd + '`') + execSync(cmd, {stdio: 'inherit'}) +} + +if (!existsSync(join(__dirname, '..', 'deltachat-core-rust'))) { + console.log('[i] deltachat-node/deltachat-core-rust doesn\'t exist, fetching submodule. If this fails, make sure git is installed') + run('npm run submodule') +} + +// Build bindings +if (process.env.USE_SYSTEM_LIBDELTACHAT === 'true') { + console.log('[i] USE_SYSTEM_LIBDELTACHAT is true, rebuilding c bindings and using pkg-config to retrieve lib paths and cflags of libdeltachat') + run('npm run build:bindings:c:c') +} else { + console.log('[i] Building rust core & c bindings, if possible use prebuilds') + run('npm run install:prebuilds') +} + +if (!existsSync(join(__dirname, '..', 'dist'))) { + console.log('[i] Didn\'t find already built typescript bindings. Trying to transpile them. If this fail, make sure typescript is installed ;)') + run('npm run build:bindings:ts') +} diff --git a/node/scripts/postinstall.js b/node/scripts/postinstall.js new file mode 100644 index 000000000..6253469d1 --- /dev/null +++ b/node/scripts/postinstall.js @@ -0,0 +1,57 @@ +const fs = require('fs') +const path = require('path') + +if (process.platform !== 'win32') { + console.log('postinstall: not windows, so skipping!') + process.exit(0) +} + +const from = path.resolve( + __dirname, + '..', + 'deltachat-core-rust', + 'target', + 'release', + 'deltachat.dll' +) + +const getDestination = () => { + const argv = process.argv + if (argv.length === 3 && argv[2] === '--prebuild') { + return path.resolve( + __dirname, + '..', + 'prebuilds', + 'win32-x64', + 'deltachat.dll' + ) + } else { + return path.resolve( + __dirname, + '..', + 'build', + 'Release', + 'deltachat.dll' + ) + } +} + +const dest = getDestination() + +copy(from, dest, (err) => { + if (err) throw err + console.log(`postinstall: copied ${from} to ${dest}`) +}) + +function copy (from, to, cb) { + fs.stat(from, (err, st) => { + if (err) return cb(err) + fs.readFile(from, (err, buf) => { + if (err) return cb(err) + fs.writeFile(to, buf, (err) => { + if (err) return cb(err) + fs.chmod(to, st.mode, cb) + }) + }) + }) +} diff --git a/node/scripts/rebuild-core.js b/node/scripts/rebuild-core.js new file mode 100644 index 000000000..b29ebf2ac --- /dev/null +++ b/node/scripts/rebuild-core.js @@ -0,0 +1,17 @@ +const path = require('path') +const { spawn } = require('./common') +const opts = { + cwd: path.resolve(__dirname, '../deltachat-core-rust'), + stdio: 'inherit' +} + +const buildArgs = [ + 'build', + '--release', + '--features', + 'vendored', + '-p', + 'deltachat_ffi' +] + +spawn('cargo', buildArgs, opts) diff --git a/node/src/module.c b/node/src/module.c new file mode 100644 index 000000000..9f8b71beb --- /dev/null +++ b/node/src/module.c @@ -0,0 +1,3505 @@ +#define NAPI_VERSION 4 +#define NAPI_EXPERIMENTAL + +#include +#include +#include +#include +#include +#include +#include "napi-macros-extensions.h" + +//#define DEBUG + +#ifdef DEBUG +#define TRACE(fmt, ...) fprintf(stderr, "> module.c:%d %s() " fmt "\n", __LINE__, __func__, ##__VA_ARGS__) +#else +#define TRACE(fmt, ...) +#endif + +/** + * Custom context + */ +typedef struct dcn_context_t { + dc_context_t* dc_context; + napi_threadsafe_function threadsafe_event_handler; + uv_thread_t event_handler_thread; + int gc; +} dcn_context_t; + +/** + * Custom accounts + */ +typedef struct dcn_accounts_t { + dc_accounts_t* dc_accounts; + napi_threadsafe_function threadsafe_event_handler; + uv_thread_t event_handler_thread; + int gc; +} dcn_accounts_t; + + + + +/** + * Finalize functions. These are called once the corresponding + * external is garbage collected on the JavaScript side. + */ + +static void finalize_chat(napi_env env, void* data, void* hint) { + if (data) { + dc_chat_t* chat = (dc_chat_t*)data; + //TRACE("cleaning up chat %d", dc_chat_get_id(chat)); + dc_chat_unref(chat); + } +} + +static void finalize_chatlist(napi_env env, void* data, void* hint) { + if (data) { + //TRACE("cleaning up chatlist object"); + dc_chatlist_unref((dc_chatlist_t*)data); + } +} + +static void finalize_contact(napi_env env, void* data, void* hint) { + if (data) { + dc_contact_t* contact = (dc_contact_t*)data; + //TRACE("cleaning up contact %d", dc_contact_get_id(contact)); + dc_contact_unref(contact); + } +} + +static void finalize_lot(napi_env env, void* data, void* hint) { + if (data) { + //TRACE("cleaning up lot"); + dc_lot_unref((dc_lot_t*)data); + } +} + +static void finalize_array(napi_env env, void* data, void* hint) { + if (data) { + //TRACE("cleaning up array"); + dc_array_unref((dc_array_t*)data); + } +} + +static void finalize_msg(napi_env env, void* data, void* hint) { + if (data) { + dc_msg_t* msg = (dc_msg_t*)data; + //TRACE("cleaning up message %d", dc_msg_get_id(msg)); + dc_msg_unref(msg); + } +} + +static void finalize_provider(napi_env env, void* data, void* hint) { + if (data) { + dc_provider_t* provider = (dc_provider_t*)data; + //TRACE("cleaning up provider"); + dc_provider_unref(provider); + } +} + +static void finalize_account(napi_env env, void* data, void* hint) { + if (data) { + dc_accounts_t* dcn_accounts = (dc_accounts_t*)data; + //TRACE("cleaning up provider"); + dc_accounts_unref(dcn_accounts); + } +} + +/** + * Helpers. + */ + +static uint32_t* js_array_to_uint32(napi_env env, napi_value js_array, uint32_t* length) { + *length = 0; + NAPI_STATUS_THROWS(napi_get_array_length(env, js_array, length)); + + uint32_t* array = calloc(*length, sizeof(uint32_t)); + + for (uint32_t i = 0; i < *length; i++) { + napi_value napi_element; + NAPI_STATUS_THROWS(napi_get_element(env, js_array, i, &napi_element)); + NAPI_STATUS_THROWS(napi_get_value_uint32(env, napi_element, &array[i])); + } + + return array; +} + +static napi_value dc_array_to_js_array(napi_env env, dc_array_t* array) { + napi_value js_array; + + const int length = dc_array_get_cnt(array); + NAPI_STATUS_THROWS(napi_create_array_with_length(env, length, &js_array)); + + if (length > 0) { + for (int i = 0; i < length; i++) { + const uint32_t id = dc_array_get_id(array, i); + napi_value napi_id; + NAPI_STATUS_THROWS(napi_create_uint32(env, id, &napi_id)); + NAPI_STATUS_THROWS(napi_set_element(env, js_array, i, napi_id)); + } + } + + return js_array; +} + +/** + * Main context. + */ + +NAPI_METHOD(dcn_context_new) { + NAPI_ARGV(1); + + NAPI_ARGV_UTF8_MALLOC(db_file, 0); + + TRACE("creating new dc_context"); + + dcn_context_t* dcn_context = calloc(1, sizeof(dcn_context_t)); + dcn_context->dc_context = dc_context_new(NULL, db_file, NULL); + + + napi_value result; + NAPI_STATUS_THROWS(napi_create_external(env, dcn_context, + NULL, NULL, &result)); + return result; +} + +NAPI_METHOD(dcn_context_new_closed) { + NAPI_ARGV(1); + + NAPI_ARGV_UTF8_MALLOC(db_file, 0); + + TRACE("creating new closed dc_context"); + + dcn_context_t* dcn_context = calloc(1, sizeof(dcn_context_t)); + dcn_context->dc_context = dc_context_new_closed(db_file); + + + napi_value result; + NAPI_STATUS_THROWS(napi_create_external(env, dcn_context, + NULL, NULL, &result)); + return result; +} + +NAPI_METHOD(dcn_context_open) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(passphrase, 1); + + int result = dc_context_open(dcn_context->dc_context, passphrase); + free(passphrase); + + NAPI_RETURN_UINT32(result); +} + +NAPI_METHOD(dcn_context_is_open) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + int result = dc_context_is_open(dcn_context->dc_context); + + NAPI_RETURN_UINT32(result); +} + +/** + * Event struct for calling back to JavaScript + */ +typedef struct dcn_event_t { + int event; + uintptr_t data1_int; + uintptr_t data2_int; + char* data1_str; + char* data2_str; +} dcn_event_t; + + +static void event_handler_thread_func(void* arg) +{ + dcn_context_t* dcn_context = (dcn_context_t*)arg; + dc_context_t* dc_context = dcn_context->dc_context; + + + TRACE("event_handler_thread_func starting"); + + + dc_event_emitter_t* emitter = dc_get_event_emitter(dc_context); + dc_event_t* event; + while (true) { + if (emitter == NULL) { + TRACE("event emitter is null, bailing"); + break; + } + + event = dc_get_next_event(emitter); + if (event == NULL) { + TRACE("event is null, bailing"); + break; + } + + if (!dcn_context->threadsafe_event_handler) { + TRACE("threadsafe_event_handler not set, bailing"); + break; + } + + // Don't process events if we're being garbage collected! + if (dcn_context->gc == 1) { + TRACE("dc_context has been destroyed, bailing"); + break; + } + + + napi_status status = napi_call_threadsafe_function(dcn_context->threadsafe_event_handler, event, napi_tsfn_blocking); + + if (status == napi_closing) { + TRACE("JS function got released, bailing"); + break; + } + } + + dc_event_emitter_unref(emitter); + + TRACE("event_handler_thread_func ended"); + + napi_release_threadsafe_function(dcn_context->threadsafe_event_handler, napi_tsfn_release); +} + +static void call_js_event_handler(napi_env env, napi_value js_callback, void* _context, void* data) +{ + dc_event_t* dc_event = (dc_event_t*)data; + + napi_value global; + napi_status status = napi_get_global(env, &global); + + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to get global"); + } + +#define CALL_JS_CALLBACK_ARGC 3 + + const int argc = CALL_JS_CALLBACK_ARGC; + napi_value argv[CALL_JS_CALLBACK_ARGC]; + + const int event_id = dc_event_get_id(dc_event); + + status = napi_create_int32(env, event_id, &argv[0]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[0] for event_handler arguments"); + } + + status = napi_create_int32(env, dc_event_get_data1_int(dc_event), &argv[1]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[1] for event_handler arguments"); + } + + if DC_EVENT_DATA2_IS_STRING(event_id) { + char* data2_string = dc_event_get_data2_str(dc_event); + // Quick fix for https://github.com/deltachat/deltachat-core-rust/issues/1949 + if (data2_string != 0) { + status = napi_create_string_utf8(env, data2_string, NAPI_AUTO_LENGTH, &argv[2]); + } else { + status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &argv[2]); + } + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[2] for event_handler arguments"); + } + free(data2_string); + } else { + status = napi_create_int32(env, dc_event_get_data2_int(dc_event), &argv[2]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[2] for event_handler arguments"); + } + } + + dc_event_unref(dc_event); + dc_event = NULL; + + TRACE("calling back into js"); + + napi_value result; + status = napi_call_function( + env, + global, + js_callback, + argc, + argv, + &result); + + if (status != napi_ok) { + TRACE("Unable to call event_handler callback2"); + napi_extended_error_info* error_result; + NAPI_STATUS_THROWS(napi_get_last_error_info(env, &error_result)); + } +} + + +NAPI_METHOD(dcn_start_event_handler) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + napi_value callback = argv[1]; + + TRACE("calling.."); + napi_value async_resource_name; + NAPI_STATUS_THROWS(napi_create_string_utf8(env, "dc_event_callback", NAPI_AUTO_LENGTH, &async_resource_name)); + + TRACE("creating threadsafe function.."); + + NAPI_STATUS_THROWS(napi_create_threadsafe_function( + env, + callback, + 0, + async_resource_name, + 1, + 1, + NULL, + NULL, + dcn_context, + call_js_event_handler, + &dcn_context->threadsafe_event_handler)); + TRACE("done"); + + dcn_context->gc = 0; + TRACE("creating uv thread.."); + uv_thread_create(&dcn_context->event_handler_thread, event_handler_thread_func, dcn_context); + + NAPI_RETURN_UNDEFINED(); +} + + +NAPI_METHOD(dcn_context_unref) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + TRACE("Unrefing dc_context"); + dcn_context->gc = 1; + dc_context_unref(dcn_context->dc_context); + dcn_context->dc_context = NULL; + + NAPI_RETURN_UNDEFINED(); + +} + +/** + * Static functions + */ + +NAPI_METHOD(dcn_maybe_valid_addr) { + NAPI_ARGV(1); + NAPI_ARGV_UTF8_MALLOC(addr, 0); + + //TRACE("calling.."); + int result = dc_may_be_valid_addr(addr); + //TRACE("result %d", result); + + free(addr); + + NAPI_RETURN_INT32(result); +} + +/** + * dcn_context_t + */ + +NAPI_METHOD(dcn_add_address_book) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(address_book, 1); + + //TRACE("calling.."); + int result = dc_add_address_book(dcn_context->dc_context, address_book); + //TRACE("result %d", result); + + free(address_book); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_add_contact_to_chat) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UINT32(contact_id, 2); + + //TRACE("calling.."); + int result = dc_add_contact_to_chat(dcn_context->dc_context, + chat_id, contact_id); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_add_device_msg) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + + NAPI_ARGV_UTF8_MALLOC(label, 1); + + //TRACE("calling.."); + dc_msg_t* dc_msg = NULL; + napi_get_value_external(env, argv[2], (void**)&dc_msg); + + uint32_t msg_id = dc_add_device_msg(dcn_context->dc_context, label, dc_msg); + + free(label); + //TRACE("done"); + + NAPI_RETURN_UINT32(msg_id); +} + +NAPI_METHOD(dcn_block_contact) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(contact_id, 1); + NAPI_ARGV_INT32(new_blocking, 2); + + //TRACE("calling.."); + dc_block_contact(dcn_context->dc_context, contact_id, new_blocking); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_check_qr) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(qr, 1); + + //TRACE("calling.."); + dc_lot_t* lot = dc_check_qr(dcn_context->dc_context, qr); + + free(qr); + + napi_value result; + if (lot == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, lot, + finalize_lot, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + + +NAPI_METHOD(dcn_configure) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + TRACE("calling.."); + dc_configure(dcn_context->dc_context); + TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_accept_chat) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + dc_accept_chat(dcn_context->dc_context, chat_id); + + NAPI_RETURN_UNDEFINED() +} + +NAPI_METHOD(dcn_block_chat) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + dc_block_chat(dcn_context->dc_context, chat_id); + + NAPI_RETURN_UNDEFINED() +} + +NAPI_ASYNC_CARRIER_BEGIN(dcn_continue_key_transfer) + int msg_id; + char* setup_code; + int result; +NAPI_ASYNC_CARRIER_END(dcn_continue_key_transfer) + + +NAPI_ASYNC_EXECUTE(dcn_continue_key_transfer) { + NAPI_ASYNC_GET_CARRIER(dcn_continue_key_transfer) + carrier->result = dc_continue_key_transfer(carrier->dcn_context->dc_context, + carrier->msg_id, carrier->setup_code); +} + +NAPI_ASYNC_COMPLETE(dcn_continue_key_transfer) { + NAPI_ASYNC_GET_CARRIER(dcn_continue_key_transfer) + if (status != napi_ok) { + napi_throw_type_error(env, NULL, "Execute callback failed."); + return; + } + +#define DCN_CONTINUE_KEY_TRANSFER_CALLBACK_ARGC 1 + + const int argc = DCN_CONTINUE_KEY_TRANSFER_CALLBACK_ARGC; + napi_value argv[DCN_CONTINUE_KEY_TRANSFER_CALLBACK_ARGC]; + NAPI_STATUS_THROWS(napi_create_int32(env, carrier->result, &argv[0])); + + NAPI_ASYNC_CALL_AND_DELETE_CB() + dc_str_unref(carrier->setup_code); + free(carrier); +} + +NAPI_METHOD(dcn_continue_key_transfer) { + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + NAPI_ARGV_UTF8_MALLOC(setup_code, 2); + NAPI_ASYNC_NEW_CARRIER(dcn_continue_key_transfer); + carrier->msg_id = msg_id; + carrier->setup_code = setup_code; + + NAPI_ASYNC_QUEUE_WORK(dcn_continue_key_transfer, argv[3]); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_join_securejoin) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(qr_code, 1); + + uint32_t chat_id = dc_join_securejoin(dcn_context->dc_context, qr_code); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_create_chat_by_contact_id) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(contact_id, 1); + + //TRACE("calling.."); + uint32_t chat_id = dc_create_chat_by_contact_id(dcn_context->dc_context, contact_id); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_create_broadcast_list) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + uint32_t chat_id = dc_create_broadcast_list(dcn_context->dc_context); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_create_contact) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(name, 1); + NAPI_ARGV_UTF8_MALLOC(addr, 2); + + //TRACE("calling.."); + uint32_t contact_id = dc_create_contact(dcn_context->dc_context, name, addr); + //TRACE("result %d", contact_id); + + free(name); + free(addr); + + NAPI_RETURN_UINT32(contact_id); +} + +NAPI_METHOD(dcn_create_group_chat) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(protect, 1); + NAPI_ARGV_UTF8_MALLOC(chat_name, 2); + + //TRACE("calling.."); + uint32_t chat_id = dc_create_group_chat(dcn_context->dc_context, protect, chat_name); + //TRACE("result %d", chat_id); + + free(chat_name); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_delete_chat) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + dc_delete_chat(dcn_context->dc_context, chat_id); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_delete_contact) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(contact_id, 1); + + //TRACE("calling.."); + int result = dc_delete_contact(dcn_context->dc_context, contact_id); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_delete_msgs) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + napi_value js_array = argv[1]; + + //TRACE("calling.."); + uint32_t length; + uint32_t* msg_ids = js_array_to_uint32(env, js_array, &length); + dc_delete_msgs(dcn_context->dc_context, msg_ids, length); + free(msg_ids); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_forward_msgs) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + napi_value js_array = argv[1]; + NAPI_ARGV_UINT32(chat_id, 2); + + //TRACE("calling.."); + uint32_t length; + uint32_t* msg_ids = js_array_to_uint32(env, js_array, &length); + dc_forward_msgs(dcn_context->dc_context, msg_ids, length, chat_id); + free(msg_ids); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_get_blobdir) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + char* blobdir = dc_get_blobdir(dcn_context->dc_context); + //TRACE("result %s", blobdir); + + NAPI_RETURN_AND_UNREF_STRING(blobdir); +} + +NAPI_METHOD(dcn_get_blocked_cnt) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + int blocked_cnt = dc_get_blocked_cnt(dcn_context->dc_context); + //TRACE("result %d", blocked_cnt); + + NAPI_RETURN_INT32(blocked_cnt); +} + +NAPI_METHOD(dcn_get_blocked_contacts) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + dc_array_t* contacts = dc_get_blocked_contacts(dcn_context->dc_context); + napi_value js_array = dc_array_to_js_array(env, contacts); + dc_array_unref(contacts); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_chat) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + napi_value result; + dc_chat_t* chat = dc_get_chat(dcn_context->dc_context, chat_id); + + if (chat == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, chat, finalize_chat, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_get_chat_contacts) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + dc_array_t* contacts = dc_get_chat_contacts(dcn_context->dc_context, chat_id); + napi_value js_array = dc_array_to_js_array(env, contacts); + dc_array_unref(contacts); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_chat_encrinfo) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + char *value = dc_get_chat_encrinfo(dcn_context->dc_context, chat_id); + NAPI_RETURN_AND_UNREF_STRING(value); +} + +NAPI_METHOD(dcn_get_chat_id_by_contact_id) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(contact_id, 1); + + //TRACE("calling.."); + uint32_t chat_id = dc_get_chat_id_by_contact_id(dcn_context->dc_context, + contact_id); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_get_chat_media) { + NAPI_ARGV(5); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_INT32(msg_type1, 2); + NAPI_ARGV_INT32(msg_type2, 3); + NAPI_ARGV_INT32(msg_type3, 4); + + //TRACE("calling.."); + dc_array_t* msg_ids = dc_get_chat_media(dcn_context->dc_context, + chat_id, + msg_type1, + msg_type2, + msg_type3); + napi_value js_array = dc_array_to_js_array(env, msg_ids); + dc_array_unref(msg_ids); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_mime_headers) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + + //TRACE("calling.."); + char* headers = dc_get_mime_headers(dcn_context->dc_context, msg_id); + //TRACE("result %s", headers); + + NAPI_RETURN_AND_UNREF_STRING(headers); +} + +NAPI_METHOD(dcn_get_chat_msgs) { + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UINT32(flags, 2); + NAPI_ARGV_UINT32(marker1before, 3); + + //TRACE("calling.."); + dc_array_t* msg_ids = dc_get_chat_msgs(dcn_context->dc_context, + chat_id, + flags, + marker1before); + napi_value js_array = dc_array_to_js_array(env, msg_ids); + dc_array_unref(msg_ids); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_chatlist) { + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(listflags, 1); + NAPI_ARGV_UTF8_MALLOC(query, 2); + NAPI_ARGV_UINT32(query_contact_id, 3); + + //TRACE("calling.."); + dc_chatlist_t* chatlist = dc_get_chatlist(dcn_context->dc_context, + listflags, + query && query[0] ? query : NULL, + query_contact_id); + + free(query); + + napi_value result; + NAPI_STATUS_THROWS(napi_create_external(env, + chatlist, + finalize_chatlist, + NULL, + &result)); + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_get_config) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(key, 1); + + //TRACE("calling.."); + char *value = dc_get_config(dcn_context->dc_context, key); + //TRACE("result %s", value); + + free(key); + + NAPI_RETURN_AND_UNREF_STRING(value); +} + +NAPI_METHOD(dcn_get_contact) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(contact_id, 1); + + //TRACE("calling.."); + napi_value result; + dc_contact_t* contact = dc_get_contact(dcn_context->dc_context, contact_id); + + if (contact == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, contact, + finalize_contact, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_get_contact_encrinfo) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(contact_id, 1); + + //TRACE("calling.."); + char* encr_info = dc_get_contact_encrinfo(dcn_context->dc_context, + contact_id); + //TRACE("result %s", encr_info); + + NAPI_RETURN_AND_UNREF_STRING(encr_info); +} + +NAPI_METHOD(dcn_get_contacts) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(listflags, 1); + NAPI_ARGV_UTF8_MALLOC(query, 2); + + //TRACE("calling.."); + dc_array_t* contacts = dc_get_contacts(dcn_context->dc_context, listflags, + query && query[0] ? query : NULL); + napi_value js_array = dc_array_to_js_array(env, contacts); + free(query); + dc_array_unref(contacts); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_connectivity) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + int connectivity = dc_get_connectivity(dcn_context->dc_context); + NAPI_RETURN_INT32(connectivity); +} + +NAPI_METHOD(dcn_get_connectivity_html) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + char* connectivity = dc_get_connectivity_html(dcn_context->dc_context); + NAPI_RETURN_AND_UNREF_STRING(connectivity); +} + +NAPI_METHOD(dcn_was_device_msg_ever_added) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + + NAPI_ARGV_UTF8_MALLOC(label, 1); + + //TRACE("calling.."); + + uint32_t added = dc_was_device_msg_ever_added(dcn_context->dc_context, label); + + free(label); + //TRACE("done"); + + NAPI_RETURN_UINT32(added); +} + +NAPI_METHOD(dcn_get_draft) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + napi_value result; + dc_msg_t* draft = dc_get_draft(dcn_context->dc_context, chat_id); + + if (draft == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, draft, finalize_msg, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_get_fresh_msg_cnt) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + int msg_cnt = dc_get_fresh_msg_cnt(dcn_context->dc_context, chat_id); + //TRACE("result %d", msg_cnt); + + NAPI_RETURN_INT32(msg_cnt); +} + +NAPI_METHOD(dcn_get_fresh_msgs) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + dc_array_t* msg_ids = dc_get_fresh_msgs(dcn_context->dc_context); + napi_value js_array = dc_array_to_js_array(env, msg_ids); + dc_array_unref(msg_ids); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_get_info) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + char *str = dc_get_info(dcn_context->dc_context); + //TRACE("result %s", str); + + NAPI_RETURN_AND_UNREF_STRING(str); +} + +NAPI_METHOD(dcn_get_msg) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + + //TRACE("calling.."); + napi_value result; + dc_msg_t* msg = dc_get_msg(dcn_context->dc_context, msg_id); + + if (msg == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, msg, finalize_msg, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_get_msg_cnt) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + int msg_cnt = dc_get_msg_cnt(dcn_context->dc_context, chat_id); + //TRACE("result %d", msg_cnt); + + NAPI_RETURN_INT32(msg_cnt); +} + +NAPI_METHOD(dcn_get_msg_info) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + + //TRACE("calling.."); + char* msg_info = dc_get_msg_info(dcn_context->dc_context, msg_id); + //TRACE("result %s", msg_info); + + NAPI_RETURN_AND_UNREF_STRING(msg_info); +} + + +NAPI_METHOD(dcn_get_msg_html) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + + //TRACE("calling.."); + char* msg_html = dc_get_msg_html(dcn_context->dc_context, msg_id); + //TRACE("result %s", msg_html); + + NAPI_RETURN_AND_UNREF_STRING(msg_html); +} + +NAPI_METHOD(dcn_get_next_media) { + NAPI_ARGV(6); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + NAPI_ARGV_INT32(dir, 2); + NAPI_ARGV_INT32(msg_type1, 3); + NAPI_ARGV_INT32(msg_type2, 4); + NAPI_ARGV_INT32(msg_type3, 5); + + //TRACE("calling.."); + uint32_t next_id = dc_get_next_media(dcn_context->dc_context, + msg_id, + dir, + msg_type1, + msg_type2, + msg_type3); + //TRACE("result %d", next_id); + + NAPI_RETURN_UINT32(next_id); +} + +NAPI_METHOD(dcn_set_chat_visibility) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_INT32(visibility, 2); + //TRACE("calling.."); + dc_set_chat_visibility(dcn_context->dc_context, + chat_id, + visibility); + //TRACE("result %d", next_id); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_get_securejoin_qr) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(group_chat_id, 1); + + //TRACE("calling.."); + char* code = dc_get_securejoin_qr(dcn_context->dc_context, + group_chat_id); + //TRACE("result %s", code); + + NAPI_RETURN_AND_UNREF_STRING(code); +} + +NAPI_METHOD(dcn_get_securejoin_qr_svg) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(group_chat_id, 1); + + //TRACE("calling.."); + char* svg = dc_get_securejoin_qr_svg(dcn_context->dc_context, group_chat_id); + //TRACE("result %s", code); + + NAPI_RETURN_AND_UNREF_STRING(svg); +} + +NAPI_METHOD(dcn_imex) { + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(what, 1); + NAPI_ARGV_UTF8_MALLOC(param1, 2); + NAPI_ARGV_UTF8_MALLOC(param2, 3); + + TRACE("calling.."); + dc_imex(dcn_context->dc_context, + what, + param1, + param2 && param2[0] ? param2 : NULL); + + free(param1); + free(param2); + TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_imex_has_backup) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(dir_name, 1); + + //TRACE("calling.."); + char* file = dc_imex_has_backup(dcn_context->dc_context, dir_name); + //TRACE("result %s", file); + + free(dir_name); + + NAPI_RETURN_AND_UNREF_STRING(file); +} + +NAPI_ASYNC_CARRIER_BEGIN(dcn_initiate_key_transfer) + char* result; +NAPI_ASYNC_CARRIER_END(dcn_initiate_key_transfer) + +NAPI_ASYNC_EXECUTE(dcn_initiate_key_transfer) { + NAPI_ASYNC_GET_CARRIER(dcn_initiate_key_transfer); + carrier->result = dc_initiate_key_transfer(carrier->dcn_context->dc_context); +} + +NAPI_ASYNC_COMPLETE(dcn_initiate_key_transfer) { + NAPI_ASYNC_GET_CARRIER(dcn_initiate_key_transfer); + if (status != napi_ok) { + napi_throw_type_error(env, NULL, "Execute callback failed."); + return; + } + +#define DCN_INITIATE_KEY_TRANSFER_CALLBACK_ARGC 1 + + const int argc = DCN_INITIATE_KEY_TRANSFER_CALLBACK_ARGC; + napi_value argv[DCN_INITIATE_KEY_TRANSFER_CALLBACK_ARGC]; + + if (carrier->result) { + NAPI_STATUS_THROWS(napi_create_string_utf8(env, carrier->result, NAPI_AUTO_LENGTH, &argv[0])); + } else { + NAPI_STATUS_THROWS(napi_get_null(env, &argv[0])); + } + + NAPI_ASYNC_CALL_AND_DELETE_CB(); + dc_str_unref(carrier->result); + free(carrier); +} + +NAPI_METHOD(dcn_initiate_key_transfer) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + + NAPI_ASYNC_NEW_CARRIER(dcn_initiate_key_transfer); + + NAPI_ASYNC_QUEUE_WORK(dcn_initiate_key_transfer, argv[1]); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_is_configured) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + int result = dc_is_configured(dcn_context->dc_context); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_is_contact_in_chat) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UINT32(contact_id, 2); + + //TRACE("calling.."); + int result = dc_is_contact_in_chat(dcn_context->dc_context, + chat_id, contact_id); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_lookup_contact_id_by_addr) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(addr, 1); + + //TRACE("calling.."); + uint32_t res = dc_lookup_contact_id_by_addr(dcn_context->dc_context, addr); + //TRACE("result %d", res); + + free(addr); + + NAPI_RETURN_UINT32(res); +} + +NAPI_METHOD(dcn_marknoticed_chat) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + dc_marknoticed_chat(dcn_context->dc_context, chat_id); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_download_full_msg) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + + //TRACE("calling.."); + dc_download_full_msg(dcn_context->dc_context, msg_id); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_markseen_msgs) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + napi_value js_array = argv[1]; + + //TRACE("calling.."); + uint32_t length; + uint32_t* msg_ids = js_array_to_uint32(env, js_array, &length); + dc_markseen_msgs(dcn_context->dc_context, msg_ids, length); + free(msg_ids); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_maybe_network) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + dc_maybe_network(dcn_context->dc_context); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_new) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(viewtype, 1); + + //TRACE("calling.."); + napi_value result; + dc_msg_t* msg = dc_msg_new(dcn_context->dc_context, viewtype); + + NAPI_STATUS_THROWS(napi_create_external(env, msg, finalize_msg, + NULL, &result)); + //TRACE("done"); + + return result; +} + + +NAPI_METHOD(dcn_remove_contact_from_chat) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UINT32(contact_id, 2); + + //TRACE("calling.."); + int result = dc_remove_contact_from_chat(dcn_context->dc_context, + chat_id, contact_id); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_search_msgs) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UTF8_MALLOC(query, 2); + + //TRACE("calling.."); + dc_array_t* msg_ids = dc_search_msgs(dcn_context->dc_context, + chat_id, query); + napi_value js_array = dc_array_to_js_array(env, msg_ids); + dc_array_unref(msg_ids); + free(query); + //TRACE("done"); + + return js_array; +} + +NAPI_METHOD(dcn_send_msg) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + dc_msg_t* dc_msg = NULL; + napi_get_value_external(env, argv[2], (void**)&dc_msg); + + uint32_t msg_id = dc_send_msg(dcn_context->dc_context, chat_id, dc_msg); + //TRACE("done"); + + NAPI_RETURN_UINT32(msg_id); +} + +NAPI_ASYNC_CARRIER_BEGIN(dcn_send_videochat_invitation) + int chat_id; + int result; +NAPI_ASYNC_CARRIER_END(dcn_send_videochat_invitation) + +NAPI_ASYNC_EXECUTE(dcn_send_videochat_invitation) { + NAPI_ASYNC_GET_CARRIER(dcn_send_videochat_invitation) + carrier->result = dc_send_videochat_invitation( + carrier->dcn_context->dc_context, + carrier->chat_id + ); +} + +NAPI_ASYNC_COMPLETE(dcn_send_videochat_invitation) { + NAPI_ASYNC_GET_CARRIER(dcn_send_videochat_invitation) + if (status != napi_ok) { + napi_throw_type_error(env, NULL, "Execute callback failed."); + return; + } + +#define DCN_SEND_VIDEO_CHAT_CALLBACK_ARGC 1 + + const int argc = DCN_SEND_VIDEO_CHAT_CALLBACK_ARGC; + napi_value argv[DCN_SEND_VIDEO_CHAT_CALLBACK_ARGC]; + NAPI_STATUS_THROWS(napi_create_int32(env, carrier->result, &argv[0])); + + NAPI_ASYNC_CALL_AND_DELETE_CB() + free(carrier); +} + +NAPI_METHOD(dcn_send_videochat_invitation) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ASYNC_NEW_CARRIER(dcn_send_videochat_invitation); + carrier->chat_id = chat_id; + + NAPI_ASYNC_QUEUE_WORK(dcn_send_videochat_invitation, argv[2]); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_set_chat_name) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UTF8_MALLOC(name, 2); + + //TRACE("calling.."); + int result = dc_set_chat_name(dcn_context->dc_context, + chat_id, + name); + //TRACE("result %d", result); + + free(name); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_set_chat_protection) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_INT32(protect, 1); + + int result = dc_set_chat_protection(dcn_context->dc_context, + chat_id, + protect); + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_get_chat_ephemeral_timer) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + uint32_t result = dc_get_chat_ephemeral_timer(dcn_context->dc_context, + chat_id); + NAPI_RETURN_UINT32(result); +} + +NAPI_METHOD(dcn_set_chat_ephemeral_timer) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UINT32(timer, 2); + + int result = dc_set_chat_ephemeral_timer(dcn_context->dc_context, + chat_id, + timer); + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_set_chat_profile_image) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_UTF8_MALLOC(image, 2); + + //TRACE("calling.."); + int result = dc_set_chat_profile_image(dcn_context->dc_context, + chat_id, + image && image[0] ? image : NULL); + //TRACE("result %d", result); + + free(image); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_set_chat_mute_duration) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + NAPI_ARGV_INT32(duration, 2); + + //TRACE("calling.."); + int result = dc_set_chat_mute_duration(dcn_context->dc_context, + chat_id, + duration); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_set_config) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(key, 1); + NAPI_ARGV_UTF8_MALLOC(value, 2); + + //TRACE("calling.."); + int status = dc_set_config(dcn_context->dc_context, key, value); + //TRACE("result %d", status); + + free(key); + free(value); + + NAPI_RETURN_INT32(status); +} + +NAPI_METHOD(dcn_set_config_null) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(key, 1); + + //TRACE("calling.."); + int status = dc_set_config(dcn_context->dc_context, key, NULL); + //TRACE("result %d", status); + + free(key); + + NAPI_RETURN_INT32(status); +} + +NAPI_METHOD(dcn_set_config_from_qr) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(qr, 1); + + //TRACE("calling.."); + int status = dc_set_config_from_qr(dcn_context->dc_context, qr); + //TRACE("result %d", status); + + free(qr); + + NAPI_RETURN_INT32(status); +} + +NAPI_METHOD(dcn_estimate_deletion_cnt) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(from_server, 1); + NAPI_ARGV_INT32(seconds, 2); + + int result = dc_estimate_deletion_cnt (dcn_context->dc_context, from_server, seconds); + + NAPI_RETURN_INT32(result); +} + + +NAPI_METHOD(dcn_set_draft) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(chat_id, 1); + + //TRACE("calling.."); + dc_msg_t* dc_msg = NULL; + napi_get_value_external(env, argv[2], (void**)&dc_msg); + + dc_set_draft(dcn_context->dc_context, chat_id, dc_msg); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_set_stock_translation) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(stock_id, 1); + NAPI_ARGV_UTF8_MALLOC(stock_msg, 2); + + int result = dc_set_stock_translation(dcn_context->dc_context, stock_id, stock_msg); + free(stock_msg); + NAPI_RETURN_INT32(result); +} + + +NAPI_METHOD(dcn_start_io) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + TRACE("calling.."); + TRACE("done"); + + dc_start_io(dcn_context->dc_context); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_stop_io) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + dc_stop_io(dcn_context->dc_context); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_stop_ongoing_process) { + NAPI_ARGV(1); + NAPI_DCN_CONTEXT(); + + //TRACE("calling.."); + dc_stop_ongoing_process(dcn_context->dc_context); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +/** + * dc_chat_t + */ + +NAPI_METHOD(dcn_chat_get_color) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + uint32_t color = dc_chat_get_color(dc_chat); + //TRACE("result %d", color); + + NAPI_RETURN_UINT32(color); +} + +NAPI_METHOD(dcn_chat_get_visibility) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + uint32_t visibility = dc_chat_get_visibility(dc_chat); + //TRACE("result %d", color); + + NAPI_RETURN_UINT32(visibility); +} + +NAPI_METHOD(dcn_chat_get_id) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + uint32_t chat_id = dc_chat_get_id(dc_chat); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_chat_get_name) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + char* name = dc_chat_get_name(dc_chat); + //TRACE("result %s", name); + + NAPI_RETURN_AND_UNREF_STRING(name); +} + +NAPI_METHOD(dcn_chat_get_profile_image) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + char* profile_image = dc_chat_get_profile_image(dc_chat); + //TRACE("result %s", profile_image); + + NAPI_RETURN_AND_UNREF_STRING(profile_image); +} + +NAPI_METHOD(dcn_chat_get_type) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int type = dc_chat_get_type(dc_chat); + //TRACE("result %d", type); + + NAPI_RETURN_INT32(type); +} + +NAPI_METHOD(dcn_chat_is_self_talk) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_self_talk = dc_chat_is_self_talk(dc_chat); + //TRACE("result %d", is_self_talk); + + NAPI_RETURN_INT32(is_self_talk); +} + +NAPI_METHOD(dcn_chat_is_unpromoted) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_unpromoted = dc_chat_is_unpromoted(dc_chat); + //TRACE("result %d", is_unpromoted); + + NAPI_RETURN_INT32(is_unpromoted); +} + +NAPI_METHOD(dcn_chat_can_send) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int can_send = dc_chat_can_send(dc_chat); + //TRACE("result %d", can_send); + + NAPI_RETURN_INT32(can_send); +} + +NAPI_METHOD(dcn_chat_is_protected) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_protected = dc_chat_is_protected(dc_chat); + //TRACE("result %d", is_protected); + + NAPI_RETURN_INT32(is_protected); +} + +NAPI_METHOD(dcn_chat_is_device_talk) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_device_talk = dc_chat_is_device_talk(dc_chat); + //TRACE("result %d", is_device_talk); + + NAPI_RETURN_INT32(is_device_talk); +} + +NAPI_METHOD(dcn_chat_is_muted) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_muted = dc_chat_is_muted(dc_chat); + //TRACE("result %d", is_muted); + + NAPI_RETURN_INT32(is_muted); +} + +NAPI_METHOD(dcn_chat_is_contact_request) { + NAPI_ARGV(1); + NAPI_DC_CHAT(); + + //TRACE("calling.."); + int is_contact_request = dc_chat_is_contact_request(dc_chat); + //TRACE("result %d", is_muted); + + NAPI_RETURN_INT32(is_contact_request); +} + + + +/** + * dc_chatlist_t + */ + +NAPI_METHOD(dcn_chatlist_get_chat_id) { + NAPI_ARGV(2); + NAPI_DC_CHATLIST(); + NAPI_ARGV_INT32(index, 1); + + //TRACE("calling.."); + uint32_t chat_id = dc_chatlist_get_chat_id(dc_chatlist, index); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_chatlist_get_cnt) { + NAPI_ARGV(1); + NAPI_DC_CHATLIST(); + + //TRACE("calling.."); + int count = dc_chatlist_get_cnt(dc_chatlist); + //TRACE("result %d", count); + + NAPI_RETURN_INT32(count); +} + +NAPI_METHOD(dcn_chatlist_get_msg_id) { + NAPI_ARGV(2); + NAPI_DC_CHATLIST(); + NAPI_ARGV_INT32(index, 1); + + //TRACE("calling.."); + uint32_t message_id = dc_chatlist_get_msg_id(dc_chatlist, index); + //TRACE("result %d", message_id); + + NAPI_RETURN_UINT32(message_id); +} + +NAPI_METHOD(dcn_chatlist_get_summary) { + NAPI_ARGV(3); + NAPI_DC_CHATLIST(); + NAPI_ARGV_INT32(index, 1); + + //TRACE("calling.."); + dc_chat_t* dc_chat = NULL; + napi_get_value_external(env, argv[2], (void**)&dc_chat); + + dc_lot_t* summary = dc_chatlist_get_summary(dc_chatlist, index, dc_chat); + + napi_value result; + if (summary == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, summary, + finalize_lot, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_chatlist_get_summary2) { + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(chat_id, 1); + NAPI_ARGV_INT32(message_id, 2); + + //TRACE("calling.."); + dc_lot_t* summary = dc_chatlist_get_summary2(dcn_context->dc_context, chat_id, message_id); + + napi_value result; + if (summary == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, summary, + finalize_lot, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +/** + * dc_contact_t + */ + +NAPI_METHOD(dcn_contact_get_addr) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + char* addr = dc_contact_get_addr(dc_contact); + //TRACE("result %s", addr); + + NAPI_RETURN_AND_UNREF_STRING(addr); +} + +NAPI_METHOD(dcn_contact_get_auth_name) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + char* auth_name = dc_contact_get_auth_name(dc_contact); + + NAPI_RETURN_AND_UNREF_STRING(auth_name); +} + +NAPI_METHOD(dcn_contact_get_color) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + uint32_t color = dc_contact_get_color(dc_contact); + //TRACE("result %d", color); + + NAPI_RETURN_UINT32(color); +} + +NAPI_METHOD(dcn_contact_get_display_name) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + char* display_name = dc_contact_get_display_name(dc_contact); + //TRACE("result %s", display_name); + + NAPI_RETURN_AND_UNREF_STRING(display_name); +} + +NAPI_METHOD(dcn_contact_get_id) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + uint32_t contact_id = dc_contact_get_id(dc_contact); + //TRACE("result %d", contact_id); + + NAPI_RETURN_UINT32(contact_id); +} + +NAPI_METHOD(dcn_contact_get_name) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + char* name = dc_contact_get_name(dc_contact); + //TRACE("result %s", name); + + NAPI_RETURN_AND_UNREF_STRING(name); +} + +NAPI_METHOD(dcn_contact_get_name_n_addr) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + char* name_n_addr = dc_contact_get_name_n_addr(dc_contact); + //TRACE("result %s", name_n_addr); + + NAPI_RETURN_AND_UNREF_STRING(name_n_addr); +} + +NAPI_METHOD(dcn_contact_get_profile_image) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + char* profile_image = dc_contact_get_profile_image(dc_contact); + //TRACE("result %s", profile_image); + + NAPI_RETURN_AND_UNREF_STRING(profile_image); +} + +NAPI_METHOD(dcn_contact_get_status) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + char* status = dc_contact_get_status(dc_contact); + NAPI_RETURN_AND_UNREF_STRING(status); +} + +NAPI_METHOD(dcn_contact_get_last_seen) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + int64_t timestamp = dc_contact_get_last_seen(dc_contact); + NAPI_RETURN_INT64(timestamp); +} + +NAPI_METHOD(dcn_contact_is_blocked) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + int is_blocked = dc_contact_is_blocked(dc_contact); + //TRACE("result %d", is_blocked); + + NAPI_RETURN_UINT32(is_blocked); +} + +NAPI_METHOD(dcn_contact_is_verified) { + NAPI_ARGV(1); + NAPI_DC_CONTACT(); + + //TRACE("calling.."); + int is_verified = dc_contact_is_verified(dc_contact); + //TRACE("result %d", is_verified); + + NAPI_RETURN_UINT32(is_verified); +} + +/** + * dc_lot_t + */ + +NAPI_METHOD(dcn_lot_get_id) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + uint32_t id = dc_lot_get_id(dc_lot); + //TRACE("result %d", id); + + NAPI_RETURN_UINT32(id); +} + +NAPI_METHOD(dcn_lot_get_state) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + int state = dc_lot_get_state(dc_lot); + //TRACE("result %d", state); + + NAPI_RETURN_INT32(state); +} + +NAPI_METHOD(dcn_lot_get_text1) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + char* text1 = dc_lot_get_text1(dc_lot); + //TRACE("result %s", text1); + + NAPI_RETURN_AND_UNREF_STRING(text1); +} + +NAPI_METHOD(dcn_lot_get_text1_meaning) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + int text1_meaning = dc_lot_get_text1_meaning(dc_lot); + //TRACE("result %d", text1_meaning); + + NAPI_RETURN_INT32(text1_meaning); +} + +NAPI_METHOD(dcn_lot_get_text2) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + char* text2 = dc_lot_get_text2(dc_lot); + //TRACE("result %s", text2); + + NAPI_RETURN_AND_UNREF_STRING(text2); +} + +NAPI_METHOD(dcn_lot_get_timestamp) { + NAPI_ARGV(1); + NAPI_DC_LOT(); + + //TRACE("calling.."); + int timestamp = dc_lot_get_timestamp(dc_lot); + //TRACE("result %d", timestamp); + + NAPI_RETURN_INT32(timestamp); +} + +/** + * dc_msg_t + */ + +NAPI_METHOD(dcn_msg_get_parent) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + napi_value result; + dc_msg_t* msg = dc_msg_get_parent(dc_msg); + + if (msg == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, msg, finalize_msg, + NULL, &result)); + } + + return result; +} + +NAPI_METHOD(dcn_msg_get_download_state) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + uint32_t download_state = dc_msg_get_download_state(dc_msg); + //TRACE("result %d", download_state); + + NAPI_RETURN_UINT32(download_state); +} + +NAPI_METHOD(dcn_msg_get_chat_id) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + uint32_t chat_id = dc_msg_get_chat_id(dc_msg); + //TRACE("result %d", chat_id); + + NAPI_RETURN_UINT32(chat_id); +} + +NAPI_METHOD(dcn_msg_get_duration) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int duration = dc_msg_get_duration(dc_msg); + //TRACE("result %d", duration); + + NAPI_RETURN_INT32(duration); +} + +NAPI_METHOD(dcn_msg_get_file) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* file = dc_msg_get_file(dc_msg); + //TRACE("result %s", file); + + NAPI_RETURN_AND_UNREF_STRING(file); +} + +NAPI_METHOD(dcn_msg_get_filebytes) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + uint32_t filebytes = dc_msg_get_filebytes(dc_msg); + //TRACE("result %d", filebytes); + + NAPI_RETURN_INT32(filebytes); +} + +NAPI_METHOD(dcn_msg_get_filemime) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* filemime = dc_msg_get_filemime(dc_msg); + //TRACE("result %s", filemime); + + NAPI_RETURN_AND_UNREF_STRING(filemime); +} + +NAPI_METHOD(dcn_msg_get_filename) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* filename = dc_msg_get_filename(dc_msg); + //TRACE("result %s", filename); + + NAPI_RETURN_AND_UNREF_STRING(filename); +} + +NAPI_METHOD(dcn_msg_get_from_id) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + uint32_t contact_id = dc_msg_get_from_id(dc_msg); + //TRACE("result %d", contact_id); + + NAPI_RETURN_UINT32(contact_id); +} + +NAPI_METHOD(dcn_msg_get_height) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int height = dc_msg_get_height(dc_msg); + //TRACE("result %d", height); + + NAPI_RETURN_INT32(height); +} + +NAPI_METHOD(dcn_msg_get_id) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + uint32_t msg_id = dc_msg_get_id(dc_msg); + //TRACE("result %d", msg_id); + + NAPI_RETURN_UINT32(msg_id); +} + +NAPI_METHOD(dcn_msg_get_override_sender_name) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* override_sender_name = dc_msg_get_override_sender_name(dc_msg); + //TRACE("result %s", override_sender_name); + + NAPI_RETURN_AND_UNREF_STRING(override_sender_name); +} + +NAPI_METHOD(dcn_msg_get_quoted_text) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* text = dc_msg_get_quoted_text(dc_msg); + //TRACE("result %s", text); + + NAPI_RETURN_AND_UNREF_STRING(text); +} + +NAPI_METHOD(dcn_msg_get_quoted_msg) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + napi_value result; + dc_msg_t* msg = dc_msg_get_quoted_msg(dc_msg); + + if (msg == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, msg, finalize_msg, + NULL, &result)); + } + + return result; +} + +NAPI_METHOD(dcn_msg_get_received_timestamp) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int timestamp = dc_msg_get_received_timestamp(dc_msg); + //TRACE("result %d", timestamp); + + NAPI_RETURN_INT32(timestamp); +} + + +NAPI_METHOD(dcn_msg_get_setupcodebegin) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* setupcodebegin = dc_msg_get_setupcodebegin(dc_msg); + //TRACE("result %s", setupcodebegin); + + NAPI_RETURN_AND_UNREF_STRING(setupcodebegin); +} + +NAPI_METHOD(dcn_msg_get_showpadlock) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int showpadlock = dc_msg_get_showpadlock(dc_msg); + //TRACE("result %d", showpadlock); + + NAPI_RETURN_INT32(showpadlock); +} + +NAPI_METHOD(dcn_msg_get_sort_timestamp) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int timestamp = dc_msg_get_sort_timestamp(dc_msg); + //TRACE("result %d", timestamp); + + NAPI_RETURN_INT32(timestamp); +} + +NAPI_METHOD(dcn_msg_get_state) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int state = dc_msg_get_state(dc_msg); + //TRACE("result %d", state); + + NAPI_RETURN_INT32(state); +} + +NAPI_METHOD(dcn_msg_get_summary) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + + //TRACE("calling.."); + dc_chat_t* dc_chat = NULL; + napi_get_value_external(env, argv[1], (void**)&dc_chat); + + dc_lot_t* summary = dc_msg_get_summary(dc_msg, dc_chat); + + napi_value result; + NAPI_STATUS_THROWS(napi_create_external(env, summary, + finalize_lot, + NULL, &result)); + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_msg_get_summarytext) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_INT32(approx_characters, 1); + + //TRACE("calling.."); + char* summarytext = dc_msg_get_summarytext(dc_msg, approx_characters); + //TRACE("result %s", summarytext); + + NAPI_RETURN_AND_UNREF_STRING(summarytext); +} + +NAPI_METHOD(dcn_msg_get_subject) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* subject = dc_msg_get_subject(dc_msg); + //TRACE("result %s", subject); + + NAPI_RETURN_AND_UNREF_STRING(subject); +} + +NAPI_METHOD(dcn_msg_get_text) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + char* text = dc_msg_get_text(dc_msg); + //TRACE("result %s", text); + + NAPI_RETURN_AND_UNREF_STRING(text); +} + +NAPI_METHOD(dcn_msg_get_timestamp) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int timestamp = dc_msg_get_timestamp(dc_msg); + //TRACE("result %d", timestamp); + + NAPI_RETURN_INT32(timestamp); +} + +NAPI_METHOD(dcn_msg_get_viewtype) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int type = dc_msg_get_viewtype(dc_msg); + //TRACE("result %d", type); + + NAPI_RETURN_INT32(type); +} + +NAPI_METHOD(dcn_msg_get_videochat_type) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + int type = dc_msg_get_videochat_type(dc_msg); + NAPI_RETURN_INT32(type); +} + +NAPI_METHOD(dcn_msg_get_videochat_url) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + char* url = dc_msg_get_videochat_url(dc_msg); + NAPI_RETURN_AND_UNREF_STRING(url); +} + +NAPI_METHOD(dcn_msg_get_width) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int width = dc_msg_get_width(dc_msg); + //TRACE("result %d", width); + + NAPI_RETURN_INT32(width); +} + +NAPI_METHOD(dcn_msg_get_webxdc_info){ + NAPI_ARGV(1); + NAPI_DC_MSG(); + + char* result_json = dc_msg_get_webxdc_info(dc_msg); + + NAPI_RETURN_AND_UNREF_STRING(result_json); +} + +NAPI_METHOD(dcn_msg_has_deviating_timestamp) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int has_deviating_timestamp = dc_msg_has_deviating_timestamp(dc_msg); + //TRACE("result %d", has_deviating_timestamp); + + NAPI_RETURN_INT32(has_deviating_timestamp); +} + +NAPI_METHOD(dcn_msg_has_location) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int has_location = dc_msg_has_location(dc_msg); + //TRACE("result %d", has_location); + + NAPI_RETURN_INT32(has_location); +} + +NAPI_METHOD(dcn_msg_has_html) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int has_html = dc_msg_has_html(dc_msg); + //TRACE("result %d", has_html); + + NAPI_RETURN_INT32(has_html); +} + +NAPI_METHOD(dcn_msg_is_forwarded) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int is_forwarded = dc_msg_is_forwarded(dc_msg); + //TRACE("result %d", is_forwarded); + + NAPI_RETURN_INT32(is_forwarded); +} + +NAPI_METHOD(dcn_msg_is_increation) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int is_increation = dc_msg_is_increation(dc_msg); + //TRACE("result %d", is_increation); + + NAPI_RETURN_INT32(is_increation); +} + +NAPI_METHOD(dcn_msg_is_info) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int is_info = dc_msg_is_info(dc_msg); + //TRACE("result %d", is_info); + + NAPI_RETURN_INT32(is_info); +} + +NAPI_METHOD(dcn_msg_is_sent) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int is_sent = dc_msg_is_sent(dc_msg); + //TRACE("result %d", is_sent); + + NAPI_RETURN_INT32(is_sent); +} + +NAPI_METHOD(dcn_msg_is_setupmessage) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + + //TRACE("calling.."); + int is_setupmessage = dc_msg_is_setupmessage(dc_msg); + //TRACE("result %d", is_setupmessage); + + NAPI_RETURN_INT32(is_setupmessage); +} + +NAPI_METHOD(dcn_msg_latefiling_mediasize) { + NAPI_ARGV(4); + NAPI_DC_MSG(); + NAPI_ARGV_INT32(width, 1); + NAPI_ARGV_INT32(height, 2); + NAPI_ARGV_INT32(duration, 3); + + //TRACE("calling.."); + dc_msg_latefiling_mediasize(dc_msg, width, height, duration); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + + +NAPI_METHOD(dcn_msg_force_plaintext) { + NAPI_ARGV(1); + NAPI_DC_MSG(); + dc_msg_force_plaintext(dc_msg); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_dimension) { + NAPI_ARGV(3); + NAPI_DC_MSG(); + NAPI_ARGV_INT32(width, 1); + NAPI_ARGV_INT32(height, 2); + + //TRACE("calling.."); + dc_msg_set_dimension(dc_msg, width, height); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_duration) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_INT32(duration, 1); + + //TRACE("calling.."); + dc_msg_set_duration(dc_msg, duration); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_override_sender_name) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_UTF8_MALLOC(override_sender_name, 1); + + //TRACE("calling.."); + dc_msg_set_override_sender_name(dc_msg, override_sender_name); + //TRACE("done"); + + free(override_sender_name); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_file) { + NAPI_ARGV(3); + NAPI_DC_MSG(); + NAPI_ARGV_UTF8_MALLOC(file, 1); + NAPI_ARGV_UTF8_MALLOC(filemime, 2); + + //TRACE("calling.."); + dc_msg_set_file(dc_msg, file, filemime && filemime[0] ? filemime : NULL); + //TRACE("done"); + + free(file); + free(filemime); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_html) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_UTF8_MALLOC(html, 1); + + //TRACE("calling.."); + dc_msg_set_html(dc_msg, html); + //TRACE("done"); + + free(html); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_quote) { + NAPI_ARGV(2); + NAPI_ARGV_DC_MSG(dc_msg, 0) + + dc_msg_t* dc_msg_quote = NULL; + napi_get_value_external(env, argv[1], (void**)&dc_msg_quote); + + dc_msg_set_quote(dc_msg, dc_msg_quote); + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_msg_set_text) { + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_UTF8_MALLOC(text, 1); + + //TRACE("calling.."); + dc_msg_set_text(dc_msg, text); + //TRACE("done"); + + free(text); + + NAPI_RETURN_UNDEFINED(); +} + +/** + * locations + */ + +NAPI_METHOD(dcn_msg_set_location) { + NAPI_ARGV(3); + NAPI_DC_MSG(); + NAPI_ARGV_DOUBLE(latitude, 1); + NAPI_ARGV_DOUBLE(longitude, 2); + + //TRACE("calling.."); + dc_msg_set_location(dc_msg, latitude, longitude); + //TRACE("done"); + + NAPI_RETURN_UNDEFINED(); +} + +/** + * locations + */ + +NAPI_METHOD(dcn_set_location) { + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_DOUBLE(latitude, 1); + NAPI_ARGV_DOUBLE(longitude, 2); + NAPI_ARGV_DOUBLE(accuracy, 3); + + //TRACE("calling.."); + int result = dc_set_location(dcn_context->dc_context, latitude, longitude, accuracy); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_get_locations) { + NAPI_ARGV(5); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_INT32(chat_id, 1); + NAPI_ARGV_INT32(contact_id, 2); + NAPI_ARGV_INT32(timestamp_from, 3); + NAPI_ARGV_INT32(timestamp_to, 4); + + //TRACE("calling.."); + dc_array_t* locations = dc_get_locations(dcn_context->dc_context, + chat_id, + contact_id, + timestamp_from, + timestamp_to); + + napi_value napi_locations; + NAPI_STATUS_THROWS(napi_create_external(env, locations, + finalize_array, + NULL, &napi_locations)); + //TRACE("done"); + + return napi_locations; +} + +NAPI_METHOD(dcn_array_get_cnt) { + NAPI_ARGV(1); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t size = dc_array_get_cnt(dc_array); + + napi_value napi_size; + NAPI_STATUS_THROWS(napi_create_uint32(env, size, &napi_size)); + //TRACE("done"); + + return napi_size; +} + +NAPI_METHOD(dcn_array_get_id) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + uint32_t id = dc_array_get_id(dc_array, index); + + napi_value napi_id; + NAPI_STATUS_THROWS(napi_create_uint32(env, id, &napi_id)); + //TRACE("done"); + + return napi_id; +} + +NAPI_METHOD(dcn_array_get_accuracy) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + double accuracy = dc_array_get_accuracy(dc_array, index); + + napi_value napi_accuracy; + NAPI_STATUS_THROWS(napi_create_double(env, accuracy, &napi_accuracy)); + //TRACE("done"); + + return napi_accuracy; +} + +NAPI_METHOD(dcn_array_get_longitude) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + double longitude = dc_array_get_longitude(dc_array, index); + + napi_value napi_longitude; + NAPI_STATUS_THROWS(napi_create_double(env, longitude, &napi_longitude)); + //TRACE("done"); + + return napi_longitude; +} + +NAPI_METHOD(dcn_array_get_latitude) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + double latitude = dc_array_get_latitude(dc_array, index); + + napi_value napi_latitude; + NAPI_STATUS_THROWS(napi_create_double(env, latitude, &napi_latitude)); + //TRACE("done"); + + return napi_latitude; +} + +NAPI_METHOD(dcn_array_get_timestamp) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + int timestamp = dc_array_get_timestamp(dc_array, index); + + napi_value napi_timestamp; + NAPI_STATUS_THROWS(napi_create_int64(env, timestamp, &napi_timestamp)); + //TRACE("done"); + + return napi_timestamp; +} + +NAPI_METHOD(dcn_array_get_msg_id) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + uint32_t msg_id = dc_array_get_msg_id(dc_array, index); + + napi_value napi_msg_id; + NAPI_STATUS_THROWS(napi_create_uint32(env, msg_id, &napi_msg_id)); + //TRACE("done"); + + return napi_msg_id; +} + +NAPI_METHOD(dcn_array_is_independent) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + int result = dc_array_is_independent(dc_array, index); + //TRACE("result %d", result); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_array_get_marker) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + char* marker = dc_array_get_marker(dc_array, index); + //TRACE("result %s", marker); + + NAPI_RETURN_AND_UNREF_STRING(marker); +} + +NAPI_METHOD(dcn_array_get_contact_id) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + uint32_t contact_id = dc_array_get_contact_id(dc_array, index); + + napi_value napi_contact_id; + NAPI_STATUS_THROWS(napi_create_uint32(env, contact_id, &napi_contact_id)); + //TRACE("done"); + + return napi_contact_id; +} + +NAPI_METHOD(dcn_array_get_chat_id) { + NAPI_ARGV(2); + NAPI_DC_ARRAY(); + + //TRACE("calling.."); + uint32_t index; + NAPI_STATUS_THROWS(napi_get_value_uint32(env, argv[1], &index)); + + uint32_t chat_id = dc_array_get_chat_id(dc_array, index); + + napi_value napi_chat_id; + NAPI_STATUS_THROWS(napi_create_uint32(env, chat_id, &napi_chat_id)); + //TRACE("done"); + + return napi_chat_id; +} + +NAPI_METHOD(dcn_provider_new_from_email) { + NAPI_ARGV(2); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UTF8_MALLOC(email, 1) + + //TRACE("calling.."); + napi_value result; + dc_provider_t* provider = dc_provider_new_from_email(dcn_context->dc_context, email); + + if (provider == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + NAPI_STATUS_THROWS(napi_create_external(env, provider, finalize_provider, + NULL, &result)); + } + //TRACE("done"); + + return result; +} + +NAPI_METHOD(dcn_provider_get_overview_page) { + NAPI_ARGV(1); + NAPI_DC_PROVIDER(); + + //TRACE("calling.."); + char* overview_page = dc_provider_get_overview_page(dc_provider); + //TRACE("result %s", overview_page); + + NAPI_RETURN_AND_UNREF_STRING(overview_page); +} + +NAPI_METHOD(dcn_provider_get_before_login_hint) { + NAPI_ARGV(1); + NAPI_DC_PROVIDER(); + + //TRACE("calling.."); + char* before_login_hint = dc_provider_get_before_login_hint(dc_provider); + //TRACE("result %s", before_login_hint); + + NAPI_RETURN_AND_UNREF_STRING(before_login_hint); +} + +NAPI_METHOD(dcn_provider_get_status) { + NAPI_ARGV(1); + NAPI_DC_PROVIDER(); + + //TRACE("calling.."); + int status = dc_provider_get_status(dc_provider); + //TRACE("result %s", status); + + NAPI_RETURN_INT32(status) +} + +// webxdc + +NAPI_METHOD(dcn_send_webxdc_status_update){ + NAPI_ARGV(4); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + NAPI_ARGV_UTF8_MALLOC(json, 2); + NAPI_ARGV_UTF8_MALLOC(descr, 3); + + int result = dc_send_webxdc_status_update(dcn_context->dc_context, msg_id, json, descr); + free(json); + free(descr); + + NAPI_RETURN_UINT32(result); +} + +NAPI_METHOD(dcn_get_webxdc_status_updates){ + NAPI_ARGV(3); + NAPI_DCN_CONTEXT(); + NAPI_ARGV_UINT32(msg_id, 1); + NAPI_ARGV_UINT32(serial, 2); + + char* result_json = dc_get_webxdc_status_updates(dcn_context->dc_context, msg_id, serial); + + NAPI_RETURN_AND_UNREF_STRING(result_json); +} + +NAPI_METHOD(dcn_msg_get_webxdc_blob){ + NAPI_ARGV(2); + NAPI_DC_MSG(); + NAPI_ARGV_UTF8_MALLOC(filename, 1); + + size_t size; + char* data = dc_msg_get_webxdc_blob(dc_msg, filename, &size); + free(filename); + + napi_value jsbuffer; + if (data == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &jsbuffer)); + } else { + // https://nodejs.org/api/n-api.html#napi_create_buffer_copy + NAPI_STATUS_THROWS(napi_create_buffer_copy(env, + size, + data, + NULL, + &jsbuffer)) + dc_str_unref(data); + } + + return jsbuffer; +} + + +// dc_accounts_* + +NAPI_METHOD(dcn_accounts_new) { + NAPI_ARGV(2); + NAPI_ARGV_UTF8_MALLOC(os_name, 0); + NAPI_ARGV_UTF8_MALLOC(dir, 1); + TRACE("calling.."); + + dcn_accounts_t* dcn_accounts = calloc(1, sizeof(dcn_accounts_t)); + if (dcn_accounts == NULL) { + napi_throw_error(env, NULL, "dcn_accounts is null"); \ + } + + + dcn_accounts->dc_accounts = dc_accounts_new(os_name, dir); + + napi_value result; + NAPI_STATUS_THROWS(napi_create_external(env, dcn_accounts, + NULL, NULL, &result)); + return result; +} + + +NAPI_METHOD(dcn_accounts_unref) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + + TRACE("Unrefing dc_accounts"); + dcn_accounts->gc = 1; + dc_accounts_unref(dcn_accounts->dc_accounts); + dcn_accounts->dc_accounts = NULL; + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_accounts_add_account) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + int account_id = dc_accounts_add_account(dcn_accounts->dc_accounts); + + NAPI_RETURN_UINT32(account_id); +} + +NAPI_METHOD(dcn_accounts_add_closed_account) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + int account_id = dc_accounts_add_closed_account(dcn_accounts->dc_accounts); + + NAPI_RETURN_UINT32(account_id); +} + + +NAPI_METHOD(dcn_accounts_migrate_account) { + NAPI_ARGV(2); + NAPI_DCN_ACCOUNTS(); + NAPI_ARGV_UTF8_MALLOC(dbfile, 1); + + uint32_t account_id = dc_accounts_migrate_account(dcn_accounts->dc_accounts, dbfile); + + NAPI_RETURN_UINT32(account_id); +} + +NAPI_METHOD(dcn_accounts_remove_account) { + NAPI_ARGV(2); + NAPI_DCN_ACCOUNTS(); + NAPI_ARGV_UINT32(account_id, 1); + + int result = dc_accounts_remove_account(dcn_accounts->dc_accounts, account_id); + + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_accounts_get_all) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + dc_array_t* accounts = dc_accounts_get_all(dcn_accounts->dc_accounts); + napi_value js_array = dc_array_to_js_array(env, accounts); + dc_array_unref(accounts); + + return js_array; +} + +NAPI_METHOD(dcn_accounts_get_account) { + NAPI_ARGV(2); + NAPI_DCN_ACCOUNTS(); + NAPI_ARGV_UINT32(account_id, 1); + + dc_context_t* account_context = dc_accounts_get_account(dcn_accounts->dc_accounts, account_id); + + + napi_value result; + if (account_context == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + dcn_context_t* dcn_context = calloc(1, sizeof(dcn_context_t)); + dcn_context->dc_context = account_context; + + NAPI_STATUS_THROWS(napi_create_external(env, dcn_context, + NULL, NULL, &result)); + } + + return result; +} + +NAPI_METHOD(dcn_accounts_get_selected_account) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + dc_context_t* account_context = dc_accounts_get_selected_account(dcn_accounts->dc_accounts); + + + napi_value result; + if (account_context == NULL) { + NAPI_STATUS_THROWS(napi_get_null(env, &result)); + } else { + dcn_context_t* dcn_context = calloc(1, sizeof(dcn_context_t)); + dcn_context->dc_context = account_context; + + NAPI_STATUS_THROWS(napi_create_external(env, dcn_context, + NULL, NULL, &result)); + } + + return result; +} + +NAPI_METHOD(dcn_accounts_select_account) { + NAPI_ARGV(2); + NAPI_DCN_ACCOUNTS(); + NAPI_ARGV_UINT32(account_id, 1); + + int result = dc_accounts_select_account(dcn_accounts->dc_accounts, account_id); + NAPI_RETURN_UINT32(result); +} + +NAPI_METHOD(dcn_accounts_all_work_done) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + int result = dc_accounts_all_work_done(dcn_accounts->dc_accounts); + NAPI_RETURN_INT32(result); +} + +NAPI_METHOD(dcn_accounts_start_io) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + TRACE("calling..."); + dc_accounts_start_io(dcn_accounts->dc_accounts); + + NAPI_RETURN_UNDEFINED(); +} + +NAPI_METHOD(dcn_accounts_stop_io) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + dc_accounts_stop_io(dcn_accounts->dc_accounts); + + NAPI_RETURN_UNDEFINED(); +} + + +NAPI_METHOD(dcn_accounts_maybe_network) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + dc_accounts_maybe_network(dcn_accounts->dc_accounts); + + NAPI_RETURN_UNDEFINED(); +} + + +NAPI_METHOD(dcn_accounts_maybe_network_lost) { + NAPI_ARGV(1); + NAPI_DCN_ACCOUNTS(); + + dc_accounts_maybe_network_lost(dcn_accounts->dc_accounts); + + NAPI_RETURN_UNDEFINED(); +} + +static void accounts_event_handler_thread_func(void* arg) +{ + dcn_accounts_t* dcn_accounts = (dcn_accounts_t*)arg; + + + + TRACE("event_handler_thread_func starting"); + + dc_accounts_event_emitter_t * dc_accounts_event_emitter = dc_accounts_get_event_emitter(dcn_accounts->dc_accounts); + dc_event_t* event; + while (true) { + if (dc_accounts_event_emitter == NULL) { + TRACE("event emitter is null, bailing"); + break; + } + event = dc_accounts_get_next_event(dc_accounts_event_emitter); + if (event == NULL) { + //TRACE("received NULL event, skipping"); + continue; + } + + if (!dcn_accounts->threadsafe_event_handler) { + TRACE("threadsafe_event_handler not set, bailing"); + break; + } + + // Don't process events if we're being garbage collected! + if (dcn_accounts->gc == 1) { + TRACE("dc_accounts has been destroyed, bailing"); + break; + } + + + napi_status status = napi_call_threadsafe_function(dcn_accounts->threadsafe_event_handler, event, napi_tsfn_blocking); + + if (status == napi_closing) { + TRACE("JS function got released, bailing"); + break; + } + } + + dc_accounts_event_emitter_unref(dc_accounts_event_emitter); + + TRACE("event_handler_thread_func ended"); + + napi_release_threadsafe_function(dcn_accounts->threadsafe_event_handler, napi_tsfn_release); +} + +static void call_accounts_js_event_handler(napi_env env, napi_value js_callback, void* _context, void* data) +{ + dc_event_t* dc_event = (dc_event_t*)data; + + napi_value global; + napi_status status = napi_get_global(env, &global); + + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to get global"); + } + + +#define CALL_JS_CALLBACK_ACCOUNTS_ARGC 4 + + const int argc = CALL_JS_CALLBACK_ACCOUNTS_ARGC; + napi_value argv[CALL_JS_CALLBACK_ACCOUNTS_ARGC]; + + const int event_id = dc_event_get_id(dc_event); + + status = napi_create_uint32(env, event_id, &argv[0]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[0] for event_handler arguments"); + } + + const int account_id = dc_event_get_account_id(dc_event); + status = napi_create_uint32(env, account_id, &argv[1]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[1] for event_handler arguments"); + } + + + status = napi_create_int32(env, dc_event_get_data1_int(dc_event), &argv[2]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[2] for event_handler arguments"); + } + + if DC_EVENT_DATA2_IS_STRING(event_id) { + char* data2_string = dc_event_get_data2_str(dc_event); + // Quick fix for https://github.com/deltachat/deltachat-core-rust/issues/1949 + if (data2_string != 0) { + status = napi_create_string_utf8(env, data2_string, NAPI_AUTO_LENGTH, &argv[3]); + } else { + status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &argv[3]); + } + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[3] for event_handler arguments"); + } + free(data2_string); + } else { + status = napi_create_int32(env, dc_event_get_data2_int(dc_event), &argv[3]); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create argv[3] for event_handler arguments"); + } + } + + dc_event_unref(dc_event); + dc_event = NULL; + + TRACE("calling back into js"); + + napi_value result; + status = napi_call_function( + env, + global, + js_callback, + argc, + argv, + &result); + + if (status != napi_ok) { + TRACE("Unable to call event_handler callback2"); + napi_extended_error_info* error_result; + NAPI_STATUS_THROWS(napi_get_last_error_info(env, &error_result)); + } +} + +NAPI_METHOD(dcn_accounts_start_event_handler) { + NAPI_ARGV(2); + NAPI_DCN_ACCOUNTS(); + napi_value callback = argv[1]; + + TRACE("calling.."); + napi_value async_resource_name; + NAPI_STATUS_THROWS(napi_create_string_utf8(env, "dc_accounts_event_callback", NAPI_AUTO_LENGTH, &async_resource_name)); + + TRACE("creating threadsafe function.."); + + NAPI_STATUS_THROWS(napi_create_threadsafe_function( + env, + callback, + 0, + async_resource_name, + 1, + 1, + NULL, + NULL, + dcn_accounts, + call_accounts_js_event_handler, + &dcn_accounts->threadsafe_event_handler)); + TRACE("done"); + + dcn_accounts->gc = 0; + TRACE("creating uv thread.."); + uv_thread_create(&dcn_accounts->event_handler_thread, accounts_event_handler_thread_func, dcn_accounts); + + NAPI_RETURN_UNDEFINED(); +} + + +NAPI_INIT() { + /** + * Accounts + */ + + NAPI_EXPORT_FUNCTION(dcn_accounts_new); + NAPI_EXPORT_FUNCTION(dcn_accounts_unref); + NAPI_EXPORT_FUNCTION(dcn_accounts_add_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_add_closed_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_migrate_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_remove_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_get_all); + NAPI_EXPORT_FUNCTION(dcn_accounts_get_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_get_selected_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_select_account); + NAPI_EXPORT_FUNCTION(dcn_accounts_all_work_done); + NAPI_EXPORT_FUNCTION(dcn_accounts_start_io); + NAPI_EXPORT_FUNCTION(dcn_accounts_stop_io); + NAPI_EXPORT_FUNCTION(dcn_accounts_maybe_network); + NAPI_EXPORT_FUNCTION(dcn_accounts_maybe_network_lost); + + NAPI_EXPORT_FUNCTION(dcn_accounts_start_event_handler); + + + /** + * Main context + */ + + NAPI_EXPORT_FUNCTION(dcn_context_new); + NAPI_EXPORT_FUNCTION(dcn_context_new_closed); + NAPI_EXPORT_FUNCTION(dcn_context_open); + NAPI_EXPORT_FUNCTION(dcn_context_is_open); + NAPI_EXPORT_FUNCTION(dcn_context_unref); + NAPI_EXPORT_FUNCTION(dcn_start_event_handler); + + /** + * Static functions + */ + + NAPI_EXPORT_FUNCTION(dcn_maybe_valid_addr); + + /** + * dcn_context_t + */ + + NAPI_EXPORT_FUNCTION(dcn_add_address_book); + NAPI_EXPORT_FUNCTION(dcn_add_contact_to_chat); + NAPI_EXPORT_FUNCTION(dcn_add_device_msg); + NAPI_EXPORT_FUNCTION(dcn_block_contact); + NAPI_EXPORT_FUNCTION(dcn_check_qr); + NAPI_EXPORT_FUNCTION(dcn_configure); + NAPI_EXPORT_FUNCTION(dcn_continue_key_transfer); + NAPI_EXPORT_FUNCTION(dcn_create_chat_by_contact_id); + NAPI_EXPORT_FUNCTION(dcn_create_broadcast_list); + NAPI_EXPORT_FUNCTION(dcn_create_contact); + NAPI_EXPORT_FUNCTION(dcn_create_group_chat); + NAPI_EXPORT_FUNCTION(dcn_delete_chat); + NAPI_EXPORT_FUNCTION(dcn_delete_contact); + NAPI_EXPORT_FUNCTION(dcn_delete_msgs); + NAPI_EXPORT_FUNCTION(dcn_forward_msgs); + NAPI_EXPORT_FUNCTION(dcn_get_blobdir); + NAPI_EXPORT_FUNCTION(dcn_get_blocked_cnt); + NAPI_EXPORT_FUNCTION(dcn_get_blocked_contacts); + NAPI_EXPORT_FUNCTION(dcn_get_chat); + NAPI_EXPORT_FUNCTION(dcn_get_chat_contacts); + NAPI_EXPORT_FUNCTION(dcn_get_chat_encrinfo); + NAPI_EXPORT_FUNCTION(dcn_get_chat_id_by_contact_id); + NAPI_EXPORT_FUNCTION(dcn_get_chat_media); + NAPI_EXPORT_FUNCTION(dcn_get_mime_headers); + NAPI_EXPORT_FUNCTION(dcn_get_chat_msgs); + NAPI_EXPORT_FUNCTION(dcn_get_chatlist); + NAPI_EXPORT_FUNCTION(dcn_get_config); + NAPI_EXPORT_FUNCTION(dcn_get_contact); + NAPI_EXPORT_FUNCTION(dcn_get_contact_encrinfo); + NAPI_EXPORT_FUNCTION(dcn_get_contacts); + NAPI_EXPORT_FUNCTION(dcn_get_connectivity); + NAPI_EXPORT_FUNCTION(dcn_get_connectivity_html); + NAPI_EXPORT_FUNCTION(dcn_was_device_msg_ever_added); + NAPI_EXPORT_FUNCTION(dcn_get_draft); + NAPI_EXPORT_FUNCTION(dcn_get_fresh_msg_cnt); + NAPI_EXPORT_FUNCTION(dcn_get_fresh_msgs); + NAPI_EXPORT_FUNCTION(dcn_get_info); + NAPI_EXPORT_FUNCTION(dcn_get_msg); + NAPI_EXPORT_FUNCTION(dcn_get_msg_cnt); + NAPI_EXPORT_FUNCTION(dcn_get_msg_info); + NAPI_EXPORT_FUNCTION(dcn_get_msg_html); + NAPI_EXPORT_FUNCTION(dcn_get_next_media); + NAPI_EXPORT_FUNCTION(dcn_set_chat_visibility); + NAPI_EXPORT_FUNCTION(dcn_get_securejoin_qr); + NAPI_EXPORT_FUNCTION(dcn_get_securejoin_qr_svg); + NAPI_EXPORT_FUNCTION(dcn_imex); + NAPI_EXPORT_FUNCTION(dcn_imex_has_backup); + NAPI_EXPORT_FUNCTION(dcn_initiate_key_transfer); + NAPI_EXPORT_FUNCTION(dcn_is_configured); + NAPI_EXPORT_FUNCTION(dcn_is_contact_in_chat); + + + + NAPI_EXPORT_FUNCTION(dcn_accept_chat); + NAPI_EXPORT_FUNCTION(dcn_block_chat); + NAPI_EXPORT_FUNCTION(dcn_join_securejoin); + NAPI_EXPORT_FUNCTION(dcn_lookup_contact_id_by_addr); + NAPI_EXPORT_FUNCTION(dcn_marknoticed_chat); + NAPI_EXPORT_FUNCTION(dcn_download_full_msg); + NAPI_EXPORT_FUNCTION(dcn_markseen_msgs); + NAPI_EXPORT_FUNCTION(dcn_maybe_network); + NAPI_EXPORT_FUNCTION(dcn_msg_new); + NAPI_EXPORT_FUNCTION(dcn_remove_contact_from_chat); + NAPI_EXPORT_FUNCTION(dcn_search_msgs); + NAPI_EXPORT_FUNCTION(dcn_send_msg); + NAPI_EXPORT_FUNCTION(dcn_send_videochat_invitation); + NAPI_EXPORT_FUNCTION(dcn_set_chat_name); + NAPI_EXPORT_FUNCTION(dcn_set_chat_protection); + NAPI_EXPORT_FUNCTION(dcn_get_chat_ephemeral_timer); + NAPI_EXPORT_FUNCTION(dcn_set_chat_ephemeral_timer); + NAPI_EXPORT_FUNCTION(dcn_set_chat_profile_image); + NAPI_EXPORT_FUNCTION(dcn_set_chat_mute_duration); + NAPI_EXPORT_FUNCTION(dcn_set_config); + NAPI_EXPORT_FUNCTION(dcn_set_config_null); + NAPI_EXPORT_FUNCTION(dcn_set_config_from_qr); + NAPI_EXPORT_FUNCTION(dcn_estimate_deletion_cnt); + NAPI_EXPORT_FUNCTION(dcn_set_draft); + NAPI_EXPORT_FUNCTION(dcn_set_stock_translation); + NAPI_EXPORT_FUNCTION(dcn_start_io); + NAPI_EXPORT_FUNCTION(dcn_stop_io); + NAPI_EXPORT_FUNCTION(dcn_stop_ongoing_process); + + /** + * dc_chat_t + */ + + NAPI_EXPORT_FUNCTION(dcn_chat_get_color); + NAPI_EXPORT_FUNCTION(dcn_chat_get_visibility); + NAPI_EXPORT_FUNCTION(dcn_chat_get_id); + NAPI_EXPORT_FUNCTION(dcn_chat_get_name); + NAPI_EXPORT_FUNCTION(dcn_chat_get_profile_image); + NAPI_EXPORT_FUNCTION(dcn_chat_get_type); + NAPI_EXPORT_FUNCTION(dcn_chat_is_self_talk); + NAPI_EXPORT_FUNCTION(dcn_chat_is_unpromoted); + NAPI_EXPORT_FUNCTION(dcn_chat_can_send); + NAPI_EXPORT_FUNCTION(dcn_chat_is_protected); + NAPI_EXPORT_FUNCTION(dcn_chat_is_device_talk); + NAPI_EXPORT_FUNCTION(dcn_chat_is_muted); + NAPI_EXPORT_FUNCTION(dcn_chat_is_contact_request); + + /** + * dc_chatlist_t + */ + + NAPI_EXPORT_FUNCTION(dcn_chatlist_get_chat_id); + NAPI_EXPORT_FUNCTION(dcn_chatlist_get_cnt); + NAPI_EXPORT_FUNCTION(dcn_chatlist_get_msg_id); + NAPI_EXPORT_FUNCTION(dcn_chatlist_get_summary); + NAPI_EXPORT_FUNCTION(dcn_chatlist_get_summary2); + + /** + * dc_contact_t + */ + + NAPI_EXPORT_FUNCTION(dcn_contact_get_addr); + NAPI_EXPORT_FUNCTION(dcn_contact_get_auth_name); + NAPI_EXPORT_FUNCTION(dcn_contact_get_color); + NAPI_EXPORT_FUNCTION(dcn_contact_get_display_name); + NAPI_EXPORT_FUNCTION(dcn_contact_get_id); + NAPI_EXPORT_FUNCTION(dcn_contact_get_name); + NAPI_EXPORT_FUNCTION(dcn_contact_get_name_n_addr); + NAPI_EXPORT_FUNCTION(dcn_contact_get_profile_image); + NAPI_EXPORT_FUNCTION(dcn_contact_get_status); + NAPI_EXPORT_FUNCTION(dcn_contact_get_last_seen); + NAPI_EXPORT_FUNCTION(dcn_contact_is_blocked); + NAPI_EXPORT_FUNCTION(dcn_contact_is_verified); + + /** + * dc_lot_t + */ + + NAPI_EXPORT_FUNCTION(dcn_lot_get_id); + NAPI_EXPORT_FUNCTION(dcn_lot_get_state); + NAPI_EXPORT_FUNCTION(dcn_lot_get_text1); + NAPI_EXPORT_FUNCTION(dcn_lot_get_text1_meaning); + NAPI_EXPORT_FUNCTION(dcn_lot_get_text2); + NAPI_EXPORT_FUNCTION(dcn_lot_get_timestamp); + + /** + * dc_msg_t + */ + + NAPI_EXPORT_FUNCTION(dcn_msg_get_parent); + NAPI_EXPORT_FUNCTION(dcn_msg_get_download_state); + NAPI_EXPORT_FUNCTION(dcn_msg_get_chat_id); + NAPI_EXPORT_FUNCTION(dcn_msg_get_duration); + NAPI_EXPORT_FUNCTION(dcn_msg_get_file); + NAPI_EXPORT_FUNCTION(dcn_msg_get_filebytes); + NAPI_EXPORT_FUNCTION(dcn_msg_get_filemime); + NAPI_EXPORT_FUNCTION(dcn_msg_get_filename); + NAPI_EXPORT_FUNCTION(dcn_msg_get_from_id); + NAPI_EXPORT_FUNCTION(dcn_msg_get_height); + NAPI_EXPORT_FUNCTION(dcn_msg_get_id); + NAPI_EXPORT_FUNCTION(dcn_msg_get_override_sender_name); + NAPI_EXPORT_FUNCTION(dcn_msg_get_quoted_text); + NAPI_EXPORT_FUNCTION(dcn_msg_get_quoted_msg); + NAPI_EXPORT_FUNCTION(dcn_msg_get_received_timestamp); + NAPI_EXPORT_FUNCTION(dcn_msg_get_setupcodebegin); + NAPI_EXPORT_FUNCTION(dcn_msg_get_showpadlock); + NAPI_EXPORT_FUNCTION(dcn_msg_get_sort_timestamp); + NAPI_EXPORT_FUNCTION(dcn_msg_get_state); + NAPI_EXPORT_FUNCTION(dcn_msg_get_summary); + NAPI_EXPORT_FUNCTION(dcn_msg_get_summarytext); + NAPI_EXPORT_FUNCTION(dcn_msg_get_subject); + NAPI_EXPORT_FUNCTION(dcn_msg_get_text); + NAPI_EXPORT_FUNCTION(dcn_msg_get_timestamp); + NAPI_EXPORT_FUNCTION(dcn_msg_get_viewtype); + NAPI_EXPORT_FUNCTION(dcn_msg_get_videochat_type); + NAPI_EXPORT_FUNCTION(dcn_msg_get_videochat_url); + NAPI_EXPORT_FUNCTION(dcn_msg_get_width); + NAPI_EXPORT_FUNCTION(dcn_msg_get_webxdc_info); + NAPI_EXPORT_FUNCTION(dcn_msg_has_deviating_timestamp); + NAPI_EXPORT_FUNCTION(dcn_msg_has_location); + NAPI_EXPORT_FUNCTION(dcn_msg_has_html); + NAPI_EXPORT_FUNCTION(dcn_msg_is_forwarded); + NAPI_EXPORT_FUNCTION(dcn_msg_is_increation); + NAPI_EXPORT_FUNCTION(dcn_msg_is_info); + NAPI_EXPORT_FUNCTION(dcn_msg_is_sent); + NAPI_EXPORT_FUNCTION(dcn_msg_is_setupmessage); + NAPI_EXPORT_FUNCTION(dcn_msg_latefiling_mediasize); + NAPI_EXPORT_FUNCTION(dcn_msg_force_plaintext); + NAPI_EXPORT_FUNCTION(dcn_msg_set_dimension); + NAPI_EXPORT_FUNCTION(dcn_msg_set_duration); + NAPI_EXPORT_FUNCTION(dcn_msg_set_override_sender_name); + NAPI_EXPORT_FUNCTION(dcn_msg_set_file); + NAPI_EXPORT_FUNCTION(dcn_msg_set_html); + NAPI_EXPORT_FUNCTION(dcn_msg_set_quote); + NAPI_EXPORT_FUNCTION(dcn_msg_set_text); + NAPI_EXPORT_FUNCTION(dcn_msg_set_location); + + /** + * dc_location + */ + NAPI_EXPORT_FUNCTION(dcn_set_location); + NAPI_EXPORT_FUNCTION(dcn_get_locations); + + /** + * dc_provider + */ + NAPI_EXPORT_FUNCTION(dcn_provider_new_from_email); + NAPI_EXPORT_FUNCTION(dcn_provider_get_overview_page); + NAPI_EXPORT_FUNCTION(dcn_provider_get_before_login_hint); + NAPI_EXPORT_FUNCTION(dcn_provider_get_status); + + /** + * dc_array + */ + NAPI_EXPORT_FUNCTION(dcn_array_get_cnt); + NAPI_EXPORT_FUNCTION(dcn_array_get_id); + NAPI_EXPORT_FUNCTION(dcn_array_get_accuracy); + NAPI_EXPORT_FUNCTION(dcn_array_get_latitude); + NAPI_EXPORT_FUNCTION(dcn_array_get_longitude); + NAPI_EXPORT_FUNCTION(dcn_array_get_timestamp); + NAPI_EXPORT_FUNCTION(dcn_array_get_msg_id); + NAPI_EXPORT_FUNCTION(dcn_array_is_independent); + NAPI_EXPORT_FUNCTION(dcn_array_get_contact_id); + NAPI_EXPORT_FUNCTION(dcn_array_get_chat_id); + NAPI_EXPORT_FUNCTION(dcn_array_get_marker); + + /** webxdc **/ + + NAPI_EXPORT_FUNCTION(dcn_send_webxdc_status_update); + NAPI_EXPORT_FUNCTION(dcn_get_webxdc_status_updates); + NAPI_EXPORT_FUNCTION(dcn_msg_get_webxdc_blob); +} diff --git a/node/src/napi-macros-extensions.h b/node/src/napi-macros-extensions.h new file mode 100644 index 000000000..badb63969 --- /dev/null +++ b/node/src/napi-macros-extensions.h @@ -0,0 +1,144 @@ +#include + +#undef NAPI_STATUS_THROWS + +#define NAPI_STATUS_THROWS(call) \ + if ((call) != napi_ok) { \ + napi_throw_error(env, NULL, #call " failed!"); \ + } + +#define NAPI_DCN_CONTEXT() \ + dcn_context_t* dcn_context; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dcn_context)); \ + if (!dcn_context) { \ + const char* msg = "Provided dnc_context is null"; \ + NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg)); \ + } \ + if (!dcn_context->dc_context) { \ + const char* msg = "Provided dc_context is null, did you close the context or not open it?"; \ + NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg)); \ + } + +#define NAPI_DCN_ACCOUNTS() \ + dcn_accounts_t* dcn_accounts; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dcn_accounts)); \ + if (!dcn_accounts) { \ + const char* msg = "Provided dnc_acounts is null"; \ + NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg)); \ + } \ + if (!dcn_accounts->dc_accounts) { \ + const char* msg = "Provided dc_accounts is null, did you unref the accounts object?"; \ + NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg)); \ + } + + +#define NAPI_DC_CHAT() \ + dc_chat_t* dc_chat; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_chat)); + +#define NAPI_DC_CHATLIST() \ + dc_chatlist_t* dc_chatlist; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_chatlist)); + +#define NAPI_DC_CONTACT() \ + dc_contact_t* dc_contact; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_contact)); + +#define NAPI_DC_LOT() \ + dc_lot_t* dc_lot; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_lot)); + +#define NAPI_DC_MSG() \ + dc_msg_t* dc_msg; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_msg)); + +#define NAPI_ARGV_DC_MSG(name, position) \ + dc_msg_t* name; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[position], (void**)&name)); + +#define NAPI_DC_PROVIDER() \ + dc_provider_t* dc_provider; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_provider)); + +#define NAPI_DC_ARRAY() \ + dc_array_t* dc_array; \ + NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&dc_array)); + +#define NAPI_RETURN_UNDEFINED() \ + return 0; + +#define NAPI_RETURN_UINT64(name) \ + napi_value return_int64; \ + NAPI_STATUS_THROWS(napi_create_bigint_int64(env, name, &return_int64)); \ + return return_int64; + +#define NAPI_RETURN_INT64(name) \ + napi_value return_int64; \ + NAPI_STATUS_THROWS(napi_create_int64(env, name, &return_int64)); \ + return return_int64; + + +#define NAPI_RETURN_AND_UNREF_STRING(name) \ + napi_value return_value; \ + if (name == NULL) { \ + NAPI_STATUS_THROWS(napi_get_null(env, &return_value)); \ + return return_value; \ + } \ + NAPI_STATUS_THROWS(napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &return_value)); \ + dc_str_unref(name); \ + return return_value; + +#define NAPI_ASYNC_CARRIER_BEGIN(name) \ + typedef struct name##_carrier_t { \ + napi_ref callback_ref; \ + napi_async_work async_work; \ + dcn_context_t* dcn_context; + +#define NAPI_ASYNC_CARRIER_END(name) \ + } name##_carrier_t; + +#define NAPI_ASYNC_EXECUTE(name) \ + static void name##_execute(napi_env env, void* data) + +#define NAPI_ASYNC_GET_CARRIER(name) \ + name##_carrier_t* carrier = (name##_carrier_t*)data; + +#define NAPI_ASYNC_COMPLETE(name) \ + static void name##_complete(napi_env env, napi_status status, void* data) + +#define NAPI_ASYNC_CALL_AND_DELETE_CB() \ + napi_value global; \ + NAPI_STATUS_THROWS(napi_get_global(env, &global)); \ + napi_value callback; \ + NAPI_STATUS_THROWS(napi_get_reference_value(env, carrier->callback_ref, &callback)); \ + NAPI_STATUS_THROWS(napi_call_function(env, global, callback, argc, argv, NULL)); \ + NAPI_STATUS_THROWS(napi_delete_reference(env, carrier->callback_ref)); \ + NAPI_STATUS_THROWS(napi_delete_async_work(env, carrier->async_work)); + +#define NAPI_ASYNC_NEW_CARRIER(name) \ + name##_carrier_t* carrier = calloc(1, sizeof(name##_carrier_t)); \ + carrier->dcn_context = dcn_context; + +#define NAPI_ASYNC_QUEUE_WORK(name, cb) \ + napi_value callback = cb; \ + napi_value async_resource_name; \ + NAPI_STATUS_THROWS(napi_create_reference(env, callback, 1, &carrier->callback_ref)); \ + NAPI_STATUS_THROWS(napi_create_string_utf8(env, #name "_callback", \ + NAPI_AUTO_LENGTH, \ + &async_resource_name)); \ + NAPI_STATUS_THROWS(napi_create_async_work(env, callback, async_resource_name, \ + name##_execute, name##_complete, \ + carrier, &carrier->async_work)); \ + NAPI_STATUS_THROWS(napi_queue_async_work(env, carrier->async_work)); + +/*** this could/should be moved to napi-macros ***/ + +#define NAPI_DOUBLE(name, val) \ + double name; \ + if (napi_get_value_double(env, val, &name) != napi_ok) { \ + napi_throw_error(env, "EINVAL", "Expected double"); \ + return NULL; \ + } + +#define NAPI_ARGV_DOUBLE(name, i) \ + NAPI_DOUBLE(name, argv[i]) diff --git a/node/test/fixtures/avatar.png b/node/test/fixtures/avatar.png new file mode 100644 index 000000000..76e69ce49 Binary files /dev/null and b/node/test/fixtures/avatar.png differ diff --git a/node/test/fixtures/image.jpeg b/node/test/fixtures/image.jpeg new file mode 100644 index 000000000..7e64c9930 Binary files /dev/null and b/node/test/fixtures/image.jpeg differ diff --git a/node/test/fixtures/logo.png b/node/test/fixtures/logo.png new file mode 100644 index 000000000..9408ad90b Binary files /dev/null and b/node/test/fixtures/logo.png differ diff --git a/node/test/test.js b/node/test/test.js new file mode 100644 index 000000000..a4a781486 --- /dev/null +++ b/node/test/test.js @@ -0,0 +1,864 @@ +// @ts-check +import DeltaChat, { Message } from '../dist' +import binding from '../binding' + +import { strictEqual } from 'assert' +import chai, { expect } from 'chai' +import chaiAsPromised from 'chai-as-promised' +import { EventId2EventName, C } from '../dist/constants' +import { join } from 'path' +import { mkdtempSync, statSync } from 'fs' +import { tmpdir } from 'os' +import { Context } from '../dist/context' +chai.use(chaiAsPromised) + +async function createTempUser(url) { + const fetch = require('node-fetch') + + async function postData(url = '') { + // Default options are marked with * + const response = await fetch(url, { + method: 'POST', // *GET, POST, PUT, DELETE, etc. + mode: 'cors', // no-cors, *cors, same-origin + cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached + credentials: 'same-origin', // include, *same-origin, omit + headers: { + 'cache-control': 'no-cache', + }, + referrerPolicy: 'no-referrer', // no-referrer, *client + }) + return response.json() // parses JSON response into native JavaScript objects + } + + return await postData(url) +} + +describe('static tests', function () { + it('reverse lookup of events', function () { + const eventKeys = Object.keys(EventId2EventName).map((k) => Number(k)) + const eventValues = Object.values(EventId2EventName) + const reverse = eventValues.map((v) => C[v]) + expect(reverse).to.be.deep.equal(eventKeys) + }) + + it('event constants are consistent', function () { + const eventKeys = Object.keys(C) + .filter((k) => k.startsWith('DC_EVENT_')) + .sort() + const eventValues = Object.values(EventId2EventName).sort() + expect(eventKeys).to.be.deep.equal(eventValues) + }) + + it('static method maybeValidAddr()', function () { + expect(DeltaChat.maybeValidAddr(null)).to.equal(false) + expect(DeltaChat.maybeValidAddr('')).to.equal(false) + expect(DeltaChat.maybeValidAddr('uuu')).to.equal(false) + expect(DeltaChat.maybeValidAddr('dd.tt')).to.equal(false) + expect(DeltaChat.maybeValidAddr('tt.dd@yggmail')).to.equal(true) + expect(DeltaChat.maybeValidAddr('u@d')).to.equal(true) + //expect(DeltaChat.maybeValidAddr('u@d.')).to.equal(false) + //expect(DeltaChat.maybeValidAddr('u@d.t')).to.equal(false) + //expect(DeltaChat.maybeValidAddr('u@.tt')).to.equal(false) + expect(DeltaChat.maybeValidAddr('@d.tt')).to.equal(false) + expect(DeltaChat.maybeValidAddr('user@domain.tld')).to.equal(true) + expect(DeltaChat.maybeValidAddr('u@d.tt')).to.equal(true) + }) + + it('static getSystemInfo()', function () { + const info = Context.getSystemInfo() + expect(info).to.contain.keys([ + 'arch', + 'deltachat_core_version', + 'sqlite_version', + ]) + }) + + it('static context.getProviderFromEmail("example@example.com")', function () { + const provider = DeltaChat.getProviderFromEmail('example@example.com') + + expect(provider).to.deep.equal({ + before_login_hint: "Hush this provider doesn't exist!", + overview_page: 'https://providers.delta.chat/example-com', + status: 3, + }) + }) +}) + +describe('Basic offline Tests', function () { + it('opens a context', async function () { + const { dc, context } = DeltaChat.newTemporary() + + strictEqual(context.isConfigured(), false) + dc.close() + }) + + it('set config', async function () { + const { dc, context } = DeltaChat.newTemporary() + + context.setConfig('bot', true) + strictEqual(context.getConfig('bot'), '1') + context.setConfig('bot', false) + strictEqual(context.getConfig('bot'), '0') + context.setConfig('bot', '1') + strictEqual(context.getConfig('bot'), '1') + context.setConfig('bot', '0') + strictEqual(context.getConfig('bot'), '0') + context.setConfig('bot', 1) + strictEqual(context.getConfig('bot'), '1') + context.setConfig('bot', 0) + strictEqual(context.getConfig('bot'), '0') + + context.setConfig('bot', null) + strictEqual(context.getConfig('bot'), '') + + strictEqual(context.getConfig('selfstatus'), '') + context.setConfig('selfstatus', 'hello') + strictEqual(context.getConfig('selfstatus'), 'hello') + context.setConfig('selfstatus', '') + strictEqual(context.getConfig('selfstatus'), '') + context.setConfig('selfstatus', null) + strictEqual(context.getConfig('selfstatus'), '') + + dc.close() + }) + + it('configure with either missing addr or missing mail_pw throws', async function () { + const { dc, context } = DeltaChat.newTemporary() + dc.startEvents() + + await expect( + context.configure({ addr: 'delta1@delta.localhost' }) + ).to.eventually.be.rejectedWith('Please enter a password.') + await expect(context.configure({ mailPw: 'delta1' })).to.eventually.be + .rejected + + context.stopOngoingProcess() + dc.close() + }) + + it('context.getInfo()', async function () { + const { dc, context } = DeltaChat.newTemporary() + + const info = await context.getInfo() + expect(typeof info).to.be.equal('object') + expect(info).to.contain.keys([ + 'arch', + 'bcc_self', + 'blobdir', + 'bot', + 'configured_mvbox_folder', + 'configured_sentbox_folder', + 'database_dir', + 'database_encrypted', + 'database_version', + 'delete_device_after', + 'delete_server_after', + 'deltachat_core_version', + 'display_name', + 'download_limit', + 'e2ee_enabled', + 'entered_account_settings', + 'fetch_existing_msgs', + 'fingerprint', + 'folders_configured', + 'is_configured', + 'journal_mode', + 'key_gen_type', + 'last_housekeeping', + 'level', + 'mdns_enabled', + 'media_quality', + 'messages_in_contact_requests', + 'mvbox_move', + 'num_cpus', + 'number_of_chat_messages', + 'number_of_chats', + 'number_of_contacts', + 'only_fetch_mvbox', + 'private_key_count', + 'public_key_count', + 'quota_exceeding', + 'scan_all_folders_debounce_secs', + 'selfavatar', + 'send_sync_msgs', + 'sentbox_watch', + 'show_emails', + 'socks5_enabled', + 'sqlite_version', + 'uptime', + 'used_account_settings', + 'webrtc_instance', + ]) + + dc.close() + }) +}) + +describe('Offline Tests with unconfigured account', function () { + let [dc, context, accountId, directory] = [null, null, null, null] + + this.beforeEach(async function () { + let tmp = DeltaChat.newTemporary() + dc = tmp.dc + context = tmp.context + accountId = tmp.accountId + directory = tmp.directory + dc.startEvents() + }) + + this.afterEach(async function () { + if (context) { + context.stopOngoingProcess() + } + if (dc) { + try { + dc.stopIO() + dc.close() + } catch (error) { + console.error(error) + } + } + + dc = null + context = null + accountId = null + directory = null + }) + + it('invalid context.joinSecurejoin', async function () { + expect(context.joinSecurejoin('test')).to.be.eq(0) + }) + + it('Device Chat', async function () { + const deviceChatMessageText = 'test234' + + expect((await context.getChatList(0, '', null)).getCount()).to.equal( + 0, + 'no device chat after setup' + ) + + await context.addDeviceMessage('test', deviceChatMessageText) + + const chatList = await context.getChatList(0, '', null) + expect(chatList.getCount()).to.equal( + 1, + 'device chat after adding device msg' + ) + + const deviceChatId = await chatList.getChatId(0) + const deviceChat = await context.getChat(deviceChatId) + expect(deviceChat.isDeviceTalk()).to.be.true + expect(deviceChat.toJson().isDeviceTalk).to.be.true + + const deviceChatMessages = await context.getChatMessages(deviceChatId, 0, 0) + expect(deviceChatMessages.length).to.be.equal( + 1, + 'device chat has added message' + ) + + const deviceChatMessage = await context.getMessage(deviceChatMessages[0]) + expect(deviceChatMessage.getText()).to.equal( + deviceChatMessageText, + 'device chat message has the inserted text' + ) + }) + + it('should have e2ee enabled and right blobdir', function () { + expect(context.getConfig('e2ee_enabled')).to.equal( + '1', + 'e2eeEnabled correct' + ) + expect( + String(context.getBlobdir()).startsWith(directory), + 'blobdir should be inside temp directory' + ) + expect( + String(context.getBlobdir()).endsWith('db.sqlite-blobs'), + 'blobdir end with "db.sqlite-blobs"' + ) + }) + + it('should create chat from contact and Chat methods', async function () { + const contactId = context.createContact('aaa', 'aaa@site.org') + + strictEqual(context.lookupContactIdByAddr('aaa@site.org'), contactId) + strictEqual(context.lookupContactIdByAddr('nope@site.net'), 0) + + let chatId = context.createChatByContactId(contactId) + let chat = context.getChat(chatId) + + strictEqual( + chat.getVisibility(), + C.DC_CHAT_VISIBILITY_NORMAL, + 'not archived' + ) + strictEqual(chat.getId(), chatId, 'chat id matches') + strictEqual(chat.getName(), 'aaa', 'chat name matches') + strictEqual(chat.getProfileImage(), null, 'no profile image') + strictEqual(chat.getType(), C.DC_CHAT_TYPE_SINGLE, 'single chat') + strictEqual(chat.isSelfTalk(), false, 'no self talk') + // TODO make sure this is really the case! + strictEqual(chat.isUnpromoted(), false, 'not unpromoted') + strictEqual(chat.isProtected(), false, 'not verified') + strictEqual(typeof chat.color, 'string', 'color is a string') + + strictEqual(context.getDraft(chatId), null, 'no draft message') + context.setDraft(chatId, context.messageNew().setText('w00t!')) + strictEqual( + context.getDraft(chatId).toJson().text, + 'w00t!', + 'draft text correct' + ) + context.setDraft(chatId, null) + strictEqual(context.getDraft(chatId), null, 'draft removed') + + strictEqual(context.getChatIdByContactId(contactId), chatId) + expect(context.getChatContacts(chatId)).to.deep.equal([contactId]) + + context.setChatVisibility(chatId, C.DC_CHAT_VISIBILITY_ARCHIVED) + strictEqual( + context.getChat(chatId).getVisibility(), + C.DC_CHAT_VISIBILITY_ARCHIVED, + 'chat archived' + ) + context.setChatVisibility(chatId, C.DC_CHAT_VISIBILITY_NORMAL) + strictEqual( + chat.getVisibility(), + C.DC_CHAT_VISIBILITY_NORMAL, + 'chat unarchived' + ) + + chatId = context.createGroupChat('unverified group', false) + chat = context.getChat(chatId) + strictEqual(chat.isProtected(), false, 'is not verified') + strictEqual(chat.getType(), C.DC_CHAT_TYPE_GROUP, 'group chat') + expect(context.getChatContacts(chatId)).to.deep.equal([ + C.DC_CONTACT_ID_SELF, + ]) + + const draft2 = context.getDraft(chatId) + expect(draft2 == null, 'unpromoted group has no draft by default') + + context.setChatName(chatId, 'NEW NAME') + strictEqual(context.getChat(chatId).getName(), 'NEW NAME', 'name updated') + + chatId = context.createGroupChat('a verified group', true) + chat = context.getChat(chatId) + strictEqual(chat.isProtected(), true, 'is verified') + }) + + it('test setting profile image', async function () { + const chatId = context.createGroupChat('testing profile image group', false) + const image = 'image.jpeg' + const imagePath = join(__dirname, 'fixtures', image) + const blobs = context.getBlobdir() + + context.setChatProfileImage(chatId, imagePath) + const blobPath = context.getChat(chatId).getProfileImage() + expect(blobPath.startsWith(blobs)).to.be.true + expect(blobPath.endsWith(image)).to.be.true + + context.setChatProfileImage(chatId, null) + expect(context.getChat(chatId).getProfileImage()).to.be.equal( + null, + 'image is null' + ) + }) + + it('test setting ephemeral timer', function () { + const chatId = context.createGroupChat('testing ephemeral timer') + + strictEqual( + context.getChatEphemeralTimer(chatId), + 0, + 'ephemeral timer is not set by default' + ) + + context.setChatEphemeralTimer(chatId, 60) + strictEqual( + context.getChatEphemeralTimer(chatId), + 60, + 'ephemeral timer is set to 1 minute' + ) + + context.setChatEphemeralTimer(chatId, 0) + strictEqual( + context.getChatEphemeralTimer(chatId), + 0, + 'ephemeral timer is reset' + ) + }) + + it('should create and delete chat', function () { + const chatId = context.createGroupChat('GROUPCHAT') + const chat = context.getChat(chatId) + strictEqual(chat.getId(), chatId, 'correct chatId') + context.deleteChat(chat.getId()) + strictEqual(context.getChat(chatId), null, 'chat removed') + }) + + it('new message and Message methods', function () { + const text = 'w00t!' + const msg = context.messageNew().setText(text) + + strictEqual(msg.getChatId(), 0, 'chat id 0 before sent') + strictEqual(msg.getDuration(), 0, 'duration 0 before sent') + strictEqual(msg.getFile(), '', 'no file set by default') + strictEqual(msg.getFilebytes(), 0, 'and file bytes is 0') + strictEqual(msg.getFilemime(), '', 'no filemime by default') + strictEqual(msg.getFilename(), '', 'no filename set by default') + strictEqual(msg.getFromId(), 0, 'no contact id set by default') + strictEqual(msg.getHeight(), 0, 'plain text message have height 0') + strictEqual(msg.getId(), 0, 'id 0 before sent') + strictEqual(msg.getSetupcodebegin(), '', 'no setupcode begin') + strictEqual(msg.getShowpadlock(), false, 'no padlock by default') + + const state = msg.getState() + strictEqual(state.isUndefined(), true, 'no state by default') + strictEqual(state.isFresh(), false, 'no state by default') + strictEqual(state.isNoticed(), false, 'no state by default') + strictEqual(state.isSeen(), false, 'no state by default') + strictEqual(state.isPending(), false, 'no state by default') + strictEqual(state.isFailed(), false, 'no state by default') + strictEqual(state.isDelivered(), false, 'no state by default') + strictEqual(state.isReceived(), false, 'no state by default') + + const summary = msg.getSummary() + strictEqual(summary.getId(), 0, 'no summary id') + strictEqual(summary.getState(), 0, 'no summary state') + strictEqual(summary.getText1(), null, 'no summary text1') + strictEqual(summary.getText1Meaning(), 0, 'no summary text1 meaning') + strictEqual(summary.getText2(), '', 'no summary text2') + strictEqual(summary.getTimestamp(), 0, 'no summary timestamp') + + //strictEqual(msg.getSummarytext(50), text, 'summary text is text') + strictEqual(msg.getText(), text, 'msg text set correctly') + strictEqual(msg.getTimestamp(), 0, 'no timestamp') + + const viewType = msg.getViewType() + strictEqual(viewType.isText(), true) + strictEqual(viewType.isImage(), false) + strictEqual(viewType.isGif(), false) + strictEqual(viewType.isAudio(), false) + strictEqual(viewType.isVoice(), false) + strictEqual(viewType.isVideo(), false) + strictEqual(viewType.isFile(), false) + + strictEqual(msg.getWidth(), 0, 'no message width') + strictEqual(msg.isDeadDrop(), false, 'not deaddrop') + strictEqual(msg.isForwarded(), false, 'not forwarded') + strictEqual(msg.isIncreation(), false, 'not in creation') + strictEqual(msg.isInfo(), false, 'not an info message') + strictEqual(msg.isSent(), false, 'messge is not sent') + strictEqual(msg.isSetupmessage(), false, 'not an autocrypt setup message') + + msg.latefilingMediasize(10, 20, 30) + strictEqual(msg.getWidth(), 10, 'message width set correctly') + strictEqual(msg.getHeight(), 20, 'message height set correctly') + strictEqual(msg.getDuration(), 30, 'message duration set correctly') + + msg.setDimension(100, 200) + strictEqual(msg.getWidth(), 100, 'message width set correctly') + strictEqual(msg.getHeight(), 200, 'message height set correctly') + + msg.setDuration(314) + strictEqual(msg.getDuration(), 314, 'message duration set correctly') + + expect(() => { + msg.setFile(null) + }).to.throw('Missing filename') + + const logo = join(__dirname, 'fixtures', 'logo.png') + const stat = statSync(logo) + msg.setFile(logo) + strictEqual(msg.getFilebytes(), stat.size, 'correct file size') + strictEqual(msg.getFile(), logo, 'correct file name') + strictEqual(msg.getFilemime(), 'image/png', 'mime set implicitly') + msg.setFile(logo, 'image/gif') + strictEqual(msg.getFilemime(), 'image/gif', 'mime set (in)correctly') + msg.setFile(logo, 'image/png') + strictEqual(msg.getFilemime(), 'image/png', 'mime set correctly') + + const json = msg.toJson() + expect(json).to.not.equal(null, 'not null') + strictEqual(typeof json, 'object', 'json object') + }) + + it('Contact methods', function () { + const contactId = context.createContact('First Last', 'first.last@site.org') + const contact = context.getContact(contactId) + + strictEqual(contact.getAddress(), 'first.last@site.org', 'correct address') + strictEqual(typeof contact.color, 'string', 'color is a string') + strictEqual(contact.getDisplayName(), 'First Last', 'correct display name') + strictEqual(contact.getId(), contactId, 'contact id matches') + strictEqual(contact.getName(), 'First Last', 'correct name') + strictEqual(contact.getNameAndAddress(), 'First Last (first.last@site.org)') + strictEqual(contact.getProfileImage(), null, 'no contact image') + strictEqual(contact.isBlocked(), false, 'not blocked') + strictEqual(contact.isVerified(), false, 'unverified status') + strictEqual(contact.lastSeen, 0, 'last seen unknown') + }) + + it('create contacts from address book', function () { + const addresses = [ + 'Name One', + 'name1@site.org', + 'Name Two', + 'name2@site.org', + 'Name Three', + 'name3@site.org', + ] + const count = context.addAddressBook(addresses.join('\n')) + strictEqual(count, addresses.length / 2) + context + .getContacts(0, 'Name ') + .map((id) => context.getContact(id)) + .forEach((contact) => { + expect(contact.getName().startsWith('Name ')).to.be.true + }) + }) + + it('delete contacts', function () { + const id = context.createContact('someuser', 'someuser@site.com') + const contact = context.getContact(id) + strictEqual(contact.getId(), id, 'contact id matches') + strictEqual(context.deleteContact(id), true, 'delete call succesful') + strictEqual(context.getContact(id), null, 'contact is gone') + }) + + it('adding and removing a contact from a chat', function () { + const chatId = context.createGroupChat('adding_and_removing') + const contactId = context.createContact('Add Remove', 'add.remove@site.com') + strictEqual( + context.addContactToChat(chatId, contactId), + true, + 'contact added' + ) + strictEqual( + context.isContactInChat(chatId, contactId), + true, + 'contact in chat' + ) + strictEqual( + context.removeContactFromChat(chatId, contactId), + true, + 'contact removed' + ) + strictEqual( + context.isContactInChat(chatId, contactId), + false, + 'contact not in chat' + ) + }) + + it('blocking contacts', function () { + const id = context.createContact('badcontact', 'bad@site.com') + + strictEqual(context.getBlockedCount(), 0) + strictEqual(context.getContact(id).isBlocked(), false) + expect(context.getBlockedContacts()).to.be.empty + + context.blockContact(id, true) + strictEqual(context.getBlockedCount(), 1) + strictEqual(context.getContact(id).isBlocked(), true) + expect(context.getBlockedContacts()).to.deep.equal([id]) + + context.blockContact(id, false) + strictEqual(context.getBlockedCount(), 0) + strictEqual(context.getContact(id).isBlocked(), false) + expect(context.getBlockedContacts()).to.be.empty + }) + + it('ChatList methods', function () { + const ids = [ + context.createGroupChat('groupchat1'), + context.createGroupChat('groupchat11'), + context.createGroupChat('groupchat111'), + ] + + let chatList = context.getChatList(0, 'groupchat1', null) + strictEqual(chatList.getCount(), 3, 'should contain above chats') + expect(ids.indexOf(chatList.getChatId(0))).not.to.equal(-1) + expect(ids.indexOf(chatList.getChatId(1))).not.to.equal(-1) + expect(ids.indexOf(chatList.getChatId(2))).not.to.equal(-1) + + const lot = chatList.getSummary(0) + strictEqual(lot.getId(), 0, 'lot has no id') + strictEqual(lot.getState(), C.DC_STATE_UNDEFINED, 'correct state') + + const text = 'No messages.' + context.createGroupChat('groupchat1111') + chatList = context.getChatList(0, 'groupchat1111', null) + strictEqual( + chatList.getSummary(0).getText2(), + text, + 'custom new group message' + ) + + context.setChatVisibility(ids[0], C.DC_CHAT_VISIBILITY_ARCHIVED) + chatList = context.getChatList(C.DC_GCL_ARCHIVED_ONLY, 'groupchat1', null) + strictEqual(chatList.getCount(), 1, 'only one archived') + }) + + it('Remove qoute from (draft) message', function () { + context.addDeviceMessage('test_qoute', 'test') + const msgId = context.getChatMessages(10, 0, 0)[0] + const msg = context.messageNew() + + msg.setQuote(context.getMessage(msgId)) + expect(msg.getQuotedMessage()).to.not.be.null + msg.setQuote(null) + expect(msg.getQuotedMessage()).to.be.null + }) +}) + +describe('Integration tests', function () { + this.timeout(60 * 3000) // increase timeout to 1min + + let [dc, context, accountId, directory, account] = [ + null, + null, + null, + null, + null, + ] + + let [dc2, context2, accountId2, directory2, account2] = [ + null, + null, + null, + null, + null, + ] + + this.beforeEach(async function () { + let tmp = DeltaChat.newTemporary() + dc = tmp.dc + context = tmp.context + accountId = tmp.accountId + directory = tmp.directory + dc.startEvents() + }) + + this.afterEach(async function () { + if (context) { + try { + context.stopOngoingProcess() + } catch (error) { + console.error(error) + } + } + if (context2) { + try { + context2.stopOngoingProcess() + } catch (error) { + console.error(error) + } + } + + if (dc) { + try { + dc.stopIO() + dc.close() + } catch (error) { + console.error(error) + } + } + + dc = null + context = null + accountId = null + directory = null + + context2 = null + accountId2 = null + directory2 = null + }) + + this.beforeAll(async function () { + if (!process.env.DCC_NEW_TMP_EMAIL) { + console.log( + 'Missing DCC_NEW_TMP_EMAIL environment variable!, skip intergration tests' + ) + this.skip() + } + + account = await createTempUser(process.env.DCC_NEW_TMP_EMAIL) + if (!account || !account.email || !account.password) { + console.log( + "We didn't got back an account from the api, skip intergration tests" + ) + this.skip() + } + }) + + it('configure', async function () { + strictEqual(context.isConfigured(), false, 'should not be configured') + + // Not sure what's the best way to check the events + // TODO: check the events + + // dc.once('DC_EVENT_CONFIGURE_PROGRESS', (data) => { + // t.pass('DC_EVENT_CONFIGURE_PROGRESS called at least once') + // }) + // dc.on('DC_EVENT_ERROR', (error) => { + // console.error('DC_EVENT_ERROR', error) + // }) + // dc.on('DC_EVENT_ERROR_NETWORK', (first, error) => { + // console.error('DC_EVENT_ERROR_NETWORK', error) + // }) + + // dc.on('ALL', (event, data1, data2) => console.log('ALL', event, data1, data2)) + + await expect( + context.configure({ + addr: account.email, + mail_pw: account.password, + + displayname: 'Delta One', + selfstatus: 'From Delta One with <3', + selfavatar: join(__dirname, 'fixtures', 'avatar.png'), + }) + ).to.be.eventually.fulfilled + + strictEqual(context.getConfig('addr'), account.email, 'addr correct') + strictEqual( + context.getConfig('displayname'), + 'Delta One', + 'displayName correct' + ) + strictEqual( + context.getConfig('selfstatus'), + 'From Delta One with <3', + 'selfStatus correct' + ) + expect( + context.getConfig('selfavatar').endsWith('avatar.png'), + 'selfavatar correct' + ) + strictEqual(context.getConfig('e2ee_enabled'), '1', 'e2ee_enabled correct') + strictEqual(context.getConfig('sentbox_watch'), '0', 'sentbox_watch') + strictEqual(context.getConfig('mvbox_move'), '0', 'mvbox_move') + strictEqual( + context.getConfig('save_mime_headers'), + '', + 'save_mime_headers correct' + ) + + expect(context.getBlobdir().endsWith('db.sqlite-blobs'), 'correct blobdir') + strictEqual(context.isConfigured(), true, 'is configured') + + // whole re-configure to only change displayname: what the heck? (copied this from the old test) + await expect( + context.configure({ + addr: account.email, + mail_pw: account.password, + displayname: 'Delta Two', + selfstatus: 'From Delta One with <3', + selfavatar: join(__dirname, 'fixtures', 'avatar.png'), + }) + ).to.be.eventually.fulfilled + strictEqual( + context.getConfig('displayname'), + 'Delta Two', + 'updated displayName correct' + ) + }) + + it('Autocrypt setup - key transfer', async function () { + // Spawn a second dc instance with same account + // dc.on('ALL', (event, data1, data2) => + // console.log('FIRST ', event, data1, data2) + // ) + dc.stopIO() + await expect( + context.configure({ + addr: account.email, + mail_pw: account.password, + + displayname: 'Delta One', + selfstatus: 'From Delta One with <3', + selfavatar: join(__dirname, 'fixtures', 'avatar.png'), + }) + ).to.be.eventually.fulfilled + + const accountId2 = dc.addAccount() + console.log('accountId2:', accountId2) + context2 = dc.accountContext(accountId2) + + let setupCode = null + const waitForSetupCode = waitForSomething() + const waitForEnd = waitForSomething() + + dc.on('ALL', (event, accountId, data1, data2) => { + console.log('[' + accountId + ']', event, data1, data2) + }) + + dc.on('DC_EVENT_MSGS_CHANGED', async (aId, chatId, msgId) => { + console.log('[' + accountId + '] DC_EVENT_MSGS_CHANGED', chatId, msgId) + if ( + aId != accountId || + !context.getChat(chatId).isSelfTalk() || + !context.getMessage(msgId).isSetupmessage() + ) { + return + } + console.log('Setupcode!') + let setupCode = await waitForSetupCode.promise + // console.log('incoming msg', { setupCode }) + const messages = context.getChatMessages(chatId, 0, 0) + expect(messages.indexOf(msgId) !== -1, 'msgId is in chat messages').to.be + .true + const result = await context.continueKeyTransfer(msgId, setupCode) + expect(result === true, 'continueKeyTransfer was successful').to.be.true + + waitForEnd.done() + }) + + dc.stopIO() + await expect( + context2.configure({ + addr: account.email, + mail_pw: account.password, + + displayname: 'Delta One', + selfstatus: 'From Delta One with <3', + selfavatar: join(__dirname, 'fixtures', 'avatar.png'), + }) + ).to.be.eventually.fulfilled + dc.startIO() + + console.log('Sending autocrypt setup code') + setupCode = await context2.initiateKeyTransfer() + console.log('Sent autocrypt setup code') + waitForSetupCode.done(setupCode) + console.log('setupCode is: ' + setupCode) + expect(typeof setupCode).to.equal('string', 'setupCode is string') + + await waitForEnd.promise + }) + + it('configure using invalid password should fail', async function () { + await expect( + context.configure({ + addr: 'hpk5@testrun.org', + mail_pw: 'asd', + }) + ).to.be.eventually.rejected + }) +}) + +/** + * @returns {{done: (result?)=>void, promise:Promise }} + */ +function waitForSomething() { + let resolvePromise + const promise = new Promise((res, rej) => { + resolvePromise = res + }) + return { + done: resolvePromise, + promise, + } +} diff --git a/node/tsconfig.json b/node/tsconfig.json new file mode 100644 index 000000000..aa1b9dbc6 --- /dev/null +++ b/node/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "./lib", + "sourceMap": true, + "module": "commonjs", + "target": "es5", + "esModuleInterop": true, + "declaration": true, + "declarationMap": true, + "strictNullChecks": true, + "strict": true + }, + "exclude": ["node_modules", "deltachat-core-rust", "dist", "scripts"], + "typedocOptions": { + "mode": "file", + "out": "docs", + "excludeNotExported": true, + "defaultCategory": "index", + "includeVersion": true + } +} \ No newline at end of file diff --git a/node/windows.md b/node/windows.md new file mode 100644 index 000000000..4916c0fdb --- /dev/null +++ b/node/windows.md @@ -0,0 +1,37 @@ +> Steps on how to get windows set up properly for the node bindings + +## install git + +E.g via + +## install node + +Download and install `v16` from + +## install rust + +Download and run `rust-init.exe` from + +## configure node for native addons + +``` +$ npm i node-gyp -g +$ npm i windows-build-tools -g +``` + +`windows-build-tools` will install `Visual Studio 2017` by default and should not mess with existing installations of `Visual Studio C++`. + +## get the code + +``` +$ mkdir -p src/deltachat +$ cd src/deltachat +$ git clone https://github.com/deltachat/deltachat-node +``` + +## build the code + +``` +$ cd src/deltachat/deltachat-node +$ npm install +```