Online messaging

An XKCD comic is worth a thousand words.

There is no standard when it comes to online communication.

IRC used to be the way people communicate online. This is not true
anymore however, and with all the new protocols, platforms, applications
that pop out every now and then, communication is easier than ever !

Easier ? Not so much.

To communicate with 3 different groups of people, you need 3 different
applications. Be it Facebook Messenger, Telegram, Whatsapp, Matrix,
Mattermost, Discord, …, you name it.

Each require creating an account, turning on notifications, learning
the UI and so on, and so forth.

Of course, none of these could agree on a standard way to relay text
messages from one device to another! So now this is your problem, as
a user, to sort out the problem of switching contexts.

But maybe you don't have to…

# Matterbridge

By relaying messages from one platform the others, [matterbridge][0] is
the kind of project that provide an elegant solution to an ugly problem.

It creates a bridge between many of the existing messaging platforms,
and bridge them together. When a message is received on one platform,
the matterbridge gateway will replay this message to all the other
platforms configured, so people can effectively talk to each other in
a cross-application manner.

I use it so I can participate in a local group in my hometown on WhatsApp,
which I don't want installed on my personnal phone. It relays all
messages on IRC, and to a telegram group, so I can still participate
from my phone.

# Whatsapp

In order to use whatsapp with matterbridge, you need an account (so a
phone number), and find a way to "authorize" matterbridge to use this

The only way to do it (at least for matterbridge v1.18.0), is by tricking
whatsapp into thinking that matterbridge is a web browser, so it can
use the whatsapp [web version][1].
The web version doesn't replace the original application, so you got to
keep it running all the time.

As I didn't want whatsapp to run on my phone, I had to find another way.

# Android VM

The android SDK provide an emulator, that you can run in headless mode.
What's happening under the hood is that you get a qemu virtual machine
started for your phone's architecture (even x86_64!).

The procedure to get whatsapp running on a VM is described here:

This includes feeding the phone's webcam with ffmpeg recording your
desktop, so you can scan the QR code created by matterbridge from
whatsapp, and allow it to run (you MUST run it in graphical mode at this
point, so a working android SDK is needed).

Unforturnately, the emulator will only run on Linux, so I couldn't host
it on one of my OpenBSD server. To make it simpler, I chose to install
a Debian 10, so the SDK installation is easy to do.

	apt install android-sdk libopengl0 libxdamage1 libtinfo5 libgl1 libegl1-mesa
	curl -O
	unzip -d /usr/lib/android-sdk
	/usr/lib/android-sdk/tools/bin/sdkmanager 'system-images;android-30;google_apis_playstore;x86_64' emulator
	/usr/lib/android-sdk/tools/bin/sdkmanager --update

You'll need that on both your server and local machine. Then you can
create the android emulator locally, and start it:

	/usr/lib/android-sdk/tools/bin/avdmanager create avd -n whatsapp -k 'system-images;android-30;google_apis_playstore;x86_64'
	/usr/lib/android-sdk/emulator/emulator -avd whatsapp

Once running, install the whatsapp application, connect it to your
account and make sure it works. Follow the guide I mentionned earlier
to link it to matterbridge.
Don't forget to authorize the ADB key fingerprint at boot, so you can
later connect to the emulator from the command line.

Once done, take a snapshot of the emulator, and shut it down. Your full
emulator state should be saved to ~/.android/avd, and the adbkey to

Upload the whole ~/.android to your server, and start the emulator in
headless mode:

	/usr/lib/android-sdk/emulator/emulator -avd whatsapp -no-audio -no-window

After some time you should be able to connect to it using `adb shell`. Use
this connection to start the application (and eventually, enable the
wifi card, if you don't have internet access on the emulator):

	adb shell 'svc wifi enable'
	adb shell 'ping'
	adb shell 'am start -n com.whatsapp/.HomeActivity'

At this point, the VM should be up and running, and whatsapp started,
and in the foreground ! You can verify it by taking a screenshot:

	adb shell 'screencap -p' > capture.png

In case you want to click on the touchscreen remotely, you can use

	adb shell 'input X Y'

Where X and Y are the coordinates of where you want to click. Not very
comfortable to use, but that might be handier than stopping the VM and
starting it again locally, snapshot it, reupload, …

# Rationale