This file is indexed.

/usr/lib/thunderbird-addons/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/chrome/calendar/content/calendar/calendar-invitations-manager.js is in xul-ext-lightning 1:24.4.0+build1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

Components.utils.import("resource://calendar/modules/calUtils.jsm");
Components.utils.import("resource://calendar/modules/calItipUtils.jsm");

/**
 * This object contains functions to take care of manipulating requests.
 */
var gInvitationsRequestManager = {
    mRequestStatusList: {},

    /**
     * Add a request to the request manager.
     *
     * @param calendar    The calendar to add for.
     * @param op          The operation to add
     */
    addRequestStatus: function IRM_addRequestStatus(calendar, op) {
        if (op) {
            this.mRequestStatusList[calendar.id] = op;
        }
    },

    /**
     * Cancel all pending requests
     */
    cancelPendingRequests: function IRM_cancelPendingRequests() {
        for each (var request in this.mRequestStatusList) {
            if (request && request.isPending) {
                request.cancel(null);
            }
        }
        this.mRequestStatusList = {};
    }
};

var gInvitationsManager = null;

/**
 * Return a cached instance of the invitations manager
 *
 * @return      The invitations manager instance.
 */
function getInvitationsManager() {
    if (!gInvitationsManager) {
        gInvitationsManager = new InvitationsManager();
    }
    return gInvitationsManager;
}

/**
 * The invitations manager class constructor
 *
 * XXX do we really need this to be an instance?
 *
 * @constructor
 */
function InvitationsManager() {
    this.mItemList = new Array();
    this.mStartDate = null;
    this.mJobsPending = 0;
    this.mTimer = null;

    var self = this;
    window.addEventListener("unload", function() {
        // Unload handlers get removed automatically
        self.cancelInvitationsUpdate();
    }, false);
}

InvitationsManager.prototype = {
    mItemList: null,
    mStartDate: null,
    mJobsPending: 0,
    mTimer: null,

    /**
     * Schedule an update for the invitations manager asynchronously.
     *
     * @param firstDelay          The timeout before the operation should start.
     * @param operationListener   The calIGenericOperationListener to notify.
     */
    scheduleInvitationsUpdate: function IM_scheduleInvitationsUpdate(firstDelay,
                                                                     operationListener) {
        this.cancelInvitationsUpdate();

        var self = this;
        this.mTimer = setTimeout(function startInvitationsTimer() {
            if (getPrefSafe("calendar.invitations.autorefresh.enabled", true)) {
                self.mTimer = setInterval(function repeatingInvitationsTimer() {
                    self.getInvitations(operationListener);
                    }, getPrefSafe("calendar.invitations.autorefresh.timeout", 3) * 60000);
            }
            self.getInvitations(operationListener);
        }, firstDelay);
    },

    /**
     * Cancel pending any pending invitations update.
     */
    cancelInvitationsUpdate: function IM_cancelInvitationsUpdate() {
        clearTimeout(this.mTimer);
    },

    /**
     * Retrieve invitations from all calendars. Notify all passed
     * operation listeners.
     *
     * @param operationListener1    The first operation listener to notify.
     * @param operationListener2    (optinal) The second operation listener to
     *                                notify.
     */
    getInvitations: function IM_getInvitations(operationListener1,
                                               operationListener2) {
        var listeners = [];
        if (operationListener1) {
            listeners.push(operationListener1);
        }
        if (operationListener2) {
            listeners.push(operationListener2);
        }

        gInvitationsRequestManager.cancelPendingRequests();
        this.updateStartDate();
        this.deleteAllItems();

        var cals = getCalendarManager().getCalendars({});

        var opListener = {
            mCount: cals.length,
            mRequestManager: gInvitationsRequestManager,
            mInvitationsManager: this,
            mHandledItems: {},

            // calIOperationListener
            onOperationComplete: function(aCalendar,
                                          aStatus,
                                          aOperationType,
                                          aId,
                                          aDetail) {
                if (--this.mCount == 0) {
                    this.mInvitationsManager.mItemList.sort(
                        function (a, b) {
                            return a.startDate.compare(b.startDate);
                        });
                    for each (var listener in listeners) {
                        try {
                            if (this.mInvitationsManager.mItemList.length) {
                                // Only call if there are actually items
                                listener.onGetResult(null,
                                                     Components.results.NS_OK,
                                                     Components.interfaces.calIItemBase,
                                                     null,
                                                     this.mInvitationsManager.mItemList.length,
                                                     this.mInvitationsManager.mItemList);
                            }
                            listener.onOperationComplete(null,
                                                         Components.results.NS_OK,
                                                         Components.interfaces.calIOperationListener.GET,
                                                         null,
                                                         null);
                        } catch (exc) {
                            ERROR(exc);
                        }
                    }
                }
            },

            onGetResult: function(aCalendar,
                                  aStatus,
                                  aItemType,
                                  aDetail,
                                  aCount,
                                  aItems) {
                if (Components.isSuccessCode(aStatus)) {
                    for each (var item in aItems) {
                        // we need to retrieve by occurrence to properly filter exceptions,
                        // should be fixed with bug 416975
                        item = item.parentItem;
                        var hid = item.hashId;
                        if (!this.mHandledItems[hid]) {
                            this.mHandledItems[hid] = true;
                            this.mInvitationsManager.addItem(item);
                        }
                    }
                }
            }
        };

        for each (var calendar in cals) {
            if (!isCalendarWritable(calendar) || calendar.getProperty("disabled")) {
                opListener.onOperationComplete();
                continue;
            }

            // temporary hack unless calCachedCalendar supports REQUEST_NEEDS_ACTION filter:
            calendar = calendar.getProperty("cache.uncachedCalendar");
            if (!calendar) {
                opListener.onOperationComplete();
                continue;
            }

            try {
                calendar = calendar.QueryInterface(Components.interfaces.calICalendar);
                var endDate = this.mStartDate.clone();
                endDate.year += 1;
                var op = calendar.getItems(Components.interfaces.calICalendar.ITEM_FILTER_REQUEST_NEEDS_ACTION |
                                           Components.interfaces.calICalendar.ITEM_FILTER_TYPE_ALL |
                                           // we need to retrieve by occurrence to properly filter exceptions,
                                           // should be fixed with bug 416975
                                           Components.interfaces.calICalendar.ITEM_FILTER_CLASS_OCCURRENCES,
                                           0, this.mStartDate,
                                           endDate /* we currently cannot pass null here, because of bug 416975 */,
                                           opListener);
                gInvitationsRequestManager.addRequestStatus(calendar, op);
            } catch (exc) {
                opListener.onOperationComplete();
                ERROR(exc);
            }
        }
    },

    /**
     * Open the invitations dialog, non-modal.
     *
     * XXX Passing these listeners in instead of keeping them in the window
     * sounds fishy to me. Maybe there is a more encapsulated solution.
     *
     * @param onLoadOpListener          The operation listener to notify when
     *                                    getting invitations. Should be passed
     *                                    to this.getInvitations().
     * @param finishedCallBack          A callback function to call when the
     *                                    dialog has completed.
     */
    openInvitationsDialog: function IM_openInvitationsDialog(onLoadOpListener,
                                                             finishedCallBack) {
        var args = new Object();
        args.onLoadOperationListener = onLoadOpListener;
        args.queue = new Array();
        args.finishedCallBack = finishedCallBack;
        args.requestManager = gInvitationsRequestManager;
        args.invitationsManager = this;
        // the dialog will reset this to auto when it is done loading
        window.setCursor("wait");
        // open the dialog
        window.openDialog(
            "chrome://calendar/content/calendar-invitations-dialog.xul",
            "_blank",
            "chrome,titlebar,resizable",
            args);
    },

    /**
     * Process the passed job queue. A job is an object that consists of an
     * action, a newItem and and oldItem. This processor only takes "modify"
     * operations into account.
     *
     * @param queue                         The array of objects to process.
     * @param jobQueueFinishedCallBack      A callback function called when
     *                                        job has finished.
     */
    processJobQueue: function IM_processJobQueue(queue,
                                                 jobQueueFinishedCallBack) {
        // TODO: undo/redo
        function operationListener(mgr, queueCallback, oldItem_) {
            this.mInvitationsManager = mgr;
            this.mJobQueueFinishedCallBack = queueCallback;
            this.mOldItem = oldItem_;
        }
        operationListener.prototype = {
            onOperationComplete: function (aCalendar,
                                           aStatus,
                                           aOperationType,
                                           aId,
                                           aDetail) {
                if (Components.isSuccessCode(aStatus) &&
                    aOperationType == Components.interfaces.calIOperationListener.MODIFY) {
                    cal.itip.checkAndSend(aOperationType, aDetail, this.mOldItem);
                    this.mInvitationsManager.deleteItem(aDetail);
                    this.mInvitationsManager.addItem(aDetail);
                }
                this.mInvitationsManager.mJobsPending--;
                if (this.mInvitationsManager.mJobsPending == 0 &&
                    this.mJobQueueFinishedCallBack) {
                    this.mJobQueueFinishedCallBack();
                }
            },

            onGetResult: function(aCalendar,
                                  aStatus,
                                  aItemType,
                                  aDetail,
                                  aCount,
                                  aItems) {

            }
        };

        this.mJobsPending = 0;
        for (var i = 0; i < queue.length; i++) {
            var job = queue[i];
            var oldItem = job.oldItem;
            var newItem = job.newItem;
            switch (job.action) {
                case 'modify':
                    this.mJobsPending++;
                    newItem.calendar.modifyItem(newItem,
                                                oldItem,
                                                new operationListener(this, jobQueueFinishedCallBack, oldItem));
                    break;
                default:
                    break;
            }
        }
        if (this.mJobsPending == 0 && jobQueueFinishedCallBack) {
            jobQueueFinishedCallBack();
        }
    },

    /**
     * Checks if the internal item list contains the given item
     * XXXdbo       Please document these correctly.
     *
     * @param item      The item to look for.
     * @return          A boolean value indicating if the item was found.
     */
    hasItem: function IM_hasItem(item) {
        var hid = item.hashId;
        return this.mItemList.some(
            function someFunc(item_) {
                return hid == item_.hashId;
            });
    },

    /**
     * Adds an item to the internal item list.
     * XXXdbo       Please document these correctly.
     *
     * @param item      The item to add.
     */
    addItem: function IM_addItem(item) {
        var recInfo = item.recurrenceInfo;
        if (recInfo && !cal.isOpenInvitation(item)) {
            // scan exceptions:
            var ids = recInfo.getExceptionIds({});
            for each (var id in ids) {
                var ex = recInfo.getExceptionFor(id);
                if (ex && this.validateItem(ex) && !this.hasItem(ex)) {
                    this.mItemList.push(ex);
                }
            }
        } else if (this.validateItem(item) && !this.hasItem(item)) {
            this.mItemList.push(item);
        }
    },

    /**
     * Removes an item from the internal item list
     * XXXdbo       Please document these correctly.
     *
     * @param item      The item to remove.
     */
    deleteItem: function IM_deleteItem(item) {
        var id = item.id;
        this.mItemList.filter(
            function filterFunc(item_) {
                return id != item_.id;
            });
    },

    /**
     * Remove all items from the internal item list
     * XXXdbo       Please document these correctly.
     */
    deleteAllItems: function IM_deleteAllItems() {
        this.mItemList = [];
    },

    /**
     * Helper function to create a start date to search from. This date is the
     * current time with hour/minute/second set to zero.
     *
     * @return      Potential start date.
     */
    getStartDate: function IM_getStartDate() {
        var date = now();
        date.second = 0;
        date.minute = 0;
        date.hour = 0;
        return date;
    },

    /**
     * Updates the start date for the invitations manager to the date returned
     * from this.getStartDate(), unless the previously existing start date is
     * the same or after what getStartDate() returned.
     */
    updateStartDate: function IM_updateStartDate() {
        if (!this.mStartDate) {
            this.mStartDate = this.getStartDate();
        } else {
            var startDate = this.getStartDate();
            if (startDate.compare(this.mStartDate) > 0) {
                this.mStartDate = startDate;
            }
        }
    },

    /**
     * Checks if the item is valid for the invitation manager. Checks if the
     * item is in the range of the invitation manager and if the item is a valid
     * invitation.
     *
     * @param item      The item to check
     * @return          A boolean indicating if the item is a valid invitation.
     */
    validateItem: function IM_validateItem(item) {
        if (item.calendar instanceof Components.interfaces.calISchedulingSupport &&
            !item.calendar.isInvitation(item)) {
            return false; // exclude if organizer has invited himself
        }
        var start = item[calGetStartDateProp(item)] || item[calGetEndDateProp(item)];
        return (cal.isOpenInvitation(item) &&
                start.compare(this.mStartDate) >= 0);
    }
};