]> Sergey Matveev's repositories - mmc.git/blob - doc/arch.texi
User statuses
[mmc.git] / doc / arch.texi
1 @node Architecture
2 @unnumbered Architecture
3
4 Brief scheme of commands, utilities and files involved is below.
5 @file{F} means files/FIFOs/directories creation and altering.
6 @file{R} means running of external programs.
7
8 @verbatim
9 cmd/start
10   F tmux.conf
11   R tmux -f tmux.conf
12     R tail -f debug | tai64n
13     R for { cat users/status ; sleep 5 }
14     R fzf **(/) | cmd/newwin
15     R cmd/mmc | tai64n
16      F users/status
17      F users/.../{id,email,name,|in,out.rec,|status,last}
18      F chans/.../{id,info,out.rec,|users,last}
19      F file/{|get,|out}
20      R cmd/notify
21        R tmux display-message ...
22      R cmd/newwin
23        R cmd/rd/colourized
24          R cmd/rd < .../out.rec | spc
25        R cmd/wr
26          R for { rlwrap cat > .../in }
27
28 cmd/download
29   R echo ... > file/get
30   R tar xf < file/out
31
32 cmd/sb ... > history.rec
33 @end verbatim
34
35 @url{https://tools.suckless.org/ii/usage/, ii} IRC client's page has
36 many links to similar kind of chat software. There is some long-living
37 program, that deals with the messaging protocol and communicates with
38 other programs through the FIFO files.
39
40 There are user chats and group chats, both in IRC, XMPP and MM worlds.
41 Each user and channel have corresponding directory having the same name.
42 It contains @file{in} and @file{out} named pipes. Anything you write to
43 @file{in} will be send as a message to corresponding user/channel.
44 Reading from @file{out} will give you incoming messages from that
45 user/channel. That is the basic idea of the backend.
46
47 All you need is to write the frontend that will give you convenient
48 ability to deal with all that bunch of files. That is the most
49 complicated part of course. I do not like idea of having the single
50 window with consolidation of all chats, like using @command{multitail}
51 on several pipes. I do not like idea of purely CLI utility like
52 @url{https://github.com/agl/xmpp-client, github.com/agl/xmpp-client},
53 @url{https://en.wikipedia.org/wiki/MICQ, mICQ} or
54 @url{https://www.gnu.org/software/freetalk/, GNU Freetalk}, where you
55 have to carefully be aware of whom you are typing now. I like
56 @command{irssi} and @command{mcabber}, where you have got separate
57 window with per-user/channel output and input. We must have ability to
58 hide/close those windows/tabs/panes, automatically open them if they got
59 incoming events and we must be alerted about those events, at least by
60 bell character.
61
62 @url{https://github.com/tmux/tmux/wiki, tmux} is a perfect tool for
63 dynamic windows/tabs/panes management. It already has all necessary
64 functions to detect bell/alarms. It have ability to easily create you
65 own keybindings and already have massive amount of convenient hotkeys to
66 quickly navigate among all that windows. It already has history buffer
67 and ability to search in it. And it is likely to be already used by the
68 end user.
69
70 Messages have to be saved on filesystem. Unlike IRC, MM messages are
71 often multiline and can have additional metadata like unique identifiers
72 and attached files. So each message is a more complicated entity than
73 text string with appended timestamp and sender information. Each time
74 chat window is opened, I want to see tail of the corresponding history.
75 So instead of using FIFO that is @command{tee}-ed to ordinary
76 long-living file, I decided to use append-only
77 @url{https://www.gnu.org/software/recutils/, GNU Recutils}'es recfile.
78 And an additional program that translates it to convenient human
79 readable lines, also acting like @command{tail -f}.
80
81 It is crucial to read message you want to send not line by line, to be
82 able to send multiline messages in a single post. Closing the FIFO file
83 can be treated as the end of the message. To be able to use rich line
84 editing capabilities or even external editor, you can use great
85 @url{https://github.com/hanslub42/rlwrap, rlwrap} utility in an endless
86 loop.
87
88 So how all that decisions are composed together?
89
90 @itemize
91
92 @item @command{cmd/start} creates @file{debug} FIFO, prepares
93 configuration file for @command{tmux} (actually there is just single
94 path to the command replaced with the real one) and runs it with single
95 window split on two halves: one just shows debug messages (all raw MM
96 messages for debugging), another one runs @command{cmd/mmc} itself.
97
98 @item @command{cmd/mmc} logs in to the server, fetches a list of all
99 known users and channels I am subscribed to. Then it creates necessary
100 @file{users/NAME} and @file{chans/NAME} directories with some basic
101 regular information files (like identifiers and email addresses) with
102 various FIFOs.
103
104 @item Reading @file{users/.../status} shows you current user's status.
105 @file{chans/.../users} shows currently subscribed users.
106
107 @item @file{out.rec} is append-only database of passed messages.
108 @file{last} contains the identifier of the last received message. After
109 you log in, it is used to fetch newly appeared messages while you were
110 offline.
111
112 @item If some user changes its status, or someone is typing, then
113 @command{cmd/mmc} calls @command{cmd/notify} command with corresponding
114 message. Currently that handler just asks @command{tmux} to
115 @command{display-message} for 1.5 seconds.
116
117 @item If new message comes in, then @command{cmd/mmc} calls
118 @command{cmd/newwin} and tells what window it has to open.
119 @command{cmd/newwin} checks if @command{tmux} has opened it already.
120 Newly created window has the name of the user/channel and is split on
121 two parts:
122     @itemize
123     @item
124     one part contains the output of @command{cmd/rd} piped through
125     @url{http://supercat.nosredna.net/, supercat} (@command{spc}) for
126     colourizing.
127     @item
128     other part holds an endless cycle of @command{rlwrap}-ed
129     @command{cat} piped to corresponding @file{in} FIFO.
130     @end itemize
131
132 @item Also you can manually open desired chat window by pressing
133 predefined keybinding, that will show you
134 @command{https://github.com/junegunn/fzf, fzf}-backed list of all known
135 users and channels.
136
137 @item @command{cmd/rd} reads @file{out.rec} file and prints last ten
138 messages (by default) in human readable form. It does not close that
139 file, but regularly checks if it is updated, to immediately print the
140 newly appeared messages. It print bell character with each message, so
141 it sets an alert for window in @command{tmux}.
142
143 @end itemize
144
145 Outside that running @command{tmux} user can use @command{cmd/download}
146 utility to download specified file's identifier. It sends it identifier
147 to @file{file/get} and reads the archive with that file from
148 @file{file/out}. File is sent inside
149 @url{https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_01,
150 pax archive} to keep its original name and MM's file identifier.