summaryrefslogtreecommitdiffstats
path: root/gab
blob: f78bc4a684ce7be22f2432b2f16fa54101bcd712 (plain) (blame)
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
#!/usr/local/bin/bash

# Gab es un cliente alternativo al programa de chat en  colorfield.space
# Reemplace la variable $GAB_FILE con la ruta a su historial de chat
# Se asume que cada línea de chat comienza con: "user_name >"
# # # # # #->

HELP_TEXT=$(cat << EOF
GAB - Una simple interfaz de chat

syntax: gab [bandera] [valor]

bandera          valor
----------    ---------------
-h, --help     ninguno
-m, --msg      Mensaje entre comillas a enviar al chat\n
-l, --log      Un número entero con la cantidad de filas que quieres ver, por defecto 5
EOF
)


GAB_DEBUG=0

[[ $GAB_DEBUG -eq 0 ]] && GAB_LOG=/var/gab/chatlog.txt || GAB_LOG="$HOME/chatlog.txt"

TITLE="GAB v1.1 'Mirá mamá, sin comillas!!'"

LAST_MSG_DATE=$(date -r $(stat -f %m $GAB_LOG))

SEPARATOR='>'

# chequeamos si se puede escribir en el chatlog
if [[ ! -w "$GAB_LOG" ]] 
then
	echo "No es posible escribir en $GAB_LOG"
	exit 1
fi

# https://github.com/dylanaraps/pure-sh-bible#trim-leading-and-trailing-white-space-from-string
trim_string() {
    # Usage: trim_string "   example   string    "

    # Remove all leading white-space.
    # '${1%%[![:space:]]*}': Strip everything but leading white-space.
    # '${1#${XXX}}': Remove the white-space from the start of the string.
    trim=${1#${1%%[![:space:]]*}}

    # Remove all trailing white-space.
    # '${trim##*[![:space:]]}': Strip everything but trailing white-space.
    # '${trim%${XXX}}': Remove the white-space from the end of the string.
    trim=${trim%${trim##*[![:space:]]}}

    printf '%s\n' "$trim"
}

# Acá está la función que formatea las líneas y las imprime por pantalla
# todas las cuestiones relacionadas con modificación de apariencia de la salida
# de los mensajes debería ir por acá
# https://github.com/dylanaraps/pure-sh-bible#parsing-a-keyval-file
output_beautiful_messages(){
	while IFS="$SEPARATOR" read -r user message; do
		printf '\033[7m\033[1m %s\033[0m%s\n' "$user" "$message"
		echo '- - -'
	done
}


# Imprime las últimas n líneas del log de mensajes
# parámetro 1 es la cantidad de líneas a leer
# parámetro 2 es el path del archivo a leer
print_last_n_lines() {
	tail -n "$1" "$GAB_LOG" | output_beautiful_messages
}


# Imprime cabecera
print_header() {
	echo $TITLE
	echo "Último mensaje: $LAST_MSG_DATE"
	echo ""
}


# Agrega un mensaje al final del log de mensajes
# Primer parámetro: el mensaje
add_message() {
	IFS=' ' # necesario para $*
	msg=$(trim_string "$*")

	echo "$USER $SEPARATOR $msg" >> "$GAB_LOG"
	if [ $? -eq 0 ]
	then
		echo "Mensaje agregado exitosamente al chatlog"
		exit 0
	else
		echo "Error agregando mensaje al chatlog"
		exit 1
	fi
}


# Imprime los últimos mensajes
# Por defecto imprime 5
output_last_messages() {
	# configuramos por defecto 5 líneas
	lines=${1:-5}

	print_header
	print_last_n_lines "$lines"
}



# --- MAIN ---
if [ -z "$1" ]
then
	output_last_messages
else
	case "$1" in
		-m | --msg) 
			if [ -z "$2" ]
			then
				echo "Debe incluir un mensaje"
				exit 1
			else
				add_message ${@:2}
			fi
		;;
		-l | --log) 
			if [ -z "$2" ]
			then
				output_last_messages
			elif [[ "$2" =~ ^[0-9]+$ ]]
			then
				output_last_messages $2
			else
				echo "Error: el valor debe ser un número entero"
				exit 1
			fi
		;;
		-h | --help) 
			echo "$HELP_TEXT"
			exit 0
		;;
		*) 
			echo "Opción desconocida $1"
			echo "$HELP_TEXT"
			exit 1
		;;
	esac
fi

Un proyecto texto-plano.xyz