aboutsummaryrefslogtreecommitdiff
path: root/.i3-blocks/blocks/mail
blob: 66b1d0a29ed7473a451b7a102f31d47f71a7b4db (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
#!/bin/bash

# Requires https://www.google.com/settings/security/lesssecureapps for gmail.

CONFIG_FILE="${BLOCK_INSTANCE}"
CONFIG_FILE=${CONFIG_FILE/\~/$HOME}

# Config file needs the following settings:
#MAIL_SERVER="imap.domain.tld:993"
#MAIL_USER="user@domain.tld"
#MAIL_PASSWORD="secret"
#MAIL_FOLDER="INBOX"

if [[ ! -f "${CONFIG_FILE}" ]]; then
  echo "${CONFIG_FILE}"
  exit 33
fi

source "${CONFIG_FILE}"

MAIN_DOMAIN=$(expr match "${MAIL_SERVER}" '.*\.\(.*\..*\)' | awk -F ':' '{print $1}')

# Add https so xdg knows that it's a website
if [[ "${MAIN_DOMAIN}" != http* ]]; then
  MAIN_DOMAIN="https://${MAIN_DOMAIN}"
fi

# Left click
if [[ "${BLOCK_BUTTON}" -eq 1 ]]; then
  xdg-open "${MAIN_DOMAIN}"
fi

MAIL_FILE="/tmp/.mail"
URGENT_VALUE="100"

GET_UNREAD=$(cat<<EOF
? LOGIN "${MAIL_USER}" "${MAIL_PASSWORD}"
? STATUS "${MAIL_FOLDER}" (unseen)
? LOGOUT
EOF
)

echo "${GET_UNREAD}" | openssl s_client -connect "${MAIL_SERVER}" -crlf -ign_eof > "${MAIL_FILE}" 2>/dev/null 
UNREAD_COUNT=$(cat "${MAIL_FILE}" | grep -i "UNSEEN" | grep -oE "[0-9]*" | head -n 1)

# For security reasons
rm "${MAIL_FILE}"

if [[ "${UNREAD_COUNT}" = "" ]]; then
  exit
fi

echo "${UNREAD_COUNT}"
echo "${UNREAD_COUNT}"
echo ""

if [[ "${UNREAD_COUNT}" -ge URGENT_VALUE ]]; then
  exit 33
fi