-
Notifications
You must be signed in to change notification settings - Fork 20
/
quassellog
executable file
·119 lines (102 loc) · 2.88 KB
/
quassellog
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
#!/bin/bash
######################################################
# shell script to access quassel logs from the CLI
#
# Features:
# - grep by simple LIKE pattern
# - grep by buffer
# - grep by user
# - or any combination of those.
#
# Author: Milian Wolff <[email protected]>
# License: GPL v3 or higher
#
# Dependencies:
# - sqlite3 on the host where quassel-storage.sqlite lies
# - ssh on remote hosts
######################################################
# Set this value to the host where quassel is running
# so you can use this script from other hosts as well.
# Usage: QUASSEL_HOST="myremotehost" quassellog ...
host="$QUASSEL_HOST"
# The absolute path to the quassel database
# Usage: QUASSEL_DATABASE="/foo/bvar/asdf.sqlite" quassellog ...
if [[ "$QUASSEL_DATABASE" == "" ]]; then
database="/var/cache/quassel/quassel-storage.sqlite"
else
database="$QUASSEL_DATABASE"
fi
######################################################
######################################################
query=""
pattern=""
user=""
buffer=""
if [[ "$host" != "" && "$HOSTNAME" != "$host" ]]; then
ssh "$host" "$0" \"$1\" \"$2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8\" \"$9\"
exit
fi
function usage {
echo "$(basename $0) [-u USER] [-b BUFFER] [PATTERN]"
echo
echo " -u USER show only messages from users, who have USER at"
echo " the start of their sender name."
echo
echo " -b BUFFER show only messages in this buffer"
echo " valid buffers are:"
sqlite3 "$database" "SELECT buffername FROM buffer WHERE buffername != '' ORDER BY buffername" | column
echo
echo " PATTERN a simple pattern, use * for wildcard matching"
}
if [[ "$database" == "" || ! -f "$database" ]]; then
echo "BAD DATABASE: $database"
echo
usage
exit 1
fi
while getopts "u:b:" flag
do
if [[ "$flag" == "u" ]]; then
user="$OPTARG"
elif [[ "$flag" == "b" ]]; then
buffer="$OPTARG"
fi
done
shift $(($OPTIND - 1))
pattern="$1"
if [[ "$pattern" == "" && "$user" == "" && "$buffer" == "" ]]; then
echo "pattern required"
echo
usage
exit 1
fi
# escape '
pattern=${pattern//\'/\'\'}
# escape %
pattern=${pattern//%/%%}
# wildcards
pattern=${pattern//\*/%}
query=" \
SELECT \
('[' || DATETIME(time, 'unixepoch', 'localtime') || '] ' || sender.sender || ' > ' || message) \
FROM backlog LEFT JOIN sender \
ON backlog.senderid = sender.senderid \
WHERE"
if [[ "$pattern" != "" ]]; then
query="$query message LIKE '%$pattern%'"
fi
if [[ "$user" != "" ]]; then
if [[ "$pattern" != "" ]]; then
query="$query AND"
fi
query="$query sender LIKE '$user%'"
fi
if [[ "$buffer" != "" ]]; then
if [[ "$pattern" != "" || "$user" != "" ]]; then
query="$query AND"
fi
query="$query bufferid IN (SELECT bufferid FROM buffer WHERE buffername = '$buffer')"
fi
# to slow:
# query="$query ORDER BY time DESC"
sqlite3 "$database" "$query" || echo $query