aboutsummaryrefslogtreecommitdiff
path: root/bin/msg
blob: d8ae7fd1fd64ff9e6b8cdfa0c38db5fe9fbfa9f9 (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
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  msg [-X|-s|-S|-m|-D|-b] [MESSAGE]
		  msg -h
	EOF
}

help() {
	cat <<-'EOF'

		Options:
		  -X            send MESSAGE using the `xmpp` command
		  -s            play $XDG_DATA_HOME/msg/medium.ogg sound
		  -S            say MESSAGE using `speak`
		  -m            send email with MESSAGE as subject and empty body
		  -D            send desktop MESSAGE via `notify-send`
		  -b            print terminal bell
		  -h, --help    show this message

		  MESSAGE       the text to be sent by the relevant channel

		Examples:

		  Ring a terminal bell and play a sound:

		    $ msg -sb

		  Send an email and an XMPP message:

		    $ msg -mX 'The message goes here'
	EOF
}

for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

sound() {
	play "$XDG_DATA_HOME"/msg/medium.ogg 2>/dev/null
}

XMPP=false
SOUND=false
SPEAK=false
MAIL=false
DESKTOP=false
BELL=false
ACTION_DONE=false
SHOW_USAGE=false
while getopts 'XsSmDbh' flag; do
	case "$flag" in
		X)
			XMPP=true
			ACTION_DONE=true
			;;
		s)
			SOUND=true
			ACTION_DONE=true
			;;
		S)
			SPEAK=true
			ACTION_DONE=true
			;;
		m)
			MAIL=true
			ACTION_DONE=true
			;;
		D)
			DESKTOP=true
			ACTION_DONE=true
			;;
		b)
			BELL=true
			ACTION_DONE=true
			;;
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

if [ "$ACTION_DONE" = false ]; then
	sound
	usage
	help
	exit
fi


MESSAGE="${1:-}"

assert_arg() {
	if [ -z "$1" ]; then
		printf 'Missing %s.\n\n' "$2" >&2
		usage >&2
		exit 2
	fi
}

if [ "$XMPP" = true ]; then
	assert_arg "$MESSAGE" '-X MESSAGE'
	xmpp -m "$MESSAGE" eu@euandreh.xyz &
fi
if [ "$SOUND" = true ]; then
	sound &
fi
if [ "$SPEAK" = true ]; then
	assert_arg "$MESSAGE" '-S MESSAGE'
	echo "$MESSAGE" | speak -v pt-BR &
fi
if [ "$MAIL" = true ]; then
	assert_arg "$MESSAGE" '-m MESSAGE'
	echo " " | email -s "$MESSAGE" eu@euandre.org &
fi
if [ "$DESKTOP" = true ]; then
	assert_arg "$MESSAGE" '-D MESSAGE'
	notify-send "$MESSAGE" &
fi
if [ "$BELL" = true ]; then
	printf '\a' &
fi

wait