Communication Setup in Emacs

Much ink has been spilled on…how best to spill ink?


Updated: 2024-04-21 Sun 19:04

“There are no ordinary people. You have never talked to a mere mortal. Nations, cultures, arts, civilizations - these are mortal, and their life is to ours as the life of a gnat. But it is immortals whom we joke with, work with, marry, snub and exploit - immortal horrors or everlasting splendors. This does not mean that we are to be perpetually solemn. We must play. But our merriment must be of that kind (and it is, in fact, the merriest kind) which exists between people who have, from the outset, taken each other seriously - no flippancy, no superiority, no presumption.” – C.S. Lewis


1. Introduction

D unbar's number(s) may vary widely between individuals but what man hasn't been deluged by the onslaught of the information age? Here's how I try keep things under control. This article details the setup of my communication channels in Emacs on Debian GNU/Linux. Workflow configuration specific to myself is relegated to my dotfiles.

2. Instant Messaging - Meta with Matrix and Ement

Matrix is a protocol for secure, decentralized real time communication in the same fashion as Email. Three options exist:

  1. (Paid/Free*) A virtual private server (VPS) with SSH access
  2. (Paid) beeper
  3. (Free) An account on the matrix.org homeserver (Create one here).

Options 1. and 2. can be bridged to third party services at your convenience while 3. has no bridging capabilities as of this writing. With unity being one of the five transcendental attributes I am living the dream with Facebook messenger, WhatsApp and SMS bridged onto a self-hosted Matrix server. A server that has Ement.el serving as the client. Some elbow grease needed but the whole process shouldn't take you more than 1 hour. My own notes follow, hope that helps.

2.1. Matrix-synapse server Installation

I am installing the Synapse (as of writing, version 1.95) implementation of the Matrix protocol on Debian 12. Replace all instances of bhw.name with your domain name example.com.

# My server environment,
uname -a
# Linux oci-a1-flex 6.1.0-17-cloud-arm64 #1 SMP Debian 6.1.69-1 (2023-12-30) aarch64 GNU/Linux
# On the server machine,
sudo apt install python3-canonicaljson/bookworm-backports libsqlite3-dev caddy
# Install matrix-synapse from debian package archives.
sudo apt install matrix-synapse/bookworm-backports
# It will ask for some configuration right off the bat,
# See "server_name" in https://matrix-org.github.io/synapse/v1.95/usage/configuration/config_documentation.html
Name of the server: matrix.bhw.name
Report anonymous statistics [yes/no] yes # your choice.

Turn off TLS as we are going to reverse proxy the Matrix server with Caddy.

# Emacs TRAMP instead of nano if you please.
sudo nano /etc/matrix-synapse/conf.d/reverse_proxy.yaml

Add the following lines to the configuration file.

no_tls: True
x_forwarded: true

Make sure /etc/caddy/Caddyfile has the following content and start Caddy with systemctl start caddy.service. More in depth tutorial and context about firewall and other machine configuration. If you would like to use delegation in Matrix with Caddy take a look at the route directive.

matrix.bhw.name {
    reverse_proxy /_matrix/* localhost:8008
    reverse_proxy /_synapse/client/* localhost:8008
}
matrix.bhw.name:8448 {
    reverse_proxy /_matrix/* localhost:8008
}

Don't forget to allow inbound traffic on port 8448 and port 443 if you desire to use Matrix federation.

firewall-cmd --get-services
firewall-cmd --permanent --add-service=matrix
# Allow HTTPS traffic on port 443
# firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-services

The next step will be to add an admin account for ourselves. As per the Manpage, "For this to work, the home server must be configured with the 'registrationsharedsecret' option set." To do so,

echo "registration_shared_secret: '$(cat /dev/urandom | tr -cd '[:alnum:]' | fold -w 256 | head -n 1)'" | sudo tee /etc/matrix-synapse/conf.d/registration_shared_secret.yaml
 systemd restart matrix-synapse.service
 # Enable matrix-synapse to start at boot.
 systemd enable matrix-synapse.service
 synapse_register_new_matrix_user -u ben -p p@ssword -a -c /etc/matrix-synapse/conf.d/registration_shared_secret.yaml http://localhost:8008

Quick sanity check by visiting from your web browser https://matrix.bhw.name/_matrix/ and you should get an error code response of "Unrecognized request". We can also head to https://app.element.io/#/login and login with our administrator user. Note that we must change the "Homeserver" from matrix.org to https://matrix.bhw.name and enter our username and password. Now we can bridge to some 3rd party services.

2.2. WhatsApp Bridge Installation

Link our WhatsApp account and our Matrix server. Download the correct prebuilt executable for your architecture from the WhatsApp Bridge Documentation and extract it to /opt/mautrix-whatsapp/. Create the folder if it doesn't exist.

# Create folder if it doesn't exist. Note I am downloading arm64 executable.
curl -L -o /opt/mautrix-whatsapp/artifact.zip https://mau.dev/mautrix/whatsapp/-/jobs/artifacts/master/download?job=build%20arm64
unzip /opt/mautrix-whatsapp/artifact.zip -d /opt/mautrix-whatsapp/

Rename example-config.yaml to config.yaml and you will have to edit the following options inside that file,

homeserver:
    address: http://localhost:8008
    domain: matrix.bhw.name
appservice:
    database:
        type: sqlite3-fk-wal
        url: file:/var/lib/matrix-synapse/whatsapp.db?_txlock=immediate
bridge:
    permissions:
        "matrix.bhw.name": user
        "@ben:matrix.bhw.name": admin
cd /opt/mautrix-whatsapp/
# Generate the appservice registration file.
./mautrix-whatsapp -g
chmod 644 registration.yaml

Create a file at /etc/matrix-synapse/conf.d/bridge-registration.yaml with the following contents,

app_service_config_files:
  - /opt/mautrix-whatsapp/registration.yaml
  # Any other bridge registrations.
  #- /opt/mautrix-gmessages/registration.yaml
  #- /opt/mautrix-meta/registration.yaml

Restart matrix-synapse to confirm it works. Then create a systemd service file at /etc/systemd/system/mautrix-whatsapp.service

[Unit]
Description=mautrix-whatsapp bridge

[Service]
Type=exec
User=root
WorkingDirectory=/opt/mautrix-whatsapp
ExecStart=/opt/mautrix-whatsapp/mautrix-whatsapp
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target

Enable and start the mautrix-whatsapp.service.

# Will start this service at boot.
systemctl enable mautrix-whatsapp.service
systemctl start mautrix-whatsapp.service
# When updating bridge, restart matrix-synapse as well
# systemctl restart matrix-synapse

Login to https://app.element.io/#/login and refer to the documentation for initial setup. In my experience, all mautrix bridges are excellent. After this setup tutorial, other bridges such as Meta and Google Messenger are similar. Do note that /etc/matrix-synapse/conf.d/bridge-registration.yaml must contain a list of all appservice registrations you wish to make on your homeserver. Also note that mautrix-meta requires extra configuration.

2.3. Conclusion

Install ement.el and login,

;; First login; subsequent logins will used a cached session if
;; `ement-save-sessions' is TRUE.
(ement-connect :user-id "@ben:matrix.bhw.name"
               :password  "p@ssword"
               :uri-prefix "https://matrix.bhw.name")

My own config is available here. Three parting thoughts. I am somewhat optimistic about Matrix's longevity as the German armed forces has adopted the standard. Secondly, Emacs users often have to deal with their complex configurations falling apart on mobile phones but this is not that case with the element client available on android. Last but not least, is my thanks for the many hours of love poured in by these open source developers <3.

References

Create a Chat Server Using Matrix Synapse and Element on Debian 11 | Vultr Docs

3. Email - GMail with Mbsync and Mu4e

Mbsync synchronizes email from your IMAP server (Gmail/Outlook/Proton) and stores it locally as specified in ~/.mbsyncrc. Mu uses the open source Xapian to index the local email database. Mu4e (Mu for Emacs) provides an user interface. Sendmail manages the connection with the remote SMTP servers during the sending of email, selecting the server according to the email specified in the "from" field of the outgoing mail. One can specify more than one address.

Operation Analogy (detailed explanation):

  1. Getting mail (the mail truck filling mailbox) : mbsync
  2. Indexing and reading mail (inbox tray & your office desk): mu + mu4e
  3. Sending mail (Dropping envelope off to post office): sendmail
# My workstation environment,
uname -a
# Linux bhw-thinkpad 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 GNU/Linux
# Isync is the name of the program, but mbsync is the name of the binary.
# See and tangle .mbsyncrc in dotfiles.org.
sudo apt install isync mu4e/bookworm-backports gnutls-bin sendmail

dotfiles.org is not public, but here is the relevant excerpt. Please note to access the Gmail mail server you need an app password. M-x Woman RET mbsync to read isync's manpage to understand what's going on. I do not delete email, instead I mark it as read and move it out of my inbox to "All mail".

IMAPAccount gmail
Host imap.gmail.com
User myemailaddress@gmail.com
Pass p@ssword
SSLType IMAPS
PipelineDepth 1

IMAPStore gmail-remote
Account gmail

MaildirStore gmail-local
Path /home/ben/project-jerome/email-archive/
Inbox /home/ben/project-jerome/email-archive/inbox/
SubFolders Verbatim

Channel gmail
Master :gmail-remote:
Slave :gmail-local:
Patterns * ![Gmail]* !inbox "[Gmail]/All Mail"
Create Slave
Sync PullNew
CopyArrivalDate yes
SyncState *
# Inital sync will take much longer than consequent syncs.
mbsync -a -V
# See mu4e manual section 2.4
# https://www.djcbsoftware.nl/code/mu/mu4e/
mu init --maildir=~/project-jerome/email-archive/ --my-address=myemailaddress@gmail.com
mu index
  • [X] Install the mu4e layer. mu4e=installation-path is "/usr/share/emacs/site-lisp/elpa/mu4e-1.10.8"
  • [X] Install the org layer (for the included org-mime package)

For my usage patterns, see heading "Mu4e Config". Maybe I'll self-host someday?

3.1. Viewing HTML email in your internet browser

Microsoft Edge browser disables viewing file:// links by default for security reasons. To configure Microsoft Edge to allow this, we must download and install the Microsoft Edge administrative template. Then we edit the registry as shown in the documentation, creating a new regkey at Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies|Microsoft\Edge and creating a new REG_DWORD key IntranetFileLinksEnabled set the data to 1. Then we press WindowsKey + r and run gpedit.msc to open the "Local Group Policy Editor" and navigate to "Computer Configuration/Administrative Templates/Microsoft Edge/Content settings/Allow intranet zone file URL links from Microsoft Edge to open in Windows File Explorer/" and enable it. Last but not least, make sure in Windows settings, that Apps/Default Apps/Microsft Edge has set the default file types or link types for ".html". Lastly we must prepend file://///wsl$/Debian/ instead of the default file:/// to access Debian files through Windows.

(setf browse-url-generic-program  "/mnt/c/Windows/System32/cmd.exe"
      browse-url-generic-args     '("/c" "start")
      browse-url-browser-function #'browse-url-generic
      ;; Needed for `mu4e-action-view-in-browser'
      browse-url-filename-alist
      '(("^/\\(ftp@\\|anonymous@\\)?\\([^:/]+\\):/*" . "ftp://\\2/")
        ("^/\\([^:@/]+@\\)?\\([^:/]+\\):/*" . "ftp://\\1\\2/")
        ;; For gnus-article-browse-html-article on Windows Subsystem for Linux.
        ("^/+" . "file://///wsl$/Debian/")))

References

Search-oriented tools for Unix-style mail | Mark J. Nelson Streamline Your E-mail Management with mu4e - Emacs Mail - YouTube Reading IMAP email in Emacs Configure mu4e and msmtp - Tushar Tyagi

4. Web logs & Web feeds - Org Publish and Elfeed

I am aware of two options to read RSS/atom feeds in Emacs. GNUS with the nnrss backend and elfeed. Having used both, I prefer elfeed. If I could wave a magic wand, I would make GNUS non-blocking/giving Emacs a concurrency story a la Common Lisp. Install the elfeed package or layer. A couple of tips,

Org Publish is my preferred software for blogging because it allows my blog articles to continue as my part of my personal notes, integrated with my personal knowledge base and my TODO lists. This reduces the publishing friction to near zero. For single author blogs, I'd say this factor is by far the most important.

Not much setup required here, see the "Elfeed config", "Youtube Sub Extractor Config" and "Ox Publish Config" headings for usage.

This section is still incomplete. Chris Wellons – the creator of elfeed – and others regard Atom as a much better specification for web feeds than RSS. I know ox-rss exists, but my plan is to explore a backend for Atom export that works well Org Publish. Once ox-atom works/exists, I will leave a setup tutorial here. A someday/nice-to-have project.

I have ordered these subheadings (Instant Messaging > Email > News & Blogs) by increasing length of both response time and content. You can imagine that various author(s) have created great literature representing the eternal word and truths that we spend the rest of our lives communicating with through words and actions, though often it takes time to digest these. Emacs, of course, can help with that :). A future blog article shall be on just that; how I use Emacs to assist in taking notes on books, spaced repetition and concept mapping.