mailto: function in card listing
[confetti] / pages / cards.sh
index 08b88b9..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,147 +58,213 @@ 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() {
-  tr -d '\n' <"$1" |sed -r 's:\r ::g;s:\r:\n:g' \
+  unset key
+  sed -r ':X;N;$!bX; s;\r\n[ \t];;g; s;\r\n;\n;g;' "$1" \
   | sed -rn '
-    s:^X-MS-CARDPICTURE:PHOTO:p;
-    s:^X-GENDER:GENDER:p;
-    s:^X-ANNIVERSARY:ANNIVERSARY:p;
-    s:^X-EVOLUTION-ANNIVERSARY:ANNIVERSARY:p;
-    s:^X-KADDRESSBOOK-X-Anniversary:ANNIVERSARY:p;
-    s:^X-AIM(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p;
-    s:^X-ICQ(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p;
-    s:^X-GOOGLE-TALK(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p;
-    s:^X-JABBER(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p;
-    s:^X-MSN(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:msn\:\2:p;
-    s:^X-YAHOO(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:ymsgr\:\2:p;
-    s:^X-SIP(;[^"\:]+|;"[^"]+")*\:(sip\:)?(.*)$:IMPP\1\:sip\:\3:p;
-    s:^LABEL(;[^"\:]+|;"[^"]+")*\:(.*)$:ADR;LABEL="\2"\1\::p;
-    s:^X-EVOLUTION-BLOG-URL:URL:p;
-
-    s:^AGENT:RELATED\;TYPE=agent:p;
-    s:^X-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p;
-    s:^X-EVOLUTION-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p;
-    s:^X-KADDRESSBOOK-X-AssistantsName:RELATED\;TYPE=assistant;VALUE=text:p;
-    s:^X-MANAGER:RELATED\;TYPE=manager;VALUE=text:p;
-    s:^X-EVOLUTION-MANAGER:RELATED\;TYPE=manager;VALUE=text:p;
-    s:^X-KADDRESSBOOK-X-ManagersName:RELATED\;TYPE=manager;VALUE=text:p;
-    s:^X-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p;
-    s:^X-EVOLUTION-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p;
-    s:^X-KADDRESSBOOK-X-SpouseName:RELATED\;TYPE=spouse;VALUE=text:p;
-
-    s:^([A-Z].*)$:\1:p;
+    # === turn property names to upper case, strip group names ===
+    h; s;^([^;:]+);;;
+    x; s;^([^;:\.]+\.)?([^;:]+).*$;\2;;
+    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;;
+                        s;^X-GENDER(\;|:);GENDER\1;;
+                   s;^X-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
+         s;^X-EVOLUTION-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
+    s;^X-KADDRESSBOOK-X-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
+            s;^X-EVOLUTION-BLOG-URL(\;|:);URL\1;;
+                              s;^AGENT(\;|:);RELATED\;VALUE=text\;TYPE=agent\1;;
+                        s;^X-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
+              s;^X-EVOLUTION-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
+    s;^X-KADDRESSBOOK-X-ASSISTANTSNAME(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
+                          s;^X-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
+                s;^X-EVOLUTION-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
+      s;^X-KADDRESSBOOK-X-MANAGERSNAME(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
+                           s;^X-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
+                 s;^X-EVOLUTION-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
+        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:;;
+
+    # === Update obsolete LABEL property ===
+    s;^LABEL((\;[A-z0-9-]+|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;ADR\1\;LABEL="\5":;;
+
+    /^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{
+      h;
+      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;'\'';'\'\\\\\'\'';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;;
+      # :X s;((^|[^\\])(\\\\)*)[;,];\1\n;g; s;((^|[^\\])(\\\\)*)\\([;,]);\4;g; tX;
+      s;'\'';'\'\\\\\'\'';g
+      s;^.*$;value='\''&'\'';
+      H; g;
+
+      s;^[^\n]*[\n ]+;;
+      s;\n+$;;;
+
+      p;
+    }
     ' \
-  | sed -r 's:^([^;\:]+)(;[^"\:]+|;"[^"]+")*\:(.*)$:key="\1"\nvalue="\3"\ntag=\2:g' \
   | while read -r line; do
+    declare -A tag
     case "$line" in
-      key=*) printf %s\\n "$line"
-       ;;
-      value=*) printf %s\\n "${line}"
-       ;;
-      tag=*) ot=''
-            printf %s "$line" \
-            | sed -r 's:^tag=::;s:\;([A-Z+_-]+="[^"]+"|[A-Z+_-]+=[^\;]+):\n\1:g;' \
-            | sed -rn 's:([A-Z+_-]+)="?(.+)"?:tag\[\1\]="\2":gp' \
-            | sed -r '/^ *$/d' \
-            | sort |while read -r tag; do
-              nt="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\1:')"
-              nv="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\2:')"
-              [ "$nt" = "$ot" ] && vl="$nv,$vl" || vl="$nv"
-              printf %s\\n "tag[$nt]=\"$vl\""
-              ot="$nt"
-            done
-       ;;
+      value*) eval "$line";;
+      tag*)   eval "$line";;
+      key*)
+        if [ -z "$key" ]; then
+          eval "$line"
+        else
+          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"
+          unset n
+          while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
+          unset value
+          unset tag
+        fi
+      ;;
     esac
-  done \
-  | sed -r 's:[\\$`]:\\&:g'
+  done
 }
 
 view_card() {  #Parameter: Cardfile
   id="$1"
   cardfile="$_DATA/vcard/${id}"
   cachefile="$_DATA/cache/${id}.cache"
-  unset key
   if [ "$cachefile" -nt "$cardfile" ]; then
     cat "$cachefile"
   else
-    declare -A tags
     declare -A values
-    vcf_parse "$cardfile" |while read -r line; do
-      declare -A tag
-      case "$line" in
-        value*) eval "$line";;
-        tag*)   eval "$line";;
-        key*)
-          if [ -z "$key" ]; then
-            eval "$line"
-          else
-            values[$key]="${value//\\r\\n/$BR}"
-            for t in ${(k)tag}; do
-              tags[${key}_$t]="$tag[$t]"
-            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 value
-            unset tag
-          fi
-        ;;
-      esac
-    done
-    . $view_card |tee "$cachefile"
+    vcf_parse "$cardfile"
+    . "$_EXEC/templates/view_card.sh" |tee "$cachefile"
   fi
 }
 
@@ -201,34 +273,9 @@ edit_card() {  #Parameter: Cardfile
   cardfile="$_DATA/vcard/$id"
   tempfile="$_DATA/temp/$id"
   [ -f "$tempfile" ] && cardfile="$tempfile"
-  unset key
 
-  declare -A tags
   declare -A values
-  vcf_parse "$cardfile" |while read -r line; do
-    declare -A tag
-    case "$line" in
-      value*) eval "$line";;
-      tag*)   eval "$line";;
-      key*)
-        if [ -z "$key" ]; then
-          eval "$line"
-        else
-          [ -n "$value" ] && values[$key]="${value//\\r\\n/$BR}" || values[$key]='\r'
-          for t in ${(k)tag}; do
-            tags[${key}_$t]="$tag[$t]"
-          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 value
-            unset tag
-        fi
-      ;;
-    esac
-  done
-  . $edit_card
+  vcf_parse "$cardfile"
+  force_items $FORCE_ITEMS
+  . "$_EXEC/templates/edit_card.sh"
 }