-
Notifications
You must be signed in to change notification settings - Fork 0
/
miceweb
executable file
·4400 lines (4175 loc) · 129 KB
/
miceweb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
###############################################################################
#
#
# 88b d88 88
# 888b d888 ""
# 88`8b d8'88
# 88 `8b d8' 88 88 ,adPPYba, ,adPPYba,
# 88 `8b d8' 88 88 a8" "" a8P_____88
# 88 `8b d8' 88 88 8b 8PP"""""""
# 88 `888' 88 88 "8a, ,aa "8b, ,aa
# 88 `8' 88 88 `"Ybbd8"' `"Ybbd8"'
#
#
#
# I8, 8 ,8I 88
# `8b d8b d8' 88
# "8, ,8"8, ,8" 88
# Y8 8P Y8 8P ,adPPYba, 88,dPPYba,
# `8b d8' `8b d8' a8P_____88 88P' "8a
# `8a a8' `8a a8' 8PP""""""" 88 d8
# `8a8' `8a8' "8b, ,aa 88b, ,a8"
# `8' `8' `"Ybbd8"' 8Y"Ybbd8"'
#
#
# Depends on:
# https://docs.ipfs.io/install/command-line/
# https://www.gnu.org/software/wget/
# https://curl.se/
# https://github.com/stedolan/jq/
# https://github.com/kislyuk/yq
# https://github.com/mgdm/htmlq/
# https://github.com/makeworld-the-better-one/gemget/
# https://github.com/yt-dlp/yt-dlp/
#. https://www.ffmpeg.org/
# https://gitlab.torproject.org/tpo/core/torsocks/
# https://github.com/ImportTaste/wayback-machine-downloader/
# https://git-scm.com/downloads/
# https://www.gnu.org/software/bash/
#
# MiceWeb: https://github.com/Robotizing/MiceWeb
#
# Copyright (c) 2022 Robotizing Networks • robotizing.net
###############################################################################
# Return value of a pipeline is the value of the last (rightmost) command to
# exit with a non-zero status, or zero if all commands in the pipeline exit
# successfully.
set -o pipefail
# Set $IFS to only newline and tab.
#
# http://www.dwheeler.com/essays/filenames-in-shell.html
IFS=$'\n\t'
###############################################################################
# Globals
###############################################################################
# $_ME
#
# This program's basename.
_ME="$(basename "${0}")"
# $_VERSION
#
# Manually set this to to current version of the program. Adhere to the
# semantic versioning specification: http://semver.org
_VERSION="0.3" #WARNING: affects _upgrade()
_SUB_VERSION="${_VERSION}.7"
# MiceWeb Talks
_SITE_ADDRESS="1MiceWebdn35s6pUd3EM54uNveUJNSHsMr"
# $DEFAULT_SUBCOMMAND
#
# The subcommand to be run by default, when no subcommand name is specified.
# If the environment has an existing $DEFAULT_SUBCOMMAND set, then that value
# is used.
DEFAULT_SUBCOMMAND="${DEFAULT_SUBCOMMAND:-help}"
_REQUESTS_TOPIC="MiceWebRequests" #TODO: handle MICEWEB_PUBSUB_TOPIC?
_IPFS_FEATURES=""
# [[ $(ipfs help add) == *"--to-files"* ]]
if [[ "$IPFS_PATH" == "" ]]; then
_IPFS_REPO_PATH="$HOME/.ipfs"
else
_IPFS_REPO_PATH="$IPFS_PATH"
fi
SAVE_MFS_ADDR=""
SAVE_HASH=""
SUBDIR=""
###############################################################################
# Debug
###############################################################################
# _debug()
#
# Usage:
# _debug <command> <options>...
#
# Description:
# Execute a command and print to standard error. The command is expected to
# print a message and should typically be either `echo`, `printf`, or `cat`.
#
# Example:
# _debug printf "Debug info. Variable: %s\\n" "$0"
__DEBUG_COUNTER=0
_debug() {
if ((${_USE_DEBUG:-0}))
then
__DEBUG_COUNTER=$((__DEBUG_COUNTER+1))
{
# Prefix debug message with "bug (U+1F41B)"
printf "🐛 %s " "${__DEBUG_COUNTER}"
"${@}"
printf "―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n"
} 1>&2
fi
}
###############################################################################
# Error Messages
###############################################################################
# _exit_1()
#
# Usage:
# _exit_1 <command>
#
# Description:
# Exit with status 1 after executing the specified command with output
# redirected to standard error. The command is expected to print a message
# and should typically be either `echo`, `printf`, or `cat`.
_exit_1() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
exit 1
}
# _warn()
#
# Usage:
# _warn <command>
#
# Description:
# Print the specified command with output redirected to standard error.
# The command is expected to print a message and should typically be either
# `echo`, `printf`, or `cat`.
_warn() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
}
###############################################################################
# Utility Functions
###############################################################################
# _command_exists()
#
# Usage:
# _command_exists <name>
#
# Exit / Error Status:
# 0 (success, true) If a command with <name> is defined in the current
# environment.
# 1 (error, false) If not.
#
# Information on why `hash` is used here:
# http://stackoverflow.com/a/677212
_command_exists() {
command hash "${1}" 2>/dev/null
}
# _contains()
#
# Usage:
# _contains <query> <list-item>...
#
# Exit / Error Status:
# 0 (success, true) If the item is included in the list.
# 1 (error, false) If not.
#
# Examples:
# _contains "${_query}" "${_list[@]}"
_contains() {
local _query="${1:-}"
shift
if [[ -z "${_query}" ]] ||
[[ -z "${*:-}" ]]
then
return 1
fi
for __element in "${@}"
do
[[ "${__element}" == "${_query}" ]] && return 0
done
return 1
}
# _join()
#
# Usage:
# _join <delimiter> <list-item>...
#
# Description:
# Print a string containing all <list-item> arguments separated by
# <delimeter>.
#
# Example:
# _join "${_delimeter}" "${_list[@]}"
#
# More information:
# https://stackoverflow.com/a/17841619
_join() {
local _delimiter="${1}"
shift
printf "%s" "${1}"
shift
printf "%s" "${@/#/${_delimiter}}" | tr -d '[:space:]'
}
# _blank()
#
# Usage:
# _blank <argument>
#
# Exit / Error Status:
# 0 (success, true) If <argument> is not present or null.
# 1 (error, false) If <argument> is present and not null.
_blank() {
[[ -z "${1:-}" ]]
}
# _interactive_input()
#
# Usage:
# _interactive_input
#
# Exit / Error Status:
# 0 (success, true) If the current input is interactive (eg, a shell).
# 1 (error, false) If the current input is stdin / piped input.
_interactive_input() {
[[ -t 0 ]]
}
# _piped_input()
#
# Usage:
# _piped_input
#
# Exit / Error Status:
# 0 (success, true) If the current input is stdin / piped input.
# 1 (error, false) If the current input is interactive (eg, a shell).
_piped_input() {
! _interactive_input
}
###############################################################################
# describe
###############################################################################
# describe()
#
# Usage:
# describe <name> <description>
# describe --get <name>
#
# Options:
# --get Print the description for <name> if one has been set.
#
# Examples:
# ```
# describe "list" <<HEREDOC
# Usage:
# ${_ME} list
#
# Description:
# List items.
# HEREDOC
#
# describe --get "list"
# ```
#
# Set or print a description for a specified subcommand or function <name>. The
# <description> text can be passed as the second argument or as standard input.
#
# To make the <description> text available to other functions, `describe()`
# assigns the text to a variable with the format `$___describe_<name>`.
#
# When the `--get` option is used, the description for <name> is printed, if
# one has been set.
#
# NOTE:
#
# The `read` form of assignment is used for a balance of ease of
# implementation and simplicity. There is an alternative assignment form
# that could be used here:
#
# var="$(cat <<'HEREDOC'
# some message
# HEREDOC
# )
#
# However, this form appears to require trailing space after backslases to
# preserve newlines, which is unexpected. Using `read` simply requires
# escaping backslashes, which is more common.
describe() {
_debug printf "describe() \${*}: %s\\n" "$@"
[[ -z "${1:-}" ]] && _exit_1 printf "describe(): <name> required.\\n"
if [[ "${1}" == "--get" ]]
then # get ------------------------------------------------------------------
[[ -z "${2:-}" ]] &&
_exit_1 printf "describe(): <description> required.\\n"
local _name="${2:-}"
local _describe_var="___describe_${_name}"
if [[ -n "${!_describe_var:-}" ]]
then
printf "%s\\n" "${!_describe_var}"
else
printf "No additional information for \`%s\`\\n" "${_name}"
fi
else # set ------------------------------------------------------------------
if [[ -n "${2:-}" ]]
then # argument is present
read -r -d '' "___describe_${1}" <<HEREDOC
${2}
HEREDOC
else # no argument is present, so assume piped input
# `read` exits with non-zero status when a delimeter is not found, so
# avoid errors by ending statement with `|| true`.
read -r -d '' "___describe_${1}" || true
fi
fi
}
###############################################################################
# Program Option Parsing
#
# NOTE: The `getops` builtin command only parses short options and BSD `getopt`
# does not support long arguments (GNU `getopt` does), so use custom option
# normalization and parsing.
#
# For a pure bash `getopt` function, try pure-getopt:
# https://github.com/agriffis/pure-getopt
#
# More info:
# http://wiki.bash-hackers.org/scripting/posparams
# http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
# http://stackoverflow.com/a/14203146
# http://stackoverflow.com/a/7948533
# https://stackoverflow.com/a/12026302
# https://stackoverflow.com/a/402410
###############################################################################
# Normalize Options ###########################################################
# Source:
# https://github.com/e36freak/templates/blob/master/options
# Iterate over options, breaking -ab into -a -b and --foo=bar into --foo bar
# also turns -- into --endopts to avoid issues with things like '-o-', the '-'
# should not indicate the end of options, but be an invalid option (or the
# argument to the option, such as wget -qO-)
unset options
# while the number of arguments is greater than 0
while ((${#}))
do
case "${1}" in
# if option is of type -ab
-[!-]?*)
# loop over each character starting with the second
for ((i=1; i<${#1}; i++))
do
# extract 1 character from position 'i'
c="${1:i:1}"
# add current char to options
options+=("-${c}")
done
;;
# if option is of type --foo=bar, split on first '='
--?*=*)
options+=("${1%%=*}" "${1#*=}")
;;
# end of options, stop breaking them up
--)
options+=(--endopts)
shift
options+=("${@}")
break
;;
# otherwise, nothing special
*)
options+=("${1}")
;;
esac
shift
done
# set new positional parameters to altered options. Set default to blank.
set -- "${options[@]:-}"
unset options
# Parse Options ###############################################################
_SUBCOMMAND=""
_SUBCOMMAND_ARGUMENTS=()
_USE_DEBUG=0
while ((${#}))
do
__opt="${1}"
shift
case "${__opt}" in
-h|--help)
_SUBCOMMAND="help"
;;
--debug)
_USE_DEBUG=1
;;
*)
# The first non-option argument is assumed to be the subcommand name.
# All subsequent arguments are added to $_SUBCOMMAND_ARGUMENTS.
if [[ -n "${_SUBCOMMAND}" ]]
then
_SUBCOMMAND_ARGUMENTS+=("${__opt}")
else
_SUBCOMMAND="${__opt}"
fi
;;
esac
done
###############################################################################
# Main
###############################################################################
# Declare the $_DEFINED_SUBCOMMANDS array.
_DEFINED_SUBCOMMANDS=(backup check cleanup commands count daemon find hash help history import log present remove request resolve save snapshots talks url urls version zites)
# _main()
#
# Usage:
# _main
#
# Description:
# The primary function for starting the program.
#
# NOTE: must be called at end of program after all subcommands are defined.
_main() {
# If $_SUBCOMMAND is blank, then set to `$DEFAULT_SUBCOMMAND`
if [[ -z "${_SUBCOMMAND}" ]]
then
_SUBCOMMAND="${DEFAULT_SUBCOMMAND}"
fi
#for __name in $(declare -F)
#do
# Each element has the format `declare -f function_name`, so set the name
# to only the 'function_name' part of the string.
# local _function_name
# _function_name=$(printf "%s" "${__name}" | awk '{ print $3 }')
# if ! { [[ -z "${_function_name:-}" ]] ||
# [[ "${_function_name}" =~ ^_(.*) ]] ||
# [[ "${_function_name}" == "bats_readlinkf" ]] ||
# [[ "${_function_name}" == "describe" ]] ||
# [[ "${_function_name}" == "shell_session_update" ]]
# }
# then
# _DEFINED_SUBCOMMANDS+=("${_function_name}")
# echo -n "${_function_name} "
# fi
#done
# If the subcommand is defined, run it, otherwise return an error.
if _contains "${_SUBCOMMAND}" "${_DEFINED_SUBCOMMANDS[@]:-}"
then
# Pass all comment arguments to the program except for the first ($0).
${_SUBCOMMAND} "${_SUBCOMMAND_ARGUMENTS[@]:-}"
else
local SOMETHING="${_SUBCOMMAND}"
local URL=""
local SECOND_ARG="${_SUBCOMMAND_ARGUMENTS[0]}"
if [[ "$SOMETHING" =~ ^[0-9a-f]{40}$ ]]; then
#TODO: implement import
URL=$(url "$SOMETHING")
if [ "$URL" == "" ]; then
_warn_empty_line
return 1
fi
_snapshots_and_save_or_import "$SOMETHING" "$SECOND_ARG" | _skip_duplicates | _echo_existing_page_snapshots
_warn_empty_line
return 0
elif [[ "$SOMETHING" =~ ^[0-9a-f]{40}/[0-9]{8}GMT[0-9]{6}-. ]]; then
local SNAP="$SOMETHING"
if [[ "$SNAP" =~ ^[0-9a-f]{40}/[0-9]{8}GMT[0-9]{6}-archive$ ]]; then
save "$SNAP" | _echo_existing_page_snapshots
elif [[ "$SNAP" =~ ^[0-9a-f]{40}/[0-9]{8}GMT[0-9]{6}-importing$ ]]; then
_echo_existing_page_snapshot $SNAP
>&2 echo "Resuming getting content from IPFS Network"
check $SNAP 1>&2
elif [[ "$SNAP" =~ ^[0-9a-f]{40}/[0-9]{8}GMT[0-9]{6}-import$ ]]; then
if _safe_with_warnings ipfs files ls //MiceWeb/pages/$SNAP &>/dev/null; then
_echo_existing_page_snapshot $SNAP
elif _safe_with_warnings ipfs files ls "//MiceWeb/pages/$SNAP"ing >/dev/null; then
_echo_existing_page_snapshot $SNAP
>&2 echo "Resuming getting content from IPFS Network"
check $SNAP 1>&2
else
_warn_empty_line
return 1
fi
elif _safe_with_warnings ipfs files ls //MiceWeb/pages/$SNAP >/dev/null; then
_echo_existing_page_snapshot $SNAP
else
_warn_empty_line
return 1
fi
_warn_empty_line
return 0
elif [[ "$SOMETHING" =~ ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$ ]]; then
_snapshots_and_save_or_import "$SOMETHING" | _echo_existing_zite_snapshots
_warn_empty_line
return 0
elif [[ "$SOMETHING" =~ ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}/[0-9]{8}GMT[0-9]{6}-. ]]; then
local SNAP="$SOMETHING"
if _safe_with_warnings ipfs files ls //MiceWeb/zites/$SNAP >/dev/null; then
_echo_existing_zite_snapshot $SNAP
fi
_warn_empty_line
return 0
fi
URL="$SOMETHING"
if _check_url "$URL"; then
_snapshots_and_save_or_import "$URL" "$SECOND_ARG" | _skip_duplicates | _echo_existing_page_snapshots
else
_exit_1 printf "Unknown command: %s\\n" "${_SUBCOMMAND}"
fi
fi
if [[ "${_SUBCOMMAND_ARGUMENTS[0]}" != "--no-warnings" ]]; then
_warn_empty_line
fi
}
# --------------------------------------------------------------------- functions
_add_to_history() {
mkdir -p "$HOME/.miceweb"
local DT=$(export TZ=GMT ; date '+%Y-%m-%d GMT %H:%M:%S')
printf "%s\t%s\n" "$DT" "$1" >> "$HOME/.miceweb/history.txt"
}
_cat_file() {
local FIL="$1"
local GREP="$2"
local ADDR=""
if [[ "$FIL" == /ipns/* ]]; then
ADDR=$(_resolve_ipns "${FIL#/ipns/}")
if [[ "$GREP" != "" ]]; then
_safe ipfs cat "$ADDR" | grep -a "$GREP"
else
_safe ipfs cat "$ADDR"
fi
elif [[ "$FIL" == /ipfs/* ]]; then
ADDR="${FIL#/ipfs/}"
if [[ "$GREP" != "" ]]; then
_safe ipfs cat "$ADDR" | grep -a "$GREP"
else
_safe ipfs cat "$ADDR"
fi
elif [[ "$FIL" == /zeronet/* ]]; then
ADDR=$(_resolve_zeronet "${FIL#/zeronet/}")
if [[ $ADDR != "" ]]; then
local datadir="$(_zeronet_data)"
if [ -d "$datadir" ]; then
local filename="$datadir/$ADDR"
if [ -e "$filename" ]; then
grep -a "$GREP" "$filename"
else
>&2 echo "Error: file not found, visit http://127.0.0.1:43110/$ADDR in a browser and try again"
return 1
fi
else
>&2 echo "Error: '$datadir' not found,"
>&2 echo " check ZERONET_PATH environment variable"
return 1
fi
else
return 1
fi
else
grep -a "$GREP" "$FIL"
fi
return $?
}
_cat_docx_file() {
if _command_exists unzip; then
local FIL="$1"
if [[ "$FIL" != /ipns/* && "$FIL" != /ipfs/* && "$FIL" != /zeronet/* ]]; then
unzip -p "$FIL" word/document.xml | _cat_xml_file --
else
local MYTMPFILE=$(_temp_file)
_cat_file "$FIL" >$MYTMPFILE
_cat_docx_file "$MYTMPFILE"
local RET=$?
rm "$MYTMPFILE"
return $RET
fi
else
>&2 echo "Error: unzip is not installed"
return 1
fi
}
_cat_json_file() {
if _command_exists jq; then
_cat_file "$1" | jq --raw-output '.. | strings'
else
>&2 _print_with_hyperlink "Error: jq is not installed (seeing " "https://stedolan.github.io/jq/download/" ")"
return 1
fi
}
_cat_toml_file() {
if _command_exists tomlq; then
if _command_exists jq; then
_cat_file "$1" | tomlq | jq --raw-output '.. | strings'
else
>&2 _print_with_hyperlink "Error: jq is not installed (seeing " "https://stedolan.github.io/jq/download/" ")"
return 1
fi
else
>&2 _print_with_hyperlink "Error: tomlq is not installed (seeing " "https://kislyuk.github.io/yq/" ")"
return 1
fi
}
_cat_xml_file() {
if _command_exists xq; then
if _command_exists jq; then
_cat_file "$1" | xq | jq --raw-output '.. | strings'
return 0
else
>&2 _print_with_hyperlink "Error: jq is not installed (seeing " "https://stedolan.github.io/jq/download/" ")"
fi
else
>&2 _print_with_hyperlink "Error: xq is not installed (seeing " "https://kislyuk.github.io/yq/#installation" ")"
fi
>&2 echo "Warning: using alternate method to parse XML"
_cat_file "$1" | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g; s/\&/\&/g'
}
_cat_yaml_file() {
if _command_exists yq; then
if _command_exists jq; then
_cat_file "$1" | yq | jq --raw-output '.. | strings'
else
>&2 _print_with_hyperlink "Error: jq is not installed (seeing " "https://stedolan.github.io/jq/download/" ")"
return 1
fi
else
>&2 _print_with_hyperlink "Error: yq is not installed (seeing " "https://kislyuk.github.io/yq/#installation" ")"
return 1
fi
}
_check_free_space_for_temp() {
if [ $(_free_space $(dirname $(mktemp -u 2>/dev/null || mktemp -u -t 'mytmpdir'))) -gt 1048576 ]; then
return 0
else
return 1
fi
}
_check_url() {
local URL="$1"
if [[ "$URL" != http://* && "$URL" != https://* && "$URL" != gopher://* && "$URL" != gemini://* && "$URL" != ipns://* && "$URL" != ipfs://* && "$URL" != ftp://* && "$URL" != ftps://* && "$URL" != git://* ]]; then
return 1
else
return 0
fi
}
_current_timestamp() {
export TZ=GMT ; date '+%Y%m%dGMT%H%M%S'
}
_echo_saved_page_snapshot() {
echo "$SAVE_HASH/$SUBDIR"
}
_echo_saved_zite_snapshot() {
echo "$SAVE_HASH/$SUBDIR"
}
_echo_existing_page_snapshot() {
local GATEWAY=$(_ipfs_gateway)
local SNAP="$1"
if [[ "$GATEWAY" != "" ]]; then
local ADDR=$(present --no-warnings "$SNAP")
if [[ "$ADDR" != "" ]]; then
_print_hyperlink $GATEWAY"$ADDR" "$SNAP"
echo ""
else
echo "$SNAP"
fi
else
echo "$SNAP"
fi
}
_echo_existing_page_snapshots() {
while IFS= read -r I
do
_echo_existing_page_snapshot $I
done
}
_echo_existing_zite_snapshot() {
local GATEWAY=$(_ipfs_gateway)
local SNAP="$1"
if [[ "$GATEWAY" != "" ]]; then
local CID="$(_safe_with_warnings ipfs files stat --hash //MiceWeb/zites/$SNAP 2>/dev/null)"
_print_hyperlink "$GATEWAY/ipfs/$CID" "$SNAP"
echo ""
else
echo "$SNAP"
fi
}
_echo_existing_zite_snapshots() {
while IFS= read -r I
do
_echo_existing_zite_snapshot $I
done
}
_echo_supported_url_prefixes() {
>&2 echo "Supported URL prefixes:"
>&2 echo " http://, https://, gopher://, gemini://,"
>&2 echo " ftp://, ftps://, ipns://, ipfs://, git://"
}
_skip_duplicates() {
awk '!a[$0]++; fflush()'
}
_free_space() {
echo $(df -Pk $1 | sed 1d | grep -v used | awk '{ print $4 "\t" }')
}
_git() {
# no checkout, no recursive, no LFS (https://stackoverflow.com/questions/42019529/how-to-clone-pull-a-git-repository-ignoring-lfs)
# use snapshot with 'git push --mirror <path>' as described in https://blog.plataformatec.com.br/2013/05/how-to-properly-mirror-a-git-repository/
local URL="$1"
local MYTMPDIR=$(_temp_directory)
cd $MYTMPDIR
GIT_LFS_SKIP_SMUDGE=1 GIT_ASKPASS=false GIT_TERMINAL_PROMPT=0 git clone --quiet --m "$URL" . &>/dev/null
local GITERR=$?
local RET=1
if [[ $GITERR -eq 0 ]]; then
local CID="$(_ipfs_add_directory_to_mfs $MYTMPDIR $SAVE_MFS_ADDR/$SUBDIR --hidden)"
if [[ "$CID" != "" ]]; then
_echo_saved_page_snapshot $CID
_update_base_for_added_url "$URL"
RET=$GITERR
fi
else
>&2 echo "Error: could not download '${SUBDIR:18}', git clone err. $GITERR"
fi
cd ..
rm -rf $MYTMPDIR
return $RET
}
_grep_supported_urls() {
grep -e "^http://" -e "^https://" -e "^gopher://" -e "^gemini://" -e "^ftp://" -e "^ftps://" -e "^ipfs://" -e "^ipns://" -e "^git://"
}
_has_url_fragment() {
local URL="$1"
if [[ "$URL" =~ ['#'] ]]; then
return 0;
else
return 1;
fi
}
_has_url_query() {
local URL=$(_url_but_fragment "$1")
if [[ "$URL" =~ ['?'] ]]; then
return 0;
else
return 1;
fi
}
_hash() {
_hash_string "$(_url_but_fragment "$1")"
}
_hash_raw() {
local URL="$1"
local HASH=$(_hash "$URL")
if [[ "$HASH" != "" ]]; then
local MFS_ADDR="/MiceWeb/pages/$HASH"
_safe ipfs files ls /$MFS_ADDR/URL.txt >/dev/null
if [ $? -ne 0 ]; then
_mkdir_miceweb pages
_safe ipfs files mkdir /$MFS_ADDR
local URL0=$(_url_but_fragment "$URL")
printf "%s\n%s\n" "URL $(_random_value)" "$URL0" | _ipfs_add_pipe_to_mfs $MFS_ADDR/URL.txt
_update_base_for_added_url "$URL0"
fi
echo $HASH
return 0
else
return 1
fi
}
_hash_string() {
local HASH=$(printf '%s' "$1" | sha1sum 2>/dev/null | cut -d' ' -f1)
if [[ "$HASH" != "" ]]; then
echo "$HASH"
else
HASH=$(printf '%s' "$1" | openssl sha1 -r 2>/dev/null | cut -d' ' -f1)
if [[ "$HASH" != "" ]]; then
echo "$HASH"
else
HASH=$(printf '%s' "$1" | shasum -a 1 2>/dev/null | cut -d' ' -f1)
if [[ "$HASH" != "" ]]; then
echo "$HASH"
else
_exit_1 printf "Installed sha1sum, shasum or openssl is required for MiceWeb\n"
fi
fi
fi
}
_initiate_saving_url() {
local URL="$1"
SAVE_HASH=$(_hash_raw "$URL")
if [[ "$SAVE_HASH" != "" ]]; then
SAVE_MFS_ADDR="/MiceWeb/pages/$SAVE_HASH"
return 0
else
return 1
fi
}
_ip_address_by_domain() {
local DOMAIN=$(echo $1 | cut -d "[" -f2 | cut -d "]" -f1)
if echo $DOMAIN | grep -E "(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}" &>/dev/null; then
echo $DOMAIN # IPv4
elif echo $DOMAIN | grep -E "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))" &>/dev/null; then
echo $DOMAIN # IPv6
else
ping -n 1 -i 1 $DOMAIN 2>&1 | grep -a $DOMAIN | cut -d "[" -f2 | cut -d "]" -f1 | cut -d "(" -f2 | cut -d ")" -f1
fi
}
# for data
_ipfs_add_directory_to_mfs() {
local path="$1"
local addr="$2"
local flag="$3"
if [[ "$addr" != "" ]]; then
local CID="$(_safe ipfs add --quieter --pin=false -r $flag $path --to-files /$addr)"
if [[ "$CID" != "" ]]; then
if [[ "$CID" == "$(_safe ipfs files stat --hash /$addr)" ]]; then
echo "$CID"
return 0
fi
else
CID="$(_safe ipfs add --quieter --pin=false -r $flag $path)"
if [[ "$CID" != "" ]]; then
if _safe ipfs files cp //ipfs/$CID /$addr; then
echo "$CID"
return 0
elif [[ "$CID" == "$(_safe ipfs files stat --hash /$addr)" ]]; then
echo "$CID"
return 0
fi
fi
fi
>&2 echo "Error: can't add directory to MFS"
fi
return 1
}
_ipfs_add_file_to_mfs() {
local path="$1"
local addr="$2"
if [[ "$addr" != "" ]]; then
local CID="$(_safe ipfs add --quieter --pin=false $path --to-files /$addr)"
if [[ "$CID" != "" ]]; then
if [[ "$CID" == "$(_safe ipfs files stat --hash /$addr)" ]]; then
echo "$CID"
return 0
fi
else
CID="$(_safe ipfs add --quieter --pin=false $path)"
if [[ "$CID" != "" ]]; then
if _safe ipfs files cp //ipfs/$CID /$addr; then
echo "$CID"
return 0
elif [[ "$CID" == "$(_safe ipfs files stat --hash /$addr)" ]]; then
echo "$CID"
return 0
fi
fi
fi
>&2 echo "Error: can't add file to MFS"
fi
return 1
}
# for logs and settings
_ipfs_add_pipe_to_mfs() {
local addr="$1"
if [[ "$addr" != "" ]]; then
_safe_with_warnings ipfs files write --create --truncate /$addr
return $?
fi
return 1
}
_ipfs_gateway() { # Kubo v.0.15.0+
local GATEWAY=$(cat "$_IPFS_REPO_PATH/gateway" 2>/dev/null)
echo "${GATEWAY/127.0.0.1/localhost}"
return $?
}
_is_cid_of_empty_file() {
if [[ "$1" == "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" || "$1" == "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku" ]]; then
return 0
else
return 1
fi
}
_mkdir_miceweb() {
_safe ipfs files mkdir --cid-version=0 //MiceWeb
if [[ "$1" != "" ]]; then
_safe ipfs files mkdir //MiceWeb/$1
if [[ "$2" != "" ]]; then
_safe ipfs files mkdir //MiceWeb/$1/$2
if [[ "$3" != "" ]]; then
_safe ipfs files mkdir //MiceWeb/$1/$2/$3
fi
fi
fi
}
_news() {
if cat "$(_zeronet_data)/$_SITE_ADDRESS/news.txt" 2>/dev/null; then
echo ""
echo ""
fi
echo "Invintation:"
echo " Run 'miceweb talks' to find answers together"
}
# https://www.rfc-editor.org/rfc/rfc3986#appendix-B
#
readonly URI_REGEX='^(([^:/?#]+):)?(//((([^:/?#]+)@)?([^:/?#]+)(:([0-9]+))?))?(/([^?#]*))(\?([^#]*))?(#(.*))?'
# ↑↑ ↑ ↑↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
# |2 scheme | ||6 userinfo 7 host | 9 port | 11 rpath | 13 query | 15 fragment
# 1 scheme: | |5 userinfo@ 8 :… 10 path 12 ?… 14 #…
# | 4 authority
# 3 //…
_parse_scheme () {
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[2]}"
}
_parse_authority () {
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[4]}"
}
_parse_user () {
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[6]}"
}
_parse_host () {
[[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[7]}"