-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetcatchat
More file actions
executable file
·804 lines (783 loc) · 28 KB
/
netcatchat
File metadata and controls
executable file
·804 lines (783 loc) · 28 KB
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
#!/usr/bin/env bash
# Set strict mode
#set -euo pipefail
# Main function wrapper
main() {
# Check if common-functions exists
if [[ ! -f "$(dirname "$0")/common-functions" ]]; then
echo "Downloading common-functions from GitHub..."
if ! curl -fsSL https://raw.githubusercontent.com/Flower7C3/bash-tools/master/common-functions -o "$(dirname "$0")/common-functions"; then
echo "Failed to download common-functions"
exit 1
fi
fi
# Source common functions
source "$(dirname "$0")/common-functions"
# Call the actual main function
netcatchat_main "$@"
}
netcatchat_main() {
declare -A chat_users=()
declare -r config_file_path="${HOME}/.config/netcatchat/config.sh"
declare -r history_file_path="${HOME}/.config/netcatchat/history.txt"
function show_usage() {
log_usage_title '[-p <port>] [-n <@nickname>]'
echo
log_header 'Options'
log_usage_options_line '-p <port>;--port <port>' \
'sender port, default <b>%d</b>' "$sender_port"
log_usage_options_line '-n <@nickname>;--nickname <@nickname>' \
'sender @nickname, default <b>%s</b>' "$sender_nickname"
log_usage_options_line '-u;--update' \
'Update app'
log_usage_options_line '-h;--help' \
'Show this help message'
exit 0
}
function show_commands() {
log_header 'Commands'
log_usage_options_line '@nickname message' \
'write message to user, use <b>@all</b> to broadcast'
log_usage_options_line '/p;/port' \
'setup app port'
log_usage_options_line '/n;/nick' \
'setup Your @nickname'
log_usage_options_line '/l [c|check];/list [c|check]' \
'list saved recipients'
log_usage_options_line '/c <@nickname> <ip/addr> <port>;/create <@nickname> <ip/addr> <port>' \
'create new recipient'
log_usage_options_line '/u <@nickname> <ip/addr> <port>;/update <@nickname> <ip/addr> <port>' \
'update saved recipient data'
log_usage_options_line '/r <@nickname_old> <@nickname_new>;/rename <@nickname_old> <@nickname_new>' \
'change (rename) recipient <b>@nickname</b>'
log_usage_options_line '/d <@nickname>;/delete <@nickname>' \
'delete recipient'
log_usage_options_line '/s;/scan' \
'scan local network for hosts with open <u>%d</u> port' "$NETCAT_CHAT_APP_PORT"
log_usage_options_line '/w;/welcome' \
'send welcome message to all users from Your recipients list'
log_usage_options_line '/x;/clear' \
'clear history'
log_usage_options_line '/h;/help' \
'help screen'
log_usage_options_line '/q;/exit;/quit;/bye' '<ctrl>+c' \
'exit app'
}
### VARIABLES ###
readonly NETCAT_CHAT_SYSTEM_NICKNAME='@system'
readonly NETCAT_CHAT_BROADCAST_NICKNAME='@all'
readonly NETCAT_CHAT_APP_PORT='2812'
readonly NICKNAME_CHECK_MODE_IGNORE='nickname_validate_mode_ignore'
readonly NICKNAME_CHECK_MODE_ERR_IF_EXISTS='nickname_validate_mode_err_if_exists'
readonly NICKNAME_CHECK_MODE_ERR_IF_NOT_EXISTS='nickname_validate_mode_err_if_not_exists'
if uname | grep -iq Darwin; then
# shellcheck disable=SC2155
declare -r sender_hostname="$(ipconfig getifaddr en0)"
else
# shellcheck disable=SC2155
declare -r sender_hostname="$(ip route get 1 | awk '{print $NF;exit}')"
fi
nc_pid=-1
sender_port=''
sender_nickname=''
option=''
### CONFIG ###
function config_load() {
if [[ ! -f "$config_file_path" ]]; then
log_info ---status "$config_file_path" 'Create config file'
mkdir -p "$(dirname "$config_file_path")"
touch "$config_file_path"
chmod 400 "$config_file_path"
else
log_info ---status "$config_file_path" 'Read config file'
# shellcheck disable=SC1090
source "$config_file_path"
fi
history -r "$history_file_path"
if [[ -z "$sender_port" ]]; then
app_setup_port
fi
if [[ -z "$sender_nickname" ]]; then
sender_nickname="@$(whoami)"
app_setup_nickname
fi
if [[ "${#chat_users[*]}" -eq "0" ]]; then
# shellcheck disable=SC2059
log_info 'You have no recipients on contact list, type <b>/create</b> to add new person'
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-p | --port)
shift
local _sender_port_error
_sender_port_error=$(validate_port "$1")
if [[ -n "$_sender_port_error" ]]; then
log_error 'Invalid sender port from argv'
exit 2
else
readonly sender_port=$1
fi
;;
-n | --nickname)
shift
local _sender_nickname_error
_sender_nickname_error=$(validate_nickname "$1" "$NICKNAME_CHECK_MODE_IGNORE")
if [[ -n "$_sender_nickname_error" ]]; then
log_error 'Invalid sender nickname from argv'
exit 2
else
readonly sender_nickname=$1
fi
;;
-h | --help)
show_usage
;;
-u | --update)
update_application
;;
-*)
log_error 'Unknown option: <b>%s</b>' "$1"
echo
show_usage
;;
*)
log_error 'Unexpected argument: <b>%s</b>' "$1"
echo
show_usage
;;
esac
shift
done
local _has_previous_process
# shellcheck disable=SC2009
read -r -a _has_previous_process <<<"$(ps -a | grep "nc -l -k ${sender_port}" | grep -v 'grep' | awk '{print $1;}')"
if [[ "${#_has_previous_process[*]}" -gt "0" ]]; then
for _pid in "${_has_previous_process[@]}"; do
kill "$_pid"
done
fi
}
function config_create() {
local _variable_name="$1"
local _variable_value="$2"
local _variable_delete_error
config_delete "$_variable_name"
eval "${_variable_name}"'=${_variable_value}' 2>&1
chmod 600 "$config_file_path"
printf '%s="%s"'"\n" "$_variable_name" "$_variable_value" >>"$config_file_path"
chmod 400 "$config_file_path"
}
function config_delete() {
local _variable_name="$1"
local _variable_name_escaped=$_variable_name
_variable_name_escaped=${_variable_name_escaped/\[/\\\[}
_variable_name_escaped=${_variable_name_escaped/\]/\\\]}
eval "unset ${_variable_name}" 2>&1
chmod 600 "$config_file_path"
sed -i '' '/'"$_variable_name_escaped"'/d' "$config_file_path"
chmod 400 "$config_file_path"
}
### NETCAT ###
function netcat_start() {
if [[ -n "$sender_port" ]]; then
log_success ---status "$BASHPID" "Started netcat chat"
nc -l -k "$sender_port" 2>/dev/null &
nc_pid=$!
log_success ---status "$nc_pid" "Started netcat server on %d port" "$sender_port"
message_from_system "<b>${sender_nickname}</b> in now connected to netcatchat from <u>${sender_hostname} ${sender_port}</u>"
else
log_error 'Sender port is not defined'
app_setup_port
fi
}
function netcat_kill() {
if [[ "$nc_pid" -gt "0" ]]; then
message_from_system "<b>${sender_nickname}</b> has left the building!"
loading_animation_loop ---message 'Closing netcat server'
kill ${nc_pid}
loading_animation_loop ---message 'Closed netcat server' ---status "$nc_pid"
fi
}
### TRAP ###
ctrl_c_trap_active='n'
function ctrl_c_trap_init() {
ctrl_c_trap_active='n'
trap 'ctrl_c_trap_active="y";return;' SIGINT
}
function ctrl_c_trap_remove() {
ctrl_c_trap_active='n'
trap - SIGINT
}
function ctrl_c_trap_exec() {
if [[ "$ctrl_c_trap_active" == "y" ]]; then
echo
return 0
fi
return 1
}
### HISTORY ###
function history_save() {
history -s "$option"
history -w "$history_file_path"
}
function history_clear() {
log_success 'History removed'
history -c
}
### NAMES ###
function name_to_nickname() {
printf "@%s" "${1}"
}
function nickname_to_name() {
printf "%s" "${1/@/}"
}
### VALIDATION ###
function validate_nickname() {
local _nickname="$1"
local _validate_exists_mode="$2"
local _name
_name=$(nickname_to_name "$_nickname")
if [[ ! "$_nickname" == @* ]]; then
log_error 'Nickname <b>%s</b> is invalid, all nicknames starts with <b>@</b> sign' "$_nickname"
# return 1
elif [[ "$_nickname" == "$NETCAT_CHAT_SYSTEM_NICKNAME" ]] || [[ "$_nickname" == "$NETCAT_CHAT_BROADCAST_NICKNAME" ]]; then
log_error 'Values <b>%s</b> and <b>%s</b> are reserved nicknames' "$NETCAT_CHAT_SYSTEM_NICKNAME" "$NETCAT_CHAT_BROADCAST_NICKNAME"
# return 2
# elif [[ "${#chat_users[@]}" -gt "0" ]] && [[ -z "${chat_users[$_name]}" ]] && [[ "$_validate_exists_mode" == "$NICKNAME_CHECK_MODE_ERR_IF_EXISTS" ]]; then
# log_error 'Recipient <b>%s</b> is already defined' "$_nickname"
# return 3
# elif [[ "${#chat_users[@]}" -gt "0" ]] && [[ -z "${chat_users[$_name]}" ]] && [[ "$_validate_exists_mode" == "$NICKNAME_CHECK_MODE_ERR_IF_NOT_EXISTS" ]]; then
# log_error 'Recipient <b>%s</b> is not defined' "$_nickname"
# return 4
# else
# return 0
fi
}
function validate_hostname() {
local _hostname=$1
if [[ ! "$_hostname" =~ ^[A-Za-z0-9._%+-]+$ ]]; then
log_error 'Hostname/IP address <b>%s</b> is invalid' "$_hostname"
return 1
else
return 0
fi
}
function validate_port() {
local _port=$1
if [[ ! "$_port" =~ ^[0-9]+$ ]]; then
log_error 'Port <b>%s</b> number is invalid' "$_port"
return 1
elif [[ "$_port" -lt "1024" ]] || [[ "$_port" -gt "65536" ]]; then
log_error 'Port number must be between <b>1024</b> and <b>65536</b>'
return 2
else
return 0
fi
}
### SENDER ###
function app_setup_port() {
local _new_sender_port="${1:-}"
local _new_sender_port_error
while true; do
prompt_input '_new_sender_port' 'Setup sender port number' "$_new_sender_port"
if ctrl_c_trap_exec; then
return
fi
_new_sender_port_error=$(validate_port "$_new_sender_port")
if [[ -z "$_new_sender_port_error" ]]; then
break
else
echo "$_new_sender_port_error"
_new_sender_port=''
fi
done
config_create 'sender_port' "$_new_sender_port"
log_success 'Sender port saved' ---status "$_new_sender_port"
}
function app_setup_nickname() {
local _new_sender_nickname="${1:-}"
local _new_sender_nickname_error
while true; do
prompt_input '_new_sender_nickname' 'Setup sender nickname' "$_new_sender_nickname"
if ctrl_c_trap_exec; then
return
fi
_new_sender_nickname_error=$(validate_nickname "$_new_sender_nickname" "$NICKNAME_CHECK_MODE_IGNORE")
if [[ -z "$_new_sender_nickname_error" ]]; then
break
else
echo "$_new_sender_nickname_error"
_new_sender_nickname=''
fi
done
local _old_sender_nickname=$sender_nickname
config_create 'sender_nickname' "$_new_sender_nickname"
log_success 'Sender nickname saved' "$_new_sender_nickname"
if [[ -n "$_old_sender_nickname" ]]; then
message_from_system "$_old_sender_nickname is now known as $_new_sender_nickname"
fi
}
### RECIPIENTS ###
function recipients_list() {
local _flag="${1:-}"
if [[ "${#chat_users[*]}" -eq "0" ]]; then
# shellcheck disable=SC2059
log_error 'No recipients found, use <b>/create</b> to define new recipient'
else
for _recipient_name in "${!chat_users[@]}"; do
# check status
local _recipient_nickname
_recipient_nickname=$(name_to_nickname "$_recipient_name")
# shellcheck disable=SC2206
local _recipient_address=(${chat_users[$_recipient_name]})
case $_flag in
c | check)
log_info '%s %s' "$_recipient_nickname" "${_recipient_address[*]}"
local _recipient_online_status=0
recipient_online_status=$(ping "${_recipient_address[0]}" -c 1 -t 1 2>/dev/null | grep -e 'packets' | awk '{print $4;}')
if [[ "$recipient_online_status" -gt "0" ]]; then
local _recipient_connection_status
recipient_connection_status=$(nmap "${_recipient_address[0]}" -p "${_recipient_address[1]}" | grep -e '/tcp' | awk '{print $2;}')
if [[ "$recipient_connection_status" == "open" ]]; then
log_success '<b>%s</b> %s is online' "$_recipient_nickname" "${_recipient_address[*]}"
else
log_error '<b>%s</b> %s is not connected to app' "$_recipient_nickname" "${_recipient_address[*]}"
fi
else
log_error '<b>%s</b> %s is offline' "$_recipient_nickname" "${_recipient_address[*]}"
fi
;;
*)
log_info '<b>%s</b> %s' "$_recipient_nickname" "${_recipient_address[*]}"
;;
esac
done
fi
}
function hosts_scan_and_recipient_create() {
local _nickname
local _ip
local _port_state
local _ip_range
_ip_range=$(echo "$sender_hostname" | awk -F. '{print $(1)"."$(2)"."$(3)".1-254"}')
local _hosts_info
read -r -a _hosts_info <<<"$(nmap "$_ip_range" -p ${NETCAT_CHAT_APP_PORT} | grep -e '/tcp\|report' | sed 's/Nmap scan report for //g')"
local _sums_total
_sums_total=${#_hosts_info[@]}
for _index in $(seq 1 4 "$_sums_total"); do
_ip="${_hosts_info[$((_index - 1))]}"
_nickname=$(echo "@$_ip" | sed 's/\./_/g')
_port_state="${_hosts_info[$((_index + 1))]}"
if [[ "$_port_state" == "open" ]]; then
recipient_update "${_nickname}" "${_ip}" "$NETCAT_CHAT_APP_PORT"
elif [[ "$_port_state" == "closed" ]]; then
log_error 'Host <b>%s</b> status is <b>%s</b>' "$_ip" "$_port_state"
else
log_info 'Host <b>%s</b> status is <b>%s</b>' "$_ip" "$_port_state"
fi
done
}
function recipient_create() {
local _new_recipient_nickname="$1"
local _new_recipient_nickname_error
while true; do
prompt_input '_new_recipient_nickname' "New recipient's nickname" "$_new_recipient_nickname"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_nickname_error=$(validate_nickname "$_new_recipient_nickname" "$NICKNAME_CHECK_MODE_ERR_IF_EXISTS")
if [[ -z "$_new_recipient_nickname_error" ]]; then
break
else
echo "$_new_recipient_nickname_error"
_new_recipient_nickname=''
fi
done
local _new_recipient_name
_new_recipient_name=$(nickname_to_name "$_new_recipient_nickname")
local _new_recipient_hostname="$2"
local _new_recipient_hostname_error
while true; do
prompt_input '_new_recipient_hostname' "New recipient's hostname/IP address" "$_new_recipient_hostname"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_hostname_error=$(validate_hostname "$_new_recipient_hostname")
if [[ -z "$_new_recipient_hostname_error" ]]; then
break
else
echo "$_new_recipient_hostname_error"
_new_recipient_hostname=''
fi
done
local _new_recipient_port="$3"
local _new_recipient_port_error
while true; do
prompt_input '_new_recipient_port' "New recipient's port number" "$_new_recipient_port"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_port_error=$(validate_port "$_new_recipient_port")
if [[ -z "$_new_recipient_port_error" ]]; then
break
else
echo "$_new_recipient_port_error"
_new_recipient_port=''
fi
done
local _new_recipient_address
_new_recipient_address=("$_new_recipient_hostname" "$_new_recipient_port")
for _recipient_name in "${!chat_users[@]}"; do
local _recipient_nickname
_recipient_nickname=$(name_to_nickname "$_recipient_name")
if [[ "${chat_users[$_recipient_name]}" == "${_new_recipient_address[*]}" ]]; then
log_error 'Recipient data already defined and known as <b>%s</b>' "$_recipient_nickname"
return 1
fi
done
config_create "chat_users[${_new_recipient_name}]" "${_new_recipient_address[*]}"
log_success 'Recipient <b>%s</b> created as <b>%s</b>' "$_new_recipient_nickname" "${_new_recipient_address[*]}"
}
function recipient_rename() {
local _old_recipient_nickname="$1"
local _old_recipient_nickname_error
while true; do
prompt_input 'old_recipient_nickname' "Recipient's old nickname" "$old_recipient_nickname"
if ctrl_c_trap_exec; then
return
fi
_old_recipient_nickname_error=$(validate_nickname "$old_recipient_nickname" "$NICKNAME_CHECK_MODE_ERR_IF_NOT_EXISTS")
if [[ -z "$_old_recipient_nickname_error" ]]; then
break
else
echo "$_old_recipient_nickname_error"
old_recipient_nickname=''
fi
done
local _old_recipient_name
_old_recipient_name=$(nickname_to_name "$old_recipient_nickname")
local _new_recipient_nickname="$2"
local _new_recipient_nickname_error
while true; do
prompt_input '_new_recipient_nickname' "Recipient's new nickname" "$_new_recipient_nickname"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_nickname_error=$(validate_nickname "$_new_recipient_nickname" "$NICKNAME_CHECK_MODE_ERR_IF_EXISTS")
if [[ -z "$_new_recipient_nickname_error" ]]; then
break
else
echo "$_new_recipient_nickname_error"
_new_recipient_nickname=''
fi
done
local _recipient_address
_recipient_address=("${chat_users[${_old_recipient_name}]}")
local _new_recipient_name
_new_recipient_name=$(nickname_to_name "$_new_recipient_nickname")
config_delete "chat_users[${_old_recipient_name}]" && config_create "chat_users[${_new_recipient_name}]" "${_recipient_address[*]}"
log_success 'Recipient <b>%s</b> renamed to <b>%s</b>' "$old_recipient_nickname" "$_new_recipient_nickname"
}
function recipient_update() {
local _new_recipient_nickname="$1"
local _new_recipient_nickname_error
while true; do
prompt_input '_new_recipient_nickname' 'Recipient name' "$_new_recipient_nickname"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_nickname_error=$(validate_nickname "$_new_recipient_nickname" "$NICKNAME_CHECK_MODE_IGNORE")
if [[ -z "$_new_recipient_nickname_error" ]]; then
break
else
echo "$_new_recipient_nickname_error"
_new_recipient_nickname=''
fi
done
local _new_recipient_name
_new_recipient_name=$(nickname_to_name "$_new_recipient_nickname")
local _old_recipient_address
# shellcheck disable=SC2206
_old_recipient_address=(${chat_users[$_new_recipient_name]})
local _new_recipient_hostname="$2"
local _new_recipient_hostname_error
while true; do
prompt_input '_new_recipient_hostname' "Recipient's updated hostname/IP address" "$_new_recipient_hostname"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_hostname_error=$(validate_hostname "$_new_recipient_hostname")
if [[ -z "$_new_recipient_hostname_error" ]]; then
break
else
echo "$_new_recipient_hostname_error"
_new_recipient_hostname=''
fi
done
local _new_recipient_port="$3"
local _new_recipient_port_error
while true; do
prompt_input '_new_recipient_port' "Recipient's updated port number" "$_new_recipient_port"
if ctrl_c_trap_exec; then
return
fi
_new_recipient_port_error=$(validate_port "$_new_recipient_port")
if [[ -z "$_new_recipient_port_error" ]]; then
break
else
echo "$_new_recipient_port_error"
_new_recipient_port=''
fi
done
local _new_recipient_address
_new_recipient_address=("$_new_recipient_hostname" "$_new_recipient_port")
for _recipient_name in "${!chat_users[@]}"; do
if [[ "${chat_users[$_recipient_name]}" == "${_new_recipient_address[*]}" ]] && [[ "$_recipient_name" != "$_new_recipient_name" ]]; then
local _recipient_nickname
_recipient_nickname=$(name_to_nickname "$_recipient_name")
log_error 'Recipient data <b>%s</b> already defined and known as <b>%s</b>' "${_new_recipient_address[*]}" "$_recipient_nickname"
return 1
fi
done
config_create "chat_users[${_new_recipient_name}]" "${_new_recipient_address[*]}"
log_success 'Recipient <b>%s</b> updated as <b>%s</b>' "$_new_recipient_nickname" "${_new_recipient_address[*]}"
}
function recipient_delete() {
local _recipient_nickname="$1"
local _recipient_nickname_error
while true; do
prompt_input '_recipient_nickname' "Recipient's nickname to delete" "$_recipient_nickname"
if ctrl_c_trap_exec; then
return
fi
_recipient_nickname_error=$(validate_nickname "$_recipient_nickname" "$NICKNAME_CHECK_MODE_IGNORE")
if [[ -z "$_recipient_nickname_error" ]]; then
break
else
echo "$_recipient_nickname_error"
_recipient_nickname=''
fi
done
local _recipient_name
_recipient_name=$(nickname_to_name "$_recipient_nickname")
if [[ -z "${chat_users[$_recipient_name]}" ]]; then
log_error 'Recipient %s not found' "$_recipient_nickname"
else
config_delete "chat_users[${_recipient_name}]"
log_success 'Recipient %s deleted' "$_recipient_nickname"
fi
}
### MESSAGE ###
function message_to_user() {
local _sender_nickname=$1
local _recipient_nickname=$2
local _message_text=$3
local _datetime
_datetime=$(date +"%Y-%m-%d %H:%M:%S")
local _message
local _message_status
if [[ "$_recipient_nickname" == "$NETCAT_CHAT_BROADCAST_NICKNAME" ]]; then
# generate message
if [[ "$_sender_nickname" == "$NETCAT_CHAT_SYSTEM_NICKNAME" ]]; then
_message=$(printf "[%s] %s" "$_datetime" "$_message_text")
else
_message=$(printf "[%s] <b>%s</b> broadcast: %s" "$_datetime" "$_sender_nickname" "$_message_text")
fi
# display loopback
log_info "$_message"
# send message
for _recipient_name in "${!chat_users[@]}"; do
_message_status=$(message_unicast "$(name_to_nickname "$_recipient_name")" "$_message")
done
else
# generate message
if [[ "$_sender_nickname" == "$NETCAT_CHAT_SYSTEM_NICKNAME" ]]; then
_message="$(printf "[%s] %s" "$_datetime" "$_message_text")"
else
_message="$(printf "[%s] <b>%s</b> » <b>%s</b>: %s" "$_datetime" "$_sender_nickname" "$_recipient_nickname" "$_message_text")"
fi
# display loopback
printf "$%s\n" "$(message_unicast "$_recipient_nickname" "$_message")"
fi
}
function message_unicast() {
local _recipient_nickname="$1"
local _recipient_name
_recipient_name=$(nickname_to_name "$_recipient_nickname")
local _message="$2"
local _recipient_address
# shellcheck disable=SC2206
_recipient_address=(${chat_users[$_recipient_name]})
# shellcheck disable=SC2128
if [[ -z "$_recipient_address" ]]; then
log_error 'Recipient <b>%s</b> is not recognized, use <b>/create</b> to define new recipient or <b>/list</b> to list all recipients' "$_recipient_nickname"
return 99
else
local _recipient_hostname=${_recipient_address[0]}
# check status
local _recipient_online_status=0
_recipient_online_status=$(ping "$_recipient_hostname" -c 1 -t 1 | grep -e 'packets' | awk '{print $4;}')
if [[ "$_recipient_online_status" -gt "0" ]]; then
# send message
local _formatted_message
_formatted_message="$(log_message ---color "YELLOW" ---icon "$ICON_MESSAGE" ---bell "$_message")"
_formatted_message+="$(log_message ---color "YELLOW" ---icon "$ICON_PROMPT" ---one-line "Chat: ")"
# shellcheck disable=SC2086
echo -n "${_formatted_message}" | nc -c "${_recipient_address[*]}"
local _message_send_response=$?
# check response
if [[ "$_message_send_response" == "0" ]]; then
log_info "$_message"
return 0
else
log_error 'Recipient <b>%s</b> is not connected on %s' "$_recipient_nickname" "${_recipient_address[*]}"
return $_message_send_response
fi
else
log_error 'Recipient <b>%s</b> is offline' "$_recipient_nickname"
return 99
fi
fi
}
function message_from_system() {
local _message_text=$1
message_to_user "$NETCAT_CHAT_SYSTEM_NICKNAME" "$NETCAT_CHAT_BROADCAST_NICKNAME" "$_message_text"
}
function option_get() {
prompt_input 'option' 'Chat'
}
function option_reset() {
option='/i'
}
### INIT ###
log_title 'NetCat Chat'
check_dependencies nc ping nmap grep
# trap "echo '';netcat_kill;" EXIT
config_load "$@"
netcat_start
option_reset
while true; do
ctrl_c_trap_remove
case $option in
/p | /port | /p\ * | /port\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_sender_port=${option_array[1]:-}
unset option_array
app_setup_port "$option_sender_port"
netcat_kill
netcat_start
option_reset
;;
/n | /nick | /n\ * | /nick\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_sender_nickname=${option_array[1]:-}
unset option_array
app_setup_nickname "$option_sender_nickname"
option_reset
;;
/x | /clear)
history_clear
option_reset
;;
/q | /exit | /quit | /bye)
break
;;
/h | /help | \?)
show_commands
history_save
option_reset
;;
/l | /list | /l\ * | /list\ *)
history_save
# shellcheck disable=SC2206
option_array=($option)
option_flag=${option_array[1]:-}
unset option_array
recipients_list "$option_flag"
option_reset
;;
/s | /scan)
history_save
hosts_scan_and_recipient_create
option_reset
;;
/c | /create | /c\ * | /create\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_recipient_nickname=${option_array[1]:-}
option_recipient_hostname=${option_array[2]:-}
option_recipient_port=${option_array[3]:-}
unset option_array
recipient_create "$option_recipient_nickname" "$option_recipient_hostname" "$option_recipient_port"
option_reset
;;
/u | /update | /u\ * | /update\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_recipient_nickname=${option_array[1]:-}
option_recipient_hostname=${option_array[3]:-}
option_recipient_port=${option_array[4]:-}
unset option_array
recipient_update "$option_recipient_nickname" "$option_recipient_hostname" "$option_recipient_port"
option_reset
;;
/r | /rename | /r\ * | /rename\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_old_recipient_nickname=${option_array[1]:-}
option_new_recipient_nickname=${option_array[2]:-}
unset option_array
recipient_rename "$option_old_recipient_nickname" "$option_new_recipient_nickname"
option_reset
;;
/d | /delete | /d\ * | /delete\ *)
ctrl_c_trap_init
history_save
# shellcheck disable=SC2206
option_array=($option)
option_recipient_nickname=${option_array[1]:-}
unset option_array
recipient_delete "$option_recipient_nickname"
option_reset
;;
/w | /welcome)
history_save
message_to_user "$sender_nickname" "@all" "$(log_message 'Hi @all. My name is %s. You can add me to Your recipients list with <b>%s</b> command' "$sender_nickname" "/create $sender_nickname $sender_hostname $sender_port")"
option_reset
;;
\@*)
history_save
option_recipient_nickname=$(echo "$option" | awk '{print $1;}')
option_message_text=$(echo "$option" | cut -d' ' -f2-)
if [[ -n "$option_message_text" ]] && [[ "$option_message_text" != "@" ]] && [[ "$option_recipient_nickname" != "$option_message_text" ]]; then
message_to_user "$sender_nickname" "$option_recipient_nickname" "$option_message_text"
else
log_error 'Empty message'
fi
option_reset
;;
/i)
option_get
;;
*)
# shellcheck disable=SC2059
log_error 'Command not found. Use <code>/help</code> to see available commands.'
option_reset
;;
esac
done
}
# Call main function
main "$@"