1 ###RUN_PERL: #!/usr/bin/perl
4 # chat.pl is generated from chat.1.pl.
6 # The coincidence interface
8 # Copyright (C) 2016, 2017, 2023 Balthasar SzczepaĆski
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as
12 # published by the Free Software Foundation, either version 3 of the
13 # License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ###PERL_LIB: use lib /botm/lib/bsta
26 # use Encode::Locale ('decode_argv');
27 use Encode ('encode', 'decode');
29 ###PERL_LIB: use lib /botm/lib/bsta
31 'read_data_file', 'write_data_file',
33 'url_query_decode', 'url_query_encode',
35 'html_entity_encode_dec',
38 'CHAT_STATE', 'CHAT_ACTION',
39 'fail_method', 'fail_content_type',
40 'get_remote_addr', 'get_id', 'get_password',
41 'print_html_start', 'print_html_end',
42 'print_html_head_start', 'print_html_head_end',
43 'print_html_body_start', 'print_html_body_end',
48 ###PERL_CGI_PATH: CGI_PATH = /bsta/
49 ###PERL_CGI_COIN_PATH: CGI_COIN_PATH = /bsta/coin
51 ###PERL_DATA_CHAT_PATH: DATA_CHAT_PATH = /botm/data/bsta/chat
52 ###PERL_DATA_COIN_PATH: DATA_COIN_PATH = /botm/data/bsta/coincidence
53 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
55 ###PERL_WEBSITE_NAME: WEBSITE_NAME = Bicycles on the Moon
57 binmode STDIN, ':encoding(UTF-8)';
58 binmode STDOUT, ':encoding(UTF-8)';
59 binmode STDERR, ':encoding(UTF-8)';
76 my $action = CHAT_ACTION->{'none'};
87 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
88 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
90 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
94 exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
97 %http = read_header_env(\%ENV);
98 %cgi = url_query_decode($ENV{'QUERY_STRING'});
100 if ($method eq 'POST') {
101 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
102 my %cgi_post = url_query_decode( <STDIN> );
103 %cgi = merge_settings(\%cgi, \%cgi_post);
105 # multipart not supported
107 exit fail_content_type($method, $http{'content-type'});
111 $IP = get_remote_addr();
112 $page = get_id(\%cgi, -1);
113 $password = get_password(\%cgi);
115 %coin = read_data_file(DATA_COIN_PATH());
116 %settings = read_data_file(DATA_SETTINGS_PATH());
118 $password_ok = ($password eq $settings{'password'});
120 if ($cgi{'words'} ne '') {
121 $words = $cgi{'words'};
123 if ($password_ok && ($cgi{'username'} ne '')) {
124 $username = $cgi{'username'};
126 foreach my $action_id ('join', 'leave', 'nopost', 'file') {
127 if ($cgi{$action_id} ne '') {
128 $action = CHAT_ACTION->{$action_id};
135 if (open ($fh, "+<", encode('locale_fs', DATA_CHAT_PATH()))){
137 %chat = read_data_file($fh);
139 $chat_state = int($chat{'state'});
140 $chat_id = int($chat{'id'});
143 if (($action == CHAT_ACTION->{'none'}) && ($words ne '')) {
144 if (($chat_state < CHAT_STATE->{'ready'}) && !$password_ok) {
145 $message = 'Not connected.';
148 if ($words !~ /[\r\n]/) {
149 if ($username =~ /^[A-Za-z]*$/) {
150 $chat{'content'} .= $username.': '.$words."\n";
151 if ($chat_state < CHAT_STATE->{'active'}) {
152 $chat_state = CHAT_STATE->{'active'};
153 $chat{'state'} = $chat_state;
155 write_data_file($fh, '', '', \%chat);
158 $message = 'Invalid username.';
162 $message = 'Invalid text.';
167 elsif ($action == CHAT_ACTION->{'join'}) {
168 if (($chat_state > CHAT_STATE->{'disconnected'}) && !$password_ok) {
169 $message = 'Already connected.';
172 if ($username =~ /^[A-Za-z]*$/) {
173 if ($password_ok || $words eq $coin{'server'}) {
174 $chat{'content'} .= 'join@'.$username.': '.$words."\n";
175 if ($chat_state < CHAT_STATE->{'ready'}) {
176 $chat_state = CHAT_STATE->{'ready'};
177 $chat{'state'} = $chat_state;
179 write_data_file($fh, '', '', \%chat);
181 elsif ($words eq '') {
182 $message = 'Server ID missing.';
184 elsif ($words !~ /^[0-9]+$/) {
185 $message = 'Invalid server ID.';
188 $message = 'No active Coincidence server with this ID.';
192 $message = 'Invalid username.';
197 elsif ($action == CHAT_ACTION->{'leave'}) {
198 if (($chat_state < CHAT_STATE->{'ready'}) && !$password_ok) {
199 $message = 'Already disconnected.';
202 if ($username =~ /^[A-Za-z]*$/) {
203 $chat{'content'} .= 'leave@'.$username.': '.$words."\n";
204 if ($username ne '') {
205 write_data_file($fh, '', '', \%chat);
209 if ($chat_state > 1) {
210 write_data_file(DATA_CHAT_PATH.$chat_id, '', '', \%chat);
211 $new_chat{'id'} = $chat_id+1;
214 $new_chat{'id'} = $chat_id;
216 $new_chat{'state'} = CHAT_STATE->{'disconnected'};
217 $new_chat{'content'} = '';
218 write_data_file($fh, '', '', \%new_chat);
222 $message = 'Invalid username.';
228 ($action == CHAT_ACTION->{'file'}) &&
229 ($cgi{'file'} ne '') &&
233 if ($words !~ /[\r\n]/) {
234 if ($username =~ /^[A-Za-z]*$/) {
235 $chat{'content'} .= 'file@'.$username.': '.$words."\n";
236 if ($chat_state < CHAT_STATE->{'active'}) {
237 $chat_state = CHAT_STATE->{'active'};
238 $chat{'state'} = $chat_state;
240 write_data_file($fh, '', '', \%chat);
243 $message = 'Invalid username.';
247 $message = 'Invalid text.';
250 @chat_lines = split(/\r?\n/, $chat{'content'});
253 $chat_state = CHAT_STATE->{'disconnected'};
254 $message = 'Can\'t lock data file!';
260 $chat_state = CHAT_STATE->{'disconnected'};
261 $message='Can\'t open data file!';
267 %chat = read_data_file(DATA_CHAT_PATH());
268 $last_id = int($chat{'id'});
269 if ($chat_id < $last_id) {
270 %chat = read_data_file(DATA_CHAT_PATH.$page);
271 $chat_state = int($chat{'state'});
272 @chat_lines = split(/\r?\n/, $chat{'content'});
276 print "Content-type: text/html\n\n";
277 if($method eq 'HEAD') {
281 if ($username eq '') {
282 $username = $coin{'name'};
285 my $base_url = CGI_PATH();
286 my $coin_url = CGI_COIN_PATH();
287 my $form_url = $coin_url;
288 my $oldest_url = merge_url(
289 {'path' => $coin_url},
292 my $older_url = merge_url(
293 {'path' => $coin_url},
294 {'path' => $chat_id -1}
296 my $newer_url = ($chat_id < ($last_id -1)) ?
298 {'path' => $coin_url},
299 {'path' => $chat_id +1}
303 my $password_query = url_query_encode({'p', $settings{'password'}});
304 $coin_url = merge_url($coin_url , {'query' => $password_query, 'append_query' => 1});
305 $oldest_url = merge_url($oldest_url, {'query' => $password_query, 'append_query' => 1});
306 $older_url = merge_url($older_url , {'query' => $password_query, 'append_query' => 1});
307 $newer_url = merge_url($newer_url , {'query' => $password_query, 'append_query' => 1});
310 my $abbr = abbr_name($username);
311 my $_website_name = html_entity_encode_dec(WEBSITE_NAME() , 1);
312 my $_server = html_entity_encode_dec($coin {'server'} , 1);
313 my $_key = html_entity_encode_dec($coin {'key'} , 1);
314 my $_password = html_entity_encode_dec($settings{'password'}, 1);
315 my $_cgi_username = html_entity_encode_dec($cgi {'username'}, 1);
316 my $_username = html_entity_encode_dec($username , 1);
317 my $_abbr = html_entity_encode_dec($abbr , 1);
318 my $_message = html_entity_encode_dec($message , 1);
319 my $_base_url = html_entity_encode_dec($base_url , 1);
320 my $_coin_url = html_entity_encode_dec($coin_url , 1);
321 my $_form_url = html_entity_encode_dec($form_url , 1);
322 my $_oldest_url = html_entity_encode_dec($oldest_url, 1);
323 my $_older_url = html_entity_encode_dec($older_url , 1);
324 my $_newer_url = html_entity_encode_dec($newer_url , 1);
326 print_html_start(\*STDOUT);
327 print_html_head_start(\*STDOUT);
329 print ' <title>Coincidence • '.$_website_name.'</title>'."\n";
331 print_html_head_end(\*STDOUT);
332 print_html_body_start(\*STDOUT);
334 print ' <div id="inst" class="ins">'."\n";
336 print ' <div id="title">'."\n";
337 print ' <H1 id="titletext">Coincidence</H1>'."\n";
338 print ' </div>'."\n";
340 print ' <div id="storypuzzle">'."\n";
342 print ' Before: '.$chat_id."\n";
344 elsif ($chat_state > CHAT_STATE->{'disconnected'}) {
345 print ' Connected to server <span class="br">'.$_server.'</span> as user <span class="ni">'.$_username.'</span> (<span class="ni">'.$_abbr.'</span>), public key <span class="br">'.$_key.'</span>.'."\n";
348 print ' Not connected.';
350 print ' </div>'."\n";
352 print ' <div id="command">'."\n";
353 if ($message ne '') {
354 print ' <span class="br">'.$_message.'</span>'."\n";
357 print ' <form method="post" action="'.$_form_url.'">'."\n";
359 print ' <input class="intxc" type="text" name="words">'."\n";
360 print ' <input class="inbt" type="submit" value="Send">'."\n";
362 print ' <input class="intx" type="text" name="username" value="'.$_cgi_username.'">'."\n";
363 print ' <input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
364 print ' <input class="inbt" type="submit" name="join" value="Connect">'."\n";
365 print ' <input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
366 print ' <input class="inbt" type="submit" name="file" value="Send file">'."\n";
367 print ' <input type="hidden" name="p" value="'.$_password.'">'."\n";
369 elsif ($chat_state > CHAT_STATE->{'disconnected'}) {
370 print ' <input class="intxc" type="text" name="words">'."\n";
371 print ' <input class="inbt" type="submit" value="Send">'."\n";
373 print ' <input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
374 print ' <input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
377 print ' <input class="intx" type="text" name="words">'."\n";
378 print ' <input class="inbt" type="submit" name="join" value="Connect">'."\n";
380 print ' </form>'."\n";
382 print ' </div>'."\n";
384 print ' </div>'."\n";
385 print ' <div id="insb" class="ins">'."\n";
387 print ' <div id="chat">'."\n";
389 for (my $i = @chat_lines-1; $i>=0; --$i) {
390 print ' '.chat_line($chat_lines[$i])."<br>\n";
394 for (my $i = 0; $i<@chat_lines; ++$i) {
395 print ' '.chat_line($chat_lines[$i])."<br>\n";
398 print ' </div>'."\n";
400 print ' <div id="underlinks">'."\n";
401 print ' <a href="'.$_base_url.'">BSTA</a> | <a href="'.$_coin_url.'">Once again</a>';
403 print ' | <a href="'.$_older_url.'">Before</a>';
405 if ($chat_id < $last_id) {
406 print ' | <a href="'.$_newer_url.'">Unbefore</a>';
409 print ' | <a href="'.$_oldest_url.'">Initially</a>';
411 print ' | (This interface is only a demo, a proof of concept. It is very limited. No autorefresh, no private chat, etc. For full functionality use the actual Coincidence client.)'."\n";
412 print ' </div>'."\n";
414 print ' </div>'."\n";
416 print_html_body_end(\*STDOUT);
417 print_html_end(\*STDOUT);
424 if($name !~ /^[A-Za-z]+$/) {
428 $abbr = uc(substr($name,0,1));
429 $name = substr($name,1);
430 while($name =~ m/([A-Z])/g) {
439 if ($line =~ /^([a-z]*@)?([A-Za-z]*): (.*)$/) {
445 $name = $coin{'name'};
451 $abbr = abbr_name($name);
453 my $_name = html_entity_encode_dec($name , 1);
454 my $_abbr = html_entity_encode_dec($abbr , 1);
455 my $_text = html_entity_encode_dec($text , 1);
456 my $_server = html_entity_encode_dec($coin{'server'}, 1);
459 if ($action eq 'join@') {
460 return "$_name ($_abbr) joined the public chat on server $_server.";
462 elsif ($action eq 'leave@') {
463 return "$_name ($_abbr) left the public chat on server $_server.";
465 elsif ($action eq 'file@') {
466 return "$_name ($_abbr) sent the file $_text.";
473 return "<span class=\"$color\">$_abbr: $_text</span>";