til - annna - Annna the nice friendly bot. | |
git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws6… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
til (1459B) | |
--- | |
1 #!/bin/sh | |
2 | |
3 nick="${1}" | |
4 learned="${2}" | |
5 tilmodbase="$HOME/bin/modules/til" | |
6 tildb="${tilmodbase}/til.txt" | |
7 | |
8 # Make sure there's a file to store the TILs. | |
9 if [ ! -f "${tildb}" ]; | |
10 then | |
11 mkdir -p "${tilmodbase}" | |
12 touch "${tildb}" | |
13 fi | |
14 | |
15 # Make sure file is writable. | |
16 if [ ! -w "${tildb}" ]; | |
17 then | |
18 exit 1 | |
19 fi | |
20 | |
21 # Clean up the learned string. | |
22 learned="$(printf "%s" "${learned}" \ | |
23 | tr -d [:cntrl:] \ | |
24 | sed 's,\n, ,g')" | |
25 | |
26 # Check to see if its already been learned. | |
27 already_learned_entry="$(grep -m 1 "${learned}" "${tildb}")" | |
28 if [ $? -eq 0 ]; # Match found (grep exit status of 0) | |
29 then | |
30 timestamp="$(printf "%s" "${already_learned_entry}" \ | |
31 | tr "\t" "\n" \ | |
32 | tail -n 1)" | |
33 user="$(printf "%s" "${already_learned_entry}" \ | |
34 | tr "\t" "\n" \ | |
35 | head -n 1)" | |
36 learned_text="$(printf "%s" "${already_learned_entry}" \ | |
37 | tr "\t" "\n" \ | |
38 | tail -n 2 \ | |
39 | head -n 1)" | |
40 paste="$(printf '%s\n%s learned %s\n' \ | |
41 '${timestamp}' \ | |
42 '${user}' \ | |
43 '${learned_text}' \ | |
44 | /br/bin/bitreich-paste)" | |
45 printf "looks like that's already been learned by %s on %s! %s" \ | |
46 "${user:-someone}" \ | |
47 "$(date -d "${timestamp}" +"%B %d, %Y")" \ | |
48 '${paste}' | |
49 exit | |
50 fi | |
51 | |
52 # Add the new TIL to the database. | |
53 timestamp=$(date -u) | |
54 printf "%s\n" "${nick} ${learned} $(date -u)" \ | |
55 >> "${tildb}" | |
56 | |
57 # Count users TILs | |
58 til_count="$(grep -cP "^${nick}\t" "${tildb}")" | |
59 | |
60 # Successful return message. | |
61 printf "well done! You've learned ${til_count} things so far! o.o" | |
62 exit 0 |