-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipdaemon
executable file
·120 lines (92 loc) · 3.41 KB
/
clipdaemon
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
#! /usr/bin/env bash
set -eo pipefail
if test -z $COFFEE_PESTO_DIR; then
DIR=$HOME/.coffeepesto
else
DIR=$COFFEE_PESTO_DIR
fi
if test -z $COFFEE_PESTO_MAX_ITEMS || ! [[ $COFFEE_PESTO_MAX_ITEMS =~ ^[0-9]+$ ]]; then
MAXITEMS=100
else
MAXITEMS=$COFFEE_PESTO_MAX_ITEMS
fi
if test -z $COFFEE_PESTO_IGNORED_TARGETS; then
ignored_targets="
TARGETS TIMESTAMP SAVE_TARGETS MULTIPLE STRING UTF8_STRING TEXT COMPOUND_TEXT text/plain;charset=utf-8
application/ico image/bmp image/ico image/icon image/jpeg image/tiff image/vnd.microsoft.icon image/x-bmp
image/x-ico image/x-icon image/x-MS-bmp image/x-win-bitmap text/ico x-moz-url application/x-qt-image BITMAP
image/cur image/jpg image/pbm image/pgm image/ppm image/xbm image/xpm PIXMAP
"
else
ignored_targets=$COFFEE_PESTO_IGNORED_TARGETS
fi
if test "$1" == "-V" || test "$1" == "--verbose"; then
DEBUG=true
else
DEBUG=false
fi
function nextidx {
local i=$(ls $DIR/h 2>/dev/null | tr -d - | sort -n | tail -1)
if test -z $i; then echo 1; else echo $((i + 1)); fi
}
function ringdelete {
local i=$(ls $DIR/h 2>/dev/null | tr -d - | sort -n | head -1)
if ! test -z $i; then rm -r $DIR/h/$i; fi;
}
function isblacklisted {
test -f $DIR/blacklist && (xdotool getactivewindow getwindowname | grep -Eiq "$(cat $DIR/blacklist)")
}
function contains {
echo $1 | grep -qE "(^| )$2( |$)"
}
function nicedate {
date +"%a %B %d %I:%M %p %Z %Y"
}
function log {
local logfile=$DIR/log
if $DEBUG; then echo $(nicedate): $owner: "$@" >> $logfile; fi
}
prev_listing_dir=
while owner=$(waitforcp); do
if test -f $DIR/pause || isblacklisted; then continue; fi
if test -f $DIR/ourpaste; then rm $DIR/ourpaste; log ourpaste; continue; fi
idx=$(nextidx)
log idx is $idx
if test $idx -gt $MAXITEMS; then
ringdelete
log ringdeleted...
fi
new_listing_dir=$DIR/h/$idx
log new_listing_dir is $new_listing_dir
found_a_valid_target=false
getcp TARGETS | while read -r unsafe_target; do
log unsafe_target is "$unsafe_target"
if contains "$ignored_targets" "$unsafe_target"; then
log ignored "$unsafe_target" as its "in" "$ignored_targets"
continue
fi
target=$(echo "$unsafe_target" | cut -d';' -f1 | tr -cd 'a-zA-Z/-')
if test -z $target; then
echo "Window with id $owner is being shady with their targets :)"
continue;
fi;
variant_dir=$new_listing_dir/$(echo $target | tr / '\\')
mkdir -p $variant_dir
log variant_dir is $variant_dir
getcp $target > $variant_dir/data
xdotool getactivewindow getwindowname > $variant_dir/index
xdotool getactivewindow getwindowclassname >> $variant_dir/index
nicedate >> $variant_dir/index
test -x $DIR/custom-index && $DIR/custom-index $variant_dir
if ! file -bi $variant_dir/data | grep -q 'charset=binary'; then
log assuming $variant_dir as text...
cat $variant_dir/data >> $variant_dir/index
fi
found_a_valid_target=true
done
if $found_a_valid_target && ! test -z $prev_listing_dir && diff --exclude=index -rq $new_listing_dir $prev_listing_dir 2>/dev/null | head -1 >/dev/null 2>&1; then
log $new_listing_dir is the same copy as previous one $perv_listing_dir... deduping...
rm -r $new_listing_dir
fi
prev_listing_dir=$new_listing_dir
done