summaryrefslogtreecommitdiff
path: root/rt/share/html/NoAuth/Calendar/dhandler
blob: 4b4aa631e8579e7d65808be2fccf6527a4212ef5 (plain)
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
<%init>

use Data::ICal;
use Data::ICal::Entry::Todo;
use Data::ICal::Entry::Event;
use Date::ICal;

$RT::ICalTicketType   ||= "Data::ICal::Entry::Todo";
$RT::ICalReminderType ||= "Data::ICal::Entry::Event";

my ($UserId, $SearchId, $MagicNumber);
my $arg = $m->dhandler_arg;

if ($arg =~ m{^(\d+)@(\d+)/(.*)$}) {
    $UserId = $1;
    $SearchId = $2;
    $MagicNumber = $3;
} elsif ($arg =~ m{^(\d+)/(.*)}) {
    $UserId = $1;
    $MagicNumber = $2;
} else {
    Abort("Corrupted URL.");
}

my $CurrentUser = new RT::CurrentUser();
$CurrentUser->LoadById($UserId);
my $user = $CurrentUser->Name;

# if no user, abort
unless ($CurrentUser->Id) {
    $RT::Logger->error("No such user id $UserId from $ENV{'REMOTE_ADDR'}");
    $m->out("RT/".$RT::VERSION ." ".404 ."\n\nno such file\n");
    $m->abort;
}

# verify user has LoadSavedSearch right
if ($SearchId and not $CurrentUser->HasRight( Right => 'LoadSavedSearch',
                                              Object=> $RT::System )) {
    $RT::Logger->error("not enough rights for user $user from $ENV{'REMOTE_ADDR'}");
    $m->out("RT/".$RT::VERSION ." ".404 ."\n\nno such file\n");
    $m->abort;
}


# if MagicNumber doesn't match the one stored in database, abort
my $Search;
my $ICalAttribute;
if ($SearchId) {
    $Search = $CurrentUser->Attributes->WithId($SearchId);
    $ICalAttribute = $Search->FirstAttribute('ICalURL');
} else {
    $ICalAttribute = $CurrentUser->UserObj->FirstAttribute('ICalURL');
}

unless ($ICalAttribute) {
    $RT::Logger->error("No such ICal feed for $user from $ENV{'REMOTE_ADDR'}");
    $m->out("RT/".$RT::VERSION ." ".404 ."\n\nno such file\n");
    $m->abort;
}


if ($MagicNumber ne $ICalAttribute->Content) {
    $RT::Logger->error("FAILED LOGIN for $user from $ENV{'REMOTE_ADDR'}");
    $m->out("RT/".$RT::VERSION ." ".404 ."\n\nno such file\n");
    $m->abort;
}

my $Tickets   = RT::Tickets->new($CurrentUser);

my $Query = "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
 AND ( Owner = '" . $CurrentUser->Id ."' OR Owner = 'Nobody'  )
 AND ( Type = 'reminder' OR 'Type' = 'ticket' )";

$Query = $Search->SubValue('Query')
    if $Search;

$Query .= " AND ( Due > '1970-01-01' OR Starts > '1970-01-01' )";

$Tickets->FromSQL($Query);

$Tickets->OrderBy(FIELD => 'Due', ORDER => 'ASC');

my $calendar = Data::ICal->new();

my ($uid) = $RT::WebURL =~ m{https?://([^:]+)};

while (my $Ticket = $Tickets->Next ) {

    my $event;
    if ($Ticket->Type eq 'ticket') {
	$event = add_todo($Ticket, $uid);
    } else {
	$event = add_event($Ticket, $uid);
    }
    next unless $event;
    $calendar->add_entry($event);
}

my $cal = $calendar->as_string;

$r->content_type('text/calendar;charset=utf-8');
$m->clear_buffer();
$m->out($cal);
$m->abort; 

sub add_event {
    my ($Reminder, $uid) = @_;

    return unless defined $Reminder->RefersTo->First;
    my $Ticket  = $Reminder->RefersTo->First->TargetObj;
    
    my %event = (
	summary => $Reminder->Subject ? $Reminder->Subject : '',
	url        => "${RT::WebURL}/Ticket/Display.html?id=" . $Ticket->id,
	uid        => Date::ICal->new( epoch => time() )->ical() . "-" . $Reminder->Id . "@" . $uid,
	categories => $Ticket->QueueObj->Name,
	dtstart     => Date::ICal->new( epoch => $Reminder->DueObj->Unix )->ical,
    );

    my $event = $RT::ICalReminderType->new();
    $event->add_properties(%event);

    return $event;
}

sub add_todo {
    my ($Ticket, $uid) = @_;

    my %vtodo = (
	summary    => $Ticket->Subject ? $Ticket->Subject : '',
	dtstart    => Date::ICal->new( epoch => $Ticket->CreatedObj->Unix )->ical,
	url        => "${RT::WebURL}/Ticket/Display.html?id=" . $Ticket->id,
	uid        => Date::ICal->new( epoch => time() )->ical() . "-" . $Ticket->Id . "@" . $uid,
	categories => $Ticket->QueueObj->Name,
    );

    $vtodo{due} = Date::ICal->new( epoch => $Ticket->DueObj->Unix )->ical,
        if $Ticket->DueObj;

    if ($Ticket->OwnerObj->Id != $RT::Nobody->Id and $Ticket->OwnerObj->EmailAddress) {
	$vtodo{organizer} = "MAILTO:" . $Ticket->OwnerObj->EmailAddress;
	$vtodo{attendee} = "MAILTO:" . $Ticket->OwnerObj->EmailAddress;
    } elsif ($Ticket->QueueObj->CommentAddress) {
	$vtodo{organizer} = "MAILTO:" . $Ticket->QueueObj->CommentAddress;
	$vtodo{attendee} = "MAILTO:" . $Ticket->QueueObj->CommentAddress; 
    }

    $vtodo{priority} = $Ticket->Priority
	if $Ticket->Priority;

    my $vtodo = $RT::ICalTicketType->new();
    $vtodo->add_properties(%vtodo);

    return $vtodo;
}



</%init>