2929
3030use OCA \DAV \CalDAV \Auth \CustomPrincipalPlugin ;
3131use OCA \DAV \CalDAV \InvitationResponse \InvitationResponseServer ;
32+ use OCP \AppFramework \Utility \ITimeFactory ;
3233use OCP \Calendar \Exceptions \CalendarException ;
3334use OCP \Calendar \ICreateFromString ;
3435use OCP \Constants ;
36+ use OCP \Security \ISecureRandom ;
37+ use Psr \Log \LoggerInterface ;
3538use Sabre \DAV \Exception \Conflict ;
39+ use Sabre \VObject \Component \VCalendar ;
40+ use Sabre \VObject \Component \VEvent ;
41+ use Sabre \VObject \Document ;
42+ use Sabre \VObject \ITip \Message ;
43+ use Sabre \VObject \Property \VCard \DateTime ;
44+ use Sabre \VObject \Reader ;
3645use function Sabre \Uri \split as uriSplit ;
3746
3847class CalendarImpl implements ICreateFromString {
@@ -46,13 +55,6 @@ class CalendarImpl implements ICreateFromString {
4655 /** @var array */
4756 private $ calendarInfo ;
4857
49- /**
50- * CalendarImpl constructor.
51- *
52- * @param Calendar $calendar
53- * @param array $calendarInfo
54- * @param CalDavBackend $backend
55- */
5658 public function __construct (Calendar $ calendar ,
5759 array $ calendarInfo ,
5860 CalDavBackend $ backend ) {
@@ -177,4 +179,59 @@ public function createFromString(string $name, string $calendarData): void {
177179 fclose ($ stream );
178180 }
179181 }
182+
183+ /**
184+ * @throws CalendarException
185+ */
186+ public function handleIMipMessage (string $ name , string $ calendarData ): void {
187+ $ server = new InvitationResponseServer (false );
188+
189+ /** @var CustomPrincipalPlugin $plugin */
190+ $ plugin = $ server ->server ->getPlugin ('auth ' );
191+ // we're working around the previous implementation
192+ // that only allowed the public system principal to be used
193+ // so set the custom principal here
194+ $ plugin ->setCurrentPrincipal ($ this ->calendar ->getPrincipalURI ());
195+
196+ if (empty ($ this ->calendarInfo ['uri ' ])) {
197+ throw new CalendarException ('Could not write to calendar as URI parameter is missing ' );
198+ }
199+ // Force calendar change URI
200+ /** @var Schedule\Plugin $schedulingPlugin */
201+ $ schedulingPlugin = $ server ->server ->getPlugin ('caldav-schedule ' );
202+ // Let sabre handle the rest
203+ $ iTipMessage = new Message ();
204+ /** @var VCalendar $vObject */
205+ $ vObject = Reader::read ($ calendarData );
206+ /** @var VEvent $vEvent */
207+ $ vEvent = $ vObject ->{'VEVENT ' };
208+
209+ if ($ vObject ->{'METHOD ' } === null ) {
210+ throw new CalendarException ('No Method provided for scheduling data. Could not process message ' );
211+ }
212+
213+ if (!isset ($ vEvent ->{'ORGANIZER ' }) || !isset ($ vEvent ->{'ATTENDEE ' })) {
214+ throw new CalendarException ('Could not process scheduling data, neccessary data missing from ICAL ' );
215+ }
216+ $ orgaizer = $ vEvent ->{'ORGANIZER ' }->getValue ();
217+ $ attendee = $ vEvent ->{'ATTENDEE ' }->getValue ();
218+
219+ $ iTipMessage ->method = $ vObject ->{'METHOD ' }->getValue ();
220+ if ($ iTipMessage ->method === 'REPLY ' ) {
221+ if ($ server ->isExternalAttendee ($ vEvent ->{'ATTENDEE ' }->getValue ())) {
222+ $ iTipMessage ->recipient = $ orgaizer ;
223+ } else {
224+ $ iTipMessage ->recipient = $ attendee ;
225+ }
226+ $ iTipMessage ->sender = $ attendee ;
227+ } else if ($ iTipMessage ->method === 'CANCEL ' ) {
228+ $ iTipMessage ->recipient = $ attendee ;
229+ $ iTipMessage ->sender = $ orgaizer ;
230+ }
231+ $ iTipMessage ->uid = isset ($ vEvent ->{'UID ' }) ? $ vEvent ->{'UID ' }->getValue () : '' ;
232+ $ iTipMessage ->component = 'VEVENT ' ;
233+ $ iTipMessage ->sequence = isset ($ vEvent ->{'SEQUENCE ' }) ? (int )$ vEvent ->{'SEQUENCE ' }->getValue () : 0 ;
234+ $ iTipMessage ->message = $ vObject ;
235+ $ schedulingPlugin ->scheduleLocalDelivery ($ iTipMessage );
236+ }
180237}
0 commit comments