davup.sh (2920B) - raw


      1 #!/bin/sh
      2 # DAVup
      3 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as
      7 # published by the Free Software Foundation, either version 3 of the
      8 # License, or (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 # Back up calendars and contacts from a DAV server (CalDAV and CardDAV).
     19 
     20 domain="<DAV server address>"       # example: https://dav.mailbox.org (no trailing "/")
     21 user="<username>"
     22 pass="<password>"
     23 
     24 get_cal() {
     25     curl -s -X "PROPFIND" -u "${user}:${pass}" -H "Content-Type: text/xml" -H "Depth: 1" \
     26         --data "<propfind xmlns='DAV:'><prop><calendar-data xmlns='urn:ietf:params:xml:ns:caldav'/></prop></propfind>" \
     27         "${domain}/${resource}"
     28 }
     29 
     30 get_card() {
     31     curl -s -X "PROPFIND" -u "${user}:${pass}" -H "Content-Type: text/xml" -H "Depth: 1" \
     32         --data "<propfind xmlns='DAV:'><prop><address-data xmlns=\"urn:ietf:params:xml:ns:carddav\"/></prop></propfind>" \
     33         "${domain}/${resource}"
     34 }
     35 
     36 process_cal() {
     37     intro="BEGIN:VCALENDAR"
     38     preevent="BEGIN:VEVENT"
     39     postevent="END:VEVENT"
     40     pretodo="BEGIN:VTODO"
     41     posttodo="END:VTODO"
     42     step="0"
     43     while read -r line; do
     44         line="${line%
}"
     45         case $step in
     46             0) echo "${line}" | grep -q "${intro}" && step="1" && echo "${intro}" ;;
     47             1) echo "${line}"; \
     48                 if [ "${line}" = "${preevent}" ]; then step="2"; \
     49                 elif [ "${line}" = "${pretodo}" ]; then step="3"; fi ;;
     50             2) echo "${line}" && [ "${line}" = "${postevent}" ] && step="4" ;;
     51             3) echo "${line}" && [ "${line}" = "${posttodo}" ] && step="4" ;;
     52             4) if [ "${line}" = "${preevent}" ]; then step="2"; echo "${line}"; \
     53                 elif [ "${line}" = "${pretodo}" ]; then step="3"; echo "${line}"; fi ;;
     54         esac
     55     done
     56     echo "END:VCALENDAR"
     57 }
     58 
     59 process_card() {
     60     pre="BEGIN:VCARD"
     61     post="END:VCARD"
     62     step="1"
     63     while read -r line; do
     64         line="${line%
}"
     65         case $step in
     66             1) echo "${line}" | grep -q "${pre}" && step="2" && echo "${pre}" ;;
     67             2) echo "${line}" | grep -q "${post}" && step="1" && echo "${post}" \
     68                 || echo "${line}" ;;
     69         esac
     70     done
     71 }
     72 
     73 # examples (resource address will be "${domain}/${resource}"):
     74 #     resource="caldav/mycal" && get_cal | process_cal > calendar_and_todos.ics
     75 #     resource="carddav/mycard" && get_card | process_card > contacts.vcf