diff --git a/packages/simplex_app/.github/FUNDING.yml b/packages/simplex_app/.github/FUNDING.yml
new file mode 100644
index 0000000000..395480a7d4
--- /dev/null
+++ b/packages/simplex_app/.github/FUNDING.yml
@@ -0,0 +1 @@
+open_collective: simplex-chat
diff --git a/packages/simplex_app/.github/changelog_conf.json b/packages/simplex_app/.github/changelog_conf.json
new file mode 100644
index 0000000000..e4c7eab9b1
--- /dev/null
+++ b/packages/simplex_app/.github/changelog_conf.json
@@ -0,0 +1,4 @@
+{
+ "template": "${{UNCATEGORIZED}}",
+ "pr_template": "- ${{TITLE}}\n"
+}
diff --git a/packages/simplex_app/.github/workflows/build.yml b/packages/simplex_app/.github/workflows/build.yml
new file mode 100644
index 0000000000..723b1d4a39
--- /dev/null
+++ b/packages/simplex_app/.github/workflows/build.yml
@@ -0,0 +1,102 @@
+name: build
+
+on:
+ push:
+ branches:
+ - master
+ - v5
+ tags:
+ - "v*"
+ pull_request:
+
+jobs:
+ prepare-release:
+ if: startsWith(github.ref, 'refs/tags/v')
+ runs-on: ubuntu-latest
+ steps:
+ - name: Clone project
+ uses: actions/checkout@v2
+
+ - name: Build changelog
+ id: build_changelog
+ uses: mikepenz/release-changelog-builder-action@v1
+ with:
+ configuration: .github/changelog_conf.json
+ failOnError: true
+ ignorePreReleases: true
+ commitMode: true
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+ - name: Create release
+ uses: softprops/action-gh-release@v1
+ with:
+ body: ${{ steps.build_changelog.outputs.changelog }}
+ files: |
+ LICENSE
+ fail_on_unmatched_files: true
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+ build:
+ name: build-${{ matrix.os }}
+ if: always()
+ needs: prepare-release
+ runs-on: ${{ matrix.os }}
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - os: ubuntu-20.04
+ cache_path: ~/.stack
+ stack_args: "--test"
+ artifact_rel_path: /bin/simplex-chat
+ asset_name: simplex-chat-ubuntu-20_04-x86-64
+ - os: ubuntu-18.04
+ cache_path: ~/.stack
+ stack_args: "--test"
+ artifact_rel_path: /bin/simplex-chat
+ asset_name: simplex-chat-ubuntu-18_04-x86-64
+ - os: macos-latest
+ cache_path: ~/.stack
+ stack_args: "--test"
+ artifact_rel_path: /bin/simplex-chat
+ asset_name: simplex-chat-macos-x86-64
+ # TODO enable tests for windows once fixed (remove stack_args altogether)
+ - os: windows-latest
+ cache_path: C:/sr
+ stack_args: ""
+ artifact_rel_path: /bin/simplex-chat.exe
+ asset_name: simplex-chat-windows-x86-64
+ steps:
+ - name: Clone project
+ uses: actions/checkout@v2
+
+ - name: Setup Stack
+ uses: haskell/actions/setup@v1
+ with:
+ ghc-version: '8.8.4'
+ enable-stack: true
+ stack-version: 'latest'
+
+ - name: Cache dependencies
+ uses: actions/cache@v2
+ with:
+ path: ${{ matrix.cache_path }}
+ key: ${{ matrix.os }}-${{ hashFiles('stack.yaml') }}
+
+ - name: Build & test
+ id: build_test
+ working-directory: ./haskell
+ run: |
+ stack build ${{ matrix.stack_args }}
+ echo "::set-output name=LOCAL_INSTALL_ROOT::$(stack path --local-install-root)"
+
+ - name: Upload binaries to release
+ if: startsWith(github.ref, 'refs/tags/v')
+ uses: svenstaro/upload-release-action@v2
+ with:
+ repo_token: ${{ secrets.GITHUB_TOKEN }}
+ file: ${{ steps.build_test.outputs.LOCAL_INSTALL_ROOT }}${{ matrix.artifact_rel_path }}
+ asset_name: ${{ matrix.asset_name }}
+ tag: ${{ github.ref }}
diff --git a/packages/simplex_app/.gitignore b/packages/simplex_app/.gitignore
new file mode 100644
index 0000000000..c75fab8dab
--- /dev/null
+++ b/packages/simplex_app/.gitignore
@@ -0,0 +1,93 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+.DS_Store
+
+# Haskell
+dist
+dist-*
+cabal-dev
+*.o
+*.hi
+*.hie
+*.chi
+*.chs.h
+*.dyn_o
+*.dyn_hi
+.hpc
+.hsenv
+.cabal-sandbox/
+cabal.sandbox.config
+*.prof
+*.aux
+*.hp
+*.eventlog
+.stack-work/
+cabal.project.local
+cabal.project.local~
+.HTF/
+.ghc.environment.*
+*.cabal
+stack.yaml.lock
+
+# chat database
+*.db
+*.db.bak
+
+# Misc
+*.class
+*.log
+*.pyc
+*.swp
+.DS_Store
+.atom/
+.buildlog/
+.history
+.svn/
+
+# IntelliJ
+*.iml
+*.ipr
+*.iws
+.idea/
+
+# VS Code
+.vscode/
+
+# Flutter/Dart/Pub
+**/doc/api/
+**/ios/Flutter/.last_build_id
+.dart_tool/
+.flutter-plugins
+.flutter-plugins-dependencies
+.packages
+.pub-cache/
+.pub/
+/build/
+
+# Web
+lib/generated_plugin_registrant.dart
+
+# Symbolication
+app.*.symbols
+
+# Obfuscation
+app.*.map.json
+
+# Android Studio build artifacts
+/android/app/debug
+/android/app/profile
+/android/app/release
diff --git a/packages/simplex_app/LICENSE b/packages/simplex_app/LICENSE
new file mode 100644
index 0000000000..0ad25db4bd
--- /dev/null
+++ b/packages/simplex_app/LICENSE
@@ -0,0 +1,661 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 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 Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are 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.
+
+ 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.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ 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 Affero 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. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ 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 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 work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero 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 Affero 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 Affero 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 Affero 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 Affero 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 Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ 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 AGPL, see
+.
diff --git a/packages/simplex_app/README.md b/packages/simplex_app/README.md
index 08c1f529ab..3cce18d850 100644
--- a/packages/simplex_app/README.md
+++ b/packages/simplex_app/README.md
@@ -1,16 +1,232 @@
-# simplex_app
+
-A new Flutter project.
+# SimpleX chat
-## Getting Started
+## Private, secure, decentralized
-This project is a starting point for a Flutter application.
+[](https://github.com/simplex-chat/simplex-chat/actions?query=workflow%3Abuild)
+[](https://github.com/simplex-chat/simplex-chat/releases)
-A few resources to get you started if this is your first Flutter project:
+> **NEW in v0.4: [groups](#groups) and [sending files](#sending-files)!**
-- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
-- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
+The motivation for SimpleX chat is [presented here](./simplex.md).
-For help getting started with Flutter, view our
-[online documentation](https://flutter.dev/docs), which offers tutorials,
-samples, guidance on mobile development, and a full API reference.
+SimpleX chat prototype is a thin terminal UI on top of [SimpleXMQ](https://github.com/simplex-chat/simplexmq) message broker that uses [SMP protocols](https://github.com/simplex-chat/simplexmq/blob/master/protocol).
+
+See [simplex.chat](https://simplex.chat) website for chat demo and the explanations of the system and how SMP protocol works.
+
+
+
+## Table of contents
+
+- [Disclaimer](#disclaimer)
+- [Network topology](#network-topology)
+- [Terminal chat features](#terminal-chat-features)
+- [Installation](#installation)
+ - [Download chat client](#download-chat-client)
+ - [Build from source](#build-from-source)
+ - [Using Docker](#using-docker)
+ - [Using Haskell stack](#using-haskell-stack)
+- [Usage](#usage)
+ - [Running the chat client](#running-the-chat-client)
+ - [How to use SimpleX chat](#how-to-use-simplex-chat)
+ - [Groups](#groups)
+ - [Sending files](#sending-files)
+ - [Access chat history](#access-chat-history)
+- [Future roadmap](#future-roadmap)
+- [License](#license)
+
+## Disclaimer
+
+This is WIP implementation of SimpleX chat that implements a new network topology for asynchronous communication combining the advantages and avoiding the disadvantages of federated and P2P networks.
+
+If you expect a software being reliable most of the time and doing something useful, then this is probably not ready for you yet. We do use it for terminal chat though, and it seems to work most of the time - we would really appreciate if you try it and give us your feedback.
+
+**Please note:** The main differentiation of SimpleX network is the approach to internet message routing rather than encryption; for that reason no sufficient attention was paid to either TCP transport level encryption or to E2E encryption protocols - they are implemented in an ad hoc way based on RSA and AES algorithms. See [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a) on TCP transport encryption protocol (AEAD-GCM scheme, with an AES key negotiation based on RSA key hash known to the client in advance) and [this section](https://github.com/simplex-chat/simplexmq/blob/master/rfcs/2021-01-26-crypto.md#e2e-encryption) on E2E encryption protocol (an ad hoc hybrid scheme a la PGP). These protocols will change in a consumer ready version to something more robust.
+
+## Network topology
+
+SimpleX is a decentralized client-server network that uses redundant, disposable nodes to asynchronously pass the messages via message queues, providing receiver and sender anonymity.
+
+Unlike P2P networks, all messages are passed through one or several (for redundancy) servers, that do not even need to have persistence (in fact, the current [SMP server implementation](https://github.com/simplex-chat/simplexmq#smp-server) uses in-memory message storage, persisting only the queue records) - it provides better metadata protection than P2P designs, as no global participant ID is required, and avoids many [problems of P2P networks](https://github.com/simplex-chat/simplex-chat/blob/master/simplex.md#comparison-with-p2p-messaging-protocols).
+
+Unlike federated networks, the participating server nodes do NOT have records of the users, do NOT communicate with each other, do NOT store messages after they are delivered to the recipients, and there is no way to discover the full list of participating servers - it avoids the problem of metadata visibility that federated networks suffer from and better protects the network, as servers do not communicate with each other. Each server node provides unidirectional "dumb pipes" to the users, that do authorization without authentication, having no knowledge of the the users or their contacts. Each queue is assigned two RSA keys - one for receiver and one for sender - and each queue access is authorized with a signature created using a respective key's private counterpart.
+
+The routing of messages relies on the knowledge of client devices how user contacts and groups map at any given moment of time to these disposable queues on server nodes.
+
+## Terminal chat features
+
+- 1-to-1 chat with multiple people in the same terminal window.
+- Group messaging.
+- Sending files to contacts and groups.
+- Auto-populated recipient name - just type your messages to reply to the sender once the connection is established.
+- Demo SMP servers available and pre-configured in the app - or you can [deploy your own server](https://github.com/simplex-chat/simplexmq#using-smp-server-and-smp-agent).
+- No global identity or any names visible to the server(s), ensuring full privacy of your contacts and conversations.
+- E2E encryption, with RSA public key that has to be passed out-of-band (see [How to use SimpleX chat](#how-to-use-simplex-chat)).
+- Message signing and verification with automatically generated RSA keys.
+- Message integrity validation (via including the digests of the previous messages).
+- Authentication of each command/message by SMP servers with automatically generated RSA key pairs.
+- TCP transport encryption using SMP transport protocol.
+
+RSA keys are not used as identity, they are randomly generated for each contact.
+
+## Installation
+
+### Download chat client
+
+Download the chat binary for your system from the [latest stable release](https://github.com/simplex-chat/simplex-chat/releases) and make it executable as shown below.
+
+#### Linux and MacOS
+
+```sh
+chmod +x
+mv ~/.local/bin/simplex-chat
+```
+
+(or any other preferred location on PATH).
+
+On MacOS you also need to [allow Gatekeeper to run it](https://support.apple.com/en-us/HT202491).
+
+#### Windows
+
+```sh
+move %APPDATA%/local/bin/simplex-chat.exe
+```
+
+### Build from source
+
+#### Using Docker
+
+On Linux, you can build the chat executable using [docker build with custom output](https://docs.docker.com/engine/reference/commandline/build/#custom-build-outputs):
+
+```shell
+$ git clone git@github.com:simplex-chat/simplex-chat.git
+$ cd simplex-chat
+$ DOCKER_BUILDKIT=1 docker build --output ~/.local/bin .
+```
+
+> **Please note:** If you encounter ``version `GLIBC_2.28' not found`` error, rebuild it with `haskell:8.8.4-stretch` base image (change it in your local [Dockerfile](Dockerfile)).
+
+#### Using Haskell stack
+
+Install [Haskell stack](https://docs.haskellstack.org/en/stable/README/):
+
+```shell
+curl -sSL https://get.haskellstack.org/ | sh
+```
+
+and build the project:
+
+```shell
+$ git clone git@github.com:simplex-chat/simplex-chat.git
+$ cd simplex-chat
+$ stack install
+```
+
+## Usage
+
+### Running the chat client
+
+To start the chat client, run `simplex-chat` from the terminal.
+
+By default, app data directory is created in the home directory (`~/.simplex`, or `%APPDATA%/simplex` on Windows), and two SQLite database files `simplex.chat.db` and `simplex.agent.db` are initialized in it.
+
+To specify a different file path prefix for the database files use `-d` command line option:
+
+```shell
+$ simplex-chat -d alice
+```
+
+Running above, for example, would create `alice.chat.db` and `alice.agent.db` database files in current directory.
+
+Default SMP servers are hosted on Linode (London, UK and Fremont, CA) - they are [pre-configured in the app](https://github.com/simplex-chat/simplex-chat/blob/master/src/Simplex/Chat/Options.hs#L40). Base-64 encoded string after server host is the transport key digest.
+
+If you deployed your own SMP server(s) you can configure client via `-s` option:
+
+```shell
+$ simplex-chat -s smp.example.com:5223#KXNE1m2E1m0lm92WGKet9CL6+lO742Vy5G6nsrkvgs8=
+```
+
+The base-64 encoded string in server address is the digest of RSA transport handshake key that the server will generate on the first run and output its digest.
+
+You can still talk to people using default or any other server - it only affects the location of the message queue when you initiate the connection (and the reply queue can be on another server, as set by the other party's client).
+
+Run `simplex-chat -h` to see all available options.
+
+### How to use SimpleX chat
+
+Once you have started the chat, you will be prompted to specify your "display name" and an optional "full name" to create a local chat profile. Your display name is an alias for your contacts to refer to you by - it is not unique and does not serve as a global identity. In case different contacts chose the same display name, the chat client adds a numeric suffix to their local display names.
+
+This diagram shows how to connect and message a contact:
+
+
+
+
+
+Once you've set up your local profile, enter `/c` (for `/connect`) to create a new connection and generate an invitation. Send this invitation to your contact via any other channel.
+
+You are able to create multiple invitations by entering `/connect` multiple times and sending these invitations to the corresponding contacts you'd like to connect with.
+
+The invitation has the format `smp::::::`. The invitation can only be used once and even if this is intercepted, the attacker would not be able to use it to send you the messages via this queue once your contact confirms that the connection is established.
+
+The contact who received the invitation should enter `/c ` to accept the connection. This establishes the connection, and both parties are notified.
+
+They would then use `@` commands to send messages. You may also just start typing a message to send it to the contact that was the last.
+
+Use `/help` in chat to see the list of available commands.
+
+### Groups
+
+To create a group use `/g `, then add contacts to it with `/a `and send messages with `#`. Use `/help groups` for other commands.
+
+
+
+> **Please note**: the groups are not stored on any server, they are maintained as a list of members in the app database to whom the messages will be sent.
+
+### Sending files
+
+You can send a file to your contact with `/f @` - the recipient will have to accept it before it is sent. Use `/help files` for other commands.
+
+
+
+You can send files to a group with `/f #`.
+
+### Access chat history
+
+> 🚧 **Section currently out of date - will be updated soon** 🏗
+
+SimpleX chat stores all your contacts and conversations in a local database file, making it private and portable by design, fully owned and controlled by you.
+
+You can search your chat history via SQLite database file:
+
+```
+sqlite3 ~/.simplex/smp-chat.db
+```
+
+Now you can query `messages` table, for example:
+
+```sql
+select * from messages
+where conn_alias = cast('alice' as blob)
+ and body like '%cats%'
+order by internal_id desc;
+```
+
+> **Please note:** SQLite foreign key constraints are disabled by default, and must be **[enabled separately for each database connection](https://sqlite.org/foreignkeys.html#fk_enable)**. The latter can be achieved by running `PRAGMA foreign_keys = ON;` command on an open database connection. By running data altering queries without enabling foreign keys prior to that, you may risk putting your database in an inconsistent state.
+
+## Future roadmap
+
+1. Mobile and desktop apps (in progress).
+2. SMP protocol improvements:
+ - SMP queue redundancy and rotation.
+ - Message delivery confirmation.
+ - Support multiple devices.
+3. Privacy-preserving identity server for optional DNS-based contact/group addresses to simplify connection and discovery, but not used to deliver messages:
+ - keep all your contacts and groups even if you lose the domain.
+ - the server doesn't have information about your contacts and groups.
+4. Media server to optimize sending large files to groups.
+5. Channels server for large groups and broadcast channels.
+
+## License
+
+[AGPL v3](./LICENSE)
diff --git a/packages/simplex_app/analysis_options.yaml b/packages/simplex_app/analysis_options.yaml
index fa814e0073..61b6c4de17 100644
--- a/packages/simplex_app/analysis_options.yaml
+++ b/packages/simplex_app/analysis_options.yaml
@@ -1,45 +1,29 @@
-# https://dart.dev/guides/language/analysis-options
+# This file configures the analyzer, which statically analyzes Dart code to
+# check for errors, warnings, and lints.
+#
+# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
+# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
+# invoked from the command line by running `flutter analyze`.
+
+# The following line activates a set of recommended lints for Flutter apps,
+# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
linter:
+ # The lint rules applied to this project can be customized in the
+ # section below to disable rules from the `package:flutter_lints/flutter.yaml`
+ # included above or to enable additional rules. A list of all available lints
+ # and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
+ #
+ # Instead of disabling a lint rule for the entire project in the
+ # section below, it can also be suppressed for a single line of code
+ # or a specific dart file by using the `// ignore: name_of_lint` and
+ # `// ignore_for_file: name_of_lint` syntax on the line or in the file
+ # producing the lint.
rules:
- prefer_double_quotes: true
- always_declare_return_types: true
- avoid_dynamic_calls: true
- avoid_empty_else: true
- avoid_relative_lib_imports: true
- avoid_shadowing_type_parameters: true
- avoid_slow_async_io: true
- avoid_types_as_parameter_names: true
- await_only_futures: true
- camel_case_extensions: true
- camel_case_types: true
- cancel_subscriptions: true
- curly_braces_in_flow_control_structures: true
- directives_ordering: true
- empty_catches: true
- hash_and_equals: true
- iterable_contains_unrelated_type: true
- list_remove_unrelated_type: true
- no_adjacent_strings_in_list: true
- no_duplicate_case_values: true
- package_api_docs: true
- package_prefixed_library_names: true
- prefer_generic_function_type_aliases: true
- prefer_is_empty: true
- prefer_is_not_empty: true
- prefer_iterable_whereType: true
- prefer_typing_uninitialized_variables: true
- sort_child_properties_last: true
- test_types_in_equals: true
- throw_in_finally: true
- unawaited_futures: true
- unnecessary_import: true
- unnecessary_null_aware_assignments: true
- unnecessary_statements: true
- unnecessary_type_check: true
- unrelated_type_equality_checks: true
- unsafe_html: true
- use_full_hex_values_for_flutter_colors: true
- valid_regexps: true
+ # avoid_print: false # Uncomment to disable the `avoid_print` rule
+ # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
+
+# Additional information about this file can be found at
+# https://dart.dev/guides/language/analysis-options
diff --git a/packages/simplex_app/android/app/build.gradle b/packages/simplex_app/android/app/build.gradle
index c9d2295f7c..511daef851 100644
--- a/packages/simplex_app/android/app/build.gradle
+++ b/packages/simplex_app/android/app/build.gradle
@@ -43,7 +43,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
- applicationId "chat.simplex.app"
+ applicationId "com.example.simplex_chat"
minSdkVersion 16
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
diff --git a/packages/simplex_app/android/app/src/debug/AndroidManifest.xml b/packages/simplex_app/android/app/src/debug/AndroidManifest.xml
index 687ce82258..d79e2bbcb1 100644
--- a/packages/simplex_app/android/app/src/debug/AndroidManifest.xml
+++ b/packages/simplex_app/android/app/src/debug/AndroidManifest.xml
@@ -1,5 +1,5 @@
+ package="com.example.simplex_chat">
diff --git a/packages/simplex_app/android/app/src/main/AndroidManifest.xml b/packages/simplex_app/android/app/src/main/AndroidManifest.xml
index 2da9cbe58f..ee2f690113 100644
--- a/packages/simplex_app/android/app/src/main/AndroidManifest.xml
+++ b/packages/simplex_app/android/app/src/main/AndroidManifest.xml
@@ -1,7 +1,7 @@
+ package="com.example.simplex_chat">
+ package="com.example.simplex_chat">
diff --git a/packages/simplex_app/assets/connection.gif b/packages/simplex_app/assets/connection.gif
new file mode 100644
index 0000000000..f15f7bcb09
Binary files /dev/null and b/packages/simplex_app/assets/connection.gif differ
diff --git a/packages/simplex_app/assets/dp.png b/packages/simplex_app/assets/dp.png
new file mode 100644
index 0000000000..bd07199e06
Binary files /dev/null and b/packages/simplex_app/assets/dp.png differ
diff --git a/packages/simplex_app/assets/files.gif b/packages/simplex_app/assets/files.gif
new file mode 100644
index 0000000000..ad8beaa183
Binary files /dev/null and b/packages/simplex_app/assets/files.gif differ
diff --git a/packages/simplex_app/assets/groups.gif b/packages/simplex_app/assets/groups.gif
new file mode 100644
index 0000000000..0ce6dc30ec
Binary files /dev/null and b/packages/simplex_app/assets/groups.gif differ
diff --git a/packages/simplex_app/assets/how-to-use-simplex.svg b/packages/simplex_app/assets/how-to-use-simplex.svg
new file mode 100644
index 0000000000..1be91eb293
--- /dev/null
+++ b/packages/simplex_app/assets/how-to-use-simplex.svg
@@ -0,0 +1,32 @@
+
diff --git a/packages/simplex_app/assets/logo.svg b/packages/simplex_app/assets/logo.svg
new file mode 100644
index 0000000000..bb4e50787a
--- /dev/null
+++ b/packages/simplex_app/assets/logo.svg
@@ -0,0 +1,12 @@
+
diff --git a/packages/simplex_app/assets/menu.svg b/packages/simplex_app/assets/menu.svg
new file mode 100644
index 0000000000..619f5cfb6b
--- /dev/null
+++ b/packages/simplex_app/assets/menu.svg
@@ -0,0 +1,4 @@
+
diff --git a/packages/simplex_app/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill b/packages/simplex_app/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill
deleted file mode 100644
index b372cb8899..0000000000
Binary files a/packages/simplex_app/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill and /dev/null differ
diff --git a/packages/simplex_app/build/unit_test_assets/AssetManifest.json b/packages/simplex_app/build/unit_test_assets/AssetManifest.json
deleted file mode 100644
index 03eaddffb9..0000000000
--- a/packages/simplex_app/build/unit_test_assets/AssetManifest.json
+++ /dev/null
@@ -1 +0,0 @@
-{"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
\ No newline at end of file
diff --git a/packages/simplex_app/build/unit_test_assets/FontManifest.json b/packages/simplex_app/build/unit_test_assets/FontManifest.json
deleted file mode 100644
index 464ab5882a..0000000000
--- a/packages/simplex_app/build/unit_test_assets/FontManifest.json
+++ /dev/null
@@ -1 +0,0 @@
-[{"family":"MaterialIcons","fonts":[{"asset":"fonts/MaterialIcons-Regular.otf"}]},{"family":"packages/cupertino_icons/CupertinoIcons","fonts":[{"asset":"packages/cupertino_icons/assets/CupertinoIcons.ttf"}]}]
\ No newline at end of file
diff --git a/packages/simplex_app/build/unit_test_assets/NOTICES.Z b/packages/simplex_app/build/unit_test_assets/NOTICES.Z
deleted file mode 100644
index e4a010d7e5..0000000000
Binary files a/packages/simplex_app/build/unit_test_assets/NOTICES.Z and /dev/null differ
diff --git a/packages/simplex_app/build/unit_test_assets/fonts/MaterialIcons-Regular.otf b/packages/simplex_app/build/unit_test_assets/fonts/MaterialIcons-Regular.otf
deleted file mode 100644
index 3246ad559e..0000000000
Binary files a/packages/simplex_app/build/unit_test_assets/fonts/MaterialIcons-Regular.otf and /dev/null differ
diff --git a/packages/simplex_app/build/unit_test_assets/packages/cupertino_icons/assets/CupertinoIcons.ttf b/packages/simplex_app/build/unit_test_assets/packages/cupertino_icons/assets/CupertinoIcons.ttf
deleted file mode 100644
index 79ba7ea083..0000000000
Binary files a/packages/simplex_app/build/unit_test_assets/packages/cupertino_icons/assets/CupertinoIcons.ttf and /dev/null differ
diff --git a/packages/simplex_app/haskell/Dockerfile b/packages/simplex_app/haskell/Dockerfile
new file mode 100644
index 0000000000..9554d69fff
--- /dev/null
+++ b/packages/simplex_app/haskell/Dockerfile
@@ -0,0 +1,10 @@
+FROM haskell:8.10.4 AS build-stage
+# if you encounter "version `GLIBC_2.28' not found" error when running
+# chat client executable, build with the following base image instead:
+# FROM haskell:8.10.4-stretch AS build-stage
+COPY . /project
+WORKDIR /project
+RUN stack install
+
+FROM scratch AS export-stage
+COPY --from=build-stage /root/.local/bin/simplex-chat /
diff --git a/packages/simplex_app/haskell/README.md b/packages/simplex_app/haskell/README.md
new file mode 100644
index 0000000000..b87f8590f0
--- /dev/null
+++ b/packages/simplex_app/haskell/README.md
@@ -0,0 +1,232 @@
+
+
+# SimpleX chat (legacy Haskell terminal prototype)
+
+## Private, secure, decentralized
+
+[](https://github.com/simplex-chat/simplex-chat/actions?query=workflow%3Abuild)
+[](https://github.com/simplex-chat/simplex-chat/releases)
+
+> **NEW in v0.4: [groups](#groups) and [sending files](#sending-files)!**
+
+The motivation for SimpleX chat is [presented here](./simplex.md).
+
+SimpleX chat prototype is a thin terminal UI on top of [SimpleXMQ](https://github.com/simplex-chat/simplexmq) message broker that uses [SMP protocols](https://github.com/simplex-chat/simplexmq/blob/master/protocol).
+
+See [simplex.chat](https://simplex.chat) website for chat demo and the explanations of the system and how SMP protocol works.
+
+
+
+## Table of contents
+
+- [Disclaimer](#disclaimer)
+- [Network topology](#network-topology)
+- [Terminal chat features](#terminal-chat-features)
+- [Installation](#installation)
+ - [Download chat client](#download-chat-client)
+ - [Build from source](#build-from-source)
+ - [Using Docker](#using-docker)
+ - [Using Haskell stack](#using-haskell-stack)
+- [Usage](#usage)
+ - [Running the chat client](#running-the-chat-client)
+ - [How to use SimpleX chat](#how-to-use-simplex-chat)
+ - [Groups](#groups)
+ - [Sending files](#sending-files)
+ - [Access chat history](#access-chat-history)
+- [Future roadmap](#future-roadmap)
+- [License](#license)
+
+## Disclaimer
+
+This is WIP implementation of SimpleX chat that implements a new network topology for asynchronous communication combining the advantages and avoiding the disadvantages of federated and P2P networks.
+
+If you expect a software being reliable most of the time and doing something useful, then this is probably not ready for you yet. We do use it for terminal chat though, and it seems to work most of the time - we would really appreciate if you try it and give us your feedback.
+
+**Please note:** The main differentiation of SimpleX network is the approach to internet message routing rather than encryption; for that reason no sufficient attention was paid to either TCP transport level encryption or to E2E encryption protocols - they are implemented in an ad hoc way based on RSA and AES algorithms. See [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a) on TCP transport encryption protocol (AEAD-GCM scheme, with an AES key negotiation based on RSA key hash known to the client in advance) and [this section](https://github.com/simplex-chat/simplexmq/blob/master/rfcs/2021-01-26-crypto.md#e2e-encryption) on E2E encryption protocol (an ad hoc hybrid scheme a la PGP). These protocols will change in a consumer ready version to something more robust.
+
+## Network topology
+
+SimpleX is a decentralized client-server network that uses redundant, disposable nodes to asynchronously pass the messages via message queues, providing receiver and sender anonymity.
+
+Unlike P2P networks, all messages are passed through one or several (for redundancy) servers, that do not even need to have persistence (in fact, the current [SMP server implementation](https://github.com/simplex-chat/simplexmq#smp-server) uses in-memory message storage, persisting only the queue records) - it provides better metadata protection than P2P designs, as no global participant ID is required, and avoids many [problems of P2P networks](https://github.com/simplex-chat/simplex-chat/blob/master/simplex.md#comparison-with-p2p-messaging-protocols).
+
+Unlike federated networks, the participating server nodes do NOT have records of the users, do NOT communicate with each other, do NOT store messages after they are delivered to the recipients, and there is no way to discover the full list of participating servers - it avoids the problem of metadata visibility that federated networks suffer from and better protects the network, as servers do not communicate with each other. Each server node provides unidirectional "dumb pipes" to the users, that do authorization without authentication, having no knowledge of the the users or their contacts. Each queue is assigned two RSA keys - one for receiver and one for sender - and each queue access is authorized with a signature created using a respective key's private counterpart.
+
+The routing of messages relies on the knowledge of client devices how user contacts and groups map at any given moment of time to these disposable queues on server nodes.
+
+## Terminal chat features
+
+- 1-to-1 chat with multiple people in the same terminal window.
+- Group messaging.
+- Sending files to contacts and groups.
+- Auto-populated recipient name - just type your messages to reply to the sender once the connection is established.
+- Demo SMP servers available and pre-configured in the app - or you can [deploy your own server](https://github.com/simplex-chat/simplexmq#using-smp-server-and-smp-agent).
+- No global identity or any names visible to the server(s), ensuring full privacy of your contacts and conversations.
+- E2E encryption, with RSA public key that has to be passed out-of-band (see [How to use SimpleX chat](#how-to-use-simplex-chat)).
+- Message signing and verification with automatically generated RSA keys.
+- Message integrity validation (via including the digests of the previous messages).
+- Authentication of each command/message by SMP servers with automatically generated RSA key pairs.
+- TCP transport encryption using SMP transport protocol.
+
+RSA keys are not used as identity, they are randomly generated for each contact.
+
+## Installation
+
+### Download chat client
+
+Download the chat binary for your system from the [latest stable release](https://github.com/simplex-chat/simplex-chat/releases) and make it executable as shown below.
+
+#### Linux and MacOS
+
+```sh
+chmod +x
+mv ~/.local/bin/simplex-chat
+```
+
+(or any other preferred location on PATH).
+
+On MacOS you also need to [allow Gatekeeper to run it](https://support.apple.com/en-us/HT202491).
+
+#### Windows
+
+```sh
+move %APPDATA%/local/bin/simplex-chat.exe
+```
+
+### Build from source
+
+#### Using Docker
+
+On Linux, you can build the chat executable using [docker build with custom output](https://docs.docker.com/engine/reference/commandline/build/#custom-build-outputs):
+
+```shell
+$ git clone git@github.com:simplex-chat/simplex-chat.git
+$ cd simplex-chat
+$ DOCKER_BUILDKIT=1 docker build --output ~/.local/bin .
+```
+
+> **Please note:** If you encounter ``version `GLIBC_2.28' not found`` error, rebuild it with `haskell:8.8.4-stretch` base image (change it in your local [Dockerfile](Dockerfile)).
+
+#### Using Haskell stack
+
+Install [Haskell stack](https://docs.haskellstack.org/en/stable/README/):
+
+```shell
+curl -sSL https://get.haskellstack.org/ | sh
+```
+
+and build the project:
+
+```shell
+$ git clone git@github.com:simplex-chat/simplex-chat.git
+$ cd simplex-chat
+$ stack install
+```
+
+## Usage
+
+### Running the chat client
+
+To start the chat client, run `simplex-chat` from the terminal.
+
+By default, app data directory is created in the home directory (`~/.simplex`, or `%APPDATA%/simplex` on Windows), and two SQLite database files `simplex.chat.db` and `simplex.agent.db` are initialized in it.
+
+To specify a different file path prefix for the database files use `-d` command line option:
+
+```shell
+$ simplex-chat -d alice
+```
+
+Running above, for example, would create `alice.chat.db` and `alice.agent.db` database files in current directory.
+
+Default SMP servers are hosted on Linode (London, UK and Fremont, CA) - they are [pre-configured in the app](https://github.com/simplex-chat/simplex-chat/blob/master/src/Simplex/Chat/Options.hs#L40). Base-64 encoded string after server host is the transport key digest.
+
+If you deployed your own SMP server(s) you can configure client via `-s` option:
+
+```shell
+$ simplex-chat -s smp.example.com:5223#KXNE1m2E1m0lm92WGKet9CL6+lO742Vy5G6nsrkvgs8=
+```
+
+The base-64 encoded string in server address is the digest of RSA transport handshake key that the server will generate on the first run and output its digest.
+
+You can still talk to people using default or any other server - it only affects the location of the message queue when you initiate the connection (and the reply queue can be on another server, as set by the other party's client).
+
+Run `simplex-chat -h` to see all available options.
+
+### How to use SimpleX chat
+
+Once you have started the chat, you will be prompted to specify your "display name" and an optional "full name" to create a local chat profile. Your display name is an alias for your contacts to refer to you by - it is not unique and does not serve as a global identity. In case different contacts chose the same display name, the chat client adds a numeric suffix to their local display names.
+
+This diagram shows how to connect and message a contact:
+
+
+
+
+
+Once you've set up your local profile, enter `/c` (for `/connect`) to create a new connection and generate an invitation. Send this invitation to your contact via any other channel.
+
+You are able to create multiple invitations by entering `/connect` multiple times and sending these invitations to the corresponding contacts you'd like to connect with.
+
+The invitation has the format `smp::::::`. The invitation can only be used once and even if this is intercepted, the attacker would not be able to use it to send you the messages via this queue once your contact confirms that the connection is established.
+
+The contact who received the invitation should enter `/c ` to accept the connection. This establishes the connection, and both parties are notified.
+
+They would then use `@` commands to send messages. You may also just start typing a message to send it to the contact that was the last.
+
+Use `/help` in chat to see the list of available commands.
+
+### Groups
+
+To create a group use `/g `, then add contacts to it with `/a `and send messages with `#`. Use `/help groups` for other commands.
+
+
+
+> **Please note**: the groups are not stored on any server, they are maintained as a list of members in the app database to whom the messages will be sent.
+
+### Sending files
+
+You can send a file to your contact with `/f @` - the recipient will have to accept it before it is sent. Use `/help files` for other commands.
+
+
+
+You can send files to a group with `/f #`.
+
+### Access chat history
+
+> 🚧 **Section currently out of date - will be updated soon** 🏗
+
+SimpleX chat stores all your contacts and conversations in a local database file, making it private and portable by design, fully owned and controlled by you.
+
+You can search your chat history via SQLite database file:
+
+```
+sqlite3 ~/.simplex/smp-chat.db
+```
+
+Now you can query `messages` table, for example:
+
+```sql
+select * from messages
+where conn_alias = cast('alice' as blob)
+ and body like '%cats%'
+order by internal_id desc;
+```
+
+> **Please note:** SQLite foreign key constraints are disabled by default, and must be **[enabled separately for each database connection](https://sqlite.org/foreignkeys.html#fk_enable)**. The latter can be achieved by running `PRAGMA foreign_keys = ON;` command on an open database connection. By running data altering queries without enabling foreign keys prior to that, you may risk putting your database in an inconsistent state.
+
+## Future roadmap
+
+1. Mobile and desktop apps (in progress).
+2. SMP protocol improvements:
+ - SMP queue redundancy and rotation.
+ - Message delivery confirmation.
+ - Support multiple devices.
+3. Privacy-preserving identity server for optional DNS-based contact/group addresses to simplify connection and discovery, but not used to deliver messages:
+ - keep all your contacts and groups even if you lose the domain.
+ - the server doesn't have information about your contacts and groups.
+4. Media server to optimize sending large files to groups.
+5. Channels server for large groups and broadcast channels.
+
+## License
+
+[AGPL v3](./LICENSE)
diff --git a/packages/simplex_app/haskell/apps/simplex-chat/Demo.hs b/packages/simplex_app/haskell/apps/simplex-chat/Demo.hs
new file mode 100644
index 0000000000..2fff0b0a32
--- /dev/null
+++ b/packages/simplex_app/haskell/apps/simplex-chat/Demo.hs
@@ -0,0 +1,119 @@
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Demo where
+
+import Simplex.Chat.Styled
+import System.Console.ANSI.Types
+import System.Terminal
+
+someViewUpdate :: Monad m => m ()
+someViewUpdate = pure ()
+
+chatLayoutDemo :: MonadTerminal m => m ()
+chatLayoutDemo =
+ mapM_
+ putStyledLn
+ [ " search " <> Styled gray "(ctrl-s) " <> lineV <> Styled toContact " @bob " <> "Bob Roberts " <> Styled greenColor "@john" <> "",
+ " " <> lineV <> Styled gray " 14:15 online profile (ctrl-p)",
+ lineH 20 <> crossover <> lineH 59,
+ "* " <> Styled [SetConsoleIntensity BoldIntensity] "all chats " <> " " <> lineV <> "",
+ Styled gray " (ctrl-a) " <> lineV <> "",
+ "*" <> Styled toContact " @alice " <> Styled darkGray "14:37 " <> lineV <> "",
+ Styled gray " Hello there! ... " <> lineV <> "",
+ Styled selected " " <> Styled (toContact <> selected) " @bob " <> Styled (selected <> gray) "12:35 " <> lineV <> "",
+ Styled selected " All good, John... " <> lineV <> "",
+ "*" <> Styled group " #team " <> Styled darkGray "10:55 " <> lineV <> "",
+ Styled gray " What's up ther... " <> lineV <> "",
+ " " <> Styled toContact " @tom " <> Styled darkGray "Wed " <> lineV <> "",
+ Styled gray " Have you seen ... " <> lineV <> "",
+ " " <> lineV,
+ " " <> lineV,
+ " " <> lineV,
+ " " <> lineV,
+ " " <> lineV,
+ " " <> lineV <> Styled greenColor " ✔︎" <> Styled darkGray " 12:30" <> Styled toContact " @bob" <> " hey bob - how is it going?",
+ " " <> lineV <> Styled greenColor " ✔︎" <> Styled darkGray " " <> Styled toContact " " <> " let's meet soon!",
+ " " <> lineV <> " *" <> Styled darkGray " 12:35" <> Styled contact " bob>" <> " All good, John! How are you?",
+ " " <> teeL <> lineH 59,
+ " " <> lineV <> " > " <> Styled toContact "@bob" <> " 😀 This is the message that will be sent to @bob"
+ ]
+ >> putStyled (Styled ctrlKeys " help (ctrl-h) new contact (ctrl-n) choose chat (ctrl-↓↑) new group (ctrl-g) ")
+
+contact :: [SGR]
+contact = [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Yellow]
+
+toContact :: [SGR]
+toContact = [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Cyan]
+
+group :: [SGR]
+group = [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Cyan]
+
+selected :: [SGR]
+selected = [SetColor Background Vivid Black]
+
+ctrlKeys :: [SGR]
+ctrlKeys = [SetColor Background Dull White, SetColor Foreground Dull Black]
+
+gray :: [SGR]
+gray = [SetColor Foreground Dull White]
+
+darkGray :: [SGR]
+darkGray = [SetColor Foreground Vivid Black]
+
+greenColor :: [SGR]
+greenColor = [SetColor Foreground Vivid Green]
+
+lineV :: StyledString
+lineV = Styled selected " " -- "\x2502"
+
+lineH :: Int -> StyledString
+lineH n = Styled darkGray $ replicate n '\x2500'
+
+teeL :: StyledString
+teeL = Styled selected " " -- "\x251C"
+
+crossover :: StyledString
+crossover = Styled selected " " -- "\x253C"
+
+putStyledLn :: MonadTerminal m => StyledString -> m ()
+putStyledLn s = putStyled s >> putLn
+
+putStyled :: MonadTerminal m => StyledString -> m ()
+putStyled (s1 :<>: s2) = putStyled s1 >> putStyled s2
+putStyled (Styled [] s) = putString s
+putStyled (Styled sgr s) = setSGR sgr >> putString s >> resetAttributes
+
+setSGR :: MonadTerminal m => [SGR] -> m ()
+setSGR = mapM_ $ \case
+ Reset -> resetAttributes
+ SetConsoleIntensity BoldIntensity -> setAttribute bold
+ SetConsoleIntensity _ -> resetAttribute bold
+ SetItalicized True -> setAttribute italic
+ SetItalicized _ -> resetAttribute italic
+ SetUnderlining NoUnderline -> resetAttribute underlined
+ SetUnderlining _ -> setAttribute underlined
+ SetSwapForegroundBackground True -> setAttribute inverted
+ SetSwapForegroundBackground _ -> resetAttribute inverted
+ SetColor l i c -> setAttribute . layer l . intensity i $ color c
+ SetBlinkSpeed _ -> pure ()
+ SetVisible _ -> pure ()
+ SetRGBColor _ _ -> pure ()
+ SetPaletteColor _ _ -> pure ()
+ SetDefaultColor _ -> pure ()
+ where
+ layer = \case
+ Foreground -> foreground
+ Background -> background
+ intensity = \case
+ Dull -> id
+ Vivid -> bright
+ color = \case
+ Black -> black
+ Red -> red
+ Green -> green
+ Yellow -> yellow
+ Blue -> blue
+ Magenta -> magenta
+ Cyan -> cyan
+ White -> white
diff --git a/packages/simplex_app/haskell/apps/simplex-chat/Main.hs b/packages/simplex_app/haskell/apps/simplex-chat/Main.hs
new file mode 100644
index 0000000000..e11f943171
--- /dev/null
+++ b/packages/simplex_app/haskell/apps/simplex-chat/Main.hs
@@ -0,0 +1,58 @@
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Main where
+
+import Simplex.Chat
+import Simplex.Chat.Options
+import System.Directory (getAppUserDataDirectory)
+import System.Terminal (withTerminal)
+
+main :: IO ()
+main = do
+ opts <- welcomeGetOpts
+ t <- withTerminal pure
+ simplexChat defaultChatConfig opts t
+
+welcomeGetOpts :: IO ChatOpts
+welcomeGetOpts = do
+ appDir <- getAppUserDataDirectory "simplex"
+ opts@ChatOpts {dbFile} <- getChatOpts appDir
+ putStrLn "SimpleX chat prototype v0.4.0"
+ putStrLn $ "db: " <> dbFile <> ".chat.db, " <> dbFile <> ".agent.db"
+ putStrLn "type \"/help\" or \"/h\" for usage info"
+ pure opts
+
+-- defaultSettings :: C.Size -> C.VirtualTerminalSettings
+-- defaultSettings size =
+-- C.VirtualTerminalSettings
+-- { C.virtualType = "xterm",
+-- C.virtualWindowSize = pure size,
+-- C.virtualEvent = retry,
+-- C.virtualInterrupt = retry
+-- }
+
+-- main :: IO ()
+-- main = do
+-- void $ createStore "simplex-chat.db" 4
+
+-- hFlush stdout
+-- -- ChatTerminal {termSize} <- newChatTerminal
+-- -- pos <- C.withVirtualTerminal (defaultSettings termSize) $
+-- -- \t -> runTerminalT (C.setAlternateScreenBuffer True >> C.putString "a" >> C.flush >> C.getCursorPosition) t
+-- -- print pos
+-- -- race_ (printEvents t) (updateTerminal t)
+-- void . withTerminal . runTerminalT $ chatLayoutDemo >> C.flush >> C.awaitEvent
+
+-- printEvents :: C.VirtualTerminal -> IO ()
+-- printEvents t = forever $ do
+-- event <- withTerminal . runTerminalT $ C.flush >> C.awaitEvent
+-- runTerminalT (putStringLn $ show event) t
+
+-- updateTerminal :: C.VirtualTerminal -> IO ()
+-- updateTerminal t = forever $ do
+-- threadDelay 10000
+-- win <- readTVarIO $ C.virtualWindow t
+-- withTerminal . runTerminalT $ mapM_ C.putStringLn win >> C.flush
diff --git a/packages/simplex_app/haskell/images/connection.gif b/packages/simplex_app/haskell/images/connection.gif
new file mode 100644
index 0000000000..f15f7bcb09
Binary files /dev/null and b/packages/simplex_app/haskell/images/connection.gif differ
diff --git a/packages/simplex_app/haskell/images/files.gif b/packages/simplex_app/haskell/images/files.gif
new file mode 100644
index 0000000000..ad8beaa183
Binary files /dev/null and b/packages/simplex_app/haskell/images/files.gif differ
diff --git a/packages/simplex_app/haskell/images/groups.gif b/packages/simplex_app/haskell/images/groups.gif
new file mode 100644
index 0000000000..0ce6dc30ec
Binary files /dev/null and b/packages/simplex_app/haskell/images/groups.gif differ
diff --git a/packages/simplex_app/haskell/images/how-to-use-simplex.svg b/packages/simplex_app/haskell/images/how-to-use-simplex.svg
new file mode 100644
index 0000000000..1be91eb293
--- /dev/null
+++ b/packages/simplex_app/haskell/images/how-to-use-simplex.svg
@@ -0,0 +1,32 @@
+
diff --git a/packages/simplex_app/haskell/images/logo.svg b/packages/simplex_app/haskell/images/logo.svg
new file mode 100644
index 0000000000..bb4e50787a
--- /dev/null
+++ b/packages/simplex_app/haskell/images/logo.svg
@@ -0,0 +1,12 @@
+
diff --git a/packages/simplex_app/haskell/migrations/20210612_initial.sql b/packages/simplex_app/haskell/migrations/20210612_initial.sql
new file mode 100644
index 0000000000..933ab1e604
--- /dev/null
+++ b/packages/simplex_app/haskell/migrations/20210612_initial.sql
@@ -0,0 +1,278 @@
+CREATE TABLE contact_profiles ( -- remote user profile
+ contact_profile_id INTEGER PRIMARY KEY,
+ display_name TEXT NOT NULL, -- contact name set by remote user (not unique), this name must not contain spaces
+ full_name TEXT NOT NULL,
+ properties TEXT NOT NULL DEFAULT '{}' -- JSON with contact profile properties
+);
+
+CREATE INDEX contact_profiles_index ON contact_profiles (display_name, full_name);
+
+CREATE TABLE users (
+ user_id INTEGER PRIMARY KEY,
+ contact_id INTEGER NOT NULL UNIQUE REFERENCES contacts ON DELETE CASCADE
+ DEFERRABLE INITIALLY DEFERRED,
+ local_display_name TEXT NOT NULL UNIQUE,
+ active_user INTEGER NOT NULL DEFAULT 0, -- 1 for active user
+ FOREIGN KEY (user_id, local_display_name)
+ REFERENCES display_names (user_id, local_display_name)
+ ON DELETE RESTRICT
+ ON UPDATE CASCADE
+ DEFERRABLE INITIALLY DEFERRED
+);
+
+CREATE TABLE display_names (
+ user_id INTEGER NOT NULL REFERENCES users,
+ local_display_name TEXT NOT NULL,
+ ldn_base TEXT NOT NULL,
+ ldn_suffix INTEGER NOT NULL DEFAULT 0,
+ PRIMARY KEY (user_id, local_display_name) ON CONFLICT FAIL,
+ UNIQUE (user_id, ldn_base, ldn_suffix) ON CONFLICT FAIL
+) WITHOUT ROWID;
+
+CREATE TABLE contacts (
+ contact_id INTEGER PRIMARY KEY,
+ contact_profile_id INTEGER REFERENCES contact_profiles, -- NULL if it's an incognito profile
+ user_id INTEGER NOT NULL REFERENCES users,
+ local_display_name TEXT NOT NULL,
+ is_user INTEGER NOT NULL DEFAULT 0, -- 1 if this contact is a user
+ via_group INTEGER REFERENCES groups (group_id) ON DELETE SET NULL,
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
+ FOREIGN KEY (user_id, local_display_name)
+ REFERENCES display_names (user_id, local_display_name)
+ ON DELETE RESTRICT
+ ON UPDATE CASCADE,
+ UNIQUE (user_id, local_display_name),
+ UNIQUE (user_id, contact_profile_id)
+);
+
+CREATE TABLE sent_probes (
+ sent_probe_id INTEGER PRIMARY KEY,
+ contact_id INTEGER NOT NULL UNIQUE REFERENCES contacts ON DELETE CASCADE,
+ probe BLOB NOT NULL,
+ user_id INTEGER NOT NULL REFERENCES users,
+ UNIQUE (user_id, probe)
+);
+
+CREATE TABLE sent_probe_hashes (
+ sent_probe_hash_id INTEGER PRIMARY KEY,
+ sent_probe_id INTEGER NOT NULL REFERENCES sent_probes ON DELETE CASCADE,
+ contact_id INTEGER NOT NULL REFERENCES contacts ON DELETE CASCADE,
+ user_id INTEGER NOT NULL REFERENCES users,
+ UNIQUE (sent_probe_id, contact_id)
+);
+
+CREATE TABLE received_probes (
+ received_probe_id INTEGER PRIMARY KEY,
+ contact_id INTEGER NOT NULL REFERENCES contacts ON DELETE CASCADE,
+ probe BLOB,
+ probe_hash BLOB NOT NULL,
+ user_id INTEGER NOT NULL REFERENCES users
+);
+
+CREATE TABLE known_servers(
+ server_id INTEGER PRIMARY KEY,
+ host TEXT NOT NULL,
+ port TEXT NOT NULL,
+ key_hash BLOB,
+ user_id INTEGER NOT NULL REFERENCES users,
+ UNIQUE (user_id, host, port)
+) WITHOUT ROWID;
+
+CREATE TABLE group_profiles ( -- shared group profiles
+ group_profile_id INTEGER PRIMARY KEY,
+ display_name TEXT NOT NULL, -- this name must not contain spaces
+ full_name TEXT NOT NULL,
+ properties TEXT NOT NULL DEFAULT '{}' -- JSON with user or contact profile
+);
+
+CREATE TABLE groups (
+ group_id INTEGER PRIMARY KEY, -- local group ID
+ user_id INTEGER NOT NULL REFERENCES users,
+ local_display_name TEXT NOT NULL, -- local group name without spaces
+ group_profile_id INTEGER REFERENCES group_profiles, -- shared group profile
+ inv_queue_info BLOB,
+ FOREIGN KEY (user_id, local_display_name)
+ REFERENCES display_names (user_id, local_display_name)
+ ON DELETE RESTRICT
+ ON UPDATE CASCADE,
+ UNIQUE (user_id, local_display_name),
+ UNIQUE (user_id, group_profile_id)
+);
+
+CREATE TABLE group_members ( -- group members, excluding the local user
+ group_member_id INTEGER PRIMARY KEY,
+ group_id INTEGER NOT NULL REFERENCES groups ON DELETE RESTRICT,
+ member_id BLOB NOT NULL, -- shared member ID, unique per group
+ member_role TEXT NOT NULL, -- owner, admin, member
+ member_category TEXT NOT NULL, -- see GroupMemberCategory
+ member_status TEXT NOT NULL, -- see GroupMemberStatus
+ invited_by INTEGER REFERENCES contacts (contact_id) ON DELETE RESTRICT, -- NULL for the members who joined before the current user and for the group creator
+ group_queue_info BLOB,
+ direct_queue_info BLOB,
+ user_id INTEGER NOT NULL REFERENCES users,
+ local_display_name TEXT NOT NULL, -- should be the same as contact
+ contact_profile_id INTEGER NOT NULL REFERENCES contact_profiles ON DELETE RESTRICT,
+ contact_id INTEGER REFERENCES contacts ON DELETE RESTRICT,
+ FOREIGN KEY (user_id, local_display_name)
+ REFERENCES display_names (user_id, local_display_name)
+ ON DELETE RESTRICT
+ ON UPDATE CASCADE,
+ UNIQUE (group_id, member_id)
+);
+
+CREATE TABLE group_member_intros (
+ group_member_intro_id INTEGER PRIMARY KEY,
+ re_group_member_id INTEGER NOT NULL REFERENCES group_members (group_member_id) ON DELETE CASCADE,
+ to_group_member_id INTEGER NOT NULL REFERENCES group_members (group_member_id) ON DELETE CASCADE,
+ group_queue_info BLOB,
+ direct_queue_info BLOB,
+ intro_status TEXT NOT NULL, -- see GroupMemberIntroStatus
+ UNIQUE (re_group_member_id, to_group_member_id)
+);
+
+CREATE TABLE files (
+ file_id INTEGER PRIMARY KEY,
+ contact_id INTEGER REFERENCES contacts ON DELETE RESTRICT,
+ group_id INTEGER REFERENCES groups ON DELETE RESTRICT,
+ file_name TEXT NOT NULL,
+ file_path TEXT,
+ file_size INTEGER NOT NULL,
+ chunk_size INTEGER NOT NULL,
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
+ user_id INTEGER NOT NULL REFERENCES users
+);
+
+CREATE TABLE snd_files (
+ file_id INTEGER NOT NULL REFERENCES files ON DELETE RESTRICT,
+ connection_id INTEGER NOT NULL REFERENCES connections ON DELETE RESTRICT,
+ file_status TEXT NOT NULL, -- new, accepted, connected, completed
+ group_member_id INTEGER REFERENCES group_members ON DELETE RESTRICT,
+ PRIMARY KEY (file_id, connection_id)
+) WITHOUT ROWID;
+
+CREATE TABLE rcv_files (
+ file_id INTEGER PRIMARY KEY REFERENCES files ON DELETE RESTRICT,
+ file_status TEXT NOT NULL, -- new, accepted, connected, completed
+ group_member_id INTEGER REFERENCES group_members ON DELETE RESTRICT,
+ file_queue_info BLOB
+);
+
+CREATE TABLE snd_file_chunks (
+ file_id INTEGER NOT NULL,
+ connection_id INTEGER NOT NULL,
+ chunk_number INTEGER NOT NULL,
+ chunk_agent_msg_id INTEGER,
+ chunk_sent INTEGER NOT NULL DEFAULT 0, -- 0 (sent to agent), 1 (sent to server)
+ FOREIGN KEY (file_id, connection_id) REFERENCES snd_files ON DELETE CASCADE,
+ PRIMARY KEY (file_id, connection_id, chunk_number)
+) WITHOUT ROWID;
+
+CREATE TABLE rcv_file_chunks (
+ file_id INTEGER NOT NULL REFERENCES rcv_files,
+ chunk_number INTEGER NOT NULL,
+ chunk_agent_msg_id INTEGER NOT NULL,
+ chunk_stored INTEGER NOT NULL DEFAULT 0, -- 0 (received), 1 (appended to file)
+ PRIMARY KEY (file_id, chunk_number)
+) WITHOUT ROWID;
+
+CREATE TABLE connections ( -- all SMP agent connections
+ connection_id INTEGER PRIMARY KEY,
+ agent_conn_id BLOB NOT NULL UNIQUE,
+ conn_level INTEGER NOT NULL DEFAULT 0,
+ via_contact INTEGER REFERENCES contacts (contact_id),
+ conn_status TEXT NOT NULL,
+ conn_type TEXT NOT NULL, -- contact, member, rcv_file, snd_file
+ contact_id INTEGER REFERENCES contacts ON DELETE RESTRICT,
+ group_member_id INTEGER REFERENCES group_members ON DELETE RESTRICT,
+ snd_file_id INTEGER,
+ rcv_file_id INTEGER REFERENCES rcv_files (file_id) ON DELETE RESTRICT,
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
+ user_id INTEGER NOT NULL REFERENCES users,
+ FOREIGN KEY (snd_file_id, connection_id)
+ REFERENCES snd_files (file_id, connection_id)
+ ON DELETE RESTRICT
+ DEFERRABLE INITIALLY DEFERRED
+);
+
+CREATE TABLE events ( -- messages received by the agent, append only
+ event_id INTEGER PRIMARY KEY,
+ agent_msg_id INTEGER NOT NULL, -- internal message ID
+ external_msg_id INTEGER NOT NULL, -- external message ID (sent or received)
+ agent_meta TEXT NOT NULL, -- JSON with timestamps etc. sent in MSG
+ connection_id INTEGER NOT NULL REFERENCES connections,
+ received INTEGER NOT NULL, -- 0 for received, 1 for sent
+ chat_event_id INTEGER,
+ continuation_of INTEGER, -- references chat_event_id, but can be incorrect
+ event_type TEXT NOT NULL, -- event type - see protocol/types.ts
+ event_encoding INTEGER NOT NULL, -- format of event_body: 0 - binary, 1 - text utf8, 2 - JSON (utf8)
+ content_type TEXT NOT NULL, -- content type - see protocol/types.ts
+ event_body BLOB, -- agent message body as sent
+ event_hash BLOB NOT NULL,
+ integrity TEXT NOT NULL DEFAULT '',
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
+);
+
+CREATE INDEX events_external_msg_id_index ON events (connection_id, external_msg_id);
+
+CREATE TABLE event_body_parts (
+ event_body_part_id INTEGER PRIMARY KEY,
+ event_id REFERENCES events,
+ full_size INTEGER NOT NULL,
+ part_status TEXT, -- full, partial
+ content_type TEXT NOT NULL,
+ event_part BLOB
+);
+
+CREATE TABLE contact_profile_events (
+ event_id INTEGER NOT NULL UNIQUE REFERENCES events,
+ contact_profile_id INTEGER NOT NULL REFERENCES contact_profiles
+);
+
+CREATE TABLE group_profile_events (
+ event_id INTEGER NOT NULL UNIQUE REFERENCES events,
+ group_profile_id INTEGER NOT NULL REFERENCES group_profiles
+);
+
+CREATE TABLE group_events (
+ event_id INTEGER NOT NULL UNIQUE REFERENCES events,
+ group_id INTEGER NOT NULL REFERENCES groups ON DELETE RESTRICT,
+ group_member_id INTEGER REFERENCES group_members -- NULL for current user
+);
+
+CREATE TABLE group_event_parents (
+ group_event_parent_id INTEGER PRIMARY KEY,
+ event_id INTEGER NOT NULL REFERENCES group_events (event_id),
+ parent_group_member_id INTEGER REFERENCES group_members (group_member_id), -- can be NULL if parent_member_id is incorrect
+ parent_member_id BLOB, -- shared member ID, unique per group
+ parent_event_id INTEGER REFERENCES events (event_id) ON DELETE CASCADE, -- this can be NULL if received event references another event that's not received yet
+ parent_chat_event_id INTEGER NOT NULL,
+ parent_event_hash BLOB NOT NULL
+);
+
+CREATE INDEX group_event_parents_parent_chat_event_id_index
+ ON group_event_parents (parent_member_id, parent_chat_event_id);
+
+CREATE TABLE messages ( -- mutable messages presented to user
+ message_id INTEGER PRIMARY KEY,
+ contact_id INTEGER NOT NULL REFERENCES contacts ON DELETE RESTRICT, -- 1 for sent messages
+ group_id INTEGER REFERENCES groups ON DELETE RESTRICT, -- NULL for direct messages
+ deleted INTEGER NOT NULL, -- 1 for deleted
+ msg_type TEXT NOT NULL,
+ content_type TEXT NOT NULL,
+ msg_text TEXT NOT NULL, -- textual representation
+ msg_props TEXT NOT NULL -- JSON
+);
+
+CREATE TABLE message_content (
+ message_content_id INTEGER PRIMARY KEY,
+ message_id INTEGER REFERENCES messages ON DELETE CASCADE,
+ content_type TEXT NOT NULL,
+ content_size INTEGER, -- full expected content size
+ content_status TEXT, -- empty, part, full
+ content BLOB NOT NULL
+);
+
+CREATE TABLE message_events (
+ event_id INTEGER NOT NULL UNIQUE REFERENCES events,
+ message_id INTEGER NOT NULL REFERENCES messages
+);
diff --git a/packages/simplex_app/haskell/package.yaml b/packages/simplex_app/haskell/package.yaml
new file mode 100644
index 0000000000..a46590627d
--- /dev/null
+++ b/packages/simplex_app/haskell/package.yaml
@@ -0,0 +1,72 @@
+name: simplex-chat
+version: 0.4.2
+#synopsis:
+#description:
+homepage: https://github.com/simplex-chat/simplex-chat#readme
+license: AGPL-3
+author: Evgeny Poberezkin
+maintainer: evgeny@poberezkin.com
+copyright: 2020 Evgeny Poberezkin
+category: Web, System, Services, Cryptography
+extra-source-files:
+ - README.md
+
+dependencies:
+ - aeson == 1.5.*
+ - ansi-terminal == 0.10.*
+ - attoparsec == 0.13.*
+ - base >= 4.7 && < 5
+ - base64-bytestring >= 1.0 && < 1.3
+ - bytestring == 0.10.*
+ - composition == 1.0.*
+ - containers == 0.6.*
+ - cryptonite >= 0.27 && < 0.30
+ - directory == 1.3.*
+ - exceptions == 0.10.*
+ - file-embed == 0.0.14.*
+ - filepath == 1.4.*
+ - mtl == 2.2.*
+ - optparse-applicative == 0.15.*
+ - process == 1.6.*
+ - simple-logger == 0.1.*
+ - simplexmq == 0.4.*
+ - sqlite-simple == 0.4.*
+ - stm == 2.5.*
+ - terminal == 0.2.*
+ - text == 1.2.*
+ - time == 1.9.*
+ - unliftio == 0.2.*
+ - unliftio-core == 0.2.*
+
+library:
+ source-dirs: src
+
+executables:
+ simplex-chat:
+ source-dirs: apps/simplex-chat
+ main: Main.hs
+ dependencies:
+ - simplex-chat
+ ghc-options:
+ - -threaded
+
+tests:
+ simplex-chat-test:
+ source-dirs: tests
+ main: Test.hs
+ dependencies:
+ - simplex-chat
+ - async == 2.2.*
+ - hspec == 2.7.*
+ - network == 3.1.*
+ - stm == 2.5.*
+
+ghc-options:
+ # - -haddock
+ - -Wall
+ - -Wcompat
+ - -Werror=incomplete-patterns
+ - -Wredundant-constraints
+ - -Wincomplete-record-updates
+ - -Wincomplete-uni-patterns
+ - -Wunused-type-patterns
diff --git a/packages/simplex_app/haskell/protocol/types.ts b/packages/simplex_app/haskell/protocol/types.ts
new file mode 100644
index 0000000000..f2084480e1
--- /dev/null
+++ b/packages/simplex_app/haskell/protocol/types.ts
@@ -0,0 +1,74 @@
+// x. namespace is for chat messages transmitted inside SMP agent MSG
+type MemberMessageType =
+ | "x.grp.info" // group profile information or update
+ | "x.grp.off" // disable group
+ | "x.grp.del" // group deleted
+ | "x.grp.mem.new" // new group member
+ | "x.grp.mem.acl" // group member permissions (ACL)
+ | "x.grp.mem.leave" // group member left
+ | "x.grp.mem.off" // suspend group member
+ | "x.grp.mem.on" // enable group member
+ | "x.grp.mem.del" // group member removed
+
+type ProfileMessageType =
+ | "x.info" // profile information or update
+ | "x.info.grp" // information about group in profile
+ | "x.info.con" // information about contact in profile
+
+type NotificationMessageType = "x.msg.read"
+
+type OpenConnMessageType =
+ | "x.open.grp" // open invitation to the group
+ | "x.open.con" // open invitation to the contact
+
+type ContentMessageType =
+ | "x.msg.new" // new message
+ | "x.msg.append" // additional part of the message
+ | "x.msg.del" // delete message
+ | "x.msg.update" // update message
+ | "x.msg.fwd" // forward message
+ | "x.msg.reply" // reply to message
+
+// TODO namespace for chat messages transmitted as other agent messages
+
+type DirectMessageType =
+ | ProfileMessageType
+ | NotificationMessageType
+ | OpenConnMessageType
+ | ContentMessageType
+
+type GroupMessageType = MemberMessageType | DirectMessageType
+
+type ContentType =
+ | "c.text"
+ | "c.html"
+ | "c.image"
+ | "c.audio"
+ | "c.video"
+ | "c.doc"
+ | "c.sticker"
+ | "c.file"
+ | "c.link"
+ | "c.form"
+ | "c.poll"
+ | "c.applet"
+
+// the type of message data transmitted inside SMP agent MSG
+interface MessageData {
+ type: T
+ sent: Date
+ data: unknown
+}
+
+interface DirectMessageData extends MessageData {}
+
+interface GroupMessageData extends MessageData {
+ msgId: number
+ parents: ParentMessage[]
+}
+
+interface ParentMessage {
+ memberId: Uint8Array
+ msgId: number
+ msgHash: Uint8Array
+}
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat.hs b/packages/simplex_app/haskell/src/Simplex/Chat.hs
new file mode 100644
index 0000000000..5427f7af97
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat.hs
@@ -0,0 +1,1132 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE RecordWildCards #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TupleSections #-}
+
+module Simplex.Chat where
+
+import Control.Applicative (optional, (<|>))
+import Control.Concurrent.STM (stateTVar)
+import Control.Logger.Simple
+import Control.Monad.Except
+import Control.Monad.IO.Unlift
+import Control.Monad.Reader
+import Crypto.Random (drgNew)
+import Data.Attoparsec.ByteString.Char8 (Parser)
+import qualified Data.Attoparsec.ByteString.Char8 as A
+import Data.Bifunctor (first)
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as B
+import Data.Functor (($>))
+import Data.Int (Int64)
+import Data.List (find)
+import Data.Map.Strict (Map)
+import qualified Data.Map.Strict as M
+import Data.Maybe (isJust, mapMaybe)
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Text.Encoding (encodeUtf8)
+import Simplex.Chat.Controller
+import Simplex.Chat.Help
+import Simplex.Chat.Input
+import Simplex.Chat.Notification
+import Simplex.Chat.Options (ChatOpts (..))
+import Simplex.Chat.Protocol
+import Simplex.Chat.Store
+import Simplex.Chat.Styled (plain)
+import Simplex.Chat.Terminal
+import Simplex.Chat.Types
+import Simplex.Chat.Util (ifM, unlessM)
+import Simplex.Chat.View
+import Simplex.Messaging.Agent
+import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..), defaultAgentConfig)
+import Simplex.Messaging.Agent.Protocol
+import qualified Simplex.Messaging.Crypto as C
+import Simplex.Messaging.Parsers (parseAll)
+import qualified Simplex.Messaging.Protocol as SMP
+import Simplex.Messaging.Util (bshow, raceAny_, tryError)
+import System.Exit (exitFailure, exitSuccess)
+import System.FilePath (combine, splitExtensions, takeFileName)
+import System.IO (Handle, IOMode (..), SeekMode (..), hFlush, openFile, stdout)
+import Text.Read (readMaybe)
+import UnliftIO.Async (race_)
+import UnliftIO.Concurrent (forkIO, threadDelay)
+import UnliftIO.Directory (doesDirectoryExist, doesFileExist, getFileSize, getHomeDirectory, getTemporaryDirectory)
+import qualified UnliftIO.Exception as E
+import UnliftIO.IO (hClose, hSeek, hTell)
+import UnliftIO.STM
+
+data ChatCommand
+ = ChatHelp
+ | FilesHelp
+ | GroupsHelp
+ | MarkdownHelp
+ | AddContact
+ | Connect SMPQueueInfo
+ | DeleteContact ContactName
+ | SendMessage ContactName ByteString
+ | NewGroup GroupProfile
+ | AddMember GroupName ContactName GroupMemberRole
+ | JoinGroup GroupName
+ | RemoveMember GroupName ContactName
+ | MemberRole GroupName ContactName GroupMemberRole
+ | LeaveGroup GroupName
+ | DeleteGroup GroupName
+ | ListMembers GroupName
+ | SendGroupMessage GroupName ByteString
+ | SendFile ContactName FilePath
+ | SendGroupFile GroupName FilePath
+ | ReceiveFile Int64 (Maybe FilePath)
+ | CancelFile Int64
+ | FileStatus Int64
+ | UpdateProfile Profile
+ | ShowProfile
+ | QuitChat
+ deriving (Show)
+
+defaultChatConfig :: ChatConfig
+defaultChatConfig =
+ ChatConfig
+ { agentConfig =
+ defaultAgentConfig
+ { tcpPort = undefined, -- agent does not listen to TCP
+ smpServers = undefined, -- filled in from options
+ dbFile = undefined, -- filled in from options
+ dbPoolSize = 1
+ },
+ dbPoolSize = 1,
+ tbqSize = 16,
+ fileChunkSize = 7050
+ }
+
+logCfg :: LogConfig
+logCfg = LogConfig {lc_file = Nothing, lc_stderr = True}
+
+simplexChat :: WithTerminal t => ChatConfig -> ChatOpts -> t -> IO ()
+simplexChat cfg opts t =
+ -- setLogLevel LogInfo -- LogError
+ -- withGlobalLogging logCfg $ do
+ initializeNotifications
+ >>= newChatController cfg opts t
+ >>= runSimplexChat
+
+newChatController :: WithTerminal t => ChatConfig -> ChatOpts -> t -> (Notification -> IO ()) -> IO ChatController
+newChatController config@ChatConfig {agentConfig = cfg, dbPoolSize, tbqSize} ChatOpts {dbFile, smpServers} t sendNotification = do
+ chatStore <- createStore (dbFile <> ".chat.db") dbPoolSize
+ currentUser <- newTVarIO =<< getCreateActiveUser chatStore
+ chatTerminal <- newChatTerminal t
+ smpAgent <- getSMPAgentClient cfg {dbFile = dbFile <> ".agent.db", smpServers}
+ idsDrg <- newTVarIO =<< drgNew
+ inputQ <- newTBQueueIO tbqSize
+ notifyQ <- newTBQueueIO tbqSize
+ chatLock <- newTMVarIO ()
+ sndFiles <- newTVarIO M.empty
+ rcvFiles <- newTVarIO M.empty
+ pure ChatController {..}
+
+runSimplexChat :: ChatController -> IO ()
+runSimplexChat = runReaderT (race_ runTerminalInput runChatController)
+
+runChatController :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+runChatController =
+ raceAny_
+ [ inputSubscriber,
+ agentSubscriber,
+ notificationSubscriber
+ ]
+
+withLock :: MonadUnliftIO m => TMVar () -> m () -> m ()
+withLock lock =
+ E.bracket_
+ (void . atomically $ takeTMVar lock)
+ (atomically $ putTMVar lock ())
+
+inputSubscriber :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+inputSubscriber = do
+ q <- asks inputQ
+ l <- asks chatLock
+ a <- asks smpAgent
+ forever $
+ atomically (readTBQueue q) >>= \case
+ InputControl _ -> pure ()
+ InputCommand s ->
+ case parseAll chatCommandP . encodeUtf8 $ T.pack s of
+ Left e -> printToView [plain s, "invalid input: " <> plain e]
+ Right cmd -> do
+ case cmd of
+ SendMessage c msg -> showSentMessage c msg
+ SendGroupMessage g msg -> showSentGroupMessage g msg
+ SendFile c f -> showSentFileInvitation c f
+ SendGroupFile g f -> showSentGroupFileInvitation g f
+ _ -> printToView [plain s]
+ user <- readTVarIO =<< asks currentUser
+ withAgentLock a . withLock l . void . runExceptT $
+ processChatCommand user cmd `catchError` showChatError
+
+processChatCommand :: forall m. ChatMonad m => User -> ChatCommand -> m ()
+processChatCommand user@User {userId, profile} = \case
+ ChatHelp -> printToView chatHelpInfo
+ FilesHelp -> printToView filesHelpInfo
+ GroupsHelp -> printToView groupsHelpInfo
+ MarkdownHelp -> printToView markdownInfo
+ AddContact -> do
+ (connId, qInfo) <- withAgent createConnection
+ withStore $ \st -> createDirectConnection st userId connId
+ showInvitation qInfo
+ Connect qInfo -> do
+ connId <- withAgent $ \a -> joinConnection a qInfo . directMessage $ XInfo profile
+ withStore $ \st -> createDirectConnection st userId connId
+ DeleteContact cName ->
+ withStore (\st -> getContactGroupNames st userId cName) >>= \case
+ [] -> do
+ conns <- withStore $ \st -> getContactConnections st userId cName
+ withAgent $ \a -> forM_ conns $ \Connection {agentConnId} ->
+ deleteConnection a agentConnId `catchError` \(_ :: AgentErrorType) -> pure ()
+ withStore $ \st -> deleteContact st userId cName
+ unsetActive $ ActiveC cName
+ showContactDeleted cName
+ gs -> showContactGroups cName gs
+ SendMessage cName msg -> do
+ contact <- withStore $ \st -> getContact st userId cName
+ let msgEvent = XMsgNew $ MsgContent MTText [] [MsgContentBody {contentType = SimplexContentType XCText, contentData = msg}]
+ sendDirectMessage (contactConnId contact) msgEvent
+ setActive $ ActiveC cName
+ NewGroup gProfile -> do
+ gVar <- asks idsDrg
+ group <- withStore $ \st -> createNewGroup st gVar user gProfile
+ showGroupCreated group
+ AddMember gName cName memRole -> do
+ (group, contact) <- withStore $ \st -> (,) <$> getGroup st user gName <*> getContact st userId cName
+ let Group {groupId, groupProfile, membership, members} = group
+ userRole = memberRole membership
+ userMemberId = memberId membership
+ when (userRole < GRAdmin || userRole < memRole) $ chatError CEGroupUserRole
+ when (memberStatus membership == GSMemInvited) $ chatError (CEGroupNotJoined gName)
+ unless (memberActive membership) $ chatError CEGroupMemberNotActive
+ when (isJust $ contactMember contact members) $ chatError (CEGroupDuplicateMember cName)
+ gVar <- asks idsDrg
+ (agentConnId, qInfo) <- withAgent createConnection
+ GroupMember {memberId} <- withStore $ \st -> createContactGroupMember st gVar user groupId contact memRole agentConnId
+ let msg = XGrpInv $ GroupInvitation (userMemberId, userRole) (memberId, memRole) qInfo groupProfile
+ sendDirectMessage (contactConnId contact) msg
+ showSentGroupInvitation gName cName
+ setActive $ ActiveG gName
+ JoinGroup gName -> do
+ ReceivedGroupInvitation {fromMember, userMember, queueInfo} <- withStore $ \st -> getGroupInvitation st user gName
+ agentConnId <- withAgent $ \a -> joinConnection a queueInfo . directMessage . XGrpAcpt $ memberId userMember
+ withStore $ \st -> do
+ createMemberConnection st userId fromMember agentConnId
+ updateGroupMemberStatus st userId fromMember GSMemAccepted
+ updateGroupMemberStatus st userId userMember GSMemAccepted
+ MemberRole _gName _cName _mRole -> pure ()
+ RemoveMember gName cName -> do
+ Group {membership, members} <- withStore $ \st -> getGroup st user gName
+ case find ((== cName) . (localDisplayName :: GroupMember -> ContactName)) members of
+ Nothing -> chatError $ CEGroupMemberNotFound cName
+ Just member -> do
+ let userRole = memberRole membership
+ when (userRole < GRAdmin || userRole < memberRole member) $ chatError CEGroupUserRole
+ sendGroupMessage members . XGrpMemDel $ memberId member
+ deleteMemberConnection member
+ withStore $ \st -> updateGroupMemberStatus st userId member GSMemRemoved
+ showDeletedMember gName Nothing (Just member)
+ LeaveGroup gName -> do
+ Group {membership, members} <- withStore $ \st -> getGroup st user gName
+ sendGroupMessage members XGrpLeave
+ mapM_ deleteMemberConnection members
+ withStore $ \st -> updateGroupMemberStatus st userId membership GSMemLeft
+ showLeftMemberUser gName
+ DeleteGroup gName -> do
+ g@Group {membership, members} <- withStore $ \st -> getGroup st user gName
+ let s = memberStatus membership
+ canDelete =
+ memberRole membership == GROwner
+ || (s == GSMemRemoved || s == GSMemLeft || s == GSMemGroupDeleted)
+ unless canDelete $ chatError CEGroupUserRole
+ when (memberActive membership) $ sendGroupMessage members XGrpDel
+ mapM_ deleteMemberConnection members
+ withStore $ \st -> deleteGroup st user g
+ showGroupDeletedUser gName
+ ListMembers gName -> do
+ group <- withStore $ \st -> getGroup st user gName
+ showGroupMembers group
+ SendGroupMessage gName msg -> do
+ -- TODO save sent messages
+ -- TODO save pending message delivery for members without connections
+ Group {members, membership} <- withStore $ \st -> getGroup st user gName
+ unless (memberActive membership) $ chatError CEGroupMemberUserRemoved
+ let msgEvent = XMsgNew $ MsgContent MTText [] [MsgContentBody {contentType = SimplexContentType XCText, contentData = msg}]
+ sendGroupMessage members msgEvent
+ setActive $ ActiveG gName
+ SendFile cName f -> do
+ (fileSize, chSize) <- checkSndFile f
+ contact <- withStore $ \st -> getContact st userId cName
+ (agentConnId, fileQInfo) <- withAgent createConnection
+ let fileInv = FileInvitation {fileName = takeFileName f, fileSize, fileQInfo}
+ SndFileTransfer {fileId} <- withStore $ \st ->
+ createSndFileTransfer st userId contact f fileInv agentConnId chSize
+ sendDirectMessage (contactConnId contact) $ XFile fileInv
+ showSentFileInfo fileId
+ setActive $ ActiveC cName
+ SendGroupFile gName f -> do
+ (fileSize, chSize) <- checkSndFile f
+ group@Group {members, membership} <- withStore $ \st -> getGroup st user gName
+ unless (memberActive membership) $ chatError CEGroupMemberUserRemoved
+ let fileName = takeFileName f
+ ms <- forM (filter memberActive members) $ \m -> do
+ (connId, fileQInfo) <- withAgent createConnection
+ pure (m, connId, FileInvitation {fileName, fileSize, fileQInfo})
+ fileId <- withStore $ \st -> createSndGroupFileTransfer st userId group ms f fileSize chSize
+ forM_ ms $ \(m, _, fileInv) ->
+ traverse (`sendDirectMessage` XFile fileInv) $ memberConnId m
+ showSentFileInfo fileId
+ setActive $ ActiveG gName
+ ReceiveFile fileId filePath_ -> do
+ ft@RcvFileTransfer {fileInvitation = FileInvitation {fileName, fileQInfo}, fileStatus} <- withStore $ \st -> getRcvFileTransfer st userId fileId
+ unless (fileStatus == RFSNew) . chatError $ CEFileAlreadyReceiving fileName
+ tryError (withAgent $ \a -> joinConnection a fileQInfo . directMessage $ XFileAcpt fileName) >>= \case
+ Right agentConnId -> do
+ filePath <- getRcvFilePath fileId filePath_ fileName
+ withStore $ \st -> acceptRcvFileTransfer st userId fileId agentConnId filePath
+ showRcvFileAccepted ft filePath
+ Left (ChatErrorAgent (SMP SMP.AUTH)) -> showRcvFileSndCancelled ft
+ Left (ChatErrorAgent (CONN DUPLICATE)) -> showRcvFileSndCancelled ft
+ Left e -> throwError e
+ CancelFile fileId ->
+ withStore (\st -> getFileTransfer st userId fileId) >>= \case
+ FTSnd fts -> do
+ forM_ fts $ \ft -> cancelSndFileTransfer ft
+ showSndGroupFileCancelled fts
+ FTRcv ft -> do
+ cancelRcvFileTransfer ft
+ showRcvFileCancelled ft
+ FileStatus fileId ->
+ withStore (\st -> getFileTransferProgress st userId fileId) >>= showFileTransferStatus
+ UpdateProfile p -> unless (p == profile) $ do
+ user' <- withStore $ \st -> updateUserProfile st user p
+ asks currentUser >>= atomically . (`writeTVar` user')
+ contacts <- withStore (`getUserContacts` user)
+ forM_ contacts $ \ct -> sendDirectMessage (contactConnId ct) $ XInfo p
+ showUserProfileUpdated user user'
+ ShowProfile -> showUserProfile profile
+ QuitChat -> liftIO exitSuccess
+ where
+ contactMember :: Contact -> [GroupMember] -> Maybe GroupMember
+ contactMember Contact {contactId} =
+ find $ \GroupMember {memberContactId = cId, memberStatus = s} ->
+ cId == Just contactId && s /= GSMemRemoved && s /= GSMemLeft
+ checkSndFile :: FilePath -> m (Integer, Integer)
+ checkSndFile f = do
+ unlessM (doesFileExist f) . chatError $ CEFileNotFound f
+ (,) <$> getFileSize f <*> asks (fileChunkSize . config)
+ getRcvFilePath :: Int64 -> Maybe FilePath -> String -> m FilePath
+ getRcvFilePath fileId filePath fileName = case filePath of
+ Nothing -> do
+ dir <- (`combine` "Downloads") <$> getHomeDirectory
+ ifM (doesDirectoryExist dir) (pure dir) getTemporaryDirectory
+ >>= (`uniqueCombine` fileName)
+ >>= createEmptyFile
+ Just fPath ->
+ ifM
+ (doesDirectoryExist fPath)
+ (fPath `uniqueCombine` fileName >>= createEmptyFile)
+ $ ifM
+ (doesFileExist fPath)
+ (chatError $ CEFileAlreadyExists fPath)
+ (createEmptyFile fPath)
+ where
+ createEmptyFile :: FilePath -> m FilePath
+ createEmptyFile fPath = emptyFile fPath `E.catch` (chatError . CEFileWrite fPath)
+ emptyFile :: FilePath -> m FilePath
+ emptyFile fPath = do
+ h <- getFileHandle fileId fPath rcvFiles AppendMode
+ liftIO $ B.hPut h "" >> hFlush h
+ pure fPath
+ uniqueCombine :: FilePath -> String -> m FilePath
+ uniqueCombine filePath fileName = tryCombine (0 :: Int)
+ where
+ tryCombine n =
+ let (name, ext) = splitExtensions fileName
+ suffix = if n == 0 then "" else "_" <> show n
+ f = filePath `combine` (name <> suffix <> ext)
+ in ifM (doesFileExist f) (tryCombine $ n + 1) (pure f)
+
+agentSubscriber :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+agentSubscriber = do
+ q <- asks $ subQ . smpAgent
+ l <- asks chatLock
+ subscribeUserConnections
+ forever $ do
+ (_, connId, msg) <- atomically $ readTBQueue q
+ user <- readTVarIO =<< asks currentUser
+ withLock l . void . runExceptT $
+ processAgentMessage user connId msg `catchError` showChatError
+
+subscribeUserConnections :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+subscribeUserConnections = void . runExceptT $ do
+ user <- readTVarIO =<< asks currentUser
+ subscribeContacts user
+ subscribeGroups user
+ subscribeFiles user
+ subscribePendingConnections user
+ where
+ subscribeContacts user = do
+ contacts <- withStore (`getUserContacts` user)
+ forM_ contacts $ \ct@Contact {localDisplayName = c} ->
+ (subscribe (contactConnId ct) >> showContactSubscribed c) `catchError` showContactSubError c
+ subscribeGroups user = do
+ groups <- withStore (`getUserGroups` user)
+ forM_ groups $ \Group {members, membership, localDisplayName = g} -> do
+ let connectedMembers = mapMaybe (\m -> (m,) <$> memberConnId m) members
+ if null connectedMembers
+ then
+ if memberActive membership
+ then showGroupEmpty g
+ else showGroupRemoved g
+ else do
+ forM_ connectedMembers $ \(GroupMember {localDisplayName = c}, cId) ->
+ subscribe cId `catchError` showMemberSubError g c
+ showGroupSubscribed g
+ subscribeFiles user = do
+ withStore (`getLiveSndFileTransfers` user) >>= mapM_ subscribeSndFile
+ withStore (`getLiveRcvFileTransfers` user) >>= mapM_ subscribeRcvFile
+ where
+ subscribeSndFile ft@SndFileTransfer {fileId, fileStatus, agentConnId} = do
+ subscribe agentConnId `catchError` showSndFileSubError ft
+ void . forkIO $ do
+ threadDelay 1000000
+ l <- asks chatLock
+ a <- asks smpAgent
+ unless (fileStatus == FSNew) . unlessM (isFileActive fileId sndFiles) $
+ withAgentLock a . withLock l $
+ sendFileChunk ft
+ subscribeRcvFile ft@RcvFileTransfer {fileStatus} =
+ case fileStatus of
+ RFSAccepted fInfo -> resume fInfo
+ RFSConnected fInfo -> resume fInfo
+ _ -> pure ()
+ where
+ resume RcvFileInfo {agentConnId} =
+ subscribe agentConnId `catchError` showRcvFileSubError ft
+ subscribePendingConnections user = do
+ connections <- withStore (`getPendingConnections` user)
+ forM_ connections $ \Connection {agentConnId} ->
+ subscribe agentConnId `catchError` \_ -> pure ()
+ subscribe cId = withAgent (`subscribeConnection` cId)
+
+processAgentMessage :: forall m. ChatMonad m => User -> ConnId -> ACommand 'Agent -> m ()
+processAgentMessage user@User {userId, profile} agentConnId agentMessage = do
+ chatDirection <- withStore $ \st -> getConnectionChatDirection st user agentConnId
+ forM_ (agentMsgConnStatus agentMessage) $ \status ->
+ withStore $ \st -> updateConnectionStatus st (fromConnection chatDirection) status
+ case chatDirection of
+ ReceivedDirectMessage conn maybeContact ->
+ processDirectMessage agentMessage conn maybeContact
+ ReceivedGroupMessage conn gName m ->
+ processGroupMessage agentMessage conn gName m
+ RcvFileConnection conn ft ->
+ processRcvFileConn agentMessage conn ft
+ SndFileConnection conn ft ->
+ processSndFileConn agentMessage conn ft
+ where
+ isMember :: MemberId -> Group -> Bool
+ isMember memId Group {membership, members} =
+ memberId membership == memId || isJust (find ((== memId) . memberId) members)
+
+ contactIsReady :: Contact -> Bool
+ contactIsReady Contact {activeConn} = connStatus activeConn == ConnReady
+
+ memberIsReady :: GroupMember -> Bool
+ memberIsReady GroupMember {activeConn} = maybe False ((== ConnReady) . connStatus) activeConn
+
+ agentMsgConnStatus :: ACommand 'Agent -> Maybe ConnStatus
+ agentMsgConnStatus = \case
+ REQ _ _ -> Just ConnRequested
+ INFO _ -> Just ConnSndReady
+ CON -> Just ConnReady
+ _ -> Nothing
+
+ processDirectMessage :: ACommand 'Agent -> Connection -> Maybe Contact -> m ()
+ processDirectMessage agentMsg conn = \case
+ Nothing -> case agentMsg of
+ REQ confId connInfo -> do
+ saveConnInfo conn connInfo
+ acceptAgentConnection conn confId $ XInfo profile
+ INFO connInfo ->
+ saveConnInfo conn connInfo
+ MSG meta _ ->
+ withAckMessage agentConnId meta $ pure ()
+ _ -> pure ()
+ Just ct@Contact {localDisplayName = c} -> case agentMsg of
+ MSG meta msgBody -> withAckMessage agentConnId meta $ do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage msgBody
+ case chatMsgEvent of
+ XMsgNew (MsgContent MTText [] body) -> newTextMessage c meta $ find (isSimplexContentType XCText) body
+ XFile fInv -> processFileInvitation ct meta fInv
+ XInfo p -> xInfo ct p
+ XGrpInv gInv -> processGroupInvitation ct gInv
+ XInfoProbe probe -> xInfoProbe ct probe
+ XInfoProbeCheck probeHash -> xInfoProbeCheck ct probeHash
+ XInfoProbeOk probe -> xInfoProbeOk ct probe
+ _ -> pure ()
+ REQ confId connInfo -> do
+ -- confirming direct connection with a member
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case chatMsgEvent of
+ XGrpMemInfo _memId _memProfile -> do
+ -- TODO check member ID
+ -- TODO update member profile
+ acceptAgentConnection conn confId XOk
+ _ -> messageError "REQ from member must have x.grp.mem.info"
+ INFO connInfo -> do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case chatMsgEvent of
+ XGrpMemInfo _memId _memProfile -> do
+ -- TODO check member ID
+ -- TODO update member profile
+ pure ()
+ XOk -> pure ()
+ _ -> messageError "INFO from member must have x.grp.mem.info or x.ok"
+ CON ->
+ withStore (\st -> getViaGroupMember st user ct) >>= \case
+ Nothing -> do
+ showContactConnected ct
+ setActive $ ActiveC c
+ showToast (c <> "> ") "connected"
+ Just (gName, m) ->
+ when (memberIsReady m) $ do
+ notifyMemberConnected gName m
+ when (memberCategory m == GCPreMember) $ probeMatchingContacts ct
+ END -> do
+ showContactAnotherClient c
+ showToast (c <> "> ") "connected to another client"
+ unsetActive $ ActiveC c
+ DOWN -> do
+ showContactDisconnected c
+ showToast (c <> "> ") "disconnected"
+ UP -> do
+ showContactSubscribed c
+ showToast (c <> "> ") "is active"
+ setActive $ ActiveC c
+ _ -> pure ()
+
+ processGroupMessage :: ACommand 'Agent -> Connection -> GroupName -> GroupMember -> m ()
+ processGroupMessage agentMsg conn gName m = case agentMsg of
+ REQ confId connInfo -> do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case memberCategory m of
+ GCInviteeMember ->
+ case chatMsgEvent of
+ XGrpAcpt memId
+ | memId == memberId m -> do
+ withStore $ \st -> updateGroupMemberStatus st userId m GSMemAccepted
+ acceptAgentConnection conn confId XOk
+ | otherwise -> messageError "x.grp.acpt: memberId is different from expected"
+ _ -> messageError "REQ from invited member must have x.grp.acpt"
+ _ ->
+ case chatMsgEvent of
+ XGrpMemInfo memId _memProfile
+ | memId == memberId m -> do
+ -- TODO update member profile
+ Group {membership} <- withStore $ \st -> getGroup st user gName
+ acceptAgentConnection conn confId $ XGrpMemInfo (memberId membership) profile
+ | otherwise -> messageError "x.grp.mem.info: memberId is different from expected"
+ _ -> messageError "REQ from member must have x.grp.mem.info"
+ INFO connInfo -> do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case chatMsgEvent of
+ XGrpMemInfo memId _memProfile
+ | memId == memberId m -> do
+ -- TODO update member profile
+ pure ()
+ | otherwise -> messageError "x.grp.mem.info: memberId is different from expected"
+ XOk -> pure ()
+ _ -> messageError "INFO from member must have x.grp.mem.info"
+ pure ()
+ CON -> do
+ group@Group {members, membership} <- withStore $ \st -> getGroup st user gName
+ withStore $ \st -> do
+ updateGroupMemberStatus st userId m GSMemConnected
+ unless (memberActive membership) $
+ updateGroupMemberStatus st userId membership GSMemConnected
+ -- TODO forward any pending (GMIntroInvReceived) introductions
+ case memberCategory m of
+ GCHostMember -> do
+ showUserJoinedGroup gName
+ setActive $ ActiveG gName
+ showToast ("#" <> gName) "you are connected to group"
+ GCInviteeMember -> do
+ showJoinedGroupMember gName m
+ setActive $ ActiveG gName
+ showToast ("#" <> gName) $ "member " <> localDisplayName (m :: GroupMember) <> " is connected"
+ intros <- withStore $ \st -> createIntroductions st group m
+ sendGroupMessage members . XGrpMemNew $ memberInfo m
+ forM_ intros $ \intro -> do
+ sendDirectMessage agentConnId . XGrpMemIntro . memberInfo $ reMember intro
+ withStore $ \st -> updateIntroStatus st intro GMIntroSent
+ _ -> do
+ -- TODO send probe and decide whether to use existing contact connection or the new contact connection
+ -- TODO notify member who forwarded introduction - question - where it is stored? There is via_contact but probably there should be via_member in group_members table
+ withStore (\st -> getViaGroupContact st user m) >>= \case
+ Nothing -> do
+ notifyMemberConnected gName m
+ messageError "implementation error: connected member does not have contact"
+ Just ct ->
+ when (contactIsReady ct) $ do
+ notifyMemberConnected gName m
+ when (memberCategory m == GCPreMember) $ probeMatchingContacts ct
+ MSG meta msgBody -> withAckMessage agentConnId meta $ do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage msgBody
+ case chatMsgEvent of
+ XMsgNew (MsgContent MTText [] body) ->
+ newGroupTextMessage gName m meta $ find (isSimplexContentType XCText) body
+ XFile fInv -> processGroupFileInvitation gName m meta fInv
+ XGrpMemNew memInfo -> xGrpMemNew gName m memInfo
+ XGrpMemIntro memInfo -> xGrpMemIntro gName m memInfo
+ XGrpMemInv memId introInv -> xGrpMemInv gName m memId introInv
+ XGrpMemFwd memInfo introInv -> xGrpMemFwd gName m memInfo introInv
+ XGrpMemDel memId -> xGrpMemDel gName m memId
+ XGrpLeave -> xGrpLeave gName m
+ XGrpDel -> xGrpDel gName m
+ _ -> messageError $ "unsupported message: " <> T.pack (show chatMsgEvent)
+ _ -> pure ()
+
+ processSndFileConn :: ACommand 'Agent -> Connection -> SndFileTransfer -> m ()
+ processSndFileConn agentMsg conn ft@SndFileTransfer {fileId, fileName, fileStatus} =
+ case agentMsg of
+ REQ confId connInfo -> do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case chatMsgEvent of
+ XFileAcpt name
+ | name == fileName -> do
+ withStore $ \st -> updateSndFileStatus st ft FSAccepted
+ acceptAgentConnection conn confId XOk
+ | otherwise -> messageError "x.file.acpt: fileName is different from expected"
+ _ -> messageError "REQ from file connection must have x.file.acpt"
+ CON -> do
+ withStore $ \st -> updateSndFileStatus st ft FSConnected
+ showSndFileStart ft
+ sendFileChunk ft
+ SENT msgId -> do
+ withStore $ \st -> updateSndFileChunkSent st ft msgId
+ unless (fileStatus == FSCancelled) $ sendFileChunk ft
+ MERR _ err -> do
+ cancelSndFileTransfer ft
+ case err of
+ SMP SMP.AUTH -> unless (fileStatus == FSCancelled) $ showSndFileRcvCancelled ft
+ _ -> chatError $ CEFileSend fileId err
+ MSG meta _ ->
+ withAckMessage agentConnId meta $ pure ()
+ _ -> pure ()
+
+ processRcvFileConn :: ACommand 'Agent -> Connection -> RcvFileTransfer -> m ()
+ processRcvFileConn agentMsg _conn ft@RcvFileTransfer {fileId, chunkSize} =
+ case agentMsg of
+ CON -> do
+ withStore $ \st -> updateRcvFileStatus st ft FSConnected
+ showRcvFileStart ft
+ MSG meta@MsgMeta {recipient = (msgId, _), integrity} msgBody -> withAckMessage agentConnId meta $ do
+ parseFileChunk msgBody >>= \case
+ (0, _) -> do
+ cancelRcvFileTransfer ft
+ showRcvFileSndCancelled ft
+ (chunkNo, chunk) -> do
+ case integrity of
+ MsgOk -> pure ()
+ MsgError MsgDuplicate -> pure () -- TODO remove once agent removes duplicates
+ MsgError e ->
+ badRcvFileChunk ft $ "invalid file chunk number " <> show chunkNo <> ": " <> show e
+ withStore (\st -> createRcvFileChunk st ft chunkNo msgId) >>= \case
+ RcvChunkOk ->
+ if B.length chunk /= fromInteger chunkSize
+ then badRcvFileChunk ft "incorrect chunk size"
+ else appendFileChunk ft chunkNo chunk
+ RcvChunkFinal ->
+ if B.length chunk > fromInteger chunkSize
+ then badRcvFileChunk ft "incorrect chunk size"
+ else do
+ appendFileChunk ft chunkNo chunk
+ withStore $ \st -> do
+ updateRcvFileStatus st ft FSComplete
+ deleteRcvFileChunks st ft
+ showRcvFileComplete ft
+ closeFileHandle fileId rcvFiles
+ withAgent (`deleteConnection` agentConnId)
+ RcvChunkDuplicate -> pure ()
+ RcvChunkError -> badRcvFileChunk ft $ "incorrect chunk number " <> show chunkNo
+ _ -> pure ()
+
+ withAckMessage :: ConnId -> MsgMeta -> m () -> m ()
+ withAckMessage cId MsgMeta {recipient = (msgId, _)} action =
+ action `E.finally` withAgent (\a -> ackMessage a cId msgId `catchError` \_ -> pure ())
+
+ badRcvFileChunk :: RcvFileTransfer -> String -> m ()
+ badRcvFileChunk ft@RcvFileTransfer {fileStatus} err =
+ case fileStatus of
+ RFSCancelled _ -> pure ()
+ _ -> do
+ cancelRcvFileTransfer ft
+ chatError $ CEFileRcvChunk err
+
+ notifyMemberConnected :: GroupName -> GroupMember -> m ()
+ notifyMemberConnected gName m@GroupMember {localDisplayName} = do
+ showConnectedToGroupMember gName m
+ setActive $ ActiveG gName
+ showToast ("#" <> gName) $ "member " <> localDisplayName <> " is connected"
+
+ probeMatchingContacts :: Contact -> m ()
+ probeMatchingContacts ct = do
+ gVar <- asks idsDrg
+ (probe, probeId) <- withStore $ \st -> createSentProbe st gVar userId ct
+ sendDirectMessage (contactConnId ct) $ XInfoProbe probe
+ cs <- withStore (\st -> getMatchingContacts st userId ct)
+ let probeHash = C.sha256Hash probe
+ forM_ cs $ \c -> sendProbeHash c probeHash probeId `catchError` const (pure ())
+ where
+ sendProbeHash c probeHash probeId = do
+ sendDirectMessage (contactConnId c) $ XInfoProbeCheck probeHash
+ withStore $ \st -> createSentProbeHash st userId probeId c
+
+ messageWarning :: Text -> m ()
+ messageWarning = showMessageError "warning"
+
+ messageError :: Text -> m ()
+ messageError = showMessageError "error"
+
+ newTextMessage :: ContactName -> MsgMeta -> Maybe MsgContentBody -> m ()
+ newTextMessage c meta = \case
+ Just MsgContentBody {contentData = bs} -> do
+ let text = safeDecodeUtf8 bs
+ showReceivedMessage c (snd $ broker meta) (msgPlain text) (integrity meta)
+ showToast (c <> "> ") text
+ setActive $ ActiveC c
+ _ -> messageError "x.msg.new: no expected message body"
+
+ newGroupTextMessage :: GroupName -> GroupMember -> MsgMeta -> Maybe MsgContentBody -> m ()
+ newGroupTextMessage gName GroupMember {localDisplayName = c} meta = \case
+ Just MsgContentBody {contentData = bs} -> do
+ let text = safeDecodeUtf8 bs
+ showReceivedGroupMessage gName c (snd $ broker meta) (msgPlain text) (integrity meta)
+ showToast ("#" <> gName <> " " <> c <> "> ") text
+ setActive $ ActiveG gName
+ _ -> messageError "x.msg.new: no expected message body"
+
+ processFileInvitation :: Contact -> MsgMeta -> FileInvitation -> m ()
+ processFileInvitation contact@Contact {localDisplayName = c} meta fInv = do
+ -- TODO chunk size has to be sent as part of invitation
+ chSize <- asks $ fileChunkSize . config
+ ft <- withStore $ \st -> createRcvFileTransfer st userId contact fInv chSize
+ showReceivedMessage c (snd $ broker meta) (receivedFileInvitation ft) (integrity meta)
+ setActive $ ActiveC c
+
+ processGroupFileInvitation :: GroupName -> GroupMember -> MsgMeta -> FileInvitation -> m ()
+ processGroupFileInvitation gName m@GroupMember {localDisplayName = c} meta fInv = do
+ chSize <- asks $ fileChunkSize . config
+ ft <- withStore $ \st -> createRcvGroupFileTransfer st userId m fInv chSize
+ showReceivedGroupMessage gName c (snd $ broker meta) (receivedFileInvitation ft) (integrity meta)
+ setActive $ ActiveG gName
+
+ processGroupInvitation :: Contact -> GroupInvitation -> m ()
+ processGroupInvitation ct@Contact {localDisplayName} inv@(GroupInvitation (fromMemId, fromRole) (memId, memRole) _ _) = do
+ when (fromRole < GRAdmin || fromRole < memRole) $ chatError (CEGroupContactRole localDisplayName)
+ when (fromMemId == memId) $ chatError CEGroupDuplicateMemberId
+ group <- withStore $ \st -> createGroupInvitation st user ct inv
+ showReceivedGroupInvitation group localDisplayName memRole
+
+ xInfo :: Contact -> Profile -> m ()
+ xInfo c@Contact {profile = p} p' = unless (p == p') $ do
+ c' <- withStore $ \st -> updateContactProfile st userId c p'
+ showContactUpdated c c'
+
+ xInfoProbe :: Contact -> ByteString -> m ()
+ xInfoProbe c2 probe = do
+ r <- withStore $ \st -> matchReceivedProbe st userId c2 probe
+ forM_ r $ \c1 -> probeMatch c1 c2 probe
+
+ xInfoProbeCheck :: Contact -> ByteString -> m ()
+ xInfoProbeCheck c1 probeHash = do
+ r <- withStore $ \st -> matchReceivedProbeHash st userId c1 probeHash
+ forM_ r . uncurry $ probeMatch c1
+
+ probeMatch :: Contact -> Contact -> ByteString -> m ()
+ probeMatch c1@Contact {profile = p1} c2@Contact {profile = p2} probe =
+ when (p1 == p2) $ do
+ sendDirectMessage (contactConnId c1) $ XInfoProbeOk probe
+ mergeContacts c1 c2
+
+ xInfoProbeOk :: Contact -> ByteString -> m ()
+ xInfoProbeOk c1 probe = do
+ r <- withStore $ \st -> matchSentProbe st userId c1 probe
+ forM_ r $ \c2 -> mergeContacts c1 c2
+
+ mergeContacts :: Contact -> Contact -> m ()
+ mergeContacts to from = do
+ withStore $ \st -> mergeContactRecords st userId to from
+ showContactsMerged to from
+
+ parseChatMessage :: ByteString -> Either ChatError ChatMessage
+ parseChatMessage msgBody = first ChatErrorMessage (parseAll rawChatMessageP msgBody >>= toChatMessage)
+
+ saveConnInfo :: Connection -> ConnInfo -> m ()
+ saveConnInfo activeConn connInfo = do
+ ChatMessage {chatMsgEvent} <- liftEither $ parseChatMessage connInfo
+ case chatMsgEvent of
+ XInfo p ->
+ withStore $ \st -> createDirectContact st userId activeConn p
+ -- TODO show/log error, other events in SMP confirmation
+ _ -> pure ()
+
+ xGrpMemNew :: GroupName -> GroupMember -> MemberInfo -> m ()
+ xGrpMemNew gName m memInfo@(MemberInfo memId _ _) = do
+ group@Group {membership} <- withStore $ \st -> getGroup st user gName
+ when (memberId membership /= memId) $
+ if isMember memId group
+ then messageError "x.grp.mem.new error: member already exists"
+ else do
+ newMember <- withStore $ \st -> createNewGroupMember st user group memInfo GCPostMember GSMemAnnounced
+ showJoinedGroupMemberConnecting gName m newMember
+
+ xGrpMemIntro :: GroupName -> GroupMember -> MemberInfo -> m ()
+ xGrpMemIntro gName m memInfo@(MemberInfo memId _ _) =
+ case memberCategory m of
+ GCHostMember -> do
+ group <- withStore $ \st -> getGroup st user gName
+ if isMember memId group
+ then messageWarning "x.grp.mem.intro ignored: member already exists"
+ else do
+ (groupConnId, groupQInfo) <- withAgent createConnection
+ (directConnId, directQInfo) <- withAgent createConnection
+ newMember <- withStore $ \st -> createIntroReMember st user group m memInfo groupConnId directConnId
+ let msg = XGrpMemInv memId IntroInvitation {groupQInfo, directQInfo}
+ sendDirectMessage agentConnId msg
+ withStore $ \st -> updateGroupMemberStatus st userId newMember GSMemIntroInvited
+ _ -> messageError "x.grp.mem.intro can be only sent by host member"
+
+ xGrpMemInv :: GroupName -> GroupMember -> MemberId -> IntroInvitation -> m ()
+ xGrpMemInv gName m memId introInv =
+ case memberCategory m of
+ GCInviteeMember -> do
+ group <- withStore $ \st -> getGroup st user gName
+ case find ((== memId) . memberId) $ members group of
+ Nothing -> messageError "x.grp.mem.inv error: referenced member does not exists"
+ Just reMember -> do
+ intro <- withStore $ \st -> saveIntroInvitation st reMember m introInv
+ case activeConn (reMember :: GroupMember) of
+ Nothing -> pure () -- this is not an error, introduction will be forwarded once the member is connected
+ Just Connection {agentConnId = reAgentConnId} -> do
+ sendDirectMessage reAgentConnId $ XGrpMemFwd (memberInfo m) introInv
+ withStore $ \st -> updateIntroStatus st intro GMIntroInvForwarded
+ _ -> messageError "x.grp.mem.inv can be only sent by invitee member"
+
+ xGrpMemFwd :: GroupName -> GroupMember -> MemberInfo -> IntroInvitation -> m ()
+ xGrpMemFwd gName m memInfo@(MemberInfo memId _ _) introInv@IntroInvitation {groupQInfo, directQInfo} = do
+ group@Group {membership} <- withStore $ \st -> getGroup st user gName
+ toMember <- case find ((== memId) . memberId) $ members group of
+ -- TODO if the missed messages are correctly sent as soon as there is connection before anything else is sent
+ -- the situation when member does not exist is an error
+ -- member receiving x.grp.mem.fwd should have also received x.grp.mem.new prior to that.
+ -- For now, this branch compensates for the lack of delayed message delivery.
+ Nothing -> withStore $ \st -> createNewGroupMember st user group memInfo GCPostMember GSMemAnnounced
+ Just m' -> pure m'
+ withStore $ \st -> saveMemberInvitation st toMember introInv
+ let msg = XGrpMemInfo (memberId membership) profile
+ groupConnId <- withAgent $ \a -> joinConnection a groupQInfo $ directMessage msg
+ directConnId <- withAgent $ \a -> joinConnection a directQInfo $ directMessage msg
+ withStore $ \st -> createIntroToMemberContact st userId m toMember groupConnId directConnId
+
+ xGrpMemDel :: GroupName -> GroupMember -> MemberId -> m ()
+ xGrpMemDel gName m memId = do
+ Group {membership, members} <- withStore $ \st -> getGroup st user gName
+ if memberId membership == memId
+ then do
+ mapM_ deleteMemberConnection members
+ withStore $ \st -> updateGroupMemberStatus st userId membership GSMemRemoved
+ showDeletedMemberUser gName m
+ else case find ((== memId) . memberId) members of
+ Nothing -> messageError "x.grp.mem.del with unknown member ID"
+ Just member -> do
+ let mRole = memberRole m
+ if mRole < GRAdmin || mRole < memberRole member
+ then messageError "x.grp.mem.del with insufficient member permissions"
+ else do
+ deleteMemberConnection member
+ withStore $ \st -> updateGroupMemberStatus st userId member GSMemRemoved
+ showDeletedMember gName (Just m) (Just member)
+
+ xGrpLeave :: GroupName -> GroupMember -> m ()
+ xGrpLeave gName m = do
+ deleteMemberConnection m
+ withStore $ \st -> updateGroupMemberStatus st userId m GSMemLeft
+ showLeftMember gName m
+
+ xGrpDel :: GroupName -> GroupMember -> m ()
+ xGrpDel gName m = do
+ when (memberRole m /= GROwner) $ chatError CEGroupUserRole
+ ms <- withStore $ \st -> do
+ Group {members, membership} <- getGroup st user gName
+ updateGroupMemberStatus st userId membership GSMemGroupDeleted
+ pure members
+ mapM_ deleteMemberConnection ms
+ showGroupDeleted gName m
+
+sendFileChunk :: ChatMonad m => SndFileTransfer -> m ()
+sendFileChunk ft@SndFileTransfer {fileId, fileStatus, agentConnId} =
+ unless (fileStatus == FSComplete || fileStatus == FSCancelled) $
+ withStore (`createSndFileChunk` ft) >>= \case
+ Just chunkNo -> sendFileChunkNo ft chunkNo
+ Nothing -> do
+ withStore $ \st -> do
+ updateSndFileStatus st ft FSComplete
+ deleteSndFileChunks st ft
+ showSndFileComplete ft
+ closeFileHandle fileId sndFiles
+ withAgent (`deleteConnection` agentConnId)
+
+sendFileChunkNo :: ChatMonad m => SndFileTransfer -> Integer -> m ()
+sendFileChunkNo ft@SndFileTransfer {agentConnId} chunkNo = do
+ bytes <- readFileChunk ft chunkNo
+ msgId <- withAgent $ \a -> sendMessage a agentConnId $ serializeFileChunk chunkNo bytes
+ withStore $ \st -> updateSndFileChunkMsg st ft chunkNo msgId
+
+readFileChunk :: ChatMonad m => SndFileTransfer -> Integer -> m ByteString
+readFileChunk SndFileTransfer {fileId, filePath, chunkSize} chunkNo =
+ read_ `E.catch` (chatError . CEFileRead filePath)
+ where
+ read_ = do
+ h <- getFileHandle fileId filePath sndFiles ReadMode
+ pos <- hTell h
+ let pos' = (chunkNo - 1) * chunkSize
+ when (pos /= pos') $ hSeek h AbsoluteSeek pos'
+ liftIO . B.hGet h $ fromInteger chunkSize
+
+parseFileChunk :: ChatMonad m => ByteString -> m (Integer, ByteString)
+parseFileChunk msg =
+ liftEither . first (ChatError . CEFileRcvChunk) $
+ parseAll ((,) <$> A.decimal <* A.space <*> A.takeByteString) msg
+
+serializeFileChunk :: Integer -> ByteString -> ByteString
+serializeFileChunk chunkNo bytes = bshow chunkNo <> " " <> bytes
+
+appendFileChunk :: ChatMonad m => RcvFileTransfer -> Integer -> ByteString -> m ()
+appendFileChunk ft@RcvFileTransfer {fileId, fileStatus} chunkNo chunk =
+ case fileStatus of
+ RFSConnected RcvFileInfo {filePath} -> append_ filePath
+ RFSCancelled _ -> pure ()
+ _ -> chatError $ CEFileInternal "receiving file transfer not in progress"
+ where
+ append_ fPath = do
+ h <- getFileHandle fileId fPath rcvFiles AppendMode
+ E.try (liftIO $ B.hPut h chunk >> hFlush h) >>= \case
+ Left e -> chatError $ CEFileWrite fPath e
+ Right () -> withStore $ \st -> updatedRcvFileChunkStored st ft chunkNo
+
+getFileHandle :: ChatMonad m => Int64 -> FilePath -> (ChatController -> TVar (Map Int64 Handle)) -> IOMode -> m Handle
+getFileHandle fileId filePath files ioMode = do
+ fs <- asks files
+ h_ <- M.lookup fileId <$> readTVarIO fs
+ maybe (newHandle fs) pure h_
+ where
+ newHandle fs = do
+ -- TODO handle errors
+ h <- liftIO (openFile filePath ioMode)
+ atomically . modifyTVar fs $ M.insert fileId h
+ pure h
+
+isFileActive :: ChatMonad m => Int64 -> (ChatController -> TVar (Map Int64 Handle)) -> m Bool
+isFileActive fileId files = do
+ fs <- asks files
+ isJust . M.lookup fileId <$> readTVarIO fs
+
+cancelRcvFileTransfer :: ChatMonad m => RcvFileTransfer -> m ()
+cancelRcvFileTransfer ft@RcvFileTransfer {fileId, fileStatus} = do
+ closeFileHandle fileId rcvFiles
+ withStore $ \st -> do
+ updateRcvFileStatus st ft FSCancelled
+ deleteRcvFileChunks st ft
+ case fileStatus of
+ RFSAccepted RcvFileInfo {agentConnId} -> withAgent (`suspendConnection` agentConnId)
+ RFSConnected RcvFileInfo {agentConnId} -> withAgent (`suspendConnection` agentConnId)
+ _ -> pure ()
+
+cancelSndFileTransfer :: ChatMonad m => SndFileTransfer -> m ()
+cancelSndFileTransfer ft@SndFileTransfer {agentConnId, fileStatus} =
+ unless (fileStatus == FSCancelled || fileStatus == FSComplete) $ do
+ withStore $ \st -> do
+ updateSndFileStatus st ft FSCancelled
+ deleteSndFileChunks st ft
+ withAgent $ \a -> do
+ void (sendMessage a agentConnId "0 ") `catchError` \_ -> pure ()
+ suspendConnection a agentConnId
+
+closeFileHandle :: ChatMonad m => Int64 -> (ChatController -> TVar (Map Int64 Handle)) -> m ()
+closeFileHandle fileId files = do
+ fs <- asks files
+ h_ <- atomically . stateTVar fs $ \m -> (M.lookup fileId m, M.delete fileId m)
+ mapM_ hClose h_ `E.catch` \(_ :: E.SomeException) -> pure ()
+
+chatError :: ChatMonad m => ChatErrorType -> m a
+chatError = throwError . ChatError
+
+deleteMemberConnection :: ChatMonad m => GroupMember -> m ()
+deleteMemberConnection m@GroupMember {activeConn} = do
+ -- User {userId} <- asks currentUser
+ withAgent $ forM_ (memberConnId m) . suspendConnection
+ -- withStore $ \st -> deleteGroupMemberConnection st userId m
+ forM_ activeConn $ \conn -> withStore $ \st -> updateConnectionStatus st conn ConnDeleted
+
+sendDirectMessage :: ChatMonad m => ConnId -> ChatMsgEvent -> m ()
+sendDirectMessage agentConnId chatMsgEvent =
+ void . withAgent $ \a -> sendMessage a agentConnId $ directMessage chatMsgEvent
+
+directMessage :: ChatMsgEvent -> ByteString
+directMessage chatMsgEvent =
+ serializeRawChatMessage $
+ rawChatMessage ChatMessage {chatMsgId = Nothing, chatMsgEvent, chatDAG = Nothing}
+
+sendGroupMessage :: ChatMonad m => [GroupMember] -> ChatMsgEvent -> m ()
+sendGroupMessage members chatMsgEvent = do
+ let msg = directMessage chatMsgEvent
+ -- TODO once scheduled delivery is implemented memberActive should be changed to memberCurrent
+ withAgent $ \a ->
+ forM_ (filter memberActive members) $
+ traverse (\connId -> sendMessage a connId msg) . memberConnId
+
+acceptAgentConnection :: ChatMonad m => Connection -> ConfirmationId -> ChatMsgEvent -> m ()
+acceptAgentConnection conn@Connection {agentConnId} confId msg = do
+ withAgent $ \a -> acceptConnection a agentConnId confId $ directMessage msg
+ withStore $ \st -> updateConnectionStatus st conn ConnAccepted
+
+getCreateActiveUser :: SQLiteStore -> IO User
+getCreateActiveUser st = do
+ user <-
+ getUsers st >>= \case
+ [] -> newUser
+ users -> maybe (selectUser users) pure (find activeUser users)
+ putStrLn $ "Current user: " <> userStr user
+ pure user
+ where
+ newUser :: IO User
+ newUser = do
+ putStrLn
+ "No user profiles found, it will be created now.\n\
+ \Please choose your display name and your full name.\n\
+ \They will be sent to your contacts when you connect.\n\
+ \They are only stored on your device and you can change them later."
+ loop
+ where
+ loop = do
+ displayName <- getContactName
+ fullName <- T.pack <$> getWithPrompt "full name (optional)"
+ liftIO (runExceptT $ createUser st Profile {displayName, fullName} True) >>= \case
+ Left SEDuplicateName -> do
+ putStrLn "chosen display name is already used by another profile on this device, choose another one"
+ loop
+ Left e -> putStrLn ("database error " <> show e) >> exitFailure
+ Right user -> pure user
+ selectUser :: [User] -> IO User
+ selectUser [user] = do
+ liftIO $ setActiveUser st (userId user)
+ pure user
+ selectUser users = do
+ putStrLn "Select user profile:"
+ forM_ (zip [1 ..] users) $ \(n :: Int, user) -> putStrLn $ show n <> " - " <> userStr user
+ loop
+ where
+ loop = do
+ nStr <- getWithPrompt $ "user profile number (1 .. " <> show (length users) <> ")"
+ case readMaybe nStr :: Maybe Int of
+ Nothing -> putStrLn "invalid user number" >> loop
+ Just n
+ | n <= 0 || n > length users -> putStrLn "invalid user number" >> loop
+ | otherwise -> do
+ let user = users !! (n - 1)
+ liftIO $ setActiveUser st (userId user)
+ pure user
+ userStr :: User -> String
+ userStr User {localDisplayName, profile = Profile {fullName}} =
+ T.unpack $ localDisplayName <> if T.null fullName || localDisplayName == fullName then "" else " (" <> fullName <> ")"
+ getContactName :: IO ContactName
+ getContactName = do
+ displayName <- getWithPrompt "display name (no spaces)"
+ if null displayName || isJust (find (== ' ') displayName)
+ then putStrLn "display name has space(s), choose another one" >> getContactName
+ else pure $ T.pack displayName
+ getWithPrompt :: String -> IO String
+ getWithPrompt s = putStr (s <> ": ") >> hFlush stdout >> getLine
+
+showToast :: (MonadUnliftIO m, MonadReader ChatController m) => Text -> Text -> m ()
+showToast title text = atomically . (`writeTBQueue` Notification {title, text}) =<< asks notifyQ
+
+notificationSubscriber :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+notificationSubscriber = do
+ ChatController {notifyQ, sendNotification} <- ask
+ forever $ atomically (readTBQueue notifyQ) >>= liftIO . sendNotification
+
+withAgent :: ChatMonad m => (AgentClient -> ExceptT AgentErrorType m a) -> m a
+withAgent action =
+ asks smpAgent
+ >>= runExceptT . action
+ >>= liftEither . first ChatErrorAgent
+
+withStore ::
+ ChatMonad m =>
+ (forall m'. (MonadUnliftIO m', MonadError StoreError m') => SQLiteStore -> m' a) ->
+ m a
+withStore action =
+ asks chatStore
+ >>= runExceptT . action
+ >>= liftEither . first ChatErrorStore
+
+chatCommandP :: Parser ChatCommand
+chatCommandP =
+ ("/help files" <|> "/help file" <|> "/hf") $> FilesHelp
+ <|> ("/help groups" <|> "/help group" <|> "/hg") $> GroupsHelp
+ <|> ("/help" <|> "/h") $> ChatHelp
+ <|> ("/group #" <|> "/group " <|> "/g #" <|> "/g ") *> (NewGroup <$> groupProfile)
+ <|> ("/add #" <|> "/add " <|> "/a #" <|> "/a ") *> (AddMember <$> displayName <* A.space <*> displayName <*> memberRole)
+ <|> ("/join #" <|> "/join " <|> "/j #" <|> "/j ") *> (JoinGroup <$> displayName)
+ <|> ("/remove #" <|> "/remove " <|> "/rm #" <|> "/rm ") *> (RemoveMember <$> displayName <* A.space <*> displayName)
+ <|> ("/leave #" <|> "/leave " <|> "/l #" <|> "/l ") *> (LeaveGroup <$> displayName)
+ <|> ("/delete #" <|> "/d #") *> (DeleteGroup <$> displayName)
+ <|> ("/members #" <|> "/members " <|> "/ms #" <|> "/ms ") *> (ListMembers <$> displayName)
+ <|> A.char '#' *> (SendGroupMessage <$> displayName <* A.space <*> A.takeByteString)
+ <|> ("/connect " <|> "/c ") *> (Connect <$> smpQueueInfoP)
+ <|> ("/connect" <|> "/c") $> AddContact
+ <|> ("/delete @" <|> "/delete " <|> "/d @" <|> "/d ") *> (DeleteContact <$> displayName)
+ <|> A.char '@' *> (SendMessage <$> displayName <*> (A.space *> A.takeByteString))
+ <|> ("/file #" <|> "/f #") *> (SendGroupFile <$> displayName <* A.space <*> filePath)
+ <|> ("/file @" <|> "/file " <|> "/f @" <|> "/f ") *> (SendFile <$> displayName <* A.space <*> filePath)
+ <|> ("/freceive " <|> "/fr ") *> (ReceiveFile <$> A.decimal <*> optional (A.space *> filePath))
+ <|> ("/fcancel " <|> "/fc ") *> (CancelFile <$> A.decimal)
+ <|> ("/fstatus " <|> "/fs ") *> (FileStatus <$> A.decimal)
+ <|> ("/markdown" <|> "/m") $> MarkdownHelp
+ <|> ("/profile " <|> "/p ") *> (UpdateProfile <$> userProfile)
+ <|> ("/profile" <|> "/p") $> ShowProfile
+ <|> ("/quit" <|> "/q") $> QuitChat
+ where
+ displayName = safeDecodeUtf8 <$> (B.cons <$> A.satisfy refChar <*> A.takeTill (== ' '))
+ refChar c = c > ' ' && c /= '#' && c /= '@'
+ userProfile = do
+ cName <- displayName
+ fullName <- fullNameP cName
+ pure Profile {displayName = cName, fullName}
+ groupProfile = do
+ gName <- displayName
+ fullName <- fullNameP gName
+ pure GroupProfile {displayName = gName, fullName}
+ fullNameP name = do
+ n <- (A.space *> A.takeByteString) <|> pure ""
+ pure $ if B.null n then name else safeDecodeUtf8 n
+ filePath = T.unpack . safeDecodeUtf8 <$> A.takeByteString
+ memberRole =
+ (" owner" $> GROwner)
+ <|> (" admin" $> GRAdmin)
+ <|> (" member" $> GRMember)
+ <|> pure GRAdmin
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Controller.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Controller.hs
new file mode 100644
index 0000000000..ae6dfd5e79
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Controller.hs
@@ -0,0 +1,87 @@
+{-# LANGUAGE ConstraintKinds #-}
+{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.Controller where
+
+import Control.Exception
+import Control.Monad.Except
+import Control.Monad.IO.Unlift
+import Control.Monad.Reader
+import Crypto.Random (ChaChaDRG)
+import Data.Int (Int64)
+import Data.Map.Strict (Map)
+import Numeric.Natural
+import Simplex.Chat.Notification
+import Simplex.Chat.Store (StoreError)
+import Simplex.Chat.Terminal
+import Simplex.Chat.Types
+import Simplex.Messaging.Agent (AgentClient)
+import Simplex.Messaging.Agent.Env.SQLite (AgentConfig)
+import Simplex.Messaging.Agent.Protocol (AgentErrorType)
+import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore)
+import System.IO (Handle)
+import UnliftIO.STM
+
+data ChatConfig = ChatConfig
+ { agentConfig :: AgentConfig,
+ dbPoolSize :: Int,
+ tbqSize :: Natural,
+ fileChunkSize :: Integer
+ }
+
+data ChatController = ChatController
+ { currentUser :: TVar User,
+ smpAgent :: AgentClient,
+ chatTerminal :: ChatTerminal,
+ chatStore :: SQLiteStore,
+ idsDrg :: TVar ChaChaDRG,
+ inputQ :: TBQueue InputEvent,
+ notifyQ :: TBQueue Notification,
+ sendNotification :: Notification -> IO (),
+ chatLock :: TMVar (),
+ sndFiles :: TVar (Map Int64 Handle),
+ rcvFiles :: TVar (Map Int64 Handle),
+ config :: ChatConfig
+ }
+
+data InputEvent = InputCommand String | InputControl Char
+
+data ChatError
+ = ChatError ChatErrorType
+ | ChatErrorMessage String
+ | ChatErrorAgent AgentErrorType
+ | ChatErrorStore StoreError
+ deriving (Show, Exception)
+
+data ChatErrorType
+ = CEGroupUserRole
+ | CEGroupContactRole ContactName
+ | CEGroupDuplicateMember ContactName
+ | CEGroupDuplicateMemberId
+ | CEGroupNotJoined GroupName
+ | CEGroupMemberNotActive
+ | CEGroupMemberUserRemoved
+ | CEGroupMemberNotFound ContactName
+ | CEGroupInternal String
+ | CEFileNotFound String
+ | CEFileAlreadyReceiving String
+ | CEFileAlreadyExists FilePath
+ | CEFileRead FilePath SomeException
+ | CEFileWrite FilePath SomeException
+ | CEFileSend Int64 AgentErrorType
+ | CEFileRcvChunk String
+ | CEFileInternal String
+ deriving (Show, Exception)
+
+type ChatMonad m = (MonadUnliftIO m, MonadReader ChatController m, MonadError ChatError m)
+
+setActive :: (MonadUnliftIO m, MonadReader ChatController m) => ActiveTo -> m ()
+setActive to = asks (activeTo . chatTerminal) >>= atomically . (`writeTVar` to)
+
+unsetActive :: (MonadUnliftIO m, MonadReader ChatController m) => ActiveTo -> m ()
+unsetActive a = asks (activeTo . chatTerminal) >>= atomically . (`modifyTVar` unset)
+ where
+ unset a' = if a == a' then ActiveNone else a'
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Help.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Help.hs
new file mode 100644
index 0000000000..56f69a723e
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Help.hs
@@ -0,0 +1,109 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.Help
+ ( chatHelpInfo,
+ filesHelpInfo,
+ groupsHelpInfo,
+ markdownInfo,
+ )
+where
+
+import Data.List (intersperse)
+import Data.Text (Text)
+import Simplex.Chat.Markdown
+import Simplex.Chat.Styled
+import System.Console.ANSI.Types
+
+highlight :: Text -> Markdown
+highlight = Markdown (Colored Cyan)
+
+green :: Text -> Markdown
+green = Markdown (Colored Green)
+
+indent :: Markdown
+indent = " "
+
+listHighlight :: [Text] -> Markdown
+listHighlight = mconcat . intersperse ", " . map highlight
+
+chatHelpInfo :: [StyledString]
+chatHelpInfo =
+ map
+ styleMarkdown
+ [ highlight "Using SimpleX chat prototype",
+ "Follow these steps to set up a connection:",
+ "",
+ green "Step 1: " <> highlight "/connect" <> " - Alice adds a contact.",
+ indent <> "Alice should send the invitation printed by the /add command",
+ indent <> "to her contact, Bob, out-of-band, via any trusted channel.",
+ "",
+ green "Step 2: " <> highlight "/connect " <> " - Bob accepts the invitation.",
+ indent <> "Bob should use the invitation he received out-of-band.",
+ "",
+ green "Step 3: " <> "Bob and Alice are notified that the connection is set up,",
+ indent <> "both can now send messages:",
+ indent <> highlight "@bob Hello, Bob!" <> " - Alice messages Bob (assuming Bob has display name 'bob').",
+ indent <> highlight "@alice Hey, Alice!" <> " - Bob replies to Alice.",
+ "",
+ green "To send file:",
+ indent <> highlight "/file bob ./photo.jpg" <> " - Alice sends file to Bob",
+ indent <> "File commands: " <> highlight "/help files",
+ "",
+ green "To create group:",
+ indent <> highlight "/group team" <> " - create group #team",
+ indent <> "Group commands: " <> highlight "/help groups",
+ "",
+ green "Other commands:",
+ indent <> highlight "/profile " <> " - show user profile",
+ indent <> highlight "/profile []" <> " - update user profile",
+ indent <> highlight "/delete " <> " - delete contact and all messages with them",
+ indent <> highlight "/markdown " <> " - show supported markdown syntax",
+ indent <> highlight "/quit " <> " - quit chat",
+ "",
+ "The commands may be abbreviated to a single letter: " <> listHighlight ["/c", "/f", "/g", "/p", "/h"] <> ", etc."
+ ]
+
+filesHelpInfo :: [StyledString]
+filesHelpInfo =
+ map
+ styleMarkdown
+ [ green "File transfer commands:",
+ indent <> highlight "/file @ " <> " - send file to contact",
+ indent <> highlight "/file # " <> " - send file to group",
+ indent <> highlight "/freceive []" <> " - accept to receive file",
+ indent <> highlight "/fcancel " <> " - cancel sending / receiving file",
+ indent <> highlight "/fstatus " <> " - show file transfer status",
+ "",
+ "The commands may be abbreviated: " <> listHighlight ["/f", "/fr", "/fc", "/fs"]
+ ]
+
+groupsHelpInfo :: [StyledString]
+groupsHelpInfo =
+ map
+ styleMarkdown
+ [ green "Group management commands:",
+ indent <> highlight "/group [] " <> " - create group",
+ indent <> highlight "/add []" <> " - add contact to group, roles: " <> highlight "owner" <> ", " <> highlight "admin" <> " (default), " <> highlight "member",
+ indent <> highlight "/join " <> " - accept group invitation",
+ indent <> highlight "/remove " <> " - remove member from group",
+ indent <> highlight "/leave " <> " - leave group",
+ indent <> highlight "/delete " <> " - delete group",
+ indent <> highlight "/members " <> " - list group members",
+ indent <> highlight "# " <> " - send message to group",
+ "",
+ "The commands may be abbreviated: " <> listHighlight ["/g", "/a", "/j", "/rm", "/l", "/d", "/ms"]
+ ]
+
+markdownInfo :: [StyledString]
+markdownInfo =
+ map
+ styleMarkdown
+ [ green "Markdown:",
+ indent <> highlight "*bold* " <> " - " <> Markdown Bold "bold text",
+ indent <> highlight "_italic_ " <> " - " <> Markdown Italic "italic text" <> " (shown as underlined)",
+ indent <> highlight "+underlined+ " <> " - " <> Markdown Underline "underlined text",
+ indent <> highlight "~strikethrough~" <> " - " <> Markdown StrikeThrough "strikethrough text" <> " (shown as inverse)",
+ indent <> highlight "`code snippet` " <> " - " <> Markdown Snippet "a + b // no *markdown* here",
+ indent <> highlight "!1 text! " <> " - " <> Markdown (Colored Red) "red text" <> " (1-6: red, green, blue, yellow, cyan, magenta)",
+ indent <> highlight "#secret# " <> " - " <> Markdown Secret "secret text" <> " (can be copy-pasted)"
+ ]
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Input.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Input.hs
new file mode 100644
index 0000000000..5369c3db9a
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Input.hs
@@ -0,0 +1,118 @@
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+
+module Simplex.Chat.Input where
+
+import Control.Monad.IO.Unlift
+import Control.Monad.Reader
+import Data.List (dropWhileEnd)
+import qualified Data.Text as T
+import Simplex.Chat.Controller
+import Simplex.Chat.Terminal
+import System.Exit (exitSuccess)
+import System.Terminal hiding (insertChars)
+import UnliftIO.STM
+
+getKey :: MonadTerminal m => m (Key, Modifiers)
+getKey =
+ flush >> awaitEvent >>= \case
+ Left Interrupt -> liftIO exitSuccess
+ Right (KeyEvent key ms) -> pure (key, ms)
+ _ -> getKey
+
+runTerminalInput :: (MonadUnliftIO m, MonadReader ChatController m) => m ()
+runTerminalInput = do
+ ChatController {inputQ, chatTerminal = ct} <- ask
+ liftIO $
+ withChatTerm ct $ do
+ updateInput ct
+ receiveFromTTY inputQ ct
+
+receiveFromTTY :: MonadTerminal m => TBQueue InputEvent -> ChatTerminal -> m ()
+receiveFromTTY inputQ ct@ChatTerminal {activeTo, termSize, termState} =
+ forever $ getKey >>= processKey >> withTermLock ct (updateInput ct)
+ where
+ processKey :: MonadTerminal m => (Key, Modifiers) -> m ()
+ processKey = \case
+ (EnterKey, _) -> submitInput
+ key -> atomically $ do
+ ac <- readTVar activeTo
+ modifyTVar termState $ updateTermState ac (width termSize) key
+
+ submitInput :: MonadTerminal m => m ()
+ submitInput = atomically $ do
+ ts <- readTVar termState
+ let s = inputString ts
+ writeTVar termState $ ts {inputString = "", inputPosition = 0, previousInput = s}
+ writeTBQueue inputQ $ InputCommand s
+
+updateTermState :: ActiveTo -> Int -> (Key, Modifiers) -> TerminalState -> TerminalState
+updateTermState ac tw (key, ms) ts@TerminalState {inputString = s, inputPosition = p} = case key of
+ CharKey c
+ | ms == mempty || ms == shiftKey -> insertCharsWithContact [c]
+ | ms == altKey && c == 'b' -> setPosition prevWordPos
+ | ms == altKey && c == 'f' -> setPosition nextWordPos
+ | otherwise -> ts
+ TabKey -> insertCharsWithContact " "
+ BackspaceKey -> backDeleteChar
+ DeleteKey -> deleteChar
+ HomeKey -> setPosition 0
+ EndKey -> setPosition $ length s
+ ArrowKey d -> case d of
+ Leftwards -> setPosition leftPos
+ Rightwards -> setPosition rightPos
+ Upwards
+ | ms == mempty && null s -> let s' = previousInput ts in ts' (s', length s')
+ | ms == mempty -> let p' = p - tw in if p' > 0 then setPosition p' else ts
+ | otherwise -> ts
+ Downwards
+ | ms == mempty -> let p' = p + tw in if p' <= length s then setPosition p' else ts
+ | otherwise -> ts
+ _ -> ts
+ where
+ insertCharsWithContact cs
+ | null s && cs /= "@" && cs /= "#" && cs /= "/" =
+ insertChars $ contactPrefix <> cs
+ | otherwise = insertChars cs
+ insertChars = ts' . if p >= length s then append else insert
+ append cs = let s' = s <> cs in (s', length s')
+ insert cs = let (b, a) = splitAt p s in (b <> cs <> a, p + length cs)
+ contactPrefix = case ac of
+ ActiveNone -> ""
+ ActiveC c -> "@" <> T.unpack c <> " "
+ ActiveG g -> "#" <> T.unpack g <> " "
+ backDeleteChar
+ | p == 0 || null s = ts
+ | p >= length s = ts' (init s, length s - 1)
+ | otherwise = let (b, a) = splitAt p s in ts' (init b <> a, p - 1)
+ deleteChar
+ | p >= length s || null s = ts
+ | p == 0 = ts' (tail s, 0)
+ | otherwise = let (b, a) = splitAt p s in ts' (b <> tail a, p)
+ leftPos
+ | ms == mempty = max 0 (p - 1)
+ | ms == shiftKey = 0
+ | ms == ctrlKey = prevWordPos
+ | ms == altKey = prevWordPos
+ | otherwise = p
+ rightPos
+ | ms == mempty = min (length s) (p + 1)
+ | ms == shiftKey = length s
+ | ms == ctrlKey = nextWordPos
+ | ms == altKey = nextWordPos
+ | otherwise = p
+ setPosition p' = ts' (s, p')
+ prevWordPos
+ | p == 0 || null s = p
+ | otherwise =
+ let before = take p s
+ beforeWord = dropWhileEnd (/= ' ') $ dropWhileEnd (== ' ') before
+ in max 0 $ p - length before + length beforeWord
+ nextWordPos
+ | p >= length s || null s = p
+ | otherwise =
+ let after = drop p s
+ afterWord = dropWhile (/= ' ') $ dropWhile (== ' ') after
+ in min (length s) $ p + length after - length afterWord
+ ts' (s', p') = ts {inputString = s', inputPosition = p'}
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Markdown.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Markdown.hs
new file mode 100644
index 0000000000..82aa84c631
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Markdown.hs
@@ -0,0 +1,138 @@
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.Markdown where
+
+import Control.Applicative ((<|>))
+import Data.Attoparsec.Text (Parser)
+import qualified Data.Attoparsec.Text as A
+import Data.Either (fromRight)
+import Data.Functor (($>))
+import Data.Map.Strict (Map)
+import qualified Data.Map.Strict as M
+import Data.String
+import Data.Text (Text)
+import qualified Data.Text as T
+import System.Console.ANSI.Types
+
+data Markdown = Markdown Format Text | Markdown :|: Markdown
+ deriving (Eq, Show)
+
+data Format
+ = Bold
+ | Italic
+ | Underline
+ | StrikeThrough
+ | Snippet
+ | Secret
+ | Colored Color
+ | NoFormat
+ deriving (Eq, Show)
+
+instance Semigroup Markdown where (<>) = (:|:)
+
+instance Monoid Markdown where mempty = unmarked ""
+
+instance IsString Markdown where fromString = unmarked . T.pack
+
+unmarked :: Text -> Markdown
+unmarked = Markdown NoFormat
+
+colorMD :: Char
+colorMD = '!'
+
+secretMD :: Char
+secretMD = '#'
+
+formats :: Map Char Format
+formats =
+ M.fromList
+ [ ('*', Bold),
+ ('_', Italic),
+ ('+', Underline),
+ ('~', StrikeThrough),
+ ('`', Snippet),
+ (secretMD, Secret),
+ (colorMD, Colored White)
+ ]
+
+colors :: Map Text Color
+colors =
+ M.fromList
+ [ ("red", Red),
+ ("green", Green),
+ ("blue", Blue),
+ ("yellow", Yellow),
+ ("cyan", Cyan),
+ ("magenta", Magenta),
+ ("r", Red),
+ ("g", Green),
+ ("b", Blue),
+ ("y", Yellow),
+ ("c", Cyan),
+ ("m", Magenta),
+ ("1", Red),
+ ("2", Green),
+ ("3", Blue),
+ ("4", Yellow),
+ ("5", Cyan),
+ ("6", Magenta)
+ ]
+
+parseMarkdown :: Text -> Markdown
+parseMarkdown s = fromRight (unmarked s) $ A.parseOnly (markdownP <* A.endOfInput) s
+
+markdownP :: Parser Markdown
+markdownP = merge <$> A.many' fragmentP
+ where
+ merge :: [Markdown] -> Markdown
+ merge [] = ""
+ merge fs = foldr1 (:|:) fs
+ fragmentP :: Parser Markdown
+ fragmentP =
+ A.anyChar >>= \case
+ ' ' -> unmarked . T.cons ' ' <$> A.takeWhile (== ' ')
+ c -> case M.lookup c formats of
+ Just Secret -> secretP
+ Just (Colored White) -> coloredP
+ Just f -> formattedP c "" f
+ Nothing -> unformattedP c
+ formattedP :: Char -> Text -> Format -> Parser Markdown
+ formattedP c p f = do
+ s <- A.takeTill (== c)
+ (A.char c $> markdown c p f s) <|> noFormat (c `T.cons` p <> s)
+ markdown :: Char -> Text -> Format -> Text -> Markdown
+ markdown c p f s
+ | T.null s || T.head s == ' ' || T.last s == ' ' =
+ unmarked $ c `T.cons` p <> s `T.snoc` c
+ | otherwise = Markdown f s
+ secretP :: Parser Markdown
+ secretP = secret <$> A.takeWhile (== secretMD) <*> A.takeTill (== secretMD) <*> A.takeWhile (== secretMD)
+ secret :: Text -> Text -> Text -> Markdown
+ secret b s a
+ | T.null a || T.null s || T.head s == ' ' || T.last s == ' ' =
+ unmarked $ secretMD `T.cons` ss
+ | otherwise = Markdown Secret $ T.init ss
+ where
+ ss = b <> s <> a
+ coloredP :: Parser Markdown
+ coloredP = do
+ color <- A.takeWhile (\c -> c /= ' ' && c /= colorMD)
+ case M.lookup color colors of
+ Just c ->
+ let f = Colored c
+ in (A.char ' ' *> formattedP colorMD (color `T.snoc` ' ') f)
+ <|> noFormat (colorMD `T.cons` color)
+ _ -> noFormat (colorMD `T.cons` color)
+ unformattedP :: Char -> Parser Markdown
+ unformattedP c = unmarked . T.cons c <$> wordsP
+ wordsP :: Parser Text
+ wordsP = do
+ s <- (<>) <$> A.takeTill (== ' ') <*> A.takeWhile (== ' ')
+ A.peekChar >>= \case
+ Nothing -> pure s
+ Just c -> case M.lookup c formats of
+ Just _ -> pure s
+ Nothing -> (s <>) <$> wordsP
+ noFormat :: Text -> Parser Markdown
+ noFormat = pure . unmarked
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Notification.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Notification.hs
new file mode 100644
index 0000000000..6339d62d6c
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Notification.hs
@@ -0,0 +1,98 @@
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+
+module Simplex.Chat.Notification (Notification (..), initializeNotifications) where
+
+import Control.Exception
+import Control.Monad (void)
+import Data.List (isInfixOf)
+import Data.Map (Map, fromList)
+import qualified Data.Map as M
+import Data.Maybe (fromMaybe)
+import Data.Text (Text)
+import qualified Data.Text as T
+import System.Directory (createDirectoryIfMissing, doesFileExist, getAppUserDataDirectory)
+import System.FilePath (combine)
+import System.Info (os)
+import System.Process (readCreateProcess, shell)
+
+data Notification = Notification {title :: Text, text :: Text}
+
+initializeNotifications :: IO (Notification -> IO ())
+initializeNotifications =
+ hideException <$> case os of
+ "darwin" -> pure $ notify macScript
+ "mingw32" -> initWinNotify
+ "linux" ->
+ doesFileExist "/proc/sys/kernel/osrelease" >>= \case
+ False -> pure $ notify linuxScript
+ True -> do
+ v <- readFile "/proc/sys/kernel/osrelease"
+ if "Microsoft" `isInfixOf` v || "WSL" `isInfixOf` v
+ then initWslNotify
+ else pure $ notify linuxScript
+ _ -> pure . const $ pure ()
+
+hideException :: (a -> IO ()) -> (a -> IO ())
+hideException f a = f a `catch` \(_ :: SomeException) -> pure ()
+
+notify :: (Notification -> Text) -> Notification -> IO ()
+notify script notification =
+ void $ readCreateProcess (shell . T.unpack $ script notification) ""
+
+linuxScript :: Notification -> Text
+linuxScript Notification {title, text} = "notify-send '" <> linuxEscape title <> "' '" <> linuxEscape text <> "'"
+
+linuxEscape :: Text -> Text
+linuxEscape = replaceAll $ fromList [('\'', "'\\''")]
+
+macScript :: Notification -> Text
+macScript Notification {title, text} = "osascript -e 'display notification \"" <> macEscape text <> "\" with title \"" <> macEscape title <> "\"'"
+
+macEscape :: Text -> Text
+macEscape = replaceAll $ fromList [('"', "\\\""), ('\'', "")]
+
+initWslNotify :: IO (Notification -> IO ())
+initWslNotify = notify . wslScript <$> savePowershellScript
+
+wslScript :: FilePath -> Notification -> Text
+wslScript path Notification {title, text} = "powershell.exe \"" <> T.pack path <> " \\\"" <> wslEscape title <> "\\\" \\\"" <> wslEscape text <> "\\\"\""
+
+wslEscape :: Text -> Text
+wslEscape = replaceAll $ fromList [('`', "\\`\\`"), ('\\', "\\\\"), ('"', "\\`\\\"")]
+
+initWinNotify :: IO (Notification -> IO ())
+initWinNotify = notify . winScript <$> savePowershellScript
+
+winScript :: FilePath -> Notification -> Text
+winScript path Notification {title, text} = "powershell.exe \"" <> T.pack path <> " '" <> winRemoveQuotes title <> "' '" <> winRemoveQuotes text <> "'\""
+
+winRemoveQuotes :: Text -> Text
+winRemoveQuotes = replaceAll $ fromList [('`', ""), ('\'', ""), ('"', "")]
+
+replaceAll :: Map Char Text -> Text -> Text
+replaceAll rules = T.concatMap $ \c -> T.singleton c `fromMaybe` M.lookup c rules
+
+savePowershellScript :: IO FilePath
+savePowershellScript = do
+ appDir <- getAppUserDataDirectory "simplex"
+ createDirectoryIfMissing False appDir
+ let psScript = combine appDir "win-toast-notify.ps1"
+ writeFile
+ psScript
+ "[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null\n\
+ \$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)\n\
+ \$RawXml = [xml] $Template.GetXml()\n\
+ \($RawXml.toast.visual.binding.text|where {$_.id -eq \"1\"}).AppendChild($RawXml.CreateTextNode($args[0])) > $null\n\
+ \($RawXml.toast.visual.binding.text|where {$_.id -eq \"2\"}).AppendChild($RawXml.CreateTextNode($args[1])) > $null\n\
+ \$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument\n\
+ \$SerializedXml.LoadXml($RawXml.OuterXml)\n\
+ \$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)\n\
+ \$Toast.Tag = \"simplex-chat\"\n\
+ \$Toast.Group = \"simplex-chat\"\n\
+ \$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)\n\
+ \$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier(\"PowerShell\")\n\
+ \$Notifier.Show($Toast);\n"
+ return psScript
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Options.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Options.hs
new file mode 100644
index 0000000000..11eb57132c
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Options.hs
@@ -0,0 +1,62 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.Options (getChatOpts, ChatOpts (..)) where
+
+import qualified Data.Attoparsec.ByteString.Char8 as A
+import qualified Data.ByteString.Char8 as B
+import Data.List.NonEmpty (NonEmpty)
+import qualified Data.List.NonEmpty as L
+import Options.Applicative
+import Simplex.Messaging.Agent.Protocol (SMPServer (..), smpServerP)
+import Simplex.Messaging.Parsers (parseAll)
+import System.FilePath (combine)
+
+data ChatOpts = ChatOpts
+ { dbFile :: String,
+ smpServers :: NonEmpty SMPServer
+ }
+
+chatOpts :: FilePath -> Parser ChatOpts
+chatOpts appDir =
+ ChatOpts
+ <$> strOption
+ ( long "database"
+ <> short 'd'
+ <> metavar "DB_FILE"
+ <> help ("sqlite database file path (" <> defaultDbFilePath <> ")")
+ <> value defaultDbFilePath
+ )
+ <*> option
+ parseSMPServer
+ ( long "server"
+ <> short 's'
+ <> metavar "SERVER"
+ <> help
+ ( "SMP server(s) to use"
+ <> "\n(smp2.simplex.im,smp3.simplex.im)"
+ )
+ <> value
+ ( L.fromList
+ [ "smp2.simplex.im#z5W2QLQ1Br3Yd6CoWg7bIq1bHdwK7Y8bEiEXBs/WfAg=", -- London, UK
+ "smp3.simplex.im#nxc7HnrnM8dOKgkMp008ub/9o9LXJlxlMrMpR+mfMQw=" -- Fremont, CA
+ ]
+ )
+ )
+ where
+ defaultDbFilePath = combine appDir "simplex"
+
+parseSMPServer :: ReadM (NonEmpty SMPServer)
+parseSMPServer = eitherReader $ parseAll servers . B.pack
+ where
+ servers = L.fromList <$> smpServerP `A.sepBy1` A.char ','
+
+getChatOpts :: FilePath -> IO ChatOpts
+getChatOpts appDir = execParser opts
+ where
+ opts =
+ info
+ (chatOpts appDir <**> helper)
+ ( fullDesc
+ <> header "Chat prototype using Simplex Messaging Protocol (SMP)"
+ <> progDesc "Start chat with DB_FILE file and use SERVER as SMP server"
+ )
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Protocol.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Protocol.hs
new file mode 100644
index 0000000000..2ccb8289c5
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Protocol.hs
@@ -0,0 +1,383 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE KindSignatures #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE StandaloneDeriving #-}
+{-# LANGUAGE TupleSections #-}
+
+module Simplex.Chat.Protocol where
+
+import Control.Applicative (optional)
+import Control.Monad ((<=<))
+import Data.Aeson (FromJSON, ToJSON)
+import qualified Data.Aeson as J
+import Data.Attoparsec.ByteString.Char8 (Parser)
+import qualified Data.Attoparsec.ByteString.Char8 as A
+import qualified Data.ByteString.Base64 as B64
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as B
+import qualified Data.ByteString.Lazy.Char8 as LB
+import Data.Int (Int64)
+import Data.List (find)
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Text.Encoding (encodeUtf8)
+import Simplex.Chat.Types
+import Simplex.Chat.Util (safeDecodeUtf8)
+import Simplex.Messaging.Agent.Protocol
+import Simplex.Messaging.Parsers (parseAll)
+import Simplex.Messaging.Util (bshow)
+
+data ChatDirection (p :: AParty) where
+ ReceivedDirectMessage :: Connection -> Maybe Contact -> ChatDirection 'Agent
+ SentDirectMessage :: Contact -> ChatDirection 'Client
+ ReceivedGroupMessage :: Connection -> GroupName -> GroupMember -> ChatDirection 'Agent
+ SentGroupMessage :: GroupName -> ChatDirection 'Client
+ SndFileConnection :: Connection -> SndFileTransfer -> ChatDirection 'Agent
+ RcvFileConnection :: Connection -> RcvFileTransfer -> ChatDirection 'Agent
+
+deriving instance Eq (ChatDirection p)
+
+deriving instance Show (ChatDirection p)
+
+fromConnection :: ChatDirection 'Agent -> Connection
+fromConnection = \case
+ ReceivedDirectMessage conn _ -> conn
+ ReceivedGroupMessage conn _ _ -> conn
+ SndFileConnection conn _ -> conn
+ RcvFileConnection conn _ -> conn
+
+data ChatMsgEvent
+ = XMsgNew MsgContent
+ | XFile FileInvitation
+ | XFileAcpt String
+ | XInfo Profile
+ | XGrpInv GroupInvitation
+ | XGrpAcpt MemberId
+ | XGrpMemNew MemberInfo
+ | XGrpMemIntro MemberInfo
+ | XGrpMemInv MemberId IntroInvitation
+ | XGrpMemFwd MemberInfo IntroInvitation
+ | XGrpMemInfo MemberId Profile
+ | XGrpMemCon MemberId
+ | XGrpMemConAll MemberId
+ | XGrpMemDel MemberId
+ | XGrpLeave
+ | XGrpDel
+ | XInfoProbe ByteString
+ | XInfoProbeCheck ByteString
+ | XInfoProbeOk ByteString
+ | XOk
+ deriving (Eq, Show)
+
+data MessageType = MTText | MTImage deriving (Eq, Show)
+
+data MsgContent = MsgContent
+ { messageType :: MessageType,
+ files :: [(ContentType, Int)],
+ content :: [MsgContentBody]
+ }
+ deriving (Eq, Show)
+
+toMsgType :: ByteString -> Either String MessageType
+toMsgType = \case
+ "c.text" -> Right MTText
+ "c.image" -> Right MTImage
+ t -> Left $ "invalid message type " <> B.unpack t
+
+rawMsgType :: MessageType -> ByteString
+rawMsgType = \case
+ MTText -> "c.text"
+ MTImage -> "c.image"
+
+data ChatMessage = ChatMessage
+ { chatMsgId :: Maybe Int64,
+ chatMsgEvent :: ChatMsgEvent,
+ chatDAG :: Maybe ByteString
+ }
+ deriving (Eq, Show)
+
+toChatMessage :: RawChatMessage -> Either String ChatMessage
+toChatMessage RawChatMessage {chatMsgId, chatMsgEvent, chatMsgParams, chatMsgBody} = do
+ (chatDAG, body) <- getDAG <$> mapM toMsgBodyContent chatMsgBody
+ let chatMsg msg = pure ChatMessage {chatMsgId, chatMsgEvent = msg, chatDAG}
+ case (chatMsgEvent, chatMsgParams) of
+ ("x.msg.new", mt : rawFiles) -> do
+ t <- toMsgType mt
+ files <- mapM (toContentInfo <=< parseAll contentInfoP) rawFiles
+ chatMsg . XMsgNew $ MsgContent {messageType = t, files, content = body}
+ ("x.file", [name, size, qInfo]) -> do
+ let fileName = T.unpack $ safeDecodeUtf8 name
+ fileSize <- parseAll A.decimal size
+ fileQInfo <- parseAll smpQueueInfoP qInfo
+ chatMsg . XFile $ FileInvitation {fileName, fileSize, fileQInfo}
+ ("x.file.acpt", [name]) ->
+ chatMsg . XFileAcpt . T.unpack $ safeDecodeUtf8 name
+ ("x.info", []) -> do
+ profile <- getJSON body
+ chatMsg $ XInfo profile
+ ("x.grp.inv", [fromMemId, fromRole, memId, role, qInfo]) -> do
+ fromMem <- (,) <$> B64.decode fromMemId <*> toMemberRole fromRole
+ invitedMem <- (,) <$> B64.decode memId <*> toMemberRole role
+ groupQInfo <- parseAll smpQueueInfoP qInfo
+ profile <- getJSON body
+ chatMsg . XGrpInv $ GroupInvitation fromMem invitedMem groupQInfo profile
+ ("x.grp.acpt", [memId]) ->
+ chatMsg . XGrpAcpt =<< B64.decode memId
+ ("x.grp.mem.new", [memId, role]) -> do
+ chatMsg . XGrpMemNew =<< toMemberInfo memId role body
+ ("x.grp.mem.intro", [memId, role]) ->
+ chatMsg . XGrpMemIntro =<< toMemberInfo memId role body
+ ("x.grp.mem.inv", [memId, groupQInfo, directQInfo]) ->
+ chatMsg =<< (XGrpMemInv <$> B64.decode memId <*> toIntroInv groupQInfo directQInfo)
+ ("x.grp.mem.fwd", [memId, role, groupQInfo, directQInfo]) -> do
+ chatMsg =<< (XGrpMemFwd <$> toMemberInfo memId role body <*> toIntroInv groupQInfo directQInfo)
+ ("x.grp.mem.info", [memId]) ->
+ chatMsg =<< (XGrpMemInfo <$> B64.decode memId <*> getJSON body)
+ ("x.grp.mem.con", [memId]) ->
+ chatMsg . XGrpMemCon =<< B64.decode memId
+ ("x.grp.mem.con.all", [memId]) ->
+ chatMsg . XGrpMemConAll =<< B64.decode memId
+ ("x.grp.mem.del", [memId]) ->
+ chatMsg . XGrpMemDel =<< B64.decode memId
+ ("x.grp.leave", []) ->
+ chatMsg XGrpLeave
+ ("x.grp.del", []) ->
+ chatMsg XGrpDel
+ ("x.info.probe", [probe]) -> do
+ chatMsg . XInfoProbe =<< B64.decode probe
+ ("x.info.probe.check", [probeHash]) -> do
+ chatMsg =<< (XInfoProbeCheck <$> B64.decode probeHash)
+ ("x.info.probe.ok", [probe]) -> do
+ chatMsg =<< (XInfoProbeOk <$> B64.decode probe)
+ ("x.ok", []) ->
+ chatMsg XOk
+ _ -> Left $ "bad syntax or unsupported event " <> B.unpack chatMsgEvent
+ where
+ getDAG :: [MsgContentBody] -> (Maybe ByteString, [MsgContentBody])
+ getDAG body = case break (isContentType SimplexDAG) body of
+ (b, MsgContentBody SimplexDAG dag : a) -> (Just dag, b <> a)
+ _ -> (Nothing, body)
+ toMemberInfo :: ByteString -> ByteString -> [MsgContentBody] -> Either String MemberInfo
+ toMemberInfo memId role body = MemberInfo <$> B64.decode memId <*> toMemberRole role <*> getJSON body
+ toIntroInv :: ByteString -> ByteString -> Either String IntroInvitation
+ toIntroInv groupQInfo directQInfo = IntroInvitation <$> parseAll smpQueueInfoP groupQInfo <*> parseAll smpQueueInfoP directQInfo
+ toContentInfo :: (RawContentType, Int) -> Either String (ContentType, Int)
+ toContentInfo (rawType, size) = (,size) <$> toContentType rawType
+ getJSON :: FromJSON a => [MsgContentBody] -> Either String a
+ getJSON = J.eitherDecodeStrict' <=< getSimplexContentType XCJson
+
+isContentType :: ContentType -> MsgContentBody -> Bool
+isContentType t MsgContentBody {contentType = t'} = t == t'
+
+isSimplexContentType :: XContentType -> MsgContentBody -> Bool
+isSimplexContentType = isContentType . SimplexContentType
+
+getContentType :: ContentType -> [MsgContentBody] -> Either String ByteString
+getContentType t body = case find (isContentType t) body of
+ Just MsgContentBody {contentData} -> Right contentData
+ Nothing -> Left "no required content type"
+
+getSimplexContentType :: XContentType -> [MsgContentBody] -> Either String ByteString
+getSimplexContentType = getContentType . SimplexContentType
+
+rawChatMessage :: ChatMessage -> RawChatMessage
+rawChatMessage ChatMessage {chatMsgId, chatMsgEvent, chatDAG} =
+ case chatMsgEvent of
+ XMsgNew MsgContent {messageType = t, files, content} ->
+ let rawFiles = map (serializeContentInfo . rawContentInfo) files
+ in rawMsg "x.msg.new" (rawMsgType t : rawFiles) content
+ XFile FileInvitation {fileName, fileSize, fileQInfo} ->
+ rawMsg "x.file" [encodeUtf8 $ T.pack fileName, bshow fileSize, serializeSmpQueueInfo fileQInfo] []
+ XFileAcpt fileName ->
+ rawMsg "x.file.acpt" [encodeUtf8 $ T.pack fileName] []
+ XInfo profile ->
+ rawMsg "x.info" [] [jsonBody profile]
+ XGrpInv (GroupInvitation (fromMemId, fromRole) (memId, role) qInfo groupProfile) ->
+ let params =
+ [ B64.encode fromMemId,
+ serializeMemberRole fromRole,
+ B64.encode memId,
+ serializeMemberRole role,
+ serializeSmpQueueInfo qInfo
+ ]
+ in rawMsg "x.grp.inv" params [jsonBody groupProfile]
+ XGrpAcpt memId ->
+ rawMsg "x.grp.acpt" [B64.encode memId] []
+ XGrpMemNew (MemberInfo memId role profile) ->
+ let params = [B64.encode memId, serializeMemberRole role]
+ in rawMsg "x.grp.mem.new" params [jsonBody profile]
+ XGrpMemIntro (MemberInfo memId role profile) ->
+ rawMsg "x.grp.mem.intro" [B64.encode memId, serializeMemberRole role] [jsonBody profile]
+ XGrpMemInv memId IntroInvitation {groupQInfo, directQInfo} ->
+ let params = [B64.encode memId, serializeSmpQueueInfo groupQInfo, serializeSmpQueueInfo directQInfo]
+ in rawMsg "x.grp.mem.inv" params []
+ XGrpMemFwd (MemberInfo memId role profile) IntroInvitation {groupQInfo, directQInfo} ->
+ let params =
+ [ B64.encode memId,
+ serializeMemberRole role,
+ serializeSmpQueueInfo groupQInfo,
+ serializeSmpQueueInfo directQInfo
+ ]
+ in rawMsg "x.grp.mem.fwd" params [jsonBody profile]
+ XGrpMemInfo memId profile ->
+ rawMsg "x.grp.mem.info" [B64.encode memId] [jsonBody profile]
+ XGrpMemCon memId ->
+ rawMsg "x.grp.mem.con" [B64.encode memId] []
+ XGrpMemConAll memId ->
+ rawMsg "x.grp.mem.con.all" [B64.encode memId] []
+ XGrpMemDel memId ->
+ rawMsg "x.grp.mem.del" [B64.encode memId] []
+ XGrpLeave ->
+ rawMsg "x.grp.leave" [] []
+ XGrpDel ->
+ rawMsg "x.grp.del" [] []
+ XInfoProbe probe ->
+ rawMsg "x.info.probe" [B64.encode probe] []
+ XInfoProbeCheck probeHash ->
+ rawMsg "x.info.probe.check" [B64.encode probeHash] []
+ XInfoProbeOk probe ->
+ rawMsg "x.info.probe.ok" [B64.encode probe] []
+ XOk ->
+ rawMsg "x.ok" [] []
+ where
+ rawMsg :: ByteString -> [ByteString] -> [MsgContentBody] -> RawChatMessage
+ rawMsg event chatMsgParams body =
+ RawChatMessage {chatMsgId, chatMsgEvent = event, chatMsgParams, chatMsgBody = rawWithDAG body}
+ rawContentInfo :: (ContentType, Int) -> (RawContentType, Int)
+ rawContentInfo (t, size) = (rawContentType t, size)
+ jsonBody :: ToJSON a => a -> MsgContentBody
+ jsonBody x =
+ let json = LB.toStrict $ J.encode x
+ in MsgContentBody {contentType = SimplexContentType XCJson, contentData = json}
+ rawWithDAG :: [MsgContentBody] -> [RawMsgBodyContent]
+ rawWithDAG body = map rawMsgBodyContent $ case chatDAG of
+ Nothing -> body
+ Just dag -> MsgContentBody {contentType = SimplexDAG, contentData = dag} : body
+
+toMsgBodyContent :: RawMsgBodyContent -> Either String MsgContentBody
+toMsgBodyContent RawMsgBodyContent {contentType, contentData} = do
+ cType <- toContentType contentType
+ pure MsgContentBody {contentType = cType, contentData}
+
+rawMsgBodyContent :: MsgContentBody -> RawMsgBodyContent
+rawMsgBodyContent MsgContentBody {contentType = t, contentData} =
+ RawMsgBodyContent {contentType = rawContentType t, contentData}
+
+data MsgContentBody = MsgContentBody
+ { contentType :: ContentType,
+ contentData :: ByteString
+ }
+ deriving (Eq, Show)
+
+data ContentType
+ = SimplexContentType XContentType
+ | MimeContentType MContentType
+ | SimplexDAG
+ deriving (Eq, Show)
+
+data XContentType = XCText | XCImage | XCJson deriving (Eq, Show)
+
+data MContentType = MCImageJPG | MCImagePNG deriving (Eq, Show)
+
+toContentType :: RawContentType -> Either String ContentType
+toContentType (RawContentType ns cType) = case ns of
+ "x" -> case cType of
+ "text" -> Right $ SimplexContentType XCText
+ "image" -> Right $ SimplexContentType XCImage
+ "json" -> Right $ SimplexContentType XCJson
+ "dag" -> Right SimplexDAG
+ _ -> err
+ "m" -> case cType of
+ "image/jpg" -> Right $ MimeContentType MCImageJPG
+ "image/png" -> Right $ MimeContentType MCImagePNG
+ _ -> err
+ _ -> err
+ where
+ err = Left . B.unpack $ "invalid content type " <> ns <> "." <> cType
+
+rawContentType :: ContentType -> RawContentType
+rawContentType t = case t of
+ SimplexContentType t' -> RawContentType "x" $ case t' of
+ XCText -> "text"
+ XCImage -> "image"
+ XCJson -> "json"
+ MimeContentType t' -> RawContentType "m" $ case t' of
+ MCImageJPG -> "image/jpg"
+ MCImagePNG -> "image/png"
+ SimplexDAG -> RawContentType "x" "dag"
+
+newtype ContentMsg = NewContentMsg ContentData
+
+newtype ContentData = ContentText Text
+
+data RawChatMessage = RawChatMessage
+ { chatMsgId :: Maybe Int64,
+ chatMsgEvent :: ByteString,
+ chatMsgParams :: [ByteString],
+ chatMsgBody :: [RawMsgBodyContent]
+ }
+ deriving (Eq, Show)
+
+data RawMsgBodyContent = RawMsgBodyContent
+ { contentType :: RawContentType,
+ contentData :: ByteString
+ }
+ deriving (Eq, Show)
+
+data RawContentType = RawContentType NameSpace ByteString
+ deriving (Eq, Show)
+
+type NameSpace = ByteString
+
+newtype MsgData = MsgData ByteString
+ deriving (Eq, Show)
+
+class DataLength a where
+ dataLength :: a -> Int
+
+rawChatMessageP :: Parser RawChatMessage
+rawChatMessageP = do
+ chatMsgId <- optional A.decimal <* A.space
+ chatMsgEvent <- B.intercalate "." <$> identifierP `A.sepBy1'` A.char '.' <* A.space
+ chatMsgParams <- A.takeWhile1 (not . A.inClass ", ") `A.sepBy'` A.char ',' <* A.space
+ chatMsgBody <- msgBodyContent =<< contentInfoP `A.sepBy'` A.char ',' <* A.space
+ pure RawChatMessage {chatMsgId, chatMsgEvent, chatMsgParams, chatMsgBody}
+ where
+ msgBodyContent :: [(RawContentType, Int)] -> Parser [RawMsgBodyContent]
+ msgBodyContent [] = pure []
+ msgBodyContent ((contentType, size) : ps) = do
+ contentData <- A.take size <* A.space
+ ((RawMsgBodyContent {contentType, contentData}) :) <$> msgBodyContent ps
+
+contentInfoP :: Parser (RawContentType, Int)
+contentInfoP = do
+ contentType <- RawContentType <$> identifierP <* A.char '.' <*> A.takeTill (A.inClass ":, ")
+ size <- A.char ':' *> A.decimal
+ pure (contentType, size)
+
+identifierP :: Parser ByteString
+identifierP = B.cons <$> A.letter_ascii <*> A.takeWhile (\c -> A.isAlpha_ascii c || A.isDigit c)
+
+serializeRawChatMessage :: RawChatMessage -> ByteString
+serializeRawChatMessage RawChatMessage {chatMsgId, chatMsgEvent, chatMsgParams, chatMsgBody} =
+ B.unwords
+ [ maybe "" bshow chatMsgId,
+ chatMsgEvent,
+ B.intercalate "," chatMsgParams,
+ B.unwords $ map serializeBodyContentInfo chatMsgBody,
+ B.unwords $ map msgContentData chatMsgBody
+ ]
+
+serializeBodyContentInfo :: RawMsgBodyContent -> ByteString
+serializeBodyContentInfo RawMsgBodyContent {contentType = t, contentData} =
+ serializeContentInfo (t, B.length contentData)
+
+serializeContentInfo :: (RawContentType, Int) -> ByteString
+serializeContentInfo (RawContentType ns cType, size) = ns <> "." <> cType <> ":" <> bshow size
+
+msgContentData :: RawMsgBodyContent -> ByteString
+msgContentData RawMsgBodyContent {contentData} = contentData <> " "
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Store.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Store.hs
new file mode 100644
index 0000000000..ccac1f746f
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Store.hs
@@ -0,0 +1,1490 @@
+{-# LANGUAGE ConstraintKinds #-}
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE QuasiQuotes #-}
+{-# LANGUAGE RecordWildCards #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE TupleSections #-}
+{-# LANGUAGE TypeOperators #-}
+
+module Simplex.Chat.Store
+ ( SQLiteStore,
+ StoreError (..),
+ createStore,
+ createUser,
+ getUsers,
+ setActiveUser,
+ createDirectConnection,
+ createDirectContact,
+ getContactGroupNames,
+ deleteContact,
+ getContact,
+ updateUserProfile,
+ updateContactProfile,
+ getUserContacts,
+ getLiveSndFileTransfers,
+ getLiveRcvFileTransfers,
+ getPendingSndChunks,
+ getPendingConnections,
+ getContactConnections,
+ getConnectionChatDirection,
+ updateConnectionStatus,
+ createNewGroup,
+ createGroupInvitation,
+ getGroup,
+ deleteGroup,
+ getUserGroups,
+ getGroupInvitation,
+ createContactGroupMember,
+ createMemberConnection,
+ updateGroupMemberStatus,
+ createNewGroupMember,
+ deleteGroupMemberConnection,
+ createIntroductions,
+ updateIntroStatus,
+ saveIntroInvitation,
+ createIntroReMember,
+ createIntroToMemberContact,
+ saveMemberInvitation,
+ getViaGroupMember,
+ getViaGroupContact,
+ getMatchingContacts,
+ randomBytes,
+ createSentProbe,
+ createSentProbeHash,
+ matchReceivedProbe,
+ matchReceivedProbeHash,
+ matchSentProbe,
+ mergeContactRecords,
+ createSndFileTransfer,
+ createSndGroupFileTransfer,
+ updateSndFileStatus,
+ createSndFileChunk,
+ updateSndFileChunkMsg,
+ updateSndFileChunkSent,
+ deleteSndFileChunks,
+ createRcvFileTransfer,
+ createRcvGroupFileTransfer,
+ getRcvFileTransfer,
+ acceptRcvFileTransfer,
+ updateRcvFileStatus,
+ createRcvFileChunk,
+ updatedRcvFileChunkStored,
+ deleteRcvFileChunks,
+ getFileTransfer,
+ getFileTransferProgress,
+ )
+where
+
+import Control.Applicative ((<|>))
+import Control.Concurrent.STM (stateTVar)
+import Control.Exception (Exception)
+import qualified Control.Exception as E
+import Control.Monad.Except
+import Control.Monad.IO.Unlift
+import Crypto.Random (ChaChaDRG, randomBytesGenerate)
+import qualified Data.ByteString.Base64 as B64
+import Data.ByteString.Char8 (ByteString)
+import Data.Either (rights)
+import Data.FileEmbed (embedDir, makeRelativeToProject)
+import Data.Function (on)
+import Data.Functor (($>))
+import Data.Int (Int64)
+import Data.List (find, sortBy)
+import Data.Maybe (listToMaybe)
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Text.Encoding (decodeUtf8)
+import Data.Time.Clock (UTCTime, getCurrentTime)
+import Database.SQLite.Simple (NamedParam (..), Only (..), SQLError, (:.) (..))
+import qualified Database.SQLite.Simple as DB
+import Database.SQLite.Simple.QQ (sql)
+import Simplex.Chat.Protocol
+import Simplex.Chat.Types
+import Simplex.Messaging.Agent.Protocol (AParty (..), AgentMsgId, ConnId, SMPQueueInfo)
+import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore (..), createSQLiteStore, withTransaction)
+import Simplex.Messaging.Agent.Store.SQLite.Migrations (Migration (..))
+import qualified Simplex.Messaging.Crypto as C
+import Simplex.Messaging.Util (bshow, liftIOEither, (<$$>))
+import System.FilePath (takeBaseName, takeExtension, takeFileName)
+import UnliftIO.STM
+
+-- | The list of migrations in ascending order by date
+migrations :: [Migration]
+migrations =
+ sortBy (compare `on` name) . map migration . filter sqlFile $
+ $(makeRelativeToProject "migrations" >>= embedDir)
+ where
+ sqlFile (file, _) = takeExtension file == ".sql"
+ migration (file, qStr) = Migration {name = takeBaseName file, up = decodeUtf8 qStr}
+
+createStore :: FilePath -> Int -> IO SQLiteStore
+createStore dbFilePath poolSize = createSQLiteStore dbFilePath poolSize migrations
+
+checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a)
+checkConstraint err action = action `E.catch` (pure . Left . handleSQLError err)
+
+handleSQLError :: StoreError -> SQLError -> StoreError
+handleSQLError err e
+ | DB.sqlError e == DB.ErrorConstraint = err
+ | otherwise = SEInternal $ bshow e
+
+insertedRowId :: DB.Connection -> IO Int64
+insertedRowId db = fromOnly . head <$> DB.query_ db "SELECT last_insert_rowid()"
+
+type StoreMonad m = (MonadUnliftIO m, MonadError StoreError m)
+
+createUser :: StoreMonad m => SQLiteStore -> Profile -> Bool -> m User
+createUser st Profile {displayName, fullName} activeUser =
+ liftIOEither . checkConstraint SEDuplicateName . withTransaction st $ \db -> do
+ DB.execute db "INSERT INTO users (local_display_name, active_user, contact_id) VALUES (?, ?, 0)" (displayName, activeUser)
+ userId <- insertedRowId db
+ DB.execute db "INSERT INTO display_names (local_display_name, ldn_base, user_id) VALUES (?, ?, ?)" (displayName, displayName, userId)
+ DB.execute db "INSERT INTO contact_profiles (display_name, full_name) VALUES (?, ?)" (displayName, fullName)
+ profileId <- insertedRowId db
+ DB.execute db "INSERT INTO contacts (contact_profile_id, local_display_name, user_id, is_user) VALUES (?, ?, ?, ?)" (profileId, displayName, userId, True)
+ contactId <- insertedRowId db
+ DB.execute db "UPDATE users SET contact_id = ? WHERE user_id = ?" (contactId, userId)
+ pure . Right $ toUser (userId, contactId, activeUser, displayName, fullName)
+
+getUsers :: SQLiteStore -> IO [User]
+getUsers st =
+ withTransaction st $ \db ->
+ map toUser
+ <$> DB.query_
+ db
+ [sql|
+ SELECT u.user_id, u.contact_id, u.active_user, u.local_display_name, p.full_name
+ FROM users u
+ JOIN contacts c ON u.contact_id = c.contact_id
+ JOIN contact_profiles p ON c.contact_profile_id = p.contact_profile_id
+ |]
+
+toUser :: (UserId, Int64, Bool, ContactName, Text) -> User
+toUser (userId, userContactId, activeUser, displayName, fullName) =
+ let profile = Profile {displayName, fullName}
+ in User {userId, userContactId, localDisplayName = displayName, profile, activeUser}
+
+setActiveUser :: MonadUnliftIO m => SQLiteStore -> UserId -> m ()
+setActiveUser st userId = do
+ liftIO . withTransaction st $ \db -> do
+ DB.execute_ db "UPDATE users SET active_user = 0"
+ DB.execute db "UPDATE users SET active_user = 1 WHERE user_id = ?" (Only userId)
+
+createDirectConnection :: MonadUnliftIO m => SQLiteStore -> UserId -> ConnId -> m ()
+createDirectConnection st userId agentConnId =
+ liftIO . withTransaction st $ \db ->
+ void $ createConnection_ db userId agentConnId Nothing 0
+
+createConnection_ :: DB.Connection -> UserId -> ConnId -> Maybe Int64 -> Int -> IO Connection
+createConnection_ db userId agentConnId viaContact connLevel = do
+ createdAt <- getCurrentTime
+ DB.execute
+ db
+ [sql|
+ INSERT INTO connections
+ (user_id, agent_conn_id, conn_status, conn_type, via_contact, conn_level, created_at) VALUES (?,?,?,?,?,?,?);
+ |]
+ (userId, agentConnId, ConnNew, ConnContact, viaContact, connLevel, createdAt)
+ connId <- insertedRowId db
+ pure Connection {connId, agentConnId, connType = ConnContact, entityId = Nothing, viaContact, connLevel, connStatus = ConnNew, createdAt}
+
+createDirectContact :: StoreMonad m => SQLiteStore -> UserId -> Connection -> Profile -> m ()
+createDirectContact st userId Connection {connId} profile =
+ void $
+ liftIOEither . withTransaction st $ \db ->
+ createContact_ db userId connId profile Nothing
+
+createContact_ :: DB.Connection -> UserId -> Int64 -> Profile -> Maybe Int64 -> IO (Either StoreError (Text, Int64, Int64))
+createContact_ db userId connId Profile {displayName, fullName} viaGroup =
+ withLocalDisplayName db userId displayName $ \ldn -> do
+ DB.execute db "INSERT INTO contact_profiles (display_name, full_name) VALUES (?, ?)" (displayName, fullName)
+ profileId <- insertedRowId db
+ DB.execute db "INSERT INTO contacts (contact_profile_id, local_display_name, user_id, via_group) VALUES (?,?,?,?)" (profileId, ldn, userId, viaGroup)
+ contactId <- insertedRowId db
+ DB.execute db "UPDATE connections SET contact_id = ? WHERE connection_id = ?" (contactId, connId)
+ pure (ldn, contactId, profileId)
+
+getContactGroupNames :: MonadUnliftIO m => SQLiteStore -> UserId -> ContactName -> m [GroupName]
+getContactGroupNames st userId displayName =
+ liftIO . withTransaction st $ \db -> do
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT DISTINCT g.local_display_name
+ FROM groups g
+ JOIN group_members m ON m.group_id = g.group_id
+ WHERE g.user_id = ? AND m.local_display_name = ?
+ |]
+ (userId, displayName)
+
+deleteContact :: MonadUnliftIO m => SQLiteStore -> UserId -> ContactName -> m ()
+deleteContact st userId displayName =
+ liftIO . withTransaction st $ \db -> do
+ DB.executeNamed
+ db
+ [sql|
+ DELETE FROM connections WHERE connection_id IN (
+ SELECT connection_id
+ FROM connections c
+ JOIN contacts cs ON c.contact_id = cs.contact_id
+ WHERE cs.user_id = :user_id AND cs.local_display_name = :display_name
+ )
+ |]
+ [":user_id" := userId, ":display_name" := displayName]
+ DB.executeNamed
+ db
+ [sql|
+ DELETE FROM contacts
+ WHERE user_id = :user_id AND local_display_name = :display_name
+ |]
+ [":user_id" := userId, ":display_name" := displayName]
+ DB.executeNamed
+ db
+ [sql|
+ DELETE FROM display_names
+ WHERE user_id = :user_id AND local_display_name = :display_name
+ |]
+ [":user_id" := userId, ":display_name" := displayName]
+
+getContact :: StoreMonad m => SQLiteStore -> UserId -> ContactName -> m Contact
+getContact st userId localDisplayName =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ getContact_ db userId localDisplayName
+
+updateUserProfile :: StoreMonad m => SQLiteStore -> User -> Profile -> m User
+updateUserProfile st u@User {userId, userContactId, localDisplayName, profile = Profile {displayName}} p'@Profile {displayName = newName}
+ | displayName == newName =
+ liftIO . withTransaction st $ \db ->
+ updateContactProfile_ db userId userContactId p' $> (u :: User) {profile = p'}
+ | otherwise =
+ liftIOEither . checkConstraint SEDuplicateName . withTransaction st $ \db -> do
+ DB.execute db "UPDATE users SET local_display_name = ? WHERE user_id = ?" (newName, userId)
+ DB.execute db "INSERT INTO display_names (local_display_name, ldn_base, user_id) VALUES (?, ?, ?)" (newName, newName, userId)
+ updateContactProfile_ db userId userContactId p'
+ updateContact_ db userId userContactId localDisplayName newName
+ pure . Right $ (u :: User) {localDisplayName = newName, profile = p'}
+
+updateContactProfile :: StoreMonad m => SQLiteStore -> UserId -> Contact -> Profile -> m Contact
+updateContactProfile st userId c@Contact {contactId, localDisplayName, profile = Profile {displayName}} p'@Profile {displayName = newName}
+ | displayName == newName =
+ liftIO . withTransaction st $ \db ->
+ updateContactProfile_ db userId contactId p' $> (c :: Contact) {profile = p'}
+ | otherwise =
+ liftIOEither . withTransaction st $ \db ->
+ withLocalDisplayName db userId newName $ \ldn -> do
+ updateContactProfile_ db userId contactId p'
+ updateContact_ db userId contactId localDisplayName ldn
+ pure $ (c :: Contact) {localDisplayName = ldn, profile = p'}
+
+updateContactProfile_ :: DB.Connection -> UserId -> Int64 -> Profile -> IO ()
+updateContactProfile_ db userId contactId Profile {displayName, fullName} =
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE contact_profiles
+ SET display_name = :display_name,
+ full_name = :full_name
+ WHERE contact_profile_id IN (
+ SELECT contact_profile_id
+ FROM contacts
+ WHERE user_id = :user_id
+ AND contact_id = :contact_id
+ )
+ |]
+ [ ":display_name" := displayName,
+ ":full_name" := fullName,
+ ":user_id" := userId,
+ ":contact_id" := contactId
+ ]
+
+updateContact_ :: DB.Connection -> UserId -> Int64 -> ContactName -> ContactName -> IO ()
+updateContact_ db userId contactId displayName newName = do
+ DB.execute db "UPDATE contacts SET local_display_name = ? WHERE user_id = ? AND contact_id = ?" (newName, userId, contactId)
+ DB.execute db "UPDATE group_members SET local_display_name = ? WHERE user_id = ? AND contact_id = ?" (newName, userId, contactId)
+ DB.execute db "DELETE FROM display_names WHERE local_display_name = ? AND user_id = ?" (displayName, userId)
+
+-- TODO return the last connection that is ready, not any last connection
+-- requires updating connection status
+getContact_ :: DB.Connection -> UserId -> ContactName -> ExceptT StoreError IO Contact
+getContact_ db userId localDisplayName = do
+ c@Contact {contactId} <- getContactRec_
+ activeConn <- getConnection_ contactId
+ pure $ (c :: Contact) {activeConn}
+ where
+ getContactRec_ :: ExceptT StoreError IO Contact
+ getContactRec_ = ExceptT $ do
+ toContact
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT c.contact_id, p.display_name, p.full_name, c.via_group
+ FROM contacts c
+ JOIN contact_profiles p ON c.contact_profile_id = p.contact_profile_id
+ WHERE c.user_id = :user_id AND c.local_display_name = :local_display_name AND c.is_user = :is_user
+ |]
+ [":user_id" := userId, ":local_display_name" := localDisplayName, ":is_user" := False]
+ getConnection_ :: Int64 -> ExceptT StoreError IO Connection
+ getConnection_ contactId = ExceptT $ do
+ connection
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact,
+ c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.created_at
+ FROM connections c
+ WHERE c.user_id = :user_id AND c.contact_id == :contact_id
+ ORDER BY c.connection_id DESC
+ LIMIT 1
+ |]
+ [":user_id" := userId, ":contact_id" := contactId]
+ toContact :: [(Int64, Text, Text, Maybe Int64)] -> Either StoreError Contact
+ toContact [(contactId, displayName, fullName, viaGroup)] =
+ let profile = Profile {displayName, fullName}
+ in Right Contact {contactId, localDisplayName, profile, activeConn = undefined, viaGroup}
+ toContact _ = Left $ SEContactNotFound localDisplayName
+ connection :: [ConnectionRow] -> Either StoreError Connection
+ connection (connRow : _) = Right $ toConnection connRow
+ connection _ = Left $ SEContactNotReady localDisplayName
+
+getUserContacts :: MonadUnliftIO m => SQLiteStore -> User -> m [Contact]
+getUserContacts st User {userId} =
+ liftIO . withTransaction st $ \db -> do
+ contactNames <- map fromOnly <$> DB.query db "SELECT local_display_name FROM contacts WHERE user_id = ?" (Only userId)
+ rights <$> mapM (runExceptT . getContact_ db userId) contactNames
+
+getLiveSndFileTransfers :: MonadUnliftIO m => SQLiteStore -> User -> m [SndFileTransfer]
+getLiveSndFileTransfers st User {userId} =
+ liftIO . withTransaction st $ \db -> do
+ fileIds :: [Int64] <-
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT DISTINCT f.file_id
+ FROM files f
+ JOIN snd_files s
+ WHERE f.user_id = ? AND s.file_status IN (?, ?, ?)
+ |]
+ (userId, FSNew, FSAccepted, FSConnected)
+ concatMap (filter liveTransfer) . rights <$> mapM (getSndFileTransfers_ db userId) fileIds
+ where
+ liveTransfer :: SndFileTransfer -> Bool
+ liveTransfer SndFileTransfer {fileStatus} = fileStatus `elem` [FSNew, FSAccepted, FSConnected]
+
+getLiveRcvFileTransfers :: MonadUnliftIO m => SQLiteStore -> User -> m [RcvFileTransfer]
+getLiveRcvFileTransfers st User {userId} =
+ liftIO . withTransaction st $ \db -> do
+ fileIds :: [Int64] <-
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT f.file_id
+ FROM files f
+ JOIN rcv_files r
+ WHERE f.user_id = ? AND r.file_status IN (?, ?)
+ |]
+ (userId, FSAccepted, FSConnected)
+ rights <$> mapM (getRcvFileTransfer_ db userId) fileIds
+
+getPendingSndChunks :: MonadUnliftIO m => SQLiteStore -> Int64 -> Int64 -> m [Integer]
+getPendingSndChunks st fileId connId =
+ liftIO . withTransaction st $ \db ->
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT chunk_number
+ FROM snd_file_chunks
+ WHERE file_id = ? AND connection_id = ? AND chunk_agent_msg_id IS NULL
+ ORDER BY chunk_number
+ |]
+ (fileId, connId)
+
+getPendingConnections :: MonadUnliftIO m => SQLiteStore -> User -> m [Connection]
+getPendingConnections st User {userId} =
+ liftIO . withTransaction st $ \db ->
+ map toConnection
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT connection_id, agent_conn_id, conn_level, via_contact,
+ conn_status, conn_type, contact_id, group_member_id, snd_file_id, rcv_file_id, created_at
+ FROM connections
+ WHERE user_id = :user_id
+ AND conn_type = :conn_type
+ AND contact_id IS NULL
+ |]
+ [":user_id" := userId, ":conn_type" := ConnContact]
+
+getContactConnections :: StoreMonad m => SQLiteStore -> UserId -> ContactName -> m [Connection]
+getContactConnections st userId displayName =
+ liftIOEither . withTransaction st $ \db ->
+ connections
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact,
+ c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.created_at
+ FROM connections c
+ JOIN contacts cs ON c.contact_id == cs.contact_id
+ WHERE c.user_id = :user_id
+ AND cs.user_id = :user_id
+ AND cs.local_display_name == :display_name
+ |]
+ [":user_id" := userId, ":display_name" := displayName]
+ where
+ connections [] = Left $ SEContactNotFound displayName
+ connections rows = Right $ map toConnection rows
+
+type ConnectionRow = (Int64, ConnId, Int, Maybe Int64, ConnStatus, ConnType, Maybe Int64, Maybe Int64, Maybe Int64, Maybe Int64, UTCTime)
+
+type MaybeConnectionRow = (Maybe Int64, Maybe ConnId, Maybe Int, Maybe Int64, Maybe ConnStatus, Maybe ConnType, Maybe Int64, Maybe Int64, Maybe Int64, Maybe Int64, Maybe UTCTime)
+
+toConnection :: ConnectionRow -> Connection
+toConnection (connId, agentConnId, connLevel, viaContact, connStatus, connType, contactId, groupMemberId, sndFileId, rcvFileId, createdAt) =
+ let entityId = entityId_ connType
+ in Connection {connId, agentConnId, connLevel, viaContact, connStatus, connType, entityId, createdAt}
+ where
+ entityId_ :: ConnType -> Maybe Int64
+ entityId_ ConnContact = contactId
+ entityId_ ConnMember = groupMemberId
+ entityId_ ConnRcvFile = rcvFileId
+ entityId_ ConnSndFile = sndFileId
+
+toMaybeConnection :: MaybeConnectionRow -> Maybe Connection
+toMaybeConnection (Just connId, Just agentConnId, Just connLevel, viaContact, Just connStatus, Just connType, contactId, groupMemberId, sndFileId, rcvFileId, Just createdAt) =
+ Just $ toConnection (connId, agentConnId, connLevel, viaContact, connStatus, connType, contactId, groupMemberId, sndFileId, rcvFileId, createdAt)
+toMaybeConnection _ = Nothing
+
+getMatchingContacts :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> m [Contact]
+getMatchingContacts st userId Contact {contactId, profile = Profile {displayName, fullName}} =
+ liftIO . withTransaction st $ \db -> do
+ contactNames <-
+ map fromOnly
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT ct.local_display_name
+ FROM contacts ct
+ JOIN contact_profiles p ON ct.contact_profile_id = p.contact_profile_id
+ WHERE ct.user_id = :user_id AND ct.contact_id != :contact_id
+ AND p.display_name = :display_name AND p.full_name = :full_name
+ |]
+ [ ":user_id" := userId,
+ ":contact_id" := contactId,
+ ":display_name" := displayName,
+ ":full_name" := fullName
+ ]
+ rights <$> mapM (runExceptT . getContact_ db userId) contactNames
+
+createSentProbe :: StoreMonad m => SQLiteStore -> TVar ChaChaDRG -> UserId -> Contact -> m (ByteString, Int64)
+createSentProbe st gVar userId _to@Contact {contactId} =
+ liftIOEither . withTransaction st $ \db ->
+ createWithRandomBytes 32 gVar $ \probe -> do
+ DB.execute db "INSERT INTO sent_probes (contact_id, probe, user_id) VALUES (?,?,?)" (contactId, probe, userId)
+ (probe,) <$> insertedRowId db
+
+createSentProbeHash :: MonadUnliftIO m => SQLiteStore -> UserId -> Int64 -> Contact -> m ()
+createSentProbeHash st userId probeId _to@Contact {contactId} =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "INSERT INTO sent_probe_hashes (sent_probe_id, contact_id, user_id) VALUES (?,?,?)" (probeId, contactId, userId)
+
+matchReceivedProbe :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> ByteString -> m (Maybe Contact)
+matchReceivedProbe st userId _from@Contact {contactId} probe =
+ liftIO . withTransaction st $ \db -> do
+ let probeHash = C.sha256Hash probe
+ contactNames <-
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT c.local_display_name
+ FROM contacts c
+ JOIN received_probes r ON r.contact_id = c.contact_id
+ WHERE c.user_id = ? AND r.probe_hash = ? AND r.probe IS NULL
+ |]
+ (userId, probeHash)
+ DB.execute db "INSERT INTO received_probes (contact_id, probe, probe_hash, user_id) VALUES (?,?,?,?)" (contactId, probe, probeHash, userId)
+ case contactNames of
+ [] -> pure Nothing
+ cName : _ ->
+ either (const Nothing) Just
+ <$> runExceptT (getContact_ db userId cName)
+
+matchReceivedProbeHash :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> ByteString -> m (Maybe (Contact, ByteString))
+matchReceivedProbeHash st userId _from@Contact {contactId} probeHash =
+ liftIO . withTransaction st $ \db -> do
+ namesAndProbes <-
+ DB.query
+ db
+ [sql|
+ SELECT c.local_display_name, r.probe
+ FROM contacts c
+ JOIN received_probes r ON r.contact_id = c.contact_id
+ WHERE c.user_id = ? AND r.probe_hash = ? AND r.probe IS NOT NULL
+ |]
+ (userId, probeHash)
+ DB.execute db "INSERT INTO received_probes (contact_id, probe_hash, user_id) VALUES (?,?,?)" (contactId, probeHash, userId)
+ case namesAndProbes of
+ [] -> pure Nothing
+ (cName, probe) : _ ->
+ either (const Nothing) (Just . (,probe))
+ <$> runExceptT (getContact_ db userId cName)
+
+matchSentProbe :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> ByteString -> m (Maybe Contact)
+matchSentProbe st userId _from@Contact {contactId} probe =
+ liftIO . withTransaction st $ \db -> do
+ contactNames <-
+ map fromOnly
+ <$> DB.query
+ db
+ [sql|
+ SELECT c.local_display_name
+ FROM contacts c
+ JOIN sent_probes s ON s.contact_id = c.contact_id
+ JOIN sent_probe_hashes h ON h.sent_probe_id = s.sent_probe_id
+ WHERE c.user_id = ? AND s.probe = ? AND h.contact_id = ?
+ |]
+ (userId, probe, contactId)
+ case contactNames of
+ [] -> pure Nothing
+ cName : _ ->
+ either (const Nothing) Just
+ <$> runExceptT (getContact_ db userId cName)
+
+mergeContactRecords :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> Contact -> m ()
+mergeContactRecords st userId Contact {contactId = toContactId} Contact {contactId = fromContactId, localDisplayName} =
+ liftIO . withTransaction st $ \db -> do
+ DB.execute db "UPDATE connections SET contact_id = ? WHERE contact_id = ? AND user_id = ?" (toContactId, fromContactId, userId)
+ DB.execute db "UPDATE connections SET via_contact = ? WHERE via_contact = ? AND user_id = ?" (toContactId, fromContactId, userId)
+ DB.execute db "UPDATE group_members SET invited_by = ? WHERE invited_by = ? AND user_id = ?" (toContactId, fromContactId, userId)
+ DB.execute db "UPDATE messages SET contact_id = ? WHERE contact_id = ?" (toContactId, fromContactId)
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_members
+ SET contact_id = :to_contact_id,
+ local_display_name = (SELECT local_display_name FROM contacts WHERE contact_id = :to_contact_id),
+ contact_profile_id = (SELECT contact_profile_id FROM contacts WHERE contact_id = :to_contact_id)
+ WHERE contact_id = :from_contact_id
+ AND user_id = :user_id
+ |]
+ [ ":to_contact_id" := toContactId,
+ ":from_contact_id" := fromContactId,
+ ":user_id" := userId
+ ]
+ DB.execute db "DELETE FROM contacts WHERE contact_id = ? AND user_id = ?" (fromContactId, userId)
+ DB.execute db "DELETE FROM display_names WHERE local_display_name = ? AND user_id = ?" (localDisplayName, userId)
+
+getConnectionChatDirection :: StoreMonad m => SQLiteStore -> User -> ConnId -> m (ChatDirection 'Agent)
+getConnectionChatDirection st User {userId, userContactId} agentConnId =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ do
+ c@Connection {connType, entityId} <- getConnection_ db
+ case entityId of
+ Nothing ->
+ if connType == ConnContact
+ then pure $ ReceivedDirectMessage c Nothing
+ else throwError $ SEInternal $ "connection " <> bshow connType <> " without entity"
+ Just entId ->
+ case connType of
+ ConnMember -> uncurry (ReceivedGroupMessage c) <$> getGroupAndMember_ db entId c
+ ConnContact -> ReceivedDirectMessage c . Just <$> getContactRec_ db entId c
+ ConnSndFile -> SndFileConnection c <$> getConnSndFileTransfer_ db entId c
+ ConnRcvFile -> RcvFileConnection c <$> ExceptT (getRcvFileTransfer_ db userId entId)
+ where
+ getConnection_ :: DB.Connection -> ExceptT StoreError IO Connection
+ getConnection_ db = ExceptT $ do
+ connection
+ <$> DB.query
+ db
+ [sql|
+ SELECT connection_id, agent_conn_id, conn_level, via_contact,
+ conn_status, conn_type, contact_id, group_member_id, snd_file_id, rcv_file_id, created_at
+ FROM connections
+ WHERE user_id = ? AND agent_conn_id = ?
+ |]
+ (userId, agentConnId)
+ connection :: [ConnectionRow] -> Either StoreError Connection
+ connection (connRow : _) = Right $ toConnection connRow
+ connection _ = Left $ SEConnectionNotFound agentConnId
+ getContactRec_ :: DB.Connection -> Int64 -> Connection -> ExceptT StoreError IO Contact
+ getContactRec_ db contactId c = ExceptT $ do
+ toContact contactId c
+ <$> DB.query
+ db
+ [sql|
+ SELECT c.local_display_name, p.display_name, p.full_name, c.via_group
+ FROM contacts c
+ JOIN contact_profiles p ON c.contact_profile_id = p.contact_profile_id
+ WHERE c.user_id = ? AND c.contact_id = ?
+ |]
+ (userId, contactId)
+ toContact :: Int64 -> Connection -> [(ContactName, Text, Text, Maybe Int64)] -> Either StoreError Contact
+ toContact contactId activeConn [(localDisplayName, displayName, fullName, viaGroup)] =
+ let profile = Profile {displayName, fullName}
+ in Right $ Contact {contactId, localDisplayName, profile, activeConn, viaGroup}
+ toContact _ _ _ = Left $ SEInternal "referenced contact not found"
+ getGroupAndMember_ :: DB.Connection -> Int64 -> Connection -> ExceptT StoreError IO (GroupName, GroupMember)
+ getGroupAndMember_ db groupMemberId c = ExceptT $ do
+ toGroupAndMember c
+ <$> DB.query
+ db
+ [sql|
+ SELECT
+ g.local_display_name,
+ m.group_member_id, m.group_id, m.member_id, m.member_role, m.member_category, m.member_status,
+ m.invited_by, m.local_display_name, m.contact_id, p.display_name, p.full_name
+ FROM group_members m
+ JOIN contact_profiles p ON p.contact_profile_id = m.contact_profile_id
+ JOIN groups g ON g.group_id = m.group_id
+ WHERE m.group_member_id = ?
+ |]
+ (Only groupMemberId)
+ toGroupAndMember :: Connection -> [Only GroupName :. GroupMemberRow] -> Either StoreError (GroupName, GroupMember)
+ toGroupAndMember c [Only groupName :. memberRow] =
+ let member = toGroupMember userContactId memberRow
+ in Right (groupName, (member :: GroupMember) {activeConn = Just c})
+ toGroupAndMember _ _ = Left $ SEInternal "referenced group member not found"
+ getConnSndFileTransfer_ :: DB.Connection -> Int64 -> Connection -> ExceptT StoreError IO SndFileTransfer
+ getConnSndFileTransfer_ db fileId Connection {connId} =
+ ExceptT $
+ sndFileTransfer_ fileId connId
+ <$> DB.query
+ db
+ [sql|
+ SELECT s.file_status, f.file_name, f.file_size, f.chunk_size, f.file_path, cs.local_display_name, m.local_display_name
+ FROM snd_files s
+ JOIN files f USING (file_id)
+ LEFT JOIN contacts cs USING (contact_id)
+ LEFT JOIN group_members m USING (group_member_id)
+ WHERE f.user_id = ? AND f.file_id = ? AND s.connection_id = ?
+ |]
+ (userId, fileId, connId)
+ sndFileTransfer_ :: Int64 -> Int64 -> [(FileStatus, String, Integer, Integer, FilePath, Maybe ContactName, Maybe ContactName)] -> Either StoreError SndFileTransfer
+ sndFileTransfer_ fileId connId [(fileStatus, fileName, fileSize, chunkSize, filePath, contactName_, memberName_)] =
+ case contactName_ <|> memberName_ of
+ Just recipientDisplayName -> Right SndFileTransfer {..}
+ Nothing -> Left $ SESndFileInvalid fileId
+ sndFileTransfer_ fileId _ _ = Left $ SESndFileNotFound fileId
+
+updateConnectionStatus :: MonadUnliftIO m => SQLiteStore -> Connection -> ConnStatus -> m ()
+updateConnectionStatus st Connection {connId} connStatus =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "UPDATE connections SET conn_status = ? WHERE connection_id = ?" (connStatus, connId)
+
+-- | creates completely new group with a single member - the current user
+createNewGroup :: StoreMonad m => SQLiteStore -> TVar ChaChaDRG -> User -> GroupProfile -> m Group
+createNewGroup st gVar user groupProfile =
+ liftIOEither . checkConstraint SEDuplicateName . withTransaction st $ \db -> do
+ let GroupProfile {displayName, fullName} = groupProfile
+ uId = userId user
+ DB.execute db "INSERT INTO display_names (local_display_name, ldn_base, user_id) VALUES (?, ?, ?)" (displayName, displayName, uId)
+ DB.execute db "INSERT INTO group_profiles (display_name, full_name) VALUES (?, ?)" (displayName, fullName)
+ profileId <- insertedRowId db
+ DB.execute db "INSERT INTO groups (local_display_name, user_id, group_profile_id) VALUES (?, ?, ?)" (displayName, uId, profileId)
+ groupId <- insertedRowId db
+ memberId <- randomBytes gVar 12
+ membership <- createContactMember_ db user groupId user (memberId, GROwner) GCUserMember GSMemCreator IBUser
+ pure $ Right Group {groupId, localDisplayName = displayName, groupProfile, members = [], membership}
+
+-- | creates a new group record for the group the current user was invited to
+createGroupInvitation ::
+ StoreMonad m => SQLiteStore -> User -> Contact -> GroupInvitation -> m Group
+createGroupInvitation st user contact GroupInvitation {fromMember, invitedMember, queueInfo, groupProfile} =
+ liftIOEither . withTransaction st $ \db -> do
+ let GroupProfile {displayName, fullName} = groupProfile
+ uId = userId user
+ withLocalDisplayName db uId displayName $ \localDisplayName -> do
+ DB.execute db "INSERT INTO group_profiles (display_name, full_name) VALUES (?, ?)" (displayName, fullName)
+ profileId <- insertedRowId db
+ DB.execute db "INSERT INTO groups (group_profile_id, local_display_name, inv_queue_info, user_id) VALUES (?, ?, ?, ?)" (profileId, localDisplayName, queueInfo, uId)
+ groupId <- insertedRowId db
+ member <- createContactMember_ db user groupId contact fromMember GCHostMember GSMemInvited IBUnknown
+ membership <- createContactMember_ db user groupId user invitedMember GCUserMember GSMemInvited (IBContact $ contactId contact)
+ pure Group {groupId, localDisplayName, groupProfile, members = [member], membership}
+
+-- TODO return the last connection that is ready, not any last connection
+-- requires updating connection status
+getGroup :: StoreMonad m => SQLiteStore -> User -> GroupName -> m Group
+getGroup st user localDisplayName =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ fst <$> getGroup_ db user localDisplayName
+
+getGroup_ :: DB.Connection -> User -> GroupName -> ExceptT StoreError IO (Group, Maybe SMPQueueInfo)
+getGroup_ db User {userId, userContactId} localDisplayName = do
+ (g@Group {groupId}, qInfo) <- getGroupRec_
+ allMembers <- getMembers_ groupId
+ (members, membership) <- liftEither $ splitUserMember_ allMembers
+ pure (g {members, membership}, qInfo)
+ where
+ getGroupRec_ :: ExceptT StoreError IO (Group, Maybe SMPQueueInfo)
+ getGroupRec_ = ExceptT $ do
+ toGroup
+ <$> DB.query
+ db
+ [sql|
+ SELECT g.group_id, p.display_name, p.full_name, g.inv_queue_info
+ FROM groups g
+ JOIN group_profiles p ON p.group_profile_id = g.group_profile_id
+ WHERE g.local_display_name = ? AND g.user_id = ?
+ |]
+ (localDisplayName, userId)
+ toGroup :: [(Int64, GroupName, Text, Maybe SMPQueueInfo)] -> Either StoreError (Group, Maybe SMPQueueInfo)
+ toGroup [(groupId, displayName, fullName, qInfo)] =
+ let groupProfile = GroupProfile {displayName, fullName}
+ in Right (Group {groupId, localDisplayName, groupProfile, members = undefined, membership = undefined}, qInfo)
+ toGroup _ = Left $ SEGroupNotFound localDisplayName
+ getMembers_ :: Int64 -> ExceptT StoreError IO [GroupMember]
+ getMembers_ groupId = ExceptT $ do
+ Right . map toContactMember
+ <$> DB.query
+ db
+ [sql|
+ SELECT
+ m.group_member_id, m.group_id, m.member_id, m.member_role, m.member_category, m.member_status,
+ m.invited_by, m.local_display_name, m.contact_id, p.display_name, p.full_name,
+ c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact,
+ c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.created_at
+ FROM group_members m
+ JOIN contact_profiles p ON p.contact_profile_id = m.contact_profile_id
+ LEFT JOIN connections c ON c.connection_id = (
+ SELECT max(cc.connection_id)
+ FROM connections cc
+ where cc.group_member_id = m.group_member_id
+ )
+ WHERE m.group_id = ? AND m.user_id = ?
+ |]
+ (groupId, userId)
+ toContactMember :: (GroupMemberRow :. MaybeConnectionRow) -> GroupMember
+ toContactMember (memberRow :. connRow) =
+ (toGroupMember userContactId memberRow) {activeConn = toMaybeConnection connRow}
+ splitUserMember_ :: [GroupMember] -> Either StoreError ([GroupMember], GroupMember)
+ splitUserMember_ allMembers =
+ let (b, a) = break ((== Just userContactId) . memberContactId) allMembers
+ in case a of
+ [] -> Left SEGroupWithoutUser
+ u : ms -> Right (b <> ms, u)
+
+deleteGroup :: MonadUnliftIO m => SQLiteStore -> User -> Group -> m ()
+deleteGroup st User {userId} Group {groupId, members, localDisplayName} =
+ liftIO . withTransaction st $ \db -> do
+ forM_ members $ \m -> DB.execute db "DELETE FROM connections WHERE user_id = ? AND group_member_id = ?" (userId, groupMemberId m)
+ DB.execute db "DELETE FROM group_members WHERE user_id = ? AND group_id = ?" (userId, groupId)
+ DB.execute db "DELETE FROM groups WHERE user_id = ? AND group_id = ?" (userId, groupId)
+ DB.execute db "DELETE FROM display_names WHERE user_id = ? AND local_display_name = ?" (userId, localDisplayName)
+
+getUserGroups :: MonadUnliftIO m => SQLiteStore -> User -> m [Group]
+getUserGroups st user =
+ liftIO . withTransaction st $ \db -> do
+ groupNames <- liftIO $ map fromOnly <$> DB.query db "SELECT local_display_name FROM groups WHERE user_id = ?" (Only $ userId user)
+ map fst . rights <$> mapM (runExceptT . getGroup_ db user) groupNames
+
+getGroupInvitation :: StoreMonad m => SQLiteStore -> User -> GroupName -> m ReceivedGroupInvitation
+getGroupInvitation st user localDisplayName =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ do
+ (Group {membership, members, groupProfile}, qInfo) <- getGroup_ db user localDisplayName
+ when (memberStatus membership /= GSMemInvited) $ throwError SEGroupAlreadyJoined
+ case (qInfo, findFromContact (invitedBy membership) members) of
+ (Just queueInfo, Just fromMember) ->
+ pure ReceivedGroupInvitation {fromMember, userMember = membership, queueInfo, groupProfile}
+ _ -> throwError SEGroupInvitationNotFound
+ where
+ findFromContact :: InvitedBy -> [GroupMember] -> Maybe GroupMember
+ findFromContact (IBContact contactId) = find ((== Just contactId) . memberContactId)
+ findFromContact _ = const Nothing
+
+type GroupMemberRow = (Int64, Int64, ByteString, GroupMemberRole, GroupMemberCategory, GroupMemberStatus, Maybe Int64, ContactName, Maybe Int64, ContactName, Text)
+
+toGroupMember :: Int64 -> GroupMemberRow -> GroupMember
+toGroupMember userContactId (groupMemberId, groupId, memberId, memberRole, memberCategory, memberStatus, invitedById, localDisplayName, memberContactId, displayName, fullName) =
+ let memberProfile = Profile {displayName, fullName}
+ invitedBy = toInvitedBy userContactId invitedById
+ activeConn = Nothing
+ in GroupMember {..}
+
+createContactGroupMember :: StoreMonad m => SQLiteStore -> TVar ChaChaDRG -> User -> Int64 -> Contact -> GroupMemberRole -> ConnId -> m GroupMember
+createContactGroupMember st gVar user groupId contact memberRole agentConnId =
+ liftIOEither . withTransaction st $ \db ->
+ createWithRandomId gVar $ \memId -> do
+ member <- createContactMember_ db user groupId contact (memId, memberRole) GCInviteeMember GSMemInvited IBUser
+ groupMemberId <- insertedRowId db
+ void $ createMemberConnection_ db (userId user) groupMemberId agentConnId Nothing 0
+ pure member
+
+createMemberConnection :: MonadUnliftIO m => SQLiteStore -> UserId -> GroupMember -> ConnId -> m ()
+createMemberConnection st userId GroupMember {groupMemberId} agentConnId =
+ liftIO . withTransaction st $ \db ->
+ void $ createMemberConnection_ db userId groupMemberId agentConnId Nothing 0
+
+updateGroupMemberStatus :: MonadUnliftIO m => SQLiteStore -> UserId -> GroupMember -> GroupMemberStatus -> m ()
+updateGroupMemberStatus st userId GroupMember {groupMemberId} memStatus =
+ liftIO . withTransaction st $ \db ->
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_members
+ SET member_status = :member_status
+ WHERE user_id = :user_id AND group_member_id = :group_member_id
+ |]
+ [ ":user_id" := userId,
+ ":group_member_id" := groupMemberId,
+ ":member_status" := memStatus
+ ]
+
+-- | add new member with profile
+createNewGroupMember :: StoreMonad m => SQLiteStore -> User -> Group -> MemberInfo -> GroupMemberCategory -> GroupMemberStatus -> m GroupMember
+createNewGroupMember st user@User {userId} group memInfo@(MemberInfo _ _ Profile {displayName, fullName}) memCategory memStatus =
+ liftIOEither . withTransaction st $ \db ->
+ withLocalDisplayName db userId displayName $ \localDisplayName -> do
+ DB.execute db "INSERT INTO contact_profiles (display_name, full_name) VALUES (?, ?)" (displayName, fullName)
+ memProfileId <- insertedRowId db
+ let newMember =
+ NewGroupMember
+ { memInfo,
+ memCategory,
+ memStatus,
+ memInvitedBy = IBUnknown,
+ localDisplayName,
+ memContactId = Nothing,
+ memProfileId
+ }
+ createNewMember_ db user group newMember
+
+createNewMember_ :: DB.Connection -> User -> Group -> NewGroupMember -> IO GroupMember
+createNewMember_
+ db
+ User {userId, userContactId}
+ Group {groupId}
+ NewGroupMember
+ { memInfo = MemberInfo memberId memberRole memberProfile,
+ memCategory = memberCategory,
+ memStatus = memberStatus,
+ memInvitedBy = invitedBy,
+ localDisplayName,
+ memContactId = memberContactId,
+ memProfileId
+ } = do
+ let invitedById = fromInvitedBy userContactId invitedBy
+ activeConn = Nothing
+ DB.execute
+ db
+ [sql|
+ INSERT INTO group_members
+ (group_id, member_id, member_role, member_category, member_status,
+ invited_by, user_id, local_display_name, contact_profile_id, contact_id) VALUES (?,?,?,?,?,?,?,?,?,?)
+ |]
+ (groupId, memberId, memberRole, memberCategory, memberStatus, invitedById, userId, localDisplayName, memProfileId, memberContactId)
+ groupMemberId <- insertedRowId db
+ pure GroupMember {..}
+
+deleteGroupMemberConnection :: MonadUnliftIO m => SQLiteStore -> UserId -> GroupMember -> m ()
+deleteGroupMemberConnection st userId m =
+ liftIO . withTransaction st $ \db -> deleteGroupMemberConnection_ db userId m
+
+deleteGroupMemberConnection_ :: DB.Connection -> UserId -> GroupMember -> IO ()
+deleteGroupMemberConnection_ db userId GroupMember {groupMemberId} =
+ DB.execute db "DELETE FROM connections WHERE user_id = ? AND group_member_id = ?" (userId, groupMemberId)
+
+createIntroductions :: MonadUnliftIO m => SQLiteStore -> Group -> GroupMember -> m [GroupMemberIntro]
+createIntroductions st Group {members} toMember = do
+ let reMembers = filter (\m -> memberCurrent m && groupMemberId m /= groupMemberId toMember) members
+ if null reMembers
+ then pure []
+ else liftIO . withTransaction st $ \db ->
+ mapM (insertIntro_ db) reMembers
+ where
+ insertIntro_ :: DB.Connection -> GroupMember -> IO GroupMemberIntro
+ insertIntro_ db reMember = do
+ DB.execute
+ db
+ [sql|
+ INSERT INTO group_member_intros
+ (re_group_member_id, to_group_member_id, intro_status) VALUES (?,?,?)
+ |]
+ (groupMemberId reMember, groupMemberId toMember, GMIntroPending)
+ introId <- insertedRowId db
+ pure GroupMemberIntro {introId, reMember, toMember, introStatus = GMIntroPending, introInvitation = Nothing}
+
+updateIntroStatus :: MonadUnliftIO m => SQLiteStore -> GroupMemberIntro -> GroupMemberIntroStatus -> m ()
+updateIntroStatus st GroupMemberIntro {introId} introStatus' =
+ liftIO . withTransaction st $ \db ->
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_member_intros
+ SET intro_status = :intro_status
+ WHERE group_member_intro_id = :intro_id
+ |]
+ [":intro_status" := introStatus', ":intro_id" := introId]
+
+saveIntroInvitation :: StoreMonad m => SQLiteStore -> GroupMember -> GroupMember -> IntroInvitation -> m GroupMemberIntro
+saveIntroInvitation st reMember toMember introInv = do
+ liftIOEither . withTransaction st $ \db -> runExceptT $ do
+ intro <- getIntroduction_ db reMember toMember
+ liftIO $
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_member_intros
+ SET intro_status = :intro_status,
+ group_queue_info = :group_queue_info,
+ direct_queue_info = :direct_queue_info
+ WHERE group_member_intro_id = :intro_id
+ |]
+ [ ":intro_status" := GMIntroInvReceived,
+ ":group_queue_info" := groupQInfo introInv,
+ ":direct_queue_info" := directQInfo introInv,
+ ":intro_id" := introId intro
+ ]
+ pure intro {introInvitation = Just introInv, introStatus = GMIntroInvReceived}
+
+saveMemberInvitation :: StoreMonad m => SQLiteStore -> GroupMember -> IntroInvitation -> m ()
+saveMemberInvitation st GroupMember {groupMemberId} IntroInvitation {groupQInfo, directQInfo} =
+ liftIO . withTransaction st $ \db ->
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_members
+ SET member_status = :member_status,
+ group_queue_info = :group_queue_info,
+ direct_queue_info = :direct_queue_info
+ WHERE group_member_id = :group_member_id
+ |]
+ [ ":member_status" := GSMemIntroInvited,
+ ":group_queue_info" := groupQInfo,
+ ":direct_queue_info" := directQInfo,
+ ":group_member_id" := groupMemberId
+ ]
+
+getIntroduction_ :: DB.Connection -> GroupMember -> GroupMember -> ExceptT StoreError IO GroupMemberIntro
+getIntroduction_ db reMember toMember = ExceptT $ do
+ toIntro
+ <$> DB.query
+ db
+ [sql|
+ SELECT group_member_intro_id, group_queue_info, direct_queue_info, intro_status
+ FROM group_member_intros
+ WHERE re_group_member_id = ? AND to_group_member_id = ?
+ |]
+ (groupMemberId reMember, groupMemberId toMember)
+ where
+ toIntro :: [(Int64, Maybe SMPQueueInfo, Maybe SMPQueueInfo, GroupMemberIntroStatus)] -> Either StoreError GroupMemberIntro
+ toIntro [(introId, groupQInfo, directQInfo, introStatus)] =
+ let introInvitation = IntroInvitation <$> groupQInfo <*> directQInfo
+ in Right GroupMemberIntro {introId, reMember, toMember, introStatus, introInvitation}
+ toIntro _ = Left SEIntroNotFound
+
+createIntroReMember :: StoreMonad m => SQLiteStore -> User -> Group -> GroupMember -> MemberInfo -> ConnId -> ConnId -> m GroupMember
+createIntroReMember st user@User {userId} group@Group {groupId} _host@GroupMember {memberContactId, activeConn} memInfo@(MemberInfo _ _ memberProfile) groupAgentConnId directAgentConnId =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ do
+ let cLevel = 1 + maybe 0 (connLevel :: Connection -> Int) activeConn
+ Connection {connId = directConnId} <- liftIO $ createConnection_ db userId directAgentConnId memberContactId cLevel
+ (localDisplayName, contactId, memProfileId) <- ExceptT $ createContact_ db userId directConnId memberProfile (Just groupId)
+ liftIO $ do
+ let newMember =
+ NewGroupMember
+ { memInfo,
+ memCategory = GCPreMember,
+ memStatus = GSMemIntroduced,
+ memInvitedBy = IBUnknown,
+ localDisplayName,
+ memContactId = Just contactId,
+ memProfileId
+ }
+ member <- createNewMember_ db user group newMember
+ conn <- createMemberConnection_ db userId (groupMemberId member) groupAgentConnId memberContactId cLevel
+ pure (member :: GroupMember) {activeConn = Just conn}
+
+createIntroToMemberContact :: StoreMonad m => SQLiteStore -> UserId -> GroupMember -> GroupMember -> ConnId -> ConnId -> m ()
+createIntroToMemberContact st userId GroupMember {memberContactId = viaContactId, activeConn} _to@GroupMember {groupMemberId, localDisplayName} groupAgentConnId directAgentConnId =
+ liftIO . withTransaction st $ \db -> do
+ let cLevel = 1 + maybe 0 (connLevel :: Connection -> Int) activeConn
+ void $ createMemberConnection_ db userId groupMemberId groupAgentConnId viaContactId cLevel
+ Connection {connId = directConnId} <- createConnection_ db userId directAgentConnId viaContactId cLevel
+ contactId <- createMemberContact_ db directConnId
+ updateMember_ db contactId
+ where
+ createMemberContact_ :: DB.Connection -> Int64 -> IO Int64
+ createMemberContact_ db connId = do
+ DB.executeNamed
+ db
+ [sql|
+ INSERT INTO contacts (contact_profile_id, via_group, local_display_name, user_id)
+ SELECT contact_profile_id, group_id, :local_display_name, :user_id
+ FROM group_members
+ WHERE group_member_id = :group_member_id
+ |]
+ [ ":group_member_id" := groupMemberId,
+ ":local_display_name" := localDisplayName,
+ ":user_id" := userId
+ ]
+ contactId <- insertedRowId db
+ DB.execute db "UPDATE connections SET contact_id = ? WHERE connection_id = ?" (contactId, connId)
+ pure contactId
+ updateMember_ :: DB.Connection -> Int64 -> IO ()
+ updateMember_ db contactId =
+ DB.executeNamed
+ db
+ [sql|
+ UPDATE group_members
+ SET contact_id = :contact_id
+ WHERE group_member_id = :group_member_id
+ |]
+ [":contact_id" := contactId, ":group_member_id" := groupMemberId]
+
+createMemberConnection_ :: DB.Connection -> UserId -> Int64 -> ConnId -> Maybe Int64 -> Int -> IO Connection
+createMemberConnection_ db userId groupMemberId agentConnId viaContact connLevel = do
+ createdAt <- getCurrentTime
+ DB.execute
+ db
+ [sql|
+ INSERT INTO connections
+ (user_id, agent_conn_id, conn_status, conn_type, group_member_id, via_contact, conn_level, created_at) VALUES (?,?,?,?,?,?,?,?);
+ |]
+ (userId, agentConnId, ConnNew, ConnMember, groupMemberId, viaContact, connLevel, createdAt)
+ connId <- insertedRowId db
+ pure Connection {connId, agentConnId, connType = ConnMember, entityId = Just groupMemberId, viaContact, connLevel, connStatus = ConnNew, createdAt}
+
+createContactMember_ :: IsContact a => DB.Connection -> User -> Int64 -> a -> (MemberId, GroupMemberRole) -> GroupMemberCategory -> GroupMemberStatus -> InvitedBy -> IO GroupMember
+createContactMember_ db User {userId, userContactId} groupId userOrContact (memberId, memberRole) memberCategory memberStatus invitedBy = do
+ insertMember_
+ groupMemberId <- insertedRowId db
+ let memberProfile = profile' userOrContact
+ memberContactId = Just $ contactId' userOrContact
+ localDisplayName = localDisplayName' userOrContact
+ activeConn = Nothing
+ pure GroupMember {..}
+ where
+ insertMember_ =
+ DB.executeNamed
+ db
+ [sql|
+ INSERT INTO group_members
+ ( group_id, member_id, member_role, member_category, member_status, invited_by,
+ user_id, local_display_name, contact_profile_id, contact_id)
+ VALUES
+ (:group_id,:member_id,:member_role,:member_category,:member_status,:invited_by,
+ :user_id,:local_display_name,
+ (SELECT contact_profile_id FROM contacts WHERE contact_id = :contact_id),
+ :contact_id)
+ |]
+ [ ":group_id" := groupId,
+ ":member_id" := memberId,
+ ":member_role" := memberRole,
+ ":member_category" := memberCategory,
+ ":member_status" := memberStatus,
+ ":invited_by" := fromInvitedBy userContactId invitedBy,
+ ":user_id" := userId,
+ ":local_display_name" := localDisplayName' userOrContact,
+ ":contact_id" := contactId' userOrContact
+ ]
+
+getViaGroupMember :: MonadUnliftIO m => SQLiteStore -> User -> Contact -> m (Maybe (GroupName, GroupMember))
+getViaGroupMember st User {userId, userContactId} Contact {contactId} =
+ liftIO . withTransaction st $ \db ->
+ toGroupAndMember
+ <$> DB.query
+ db
+ [sql|
+ SELECT
+ g.local_display_name,
+ m.group_member_id, m.group_id, m.member_id, m.member_role, m.member_category, m.member_status,
+ m.invited_by, m.local_display_name, m.contact_id, p.display_name, p.full_name,
+ c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact,
+ c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.created_at
+ FROM group_members m
+ JOIN contacts ct ON ct.contact_id = m.contact_id
+ JOIN contact_profiles p ON p.contact_profile_id = m.contact_profile_id
+ JOIN groups g ON g.group_id = m.group_id AND g.group_id = ct.via_group
+ LEFT JOIN connections c ON c.connection_id = (
+ SELECT max(cc.connection_id)
+ FROM connections cc
+ where cc.group_member_id = m.group_member_id
+ )
+ WHERE ct.user_id = ? AND ct.contact_id = ?
+ |]
+ (userId, contactId)
+ where
+ toGroupAndMember :: [Only GroupName :. GroupMemberRow :. MaybeConnectionRow] -> Maybe (GroupName, GroupMember)
+ toGroupAndMember [Only groupName :. memberRow :. connRow] =
+ let member = toGroupMember userContactId memberRow
+ in Just (groupName, (member :: GroupMember) {activeConn = toMaybeConnection connRow})
+ toGroupAndMember _ = Nothing
+
+getViaGroupContact :: MonadUnliftIO m => SQLiteStore -> User -> GroupMember -> m (Maybe Contact)
+getViaGroupContact st User {userId} GroupMember {groupMemberId} =
+ liftIO . withTransaction st $ \db ->
+ toContact
+ <$> DB.query
+ db
+ [sql|
+ SELECT
+ ct.contact_id, ct.local_display_name, p.display_name, p.full_name, ct.via_group,
+ c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact,
+ c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.created_at
+ FROM contacts ct
+ JOIN contact_profiles p ON ct.contact_profile_id = p.contact_profile_id
+ JOIN connections c ON c.connection_id = (
+ SELECT max(cc.connection_id)
+ FROM connections cc
+ where cc.contact_id = ct.contact_id
+ )
+ JOIN groups g ON g.group_id = ct.via_group
+ JOIN group_members m ON m.group_id = g.group_id AND m.contact_id = ct.contact_id
+ WHERE ct.user_id = ? AND m.group_member_id = ?
+ |]
+ (userId, groupMemberId)
+ where
+ toContact :: [(Int64, ContactName, Text, Text, Maybe Int64) :. ConnectionRow] -> Maybe Contact
+ toContact [(contactId, localDisplayName, displayName, fullName, viaGroup) :. connRow] =
+ let profile = Profile {displayName, fullName}
+ activeConn = toConnection connRow
+ in Just Contact {contactId, localDisplayName, profile, activeConn, viaGroup}
+ toContact _ = Nothing
+
+createSndFileTransfer :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> FilePath -> FileInvitation -> ConnId -> Integer -> m SndFileTransfer
+createSndFileTransfer st userId Contact {contactId, localDisplayName = recipientDisplayName} filePath FileInvitation {fileName, fileSize} agentConnId chunkSize =
+ liftIO . withTransaction st $ \db -> do
+ DB.execute db "INSERT INTO files (user_id, contact_id, file_name, file_path, file_size, chunk_size) VALUES (?, ?, ?, ?, ?, ?)" (userId, contactId, fileName, filePath, fileSize, chunkSize)
+ fileId <- insertedRowId db
+ Connection {connId} <- createSndFileConnection_ db userId fileId agentConnId
+ let fileStatus = FSNew
+ DB.execute db "INSERT INTO snd_files (file_id, file_status, connection_id) VALUES (?, ?, ?)" (fileId, fileStatus, connId)
+ pure SndFileTransfer {..}
+
+createSndGroupFileTransfer :: MonadUnliftIO m => SQLiteStore -> UserId -> Group -> [(GroupMember, ConnId, FileInvitation)] -> FilePath -> Integer -> Integer -> m Int64
+createSndGroupFileTransfer st userId Group {groupId} ms filePath fileSize chunkSize =
+ liftIO . withTransaction st $ \db -> do
+ let fileName = takeFileName filePath
+ DB.execute db "INSERT INTO files (user_id, group_id, file_name, file_path, file_size, chunk_size) VALUES (?, ?, ?, ?, ?, ?)" (userId, groupId, fileName, filePath, fileSize, chunkSize)
+ fileId <- insertedRowId db
+ forM_ ms $ \(GroupMember {groupMemberId}, agentConnId, _) -> do
+ Connection {connId} <- createSndFileConnection_ db userId fileId agentConnId
+ DB.execute db "INSERT INTO snd_files (file_id, file_status, connection_id, group_member_id) VALUES (?, ?, ?, ?)" (fileId, FSNew, connId, groupMemberId)
+ pure fileId
+
+createSndFileConnection_ :: DB.Connection -> UserId -> Int64 -> ConnId -> IO Connection
+createSndFileConnection_ db userId fileId agentConnId = do
+ createdAt <- getCurrentTime
+ let connType = ConnSndFile
+ connStatus = ConnNew
+ DB.execute
+ db
+ [sql|
+ INSERT INTO connections
+ (user_id, snd_file_id, agent_conn_id, conn_status, conn_type, created_at) VALUES (?,?,?,?,?,?)
+ |]
+ (userId, fileId, agentConnId, connStatus, connType, createdAt)
+ connId <- insertedRowId db
+ pure Connection {connId, agentConnId, connType, entityId = Just fileId, viaContact = Nothing, connLevel = 0, connStatus, createdAt}
+
+updateSndFileStatus :: MonadUnliftIO m => SQLiteStore -> SndFileTransfer -> FileStatus -> m ()
+updateSndFileStatus st SndFileTransfer {fileId, connId} status =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "UPDATE snd_files SET file_status = ? WHERE file_id = ? AND connection_id = ?" (status, fileId, connId)
+
+createSndFileChunk :: MonadUnliftIO m => SQLiteStore -> SndFileTransfer -> m (Maybe Integer)
+createSndFileChunk st SndFileTransfer {fileId, connId, fileSize, chunkSize} =
+ liftIO . withTransaction st $ \db -> do
+ chunkNo <- getLastChunkNo db
+ insertChunk db chunkNo
+ pure chunkNo
+ where
+ getLastChunkNo db = do
+ ns <- DB.query db "SELECT chunk_number FROM snd_file_chunks WHERE file_id = ? AND connection_id = ? AND chunk_sent = 1 ORDER BY chunk_number DESC LIMIT 1" (fileId, connId)
+ pure $ case map fromOnly ns of
+ [] -> Just 1
+ n : _ -> if n * chunkSize >= fileSize then Nothing else Just (n + 1)
+ insertChunk db = \case
+ Just chunkNo -> DB.execute db "INSERT OR REPLACE INTO snd_file_chunks (file_id, connection_id, chunk_number) VALUES (?, ?, ?)" (fileId, connId, chunkNo)
+ Nothing -> pure ()
+
+updateSndFileChunkMsg :: MonadUnliftIO m => SQLiteStore -> SndFileTransfer -> Integer -> AgentMsgId -> m ()
+updateSndFileChunkMsg st SndFileTransfer {fileId, connId} chunkNo msgId =
+ liftIO . withTransaction st $ \db ->
+ DB.execute
+ db
+ [sql|
+ UPDATE snd_file_chunks
+ SET chunk_agent_msg_id = ?
+ WHERE file_id = ? AND connection_id = ? AND chunk_number = ?
+ |]
+ (msgId, fileId, connId, chunkNo)
+
+updateSndFileChunkSent :: MonadUnliftIO m => SQLiteStore -> SndFileTransfer -> AgentMsgId -> m ()
+updateSndFileChunkSent st SndFileTransfer {fileId, connId} msgId =
+ liftIO . withTransaction st $ \db ->
+ DB.execute
+ db
+ [sql|
+ UPDATE snd_file_chunks
+ SET chunk_sent = 1
+ WHERE file_id = ? AND connection_id = ? AND chunk_agent_msg_id = ?
+ |]
+ (fileId, connId, msgId)
+
+deleteSndFileChunks :: MonadUnliftIO m => SQLiteStore -> SndFileTransfer -> m ()
+deleteSndFileChunks st SndFileTransfer {fileId, connId} =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "DELETE FROM snd_file_chunks WHERE file_id = ? AND connection_id = ?" (fileId, connId)
+
+createRcvFileTransfer :: MonadUnliftIO m => SQLiteStore -> UserId -> Contact -> FileInvitation -> Integer -> m RcvFileTransfer
+createRcvFileTransfer st userId Contact {contactId, localDisplayName = c} f@FileInvitation {fileName, fileSize, fileQInfo} chunkSize =
+ liftIO . withTransaction st $ \db -> do
+ DB.execute db "INSERT INTO files (user_id, contact_id, file_name, file_size, chunk_size) VALUES (?, ?, ?, ?, ?)" (userId, contactId, fileName, fileSize, chunkSize)
+ fileId <- insertedRowId db
+ DB.execute db "INSERT INTO rcv_files (file_id, file_status, file_queue_info) VALUES (?, ?, ?)" (fileId, FSNew, fileQInfo)
+ pure RcvFileTransfer {fileId, fileInvitation = f, fileStatus = RFSNew, senderDisplayName = c, chunkSize}
+
+createRcvGroupFileTransfer :: MonadUnliftIO m => SQLiteStore -> UserId -> GroupMember -> FileInvitation -> Integer -> m RcvFileTransfer
+createRcvGroupFileTransfer st userId GroupMember {groupId, groupMemberId, localDisplayName = c} f@FileInvitation {fileName, fileSize, fileQInfo} chunkSize =
+ liftIO . withTransaction st $ \db -> do
+ DB.execute db "INSERT INTO files (user_id, group_id, file_name, file_size, chunk_size) VALUES (?, ?, ?, ?, ?)" (userId, groupId, fileName, fileSize, chunkSize)
+ fileId <- insertedRowId db
+ DB.execute db "INSERT INTO rcv_files (file_id, file_status, file_queue_info, group_member_id) VALUES (?, ?, ?, ?)" (fileId, FSNew, fileQInfo, groupMemberId)
+ pure RcvFileTransfer {fileId, fileInvitation = f, fileStatus = RFSNew, senderDisplayName = c, chunkSize}
+
+getRcvFileTransfer :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> m RcvFileTransfer
+getRcvFileTransfer st userId fileId =
+ liftIOEither . withTransaction st $ \db ->
+ getRcvFileTransfer_ db userId fileId
+
+getRcvFileTransfer_ :: DB.Connection -> UserId -> Int64 -> IO (Either StoreError RcvFileTransfer)
+getRcvFileTransfer_ db userId fileId =
+ rcvFileTransfer
+ <$> DB.query
+ db
+ [sql|
+ SELECT r.file_status, r.file_queue_info, f.file_name,
+ f.file_size, f.chunk_size, cs.local_display_name, m.local_display_name,
+ f.file_path, c.connection_id, c.agent_conn_id
+ FROM rcv_files r
+ JOIN files f USING (file_id)
+ LEFT JOIN connections c ON r.file_id = c.rcv_file_id
+ LEFT JOIN contacts cs USING (contact_id)
+ LEFT JOIN group_members m USING (group_member_id)
+ WHERE f.user_id = ? AND f.file_id = ?
+ |]
+ (userId, fileId)
+ where
+ rcvFileTransfer ::
+ [(FileStatus, SMPQueueInfo, String, Integer, Integer, Maybe ContactName, Maybe ContactName, Maybe FilePath, Maybe Int64, Maybe ConnId)] ->
+ Either StoreError RcvFileTransfer
+ rcvFileTransfer [(fileStatus', fileQInfo, fileName, fileSize, chunkSize, contactName_, memberName_, filePath_, connId_, agentConnId_)] =
+ let fileInv = FileInvitation {fileName, fileSize, fileQInfo}
+ fileInfo = (filePath_, connId_, agentConnId_)
+ in case contactName_ <|> memberName_ of
+ Nothing -> Left $ SERcvFileInvalid fileId
+ Just name ->
+ case fileStatus' of
+ FSNew -> Right RcvFileTransfer {fileId, fileInvitation = fileInv, fileStatus = RFSNew, senderDisplayName = name, chunkSize}
+ FSAccepted -> ft name fileInv RFSAccepted fileInfo
+ FSConnected -> ft name fileInv RFSConnected fileInfo
+ FSComplete -> ft name fileInv RFSComplete fileInfo
+ FSCancelled -> ft name fileInv RFSCancelled fileInfo
+ where
+ ft senderDisplayName fileInvitation rfs = \case
+ (Just filePath, Just connId, Just agentConnId) ->
+ let fileStatus = rfs RcvFileInfo {filePath, connId, agentConnId}
+ in Right RcvFileTransfer {..}
+ _ -> Left $ SERcvFileInvalid fileId
+ rcvFileTransfer _ = Left $ SERcvFileNotFound fileId
+
+acceptRcvFileTransfer :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> ConnId -> FilePath -> m ()
+acceptRcvFileTransfer st userId fileId agentConnId filePath =
+ liftIO . withTransaction st $ \db -> do
+ DB.execute db "UPDATE files SET file_path = ? WHERE user_id = ? AND file_id = ?" (filePath, userId, fileId)
+ DB.execute db "UPDATE rcv_files SET file_status = ? WHERE file_id = ?" (FSAccepted, fileId)
+
+ DB.execute db "INSERT INTO connections (agent_conn_id, conn_status, conn_type, rcv_file_id, user_id) VALUES (?, ?, ?, ?, ?)" (agentConnId, ConnJoined, ConnRcvFile, fileId, userId)
+
+updateRcvFileStatus :: MonadUnliftIO m => SQLiteStore -> RcvFileTransfer -> FileStatus -> m ()
+updateRcvFileStatus st RcvFileTransfer {fileId} status =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "UPDATE rcv_files SET file_status = ? WHERE file_id = ?" (status, fileId)
+
+createRcvFileChunk :: MonadUnliftIO m => SQLiteStore -> RcvFileTransfer -> Integer -> AgentMsgId -> m RcvChunkStatus
+createRcvFileChunk st RcvFileTransfer {fileId, fileInvitation = FileInvitation {fileSize}, chunkSize} chunkNo msgId =
+ liftIO . withTransaction st $ \db -> do
+ status <- getLastChunkNo db
+ unless (status == RcvChunkError) $
+ DB.execute db "INSERT OR REPLACE INTO rcv_file_chunks (file_id, chunk_number, chunk_agent_msg_id) VALUES (?, ?, ?)" (fileId, chunkNo, msgId)
+ pure status
+ where
+ getLastChunkNo db = do
+ ns <- DB.query db "SELECT chunk_number FROM rcv_file_chunks WHERE file_id = ? ORDER BY chunk_number DESC LIMIT 1" (Only fileId)
+ pure $ case map fromOnly ns of
+ []
+ | chunkNo == 1 ->
+ if chunkSize >= fileSize
+ then RcvChunkFinal
+ else RcvChunkOk
+ | otherwise -> RcvChunkError
+ n : _
+ | chunkNo == n -> RcvChunkDuplicate
+ | chunkNo == n + 1 ->
+ let prevSize = n * chunkSize
+ in if prevSize >= fileSize
+ then RcvChunkError
+ else
+ if prevSize + chunkSize >= fileSize
+ then RcvChunkFinal
+ else RcvChunkOk
+ | otherwise -> RcvChunkError
+
+updatedRcvFileChunkStored :: MonadUnliftIO m => SQLiteStore -> RcvFileTransfer -> Integer -> m ()
+updatedRcvFileChunkStored st RcvFileTransfer {fileId} chunkNo =
+ liftIO . withTransaction st $ \db ->
+ DB.execute
+ db
+ [sql|
+ UPDATE rcv_file_chunks
+ SET chunk_stored = 1
+ WHERE file_id = ? AND chunk_number = ?
+ |]
+ (fileId, chunkNo)
+
+deleteRcvFileChunks :: MonadUnliftIO m => SQLiteStore -> RcvFileTransfer -> m ()
+deleteRcvFileChunks st RcvFileTransfer {fileId} =
+ liftIO . withTransaction st $ \db ->
+ DB.execute db "DELETE FROM rcv_file_chunks WHERE file_id = ?" (Only fileId)
+
+getFileTransfer :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> m FileTransfer
+getFileTransfer st userId fileId =
+ liftIOEither . withTransaction st $ \db ->
+ getFileTransfer_ db userId fileId
+
+getFileTransferProgress :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> m (FileTransfer, [Integer])
+getFileTransferProgress st userId fileId =
+ liftIOEither . withTransaction st $ \db -> runExceptT $ do
+ ft <- ExceptT $ getFileTransfer_ db userId fileId
+ liftIO $
+ (ft,) . map fromOnly <$> case ft of
+ FTSnd _ -> DB.query db "SELECT COUNT(*) FROM snd_file_chunks WHERE file_id = ? and chunk_sent = 1 GROUP BY connection_id" (Only fileId)
+ FTRcv _ -> DB.query db "SELECT COUNT(*) FROM rcv_file_chunks WHERE file_id = ? AND chunk_stored = 1" (Only fileId)
+
+getFileTransfer_ :: DB.Connection -> UserId -> Int64 -> IO (Either StoreError FileTransfer)
+getFileTransfer_ db userId fileId =
+ fileTransfer
+ =<< DB.query
+ db
+ [sql|
+ SELECT s.file_id, r.file_id
+ FROM files f
+ LEFT JOIN snd_files s ON s.file_id = f.file_id
+ LEFT JOIN rcv_files r ON r.file_id = f.file_id
+ WHERE user_id = ? AND f.file_id = ?
+ |]
+ (userId, fileId)
+ where
+ fileTransfer :: [(Maybe Int64, Maybe Int64)] -> IO (Either StoreError FileTransfer)
+ fileTransfer ((Just _, Nothing) : _) = FTSnd <$$> getSndFileTransfers_ db userId fileId
+ fileTransfer [(Nothing, Just _)] = FTRcv <$$> getRcvFileTransfer_ db userId fileId
+ fileTransfer _ = pure . Left $ SEFileNotFound fileId
+
+getSndFileTransfers_ :: DB.Connection -> UserId -> Int64 -> IO (Either StoreError [SndFileTransfer])
+getSndFileTransfers_ db userId fileId =
+ sndFileTransfers
+ <$> DB.query
+ db
+ [sql|
+ SELECT s.file_status, f.file_name, f.file_size, f.chunk_size, f.file_path, s.connection_id, c.agent_conn_id,
+ cs.local_display_name, m.local_display_name
+ FROM snd_files s
+ JOIN files f USING (file_id)
+ JOIN connections c USING (connection_id)
+ LEFT JOIN contacts cs USING (contact_id)
+ LEFT JOIN group_members m USING (group_member_id)
+ WHERE f.user_id = ? AND f.file_id = ?
+ |]
+ (userId, fileId)
+ where
+ sndFileTransfers :: [(FileStatus, String, Integer, Integer, FilePath, Int64, ConnId, Maybe ContactName, Maybe ContactName)] -> Either StoreError [SndFileTransfer]
+ sndFileTransfers [] = Left $ SESndFileNotFound fileId
+ sndFileTransfers fts = mapM sndFileTransfer fts
+ sndFileTransfer (fileStatus, fileName, fileSize, chunkSize, filePath, connId, agentConnId, contactName_, memberName_) =
+ case contactName_ <|> memberName_ of
+ Just recipientDisplayName -> Right SndFileTransfer {..}
+ Nothing -> Left $ SESndFileInvalid fileId
+
+-- | Saves unique local display name based on passed displayName, suffixed with _N if required.
+-- This function should be called inside transaction.
+withLocalDisplayName :: forall a. DB.Connection -> UserId -> Text -> (Text -> IO a) -> IO (Either StoreError a)
+withLocalDisplayName db userId displayName action = getLdnSuffix >>= (`tryCreateName` 20)
+ where
+ getLdnSuffix :: IO Int
+ getLdnSuffix =
+ maybe 0 ((+ 1) . fromOnly) . listToMaybe
+ <$> DB.queryNamed
+ db
+ [sql|
+ SELECT ldn_suffix FROM display_names
+ WHERE user_id = :user_id AND ldn_base = :display_name
+ ORDER BY ldn_suffix DESC
+ LIMIT 1
+ |]
+ [":user_id" := userId, ":display_name" := displayName]
+ tryCreateName :: Int -> Int -> IO (Either StoreError a)
+ tryCreateName _ 0 = pure $ Left SEDuplicateName
+ tryCreateName ldnSuffix attempts = do
+ let ldn = displayName <> (if ldnSuffix == 0 then "" else T.pack $ '_' : show ldnSuffix)
+ E.try (insertName ldn) >>= \case
+ Right () -> Right <$> action ldn
+ Left e
+ | DB.sqlError e == DB.ErrorConstraint -> tryCreateName (ldnSuffix + 1) (attempts - 1)
+ | otherwise -> E.throwIO e
+ where
+ insertName ldn =
+ DB.execute
+ db
+ [sql|
+ INSERT INTO display_names
+ (local_display_name, ldn_base, ldn_suffix, user_id) VALUES (?, ?, ?, ?)
+ |]
+ (ldn, displayName, ldnSuffix, userId)
+
+createWithRandomId :: forall a. TVar ChaChaDRG -> (ByteString -> IO a) -> IO (Either StoreError a)
+createWithRandomId = createWithRandomBytes 12
+
+createWithRandomBytes :: forall a. Int -> TVar ChaChaDRG -> (ByteString -> IO a) -> IO (Either StoreError a)
+createWithRandomBytes size gVar create = tryCreate 3
+ where
+ tryCreate :: Int -> IO (Either StoreError a)
+ tryCreate 0 = pure $ Left SEUniqueID
+ tryCreate n = do
+ id' <- randomBytes gVar size
+ E.try (create id') >>= \case
+ Right x -> pure $ Right x
+ Left e
+ | DB.sqlError e == DB.ErrorConstraint -> tryCreate (n - 1)
+ | otherwise -> pure . Left . SEInternal $ bshow e
+
+randomBytes :: TVar ChaChaDRG -> Int -> IO ByteString
+randomBytes gVar n = B64.encode <$> (atomically . stateTVar gVar $ randomBytesGenerate n)
+
+data StoreError
+ = SEDuplicateName
+ | SEContactNotFound ContactName
+ | SEContactNotReady ContactName
+ | SEGroupNotFound GroupName
+ | SEGroupWithoutUser
+ | SEDuplicateGroupMember
+ | SEGroupAlreadyJoined
+ | SEGroupInvitationNotFound
+ | SESndFileNotFound Int64
+ | SESndFileInvalid Int64
+ | SERcvFileNotFound Int64
+ | SEFileNotFound Int64
+ | SERcvFileInvalid Int64
+ | SEConnectionNotFound ConnId
+ | SEIntroNotFound
+ | SEUniqueID
+ | SEInternal ByteString
+ deriving (Show, Exception)
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Styled.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Styled.hs
new file mode 100644
index 0000000000..f7a3a80acf
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Styled.hs
@@ -0,0 +1,74 @@
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE LambdaCase #-}
+
+module Simplex.Chat.Styled
+ ( StyledString (..),
+ StyledFormat (..),
+ styleMarkdown,
+ styleMarkdownText,
+ sLength,
+ sShow,
+ )
+where
+
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as B
+import Data.String
+import Data.Text (Text)
+import qualified Data.Text as T
+import Simplex.Chat.Markdown
+import System.Console.ANSI.Types
+
+data StyledString = Styled [SGR] String | StyledString :<>: StyledString
+
+instance Semigroup StyledString where (<>) = (:<>:)
+
+instance Monoid StyledString where mempty = plain ""
+
+instance IsString StyledString where fromString = plain
+
+styleMarkdownText :: Text -> StyledString
+styleMarkdownText = styleMarkdown . parseMarkdown
+
+styleMarkdown :: Markdown -> StyledString
+styleMarkdown (s1 :|: s2) = styleMarkdown s1 <> styleMarkdown s2
+styleMarkdown (Markdown Snippet s) = '`' `wrap` styled Snippet s
+styleMarkdown (Markdown Secret s) = '#' `wrap` styled Secret s
+styleMarkdown (Markdown f s) = styled f s
+
+wrap :: Char -> StyledString -> StyledString
+wrap c s = plain [c] <> s <> plain [c]
+
+class StyledFormat a where
+ styled :: Format -> a -> StyledString
+ plain :: a -> StyledString
+
+instance StyledFormat String where
+ styled = Styled . sgr
+ plain = Styled []
+
+instance StyledFormat ByteString where
+ styled f = styled f . B.unpack
+ plain = Styled [] . B.unpack
+
+instance StyledFormat Text where
+ styled f = styled f . T.unpack
+ plain = Styled [] . T.unpack
+
+sShow :: Show a => a -> StyledString
+sShow = plain . show
+
+sgr :: Format -> [SGR]
+sgr = \case
+ Bold -> [SetConsoleIntensity BoldIntensity]
+ Italic -> [SetUnderlining SingleUnderline, SetItalicized True]
+ Underline -> [SetUnderlining SingleUnderline]
+ StrikeThrough -> [SetSwapForegroundBackground True]
+ Colored c -> [SetColor Foreground Vivid c]
+ Secret -> [SetColor Foreground Dull Black, SetColor Background Dull Black]
+ Snippet -> []
+ NoFormat -> []
+
+sLength :: StyledString -> Int
+sLength (Styled _ s) = length s
+sLength (s1 :<>: s2) = sLength s1 + sLength s2
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Terminal.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Terminal.hs
new file mode 100644
index 0000000000..24d613f486
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Terminal.hs
@@ -0,0 +1,176 @@
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+
+module Simplex.Chat.Terminal where
+
+import Control.Monad.Catch (MonadMask)
+import Control.Monad.IO.Class (MonadIO)
+import Simplex.Chat.Styled
+import Simplex.Chat.Types
+import System.Console.ANSI.Types
+import System.Terminal
+import System.Terminal.Internal (LocalTerminal, Terminal, VirtualTerminal)
+import UnliftIO.STM
+
+data ActiveTo = ActiveNone | ActiveC ContactName | ActiveG GroupName
+ deriving (Eq)
+
+data ChatTerminal = ChatTerminal
+ { activeTo :: TVar ActiveTo,
+ termDevice :: TerminalDevice,
+ termState :: TVar TerminalState,
+ termSize :: Size,
+ nextMessageRow :: TVar Int,
+ termLock :: TMVar ()
+ }
+
+data TerminalState = TerminalState
+ { inputPrompt :: String,
+ inputString :: String,
+ inputPosition :: Int,
+ previousInput :: String
+ }
+
+class Terminal t => WithTerminal t where
+ withTerm :: (MonadIO m, MonadMask m) => t -> (t -> m a) -> m a
+
+data TerminalDevice = forall t. WithTerminal t => TerminalDevice t
+
+instance WithTerminal LocalTerminal where
+ withTerm _ = withTerminal
+
+instance WithTerminal VirtualTerminal where
+ withTerm t = ($ t)
+
+withChatTerm :: (MonadIO m, MonadMask m) => ChatTerminal -> (forall t. WithTerminal t => TerminalT t m a) -> m a
+withChatTerm ChatTerminal {termDevice = TerminalDevice t} action = withTerm t $ runTerminalT action
+
+newChatTerminal :: WithTerminal t => t -> IO ChatTerminal
+newChatTerminal t = do
+ activeTo <- newTVarIO ActiveNone
+ termSize <- withTerm t . runTerminalT $ getWindowSize
+ let lastRow = height termSize - 1
+ termState <- newTVarIO newTermState
+ termLock <- newTMVarIO ()
+ nextMessageRow <- newTVarIO lastRow
+ -- threadDelay 500000 -- this delay is the same as timeout in getTerminalSize
+ return ChatTerminal {activeTo, termDevice = TerminalDevice t, termState, termSize, nextMessageRow, termLock}
+
+newTermState :: TerminalState
+newTermState =
+ TerminalState
+ { inputString = "",
+ inputPosition = 0,
+ inputPrompt = "> ",
+ previousInput = ""
+ }
+
+withTermLock :: MonadTerminal m => ChatTerminal -> m () -> m ()
+withTermLock ChatTerminal {termLock} action = do
+ _ <- atomically $ takeTMVar termLock
+ action
+ atomically $ putTMVar termLock ()
+
+printToTerminal :: ChatTerminal -> [StyledString] -> IO ()
+printToTerminal ct s =
+ withChatTerm ct $
+ withTermLock ct $ do
+ printMessage ct s
+ updateInput ct
+
+updateInput :: forall m. MonadTerminal m => ChatTerminal -> m ()
+updateInput ChatTerminal {termSize = Size {height, width}, termState, nextMessageRow} = do
+ hideCursor
+ ts <- readTVarIO termState
+ nmr <- readTVarIO nextMessageRow
+ let ih = inputHeight ts
+ iStart = height - ih
+ prompt = inputPrompt ts
+ Position {row, col} = positionRowColumn width $ length prompt + inputPosition ts
+ if nmr >= iStart
+ then atomically $ writeTVar nextMessageRow iStart
+ else clearLines nmr iStart
+ setCursorPosition $ Position {row = max nmr iStart, col = 0}
+ putString $ prompt <> inputString ts <> " "
+ eraseInLine EraseForward
+ setCursorPosition $ Position {row = iStart + row, col}
+ showCursor
+ flush
+ where
+ clearLines :: Int -> Int -> m ()
+ clearLines from till
+ | from >= till = return ()
+ | otherwise = do
+ setCursorPosition $ Position {row = from, col = 0}
+ eraseInLine EraseForward
+ clearLines (from + 1) till
+ inputHeight :: TerminalState -> Int
+ inputHeight ts = length (inputPrompt ts <> inputString ts) `div` width + 1
+ positionRowColumn :: Int -> Int -> Position
+ positionRowColumn wid pos =
+ let row = pos `div` wid
+ col = pos - row * wid
+ in Position {row, col}
+
+printMessage :: forall m. MonadTerminal m => ChatTerminal -> [StyledString] -> m ()
+printMessage ChatTerminal {termSize = Size {height, width}, nextMessageRow} msg = do
+ nmr <- readTVarIO nextMessageRow
+ setCursorPosition $ Position {row = nmr, col = 0}
+ mapM_ printStyled msg
+ flush
+ let lc = sum $ map lineCount msg
+ atomically . writeTVar nextMessageRow $ min (height - 1) (nmr + lc)
+ where
+ lineCount :: StyledString -> Int
+ lineCount s = sLength s `div` width + 1
+ printStyled :: StyledString -> m ()
+ printStyled s = do
+ putStyled s
+ eraseInLine EraseForward
+ putLn
+
+-- Currently it is assumed that the message does not have internal line breaks.
+-- Previous implementation "kind of" supported them,
+-- but it was not determining the number of printed lines correctly
+-- because of accounting for control sequences in length
+putStyled :: MonadTerminal m => StyledString -> m ()
+putStyled (s1 :<>: s2) = putStyled s1 >> putStyled s2
+putStyled (Styled [] s) = putString s
+putStyled (Styled sgr s) = setSGR sgr >> putString s >> resetAttributes
+
+setSGR :: MonadTerminal m => [SGR] -> m ()
+setSGR = mapM_ $ \case
+ Reset -> resetAttributes
+ SetConsoleIntensity BoldIntensity -> setAttribute bold
+ SetConsoleIntensity _ -> resetAttribute bold
+ SetItalicized True -> setAttribute italic
+ SetItalicized _ -> resetAttribute italic
+ SetUnderlining NoUnderline -> resetAttribute underlined
+ SetUnderlining _ -> setAttribute underlined
+ SetSwapForegroundBackground True -> setAttribute inverted
+ SetSwapForegroundBackground _ -> resetAttribute inverted
+ SetColor l i c -> setAttribute . layer l . intensity i $ color c
+ SetBlinkSpeed _ -> pure ()
+ SetVisible _ -> pure ()
+ SetRGBColor _ _ -> pure ()
+ SetPaletteColor _ _ -> pure ()
+ SetDefaultColor _ -> pure ()
+ where
+ layer = \case
+ Foreground -> foreground
+ Background -> background
+ intensity = \case
+ Dull -> id
+ Vivid -> bright
+ color = \case
+ Black -> black
+ Red -> red
+ Green -> green
+ Yellow -> yellow
+ Blue -> blue
+ Magenta -> magenta
+ Cyan -> cyan
+ White -> white
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Types.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Types.hs
new file mode 100644
index 0000000000..ffd656a1ff
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Types.hs
@@ -0,0 +1,497 @@
+{-# LANGUAGE DeriveGeneric #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.Types where
+
+import Data.Aeson (FromJSON, ToJSON)
+import qualified Data.Aeson as J
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as B
+import Data.Int (Int64)
+import Data.Text (Text)
+import Data.Time.Clock (UTCTime)
+import Data.Typeable (Typeable)
+import Database.SQLite.Simple (ResultError (..), SQLData (..))
+import Database.SQLite.Simple.FromField (FieldParser, FromField (..), returnError)
+import Database.SQLite.Simple.Internal (Field (..))
+import Database.SQLite.Simple.Ok (Ok (Ok))
+import Database.SQLite.Simple.ToField (ToField (..))
+import GHC.Generics
+import Simplex.Messaging.Agent.Protocol (ConnId, SMPQueueInfo)
+import Simplex.Messaging.Agent.Store.SQLite (fromTextField_)
+
+class IsContact a where
+ contactId' :: a -> Int64
+ profile' :: a -> Profile
+ localDisplayName' :: a -> ContactName
+
+instance IsContact User where
+ contactId' = userContactId
+ profile' = profile
+ localDisplayName' = localDisplayName
+
+instance IsContact Contact where
+ contactId' = contactId
+ profile' = profile
+ localDisplayName' = localDisplayName
+
+data User = User
+ { userId :: UserId,
+ userContactId :: Int64,
+ localDisplayName :: ContactName,
+ profile :: Profile,
+ activeUser :: Bool
+ }
+
+type UserId = Int64
+
+data Contact = Contact
+ { contactId :: Int64,
+ localDisplayName :: ContactName,
+ profile :: Profile,
+ activeConn :: Connection,
+ viaGroup :: Maybe Int64
+ }
+ deriving (Eq, Show)
+
+contactConnId :: Contact -> ConnId
+contactConnId Contact {activeConn = Connection {agentConnId}} = agentConnId
+
+type ContactName = Text
+
+type GroupName = Text
+
+data Group = Group
+ { groupId :: Int64,
+ localDisplayName :: GroupName,
+ groupProfile :: GroupProfile,
+ members :: [GroupMember],
+ membership :: GroupMember
+ }
+ deriving (Eq, Show)
+
+data Profile = Profile
+ { displayName :: ContactName,
+ fullName :: Text
+ }
+ deriving (Generic, Eq, Show)
+
+instance ToJSON Profile where toEncoding = J.genericToEncoding J.defaultOptions
+
+instance FromJSON Profile
+
+data GroupProfile = GroupProfile
+ { displayName :: GroupName,
+ fullName :: Text
+ }
+ deriving (Generic, Eq, Show)
+
+instance ToJSON GroupProfile where toEncoding = J.genericToEncoding J.defaultOptions
+
+instance FromJSON GroupProfile
+
+data GroupInvitation = GroupInvitation
+ { fromMember :: (MemberId, GroupMemberRole),
+ invitedMember :: (MemberId, GroupMemberRole),
+ queueInfo :: SMPQueueInfo,
+ groupProfile :: GroupProfile
+ }
+ deriving (Eq, Show)
+
+data IntroInvitation = IntroInvitation
+ { groupQInfo :: SMPQueueInfo,
+ directQInfo :: SMPQueueInfo
+ }
+ deriving (Eq, Show)
+
+data MemberInfo = MemberInfo MemberId GroupMemberRole Profile
+ deriving (Eq, Show)
+
+memberInfo :: GroupMember -> MemberInfo
+memberInfo m = MemberInfo (memberId m) (memberRole m) (memberProfile m)
+
+data ReceivedGroupInvitation = ReceivedGroupInvitation
+ { fromMember :: GroupMember,
+ userMember :: GroupMember,
+ queueInfo :: SMPQueueInfo,
+ groupProfile :: GroupProfile
+ }
+ deriving (Eq, Show)
+
+data GroupMember = GroupMember
+ { groupMemberId :: Int64,
+ groupId :: Int64,
+ memberId :: MemberId,
+ memberRole :: GroupMemberRole,
+ memberCategory :: GroupMemberCategory,
+ memberStatus :: GroupMemberStatus,
+ invitedBy :: InvitedBy,
+ localDisplayName :: ContactName,
+ memberProfile :: Profile,
+ memberContactId :: Maybe Int64,
+ activeConn :: Maybe Connection
+ }
+ deriving (Eq, Show)
+
+memberConnId :: GroupMember -> Maybe ConnId
+memberConnId GroupMember {activeConn} = case activeConn of
+ Just Connection {agentConnId} -> Just agentConnId
+ Nothing -> Nothing
+
+data NewGroupMember = NewGroupMember
+ { memInfo :: MemberInfo,
+ memCategory :: GroupMemberCategory,
+ memStatus :: GroupMemberStatus,
+ memInvitedBy :: InvitedBy,
+ localDisplayName :: ContactName,
+ memProfileId :: Int64,
+ memContactId :: Maybe Int64
+ }
+
+type MemberId = ByteString
+
+data InvitedBy = IBContact Int64 | IBUser | IBUnknown
+ deriving (Eq, Show)
+
+toInvitedBy :: Int64 -> Maybe Int64 -> InvitedBy
+toInvitedBy userCtId (Just ctId)
+ | userCtId == ctId = IBUser
+ | otherwise = IBContact ctId
+toInvitedBy _ Nothing = IBUnknown
+
+fromInvitedBy :: Int64 -> InvitedBy -> Maybe Int64
+fromInvitedBy userCtId = \case
+ IBUnknown -> Nothing
+ IBContact ctId -> Just ctId
+ IBUser -> Just userCtId
+
+data GroupMemberRole = GRMember | GRAdmin | GROwner
+ deriving (Eq, Show, Ord)
+
+instance FromField GroupMemberRole where fromField = fromBlobField_ toMemberRole
+
+instance ToField GroupMemberRole where toField = toField . serializeMemberRole
+
+toMemberRole :: ByteString -> Either String GroupMemberRole
+toMemberRole = \case
+ "owner" -> Right GROwner
+ "admin" -> Right GRAdmin
+ "member" -> Right GRMember
+ r -> Left $ "invalid group member role " <> B.unpack r
+
+serializeMemberRole :: GroupMemberRole -> ByteString
+serializeMemberRole = \case
+ GROwner -> "owner"
+ GRAdmin -> "admin"
+ GRMember -> "member"
+
+fromBlobField_ :: Typeable k => (ByteString -> Either String k) -> FieldParser k
+fromBlobField_ p = \case
+ f@(Field (SQLBlob b) _) ->
+ case p b of
+ Right k -> Ok k
+ Left e -> returnError ConversionFailed f ("could not parse field: " ++ e)
+ f -> returnError ConversionFailed f "expecting SQLBlob column type"
+
+data GroupMemberCategory
+ = GCUserMember
+ | GCInviteeMember -- member invited by the user
+ | GCHostMember -- member who invited the user
+ | GCPreMember -- member who joined before the user and was introduced to the user (user receives x.grp.mem.intro about such members)
+ | GCPostMember -- member who joined after the user to whom the user was introduced (user receives x.grp.mem.new announcing these members and then x.grp.mem.fwd with invitation from these members)
+ deriving (Eq, Show)
+
+instance FromField GroupMemberCategory where fromField = fromTextField_ memberCategoryT
+
+instance ToField GroupMemberCategory where toField = toField . serializeMemberCategory
+
+memberCategoryT :: Text -> Maybe GroupMemberCategory
+memberCategoryT = \case
+ "user" -> Just GCUserMember
+ "invitee" -> Just GCInviteeMember
+ "host" -> Just GCHostMember
+ "pre" -> Just GCPreMember
+ "post" -> Just GCPostMember
+ _ -> Nothing
+
+serializeMemberCategory :: GroupMemberCategory -> Text
+serializeMemberCategory = \case
+ GCUserMember -> "user"
+ GCInviteeMember -> "invitee"
+ GCHostMember -> "host"
+ GCPreMember -> "pre"
+ GCPostMember -> "post"
+
+data GroupMemberStatus
+ = GSMemRemoved -- member who was removed from the group
+ | GSMemLeft -- member who left the group
+ | GSMemGroupDeleted -- user member of the deleted group
+ | GSMemInvited -- member is sent to or received invitation to join the group
+ | GSMemIntroduced -- user received x.grp.mem.intro for this member (only with GCPreMember)
+ | GSMemIntroInvited -- member is sent to or received from intro invitation
+ | GSMemAccepted -- member accepted invitation (only User and Invitee)
+ | GSMemAnnounced -- host announced (x.grp.mem.new) a member (Invitee and PostMember) to the group - at this point this member can send messages and invite other members (if they have sufficient permissions)
+ | GSMemConnected -- member created the group connection with the inviting member
+ | GSMemComplete -- host confirmed (x.grp.mem.all) that a member (User, Invitee and PostMember) created group connections with all previous members
+ | GSMemCreator -- user member that created the group (only GCUserMember)
+ deriving (Eq, Show, Ord)
+
+instance FromField GroupMemberStatus where fromField = fromTextField_ memberStatusT
+
+instance ToField GroupMemberStatus where toField = toField . serializeMemberStatus
+
+memberActive :: GroupMember -> Bool
+memberActive m = case memberStatus m of
+ GSMemRemoved -> False
+ GSMemLeft -> False
+ GSMemGroupDeleted -> False
+ GSMemInvited -> False
+ GSMemIntroduced -> False
+ GSMemIntroInvited -> False
+ GSMemAccepted -> False
+ GSMemAnnounced -> False
+ GSMemConnected -> True
+ GSMemComplete -> True
+ GSMemCreator -> True
+
+memberCurrent :: GroupMember -> Bool
+memberCurrent m = case memberStatus m of
+ GSMemRemoved -> False
+ GSMemLeft -> False
+ GSMemGroupDeleted -> False
+ GSMemInvited -> False
+ GSMemIntroduced -> True
+ GSMemIntroInvited -> True
+ GSMemAccepted -> True
+ GSMemAnnounced -> True
+ GSMemConnected -> True
+ GSMemComplete -> True
+ GSMemCreator -> True
+
+memberStatusT :: Text -> Maybe GroupMemberStatus
+memberStatusT = \case
+ "removed" -> Just GSMemRemoved
+ "left" -> Just GSMemLeft
+ "deleted" -> Just GSMemGroupDeleted
+ "invited" -> Just GSMemInvited
+ "introduced" -> Just GSMemIntroduced
+ "intro-inv" -> Just GSMemIntroInvited
+ "accepted" -> Just GSMemAccepted
+ "announced" -> Just GSMemAnnounced
+ "connected" -> Just GSMemConnected
+ "complete" -> Just GSMemComplete
+ "creator" -> Just GSMemCreator
+ _ -> Nothing
+
+serializeMemberStatus :: GroupMemberStatus -> Text
+serializeMemberStatus = \case
+ GSMemRemoved -> "removed"
+ GSMemLeft -> "left"
+ GSMemGroupDeleted -> "deleted"
+ GSMemInvited -> "invited"
+ GSMemIntroduced -> "introduced"
+ GSMemIntroInvited -> "intro-inv"
+ GSMemAccepted -> "accepted"
+ GSMemAnnounced -> "announced"
+ GSMemConnected -> "connected"
+ GSMemComplete -> "complete"
+ GSMemCreator -> "creator"
+
+data SndFileTransfer = SndFileTransfer
+ { fileId :: Int64,
+ fileName :: String,
+ filePath :: String,
+ fileSize :: Integer,
+ chunkSize :: Integer,
+ recipientDisplayName :: ContactName,
+ connId :: Int64,
+ agentConnId :: ConnId,
+ fileStatus :: FileStatus
+ }
+ deriving (Eq, Show)
+
+data FileInvitation = FileInvitation
+ { fileName :: String,
+ fileSize :: Integer,
+ fileQInfo :: SMPQueueInfo
+ }
+ deriving (Eq, Show)
+
+data RcvFileTransfer = RcvFileTransfer
+ { fileId :: Int64,
+ fileInvitation :: FileInvitation,
+ fileStatus :: RcvFileStatus,
+ senderDisplayName :: ContactName,
+ chunkSize :: Integer
+ }
+ deriving (Eq, Show)
+
+data RcvFileStatus
+ = RFSNew
+ | RFSAccepted RcvFileInfo
+ | RFSConnected RcvFileInfo
+ | RFSComplete RcvFileInfo
+ | RFSCancelled RcvFileInfo
+ deriving (Eq, Show)
+
+data RcvFileInfo = RcvFileInfo
+ { filePath :: FilePath,
+ connId :: Int64,
+ agentConnId :: ConnId
+ }
+ deriving (Eq, Show)
+
+data FileTransfer = FTSnd [SndFileTransfer] | FTRcv RcvFileTransfer
+
+data FileStatus = FSNew | FSAccepted | FSConnected | FSComplete | FSCancelled deriving (Eq, Ord, Show)
+
+instance FromField FileStatus where fromField = fromTextField_ fileStatusT
+
+instance ToField FileStatus where toField = toField . serializeFileStatus
+
+fileStatusT :: Text -> Maybe FileStatus
+fileStatusT = \case
+ "new" -> Just FSNew
+ "accepted" -> Just FSAccepted
+ "connected" -> Just FSConnected
+ "complete" -> Just FSComplete
+ "cancelled" -> Just FSCancelled
+ _ -> Nothing
+
+serializeFileStatus :: FileStatus -> Text
+serializeFileStatus = \case
+ FSNew -> "new"
+ FSAccepted -> "accepted"
+ FSConnected -> "connected"
+ FSComplete -> "complete"
+ FSCancelled -> "cancelled"
+
+data RcvChunkStatus = RcvChunkOk | RcvChunkFinal | RcvChunkDuplicate | RcvChunkError
+ deriving (Eq, Show)
+
+data Connection = Connection
+ { connId :: Int64,
+ agentConnId :: ConnId,
+ connLevel :: Int,
+ viaContact :: Maybe Int64,
+ connType :: ConnType,
+ connStatus :: ConnStatus,
+ entityId :: Maybe Int64, -- contact, group member or file ID
+ createdAt :: UTCTime
+ }
+ deriving (Eq, Show)
+
+data ConnStatus
+ = -- | connection is created by initiating party with agent NEW command (createConnection)
+ ConnNew
+ | -- | connection is joined by joining party with agent JOIN command (joinConnection)
+ ConnJoined
+ | -- | initiating party received CONF notification (to be renamed to REQ)
+ ConnRequested
+ | -- | initiating party accepted connection with agent LET command (to be renamed to ACPT) (allowConnection)
+ ConnAccepted
+ | -- | connection can be sent messages to (after joining party received INFO notification)
+ ConnSndReady
+ | -- | connection is ready for both parties to send and receive messages
+ ConnReady
+ | -- | connection deleted
+ ConnDeleted
+ deriving (Eq, Show)
+
+instance FromField ConnStatus where fromField = fromTextField_ connStatusT
+
+instance ToField ConnStatus where toField = toField . serializeConnStatus
+
+connStatusT :: Text -> Maybe ConnStatus
+connStatusT = \case
+ "new" -> Just ConnNew
+ "joined" -> Just ConnJoined
+ "requested" -> Just ConnRequested
+ "accepted" -> Just ConnAccepted
+ "snd-ready" -> Just ConnSndReady
+ "ready" -> Just ConnReady
+ "deleted" -> Just ConnDeleted
+ _ -> Nothing
+
+serializeConnStatus :: ConnStatus -> Text
+serializeConnStatus = \case
+ ConnNew -> "new"
+ ConnJoined -> "joined"
+ ConnRequested -> "requested"
+ ConnAccepted -> "accepted"
+ ConnSndReady -> "snd-ready"
+ ConnReady -> "ready"
+ ConnDeleted -> "deleted"
+
+data ConnType = ConnContact | ConnMember | ConnSndFile | ConnRcvFile
+ deriving (Eq, Show)
+
+instance FromField ConnType where fromField = fromTextField_ connTypeT
+
+instance ToField ConnType where toField = toField . serializeConnType
+
+connTypeT :: Text -> Maybe ConnType
+connTypeT = \case
+ "contact" -> Just ConnContact
+ "member" -> Just ConnMember
+ "snd_file" -> Just ConnSndFile
+ "rcv_file" -> Just ConnRcvFile
+ _ -> Nothing
+
+serializeConnType :: ConnType -> Text
+serializeConnType = \case
+ ConnContact -> "contact"
+ ConnMember -> "member"
+ ConnSndFile -> "snd_file"
+ ConnRcvFile -> "rcv_file"
+
+data NewConnection = NewConnection
+ { agentConnId :: ByteString,
+ connLevel :: Int,
+ viaConn :: Maybe Int64
+ }
+
+data GroupMemberIntro = GroupMemberIntro
+ { introId :: Int64,
+ reMember :: GroupMember,
+ toMember :: GroupMember,
+ introStatus :: GroupMemberIntroStatus,
+ introInvitation :: Maybe IntroInvitation
+ }
+
+data GroupMemberIntroStatus
+ = GMIntroPending
+ | GMIntroSent
+ | GMIntroInvReceived
+ | GMIntroInvForwarded
+ | GMIntroReConnected
+ | GMIntroToConnected
+ | GMIntroConnected
+
+instance FromField GroupMemberIntroStatus where fromField = fromTextField_ introStatusT
+
+instance ToField GroupMemberIntroStatus where toField = toField . serializeIntroStatus
+
+introStatusT :: Text -> Maybe GroupMemberIntroStatus
+introStatusT = \case
+ "new" -> Just GMIntroPending
+ "sent" -> Just GMIntroSent
+ "rcv" -> Just GMIntroInvReceived
+ "fwd" -> Just GMIntroInvForwarded
+ "re-con" -> Just GMIntroReConnected
+ "to-con" -> Just GMIntroToConnected
+ "con" -> Just GMIntroConnected
+ _ -> Nothing
+
+serializeIntroStatus :: GroupMemberIntroStatus -> Text
+serializeIntroStatus = \case
+ GMIntroPending -> "new"
+ GMIntroSent -> "sent"
+ GMIntroInvReceived -> "rcv"
+ GMIntroInvForwarded -> "fwd"
+ GMIntroReConnected -> "re-con"
+ GMIntroToConnected -> "to-con"
+ GMIntroConnected -> "con"
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/Util.hs b/packages/simplex_app/haskell/src/Simplex/Chat/Util.hs
new file mode 100644
index 0000000000..05ea20cf8d
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/Util.hs
@@ -0,0 +1,16 @@
+module Simplex.Chat.Util where
+
+import Data.ByteString.Char8 (ByteString)
+import Data.Text (Text)
+import Data.Text.Encoding (decodeUtf8With)
+
+safeDecodeUtf8 :: ByteString -> Text
+safeDecodeUtf8 = decodeUtf8With onError
+ where
+ onError _ _ = Just '?'
+
+ifM :: Monad m => m Bool -> m a -> m a -> m a
+ifM ba t f = ba >>= \b -> if b then t else f
+
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM b = ifM b $ pure ()
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/View.hs b/packages/simplex_app/haskell/src/Simplex/Chat/View.hs
new file mode 100644
index 0000000000..b2108eb791
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/View.hs
@@ -0,0 +1,687 @@
+{-# LANGUAGE ConstraintKinds #-}
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module Simplex.Chat.View
+ ( printToView,
+ showInvitation,
+ showChatError,
+ showContactDeleted,
+ showContactGroups,
+ showContactConnected,
+ showContactDisconnected,
+ showContactAnotherClient,
+ showContactSubscribed,
+ showContactSubError,
+ showGroupSubscribed,
+ showGroupEmpty,
+ showGroupRemoved,
+ showMemberSubError,
+ showReceivedMessage,
+ showReceivedGroupMessage,
+ showSentMessage,
+ showSentGroupMessage,
+ showSentFileInvitation,
+ showSentGroupFileInvitation,
+ showSentFileInfo,
+ showSndFileStart,
+ showSndFileComplete,
+ showSndFileCancelled,
+ showSndGroupFileCancelled,
+ showSndFileRcvCancelled,
+ receivedFileInvitation,
+ showRcvFileAccepted,
+ showRcvFileStart,
+ showRcvFileComplete,
+ showRcvFileCancelled,
+ showRcvFileSndCancelled,
+ showFileTransferStatus,
+ showSndFileSubError,
+ showRcvFileSubError,
+ showGroupCreated,
+ showGroupDeletedUser,
+ showGroupDeleted,
+ showSentGroupInvitation,
+ showReceivedGroupInvitation,
+ showJoinedGroupMember,
+ showUserJoinedGroup,
+ showJoinedGroupMemberConnecting,
+ showConnectedToGroupMember,
+ showDeletedMember,
+ showDeletedMemberUser,
+ showLeftMemberUser,
+ showLeftMember,
+ showGroupMembers,
+ showContactsMerged,
+ showUserProfile,
+ showUserProfileUpdated,
+ showContactUpdated,
+ showMessageError,
+ safeDecodeUtf8,
+ msgPlain,
+ )
+where
+
+import Control.Monad.IO.Unlift
+import Control.Monad.Reader
+import Data.ByteString.Char8 (ByteString)
+import Data.Composition ((.:), (.:.))
+import Data.Function (on)
+import Data.Int (Int64)
+import Data.List (groupBy, intersperse, sortOn)
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Time.Clock (DiffTime, UTCTime)
+import Data.Time.Format (defaultTimeLocale, formatTime)
+import Data.Time.LocalTime (TimeZone, ZonedTime, getCurrentTimeZone, getZonedTime, localDay, localTimeOfDay, timeOfDayToTime, utcToLocalTime, zonedTimeToLocalTime)
+import Numeric (showFFloat)
+import Simplex.Chat.Controller
+import Simplex.Chat.Markdown
+import Simplex.Chat.Store (StoreError (..))
+import Simplex.Chat.Styled
+import Simplex.Chat.Terminal (printToTerminal)
+import Simplex.Chat.Types
+import Simplex.Chat.Util (safeDecodeUtf8)
+import Simplex.Messaging.Agent.Protocol
+import System.Console.ANSI.Types
+
+type ChatReader m = (MonadUnliftIO m, MonadReader ChatController m)
+
+showInvitation :: ChatReader m => SMPQueueInfo -> m ()
+showInvitation = printToView . invitation
+
+showChatError :: ChatReader m => ChatError -> m ()
+showChatError = printToView . chatError
+
+showContactDeleted :: ChatReader m => ContactName -> m ()
+showContactDeleted = printToView . contactDeleted
+
+showContactGroups :: ChatReader m => ContactName -> [GroupName] -> m ()
+showContactGroups = printToView .: contactGroups
+
+showContactConnected :: ChatReader m => Contact -> m ()
+showContactConnected = printToView . contactConnected
+
+showContactDisconnected :: ChatReader m => ContactName -> m ()
+showContactDisconnected = printToView . contactDisconnected
+
+showContactAnotherClient :: ChatReader m => ContactName -> m ()
+showContactAnotherClient = printToView . contactAnotherClient
+
+showContactSubscribed :: ChatReader m => ContactName -> m ()
+showContactSubscribed = printToView . contactSubscribed
+
+showContactSubError :: ChatReader m => ContactName -> ChatError -> m ()
+showContactSubError = printToView .: contactSubError
+
+showGroupSubscribed :: ChatReader m => GroupName -> m ()
+showGroupSubscribed = printToView . groupSubscribed
+
+showGroupEmpty :: ChatReader m => GroupName -> m ()
+showGroupEmpty = printToView . groupEmpty
+
+showGroupRemoved :: ChatReader m => GroupName -> m ()
+showGroupRemoved = printToView . groupRemoved
+
+showMemberSubError :: ChatReader m => GroupName -> ContactName -> ChatError -> m ()
+showMemberSubError = printToView .:. memberSubError
+
+showReceivedMessage :: ChatReader m => ContactName -> UTCTime -> [StyledString] -> MsgIntegrity -> m ()
+showReceivedMessage = showReceivedMessage_ . ttyFromContact
+
+showReceivedGroupMessage :: ChatReader m => GroupName -> ContactName -> UTCTime -> [StyledString] -> MsgIntegrity -> m ()
+showReceivedGroupMessage = showReceivedMessage_ .: ttyFromGroup
+
+showReceivedMessage_ :: ChatReader m => StyledString -> UTCTime -> [StyledString] -> MsgIntegrity -> m ()
+showReceivedMessage_ from utcTime msg mOk = printToView =<< liftIO (receivedMessage from utcTime msg mOk)
+
+showSentMessage :: ChatReader m => ContactName -> ByteString -> m ()
+showSentMessage = showSentMessage_ . ttyToContact
+
+showSentGroupMessage :: ChatReader m => GroupName -> ByteString -> m ()
+showSentGroupMessage = showSentMessage_ . ttyToGroup
+
+showSentMessage_ :: ChatReader m => StyledString -> ByteString -> m ()
+showSentMessage_ to msg = printToView =<< liftIO (sentMessage to msg)
+
+showSentFileInvitation :: ChatReader m => ContactName -> FilePath -> m ()
+showSentFileInvitation = showSentFileInvitation_ . ttyToContact
+
+showSentGroupFileInvitation :: ChatReader m => GroupName -> FilePath -> m ()
+showSentGroupFileInvitation = showSentFileInvitation_ . ttyToGroup
+
+showSentFileInvitation_ :: ChatReader m => StyledString -> FilePath -> m ()
+showSentFileInvitation_ to filePath = printToView =<< liftIO (sentFileInvitation to filePath)
+
+showSentFileInfo :: ChatReader m => Int64 -> m ()
+showSentFileInfo = printToView . sentFileInfo
+
+showSndFileStart :: ChatReader m => SndFileTransfer -> m ()
+showSndFileStart = printToView . sndFileStart
+
+showSndFileComplete :: ChatReader m => SndFileTransfer -> m ()
+showSndFileComplete = printToView . sndFileComplete
+
+showSndFileCancelled :: ChatReader m => SndFileTransfer -> m ()
+showSndFileCancelled = printToView . sndFileCancelled
+
+showSndGroupFileCancelled :: ChatReader m => [SndFileTransfer] -> m ()
+showSndGroupFileCancelled = printToView . sndGroupFileCancelled
+
+showSndFileRcvCancelled :: ChatReader m => SndFileTransfer -> m ()
+showSndFileRcvCancelled = printToView . sndFileRcvCancelled
+
+showRcvFileAccepted :: ChatReader m => RcvFileTransfer -> FilePath -> m ()
+showRcvFileAccepted = printToView .: rcvFileAccepted
+
+showRcvFileStart :: ChatReader m => RcvFileTransfer -> m ()
+showRcvFileStart = printToView . rcvFileStart
+
+showRcvFileComplete :: ChatReader m => RcvFileTransfer -> m ()
+showRcvFileComplete = printToView . rcvFileComplete
+
+showRcvFileCancelled :: ChatReader m => RcvFileTransfer -> m ()
+showRcvFileCancelled = printToView . rcvFileCancelled
+
+showRcvFileSndCancelled :: ChatReader m => RcvFileTransfer -> m ()
+showRcvFileSndCancelled = printToView . rcvFileSndCancelled
+
+showFileTransferStatus :: ChatReader m => (FileTransfer, [Integer]) -> m ()
+showFileTransferStatus = printToView . fileTransferStatus
+
+showSndFileSubError :: ChatReader m => SndFileTransfer -> ChatError -> m ()
+showSndFileSubError = printToView .: sndFileSubError
+
+showRcvFileSubError :: ChatReader m => RcvFileTransfer -> ChatError -> m ()
+showRcvFileSubError = printToView .: rcvFileSubError
+
+showGroupCreated :: ChatReader m => Group -> m ()
+showGroupCreated = printToView . groupCreated
+
+showGroupDeletedUser :: ChatReader m => GroupName -> m ()
+showGroupDeletedUser = printToView . groupDeletedUser
+
+showGroupDeleted :: ChatReader m => GroupName -> GroupMember -> m ()
+showGroupDeleted = printToView .: groupDeleted
+
+showSentGroupInvitation :: ChatReader m => GroupName -> ContactName -> m ()
+showSentGroupInvitation = printToView .: sentGroupInvitation
+
+showReceivedGroupInvitation :: ChatReader m => Group -> ContactName -> GroupMemberRole -> m ()
+showReceivedGroupInvitation = printToView .:. receivedGroupInvitation
+
+showJoinedGroupMember :: ChatReader m => GroupName -> GroupMember -> m ()
+showJoinedGroupMember = printToView .: joinedGroupMember
+
+showUserJoinedGroup :: ChatReader m => GroupName -> m ()
+showUserJoinedGroup = printToView . userJoinedGroup
+
+showJoinedGroupMemberConnecting :: ChatReader m => GroupName -> GroupMember -> GroupMember -> m ()
+showJoinedGroupMemberConnecting = printToView .:. joinedGroupMemberConnecting
+
+showConnectedToGroupMember :: ChatReader m => GroupName -> GroupMember -> m ()
+showConnectedToGroupMember = printToView .: connectedToGroupMember
+
+showDeletedMember :: ChatReader m => GroupName -> Maybe GroupMember -> Maybe GroupMember -> m ()
+showDeletedMember = printToView .:. deletedMember
+
+showDeletedMemberUser :: ChatReader m => GroupName -> GroupMember -> m ()
+showDeletedMemberUser = printToView .: deletedMemberUser
+
+showLeftMemberUser :: ChatReader m => GroupName -> m ()
+showLeftMemberUser = printToView . leftMemberUser
+
+showLeftMember :: ChatReader m => GroupName -> GroupMember -> m ()
+showLeftMember = printToView .: leftMember
+
+showGroupMembers :: ChatReader m => Group -> m ()
+showGroupMembers = printToView . groupMembers
+
+showContactsMerged :: ChatReader m => Contact -> Contact -> m ()
+showContactsMerged = printToView .: contactsMerged
+
+showUserProfile :: ChatReader m => Profile -> m ()
+showUserProfile = printToView . userProfile
+
+showUserProfileUpdated :: ChatReader m => User -> User -> m ()
+showUserProfileUpdated = printToView .: userProfileUpdated
+
+showContactUpdated :: ChatReader m => Contact -> Contact -> m ()
+showContactUpdated = printToView .: contactUpdated
+
+showMessageError :: ChatReader m => Text -> Text -> m ()
+showMessageError = printToView .: messageError
+
+invitation :: SMPQueueInfo -> [StyledString]
+invitation qInfo =
+ [ "pass this invitation to your contact (via another channel): ",
+ "",
+ (plain . serializeSmpQueueInfo) qInfo,
+ "",
+ "and ask them to connect: " <> highlight' "/c "
+ ]
+
+contactDeleted :: ContactName -> [StyledString]
+contactDeleted c = [ttyContact c <> ": contact is deleted"]
+
+contactGroups :: ContactName -> [GroupName] -> [StyledString]
+contactGroups c gNames = [ttyContact c <> ": contact cannot be deleted, it is a member of the group(s) " <> ttyGroups gNames]
+ where
+ ttyGroups :: [GroupName] -> StyledString
+ ttyGroups [] = ""
+ ttyGroups [g] = ttyGroup g
+ ttyGroups (g : gs) = ttyGroup g <> ", " <> ttyGroups gs
+
+contactConnected :: Contact -> [StyledString]
+contactConnected ct = [ttyFullContact ct <> ": contact is connected"]
+
+contactDisconnected :: ContactName -> [StyledString]
+contactDisconnected c = [ttyContact c <> ": disconnected from server (messages will be queued)"]
+
+contactAnotherClient :: ContactName -> [StyledString]
+contactAnotherClient c = [ttyContact c <> ": contact is connected to another client"]
+
+contactSubscribed :: ContactName -> [StyledString]
+contactSubscribed c = [ttyContact c <> ": connected to server"]
+
+contactSubError :: ContactName -> ChatError -> [StyledString]
+contactSubError c e = [ttyContact c <> ": contact error " <> sShow e]
+
+groupSubscribed :: GroupName -> [StyledString]
+groupSubscribed g = [ttyGroup g <> ": connected to server(s)"]
+
+groupEmpty :: GroupName -> [StyledString]
+groupEmpty g = [ttyGroup g <> ": group is empty"]
+
+groupRemoved :: GroupName -> [StyledString]
+groupRemoved g = [ttyGroup g <> ": you are no longer a member or group deleted"]
+
+memberSubError :: GroupName -> ContactName -> ChatError -> [StyledString]
+memberSubError g c e = [ttyGroup g <> " member " <> ttyContact c <> " error: " <> sShow e]
+
+groupCreated :: Group -> [StyledString]
+groupCreated g@Group {localDisplayName} =
+ [ "group " <> ttyFullGroup g <> " is created",
+ "use " <> highlight ("/a " <> localDisplayName <> " ") <> " to add members"
+ ]
+
+groupDeletedUser :: GroupName -> [StyledString]
+groupDeletedUser g = groupDeleted_ g Nothing
+
+groupDeleted :: GroupName -> GroupMember -> [StyledString]
+groupDeleted g m = groupDeleted_ g (Just m) <> ["use " <> highlight ("/d #" <> g) <> " to delete the local copy of the group"]
+
+groupDeleted_ :: GroupName -> Maybe GroupMember -> [StyledString]
+groupDeleted_ g m = [ttyGroup g <> ": " <> memberOrUser m <> " deleted the group"]
+
+sentGroupInvitation :: GroupName -> ContactName -> [StyledString]
+sentGroupInvitation g c = ["invitation to join the group " <> ttyGroup g <> " sent to " <> ttyContact c]
+
+receivedGroupInvitation :: Group -> ContactName -> GroupMemberRole -> [StyledString]
+receivedGroupInvitation g@Group {localDisplayName} c role =
+ [ ttyFullGroup g <> ": " <> ttyContact c <> " invites you to join the group as " <> plain (serializeMemberRole role),
+ "use " <> highlight ("/j " <> localDisplayName) <> " to accept"
+ ]
+
+joinedGroupMember :: GroupName -> GroupMember -> [StyledString]
+joinedGroupMember g m = [ttyGroup g <> ": " <> ttyMember m <> " joined the group "]
+
+userJoinedGroup :: GroupName -> [StyledString]
+userJoinedGroup g = [ttyGroup g <> ": you joined the group"]
+
+joinedGroupMemberConnecting :: GroupName -> GroupMember -> GroupMember -> [StyledString]
+joinedGroupMemberConnecting g host m = [ttyGroup g <> ": " <> ttyMember host <> " added " <> ttyFullMember m <> " to the group (connecting...)"]
+
+connectedToGroupMember :: GroupName -> GroupMember -> [StyledString]
+connectedToGroupMember g m = [ttyGroup g <> ": " <> connectedMember m <> " is connected"]
+
+deletedMember :: GroupName -> Maybe GroupMember -> Maybe GroupMember -> [StyledString]
+deletedMember g by m = [ttyGroup g <> ": " <> memberOrUser by <> " removed " <> memberOrUser m <> " from the group"]
+
+deletedMemberUser :: GroupName -> GroupMember -> [StyledString]
+deletedMemberUser g by = deletedMember g (Just by) Nothing <> groupPreserved g
+
+leftMemberUser :: GroupName -> [StyledString]
+leftMemberUser g = leftMember_ g Nothing <> groupPreserved g
+
+leftMember :: GroupName -> GroupMember -> [StyledString]
+leftMember g m = leftMember_ g (Just m)
+
+leftMember_ :: GroupName -> Maybe GroupMember -> [StyledString]
+leftMember_ g m = [ttyGroup g <> ": " <> memberOrUser m <> " left the group"]
+
+groupPreserved :: GroupName -> [StyledString]
+groupPreserved g = ["use " <> highlight ("/d #" <> g) <> " to delete the group"]
+
+memberOrUser :: Maybe GroupMember -> StyledString
+memberOrUser = maybe "you" ttyMember
+
+connectedMember :: GroupMember -> StyledString
+connectedMember m = case memberCategory m of
+ GCPreMember -> "member " <> ttyFullMember m
+ GCPostMember -> "new member " <> ttyMember m -- without fullName as as it was shown in joinedGroupMemberConnecting
+ _ -> "member " <> ttyMember m -- these case is not used
+
+groupMembers :: Group -> [StyledString]
+groupMembers Group {membership, members} = map groupMember . filter (not . removedOrLeft) $ membership : members
+ where
+ removedOrLeft m = let s = memberStatus m in s == GSMemRemoved || s == GSMemLeft
+ groupMember m = ttyFullMember m <> ": " <> role m <> ", " <> category m <> status m
+ role = plain . serializeMemberRole . memberRole
+ category m = case memberCategory m of
+ GCUserMember -> "you, "
+ GCInviteeMember -> "invited, "
+ GCHostMember -> "host, "
+ _ -> ""
+ status m = case memberStatus m of
+ GSMemRemoved -> "removed"
+ GSMemLeft -> "left"
+ GSMemInvited -> "not yet joined"
+ GSMemConnected -> "connected"
+ GSMemComplete -> "connected"
+ GSMemCreator -> "created group"
+ _ -> ""
+
+contactsMerged :: Contact -> Contact -> [StyledString]
+contactsMerged _to@Contact {localDisplayName = c1} _from@Contact {localDisplayName = c2} =
+ [ "contact " <> ttyContact c2 <> " is merged into " <> ttyContact c1,
+ "use " <> ttyToContact c1 <> highlight' "" <> " to send messages"
+ ]
+
+userProfile :: Profile -> [StyledString]
+userProfile Profile {displayName, fullName} =
+ [ "user profile: " <> ttyFullName displayName fullName,
+ "use " <> highlight' "/p []" <> " to change it",
+ "(the updated profile will be sent to all your contacts)"
+ ]
+
+userProfileUpdated :: User -> User -> [StyledString]
+userProfileUpdated
+ User {localDisplayName = n, profile = Profile {fullName}}
+ User {localDisplayName = n', profile = Profile {fullName = fullName'}}
+ | n == n' && fullName == fullName' = []
+ | n == n' = ["user full name " <> (if T.null fullName' || fullName' == n' then "removed" else "changed to " <> plain fullName') <> notified]
+ | otherwise = ["user profile is changed to " <> ttyFullName n' fullName' <> notified]
+ where
+ notified = " (your contacts are notified)"
+
+contactUpdated :: Contact -> Contact -> [StyledString]
+contactUpdated
+ Contact {localDisplayName = n, profile = Profile {fullName}}
+ Contact {localDisplayName = n', profile = Profile {fullName = fullName'}}
+ | n == n' && fullName == fullName' = []
+ | n == n' = ["contact " <> ttyContact n <> fullNameUpdate]
+ | otherwise =
+ [ "contact " <> ttyContact n <> " changed to " <> ttyFullName n' fullName',
+ "use " <> ttyToContact n' <> highlight' "" <> " to send messages"
+ ]
+ where
+ fullNameUpdate = if T.null fullName' || fullName' == n' then " removed full name" else " updated full name: " <> plain fullName'
+
+messageError :: Text -> Text -> [StyledString]
+messageError prefix err = [plain prefix <> ": " <> plain err]
+
+receivedMessage :: StyledString -> UTCTime -> [StyledString] -> MsgIntegrity -> IO [StyledString]
+receivedMessage from utcTime msg mOk = do
+ t <- formatUTCTime <$> getCurrentTimeZone <*> getZonedTime
+ pure $ prependFirst (t <> " " <> from) msg ++ showIntegrity mOk
+ where
+ formatUTCTime :: TimeZone -> ZonedTime -> StyledString
+ formatUTCTime localTz currentTime =
+ let localTime = utcToLocalTime localTz utcTime
+ format =
+ if (localDay localTime < localDay (zonedTimeToLocalTime currentTime))
+ && (timeOfDayToTime (localTimeOfDay localTime) > (6 * 60 * 60 :: DiffTime))
+ then "%m-%d" -- if message is from yesterday or before and 6 hours has passed since midnight
+ else "%H:%M"
+ in styleTime $ formatTime defaultTimeLocale format localTime
+ showIntegrity :: MsgIntegrity -> [StyledString]
+ showIntegrity MsgOk = []
+ showIntegrity (MsgError err) = msgError $ case err of
+ MsgSkipped fromId toId ->
+ "skipped message ID " <> show fromId
+ <> if fromId == toId then "" else ".." <> show toId
+ MsgBadId msgId -> "unexpected message ID " <> show msgId
+ MsgBadHash -> "incorrect message hash"
+ MsgDuplicate -> "duplicate message ID"
+ msgError :: String -> [StyledString]
+ msgError s = [styled (Colored Red) s]
+
+sentMessage :: StyledString -> ByteString -> IO [StyledString]
+sentMessage to msg = sendWithTime_ to . msgPlain $ safeDecodeUtf8 msg
+
+sentFileInvitation :: StyledString -> FilePath -> IO [StyledString]
+sentFileInvitation to f = sendWithTime_ ("/f " <> to) [ttyFilePath f]
+
+sendWithTime_ :: StyledString -> [StyledString] -> IO [StyledString]
+sendWithTime_ to styledMsg = do
+ time <- formatTime defaultTimeLocale "%H:%M" <$> getZonedTime
+ pure $ prependFirst (styleTime time <> " " <> to) styledMsg
+
+prependFirst :: StyledString -> [StyledString] -> [StyledString]
+prependFirst s [] = [s]
+prependFirst s (s' : ss) = (s <> s') : ss
+
+msgPlain :: Text -> [StyledString]
+msgPlain = map styleMarkdownText . T.lines
+
+sentFileInfo :: Int64 -> [StyledString]
+sentFileInfo fileId =
+ ["use " <> highlight ("/fc " <> show fileId) <> " to cancel sending"]
+
+sndFileStart :: SndFileTransfer -> [StyledString]
+sndFileStart = sendingFile_ "started"
+
+sndFileComplete :: SndFileTransfer -> [StyledString]
+sndFileComplete = sendingFile_ "completed"
+
+sndFileCancelled :: SndFileTransfer -> [StyledString]
+sndFileCancelled = sendingFile_ "cancelled"
+
+sndGroupFileCancelled :: [SndFileTransfer] -> [StyledString]
+sndGroupFileCancelled fts =
+ case filter (\SndFileTransfer {fileStatus = s} -> s /= FSCancelled && s /= FSComplete) fts of
+ [] -> ["sending file can't be cancelled"]
+ ts@(ft : _) -> ["cancelled sending " <> sndFile ft <> " to " <> listMembers ts]
+
+sendingFile_ :: StyledString -> SndFileTransfer -> [StyledString]
+sendingFile_ status ft@SndFileTransfer {recipientDisplayName = c} =
+ [status <> " sending " <> sndFile ft <> " to " <> ttyContact c]
+
+sndFileRcvCancelled :: SndFileTransfer -> [StyledString]
+sndFileRcvCancelled ft@SndFileTransfer {recipientDisplayName = c} =
+ [ttyContact c <> " cancelled receiving " <> sndFile ft]
+
+sndFile :: SndFileTransfer -> StyledString
+sndFile SndFileTransfer {fileId, fileName} = fileTransfer fileId fileName
+
+receivedFileInvitation :: RcvFileTransfer -> [StyledString]
+receivedFileInvitation RcvFileTransfer {fileId, fileInvitation = FileInvitation {fileName, fileSize}} =
+ [ "sends file " <> ttyFilePath fileName <> " (" <> humanReadableSize fileSize <> " / " <> sShow fileSize <> " bytes)",
+ "use " <> highlight ("/fr " <> show fileId <> " [/ | ]") <> " to receive it"
+ ]
+
+humanReadableSize :: Integer -> StyledString
+humanReadableSize size
+ | size < kB = sShow size <> " bytes"
+ | size < mB = hrSize kB "KiB"
+ | size < gB = hrSize mB "MiB"
+ | otherwise = hrSize gB "GiB"
+ where
+ hrSize sB name = plain $ unwords [showFFloat (Just 1) (fromIntegral size / (fromIntegral sB :: Double)) "", name]
+ kB = 1024
+ mB = kB * 1024
+ gB = mB * 1024
+
+rcvFileAccepted :: RcvFileTransfer -> FilePath -> [StyledString]
+rcvFileAccepted RcvFileTransfer {fileId, senderDisplayName = c} filePath =
+ ["saving file " <> sShow fileId <> " from " <> ttyContact c <> " to " <> plain filePath]
+
+rcvFileStart :: RcvFileTransfer -> [StyledString]
+rcvFileStart = receivingFile_ "started"
+
+rcvFileComplete :: RcvFileTransfer -> [StyledString]
+rcvFileComplete = receivingFile_ "completed"
+
+rcvFileCancelled :: RcvFileTransfer -> [StyledString]
+rcvFileCancelled = receivingFile_ "cancelled"
+
+receivingFile_ :: StyledString -> RcvFileTransfer -> [StyledString]
+receivingFile_ status ft@RcvFileTransfer {senderDisplayName = c} =
+ [status <> " receiving " <> rcvFile ft <> " from " <> ttyContact c]
+
+rcvFileSndCancelled :: RcvFileTransfer -> [StyledString]
+rcvFileSndCancelled ft@RcvFileTransfer {senderDisplayName = c} =
+ [ttyContact c <> " cancelled sending " <> rcvFile ft]
+
+rcvFile :: RcvFileTransfer -> StyledString
+rcvFile RcvFileTransfer {fileId, fileInvitation = FileInvitation {fileName}} = fileTransfer fileId fileName
+
+fileTransfer :: Int64 -> String -> StyledString
+fileTransfer fileId fileName = "file " <> sShow fileId <> " (" <> ttyFilePath fileName <> ")"
+
+fileTransferStatus :: (FileTransfer, [Integer]) -> [StyledString]
+fileTransferStatus (FTSnd [ft@SndFileTransfer {fileStatus, fileSize, chunkSize}], chunksNum) =
+ ["sending " <> sndFile ft <> " " <> sndStatus]
+ where
+ sndStatus = case fileStatus of
+ FSNew -> "not accepted yet"
+ FSAccepted -> "just started"
+ FSConnected -> "progress " <> fileProgress chunksNum chunkSize fileSize
+ FSComplete -> "complete"
+ FSCancelled -> "cancelled"
+fileTransferStatus (FTSnd [], _) = ["no file transfers (empty group)"]
+fileTransferStatus (FTSnd fts@(ft : _), chunksNum) =
+ case concatMap membersTransferStatus $ groupBy ((==) `on` fs) $ sortOn fs fts of
+ [membersStatus] -> ["sending " <> sndFile ft <> " " <> membersStatus]
+ membersStatuses -> ("sending " <> sndFile ft <> ": ") : map (" " <>) membersStatuses
+ where
+ fs = fileStatus :: SndFileTransfer -> FileStatus
+ membersTransferStatus [] = []
+ membersTransferStatus ts@(SndFileTransfer {fileStatus, fileSize, chunkSize} : _) = [sndStatus <> ": " <> listMembers ts]
+ where
+ sndStatus = case fileStatus of
+ FSNew -> "not accepted"
+ FSAccepted -> "just started"
+ FSConnected -> "in progress (" <> sShow (sum chunksNum * chunkSize * 100 `div` (toInteger (length chunksNum) * fileSize)) <> "%)"
+ FSComplete -> "complete"
+ FSCancelled -> "cancelled"
+fileTransferStatus (FTRcv ft@RcvFileTransfer {fileId, fileInvitation = FileInvitation {fileSize}, fileStatus, chunkSize}, chunksNum) =
+ ["receiving " <> rcvFile ft <> " " <> rcvStatus]
+ where
+ rcvStatus = case fileStatus of
+ RFSNew -> "not accepted yet, use " <> highlight ("/fr " <> show fileId) <> " to receive file"
+ RFSAccepted _ -> "just started"
+ RFSConnected _ -> "progress " <> fileProgress chunksNum chunkSize fileSize
+ RFSComplete RcvFileInfo {filePath} -> "complete, path: " <> plain filePath
+ RFSCancelled RcvFileInfo {filePath} -> "cancelled, received part path: " <> plain filePath
+
+listMembers :: [SndFileTransfer] -> StyledString
+listMembers = mconcat . intersperse ", " . map (ttyContact . recipientDisplayName)
+
+fileProgress :: [Integer] -> Integer -> Integer -> StyledString
+fileProgress chunksNum chunkSize fileSize =
+ sShow (sum chunksNum * chunkSize * 100 `div` fileSize) <> "% of " <> humanReadableSize fileSize
+
+sndFileSubError :: SndFileTransfer -> ChatError -> [StyledString]
+sndFileSubError SndFileTransfer {fileId, fileName} e =
+ ["sent file " <> sShow fileId <> " (" <> plain fileName <> ") error: " <> sShow e]
+
+rcvFileSubError :: RcvFileTransfer -> ChatError -> [StyledString]
+rcvFileSubError RcvFileTransfer {fileId, fileInvitation = FileInvitation {fileName}} e =
+ ["received file " <> sShow fileId <> " (" <> plain fileName <> ") error: " <> sShow e]
+
+chatError :: ChatError -> [StyledString]
+chatError = \case
+ ChatError err -> case err of
+ CEGroupDuplicateMember c -> ["contact " <> ttyContact c <> " is already in the group"]
+ CEGroupDuplicateMemberId -> ["cannot add member - duplicate member ID"]
+ CEGroupUserRole -> ["you have insufficient permissions for this group command"]
+ CEGroupContactRole c -> ["contact " <> ttyContact c <> " has insufficient permissions for this group action"]
+ CEGroupNotJoined g -> ["you did not join this group, use " <> highlight ("/join #" <> g)]
+ CEGroupMemberNotActive -> ["you cannot invite other members yet, try later"]
+ CEGroupMemberUserRemoved -> ["you are no longer the member of the group"]
+ CEGroupMemberNotFound c -> ["contact " <> ttyContact c <> " is not a group member"]
+ CEGroupInternal s -> ["chat group bug: " <> plain s]
+ CEFileNotFound f -> ["file not found: " <> plain f]
+ CEFileAlreadyReceiving f -> ["file is already accepted: " <> plain f]
+ CEFileAlreadyExists f -> ["file already exists: " <> plain f]
+ CEFileRead f e -> ["cannot read file " <> plain f, sShow e]
+ CEFileWrite f e -> ["cannot write file " <> plain f, sShow e]
+ CEFileSend fileId e -> ["error sending file " <> sShow fileId <> ": " <> sShow e]
+ CEFileRcvChunk e -> ["error receiving file: " <> plain e]
+ CEFileInternal e -> ["file error: " <> plain e]
+ -- e -> ["chat error: " <> sShow e]
+ ChatErrorStore err -> case err of
+ SEDuplicateName -> ["this display name is already used by user, contact or group"]
+ SEContactNotFound c -> ["no contact " <> ttyContact c]
+ SEContactNotReady c -> ["contact " <> ttyContact c <> " is not active yet"]
+ SEGroupNotFound g -> ["no group " <> ttyGroup g]
+ SEGroupAlreadyJoined -> ["you already joined this group"]
+ SEFileNotFound fileId -> fileNotFound fileId
+ SESndFileNotFound fileId -> fileNotFound fileId
+ SERcvFileNotFound fileId -> fileNotFound fileId
+ e -> ["chat db error: " <> sShow e]
+ ChatErrorAgent e -> ["smp agent error: " <> sShow e]
+ ChatErrorMessage e -> ["chat message error: " <> sShow e]
+ where
+ fileNotFound fileId = ["file " <> sShow fileId <> " not found"]
+
+printToView :: (MonadUnliftIO m, MonadReader ChatController m) => [StyledString] -> m ()
+printToView s = asks chatTerminal >>= liftIO . (`printToTerminal` s)
+
+ttyContact :: ContactName -> StyledString
+ttyContact = styled (Colored Green)
+
+ttyFullContact :: Contact -> StyledString
+ttyFullContact Contact {localDisplayName, profile = Profile {fullName}} =
+ ttyFullName localDisplayName fullName
+
+ttyMember :: GroupMember -> StyledString
+ttyMember GroupMember {localDisplayName} = ttyContact localDisplayName
+
+ttyFullMember :: GroupMember -> StyledString
+ttyFullMember GroupMember {localDisplayName, memberProfile = Profile {fullName}} =
+ ttyFullName localDisplayName fullName
+
+ttyFullName :: ContactName -> Text -> StyledString
+ttyFullName c fullName = ttyContact c <> optFullName c fullName
+
+ttyToContact :: ContactName -> StyledString
+ttyToContact c = styled (Colored Cyan) $ "@" <> c <> " "
+
+ttyFromContact :: ContactName -> StyledString
+ttyFromContact c = styled (Colored Yellow) $ c <> "> "
+
+ttyGroup :: GroupName -> StyledString
+ttyGroup g = styled (Colored Blue) $ "#" <> g
+
+ttyFullGroup :: Group -> StyledString
+ttyFullGroup Group {localDisplayName, groupProfile = GroupProfile {fullName}} =
+ ttyGroup localDisplayName <> optFullName localDisplayName fullName
+
+ttyFromGroup :: GroupName -> ContactName -> StyledString
+ttyFromGroup g c = styled (Colored Yellow) $ "#" <> g <> " " <> c <> "> "
+
+ttyToGroup :: GroupName -> StyledString
+ttyToGroup g = styled (Colored Cyan) $ "#" <> g <> " "
+
+ttyFilePath :: FilePath -> StyledString
+ttyFilePath = plain
+
+optFullName :: ContactName -> Text -> StyledString
+optFullName localDisplayName fullName
+ | T.null fullName || localDisplayName == fullName = ""
+ | otherwise = plain (" (" <> fullName <> ")")
+
+highlight :: StyledFormat a => a -> StyledString
+highlight = styled (Colored Cyan)
+
+highlight' :: String -> StyledString
+highlight' = highlight
+
+styleTime :: String -> StyledString
+styleTime = Styled [SetColor Foreground Vivid Black]
diff --git a/packages/simplex_app/haskell/src/Simplex/Chat/protocol.md b/packages/simplex_app/haskell/src/Simplex/Chat/protocol.md
new file mode 100644
index 0000000000..111319c593
--- /dev/null
+++ b/packages/simplex_app/haskell/src/Simplex/Chat/protocol.md
@@ -0,0 +1,168 @@
+# Chat protocol
+
+## Design constraints
+
+- the transport message has a fixed size (8 or 16kb), but the SMP agent will be updated to support sending messages up to maximum configured size (TBC - 64-256kb) in 8-16Kb blocks.
+- the chat message can have multiple content parts, but it should fit the agent message of the variable size.
+- one of the chat message types should support transmitting large binaries in chunks that could potentially be interleaved with other messages. For example, image preview would fit the message, but the full size image will be transmitted in chunks later - same for large files.
+- using object storage can be effective for large groups, but we will postpone it until content channels are implemented.
+
+## Questions
+
+- should content types be:
+ - limited to MIME-types
+ - separate content types vocabulary
+ - both MIME types and extensions (currently we support MIME (m.) and Simplex (x.) namespaces)
+ - allow additional content types namespaces
+
+## Message syntax
+
+The syntax of the message inside agent MSG:
+
+```abnf
+agentMessageBody = [chatMsgId] SP msgEvent SP [parameters] SP [contentParts [SP msgBodyParts]]
+chatMsgId = 1*DIGIT ; used to refer to previous message;
+ ; in the group should only be used in messages sent to all members,
+ ; which is the main reason not to use external agent ID -
+ ; some messages are sent only to one member
+msgEvent = protocolNamespace 1*("." msgTypeName)
+protocolNamespace = 1*ALPHA ; "x" for all events defined in the protocol
+msgTypeName = 1*ALPHA
+parameters = parameter *("," parameter)
+parameter = 1*(%x21-2B / %x2D-7E) ; exclude control characters, space, comma (%x2C)
+contentParts = contentPart *("," contentPart)
+contentPart = contentTypeNamespace "." contentType ":" contentSize [":" contentHash]
+contentType = "i." / contentTypeNamespace "." 1*("." contentTypeName)
+contentTypeNamespace = 1*ALPHA
+contentTypeName = 1*ALPHA
+contentHash =
+msgBodyParts = msgBodyPart *(SP msgBodyPart)
+msgEventParents = msgEventParent *msgEventParent ; binary body part for content type "x.dag"
+msgEventParent = memberId refMsgId refMsgHash
+memberId = 8*8(OCTET) ; shared member ID
+refMsgId = 8*8(OCTET) ; sequential message number - external agent message ID
+refMsgHash = 16*16(OCTET) ; SHA256 of agent message body
+```
+
+### Example: messages, updates, groups
+
+```
+"3 x.msg.new c.text x.text:5 hello "
+"4 x.msg.new c.image i.image/jpg:256,i.image/png:4096 abcd abcd "
+"4 x.msg.new c.image x.dag:32,i.image/jpg:8000,i.image/png:16000 binary1"
+"5 x.msg.new c.image,i.image/jpg:150000 i.image/jpg:256 abcd "
+"6 x.msg.file 5,1.1 x.file:60000 abcd "
+"7 x.msg.file 5,1.2 x.file:60000 abcd "
+"8 x.msg.file 5,1.3 x.file:30000 abcd "
+'8 x.msg.update 3 x.text:11,x.dag:16 hello there abcd '
+'9 x.msg.delete 3'
+'10 x.msg.new app/v1 i.text/html:NNN,i.text/css:NNN,c.js:NNN,c.json:NNN ... ... ... {...} '
+'11 x.msg.eval 8 c.json:NNN {...} '
+'12 x.msg.new c.text x.text:16,x.dag:32 hello there @123 abcd '
+' x.grp.mem.inv 23456,123 x.json:NNN {...} '
+' x.grp.mem.acpt 23456 x.text:NNN '
+' x.grp.mem.intro 23456,234 x.json:NNN {...} '
+' x.grp.mem.inv 23456,234 x.text:NNN '
+' x.grp.mem.req 23456,123 x.json:NNN {...} '
+' x.grp.mem.direct.inv 23456,234 x.text:NNN '
+' x.file name,size x.text:NNN '
+```
+
+### Group protocol
+
+#### Add group member
+
+A -> B: invite to group - `MSG: x.grp.inv G_MEM_ID_A,G_MEM_ROLE_A,G_MEM_ID_B,G_MEM_ROLE_B, x.json:NNN `
+user B confirms
+B -> A: establish group connection (B: JOIN, A: LET)
+B -> Ag: join group - `in SMP confirmation: x.grp.acpt G_MEM_ID_B`
+A -> group (including B)): announce group member: `MSG: N x.grp.mem.new G_MEM_ID_B,G_MEM_ROLE_B,G_MEM_ID_M,... x.json:NNN `
+
+In the message `x.grp.mem.new` A sends the sorted list of all members to whom A is connected followed by the new member ID, role and profile. The following introductions will be sent about/to all members A "knows about" (includes members introduced to A and members who accepted group invitation but not connected yet), once they are connected, so it can be a bigger list than sent in `x.grp.mem.new`.
+
+All members who received `x.grp.mem.new` from A should check the list of connected members and if any connected members that recipients invited to the group are not in this list, they should introduce them to this new member (the last ID, role and profile in `x.grp.mem.new`). That might lead to double introductions that would provide a stronger consistency of group membership at a cost of extra connection between some members that will be unused.
+
+subsequent messages between A and B are via group connection
+A -> Bg: intro member - `MSG: x.grp.mem.intro G_MEM_ID_M,G_MEM_ROLE_M x.json:NNN `
+B -> Ag: inv for mem - `MSG: x.grp.mem.inv G_MEM_ID_M,,,`
+M is an existing member, messages are via group connection
+A -> Mg: fwd inv - `MSG: x.grp.mem.fwd G_MEM_ID_B,,,`
+M -> Bg: establish group connection (M: JOIN, B: LET)
+M -> B: establish direct connection (M: JOIN, B: LET)
+M -> Bg: confirm profile and role - `CONF: x.grp.mem.info G_MEM_ID_M,G_MEM_ROLE x.json:NNN `
+B -> Mg: send profile probe - `MSG: x.info.probe ` - it should always be send, even when there is no profile match.
+if M is a known contact (profile match) send probe to M:
+ B -> M (via old DM conn): profile match probe: `MSG: x.info.probe.check `
+ M -> B (via old DM conn): probe confirm: `MSG: x.info.probe.ok `
+ link to the same contact
+B -> Ag: connected to M: `MSG: x.grp.mem.con G_MEM_ID_M`
+M -> Ag: connected to M: `MSG: x.grp.mem.con G_MEM_ID_B`
+
+once all members connected
+A -> group: `MSG: N x.grp.mem.con.all G_MEM_ID_B`
+
+#### Send group message
+
+Example:
+
+`MSG: N x.msg.new c.text x.text:5 hello `
+
+#### Group member statuses
+
+1. Me
+ - invited
+ - accepted
+ - connected to member who invited me
+ - announced to group
+ - x.grp.mem.new to group
+ - confirmed as connected to group
+ - this happens once member who invited me sends x.grp.mem.ok to group
+1. Member that I invited:
+ - invited
+ - accepted
+ - connected to me
+ - announced to group
+ - this happens after x.grp.mem.new but before introductions are sent.
+ This message is used to determine which members should be additionally introduced if they were announced before (or in "parallel").
+ - confirmed as connected to group
+2. Member who invited me
+ - invited_me
+ - connected to me
+ - I won't know whether this member was announced or confirmed to group - with the correctly functioning clients it must have happened.
+3. Prior member introduced to me after I joined (x.grp.mem.intro)
+ - introduced
+ - sent invitation
+ - connected
+ - connected directly (or confirmed existing contact)
+4. Member I was introduced to after that member joined (via x.grp.mem.fwd)
+ - announced via x.grp.mem.new
+ - received invitation
+ - connected
+ - connected directly (or confirmed existing contact)
+
+#### Introductions
+
+1. Introductions I sent to members I invited
+ - the time of joining is determined by the time of creating the connection and sending the x.grp.mem.new message to the group.
+ - introductions of the members who were connected before the new member should be sent - how to determine which members were connected before?
+ - use time stamp of creating connection, possibly in the member record - not very reliable, as time can change.
+ - use record ID - requires changing the schema, as currently members are added as invited, not as connected. So possibly invited members should be tracked in a separate table, and all members should still be tracked together to ensure that memberId is unique.
+ - record ID is also not 100% sufficient, as there can be forks in message history and I may need to intro the member I invited to the member that was announced after my member in my chronology, but in another graph branch.
+ - some other mechanism that allows to establish who should be connected to whom and whether I should introduce or another member (in case of forks - although maybe we both can introduce and eventually two group connections will be created between these members and they would just ignore the first one - although in cases of multiple branches in the graph it can be N connections).
+ - introductions/member connection statuses:
+ - created introduction
+ - sent to the member I invited
+ - received the invitation from the member I invited
+ - forwarded this invitation to previously connected member
+ - received confirmation from member I invited
+ - received confirmation from member I forwarded to
+ - completed introduction and recorded that these members are now fully connected to each other
+2. Introductions I received from the member who invited me
+ - if somebody else sends such introduction - this is an error (can be logged or ignored)
+ - duplicate memberId is an error (e.g. it is a member that was announced in the group broadcast - I should be introduced to this member, and not the other way around? Although it can happen in case of fork and maybe I should establish the connection anyway).
+ - member connection status in this case is just a member status from part 3, so maybe no need to track invitations separately and just put SMPQueueInfo on member record.
+3. Invitation forwarded to me by any prior member
+ - any admin/owner can add members, so they can forward their queue invitations - I should just check forwarding member permission
+ - duplicate memberId is an error
+ - unannounced memberId is an error - I should have seen member announcement prior to receiving this forwarded invitation. Fork would not happen here as it is the same member that announces and forwards the invitation, so they should be in order.
+ - member connection status in this case is just a member status from part 4, so maybe no need to track invitations separately and just put SMPQueueInfo on member record.
diff --git a/packages/simplex_app/haskell/stack.yaml b/packages/simplex_app/haskell/stack.yaml
new file mode 100644
index 0000000000..f095208f59
--- /dev/null
+++ b/packages/simplex_app/haskell/stack.yaml
@@ -0,0 +1,72 @@
+# This file was automatically generated by 'stack init'
+#
+# Some commonly used options have been documented as comments in this file.
+# For advanced use and comprehensive documentation of the format, please see:
+# https://docs.haskellstack.org/en/stable/yaml_configuration/
+
+# Resolver to choose a 'specific' stackage snapshot or a compiler version.
+# A snapshot resolver dictates the compiler version and the set of packages
+# to be used for project dependencies. For example:
+#
+# resolver: lts-3.5
+# resolver: nightly-2015-09-21
+# resolver: ghc-7.10.2
+#
+# The location of a snapshot can be provided as a file or url. Stack assumes
+# a snapshot provided as a file might change, whereas a url resource does not.
+#
+# resolver: ./custom-snapshot.yaml
+# resolver: https://example.com/snapshots/2018-01-01.yaml
+resolver: lts-17.12
+
+# User packages to be built.
+# Various formats can be used as shown in the example below.
+#
+# packages:
+# - some-directory
+# - https://example.com/foo/bar/baz-0.0.2.tar.gz
+# subdirs:
+# - auto-update
+# - wai
+packages:
+ - .
+# Dependency packages to be pulled from upstream that are not in the resolver.
+# These entries can reference officially published versions as well as
+# forks / in-progress versions pinned to a git hash. For example:
+#
+extra-deps:
+ - cryptostore-0.2.1.0@sha256:9896e2984f36a1c8790f057fd5ce3da4cbcaf8aa73eb2d9277916886978c5b19,3881
+ - direct-sqlite-2.3.26@sha256:04e835402f1508abca383182023e4e2b9b86297b8533afbd4e57d1a5652e0c23,3718
+ - simple-logger-0.1.0@sha256:be8ede4bd251a9cac776533bae7fb643369ebd826eb948a9a18df1a8dd252ff8,1079
+ - sqlite-simple-0.4.18.0@sha256:3ceea56375c0a3590c814e411a4eb86943f8d31b93b110ca159c90689b6b39e5,3002
+ - terminal-0.2.0.0@sha256:de6770ecaae3197c66ac1f0db5a80cf5a5b1d3b64a66a05b50f442de5ad39570,2977
+ - simplexmq-0.4.1@sha256:3a1bc40d85e4e398458e5b9b79757e0af4fe27b8ef44eb3157f7f1e07412a8e8,7640
+ # - ../simplexmq
+ # - github: simplex-chat/simplexmq
+ # commit: 35e6593581e68f7b444e0f8f4fb6a2e2cc59a5ea
+#
+# extra-deps: []
+
+# Override default flag values for local packages and extra-deps
+# flags: {}
+
+# Extra package databases containing global packages
+# extra-package-dbs: []
+
+# Control whether we use the GHC we find on the path
+# system-ghc: true
+#
+# Require a specific version of stack, using version ranges
+# require-stack-version: -any # Default
+# require-stack-version: ">=2.1"
+#
+# Override the architecture used by stack, especially useful on Windows
+# arch: i386
+# arch: x86_64
+#
+# Extra directories used by stack for building
+# extra-include-dirs: [/path/to/dir]
+# extra-lib-dirs: [/path/to/dir]
+#
+# Allow a newer minor version of GHC than the snapshot specifies
+# compiler-check: newer-minor
diff --git a/packages/simplex_app/haskell/tests/ChatClient.hs b/packages/simplex_app/haskell/tests/ChatClient.hs
new file mode 100644
index 0000000000..eff8ff6c10
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/ChatClient.hs
@@ -0,0 +1,199 @@
+{-# LANGUAGE DuplicateRecordFields #-}
+{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedLists #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE TypeApplications #-}
+
+module ChatClient where
+
+import Control.Concurrent (ThreadId, forkIOWithUnmask, killThread)
+import Control.Concurrent.Async
+import Control.Concurrent.STM
+import Control.Exception (bracket, bracket_)
+import Control.Monad.Except
+import Data.List (dropWhileEnd)
+import Network.Socket
+import Simplex.Chat
+import Simplex.Chat.Controller (ChatConfig (..), ChatController (..))
+import Simplex.Chat.Options
+import Simplex.Chat.Store
+import Simplex.Chat.Types (Profile)
+import Simplex.Messaging.Agent.Env.SQLite
+import Simplex.Messaging.Agent.RetryInterval
+import Simplex.Messaging.Server (runSMPServerBlocking)
+import Simplex.Messaging.Server.Env.STM
+import Simplex.Messaging.Transport
+import System.Directory (createDirectoryIfMissing, removeDirectoryRecursive)
+import qualified System.Terminal as C
+import System.Terminal.Internal (VirtualTerminal (..), VirtualTerminalSettings (..), withVirtualTerminal)
+import System.Timeout (timeout)
+
+testDBPrefix :: FilePath
+testDBPrefix = "tests/tmp/test"
+
+serverPort :: ServiceName
+serverPort = "5000"
+
+opts :: ChatOpts
+opts =
+ ChatOpts
+ { dbFile = undefined,
+ smpServers = ["localhost:5000"]
+ }
+
+termSettings :: VirtualTerminalSettings
+termSettings =
+ VirtualTerminalSettings
+ { virtualType = "xterm",
+ virtualWindowSize = pure C.Size {height = 24, width = 1000},
+ virtualEvent = retry,
+ virtualInterrupt = retry
+ }
+
+data TestCC = TestCC
+ { chatController :: ChatController,
+ virtualTerminal :: VirtualTerminal,
+ chatAsync :: Async (),
+ termAsync :: Async (),
+ termQ :: TQueue String
+ }
+
+aCfg :: AgentConfig
+aCfg = agentConfig defaultChatConfig
+
+cfg :: ChatConfig
+cfg =
+ defaultChatConfig
+ { agentConfig =
+ aCfg {retryInterval = (retryInterval aCfg) {initialInterval = 50000}}
+ }
+
+virtualSimplexChat :: FilePath -> Profile -> IO TestCC
+virtualSimplexChat dbFile profile = do
+ st <- createStore (dbFile <> ".chat.db") 1
+ void . runExceptT $ createUser st profile True
+ t <- withVirtualTerminal termSettings pure
+ cc <- newChatController cfg opts {dbFile} t . const $ pure () -- no notifications
+ chatAsync <- async $ runSimplexChat cc
+ termQ <- newTQueueIO
+ termAsync <- async $ readTerminalOutput t termQ
+ pure TestCC {chatController = cc, virtualTerminal = t, chatAsync, termAsync, termQ}
+
+readTerminalOutput :: VirtualTerminal -> TQueue String -> IO ()
+readTerminalOutput t termQ = do
+ let w = virtualWindow t
+ winVar <- atomically $ newTVar . init =<< readTVar w
+ forever . atomically $ do
+ win <- readTVar winVar
+ win' <- init <$> readTVar w
+ if win' == win
+ then retry
+ else do
+ let diff = getDiff win' win
+ forM_ diff $ writeTQueue termQ
+ writeTVar winVar win'
+ where
+ getDiff :: [String] -> [String] -> [String]
+ getDiff win win' = getDiff_ 1 (length win) win win'
+ getDiff_ :: Int -> Int -> [String] -> [String] -> [String]
+ getDiff_ n len win' win =
+ let diff = drop (len - n) win'
+ in if drop n win <> diff == win'
+ then map (dropWhileEnd (== ' ')) diff
+ else getDiff_ (n + 1) len win' win
+
+testChatN :: [Profile] -> ([TestCC] -> IO ()) -> IO ()
+testChatN ps test =
+ bracket_
+ (createDirectoryIfMissing False "tests/tmp")
+ (removeDirectoryRecursive "tests/tmp")
+ $ do
+ let envs = zip ps $ map ((testDBPrefix <>) . show) [(1 :: Int) ..]
+ tcs <- getTestCCs envs []
+ test tcs
+ where
+ getTestCCs [] tcs = pure tcs
+ getTestCCs ((p, db) : envs') tcs = (:) <$> virtualSimplexChat db p <*> getTestCCs envs' tcs
+
+testChat2 :: Profile -> Profile -> (TestCC -> TestCC -> IO ()) -> IO ()
+testChat2 p1 p2 test = testChatN [p1, p2] test_
+ where
+ test_ :: [TestCC] -> IO ()
+ test_ [tc1, tc2] = test tc1 tc2
+ test_ _ = error "expected 2 chat clients"
+
+testChat3 :: Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
+testChat3 p1 p2 p3 test = testChatN [p1, p2, p3] test_
+ where
+ test_ :: [TestCC] -> IO ()
+ test_ [tc1, tc2, tc3] = test tc1 tc2 tc3
+ test_ _ = error "expected 3 chat clients"
+
+testChat4 :: Profile -> Profile -> Profile -> Profile -> (TestCC -> TestCC -> TestCC -> TestCC -> IO ()) -> IO ()
+testChat4 p1 p2 p3 p4 test = testChatN [p1, p2, p3, p4] test_
+ where
+ test_ :: [TestCC] -> IO ()
+ test_ [tc1, tc2, tc3, tc4] = test tc1 tc2 tc3 tc4
+ test_ _ = error "expected 4 chat clients"
+
+concurrentlyN_ :: [IO a] -> IO ()
+concurrentlyN_ = mapConcurrently_ id
+
+serverCfg :: ServerConfig
+serverCfg =
+ ServerConfig
+ { transports = [(serverPort, transport @TCP)],
+ tbqSize = 1,
+ msgQueueQuota = 4,
+ queueIdBytes = 12,
+ msgIdBytes = 6,
+ storeLog = Nothing,
+ blockSize = 4096,
+ serverPrivateKey =
+ -- full RSA private key (only for tests)
+ "MIIFIwIBAAKCAQEArZyrri/NAwt5buvYjwu+B/MQeJUszDBpRgVqNddlI9kNwDXu\
+ \kaJ8chEhrtaUgXeSWGooWwqjXEUQE6RVbCC6QVo9VEBSP4xFwVVd9Fj7OsgfcXXh\
+ \AqWxfctDcBZQ5jTUiJpdBc+Vz2ZkumVNl0W+j9kWm9nfkMLQj8c0cVSDxz4OKpZb\
+ \qFuj0uzHkis7e7wsrKSKWLPg3M5ZXPZM1m9qn7SfJzDRDfJifamxWI7uz9XK2+Dp\
+ \NkUQlGQgFJEv1cKN88JAwIqZ1s+TAQMQiB+4QZ2aNfSqGEzRJN7FMCKRK7pM0A9A\
+ \PCnijyuImvKFxTdk8Bx1q+XNJzsY6fBrLWJZ+QKBgQCySG4tzlcEm+tOVWRcwrWh\
+ \6zsczGZp9mbf9c8itRx6dlldSYuDG1qnddL70wuAZF2AgS1JZgvcRZECoZRoWP5q\
+ \Kq2wvpTIYjFPpC39lxgUoA/DXKVKZZdan+gwaVPAPT54my1CS32VrOiAY4gVJ3LJ\
+ \Mn1/FqZXUFQA326pau3loQKCAQEAoljmJMp88EZoy3HlHUbOjl5UEhzzVsU1TnQi\
+ \QmPm+aWRe2qelhjW4aTvSVE5mAUJsN6UWTeMf4uvM69Z9I5pfw2pEm8x4+GxRibY\
+ \iiwF2QNaLxxmzEHm1zQQPTgb39o8mgklhzFPill0JsnL3f6IkVwjFJofWSmpqEGs\
+ \dFSMRSXUTVXh1p/o7QZrhpwO/475iWKVS7o48N/0Xp513re3aXw+DRNuVnFEaBIe\
+ \TLvWM9Czn16ndAu1HYiTBuMvtRbAWnGZxU8ewzF4wlWK5tdIL5PTJDd1VhZJAKtB\
+ \npDvJpwxzKmjAhcTmjx0ckMIWtdVaOVm/2gWCXDty2FEdg7koQKBgQDOUUguJ/i7\
+ \q0jldWYRnVkotKnpInPdcEaodrehfOqYEHnvro9xlS6OeAS4Vz5AdH45zQ/4J3bV\
+ \2cH66tNr18ebM9nL//t5G69i89R9W7szyUxCI3LmAIdi3oSEbmz5GQBaw4l6h9Wi\
+ \n4FmFQaAXZrjQfO2qJcAHvWRsMp2pmqAGwKBgQDXaza0DRsKWywWznsHcmHa0cx8\
+ \I4jxqGaQmLO7wBJRP1NSFrywy1QfYrVX9CTLBK4V3F0PCgZ01Qv94751CzN43TgF\
+ \ebd/O9r5NjNTnOXzdWqETbCffLGd6kLgCMwPQWpM9ySVjXHWCGZsRAnF2F6M1O32\
+ \43StIifvwJQFqSM3ewKBgCaW6y7sRY90Ua7283RErezd9EyT22BWlDlACrPu3FNC\
+ \LtBf1j43uxBWBQrMLsHe2GtTV0xt9m0MfwZsm2gSsXcm4Xi4DJgfN+Z7rIlyy9UY\
+ \PCDSdZiU1qSr+NrffDrXlfiAM1cUmCdUX7eKjp/ltkUHNaOGfSn5Pdr3MkAiD/Hf\
+ \AoGBAKIdKCuOwuYlwjS9J+IRGuSSM4o+OxQdwGmcJDTCpyWb5dEk68e7xKIna3zf\
+ \jc+H+QdMXv1nkRK9bZgYheXczsXaNZUSTwpxaEldzVD3hNvsXSgJRy9fqHwA4PBq\
+ \vqiBHoO3RNbqg+2rmTMfDuXreME3S955ZiPZm4Z+T8Hj52mPAoGAQm5QH/gLFtY5\
+ \+znqU/0G8V6BKISCQMxbbmTQVcTgGySrP2gVd+e4MWvUttaZykhWqs8rpr7mgpIY\
+ \hul7Swx0SHFN3WpXu8uj+B6MLpRcCbDHO65qU4kQLs+IaXXsuuTjMvJ5LwjkZVrQ\
+ \TmKzSAw7iVWwEUZR/PeiEKazqrpp9VU="
+ }
+
+withSmpServer :: IO a -> IO a
+withSmpServer = serverBracket (`runSMPServerBlocking` serverCfg) (pure ()) . const
+
+serverBracket :: (TMVar Bool -> IO ()) -> IO () -> (ThreadId -> IO a) -> IO a
+serverBracket process afterProcess f = do
+ started <- newEmptyTMVarIO
+ bracket
+ (forkIOWithUnmask ($ process started))
+ (\t -> killThread t >> afterProcess >> waitFor started "stop")
+ (\t -> waitFor started "start" >> f t)
+ where
+ waitFor started s =
+ 5000000 `timeout` atomically (takeTMVar started) >>= \case
+ Nothing -> error $ "server did not " <> s
+ _ -> pure ()
diff --git a/packages/simplex_app/haskell/tests/ChatTests.hs b/packages/simplex_app/haskell/tests/ChatTests.hs
new file mode 100644
index 0000000000..ad1c521ad8
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/ChatTests.hs
@@ -0,0 +1,667 @@
+{-# LANGUAGE NamedFieldPuns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE PostfixOperators #-}
+
+module ChatTests where
+
+import ChatClient
+import Control.Concurrent.Async (concurrently_)
+import Control.Concurrent.STM
+import qualified Data.ByteString as B
+import Data.Char (isDigit)
+import qualified Data.Text as T
+import Simplex.Chat.Controller
+import Simplex.Chat.Types (Profile (..), User (..))
+import Simplex.Chat.Util (unlessM)
+import System.Directory (doesFileExist)
+import System.Timeout (timeout)
+import Test.Hspec
+
+aliceProfile :: Profile
+aliceProfile = Profile {displayName = "alice", fullName = "Alice"}
+
+bobProfile :: Profile
+bobProfile = Profile {displayName = "bob", fullName = "Bob"}
+
+cathProfile :: Profile
+cathProfile = Profile {displayName = "cath", fullName = "Catherine"}
+
+danProfile :: Profile
+danProfile = Profile {displayName = "dan", fullName = "Daniel"}
+
+chatTests :: Spec
+chatTests = do
+ describe "direct messages" $
+ it "add contact and send/receive message" testAddContact
+ describe "chat groups" $ do
+ it "add contacts, create group and send/receive messages" testGroup
+ it "create and join group with 4 members" testGroup2
+ it "create and delete group" testGroupDelete
+ it "remove contact from group and add again" testGroupRemoveAdd
+ describe "user profiles" $
+ it "update user profiles and notify contacts" testUpdateProfile
+ describe "sending and receiving files" $ do
+ it "send and receive file" testFileTransfer
+ it "send and receive a small file" testSmallFileTransfer
+ it "sender cancelled file transfer" testFileSndCancel
+ it "recipient cancelled file transfer" testFileRcvCancel
+ it "send and receive file to group" testGroupFileTransfer
+
+testAddContact :: IO ()
+testAddContact =
+ testChat2 aliceProfile bobProfile $
+ \alice bob -> do
+ alice ##> "/c"
+ inv <- getInvitation alice
+ bob ##> ("/c " <> inv)
+ concurrently_
+ (bob <## "alice (Alice): contact is connected")
+ (alice <## "bob (Bob): contact is connected")
+ alice #> "@bob hello"
+ bob <# "alice> hello"
+ bob #> "@alice hi"
+ alice <# "bob> hi"
+ -- test adding the same contact one more time - local name will be different
+ alice ##> "/c"
+ inv' <- getInvitation alice
+ bob ##> ("/c " <> inv')
+ concurrently_
+ (bob <## "alice_1 (Alice): contact is connected")
+ (alice <## "bob_1 (Bob): contact is connected")
+ alice #> "@bob_1 hello"
+ bob <# "alice_1> hello"
+ bob #> "@alice_1 hi"
+ alice <# "bob_1> hi"
+ -- test deleting contact
+ alice ##> "/d bob_1"
+ alice <## "bob_1: contact is deleted"
+ alice #> "@bob_1 hey"
+ alice <## "no contact bob_1"
+
+testGroup :: IO ()
+testGroup =
+ testChat3 aliceProfile bobProfile cathProfile $
+ \alice bob cath -> do
+ connectUsers alice bob
+ connectUsers alice cath
+ alice ##> "/g team"
+ alice <## "group #team is created"
+ alice <## "use /a team to add members"
+ alice ##> "/a team bob"
+ concurrentlyN_
+ [ alice <## "invitation to join the group #team sent to bob",
+ do
+ bob <## "#team: alice invites you to join the group as admin"
+ bob <## "use /j team to accept"
+ ]
+ bob ##> "/j team"
+ concurrently_
+ (alice <## "#team: bob joined the group")
+ (bob <## "#team: you joined the group")
+ alice ##> "/a team cath"
+ concurrentlyN_
+ [ alice <## "invitation to join the group #team sent to cath",
+ do
+ cath <## "#team: alice invites you to join the group as admin"
+ cath <## "use /j team to accept"
+ ]
+ cath ##> "/j team"
+ concurrentlyN_
+ [ alice <## "#team: cath joined the group",
+ do
+ cath <## "#team: you joined the group"
+ cath <## "#team: member bob (Bob) is connected",
+ do
+ bob <## "#team: alice added cath (Catherine) to the group (connecting...)"
+ bob <## "#team: new member cath is connected"
+ ]
+ alice #> "#team hello"
+ concurrently_
+ (bob <# "#team alice> hello")
+ (cath <# "#team alice> hello")
+ bob #> "#team hi there"
+ concurrently_
+ (alice <# "#team bob> hi there")
+ (cath <# "#team bob> hi there")
+ cath #> "#team hey"
+ concurrently_
+ (alice <# "#team cath> hey")
+ (bob <# "#team cath> hey")
+ bob <##> cath
+ -- remove member
+ bob ##> "/rm team cath"
+ concurrentlyN_
+ [ bob <## "#team: you removed cath from the group",
+ alice <## "#team: bob removed cath from the group",
+ do
+ cath <## "#team: bob removed you from the group"
+ cath <## "use /d #team to delete the group"
+ ]
+ bob #> "#team hi"
+ concurrently_
+ (alice <# "#team bob> hi")
+ (cath )
+ alice #> "#team hello"
+ concurrently_
+ (bob <# "#team alice> hello")
+ (cath )
+ cath #> "#team hello"
+ cath <## "you are no longer the member of the group"
+ bob <##> cath
+
+testGroup2 :: IO ()
+testGroup2 =
+ testChat4 aliceProfile bobProfile cathProfile danProfile $
+ \alice bob cath dan -> do
+ connectUsers alice bob
+ connectUsers alice cath
+ connectUsers bob dan
+ connectUsers alice dan
+ alice ##> "/g club"
+ alice <## "group #club is created"
+ alice <## "use /a club to add members"
+ alice ##> "/a club bob"
+ concurrentlyN_
+ [ alice <## "invitation to join the group #club sent to bob",
+ do
+ bob <## "#club: alice invites you to join the group as admin"
+ bob <## "use /j club to accept"
+ ]
+ alice ##> "/a club cath"
+ concurrentlyN_
+ [ alice <## "invitation to join the group #club sent to cath",
+ do
+ cath <## "#club: alice invites you to join the group as admin"
+ cath <## "use /j club to accept"
+ ]
+ bob ##> "/j club"
+ concurrently_
+ (alice <## "#club: bob joined the group")
+ (bob <## "#club: you joined the group")
+ cath ##> "/j club"
+ concurrentlyN_
+ [ alice <## "#club: cath joined the group",
+ do
+ cath <## "#club: you joined the group"
+ cath <## "#club: member bob (Bob) is connected",
+ do
+ bob <## "#club: alice added cath (Catherine) to the group (connecting...)"
+ bob <## "#club: new member cath is connected"
+ ]
+ bob ##> "/a club dan"
+ concurrentlyN_
+ [ bob <## "invitation to join the group #club sent to dan",
+ do
+ dan <## "#club: bob invites you to join the group as admin"
+ dan <## "use /j club to accept"
+ ]
+ dan ##> "/j club"
+ concurrentlyN_
+ [ bob <## "#club: dan joined the group",
+ do
+ dan <## "#club: you joined the group"
+ dan
+ <### [ "#club: member alice_1 (Alice) is connected",
+ "contact alice_1 is merged into alice",
+ "use @alice to send messages",
+ "#club: member cath (Catherine) is connected"
+ ],
+ do
+ alice <## "#club: bob added dan_1 (Daniel) to the group (connecting...)"
+ alice <## "#club: new member dan_1 is connected"
+ alice <## "contact dan_1 is merged into dan"
+ alice <## "use @dan to send messages",
+ do
+ cath <## "#club: bob added dan (Daniel) to the group (connecting...)"
+ cath <## "#club: new member dan is connected"
+ ]
+ alice #> "#club hello"
+ concurrentlyN_
+ [ bob <# "#club alice> hello",
+ cath <# "#club alice> hello",
+ dan <# "#club alice> hello"
+ ]
+ bob #> "#club hi there"
+ concurrentlyN_
+ [ alice <# "#club bob> hi there",
+ cath <# "#club bob> hi there",
+ dan <# "#club bob> hi there"
+ ]
+ cath #> "#club hey"
+ concurrentlyN_
+ [ alice <# "#club cath> hey",
+ bob <# "#club cath> hey",
+ dan <# "#club cath> hey"
+ ]
+ dan #> "#club how is it going?"
+ concurrentlyN_
+ [ alice <# "#club dan> how is it going?",
+ bob <# "#club dan> how is it going?",
+ cath <# "#club dan> how is it going?"
+ ]
+ bob <##> cath
+ dan <##> cath
+ dan <##> alice
+ -- remove member
+ cath ##> "/rm club dan"
+ concurrentlyN_
+ [ cath <## "#club: you removed dan from the group",
+ alice <## "#club: cath removed dan from the group",
+ bob <## "#club: cath removed dan from the group",
+ do
+ dan <## "#club: cath removed you from the group"
+ dan <## "use /d #club to delete the group"
+ ]
+ alice #> "#club hello"
+ concurrentlyN_
+ [ bob <# "#club alice> hello",
+ cath <# "#club alice> hello",
+ (dan )
+ ]
+ bob #> "#club hi there"
+ concurrentlyN_
+ [ alice <# "#club bob> hi there",
+ cath <# "#club bob> hi there",
+ (dan )
+ ]
+ cath #> "#club hey"
+ concurrentlyN_
+ [ alice <# "#club cath> hey",
+ bob <# "#club cath> hey",
+ (dan )
+ ]
+ dan #> "#club how is it going?"
+ dan <## "you are no longer the member of the group"
+ dan <##> cath
+ dan <##> alice
+ -- member leaves
+ bob ##> "/l club"
+ concurrentlyN_
+ [ do
+ bob <## "#club: you left the group"
+ bob <## "use /d #club to delete the group",
+ alice <## "#club: bob left the group",
+ cath <## "#club: bob left the group"
+ ]
+ alice #> "#club hello"
+ concurrently_
+ (cath <# "#club alice> hello")
+ (bob )
+ cath #> "#club hey"
+ concurrently_
+ (alice <# "#club cath> hey")
+ (bob )
+ bob #> "#club how is it going?"
+ bob <## "you are no longer the member of the group"
+ bob <##> cath
+ bob <##> alice
+
+testGroupDelete :: IO ()
+testGroupDelete =
+ testChat3 aliceProfile bobProfile cathProfile $
+ \alice bob cath -> do
+ createGroup3 "team" alice bob cath
+ alice ##> "/d #team"
+ concurrentlyN_
+ [ alice <## "#team: you deleted the group",
+ do
+ bob <## "#team: alice deleted the group"
+ bob <## "use /d #team to delete the local copy of the group",
+ do
+ cath <## "#team: alice deleted the group"
+ cath <## "use /d #team to delete the local copy of the group"
+ ]
+ bob ##> "/d #team"
+ bob <## "#team: you deleted the group"
+ cath #> "#team hi"
+ cath <## "you are no longer the member of the group"
+
+testGroupRemoveAdd :: IO ()
+testGroupRemoveAdd =
+ testChat3 aliceProfile bobProfile cathProfile $
+ \alice bob cath -> do
+ createGroup3 "team" alice bob cath
+ -- remove member
+ alice ##> "/rm team bob"
+ concurrentlyN_
+ [ alice <## "#team: you removed bob from the group",
+ do
+ bob <## "#team: alice removed you from the group"
+ bob <## "use /d #team to delete the group",
+ cath <## "#team: alice removed bob from the group"
+ ]
+ alice ##> "/a team bob"
+ alice <## "invitation to join the group #team sent to bob"
+ bob <## "#team_1 (team): alice invites you to join the group as admin"
+ bob <## "use /j team_1 to accept"
+ bob ##> "/j team_1"
+ concurrentlyN_
+ [ alice <## "#team: bob joined the group",
+ do
+ bob <## "#team_1: you joined the group"
+ bob <## "#team_1: member cath_1 (Catherine) is connected"
+ bob <## "contact cath_1 is merged into cath"
+ bob <## "use @cath to send messages",
+ do
+ cath <## "#team: alice added bob_1 (Bob) to the group (connecting...)"
+ cath <## "#team: new member bob_1 is connected"
+ cath <## "contact bob_1 is merged into bob"
+ cath <## "use @bob to send messages"
+ ]
+ alice #> "#team hi"
+ concurrently_
+ (bob <# "#team_1 alice> hi")
+ (cath <# "#team alice> hi")
+ bob #> "#team_1 hey"
+ concurrently_
+ (alice <# "#team bob> hey")
+ (cath <# "#team bob> hey")
+ cath #> "#team hello"
+ concurrently_
+ (alice <# "#team cath> hello")
+ (bob <# "#team_1 cath> hello")
+
+testUpdateProfile :: IO ()
+testUpdateProfile =
+ testChat3 aliceProfile bobProfile cathProfile $
+ \alice bob cath -> do
+ createGroup3 "team" alice bob cath
+ alice ##> "/p"
+ alice <## "user profile: alice (Alice)"
+ alice <## "use /p [] to change it"
+ alice <## "(the updated profile will be sent to all your contacts)"
+ alice ##> "/p alice"
+ concurrentlyN_
+ [ alice <## "user full name removed (your contacts are notified)",
+ bob <## "contact alice removed full name",
+ cath <## "contact alice removed full name"
+ ]
+ alice ##> "/p alice Alice Jones"
+ concurrentlyN_
+ [ alice <## "user full name changed to Alice Jones (your contacts are notified)",
+ bob <## "contact alice updated full name: Alice Jones",
+ cath <## "contact alice updated full name: Alice Jones"
+ ]
+ cath ##> "/p cate"
+ concurrentlyN_
+ [ cath <## "user profile is changed to cate (your contacts are notified)",
+ do
+ alice <## "contact cath changed to cate"
+ alice <## "use @cate to send messages",
+ do
+ bob <## "contact cath changed to cate"
+ bob <## "use @cate to send messages"
+ ]
+ cath ##> "/p cat Cate"
+ concurrentlyN_
+ [ cath <## "user profile is changed to cat (Cate) (your contacts are notified)",
+ do
+ alice <## "contact cate changed to cat (Cate)"
+ alice <## "use @cat to send messages",
+ do
+ bob <## "contact cate changed to cat (Cate)"
+ bob <## "use @cat to send messages"
+ ]
+
+testFileTransfer :: IO ()
+testFileTransfer =
+ testChat2 aliceProfile bobProfile $
+ \alice bob -> do
+ connectUsers alice bob
+ startFileTransfer alice bob
+ concurrentlyN_
+ [ do
+ bob #> "@alice receiving here..."
+ bob <## "completed receiving file 1 (test.jpg) from alice",
+ do
+ alice <# "bob> receiving here..."
+ alice <## "completed sending file 1 (test.jpg) to bob"
+ ]
+ src <- B.readFile "./tests/fixtures/test.jpg"
+ dest <- B.readFile "./tests/tmp/test.jpg"
+ dest `shouldBe` src
+
+testSmallFileTransfer :: IO ()
+testSmallFileTransfer =
+ testChat2 aliceProfile bobProfile $
+ \alice bob -> do
+ connectUsers alice bob
+ alice #> "/f @bob ./tests/fixtures/test.txt"
+ alice <## "use /fc 1 to cancel sending"
+ bob <# "alice> sends file test.txt (11 bytes / 11 bytes)"
+ bob <## "use /fr 1 [/ | ] to receive it"
+ bob ##> "/fr 1 ./tests/tmp"
+ bob <## "saving file 1 from alice to ./tests/tmp/test.txt"
+ concurrentlyN_
+ [ do
+ bob <## "started receiving file 1 (test.txt) from alice"
+ bob <## "completed receiving file 1 (test.txt) from alice",
+ do
+ alice <## "started sending file 1 (test.txt) to bob"
+ alice <## "completed sending file 1 (test.txt) to bob"
+ ]
+ src <- B.readFile "./tests/fixtures/test.txt"
+ dest <- B.readFile "./tests/tmp/test.txt"
+ dest `shouldBe` src
+
+testFileSndCancel :: IO ()
+testFileSndCancel =
+ testChat2 aliceProfile bobProfile $
+ \alice bob -> do
+ connectUsers alice bob
+ startFileTransfer alice bob
+ alice ##> "/fc 1"
+ concurrentlyN_
+ [ do
+ alice <## "cancelled sending file 1 (test.jpg) to bob"
+ alice ##> "/fs 1"
+ alice <## "sending file 1 (test.jpg) cancelled",
+ do
+ bob <## "alice cancelled sending file 1 (test.jpg)"
+ bob ##> "/fs 1"
+ bob <## "receiving file 1 (test.jpg) cancelled, received part path: ./tests/tmp/test.jpg"
+ ]
+ checkPartialTransfer
+
+testFileRcvCancel :: IO ()
+testFileRcvCancel =
+ testChat2 aliceProfile bobProfile $
+ \alice bob -> do
+ connectUsers alice bob
+ startFileTransfer alice bob
+ bob ##> "/fs 1"
+ getTermLine bob >>= (`shouldStartWith` "receiving file 1 (test.jpg) progress")
+ waitFileExists "./tests/tmp/test.jpg"
+ bob ##> "/fc 1"
+ concurrentlyN_
+ [ do
+ bob <## "cancelled receiving file 1 (test.jpg) from alice"
+ bob ##> "/fs 1"
+ bob <## "receiving file 1 (test.jpg) cancelled, received part path: ./tests/tmp/test.jpg",
+ do
+ alice <## "bob cancelled receiving file 1 (test.jpg)"
+ alice ##> "/fs 1"
+ alice <## "sending file 1 (test.jpg) cancelled"
+ ]
+ checkPartialTransfer
+ where
+ waitFileExists f = unlessM (doesFileExist f) $ waitFileExists f
+
+testGroupFileTransfer :: IO ()
+testGroupFileTransfer =
+ testChat3 aliceProfile bobProfile cathProfile $
+ \alice bob cath -> do
+ createGroup3 "team" alice bob cath
+ alice #> "/f #team ./tests/fixtures/test.jpg"
+ alice <## "use /fc 1 to cancel sending"
+ concurrentlyN_
+ [ do
+ bob <# "#team alice> sends file test.jpg (136.5 KiB / 139737 bytes)"
+ bob <## "use /fr 1 [/ | ] to receive it",
+ do
+ cath <# "#team alice> sends file test.jpg (136.5 KiB / 139737 bytes)"
+ cath <## "use /fr 1 [/ | ] to receive it"
+ ]
+ alice ##> "/fs 1"
+ getTermLine alice >>= (`shouldStartWith` "sending file 1 (test.jpg) not accepted")
+ bob ##> "/fr 1 ./tests/tmp/"
+ bob <## "saving file 1 from alice to ./tests/tmp/test.jpg"
+ concurrentlyN_
+ [ do
+ alice <## "started sending file 1 (test.jpg) to bob"
+ alice <## "completed sending file 1 (test.jpg) to bob"
+ alice ##> "/fs 1"
+ alice <## "sending file 1 (test.jpg):"
+ alice <### [" complete: bob", " not accepted: cath"],
+ do
+ bob <## "started receiving file 1 (test.jpg) from alice"
+ bob <## "completed receiving file 1 (test.jpg) from alice"
+ ]
+ cath ##> "/fr 1 ./tests/tmp/"
+ cath <## "saving file 1 from alice to ./tests/tmp/test_1.jpg"
+ concurrentlyN_
+ [ do
+ alice <## "started sending file 1 (test.jpg) to cath"
+ alice <## "completed sending file 1 (test.jpg) to cath"
+ alice ##> "/fs 1"
+ getTermLine alice >>= (`shouldStartWith` "sending file 1 (test.jpg) complete"),
+ do
+ cath <## "started receiving file 1 (test.jpg) from alice"
+ cath <## "completed receiving file 1 (test.jpg) from alice"
+ ]
+
+startFileTransfer :: TestCC -> TestCC -> IO ()
+startFileTransfer alice bob = do
+ alice #> "/f @bob ./tests/fixtures/test.jpg"
+ alice <## "use /fc 1 to cancel sending"
+ bob <# "alice> sends file test.jpg (136.5 KiB / 139737 bytes)"
+ bob <## "use /fr 1 [/ | ] to receive it"
+ bob ##> "/fr 1 ./tests/tmp"
+ bob <## "saving file 1 from alice to ./tests/tmp/test.jpg"
+ concurrently_
+ (bob <## "started receiving file 1 (test.jpg) from alice")
+ (alice <## "started sending file 1 (test.jpg) to bob")
+
+checkPartialTransfer :: IO ()
+checkPartialTransfer = do
+ src <- B.readFile "./tests/fixtures/test.jpg"
+ dest <- B.readFile "./tests/tmp/test.jpg"
+ B.unpack src `shouldStartWith` B.unpack dest
+ B.length src > B.length dest `shouldBe` True
+
+connectUsers :: TestCC -> TestCC -> IO ()
+connectUsers cc1 cc2 = do
+ name1 <- showName cc1
+ name2 <- showName cc2
+ cc1 ##> "/c"
+ inv <- getInvitation cc1
+ cc2 ##> ("/c " <> inv)
+ concurrently_
+ (cc2 <## (name1 <> ": contact is connected"))
+ (cc1 <## (name2 <> ": contact is connected"))
+
+showName :: TestCC -> IO String
+showName (TestCC ChatController {currentUser} _ _ _ _) = do
+ User {localDisplayName, profile = Profile {fullName}} <- readTVarIO currentUser
+ pure . T.unpack $ localDisplayName <> " (" <> fullName <> ")"
+
+createGroup3 :: String -> TestCC -> TestCC -> TestCC -> IO ()
+createGroup3 gName cc1 cc2 cc3 = do
+ connectUsers cc1 cc2
+ connectUsers cc1 cc3
+ name2 <- userName cc2
+ name3 <- userName cc3
+ sName2 <- showName cc2
+ sName3 <- showName cc3
+ cc1 ##> ("/g " <> gName)
+ cc1 <## ("group #" <> gName <> " is created")
+ cc1 <## ("use /a " <> gName <> " to add members")
+ addMember cc2
+ cc2 ##> ("/j " <> gName)
+ concurrently_
+ (cc1 <## ("#" <> gName <> ": " <> name2 <> " joined the group"))
+ (cc2 <## ("#" <> gName <> ": you joined the group"))
+ addMember cc3
+ cc3 ##> ("/j " <> gName)
+ concurrentlyN_
+ [ cc1 <## ("#" <> gName <> ": " <> name3 <> " joined the group"),
+ do
+ cc3 <## ("#" <> gName <> ": you joined the group")
+ cc3 <## ("#" <> gName <> ": member " <> sName2 <> " is connected"),
+ do
+ cc2 <## ("#" <> gName <> ": alice added " <> sName3 <> " to the group (connecting...)")
+ cc2 <## ("#" <> gName <> ": new member " <> name3 <> " is connected")
+ ]
+ where
+ addMember :: TestCC -> IO ()
+ addMember mem = do
+ name1 <- userName cc1
+ memName <- userName mem
+ cc1 ##> ("/a " <> gName <> " " <> memName)
+ concurrentlyN_
+ [ cc1 <## ("invitation to join the group #" <> gName <> " sent to " <> memName),
+ do
+ mem <## ("#" <> gName <> ": " <> name1 <> " invites you to join the group as admin")
+ mem <## ("use /j " <> gName <> " to accept")
+ ]
+
+-- | test sending direct messages
+(<##>) :: TestCC -> TestCC -> IO ()
+cc1 <##> cc2 = do
+ name1 <- userName cc1
+ name2 <- userName cc2
+ cc1 #> ("@" <> name2 <> " hi")
+ cc2 <# (name1 <> "> hi")
+ cc2 #> ("@" <> name1 <> " hey")
+ cc1 <# (name2 <> "> hey")
+
+userName :: TestCC -> IO [Char]
+userName (TestCC ChatController {currentUser} _ _ _ _) = T.unpack . localDisplayName <$> readTVarIO currentUser
+
+(##>) :: TestCC -> String -> IO ()
+cc ##> cmd = do
+ cc `send` cmd
+ cc <## cmd
+
+(#>) :: TestCC -> String -> IO ()
+cc #> cmd = do
+ cc `send` cmd
+ cc <# cmd
+
+send :: TestCC -> String -> IO ()
+send TestCC {chatController = cc} cmd = atomically $ writeTBQueue (inputQ cc) $ InputCommand cmd
+
+(<##) :: TestCC -> String -> Expectation
+cc <## line = getTermLine cc `shouldReturn` line
+
+(<###) :: TestCC -> [String] -> Expectation
+_ <### [] = pure ()
+cc <### ls = do
+ line <- getTermLine cc
+ if line `elem` ls
+ then cc <### filter (/= line) ls
+ else error $ "unexpected output: " <> line
+
+(<#) :: TestCC -> String -> Expectation
+cc <# line = (dropTime <$> getTermLine cc) `shouldReturn` line
+
+() :: TestCC -> Expectation
+() cc = timeout 500000 (getTermLine cc) `shouldReturn` Nothing
+
+dropTime :: String -> String
+dropTime msg = case splitAt 6 msg of
+ ([m, m', ':', s, s', ' '], text) ->
+ if all isDigit [m, m', s, s'] then text else error "invalid time"
+ _ -> error "invalid time"
+
+getTermLine :: TestCC -> IO String
+getTermLine = atomically . readTQueue . termQ
+
+getInvitation :: TestCC -> IO String
+getInvitation cc = do
+ cc <## "pass this invitation to your contact (via another channel):"
+ cc <## ""
+ inv <- getTermLine cc
+ cc <## ""
+ cc <## "and ask them to connect: /c "
+ pure inv
diff --git a/packages/simplex_app/haskell/tests/MarkdownTests.hs b/packages/simplex_app/haskell/tests/MarkdownTests.hs
new file mode 100644
index 0000000000..e236307b81
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/MarkdownTests.hs
@@ -0,0 +1,128 @@
+{-# LANGUAGE BlockArguments #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module MarkdownTests where
+
+import Data.Text (Text)
+import Simplex.Chat.Markdown
+import System.Console.ANSI.Types
+import Test.Hspec
+
+markdownTests :: Spec
+markdownTests = do
+ textFormat
+ secretText
+ textColor
+
+textFormat :: Spec
+textFormat = describe "text format (bold)" do
+ it "correct markdown" do
+ parseMarkdown "this is *bold formatted* text"
+ `shouldBe` "this is " <> Markdown Bold "bold formatted" <> " " <> "text"
+ parseMarkdown "*bold formatted* text"
+ `shouldBe` Markdown Bold "bold formatted" <> " " <> "text"
+ parseMarkdown "this is *bold*"
+ `shouldBe` "this is " <> Markdown Bold "bold"
+ parseMarkdown " *bold* text"
+ `shouldBe` " " <> Markdown Bold "bold" <> " " <> "text"
+ parseMarkdown " *bold* text"
+ `shouldBe` " " <> Markdown Bold "bold" <> " " <> "text"
+ parseMarkdown "this is *bold* "
+ `shouldBe` "this is " <> Markdown Bold "bold" <> " "
+ parseMarkdown "this is *bold* "
+ `shouldBe` "this is " <> Markdown Bold "bold" <> " "
+ it "ignored as markdown" do
+ parseMarkdown "this is * unformatted * text"
+ `shouldBe` "this is " <> "* unformatted *" <> " " <> "text"
+ parseMarkdown "this is *unformatted * text"
+ `shouldBe` "this is " <> "*unformatted *" <> " " <> "text"
+ parseMarkdown "this is * unformatted* text"
+ `shouldBe` "this is " <> "* unformatted*" <> " " <> "text"
+ parseMarkdown "this is **unformatted** text"
+ `shouldBe` "this is " <> "**" <> "unformatted** text"
+ parseMarkdown "this is*unformatted* text"
+ `shouldBe` "this is*unformatted* text"
+ parseMarkdown "this is *unformatted text"
+ `shouldBe` "this is " <> "*unformatted text"
+ it "ignored internal markdown" do
+ parseMarkdown "this is *long _bold_ (not italic)* text"
+ `shouldBe` "this is " <> Markdown Bold "long _bold_ (not italic)" <> " " <> "text"
+ parseMarkdown "snippet: `this is *bold text*`"
+ `shouldBe` "snippet: " <> Markdown Snippet "this is *bold text*"
+
+secretText :: Spec
+secretText = describe "secret text" do
+ it "correct markdown" do
+ parseMarkdown "this is #black_secret# text"
+ `shouldBe` "this is " <> Markdown Secret "black_secret" <> " " <> "text"
+ parseMarkdown "##black_secret### text"
+ `shouldBe` Markdown Secret "#black_secret##" <> " " <> "text"
+ parseMarkdown "this is #black secret# text"
+ `shouldBe` "this is " <> Markdown Secret "black secret" <> " " <> "text"
+ parseMarkdown "##black secret### text"
+ `shouldBe` Markdown Secret "#black secret##" <> " " <> "text"
+ parseMarkdown "this is #secret#"
+ `shouldBe` "this is " <> Markdown Secret "secret"
+ parseMarkdown " #secret# text"
+ `shouldBe` " " <> Markdown Secret "secret" <> " " <> "text"
+ parseMarkdown " #secret# text"
+ `shouldBe` " " <> Markdown Secret "secret" <> " " <> "text"
+ parseMarkdown "this is #secret# "
+ `shouldBe` "this is " <> Markdown Secret "secret" <> " "
+ parseMarkdown "this is #secret# "
+ `shouldBe` "this is " <> Markdown Secret "secret" <> " "
+ it "ignored as markdown" do
+ parseMarkdown "this is # unformatted # text"
+ `shouldBe` "this is " <> "# unformatted #" <> " " <> "text"
+ parseMarkdown "this is #unformatted # text"
+ `shouldBe` "this is " <> "#unformatted #" <> " " <> "text"
+ parseMarkdown "this is # unformatted# text"
+ `shouldBe` "this is " <> "# unformatted#" <> " " <> "text"
+ parseMarkdown "this is ## unformatted ## text"
+ `shouldBe` "this is " <> "## unformatted ##" <> " " <> "text"
+ parseMarkdown "this is#unformatted# text"
+ `shouldBe` "this is#unformatted# text"
+ parseMarkdown "this is #unformatted text"
+ `shouldBe` "this is " <> "#unformatted text"
+ it "ignored internal markdown" do
+ parseMarkdown "snippet: `this is #secret_text#`"
+ `shouldBe` "snippet: " <> Markdown Snippet "this is #secret_text#"
+
+red :: Text -> Markdown
+red = Markdown (Colored Red)
+
+textColor :: Spec
+textColor = describe "text color (red)" do
+ it "correct markdown" do
+ parseMarkdown "this is !1 red color! text"
+ `shouldBe` "this is " <> red "red color" <> " " <> "text"
+ parseMarkdown "!1 red! text"
+ `shouldBe` red "red" <> " " <> "text"
+ parseMarkdown "this is !1 red!"
+ `shouldBe` "this is " <> red "red"
+ parseMarkdown " !1 red! text"
+ `shouldBe` " " <> red "red" <> " " <> "text"
+ parseMarkdown " !1 red! text"
+ `shouldBe` " " <> red "red" <> " " <> "text"
+ parseMarkdown "this is !1 red! "
+ `shouldBe` "this is " <> red "red" <> " "
+ parseMarkdown "this is !1 red! "
+ `shouldBe` "this is " <> red "red" <> " "
+ it "ignored as markdown" do
+ parseMarkdown "this is !1 unformatted ! text"
+ `shouldBe` "this is " <> "!1 unformatted !" <> " " <> "text"
+ parseMarkdown "this is !1 unformatted ! text"
+ `shouldBe` "this is " <> "!1 unformatted !" <> " " <> "text"
+ parseMarkdown "this is !1 unformatted! text"
+ `shouldBe` "this is " <> "!1 unformatted!" <> " " <> "text"
+ -- parseMarkdown "this is !!1 unformatted!! text"
+ -- `shouldBe` "this is " <> "!!1" <> "unformatted!! text"
+ parseMarkdown "this is!1 unformatted! text"
+ `shouldBe` "this is!1 unformatted! text"
+ parseMarkdown "this is !1 unformatted text"
+ `shouldBe` "this is " <> "!1 unformatted text"
+ it "ignored internal markdown" do
+ parseMarkdown "this is !1 long *red* (not bold)! text"
+ `shouldBe` "this is " <> red "long *red* (not bold)" <> " " <> "text"
+ parseMarkdown "snippet: `this is !1 red text!`"
+ `shouldBe` "snippet: " <> Markdown Snippet "this is !1 red text!"
diff --git a/packages/simplex_app/haskell/tests/ProtocolTests.hs b/packages/simplex_app/haskell/tests/ProtocolTests.hs
new file mode 100644
index 0000000000..3a7b5d0902
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/ProtocolTests.hs
@@ -0,0 +1,47 @@
+{-# LANGUAGE OverloadedLists #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module ProtocolTests where
+
+import Data.ByteString.Char8 (ByteString)
+import Simplex.Chat.Protocol
+import Simplex.Messaging.Parsers (parseAll)
+import Test.Hspec
+
+protocolTests :: Spec
+protocolTests = do
+ parseChatMessageTest
+
+(#==) :: ByteString -> RawChatMessage -> Expectation
+s #== msg = parseAll rawChatMessageP s `shouldBe` Right msg
+
+parseChatMessageTest :: Spec
+parseChatMessageTest = describe "Raw chat message format" $ do
+ it "no parameters and content" $
+ "5 x.grp.mem.leave " #== RawChatMessage (Just 5) "x.grp.mem.leave" [] []
+ it "one parameter, no content" $
+ "6 x.msg.del 3 " #== RawChatMessage (Just 6) "x.msg.del" ["3"] []
+ it "with content that fits the message" $
+ "7 x.msg.new c.text x.text:11 hello there "
+ #== RawChatMessage
+ (Just 7)
+ "x.msg.new"
+ ["c.text"]
+ [RawMsgBodyContent (RawContentType "x" "text") "hello there"]
+ it "with DAG reference and partial content" $
+ "8 x.msg.new c.image x.dag:16,x.text:7,m.image/jpg:6 0123456789012345 picture abcdef "
+ #== RawChatMessage
+ (Just 8)
+ "x.msg.new"
+ ["c.image"]
+ [ RawMsgBodyContent (RawContentType "x" "dag") "0123456789012345",
+ RawMsgBodyContent (RawContentType "x" "text") "picture",
+ RawMsgBodyContent (RawContentType "m" "image/jpg") "abcdef"
+ ]
+ it "without message id" $
+ " x.grp.mem.inv 23456,123 x.json:46 {\"contactRef\":\"john\",\"displayName\":\"John Doe\"} "
+ #== RawChatMessage
+ Nothing
+ "x.grp.mem.inv"
+ ["23456", "123"]
+ [RawMsgBodyContent (RawContentType "x" "json") "{\"contactRef\":\"john\",\"displayName\":\"John Doe\"}"]
diff --git a/packages/simplex_app/haskell/tests/Test.hs b/packages/simplex_app/haskell/tests/Test.hs
new file mode 100644
index 0000000000..961475ab38
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/Test.hs
@@ -0,0 +1,11 @@
+import ChatClient
+import ChatTests
+import MarkdownTests
+import ProtocolTests
+import Test.Hspec
+
+main :: IO ()
+main = withSmpServer . hspec $ do
+ describe "SimpleX chat markdown" markdownTests
+ describe "SimpleX chat protocol" protocolTests
+ describe "SimpleX chat client" chatTests
diff --git a/packages/simplex_app/haskell/tests/fixtures/test.jpg b/packages/simplex_app/haskell/tests/fixtures/test.jpg
new file mode 100644
index 0000000000..4a11030592
Binary files /dev/null and b/packages/simplex_app/haskell/tests/fixtures/test.jpg differ
diff --git a/packages/simplex_app/haskell/tests/fixtures/test.txt b/packages/simplex_app/haskell/tests/fixtures/test.txt
new file mode 100644
index 0000000000..d2aeeec3fa
--- /dev/null
+++ b/packages/simplex_app/haskell/tests/fixtures/test.txt
@@ -0,0 +1 @@
+hello there
\ No newline at end of file
diff --git a/packages/simplex_app/ios/Runner.xcodeproj/project.pbxproj b/packages/simplex_app/ios/Runner.xcodeproj/project.pbxproj
index 2c60f32934..f3fbcb8a28 100644
--- a/packages/simplex_app/ios/Runner.xcodeproj/project.pbxproj
+++ b/packages/simplex_app/ios/Runner.xcodeproj/project.pbxproj
@@ -291,7 +291,7 @@
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
- PRODUCT_BUNDLE_IDENTIFIER = chat.simplex.app;
+ PRODUCT_BUNDLE_IDENTIFIER = com.example.simplexChat;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
@@ -415,7 +415,7 @@
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
- PRODUCT_BUNDLE_IDENTIFIER = chat.simplex.app;
+ PRODUCT_BUNDLE_IDENTIFIER = com.example.simplexChat;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@@ -434,7 +434,7 @@
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
- PRODUCT_BUNDLE_IDENTIFIER = chat.simplex.app;
+ PRODUCT_BUNDLE_IDENTIFIER = com.example.simplexChat;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
diff --git a/packages/simplex_app/lib/app_routes.dart b/packages/simplex_app/lib/app_routes.dart
new file mode 100644
index 0000000000..d7df18dd6c
--- /dev/null
+++ b/packages/simplex_app/lib/app_routes.dart
@@ -0,0 +1,4 @@
+class AppRoutes {
+ static final intro = '/intro';
+ static final setupProfile = '/setupProfile';
+}
diff --git a/packages/simplex_app/lib/constants.dart b/packages/simplex_app/lib/constants.dart
new file mode 100644
index 0000000000..8a7234fe4a
--- /dev/null
+++ b/packages/simplex_app/lib/constants.dart
@@ -0,0 +1,22 @@
+import 'package:flutter/material.dart';
+
+// colors
+const kPrimaryColor = Color(0xff062d56);
+const kSecondaryColor = Color(0xff07b4b9);
+
+// text styles
+const TextStyle kHeadingStyle = TextStyle(
+ fontSize: 28.0,
+ fontWeight: FontWeight.bold,
+ letterSpacing: 1.3,
+);
+
+const TextStyle kMediumHeadingStyle = TextStyle(
+ fontSize: 20.0,
+ fontWeight: FontWeight.w500,
+);
+
+const TextStyle kSmallHeadingStyle = TextStyle(
+ fontSize: 18.0,
+ fontWeight: FontWeight.w500,
+);
diff --git a/packages/simplex_app/lib/main.dart b/packages/simplex_app/lib/main.dart
index d5542cb127..098d02410c 100644
--- a/packages/simplex_app/lib/main.dart
+++ b/packages/simplex_app/lib/main.dart
@@ -1,115 +1,55 @@
-import "package:flutter/material.dart";
+import 'package:flutter/material.dart';
+import 'package:simplex_chat/app_routes.dart';
+import 'package:simplex_chat/constants.dart';
+import 'package:simplex_chat/views/onBoarding/intro_view.dart';
+import 'package:simplex_chat/views/setup_profile_view.dart';
void main() {
+ WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
- // This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
- title: "Flutter Demo",
+ debugShowCheckedModeBanner: false,
+ title: 'SimpleX Chat',
theme: ThemeData(
- // This is the theme of your application.
- //
- // Try running your application with "flutter run". You'll see the
- // application has a blue toolbar. Then, without quitting the app, try
- // changing the primarySwatch below to Colors.green and then invoke
- // "hot reload" (press "r" in the console where you ran "flutter run",
- // or simply save your changes to "hot reload" in a Flutter IDE).
- // Notice that the counter didn't reset back to zero; the application
- // is not restarted.
- primarySwatch: Colors.blue,
+ primarySwatch: Colors.teal,
+ primaryColor: kPrimaryColor,
+ accentColor: kPrimaryColor,
),
- home: const MyHomePage(title: "Flutter Demo Home Page"),
+ builder: (context, widget) {
+ return ScrollConfiguration(
+ behavior: ScrollBehaviorModified(),
+ child: widget!,
+ );
+ },
+ initialRoute: AppRoutes.intro,
+ routes: {
+ AppRoutes.intro: (_) => IntroView(),
+ AppRoutes.setupProfile: (_) => SetupProfileView(),
+ },
);
}
}
-class MyHomePage extends StatefulWidget {
- const MyHomePage({Key? key, required this.title}) : super(key: key);
-
- // This widget is the home page of your application. It is stateful, meaning
- // that it has a State object (defined below) that contains fields that affect
- // how it looks.
-
- // This class is the configuration for the state. It holds the values (in this
- // case the title) provided by the parent (in this case the App widget) and
- // used by the build method of the State. Fields in a Widget subclass are
- // always marked "final".
-
- final String title;
-
+class ScrollBehaviorModified extends ScrollBehavior {
+ const ScrollBehaviorModified();
@override
- State createState() => _MyHomePageState();
-}
-
-class _MyHomePageState extends State {
- int _counter = 0;
-
- void _incrementCounter() {
- setState(() {
- // This call to setState tells the Flutter framework that something has
- // changed in this State, which causes it to rerun the build method below
- // so that the display can reflect the updated values. If we changed
- // _counter without calling setState(), then the build method would not be
- // called again, and so nothing would appear to happen.
- _counter++;
- });
- }
-
- @override
- Widget build(BuildContext context) {
- // This method is rerun every time setState is called, for instance as done
- // by the _incrementCounter method above.
- //
- // The Flutter framework has been optimized to make rerunning build methods
- // fast, so that you can just rebuild anything that needs updating rather
- // than having to individually change instances of widgets.
- return Scaffold(
- appBar: AppBar(
- // Here we take the value from the MyHomePage object that was created by
- // the App.build method, and use it to set our appbar title.
- title: Text(widget.title),
- ),
- body: Center(
- // Center is a layout widget. It takes a single child and positions it
- // in the middle of the parent.
- child: Column(
- // Column is also a layout widget. It takes a list of children and
- // arranges them vertically. By default, it sizes itself to fit its
- // children horizontally, and tries to be as tall as its parent.
- //
- // Invoke "debug painting" (press "p" in the console, choose the
- // "Toggle Debug Paint" action from the Flutter Inspector in Android
- // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
- // to see the wireframe for each widget.
- //
- // Column has various properties to control how it sizes itself and
- // how it positions its children. Here we use mainAxisAlignment to
- // center the children vertically; the main axis here is the vertical
- // axis because Columns are vertical (the cross axis would be
- // horizontal).
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Text(
- "You have pushed the button this many times:",
- ),
- Text(
- "$_counter",
- style: Theme.of(context).textTheme.headline4,
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: _incrementCounter,
- tooltip: "Increment",
- child: const Icon(Icons.add),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
+ ScrollPhysics getScrollPhysics(BuildContext context) {
+ switch (getPlatform(context)) {
+ case TargetPlatform.iOS:
+ case TargetPlatform.macOS:
+ case TargetPlatform.android:
+ return const BouncingScrollPhysics();
+ case TargetPlatform.fuchsia:
+ case TargetPlatform.linux:
+ case TargetPlatform.windows:
+ return const ClampingScrollPhysics();
+ }
}
}
diff --git a/packages/simplex_app/lib/views/home/drawer.dart b/packages/simplex_app/lib/views/home/drawer.dart
new file mode 100644
index 0000000000..79b8199e19
--- /dev/null
+++ b/packages/simplex_app/lib/views/home/drawer.dart
@@ -0,0 +1,54 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_svg/flutter_svg.dart';
+
+class MyDrawer extends StatelessWidget {
+ const MyDrawer({Key? key}) : super(key: key);
+
+ @override
+ Widget build(BuildContext context) {
+ return SizedBox(
+ width: MediaQuery.of(context).size.width * 0.82,
+ child: Material(
+ color: Colors.white,
+ child: Padding(
+ padding: const EdgeInsets.all(10.0),
+ child: Column(
+ children: [
+ const SizedBox(height: 30.0),
+ SvgPicture.asset(
+ 'assets/logo.svg',
+ height: 50.0,
+ ),
+ const Divider(height: 50.0),
+ ListTile(
+ leading: const Icon(Icons.contact_phone),
+ title: const Text('Your contacts'),
+ subtitle: const Text('Start a conversation right away!'),
+ onTap: () {},
+ ),
+ ListTile(
+ leading: const Icon(Icons.insert_invitation),
+ title: const Text('Invitations'),
+ subtitle: const Text('Increase your contact circle!'),
+ onTap: () {},
+ ),
+ ListTile(
+ leading: const Icon(Icons.group),
+ title: const Text('Your groups'),
+ subtitle: const Text('Get in touch with numbers!'),
+ onTap: () {},
+ ),
+ Spacer(),
+ ListTile(
+ leading: const Icon(Icons.exit_to_app_rounded),
+ title: const Text('Logout'),
+ subtitle: const Text('Good bye! See you soon :)'),
+ onTap: () => Navigator.pop(context),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/packages/simplex_app/lib/views/home/home_view.dart b/packages/simplex_app/lib/views/home/home_view.dart
new file mode 100644
index 0000000000..9bb71c3286
--- /dev/null
+++ b/packages/simplex_app/lib/views/home/home_view.dart
@@ -0,0 +1,124 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_svg/flutter_svg.dart';
+import 'dart:math' as math;
+
+import 'package:simplex_chat/views/home/drawer.dart';
+import 'package:simplex_chat/views/home/home_view_widget.dart';
+
+class HomeView extends StatefulWidget {
+ final double? maxSlide;
+ const HomeView({
+ Key? key,
+ this.maxSlide,
+ }) : super(key: key);
+
+ @override
+ _HomeViewState createState() => _HomeViewState();
+}
+
+class _HomeViewState extends State with TickerProviderStateMixin {
+ AnimationController? animationController;
+ bool? _canBeDragged;
+
+ void toggle() => animationController!.isDismissed
+ ? animationController!.forward()
+ : animationController!.reverse();
+
+ @override
+ void initState() {
+ super.initState();
+ animationController =
+ AnimationController(vsync: this, duration: Duration(milliseconds: 250));
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return GestureDetector(
+ onHorizontalDragStart: _onDragStart,
+ onHorizontalDragUpdate: _onDragUpdate,
+ onHorizontalDragEnd: _onDragEnd,
+ behavior: HitTestBehavior.translucent,
+ child: AnimatedBuilder(
+ animation: animationController!,
+ builder: (context, _) {
+ return Material(
+ color: Colors.white70,
+ child: SafeArea(
+ child: Stack(
+ children: [
+ Transform.translate(
+ offset: Offset(
+ widget.maxSlide! * (animationController!.value - 1), 0),
+ child: Transform(
+ transform: Matrix4.identity()
+ ..setEntry(3, 2, 0.001)
+ ..rotateY(
+ math.pi / 2 * (1 - animationController!.value)),
+ alignment: Alignment.centerRight,
+ child: MyDrawer(),
+ ),
+ ),
+ Transform.translate(
+ offset: Offset(
+ widget.maxSlide! * animationController!.value, 0),
+ child: Transform(
+ transform: Matrix4.identity()
+ ..setEntry(3, 2, 0.001)
+ ..rotateY(-math.pi / 2 * animationController!.value),
+ alignment: Alignment.centerLeft,
+ child: HomeViewWidget()),
+ ),
+ Positioned(
+ top: MediaQuery.of(context).padding.top,
+ left: MediaQuery.of(context).size.width * 0.03 +
+ animationController!.value * widget.maxSlide!,
+ child: InkWell(
+ onTap: toggle,
+ child: Padding(
+ padding: const EdgeInsets.all(8.0),
+ child: SvgPicture.asset(
+ 'assets/menu.svg',
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ },
+ ),
+ );
+ }
+
+ void _onDragStart(DragStartDetails details) {
+ bool isDragOpenFromLeft = animationController!.isDismissed;
+ bool isDragCloseFromRight = animationController!.isCompleted;
+ _canBeDragged = isDragOpenFromLeft || isDragCloseFromRight;
+ }
+
+ void _onDragUpdate(DragUpdateDetails details) {
+ if (_canBeDragged!) {
+ double delta = details.primaryDelta! / widget.maxSlide!;
+ animationController!.value += delta;
+ }
+ }
+
+ void _onDragEnd(DragEndDetails details) {
+ double _kMinFlingVelocity = 365.0;
+
+ if (animationController!.isDismissed || animationController!.isCompleted) {
+ return;
+ }
+ if (details.velocity.pixelsPerSecond.dx.abs() >= _kMinFlingVelocity) {
+ double visualVelocity = details.velocity.pixelsPerSecond.dx /
+ MediaQuery.of(context).size.width;
+
+ animationController!.fling(velocity: visualVelocity);
+ } else if (animationController!.value < 0.5) {
+ animationController!.reverse();
+ } else {
+ animationController!.forward();
+ }
+ }
+}
diff --git a/packages/simplex_app/lib/views/home/home_view_widget.dart b/packages/simplex_app/lib/views/home/home_view_widget.dart
new file mode 100644
index 0000000000..124633d5fe
--- /dev/null
+++ b/packages/simplex_app/lib/views/home/home_view_widget.dart
@@ -0,0 +1,78 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_svg/flutter_svg.dart';
+import 'package:simplex_chat/constants.dart';
+
+class HomeViewWidget extends StatelessWidget {
+ const HomeViewWidget({Key? key}) : super(key: key);
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ backgroundColor: Colors.white,
+ body: SingleChildScrollView(
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
+ child: Center(
+ child: Column(
+ children: [
+ Align(
+ alignment: Alignment.centerRight,
+ child: SvgPicture.asset(
+ 'assets/logo.svg',
+ height: 40.0,
+ ),
+ ),
+ const SizedBox(height: 30.0),
+ Container(
+ height: MediaQuery.of(context).size.height * 0.7,
+ child: Center(
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ const Text(
+ "You don't have any conversation yet!",
+ style: kMediumHeadingStyle,
+ textAlign: TextAlign.center,
+ ),
+ const SizedBox(height: 8.0),
+ const Text(
+ "Click the icon below to add a contact",
+ textAlign: TextAlign.center,
+ ),
+ ],
+ ),
+ ),
+ )
+ ],
+ ),
+ ),
+ ),
+ ),
+ floatingActionButton: PopupMenuButton(
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(5.0),
+ ),
+ offset: Offset(-10, -180),
+ itemBuilder: (context) => [
+ 'Add contact',
+ 'Scan invitation',
+ 'New group',
+ ]
+ .map(
+ (opt) => PopupMenuItem(
+ value: opt,
+ child: Text(opt),
+ ),
+ )
+ .toList(),
+ child: FloatingActionButton(
+ heroTag: 'connect',
+ onPressed: null,
+ child: const Icon(
+ Icons.person_add,
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/packages/simplex_app/lib/views/onBoarding/intro_view.dart b/packages/simplex_app/lib/views/onBoarding/intro_view.dart
new file mode 100644
index 0000000000..aabd4ece32
--- /dev/null
+++ b/packages/simplex_app/lib/views/onBoarding/intro_view.dart
@@ -0,0 +1,41 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_svg/flutter_svg.dart';
+import 'package:simplex_chat/app_routes.dart';
+import 'package:simplex_chat/constants.dart';
+
+class IntroView extends StatelessWidget {
+ const IntroView({Key? key}) : super(key: key);
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ body: SafeArea(
+ child: Padding(
+ padding: const EdgeInsets.all(12.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ SvgPicture.asset(
+ 'assets/logo.svg',
+ height: 80.0,
+ ),
+ const SizedBox(height: 50.0),
+ const Text(
+ 'Complete your profile to begin using SimpleX. Your profile is local to your device and will help identify you to your connections.',
+ style: kMediumHeadingStyle,
+ )
+ ],
+ ),
+ ),
+ ),
+ floatingActionButton: FloatingActionButton(
+ heroTag: 'welcome',
+ onPressed: () => Navigator.pushNamed(context, AppRoutes.setupProfile),
+ child: const Icon(
+ Icons.arrow_forward,
+ ),
+ ),
+ );
+ }
+}
diff --git a/packages/simplex_app/lib/views/setup_profile_view.dart b/packages/simplex_app/lib/views/setup_profile_view.dart
new file mode 100644
index 0000000000..2b4924bdce
--- /dev/null
+++ b/packages/simplex_app/lib/views/setup_profile_view.dart
@@ -0,0 +1,291 @@
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+import 'package:image_picker/image_picker.dart';
+import 'package:simplex_chat/constants.dart';
+import 'package:simplex_chat/views/home/home_view.dart';
+import 'package:simplex_chat/widgets/customTextField.dart';
+
+class SetupProfileView extends StatefulWidget {
+ const SetupProfileView({Key? key}) : super(key: key);
+
+ @override
+ _SetupProfileViewState createState() => _SetupProfileViewState();
+}
+
+class _SetupProfileViewState extends State {
+ final _formKey = GlobalKey();
+ // controllers
+ final TextEditingController _displayNameController = TextEditingController();
+ final TextEditingController _fullNameController = TextEditingController();
+
+ @override
+ void dispose() {
+ _displayNameController.dispose();
+ _fullNameController.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ body: GestureDetector(
+ onTap: () => FocusScope.of(context).unfocus(),
+ child: SafeArea(
+ child: SingleChildScrollView(
+ child: Padding(
+ padding: const EdgeInsets.all(12.0),
+ child: Form(
+ key: _formKey,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Align(
+ alignment: Alignment.centerLeft,
+ child: BackButton(
+ onPressed: () => Navigator.pop(context),
+ ),
+ ),
+ Center(child: UserProfilePic()),
+ const SizedBox(height: 25.0),
+ const Text('Display Name', style: kSmallHeadingStyle),
+ const SizedBox(height: 10.0),
+ CustomTextField(
+ textEditingController: _displayNameController,
+ textInputType: TextInputType.name,
+ hintText: 'e.g John',
+ validatorFtn: (value) {
+ if (value!.isEmpty) {
+ return "Display name cannot be empty!";
+ }
+ return null;
+ },
+ ),
+ const SizedBox(height: 25.0),
+ const Text('Full Name', style: kSmallHeadingStyle),
+ const SizedBox(height: 10.0),
+ CustomTextField(
+ textEditingController: _fullNameController,
+ textInputType: TextInputType.name,
+ hintText: 'e.g John Doe',
+ validatorFtn: (value) {
+ if (value!.isEmpty) {
+ return "Full name cannot be empty!";
+ }
+ return null;
+ },
+ ),
+ const SizedBox(height: 25.0),
+ const Text(
+ 'Your display name is what your contact will know you :)',
+ style: TextStyle(letterSpacing: 1.2),
+ )
+ ],
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ floatingActionButton: FloatingActionButton(
+ heroTag: 'setup',
+ onPressed: () {
+ if (_formKey.currentState!.validate()) {
+ FocusScope.of(context).unfocus();
+ Navigator.push(
+ context,
+ MaterialPageRoute(
+ builder: (_) => HomeView(
+ maxSlide: MediaQuery.of(context).size.width * 0.82,
+ ),
+ ),
+ );
+ }
+ },
+ child: const Icon(Icons.check),
+ ),
+ );
+ }
+}
+
+class UserProfilePic extends StatefulWidget {
+ const UserProfilePic({Key? key}) : super(key: key);
+
+ @override
+ _UserProfilePicState createState() => _UserProfilePicState();
+}
+
+class _UserProfilePicState extends State {
+ // Image Picker --> DP properties
+ final imgPicker = ImagePicker();
+ File? image;
+ String photoUrl = "";
+ bool _uploading = false;
+ bool _imageUploaded = false;
+
+ // image buttons options
+ final _dpBtnText = ["Gallery", "Camera"];
+ final _dpBtnColors = [Colors.purple, Colors.green];
+ final _dpBtnIcons = [Icons.photo_rounded, Icons.camera_alt_rounded];
+
+ @override
+ Widget build(BuildContext context) {
+ return Container(
+ height: 180.0,
+ width: 180.0,
+ child: Stack(
+ children: [
+ _imageUploaded
+ ? CircleAvatar(
+ radius: 100.0,
+ backgroundImage: FileImage(image!),
+ )
+ : CircleAvatar(
+ radius: 100.0,
+ backgroundImage: AssetImage('assets/dp.png'),
+ ),
+ Positioned(
+ right: 0,
+ bottom: 0,
+ child: FloatingActionButton(
+ backgroundColor: kSecondaryColor,
+ elevation: 2.0,
+ mini: true,
+ onPressed: _updateProfilePic,
+ child: _uploading
+ ? SizedBox(
+ height: 18.0,
+ width: 18.0,
+ child: CircularProgressIndicator(
+ strokeWidth: 2.0,
+ valueColor: AlwaysStoppedAnimation(Colors.white),
+ ),
+ )
+ : const Icon(
+ Icons.add_a_photo,
+ size: 20,
+ ),
+ ),
+ )
+ ],
+ ),
+ );
+ }
+
+ void _updateProfilePic() {
+ showModalBottomSheet(
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.only(
+ topLeft: Radius.circular(10.0),
+ topRight: Radius.circular(10.0),
+ ),
+ ),
+ context: context,
+ builder: (context) => Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 20.0),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Container(
+ decoration: BoxDecoration(
+ color: Colors.grey,
+ borderRadius: BorderRadius.circular(360.0)),
+ height: 7.0,
+ width: 50.0,
+ ),
+ const SizedBox(height: 20.0),
+ Align(
+ alignment: Alignment.centerLeft,
+ child: Text(
+ " Profile photo",
+ style: kHeadingStyle,
+ ),
+ ),
+ const SizedBox(height: 15.0),
+ Row(
+ children: List.generate(
+ 2,
+ (index) => Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ MaterialButton(
+ color: _dpBtnColors.map((e) => e).elementAt(index),
+ shape: CircleBorder(),
+ onPressed:
+ index == 0 ? () => _galleryPic() : () => _cameraPic(),
+ child: Icon(
+ _dpBtnIcons.map((e) => e).elementAt(index),
+ color: Colors.white,
+ ),
+ ),
+ Text(
+ _dpBtnText.map((e) => e).elementAt(index),
+ textAlign: TextAlign.center,
+ )
+ ],
+ ),
+ ))
+ ],
+ ),
+ ),
+ );
+ }
+
+ void _cameraPic() async {
+ try {
+ setState(() {
+ _uploading = true;
+ });
+
+ // picking Image from Camera
+ final file = await imgPicker.getImage(
+ source: ImageSource.camera,
+ );
+
+ if (file != null) {
+ image = File(file.path);
+ setState(() {
+ _uploading = false;
+ _imageUploaded = true;
+ });
+ } else {
+ setState(() {
+ _uploading = false;
+ });
+ }
+
+ Navigator.pop(context);
+ } catch (e) {
+ throw e;
+ }
+ }
+
+ void _galleryPic() async {
+ try {
+ setState(() {
+ _uploading = true;
+ });
+
+ // picking Image from local storage
+ final file = await imgPicker.getImage(
+ source: ImageSource.gallery,
+ );
+
+ if (file != null) {
+ image = File(file.path);
+ setState(() {
+ _uploading = false;
+ _imageUploaded = true;
+ });
+ } else {
+ setState(() {
+ _uploading = false;
+ });
+ }
+
+ Navigator.pop(context);
+ } catch (e) {
+ throw e;
+ }
+ }
+}
diff --git a/packages/simplex_app/lib/widgets/customTextField.dart b/packages/simplex_app/lib/widgets/customTextField.dart
new file mode 100644
index 0000000000..b51d66a32f
--- /dev/null
+++ b/packages/simplex_app/lib/widgets/customTextField.dart
@@ -0,0 +1,92 @@
+import 'package:flutter/material.dart';
+
+class CustomTextField extends StatefulWidget {
+ final TextEditingController? textEditingController;
+ final TextInputType? textInputType;
+ final FocusNode? node;
+
+ final String? hintText;
+ final bool? isPassword;
+ final IconData? icon;
+ final Color? iconColor;
+ final Color? passIconColor;
+
+ final IconData? trailing;
+ final void Function()? trailingCallBack;
+
+ final Function(String)? onChangeFtn;
+ final void Function()? onEditComplete;
+ final String? Function(String?)? validatorFtn;
+ final Function(String)? onFieldSubmit;
+ final String? errorText;
+
+ const CustomTextField({
+ Key? key,
+ @required this.textEditingController,
+ @required this.textInputType,
+ this.trailing,
+ this.trailingCallBack,
+ this.node,
+ @required this.hintText,
+ this.icon,
+ this.iconColor,
+ this.passIconColor,
+ this.isPassword = false,
+ this.onChangeFtn,
+ this.onEditComplete,
+ this.validatorFtn,
+ this.onFieldSubmit,
+ this.errorText,
+ }) : super(key: key);
+
+ @override
+ _CustomTextFieldState createState() => _CustomTextFieldState();
+}
+
+class _CustomTextFieldState extends State {
+ FocusNode _node = FocusNode();
+
+ @override
+ void dispose() {
+ _node.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ double width = MediaQuery.of(context).size.width;
+
+ return Container(
+ width: width * 0.89,
+ decoration: BoxDecoration(borderRadius: BorderRadius.circular(8.0)),
+ child: TextFormField(
+ controller: widget.textEditingController,
+ textInputAction: TextInputAction.done,
+ keyboardType: widget.textInputType,
+ onChanged: widget.onChangeFtn,
+ onEditingComplete: widget.onEditComplete,
+ decoration: InputDecoration(
+ errorText: widget.errorText,
+ contentPadding: const EdgeInsets.symmetric(horizontal: 15.0),
+ hintText: widget.hintText,
+ hintStyle: Theme.of(context).textTheme.caption,
+ fillColor: Colors.grey[200],
+ filled: true,
+ enabledBorder: OutlineInputBorder(
+ borderSide: BorderSide(color: Colors.transparent)),
+ focusedBorder: OutlineInputBorder(
+ borderSide: BorderSide(color: Colors.transparent),
+ ),
+ errorBorder: OutlineInputBorder(
+ borderSide: BorderSide(color: Colors.red),
+ ),
+ focusedErrorBorder: OutlineInputBorder(
+ borderSide: BorderSide(color: Colors.red),
+ ),
+ ),
+ validator: widget.validatorFtn,
+ onFieldSubmitted: widget.onFieldSubmit,
+ ),
+ );
+ }
+}
diff --git a/packages/simplex_app/pubspec.lock b/packages/simplex_app/pubspec.lock
index 019b61be4f..80eb307f34 100644
--- a/packages/simplex_app/pubspec.lock
+++ b/packages/simplex_app/pubspec.lock
@@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
- version: "2.8.1"
+ version: "2.5.0"
boolean_selector:
dependency: transitive
description:
@@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
- version: "1.3.1"
+ version: "1.2.0"
clock:
dependency: transitive
description:
@@ -43,6 +43,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
+ cross_file:
+ dependency: transitive
+ description:
+ name: cross_file
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.3.1+5"
cupertino_icons:
dependency: "direct main"
description:
@@ -57,6 +64,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
+ file_picker:
+ dependency: "direct dev"
+ description:
+ name: file_picker
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "3.0.4"
flutter:
dependency: "direct main"
description: flutter
@@ -69,11 +83,72 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
+ flutter_plugin_android_lifecycle:
+ dependency: transitive
+ description:
+ name: flutter_plugin_android_lifecycle
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "2.0.3"
+ flutter_svg:
+ dependency: "direct dev"
+ description:
+ name: flutter_svg
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.22.0"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
+ flutter_web_plugins:
+ dependency: transitive
+ description: flutter
+ source: sdk
+ version: "0.0.0"
+ http:
+ dependency: transitive
+ description:
+ name: http
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.13.3"
+ http_parser:
+ dependency: transitive
+ description:
+ name: http_parser
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "4.0.0"
+ image_picker:
+ dependency: "direct dev"
+ description:
+ name: image_picker
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.7.5+4"
+ image_picker_for_web:
+ dependency: transitive
+ description:
+ name: image_picker_for_web
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "2.1.3"
+ image_picker_platform_interface:
+ dependency: transitive
+ description:
+ name: image_picker_platform_interface
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "2.4.1"
+ js:
+ dependency: transitive
+ description:
+ name: js
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.6.3"
lints:
dependency: transitive
description:
@@ -94,7 +169,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
- version: "1.7.0"
+ version: "1.3.0"
path:
dependency: transitive
description:
@@ -102,6 +177,41 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
+ path_drawing:
+ dependency: transitive
+ description:
+ name: path_drawing
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.5.1"
+ path_parsing:
+ dependency: transitive
+ description:
+ name: path_parsing
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.2.1"
+ pedantic:
+ dependency: transitive
+ description:
+ name: pedantic
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "1.11.1"
+ petitparser:
+ dependency: transitive
+ description:
+ name: petitparser
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "4.1.0"
+ plugin_platform_interface:
+ dependency: transitive
+ description:
+ name: plugin_platform_interface
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "2.0.2"
sky_engine:
dependency: transitive
description: flutter
@@ -113,7 +223,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
- version: "1.8.1"
+ version: "1.8.0"
stack_trace:
dependency: transitive
description:
@@ -148,7 +258,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
- version: "0.4.2"
+ version: "0.2.19"
typed_data:
dependency: transitive
description:
@@ -163,5 +273,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
+ xml:
+ dependency: transitive
+ description:
+ name: xml
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "5.1.2"
sdks:
- dart: ">=2.14.0 <3.0.0"
+ dart: ">=2.12.0 <3.0.0"
+ flutter: ">=2.0.0"
diff --git a/packages/simplex_app/pubspec.yaml b/packages/simplex_app/pubspec.yaml
index a9367ead20..12b6260264 100644
--- a/packages/simplex_app/pubspec.yaml
+++ b/packages/simplex_app/pubspec.yaml
@@ -1,4 +1,4 @@
-name: simplex_app
+name: simplex_chat
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
@@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
- sdk: ">=2.14.0 <3.0.0"
+ sdk: ">=2.12.0 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
@@ -46,6 +46,13 @@ dev_dependencies:
# rules and activating additional ones.
flutter_lints: ^1.0.0
+ # svg
+ flutter_svg: ^0.22.0
+
+ # attachments
+ image_picker: ^0.7.5+3
+ file_picker: ^3.0.2+2
+
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
@@ -58,8 +65,8 @@ flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
- # assets:
- # - images/a_dot_burr.jpeg
+ assets:
+ - assets/
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
diff --git a/packages/simplex_app/simplex.md b/packages/simplex_app/simplex.md
new file mode 100644
index 0000000000..b39703e557
--- /dev/null
+++ b/packages/simplex_app/simplex.md
@@ -0,0 +1,84 @@
+# Federated chat system with [E2EE][1] and low risk of [MITM attack][2]
+
+## Problems
+
+Existing chat platforms and protocols have some or all of the following problems:
+
+- Lack of privacy of the conversation, partially caused by [E2EE][1] implementations.
+- Lack of privacy of the user profile and connections.
+- Unsolicited messages (spam and abuse).
+- Lack of data ownership and protection.
+- Complexity of usage for all non-centralized protocols to non-technical users.
+
+The concentration of the communication in a small number of centralized platforms makes resolving these problems quite difficult.
+
+## Proposed solution
+
+Proposed stack of protocols solves these and other problems by making both messages and contacts accessible only on client devices, reducing the role of the servers to simple message brokers that only require authorization of messages sent to the queues, but do NOT require user authentication - not only the messages but also the metadata is protected.
+
+See [SMP protocol][6] and [SMP agent protocol][8].
+
+## Comparison with other protocols
+
+| | SimpleX chat | Signal, big platforms | XMPP, Matrix | P2P protocols |
+|:-------- |:------------:|:---------------------:|:------------:|:-------------:|
+| Requires global identity | No = private | Yes1 | Yes2 | Yes3 |
+| Possibility of MITM | No = secure | Yes4 | Yes | Yes |
+| Dependence on DNS | No = resilient | Yes | Yes | No |
+| Federation | Yes | No | Yes | No5 |
+| Central component or other network-wide attack | No = resilient | Yes | Yes2 | Yes6 |
+
+1. Usually based on a phone number, in some cases on usernames.
+2. DNS based.
+3. Public key or some other globally unique ID.
+4. If operator’s servers are compromised.
+5. While P2P networks are distributed, they are not federated - they operate as a single network.
+6. P2P networks either have a central authority or the whole network can be compromised - see the next section.
+
+## Comparison with [P2P][9] messaging protocols
+
+There are several P2P chat/messaging protocols and implementations that aim to solve privacy and centralisation problem, but they have their own set of problems that makes them less reliable than the proposed chat system design, more complex to implement and analyse and more vulnerable to attacks.
+
+1. [P2P][9] networks either have some centralized component, which makes them highly vulnerable, or, more commonly, use some variant of [DHT][10] to route messages/requests through the network. DHT implementations have complex designs that have to balance reliability, delivery guarantee and latency, and also have some other problems. The proposed chat system design has both higher delivery guarantee and low latency (the message is passed multiple times in parallel, through one node each time, using servers chosen by the recipient, while in P2P networks the message is passed through `O(log N)` nodes sequentially, using nodes chosen by the algorithm).
+
+2. The proposed design, unlike most P2P networks, has no global identity of any form, even temporary.
+
+3. P2P itself does not solve [MITM attack][2] problem, but most existing solutions do not use out-of-band messages for the initial key exchange. The proposed design uses out-of-band messages or, in some cases, pre-existing secure and trusted connections for the initial key exchange.
+
+4. P2P implementations can be blocked by some Internet providers (like [BitTorrent][11]). The proposed design is transport agnostic - it can work over standard web protocols, and the servers can be deployed on the same domains as the websites.
+
+5. All known P2P networks are likely to be vulnerable to [Sybil attack][12], because each node is discoverable, and the network operates as a whole. Known measures to reduce the probability of the Sybil attack either require a vulnerable centralized component or expensive [proof of work][13]. The proposed design, on the opposite, has no server discoverability - servers are not connected, not known to each other and to all clients. The chat network is fragmented and operates as multiple isolated connections. It makes Sybil attack on the whole simplex messaging network impossible - even if some servers are compromised, other parts of the network can operate normally, and affected clients can always switch to using other servers without losing contacts or messages.
+
+6. P2P networks are likely to be vulnerable to [DRDoS attack][14]. In the proposed design clients only relay traffic from known trusted connection and cannot be used to reflect and amplify the traffic in the whole network.
+
+## Network features
+
+- No user identity known to system servers - no phone numbers, user names and no DNS are needed to authorize users to the network.
+- Each user can be connected to multiple servers to ensure message delivery, even if some of the servers are compromised.
+- No single server in the system has visibility of all connections or messages of any user, as user profiles are identified by multiple rotating public keys, using separate key for each profile connection.
+- Uses standard asymmetric cryptographic protocols, so that system users can create independent server and client implementations complying with the protocols.
+- Open-source server implementations that can be easily deployed by any user with minimal technical expertise (e.g. on Heroku via web UI).
+- Open-source client implementations so that system users can independently assess system security model.
+- Only client applications store user profiles, contacts of other user profiles, messages; servers do NOT have access to any of this information and (unless compromised) do NOT store encrypted messages or any logs.
+- Multiple client applications and devices can be used by each user profile to communicate and to share connections and message history - the devices are not known to the servers.
+- Initial key exchange and establishing connections between user profiles is done by sharing the invitation (e.g. QR code via any independent communication channel (or directly via screen and camera), system servers are NOT used for key exchange - to reduce risk of key substitution in [MITM attack][2]. QR code contains the connection-specific public key and other information needed to establish the connection.
+- Connections between users can be established via shared trusted connections to simplify key exchange.
+- Servers do NOT communicate with each other, they only communicate with client applications.
+- Unique public key is used for each user profile connection in order to:
+ - reduce the risk of attacker posing as user's connection;
+ - avoid exposing all user connections to the servers.
+- Unique public key is used to identify each connection participant to each server.
+- Public keys used between connections are regularly rotated to prevent decryption of the full message history ([forward secrecy][4]) in case when some servers or middlemen preserve message history and the current key is compromised.
+- Users can repeat key exchange using QR code and alternative channel at any point to increase communication security and trust.
+
+[1]: https://en.wikipedia.org/wiki/End-to-end_encryption
+[2]: https://en.wikipedia.org/wiki/Man-in-the-middle_attack
+[4]: https://en.wikipedia.org/wiki/Forward_secrecy
+[6]: https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md
+[8]: https://github.com/simplex-chat/simplexmq/blob/master/protocol/agent-protocol.md
+[9]: https://en.wikipedia.org/wiki/Peer-to-peer
+[10]: https://en.wikipedia.org/wiki/Distributed_hash_table
+[11]: https://en.wikipedia.org/wiki/BitTorrent
+[12]: https://en.wikipedia.org/wiki/Sybil_attack
+[13]: https://en.wikipedia.org/wiki/Proof_of_work
+[14]: https://www.usenix.org/conference/woot15/workshop-program/presentation/p2p-file-sharing-hell-exploiting-bittorrent
diff --git a/packages/simplex_app/test/widget_test.dart b/packages/simplex_app/test/widget_test.dart
index f142cb5aa2..f06b78b7fa 100644
--- a/packages/simplex_app/test/widget_test.dart
+++ b/packages/simplex_app/test/widget_test.dart
@@ -5,26 +5,26 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
-import "package:flutter/material.dart";
-import "package:flutter_test/flutter_test.dart";
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
-import "package:simplex_app/main.dart";
+import 'package:simplex_chat/main.dart';
void main() {
- testWidgets("Counter increments smoke test", (WidgetTester tester) async {
+ testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
- expect(find.text("0"), findsOneWidget);
- expect(find.text("1"), findsNothing);
+ expect(find.text('0'), findsOneWidget);
+ expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
- expect(find.text("0"), findsNothing);
- expect(find.text("1"), findsOneWidget);
+ expect(find.text('0'), findsNothing);
+ expect(find.text('1'), findsOneWidget);
});
}