summaryrefslogtreecommitdiff
path: root/dotfiles/hyprland/.local/bin/pinentry-fuzzel
blob: 4cbe6b7bc59032e55e58569567e3d1b492eaa7ba (plain)
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
#!/bin/sh

ENABLE_LOGGING="TRUE"
logger() {
    if [ "$ENABLE_LOGGING" == "TRUE" ]; then
        /usr/bin/logger -t "${0} [$]" "$@";
    fi
}

# Decode URL-encoded characters (%0A = newline, %20 = space, etc.)
decode() {
    printf '%b' "$(echo "$1" | sed 's/%\([0-9A-Fa-f][0-9A-Fa-f]\)/\\x\1/g')"
}

# Base command and misc variables
DESC=""
PROMPT=""
COUNTFILE="/tmp/pinentry-fuzzel-count-$$"

# Check if this is a repeat prompt (within 5 seconds of last)
LASTFILE="/tmp/pinentry-fuzzel-last"
NOW=$(date +%s)
if [ -f "$LASTFILE" ]; then
    LAST=$(cat "$LASTFILE")
    if [ $((NOW - LAST)) -lt 5 ]; then
        REPEAT=1
    else
        REPEAT=0
    fi
else
    REPEAT=0
fi
echo "$NOW" > "$LASTFILE"

echo "OK Please go ahead"
while read cmd rest; do
    logger "RAW=< ${cmd} ${rest} >"
    logger "cmd=<${cmd}> rest=<${rest}>"

    case $cmd in
        GETINFO)
            case "$rest" in
                flavor)
                    echo "D fuzzel"
                    echo "OK"
                    ;;
                version)
                    echo "D 0.1"
                    echo "OK"
                    ;;
                ttyinfo)
                    echo "D - - -"
                    echo "OK"
                    ;;
                pid)
                    echo "D $$"
                    echo "OK"
                    ;;
            esac
            ;;

        SETDESC)
            DESC=$(decode "$rest")
            echo "OK"
            ;;

        SETERROR)
            logger "ERROR $rest"
            # Silently acknowledge errors (e.g., empty passphrase on escape)
            echo "OK"
            ;;

        SETPROMPT)
            PROMPT=$(decode "$rest")
            # Remove trailing colon if present (we add our own)
            PROMPT="${PROMPT%:}"
            echo "OK"
            ;;

        GETPIN | getpin)
            if [ "$REPEAT" -eq 0 ]; then
                LABEL="password:  "
            else
                LABEL="reenter:  "
            fi
            PASS=$(fuzzel --prompt "$LABEL" --width 25 --lines 0 --cache /dev/null --password --dmenu)
            if [ -z "$PASS" ]; then
                # User cancelled - return error to GPG
                rm -f "$LASTFILE"
                echo "ERR 83886179 Operation cancelled"
            else
                echo "D $PASS"
                echo "OK"
            fi
            ;;

        BYE|bye)
            echo "OK closing connection"
            logger "EXITING"
            exit 0
            ;;

        *)
            echo "OK"
            ;;
        esac
done