mailto: function in card listing
[confetti] / pages / cards.sh
index 3f0be4a..538146d 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-# Copyright 2014 - 2016 Paul Hänsch
+# Copyright 2014 - 2017 Paul Hänsch
 #
 # This file is part of Confetti.
 # 
 BR='
 '
 
+force_items(){
+  for each in "$@"; do
+    [ -z "${values[$each]+x}" ] && values[${each}]=''
+  done
+}
+
 case $PROFILE in
 medical)
-  view_card="$_EXEC/templates/view_client.sh"
-  edit_card="$_EXEC/templates/edit_client.sh"
+  SUP_FIELDS=(N NICKNAME GENDER BDAY ADR TEL EMAIL X-HEALTH-INSURANCE X-HEALTH-INSURANCE-NOCONTRIB IMPP URL NOTE X-CLIENT-REFERRAL)
+  FORCE_ITEMS=(ADR TEL EMAIL NOTE X-CLIENT-REFERRAL)
   _GET[order]="${_GET[order]:-lastname}"
   _GET[filtertype]="${_GET[filtertype]:-name}"
   profile_medical=x
 ;;
 circus)
-  view_card="$_EXEC/templates/view_attendee.sh"
-  edit_card="$_EXEC/templates/edit_attendee.sh"
+  SUP_FIELDS=(N NICKNAME GENDER BDAY X-ZACK-JOINDATE X-ZACK-LEAVEDATE EMAIL TEL IMPP ADR URL NOTE)
+  FORCE_ITEMS=(BDAY X-ZACK-JOINDATE TEL EMAIL ADR NOTE)
   _GET[order]="${_GET[order]:-firstname}"
   _GET[filtertype]="${_GET[filtertype]:-any}"
   profile_circus=x
@@ -52,53 +58,65 @@ list_hi_companies(){
   | sort -u
 }
 
+list_categories() {
+  catfile="${_DATA}/mappings/categories"
+  sort -u "$catfile" \
+  | sed -r '/^[\t ]*$/d'
+}
+
+listcards_order(){
+  while read file; do
+    case "${_GET[order]}" in
+      firstname)
+        printf '%s\t%s\n' "$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")" "$file"
+        ;;
+      lastname)
+        printf '%s\t%s\n' "$(sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$file")" "$file"
+        ;;
+      bdate)
+        printf '%s\t%s\n' "$(sed -rn 's:^BDAY(;.+)*\:(.*)$:\2:p' "$file")" "$file"
+        ;;
+      *)  printf 'x\t%s\n' "$file"
+        ;;
+    esac
+  done \
+  | sort -u |sed -r 's;^.*\t;;'
+}
+
+listcards_filter(){
+  filterex='s;^([^\n]+)\n.*$;\1;p'
+  printf '%s\n' "${_GET[filter]}" |tr '^' '\n' \
+  | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
+  | while read each; do
+    case $each in
+        name:*) expr='(FN|NICKNAME|N)(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
+      street:*) expr='ADR(\;[^\n]+)*:([^\;]*;){2}[^\;\n]*'"(${each#*:})";;
+         zip:*) expr='ADR(\;[^\n]+)*:([^\;]*;){5}[^\;\n]*'"(${each#*:})";;
+      any:*|:*) expr="[^\n]*"'(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
+           *:*) expr="${each%%:*}"'(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
+             *) expr="(${each})";;
+    esac
+    filterex='/(^|\n)'"${expr}"'/I{'"${filterex}"'}'
+  done
+
+  while read n; do
+    { printf '%s\n' "$n"; cat "$n"; } \
+    | sed -En ':X;N;$!bX; {'"$filterex"'}'
+  done
+
+}
+
 listcards() {
-  case "${_GET[filtertype]}" in
-    any)
-       grep -il "${_GET[filter]}" ${_DATA}/vcard/*vcf
-      ;;
-    name)
-       egrep -xil "(FN|NICKNAME|N)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
-      ;;
-    street)
-       egrep -xil "(ADR)(;.+)*:([^;]*;){2}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
-      ;;
-    zip)
-       egrep -xil "(ADR)(;.+)*:([^;]*;){5}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
-      ;;
-    telephone)
-       egrep -xil "(TEL)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
-      ;;
-    birth)
-       egrep -xil "(BDAY)(;.+)*:${_GET[filter]}.*" ${_DATA}/vcard/*vcf
-      ;;
-    course)
-      ;;
-    *) ls -1 ${_DATA}/vcard/*vcf 2>/dev/null
-      ;;
-  esac |case "${_GET[order]}" in
-    firstname)
-      while read file; do
-        fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
-       echo "$fn\t$file"
-      done
-      ;;
-    lastname)
-      while read file; do
-        ln=$(sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$file")
-       echo "$ln\t$file"
-      done
-      ;;
-    bdate)
-      while read file; do
-        bd=$(sed -rn 's:^BDAY(;.+)*\:(.*)$:\2:p' "$file")
-       echo "$bd\t$file"
-      done
-      ;;
-    *)
-      sed -r 's:^.*$:x\t&:'
-      ;;
-  esac |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
+  printf %s\\n "${_DATA}/vcard/"*.vcf \
+  | listcards_filter \
+  | listcards_order \
+  | sed -E 's;^(.*/)*;;;'
+}
+
+listcards_mail() {
+  printf %s\\n "${_DATA}/vcard/"*.vcf \
+  | listcards_filter \
+  | xargs -r -d\\n sed -En 's:^EMAIL(;.+)*\:(.+)\r$:\2,:p'
 }
 
 vcf_parse() {
@@ -111,7 +129,8 @@ vcf_parse() {
     y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
     G; s;\n;;;
 
-    
+    # === strip trailing CR (but keep CRs in property value) ===
+    s;\r$;;;
 
     # === Normalise various known vendor properties ===
                 s;^X-MS-CARDPICTURE(\;|:);PHOTO\1;;
@@ -132,29 +151,29 @@ vcf_parse() {
         s;^X-KADDRESSBOOK-X-SPOUSENAME(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
 
     # === Normalise obsolete vendor IM properties ===
-            s;^X-AIM((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
-            s;^X-ICQ((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
-    s;^X-GOOGLE-TALK((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
-         s;^X-JABBER((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
-            s;^X-MSN((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:msn:;;
-          s;^X-YAHOO((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:ymsgr:;;
-            s;^X-SIP((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(sip:)?;IMPP\1:sip:;;
+            s;^X-AIM((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
+            s;^X-ICQ((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
+    s;^X-GOOGLE-TALK((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
+         s;^X-JABBER((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
+            s;^X-MSN((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:msn:;;
+          s;^X-YAHOO((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:ymsgr:;;
+            s;^X-SIP((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(sip:)?;IMPP\1:sip:;;
 
     # === Update obsolete LABEL property ===
-    s;^LABEL((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;ADR\1\;LABEL="\5":;;
+    s;^LABEL((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;ADR\1\;LABEL="\5":;;
 
-    /^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{
+    /^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{
       h;
-      s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';;
+      s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';;
       H; g;
 
-      s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;;
+      s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;;
       s;'\'';'\'\\\\\'\'';g
       s;\;([A-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g
       s;^\n+;;; s;\n+$;;;
       /^.+$/H; g;
 
-      s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;;
+      s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;;
       # :X s;((^|[^\\])(\\\\)*)[;,];\1\n;g; s;((^|[^\\])(\\\\)*)\\([;,]);\4;g; tX;
       s;'\'';'\'\\\\\'\'';g
       s;^.*$;value='\''&'\'';
@@ -175,16 +194,59 @@ vcf_parse() {
         if [ -z "$key" ]; then
           eval "$line"
         else
-          values[${key}]="${value//\\r\\n/$BR}"
-          for t in ${(k)tag}; do
-            values[${key}_${t}]="${tag[$t]}"
+          printf '%s\n' "$value" |sed -rn '
+            :X
+            s;((^|[^\\])(\\\\)*),;\1\n;g; 
+            tX;
+            s;\\,;,;g;
+            p
+          ' \
+          | while read -r val; do
+            while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
+            if printf '%s\n' "$val" |grep -qE '((^|[^\\])(\\\\)*)\;'; then
+              m=0
+              values[${key}${n}]="${val}"
+              printf '%s\n' "$val" |sed -rn '
+                :X
+                s;((^|[^\\])(\\\\)*)\;;\1\n;g; 
+                tX;
+                s;\\\;;\;;g;
+                p
+              ' \
+              | while read -r v; do
+                values[${key}${n}+${m}]="$(
+                  printf %s\\n "${v}" \
+                  | sed -rn '
+                    :X
+                    s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
+                    tX;
+                    s;\\\\;\\;g;
+                    p
+                  '
+                )"
+                m=$(($m + 1))
+              done
+            else
+              values[${key}${n}]="$(
+                printf %s\\n "${val}" \
+                | sed -rn '
+                  :X
+                  s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
+                  tX;
+                  s;\\\;;\;;g;
+                  s;\\\\;\\;g;
+                  p
+                '
+              )"
+            fi
+            for t in ${(k)tag}; do
+              values[${key}${n}_${t}]="${tag[$t]}"
+            done
           done
+
           eval "$line"
-          if [ -n "$values[$key]" ]; then
-            n=0
-            while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
-            key=$key$n
-          fi
+          unset n
+          while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
           unset value
           unset tag
         fi
@@ -202,7 +264,7 @@ view_card() {  #Parameter: Cardfile
   else
     declare -A values
     vcf_parse "$cardfile"
-    . $view_card |tee "$cachefile"
+    . "$_EXEC/templates/view_card.sh" |tee "$cachefile"
   fi
 }
 
@@ -214,5 +276,6 @@ edit_card() {  #Parameter: Cardfile
 
   declare -A values
   vcf_parse "$cardfile"
-  . $edit_card
+  force_items $FORCE_ITEMS
+  . "$_EXEC/templates/edit_card.sh"
 }