Debian Codegraph @

src/EWS/common/e-ews-query-to-restriction.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
3 * SPDX-FileContributor: Punit Jain <[email protected]>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "evolution-ews-config.h"
8
9 #include <string.h>
10 #include <glib/gi18n-lib.h>
11 #include <glib/gstdio.h>
12 #include <camel/camel.h>
13 #include <libedata-cal/libedata-cal.h>
14
15 #include "e-ews-request.h"
16
17 #include "e-ews-query-to-restriction.h"
18
19 #define d(x) x
20
21 typedef enum {
22 MATCH_CONTAINS,
23 MATCH_IS,
24 MATCH_BEGINS_WITH,
25 MATCH_ENDS_WITH,
26 MATCH_AND,
27 MATCH_OR,
28 MATCH_NOT
29 } match_type;
30
31 typedef enum {
32 CONTACT_NAME,
33 CONTACT_NAME_OTHER,
34 CONTACT_EMAIL,
35 CONTACT_IM,
36 CONTACT_ADDRESS,
37 CONTACT_PHONE,
38 CONTACT_OTHER
39 } contact_type;
40
41 typedef struct ContactField {
42 gboolean indexed;
43 contact_type flag;
44 const gchar *field_uri;
45 } ContactField;
46
47 static ContactField contact_field[] = {
48 {FALSE, CONTACT_NAME, "contacts:DisplayName"},
49 {FALSE, CONTACT_NAME, "contacts:GivenName"},
50 {FALSE, CONTACT_NAME, "contacts:Nickname"},
51 {FALSE, CONTACT_NAME, "contacts:Surname"},
52 {FALSE, CONTACT_NAME, "contacts:MiddleName"},
53 {FALSE, CONTACT_NAME_OTHER, "contacts:AssistantName"},
54 {FALSE, CONTACT_NAME_OTHER, "contacts:CompanyName"},
55 {FALSE, CONTACT_NAME_OTHER, "contacts:Manager"},
56 {FALSE, CONTACT_NAME_OTHER, "contacts:SpouseName"},
57 {FALSE, CONTACT_OTHER, "contacts:BusinessHomePage"},
58 {FALSE, CONTACT_OTHER, "contacts:JobTitle"},
59 {FALSE, CONTACT_OTHER, "contacts:Department"},
60 {FALSE, CONTACT_OTHER, "contacts:Profession"},
61
62 {TRUE, CONTACT_IM, "contacts:ImAddress"},
63 {TRUE, CONTACT_ADDRESS, "contacts:PhysicalAddress:Street"},
64 {TRUE, CONTACT_ADDRESS, "contacts:PhysicalAddress:City"},
65 {TRUE, CONTACT_ADDRESS, "contacts:PhysicalAddress:State"},
66 {TRUE, CONTACT_ADDRESS, "contacts:PhysicalAddress:Country"},
67 {TRUE, CONTACT_ADDRESS, "contacts:PhysicalAddress:PostalCode"},
68 {TRUE, CONTACT_PHONE, "contacts:PhoneNumber"},
69 {TRUE, CONTACT_EMAIL, "contacts:EmailAddress"}
70 };
71
72 typedef struct CalendarField {
73 gboolean any_field;
74 const gchar *field_uri;
75 } CalendarField;
76
77 static CalendarField calendar_field[] = {
78 {FALSE, "calendar:Start"},
79 {FALSE, "calendar:End"},
80 {FALSE, "calendar:OriginalStart"},
81 {FALSE, "calendar:IsAllDayEvent"},
82 {FALSE, "calendar:LegacyFreeBusyStatus"},
83 {TRUE, "calendar:Location"},
84 {FALSE, "calendar:When"},
85 {FALSE, "calendar:IsMeeting"},
86 {FALSE, "calendar:IsCancelled"},
87 {FALSE, "calendar:IsRecurring"},
88 {FALSE, "calendar:MeetingRequestWasSent"},
89 {FALSE, "calendar:IsResponseRequested"},
90 {FALSE, "calendar:CalendarItemType"},
91 {TRUE, "calendar:Organizer"},
92 {TRUE, "calendar:RequiredAttendees"},
93 {TRUE, "calendar:OptionalAttendees"},
94 {TRUE, "calendar:Resources"},
95 {FALSE, "calendar:Duration"},
96 {FALSE, "calendar:TimeZone"},
97 {FALSE, "calendar:AppointmentState"},
98 {FALSE, "calendar:ConferenceType"},
99 {FALSE, "calendar:IsOnlineMeeting"},
100 {TRUE, "calendar:MeetingWorkspaceUrl"}
101 };
102
103 typedef struct ItemField {
104 gboolean any_field;
105 const gchar *field_uri;
106 } ItemField;
107
108 static ItemField item_field[] = {
109 {TRUE, "item:Subject"},
110 {TRUE, "item:Body"},
111 {FALSE, "item:HasAttachments"},
112 {TRUE, "item:Categories"},
113 {FALSE, "item:Importance"},
114 {FALSE, "item:Sensitivity"},
115 {FALSE, "item:InternetMessageHeader"}
116 };
117
118 struct EmailIndex {
119 const gchar *field_index;
120 } email_index[] = {
121 {"EmailAddress1"},
122 {"EmailAddress2"},
123 {"EmailAddress3"}
124 };
125
126 typedef struct _EvalContext {
127 ESoapRequest *req; /* NULL when just checking whether any applied */
128 gboolean any_applicable;
129 } EvalContext;
130
131 static void
132 ews_restriction_write_contains_message (EvalContext *ctx,
133 const gchar *mode,
134 const gchar *compare,
135 const gchar *uri,
136 const gchar *val)
137 {
138 g_return_if_fail (ctx != NULL);
139
140 if (!ctx->req) {
141 ctx->any_applicable = TRUE;
142 return;
143 }
144
145 e_soap_request_start_element (ctx->req, "Contains", NULL, NULL);
146 e_soap_request_add_attribute (ctx->req, "ContainmentMode", mode, NULL, NULL);
147 e_soap_request_add_attribute (ctx->req, "ContainmentComparison", compare, NULL, NULL);
148 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
149 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
150 e_soap_request_end_element (ctx->req);
151 }
152
153 static void
154 ews_restriction_write_contains_message_indexed (EvalContext *ctx,
155 const gchar *mode,
156 const gchar *compare,
157 const gchar *uri,
158 const gchar *index,
159 const gchar *val)
160 {
161 g_return_if_fail (ctx != NULL);
162
163 if (!ctx->req) {
164 ctx->any_applicable = TRUE;
165 return;
166 }
167
168 e_soap_request_start_element (ctx->req, "Contains", NULL, NULL);
169 e_soap_request_add_attribute (ctx->req, "ContainmentMode", mode, NULL, NULL);
170 e_soap_request_add_attribute (ctx->req, "ContainmentComparison", compare, NULL, NULL);
171 e_soap_request_start_element (ctx->req, "IndexedFieldURI", NULL, NULL);
172 e_soap_request_add_attribute (ctx->req, "FieldURI", uri, NULL, NULL);
173 e_soap_request_add_attribute (ctx->req, "FieldIndex", index, NULL, NULL);
174 e_soap_request_end_element (ctx->req);
175 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
176 e_soap_request_end_element (ctx->req);
177 }
178
179 static void
180 ews_restriction_write_exists_message (EvalContext *ctx,
181 const gchar *uri)
182 {
183 g_return_if_fail (ctx != NULL);
184
185 if (!ctx->req) {
186 ctx->any_applicable = TRUE;
187 return;
188 }
189
190 e_soap_request_start_element (ctx->req, "Exists", NULL, NULL);
191 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
192 e_soap_request_end_element (ctx->req);
193 }
194
195 static void
196 ews_restriction_write_greater_than_or_equal_to_message (EvalContext *ctx,
197 const gchar *uri,
198 const gchar *val)
199 {
200 g_return_if_fail (ctx != NULL);
201
202 if (!ctx->req) {
203 ctx->any_applicable = TRUE;
204 return;
205 }
206
207 e_soap_request_start_element (ctx->req, "IsGreaterThanOrEqualTo", NULL, NULL);
208 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
209 e_soap_request_start_element (ctx->req, "FieldURIOrConstant", NULL, NULL);
210 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
211 e_soap_request_end_element (ctx->req);
212 e_soap_request_end_element (ctx->req);
213 }
214
215 static void
216 ews_restriction_write_less_than_or_equal_to_message (EvalContext *ctx,
217 const gchar *uri,
218 const gchar *val)
219 {
220 g_return_if_fail (ctx != NULL);
221
222 if (!ctx->req) {
223 ctx->any_applicable = TRUE;
224 return;
225 }
226
227 e_soap_request_start_element (ctx->req, "IsLessThanOrEqualTo", NULL, NULL);
228 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
229 e_soap_request_start_element (ctx->req, "FieldURIOrConstant", NULL, NULL);
230 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
231 e_soap_request_end_element (ctx->req);
232 e_soap_request_end_element (ctx->req);
233 }
234
235 static void
236 ews_restriction_write_greater_than_message (EvalContext *ctx,
237 const gchar *uri,
238 const gchar *val)
239 {
240 g_return_if_fail (ctx != NULL);
241
242 if (!ctx->req) {
243 ctx->any_applicable = TRUE;
244 return;
245 }
246
247 e_soap_request_start_element (ctx->req, "IsGreaterThan", NULL, NULL);
248 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
249 e_soap_request_start_element (ctx->req, "FieldURIOrConstant", NULL, NULL);
250 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
251 e_soap_request_end_element (ctx->req);
252 e_soap_request_end_element (ctx->req);
253 }
254
255 static void
256 ews_restriction_write_less_than_message (EvalContext *ctx,
257 const gchar *uri,
258 const gchar *val)
259 {
260 g_return_if_fail (ctx != NULL);
261
262 if (!ctx->req) {
263 ctx->any_applicable = TRUE;
264 return;
265 }
266
267 e_soap_request_start_element (ctx->req, "IsLessThan", NULL, NULL);
268 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
269 e_soap_request_start_element (ctx->req, "FieldURIOrConstant", NULL, NULL);
270 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
271 e_soap_request_end_element (ctx->req);
272 e_soap_request_end_element (ctx->req);
273 }
274
275 static void
276 ews_restriction_write_is_equal_to_message (EvalContext *ctx,
277 const gchar *uri,
278 const gchar *val)
279 {
280 g_return_if_fail (ctx != NULL);
281
282 if (!ctx->req) {
283 ctx->any_applicable = TRUE;
284 return;
285 }
286
287 e_soap_request_start_element (ctx->req, "IsEqualTo", NULL, NULL);
288 e_ews_request_write_string_parameter_with_attribute (ctx->req, "FieldURI", NULL, NULL, "FieldURI", uri);
289 e_soap_request_start_element (ctx->req, "FieldURIOrConstant", NULL, NULL);
290 e_ews_request_write_string_parameter_with_attribute (ctx->req, "Constant", NULL, NULL, "Value", val);
291 e_soap_request_end_element (ctx->req);
292 e_soap_request_end_element (ctx->req);
293 }
294
295 static ESExpResult *
296 e_ews_implement_contact_contains (ESExp *f,
297 gint argc,
298 ESExpResult **argv,
299 gpointer data,
300 match_type type)
301 {
302 ESExpResult *r;
303 EvalContext *ctx = data;
304
305 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING) {
306 const gchar *field;
307 field = argv[0]->value.string;
308
309 if (argv[1]->type == ESEXP_RES_STRING && argv[1]->value.string != NULL) {
310 const gchar *mode = NULL;
311
312 if (type == MATCH_CONTAINS || type == MATCH_ENDS_WITH)
313 mode = "Substring";
314 else if (type == MATCH_BEGINS_WITH)
315 mode = "Prefixed";
316 else if (type == MATCH_IS)
317 mode = "FullString";
318 else
319 mode = "Substring";
320
321 if (!strcmp (field, "full_name")) {
322 gint n = 0;
323 const gchar *value;
324 value = argv[1]->value.string;
325
326 if (!ctx->req) {
327 ctx->any_applicable = TRUE;
328 } else {
329 e_soap_request_start_element (ctx->req, "Or", NULL, NULL);
330 while (n < G_N_ELEMENTS (contact_field)) {
331 if ((contact_field[n].flag == CONTACT_NAME) && (!contact_field[n].indexed)) {
332 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", contact_field[n].field_uri, value);
333 }
334 n++;
335 }
336 e_soap_request_end_element (ctx->req); /* Or */
337 }
338 } else if (!strcmp (field, "x-evolution-any-field")) {
339 gint n = 0;
340 const gchar *value;
341 value = argv[1]->value.string;
342
343 if (!ctx->req) {
344 ctx->any_applicable = TRUE;
345 } else {
346 e_soap_request_start_element (ctx->req, "Or", NULL, NULL);
347 while (n < G_N_ELEMENTS (contact_field)) {
348 if (!contact_field[n].indexed) {
349 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", contact_field[n].field_uri, value);
350 } else if (contact_field[n].flag == CONTACT_EMAIL && contact_field[n].indexed) {
351 gint i = 0;
352 while (i < G_N_ELEMENTS (email_index)) {
353 ews_restriction_write_contains_message_indexed (ctx, "Substring", "IgnoreCase", "contacts:EmailAddress", email_index[i].field_index, value);
354 i++;
355 }
356 }
357 n++;
358 }
359 e_soap_request_end_element (ctx->req); /* Or */
360 }
361 } else if (!strcmp (field, "email")) {
362 const gchar *value;
363 gint n = 0;
364 value = argv[1]->value.string;
365
366 if (!ctx->req) {
367 ctx->any_applicable = TRUE;
368 } else {
369 e_soap_request_start_element (ctx->req, "Or", NULL, NULL);
370 while (n < G_N_ELEMENTS (email_index)) {
371 ews_restriction_write_contains_message_indexed (ctx, mode, "IgnoreCase", "contacts:EmailAddress", email_index[n].field_index, value);
372 n++;
373 }
374 e_soap_request_end_element (ctx->req); /* Or */
375 }
376 } else if (!strcmp (field, "category_list")) {
377 const gchar *value;
378 value = argv[1]->value.string;
379
380 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "item:Categories", value);
381 }
382 }
383 }
384
385 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
386
387 return r;
388 }
389
390 static ESExpResult *
391 e_ews_func_and_or_not (ESExp *f,
392 gint argc,
393 ESExpTerm **argv,
394 gpointer data,
395 match_type type)
396 {
397 ESExpResult *r, *r1;
398 EvalContext *ctx = data;
399 ESoapRequest *used_req;
400 gboolean was_any_applicable;
401 gint ii, n_applicable = 0;
402 const gchar *elem_name = NULL;
403
404 /* "And" and "Or" expect at least two arguments */
405
406 if (argc == 0)
407 goto result;
408
409 was_any_applicable = ctx->any_applicable;
410 used_req = ctx->req;
411 ctx->req = NULL;
412
413 for (ii = 0; ii < argc; ii++) {
414 ctx->any_applicable = FALSE;
415
416 r1 = e_sexp_term_eval (f, argv[ii]);
417 e_sexp_result_free (f, r1);
418
419 if (ctx->any_applicable)
420 n_applicable++;
421 }
422
423 ctx->req = used_req;
424
425 if (!ctx->req || !n_applicable) {
426 ctx->any_applicable = n_applicable > 0 || was_any_applicable;
427 goto result;
428 }
429
430 if (type == MATCH_AND) {
431 if (n_applicable >= 2)
432 elem_name = "And";
433
434 } else if (type == MATCH_OR) {
435 if (n_applicable >= 2)
436 elem_name = "Or";
437
438 } else if (type == MATCH_NOT)
439 elem_name = "Not";
440
441 if (elem_name)
442 e_soap_request_start_element (ctx->req, elem_name, NULL, NULL);
443
444 for (ii = 0; ii < argc; ii++) {
445 r1 = e_sexp_term_eval (f, argv[ii]);
446 e_sexp_result_free (f, r1);
447 }
448
449 if (elem_name)
450 e_soap_request_end_element (ctx->req);
451
452 result:
453 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
454
455 return r;
456 }
457
458 static ESExpResult *
459 calendar_func_contains (ESExp *f,
460 gint argc,
461 ESExpResult **argv,
462 gpointer data)
463 {
464 ESExpResult *r;
465 EvalContext *ctx = data;
466
467 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING) {
468 const gchar *field;
469 field = argv[0]->value.string;
470
471 if (argv[1]->type == ESEXP_RES_STRING && argv[1]->value.string[0] != 0) {
472 if (!g_strcmp0 (field, "summary")) {
473 const gchar *value;
474 value = argv[1]->value.string;
475
476 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Subject", value);
477 } else if (!g_strcmp0 (field, "description")) {
478 const gchar *value;
479 value = argv[1]->value.string;
480
481 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Body", value);
482 } else if (!g_strcmp0 (field, "location")) {
483 const gchar *value;
484 value = argv[1]->value.string;
485
486 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "calendar:Location", value);
487 } else if (!g_strcmp0 (field, "attendee")) {
488 const gchar *value;
489 value = argv[1]->value.string;
490
491 if (!ctx->req) {
492 ctx->any_applicable = TRUE;
493 } else {
494 e_soap_request_start_element (ctx->req, "Or", NULL, NULL);
495 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "calendar:RequiredAttendees", value);
496 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "calendar:OptionalAttendees", value);
497 e_soap_request_end_element (ctx->req);
498 }
499 } else if (!g_strcmp0 (field, "organizer")) {
500 const gchar *value;
501 value = argv[1]->value.string;
502
503 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "calendar:Organizer", value);
504 } else if (!g_strcmp0 (field, "classification")) {
505 const gchar *value;
506 value = argv[1]->value.string;
507
508 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Sensitivity", value);
509 } else if (!g_strcmp0 (field, "priority")) {
510 const gchar *value;
511 value = argv[1]->value.string;
512
513 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Importance", value);
514 } else if (!g_strcmp0 (field, "any")) {
515 const gchar *value;
516 gint n = 0;
517 value = argv[1]->value.string;
518
519 if (!ctx->req) {
520 ctx->any_applicable = TRUE;
521 } else {
522 e_soap_request_start_element (ctx->req, "Or", NULL, NULL);
523 while (n < G_N_ELEMENTS (calendar_field)) {
524 if (calendar_field[n].any_field) {
525 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", calendar_field[n].field_uri, value);
526 }
527 n++;
528 }
529 n = 0;
530 while (n < G_N_ELEMENTS (item_field)) {
531 if (item_field[n].any_field) {
532 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", item_field[n].field_uri, value);
533 }
534 n++;
535 }
536 e_soap_request_end_element (ctx->req);
537 }
538 }
539 }
540 }
541
542 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
543
544 return r;
545 }
546
547 static ESExpResult *
548 calendar_func_has_categories (ESExp *f,
549 gint argc,
550 ESExpResult **argv,
551 gpointer data)
552 {
553 ESExpResult *r;
554 EvalContext *ctx = data;
555
556 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
557 const gchar *value;
558 value = argv[0]->value.string;
559
560 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Categories", value);
561 }
562
563 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
564
565 return r;
566 }
567
568 static ESExpResult *
569 calendar_func_has_attachment (ESExp *f,
570 gint argc,
571 ESExpResult **argv,
572 gpointer data)
573 {
574 ESExpResult *r;
575 EvalContext *ctx = data;
576
577 if (argc == 0)
578 ews_restriction_write_exists_message (ctx, "item:HasAttachments");
579
580 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
581
582 return r;
583 }
584
585 static ESExpResult *
586 calendar_func_has_recurrence (ESExp *f,
587 gint argc,
588 ESExpResult **argv,
589 gpointer data)
590 {
591 ESExpResult *r;
592 EvalContext *ctx = data;
593
594 if (argc == 0)
595 ews_restriction_write_exists_message (ctx, "calendar:IsRecurring");
596
597 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
598
599 return r;
600 }
601
602 static gchar *
603 e_ews_make_timestamp (time_t when)
604 {
605 struct tm *tm;
606
607 tm = gmtime (&when);
608 return g_strdup_printf (
609 "%04d-%02d-%02dT%02d:%02d:%02dZ",
610 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
611 tm->tm_hour, tm->tm_min, tm->tm_sec);
612 }
613
614 static ESExpResult *
615 calendar_func_occur_in_time_range (ESExp *f,
616 gint argc,
617 ESExpResult **argv,
618 gpointer data)
619 {
620 ESExpResult *r;
621 EvalContext *ctx = data;
622
623 if (argv[0]->type != ESEXP_RES_TIME) {
624 e_sexp_fatal_error (
625 f, "occur-in-time-range? expects argument 1 "
626 "to be a time_t");
627 return NULL;
628 }
629
630 if (argv[1]->type != ESEXP_RES_TIME) {
631 e_sexp_fatal_error (
632 f, "occur-in-time-range? expects argument 2 "
633 "to be a time_t");
634 return NULL;
635 }
636
637 if (!ctx->req) {
638 ctx->any_applicable = TRUE;
639 } else {
640 gchar *start, *end;
641
642 start = e_ews_make_timestamp (argv[0]->value.time);
643 end = e_ews_make_timestamp (argv[1]->value.time);
644
645 e_soap_request_start_element (ctx->req, "And", NULL, NULL);
646 ews_restriction_write_greater_than_or_equal_to_message (ctx, "calendar:Start", start);
647 ews_restriction_write_less_than_or_equal_to_message (ctx, "calendar:End", end);
648 e_soap_request_end_element (ctx->req);
649
650 g_free (start);
651 g_free (end);
652 }
653
654 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
655
656 return r;
657 }
658
659 static ESExpResult *
660 calendar_func_occurrences_count (ESExp *f,
661 gint argc,
662 ESExpResult **argv,
663 gpointer data)
664 {
665 ESExpResult *r;
666
667 /*ews doesn't support restriction based on number of occurrences*/
668 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
669
670 return r;
671 }
672
673 static ESExpResult *
674 message_func_body_contains (ESExp *f,
675 gint argc,
676 ESExpResult **argv,
677 gpointer data)
678
679 {
680 ESExpResult *r;
681 EvalContext *ctx = data;
682
683 if (argv[0]->type == ESEXP_RES_STRING) {
684 const gchar *value;
685 value = argv[0]->value.string;
686
687 ews_restriction_write_contains_message (ctx, "Substring", "IgnoreCase", "item:Body", value);
688 }
689
690 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
691
692 return r;
693 }
694
695 static ESExpResult *
696 common_message_func_header_contains (ESExp *f,
697 gint argc,
698 ESExpResult **argv,
699 gpointer data,
700 match_type type)
701
702 {
703 ESExpResult *r;
704 EvalContext *ctx = data;
705 const gchar *mode;
706
707 if (type == MATCH_CONTAINS || type == MATCH_ENDS_WITH)
708 mode = "Substring";
709 else if (type == MATCH_BEGINS_WITH)
710 mode = "Prefixed";
711 else if (type == MATCH_IS)
712 mode = "FullString";
713 else
714 mode = "Substring";
715
716 if (argv[0]->type == ESEXP_RES_STRING) {
717 const gchar *headername;
718 headername = argv[0]->value.string;
719
720 if (argv[1]->type == ESEXP_RES_STRING) {
721 const gchar *value;
722 value = argv[1]->value.string;
723
724 if (!g_ascii_strcasecmp (headername, "subject")) {
725 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "item:Subject", value);
726 } else if (!g_ascii_strcasecmp (headername, "from")) {
727 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "message:From", value);
728 } else if (!g_ascii_strcasecmp (headername, "to")) {
729 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "message:ToRecipients", value);
730 } else if (!g_ascii_strcasecmp (headername, "cc")) {
731 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "message:CcRecipients", value);
732 } else if (!g_ascii_strcasecmp (headername, "bcc")) {
733 ews_restriction_write_contains_message (ctx, mode, "IgnoreCase", "message:BccRecipients", value);
734 }
735 }
736 }
737
738 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
739
740 return r;
741 }
742
743 static ESExpResult *
744 message_func_header_exists (ESExp *f,
745 gint argc,
746 ESExpResult **argv,
747 gpointer data)
748 {
749 ESExpResult *r;
750 EvalContext *ctx = data;
751
752 if (argv[0]->type == ESEXP_RES_STRING) {
753 const gchar *headername;
754 headername = argv[0]->value.string;
755
756 if (!g_ascii_strcasecmp (headername, "subject")) {
757 ews_restriction_write_exists_message (ctx, "item:Subject");
758 } else if (!g_ascii_strcasecmp (headername, "from")) {
759 ews_restriction_write_exists_message (ctx, "message:From");
760 } else if (!g_ascii_strcasecmp (headername, "to")) {
761 ews_restriction_write_exists_message (ctx, "message:ToRecipients");
762 } else if (!g_ascii_strcasecmp (headername, "cc")) {
763 ews_restriction_write_exists_message (ctx, "message:CcRecipients");
764 } else if (!g_ascii_strcasecmp (headername, "bcc")) {
765 ews_restriction_write_exists_message (ctx, "message:BccRecipients");
766 }
767 }
768
769 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
770
771 return r;
772 }
773
774 static ESExpResult *
775 message_func_system_flag (ESExp *f,
776 gint argc,
777 ESExpResult **argv,
778 gpointer data)
779 {
780 ESExpResult *r;
781 EvalContext *ctx = data;
782
783 if (argv[0]->type == ESEXP_RES_STRING) {
784 const gchar *name;
785 name = argv[0]->value.string;
786 if (!g_ascii_strcasecmp (name, "Attachments")) {
787 ews_restriction_write_exists_message (ctx, "item:HasAttachments");
788 } else if (!g_ascii_strcasecmp (name, "deleted") || !g_ascii_strcasecmp (name, "junk")) {
789 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
790 r->value.boolean = FALSE;
791 return r;
792 }
793 }
794
795 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
796
797 return r;
798
799 }
800
801 static ESExpResult *
802 message_func_sent_date (ESExp *f,
803 gint argc,
804 ESExpResult **argv,
805 gpointer data)
806 {
807 ESExpResult *r;
808
809 r = e_sexp_result_new (f, ESEXP_RES_STRING);
810 r->value.string = g_strdup ("sent-date");
811
812 return r;
813 }
814
815 static ESExpResult *
816 message_func_received_date (ESExp *f,
817 gint argc,
818 ESExpResult **argv,
819 gpointer data)
820 {
821 ESExpResult *r;
822
823 r = e_sexp_result_new (f, ESEXP_RES_STRING);
824 r->value.string = g_strdup ("received-date");
825
826 return r;
827 }
828
829 static ESExpResult *
830 message_func_current_date (ESExp *f,
831 gint argc,
832 ESExpResult **argv,
833 gpointer data)
834 {
835 ESExpResult *r;
836
837 r = e_sexp_result_new (f, ESEXP_RES_INT);
838 r->value.time = time (NULL);
839 return r;
840 }
841
842 static ESExpResult *
843 message_func_relative_months (ESExp *f,
844 gint argc,
845 ESExpResult **argv,
846 gpointer data)
847 {
848 ESExpResult *r;
849
850 if (argc != 1 || argv[0]->type != ESEXP_RES_INT) {
851 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
852 r->value.boolean = FALSE;
853
854 } else {
855 r = e_sexp_result_new (f, ESEXP_RES_INT);
856 r->value.number = camel_folder_search_util_add_months (time (NULL), argv[0]->value.number);
857 }
858
859 return r;
860 }
861
862 static ESExpResult *
863 message_func_get_size (ESExp *f,
864 gint argc,
865 ESExpResult **argv,
866 gpointer data)
867 {
868 ESExpResult *r;
869
870 r = e_sexp_result_new (f, ESEXP_RES_STRING);
871 r->value.string = g_strdup ("message-size");
872
873 return r;
874 }
875
876 static ESExpResult *
877 func_eq (ESExp *f,
878 gint argc,
879 ESExpResult **argv,
880 gpointer data)
881 {
882 ESExpResult *r;
883 EvalContext *ctx = data;
884
885 if (argc != 2) {
886 e_sexp_fatal_error (f, "two arguments are required for this operation");
887 return NULL;
888 }
889
890 if (argv[0]->type == ESEXP_RES_STRING) {
891 const gchar *name;
892 const gchar *field_uri = NULL;
893
894 name = argv[0]->value.string;
895
896 if (!g_strcmp0 (name, "sent-date")) {
897 field_uri = "item:DateTimeSent";
898 } else if (!g_strcmp0 (name, "received-date")) {
899 field_uri = "item:DateTimeReceived";
900 }
901
902 if (field_uri && argv[1]->type == ESEXP_RES_INT && argv[1]->value.number != 0) {
903 time_t time;
904 gchar *date;
905 time = argv[1]->value.number;
906 date = e_ews_make_timestamp (time);
907
908 ews_restriction_write_is_equal_to_message (ctx, field_uri, date);
909 g_free (date);
910 }
911 }
912
913 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
914
915 return r;
916 }
917
918 static ESExpResult *
919 func_gt (ESExp *f,
920 gint argc,
921 ESExpResult **argv,
922 gpointer data)
923 {
924 ESExpResult *r;
925 EvalContext *ctx = data;
926
927 if (argc != 2) {
928 e_sexp_fatal_error (f, "two arguments are required for this operation");
929 return NULL;
930 }
931
932 if (argv[0]->type == ESEXP_RES_STRING) {
933 const gchar *name;
934 const gchar *field_uri = NULL;
935 gboolean is_time = FALSE;
936
937 name = argv[0]->value.string;
938
939 if (!g_strcmp0 (name, "sent-date")) {
940 field_uri = "item:DateTimeSent";
941 is_time = TRUE;
942 } else if (!g_strcmp0 (name, "received-date")) {
943 field_uri = "item:DateTimeReceived";
944 is_time = TRUE;
945 } else if (!g_strcmp0 (name, "message-size")) {
946 field_uri = "item:Size";
947 is_time = FALSE;
948 }
949
950 if (field_uri && argv[1]->type == ESEXP_RES_INT && argv[1]->value.number != 0) {
951 if (is_time) {
952 time_t time;
953 gchar *date;
954 time = argv[1]->value.number;
955 date = e_ews_make_timestamp (time);
956
957 ews_restriction_write_greater_than_message (ctx, field_uri, date);
958 g_free (date);
959 } else {
960 gint value;
961 gchar val_str[16];
962
963 value = argv[1]->value.number;
964 value = value * (1024); //conver kB to Bytes.
965 g_sprintf (val_str, "%d", value);
966
967 ews_restriction_write_greater_than_message (ctx, field_uri, val_str);
968 }
969 }
970 }
971
972 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
973
974 return r;
975 }
976
977 static ESExpResult *
978 func_lt (ESExp *f,
979 gint argc,
980 ESExpResult **argv,
981 gpointer data)
982 {
983 ESExpResult *r;
984 EvalContext *ctx = data;
985
986 if (argc != 2) {
987 e_sexp_fatal_error (f, "two arguments are required for this operation");
988 return NULL;
989 }
990
991 if (argv[0]->type == ESEXP_RES_STRING) {
992 const gchar *name;
993 const gchar *field_uri = NULL;
994 gboolean is_time = FALSE;
995 name = argv[0]->value.string;
996
997 if (!g_strcmp0 (name, "sent-date")) {
998 field_uri = "item:DateTimeSent";
999 is_time = TRUE;
1000 } else if (!g_strcmp0 (name, "received-date")) {
1001 field_uri = "item:DateTimeReceived";
1002 is_time = TRUE;
1003 } else if (!g_strcmp0 (name, "message-size")) {
1004 field_uri = "item:Size";
1005 is_time = FALSE;
1006 }
1007
1008 if (field_uri && argv[1]->type == ESEXP_RES_INT && argv[1]->value.number != 0) {
1009 if (is_time) {
1010 time_t time;
1011 gchar *date;
1012 time = argv[1]->value.number;
1013 date = e_ews_make_timestamp (time);
1014
1015 ews_restriction_write_less_than_message (ctx, field_uri, date);
1016 g_free (date);
1017 } else {
1018 gint value;
1019 gchar val_str[16];
1020
1021 value = argv[1]->value.number;
1022 value = value * (1024); //conver kB to Bytes.
1023 g_sprintf (val_str, "%d", value);
1024
1025 ews_restriction_write_less_than_message (ctx, field_uri, val_str);
1026 }
1027 }
1028 }
1029
1030 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
1031
1032 return r;
1033 }
1034
1035 static ESExpResult *
1036 message_func_match_all (ESExp *f,
1037 gint argc,
1038 ESExpResult **argv,
1039 gpointer data)
1040 {
1041 ESExpResult *r;
1042
1043 r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
1044
1045 return r;
1046 }
1047
1048 static ESExpResult *
1049 message_func_header_contains (ESExp *f,
1050 gint argc,
1051 ESExpResult **argv,
1052 gpointer data)
1053 {
1054 return common_message_func_header_contains (f, argc, argv, data, MATCH_CONTAINS);
1055 }
1056
1057 static ESExpResult *
1058 message_func_header_matches (ESExp *f,
1059 gint argc,
1060 ESExpResult **argv,
1061 gpointer data)
1062 {
1063 return common_message_func_header_contains (f, argc, argv, data, MATCH_IS);
1064 }
1065
1066 static ESExpResult *
1067 message_func_header_starts_with (ESExp *f,
1068 gint argc,
1069 ESExpResult **argv,
1070 gpointer data)
1071 {
1072 return common_message_func_header_contains (f, argc, argv, data, MATCH_BEGINS_WITH);
1073 }
1074
1075 static ESExpResult *
1076 message_func_header_ends_with (ESExp *f,
1077 gint argc,
1078 ESExpResult **argv,
1079 gpointer data)
1080 {
1081 return common_message_func_header_contains (f, argc, argv, data, MATCH_ENDS_WITH);
1082 }
1083
1084 static ESExpResult *
1085 contact_func_contains (ESExp *f,
1086 gint argc,
1087 ESExpResult **argv,
1088 gpointer data)
1089 {
1090 return e_ews_implement_contact_contains (f, argc, argv, data, MATCH_CONTAINS);
1091 }
1092
1093 static ESExpResult *
1094 contact_func_is (ESExp *f,
1095 gint argc,
1096 ESExpResult **argv,
1097 gpointer data)
1098 {
1099 return e_ews_implement_contact_contains (f, argc, argv, data, MATCH_IS);
1100 }
1101
1102 static ESExpResult *
1103 contact_func_beginswith (ESExp *f,
1104 gint argc,
1105 ESExpResult **argv,
1106 gpointer data)
1107 {
1108 return e_ews_implement_contact_contains (f, argc, argv, data, MATCH_BEGINS_WITH);
1109 }
1110
1111 static ESExpResult *
1112 contact_func_endswith (ESExp *f,
1113 gint argc,
1114 ESExpResult **argv,
1115 gpointer data)
1116 {
1117 return e_ews_implement_contact_contains (f, argc, argv, data, MATCH_ENDS_WITH);
1118 }
1119
1120 static ESExpResult *
1121 func_or (ESExp *f,
1122 gint argc,
1123 ESExpTerm **argv,
1124 gpointer data)
1125 {
1126 return e_ews_func_and_or_not (f, argc, argv, data, MATCH_OR);
1127 }
1128
1129 static ESExpResult *
1130 func_and (ESExp *f,
1131 gint argc,
1132 ESExpTerm **argv,
1133 gpointer data)
1134 {
1135 return e_ews_func_and_or_not (f, argc, argv, data, MATCH_AND);
1136 }
1137
1138 static ESExpResult *
1139 func_not (ESExp *f,
1140 gint argc,
1141 ESExpTerm **argv,
1142 gpointer data)
1143 {
1144 return e_ews_func_and_or_not (f, argc, argv, data, MATCH_NOT);
1145 }
1146
1147 static struct {
1148 const gchar *name;
1149 ESExpFunc *func;
1150 guint immediate :1;
1151 } contact_symbols[] = {
1152 { "and", (ESExpFunc *) func_and, 1},
1153 { "or", (ESExpFunc *) func_or, 1},
1154 { "not", (ESExpFunc *) func_not, 1},
1155
1156 { "contains", contact_func_contains, 0 },
1157 { "is", contact_func_is, 0 },
1158 { "beginswith", contact_func_beginswith, 0 },
1159 { "endswith", contact_func_endswith, 0 },
1160 };
1161
1162 static struct {
1163 const gchar *name;
1164 ESExpFunc *func;
1165 guint immediate :1;
1166 } calendar_symbols[] = {
1167 { "and", (ESExpFunc *) func_and, 1},
1168 { "or", (ESExpFunc *) func_or, 1},
1169 { "not", (ESExpFunc *) func_not, 1},
1170
1171 /* Time-related functions */
1172 { "make-time", e_cal_backend_sexp_func_make_time, 0 },
1173
1174 /* Component-related functions */
1175 { "contains?", calendar_func_contains, 0},
1176 { "has-categories?", calendar_func_has_categories, 0 },
1177 { "has-attachments?", calendar_func_has_attachment, 0 },
1178 { "has-recurrences?", calendar_func_has_recurrence, 0 },
1179 { "occur-in-time-range?", calendar_func_occur_in_time_range, 0 },
1180 { "occurrences-count?", calendar_func_occurrences_count, 0 }
1181 };
1182
1183 static struct {
1184 const gchar *name;
1185 ESExpFunc *func;
1186 guint immediate :1;
1187 } message_symbols[] = {
1188 { "and", (ESExpFunc *) func_and, 1},
1189 { "or", (ESExpFunc *) func_or, 1},
1190 { "not", (ESExpFunc *) func_not, 1},
1191
1192 {"=", (ESExpFunc *) func_eq, 0},
1193 {">", (ESExpFunc *) func_gt, 0},
1194 {"<", (ESExpFunc *) func_lt, 0},
1195
1196 { "match-all", message_func_match_all, 0 },
1197 { "body-contains", message_func_body_contains, 0 },
1198 { "header-contains", message_func_header_contains, 0 },
1199 { "header-matches", message_func_header_matches, 0 },
1200 { "header-starts-with", message_func_header_starts_with, 0 },
1201 { "header-ends-with", message_func_header_ends_with, 0 },
1202 { "header-exists", message_func_header_exists, 0 },
1203 { "system-flag", message_func_system_flag, 0 },
1204 { "get-sent-date", message_func_sent_date, 0 },
1205 { "get-received-date", message_func_received_date, 0 },
1206 { "get-current-date", message_func_current_date, 0 },
1207 { "get-relative-months", message_func_relative_months, 0 },
1208 { "get-size", message_func_get_size, 0 },
1209 };
1210
1211 static void
1212 e_ews_convert_sexp_to_restriction (EvalContext *ctx,
1213 const gchar *query,
1214 EEwsFolderType type)
1215 {
1216 ESExp *sexp;
1217 ESExpResult *r;
1218 gint i;
1219
1220 sexp = e_sexp_new ();
1221
1222 if (type == E_EWS_FOLDER_TYPE_CONTACTS) {
1223 for (i = 0; i < G_N_ELEMENTS (contact_symbols); i++) {
1224 if (contact_symbols[i].immediate)
1225 e_sexp_add_ifunction (
1226 sexp, 0, contact_symbols[i].name,
1227 (ESExpIFunc *) contact_symbols[i].func, ctx);
1228 else
1229 e_sexp_add_function (
1230 sexp, 0, contact_symbols[i].name,
1231 contact_symbols[i].func, ctx);
1232 }
1233
1234 } else if (type == E_EWS_FOLDER_TYPE_CALENDAR || type == E_EWS_FOLDER_TYPE_TASKS || type == E_EWS_FOLDER_TYPE_MEMOS) {
1235 for (i = 0; i < G_N_ELEMENTS (calendar_symbols); i++) {
1236 if (calendar_symbols[i].immediate)
1237 e_sexp_add_ifunction (
1238 sexp, 0, calendar_symbols[i].name,
1239 (ESExpIFunc *) calendar_symbols[i].func, ctx);
1240 else
1241 e_sexp_add_function (
1242 sexp, 0, calendar_symbols[i].name,
1243 calendar_symbols[i].func, ctx);
1244 }
1245 } else if (type == E_EWS_FOLDER_TYPE_MAILBOX) {
1246 for (i = 0; i < G_N_ELEMENTS (message_symbols); i++) {
1247 if (message_symbols[i].immediate)
1248 e_sexp_add_ifunction (
1249 sexp, 0, message_symbols[i].name,
1250 (ESExpIFunc *) message_symbols[i].func, ctx);
1251 else
1252 e_sexp_add_function (
1253 sexp, 0, message_symbols[i].name,
1254 message_symbols[i].func, ctx);
1255 }
1256
1257 }
1258
1259 e_sexp_input_text (sexp, query, strlen (query));
1260 e_sexp_parse (sexp);
1261
1262 r = e_sexp_eval (sexp);
1263 if (!r)
1264 return;
1265
1266 e_sexp_result_free (sexp, r);
1267 g_object_unref (sexp);
1268 }
1269
1270 static gboolean
1271 e_ews_check_is_query (const gchar *query,
1272 EEwsFolderType type)
1273 {
1274
1275 if (!query)
1276 return FALSE;
1277
1278 if (type == E_EWS_FOLDER_TYPE_CONTACTS) {
1279 if (!g_strcmp0 (query, "(contains \"x-evolution-any-field\" \"\")"))
1280 return FALSE;
1281 else
1282 return TRUE;
1283
1284 } else if (type == E_EWS_FOLDER_TYPE_CALENDAR || type == E_EWS_FOLDER_TYPE_TASKS || type == E_EWS_FOLDER_TYPE_MEMOS) {
1285 if (!g_strcmp0 (query, "(contains? \"summary\" \"\")"))
1286 return FALSE;
1287 else
1288 return TRUE;
1289 } else if (type == E_EWS_FOLDER_TYPE_MAILBOX) {
1290 return TRUE;
1291 } else
1292 return FALSE;
1293 }
1294
1295 gboolean
1296 e_ews_query_check_applicable (const gchar *query,
1297 EEwsFolderType type)
1298 {
1299 EvalContext ctx;
1300
1301 if (!e_ews_check_is_query (query, type))
1302 return FALSE;
1303
1304 ctx.req = NULL;
1305 ctx.any_applicable = FALSE;
1306
1307 e_ews_convert_sexp_to_restriction (&ctx, query, type);
1308
1309 return ctx.any_applicable;
1310 }
1311
1312 void
1313 e_ews_query_to_restriction (ESoapRequest *req,
1314 const gchar *query,
1315 EEwsFolderType type)
1316 {
1317 EvalContext ctx;
1318
1319 ctx.req = req;
1320 ctx.any_applicable = FALSE;
1321
1322 e_ews_convert_sexp_to_restriction (&ctx, query, type);
1323 }