replytime

plot email reply time
git clone https://a3nm.net/git/replytime/
Log | Files | Refs

replytime.sh (2839B)


      1 #!/bin/bash
      2 # plot email response time
      3 # a3nm, 2014-08-01
      4 
      5 MYEMAILS=$(grep -E '^other_email|^primary_email' ~/.notmuch-config | cut -d '=' -f2 | paste -sd ";")
      6 MYEMAILSOR=$(echo "$MYEMAILS" | sed 's/^/from:/g' | sed 's/;/ or from:/g')
      7 MYEMAILSPIPE=$(echo "$MYEMAILS" | sed 's/;/|/g')
      8 
      9 echo "notmuch request $MYEMAILSOR"
     10 NUM=$(notmuch count "$MYEMAILSOR")
     11 echo "will process $NUM messages"
     12 # start with older messages as they are more fragile :)
     13 notmuch search --sort=oldest-first --output=files "$MYEMAILSOR" | pv -l -s$NUM |
     14 while read l
     15 do
     16   IRT=$(formail -XIn-Reply-To: -c < "$l" | cut -d':' -f2-)
     17   ID=$(formail -XMessage-Id: -c < "$l" | cut -d':' -f2- |
     18     tr -d '<>' | sed 's/^\s*//;s/\s*$//')
     19   DATE=$(formail -XDate: -c < "$l" | cut -d':' -f2-)
     20   if [ -z "$DATE" ]
     21   then
     22     # i have old sent mails from a crappy import with no "Date" field...
     23     continue
     24   fi
     25   TIME=$(date -d"$DATE" +%s)
     26   if [ -z "$TIME" ]
     27   then
     28     TO=$(formail -XTo: -c < "$l")
     29     NG=$(formail -XNewsgroups: -c < "$l")
     30     if [ -n "$NG" -a -z "$TO" ]
     31     then
     32       # this is a newsgroup message, do not warn
     33       continue
     34     fi
     35     # date was here but could not extract, means I have failed
     36     echo -n "Could not extract date" >> "$2"
     37     echo -n " from own message <$ID> file <$l>:" >> "$2"
     38     echo " extracted $DATE $TIME" >> "$2"
     39     continue
     40   fi
     41   # there might be multiple in-reply-to's
     42   echo "$IRT" |cut -d ':' -f2- | sed 's/\s/\n/g' | grep -v '^ *$' | tr -d '<>' |
     43   while read m;
     44   do
     45     SUFF=""
     46     FILE=$(notmuch search --output=files --duplicate=1 "id:$m")
     47     if [ -z "$FILE" ]
     48     then
     49       # replying to an email I don't have? weird
     50       echo -n "Could not find original message" >> "$2"
     51       echo -n " for own message <$ID> file <$l>:" >> "$2"
     52       echo " looked for $m" >> "$2"
     53       continue
     54     fi
     55     # exclude replies to own mail
     56     if formail -XFrom: -c <"$FILE" | grep -viE "$MYEMAILSPIPE" > /dev/null
     57     then
     58       DATE2=$(formail -XDelivery-Date: -c <"$FILE" | cut -d':' -f2-)
     59       TIME2=$(date -d"$DATE2" +%s 2>/dev/null)
     60       # need a lazy OR
     61       # time must be defined and not be later than my reply...
     62       if [ -z "$TIME2" ] || [ "$TIME2" -gt "$TIME" ]
     63       then
     64         # keep info that this was not delivery date
     65         SUFF="(*)"
     66         DATE2=$(formail -XDate: -c <"$FILE" | cut -d':' -f2-)
     67         TIME2=$(date -d"$DATE2" +%s 2>/dev/null)
     68       fi
     69       if [ -z "$TIME2" ] || [ "$TIME2" -gt "$TIME" ]
     70       then
     71         # still no extracted time
     72         echo -n "Could not extract sensible date" >> "$2"
     73         echo -n " from foreign message <$m> file <$FILE>:" >> "$2"
     74         echo -n " extracted $DATE2 converted $TIME2" >> "$2"
     75         echo " but my converted time $TIME" >> "$2"
     76         continue
     77       fi
     78       # save the line
     79       echo "$m $TIME2 $ID $TIME $SUFF" >> "$1"
     80     fi
     81   done
     82 done
     83