aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/connect
blob: 149d54affe03fa04174ec8e5d4a9c81d31d3117d (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env node


/**
 * The intent of this script is to test some
 * basic operations on a given server to see how
 * well we can interact with them out of the box.
 *
 * As our detection capabilities evolve this script
 * should be smaller and simpler.
 */

var CalDav = require('../lib/caldav');
var configurations = JSON.parse(require('fs').readFileSync(
  __dirname + '/test-configs.json', 'utf8'
));

var connectionType = process.argv[2];

if (!connectionType || !(connectionType in configurations)) {
  console.error(
    'You must choose an available configuration:',
    Object.keys(configurations)
  );
  process.exit(1);
}

var config = configurations[connectionType];

var con = new CalDav.Connection({
  domain: config.domain,
  user: config.user,
  password: config.password
});

var Propfind = CalDav.Request.Propfind;

function findProp(uri) {
  return new Propfind(con, {
    url: uri || config.uri
  });
}

// Have some url determine where the principal is
var findCal = findProp();
findCal.prop('current-user-principal');
findCal.prop('resourcetype');
findCal.prop('principal-URL');

function getProp(propName, obj, single) {
  var key, url, level, results = {};

  for (url in obj) {
    level = obj[url];
    for (key in level) {
      if (key === propName && level[key].status) {
        results[url] = level[key].value;
        if (single) {
          return results[url];
        }
      }
    }
  }

  return results;
}

findCal.send(function(err, data) {

  // found principal find calendar home
  var principaluri = getProp('current-user-principal', data, true);
  console.log('PRINCIPAL:', principaluri);
  var principalFind = findProp(principaluri);

  principalFind.prop('resourcetype');
  principalFind.prop(['caldav', 'calendar-home-set']);

  principalFind.send(function(err, data) {
    var caluri = getProp('calendar-home-set', data, true);
    var calFind = findProp(caluri);
    console.log('CALENDAR_HOME:', caluri);
    calFind.prop(['ical', 'calendar-color']);
    calFind.prop('owner');
    calFind.prop('displayname');
    calFind.prop('resourcetype');
    calFind.prop(['calserver', 'getctag']);
    calFind.depth = 1;


    // found calendar home find calendars.
    calFind.send(function(err, data) {
      var url, name;

      for (url in data) {
        if (data[url].resourcetype.value.indexOf('calendar') !== -1) {
          name = data[url].displayname.value.value;
          console.log('CAL RESOURCE:', name, '-', url);
        }
      }
      console.log('DATA:');
      console.log(JSON.stringify(data));
    });
  });

});