* speed up modest_account_mgr_has_accounts by caching the status
[modest] / src / modest-search.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE
32 #endif
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <string.h>
39
40 #include <tny-shared.h>
41 #include <tny-folder.h>
42 #include <tny-folder-store.h>
43 #include <tny-list.h>
44 #include <tny-iterator.h>
45 #include <tny-simple-list.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
48
49 #include <libmodest-dbus-client/libmodest-dbus-client.h>
50
51 #include "modest-text-utils.h"
52 #include "modest-account-mgr.h"
53 #include "modest-tny-account-store.h"
54 #include "modest-tny-account.h"
55 #include "modest-tny-folder.h"
56 #include "modest-search.h"
57 #include "modest-runtime.h"
58 #include "modest-platform.h"
59
60 static gchar *
61 g_strdup_or_null (const gchar *str)
62 {
63         gchar *string = NULL;
64
65         if  (str != NULL) {
66                 string = g_strdup (str);
67         }
68
69         return string;
70 }
71
72 static GList*
73 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
74 {
75         ModestSearchHit *hit;
76         TnyHeaderFlags   flags;
77         char            *furl;
78         char            *msg_url;
79         const char      *uid;
80         const char      *subject;
81         const char      *sender;
82
83         hit = g_slice_new0 (ModestSearchHit);
84
85         furl = tny_folder_get_url_string (folder);
86         printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
87         if (!furl) {
88                 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
89         }
90         
91         /* Make sure that we use the short UID instead of the long UID,
92          * and/or find out what UID form is used when finding, in camel_data_cache_get().
93          * so we can find what we get. Philip is working on this.
94          */
95         uid = tny_header_get_uid (header);
96         if (!furl) {
97                 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
98         }
99         
100         msg_url = g_strdup_printf ("%s/%s", furl, uid);
101         g_free (furl);
102         
103         subject = tny_header_get_subject (header);
104         sender = tny_header_get_from (header);
105         
106         flags = tny_header_get_flags (header);
107
108         hit->msgid = msg_url;
109         hit->subject = g_strdup_or_null (subject);
110         hit->sender = g_strdup_or_null (sender);
111         hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
112         hit->msize = tny_header_get_message_size (header);
113         hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
114         hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
115         hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
116         
117         return g_list_prepend (list, hit);
118 }
119
120 /** Call this until it returns FALSE or nread is set to 0.
121  * 
122  * @result: FALSE is something failed. */
123 static gboolean
124 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
125 {
126         gsize _nread = 0;
127         gssize res = 0;
128
129         while (_nread < count) {
130                 res = tny_stream_read (stream,
131                                        buffer + _nread, 
132                                        count - _nread);
133                 if (res == -1) { /* error */
134                         *nread = _nread;
135                         return FALSE;
136                 }
137
138                 _nread += res;
139                 
140                 if (res == 0) { /* no more bytes read. */
141                         *nread = _nread;
142                         return TRUE;
143                 }
144         }
145
146         *nread = _nread;
147         return TRUE;
148 }
149
150 #ifdef MODEST_HAVE_OGS
151 static gboolean
152 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
153 {
154         TnyStream *stream = NULL;
155         char       buffer[4096];
156         const gsize len = sizeof (buffer);
157         gsize      nread = 0;
158         gboolean   is_text_html = FALSE;
159         gboolean   found = FALSE;
160         gboolean   res = FALSE;
161
162         gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
163         if (!is_text) {
164                 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
165                         "text/* MIME part. Content type is %s", 
166                 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
167                 
168             /* Retry with specific MIME types, because the wildcard seems to fail
169              * in tinymail.
170              * Actually I'm not sure anymore that it fails, so we could probalby 
171              * remove this later: murrayc */
172             is_text = (
173                 tny_mime_part_content_type_is (part, "text/plain") ||
174                 tny_mime_part_content_type_is (part, "text/html") );
175                 
176                 if (is_text) {
177                   g_debug ("%s: Retryting with text/plain or text/html succeeded", 
178                         __FUNCTION__);  
179                 }
180         }
181         
182         if (!is_text) {
183             return FALSE;
184         }
185         
186         is_text_html = tny_mime_part_content_type_is (part, "text/html");
187
188         stream = tny_mime_part_get_stream (part);
189
190         res = read_chunk (stream, buffer, len, &nread);
191         while (res && (nread > 0)) {
192                 /* search->text_searcher was instantiated in modest_search_folder(). */
193                 
194                 if (is_text_html) {
195
196                         found = ogs_text_searcher_search_html (search->text_searcher,
197                                                                buffer,
198                                                                nread,
199                                                                nread < len);
200                 } else {
201                         found = ogs_text_searcher_search_text (search->text_searcher,
202                                                                buffer,
203                                                                nread);
204                 }
205
206                 if (found) {
207                         break;
208                 }
209                 
210                 nread = 0;
211                 res = read_chunk (stream, buffer, len, &nread);
212         }
213
214         if (!found) {
215                 found = ogs_text_searcher_search_done (search->text_searcher);
216         }
217
218         ogs_text_searcher_reset (search->text_searcher);
219         
220         /* debug stuff:
221         if (!found) {
222                 buffer[len -1] = 0;
223                 printf ("DEBUG: %s: query %s was not found in message text: %s\n", 
224                         __FUNCTION__, search->query, buffer);   
225                 
226         } else {
227                 printf ("DEBUG: %s: found.\n", __FUNCTION__);   
228         }
229         */
230
231         return found;
232 }
233
234 #else
235
236 static gboolean
237 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
238 {
239         TnyStream *stream;
240         char       buffer[8193];
241         char      *chunk[2];
242         gssize     len;
243         gsize     nread;
244         gboolean   found;
245         gboolean   res;
246
247         if (! tny_mime_part_content_type_is (part, "text/*")) {
248                 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
249                 return FALSE;
250         }
251
252         found = FALSE;
253         len = (sizeof (buffer) - 1) / 2;
254
255         if (strlen (search->body) > len) {
256                 g_warning ("Search term bigger then chunk."
257                            "We might not find everything");     
258         }
259
260         stream = tny_mime_part_get_stream (part);
261
262         memset (buffer, 0, sizeof (buffer));
263         chunk[0] = buffer;
264         chunk[1] = buffer + len;
265
266         res = read_chunk (stream, chunk[0], len, &nread);
267
268         if (res == FALSE) {
269                 goto done;
270         }
271
272         found = !modest_text_utils_utf8_strcmp (search->body,
273                                                 buffer,
274                                                 TRUE);
275         if (found) {
276                 goto done;
277         }
278
279         /* This works like this:
280          * buffer: [ooooooooooo|xxxxxxxxxxxx|\0] 
281          *          ^chunk[0]  ^chunk[1]
282          * we have prefilled chunk[0] now we always read into chunk[1]
283          * and then move the content of chunk[1] to chunk[0].
284          * The idea is to prevent not finding search terms that are
285          * spread across 2 reads:        
286          * buffer: [ooooooooTES|Txxxxxxxxxxx|\0] 
287          * We should catch that because we always search the whole
288          * buffer not only the chunks.
289          *
290          * Of course that breaks for search terms > sizeof (chunk)
291          * but sizeof (chunk) should be big enough I guess (see
292          * the g_warning in this function)
293          * */   
294         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
295                 buffer[len + nread] = '\0';
296
297                 found = !modest_text_utils_utf8_strcmp (search->body,
298                                                         buffer,
299                                                         TRUE);
300
301                 if ((found)||(nread == 0)) {
302                         break;
303                 }
304
305                 /* also move the \0 */
306                 g_memmove (chunk[0], chunk[1], len + 1);
307         }
308
309 done:
310         g_object_unref (stream);
311         return found;
312 }
313 #endif /*MODEST_HAVE_OGS*/
314
315 static gboolean
316 search_string (const char      *what,
317                const char      *where,
318                ModestSearch    *search)
319 {
320         gboolean found;
321 #ifdef MODEST_HAVE_OGS
322         if (search->flags & MODEST_SEARCH_USE_OGS) {
323                 found = ogs_text_searcher_search_text (search->text_searcher,
324                                                        where,
325                                                        strlen (where));
326
327                 ogs_text_searcher_reset (search->text_searcher);
328         } else {
329 #endif
330                 if (what == NULL || where == NULL) {
331                         return FALSE;
332                 }
333
334                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
335 #ifdef MODEST_HAVE_OGS
336         }
337 #endif
338         return found;
339 }
340
341
342 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
343 {
344         gboolean found = FALSE;
345         #ifdef MODEST_HAVE_OGS
346         found = search_mime_part_ogs (part, search);
347         #else
348         found = search_mime_part_strcmp (part, search);
349         #endif
350
351         if (found) {    
352                 return found;           
353         }
354         
355         /* Check the child part too, recursively: */
356         TnyList *child_parts = tny_simple_list_new ();
357         tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
358
359         TnyIterator *piter = tny_list_create_iterator (child_parts);
360         while (!found && !tny_iterator_is_done (piter)) {
361                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
362                 if (pcur) {
363                         found = search_mime_part_and_child_parts (pcur, search);
364
365                         g_object_unref (pcur);
366                 }
367
368                 tny_iterator_next (piter);
369         }
370
371         g_object_unref (piter);
372         g_object_unref (child_parts);
373         
374         return found;
375 }
376
377 /**
378  * modest_search:
379  * @folder: a #TnyFolder instance
380  * @search: a #ModestSearch query
381  *
382  * This operation will search @folder for headers that match the query @search,
383  * if the folder itself matches the query.
384  * It will return a doubly linked list with URIs that point to the message.
385  **/
386 GList *
387 modest_search_folder (TnyFolder *folder, ModestSearch *search)
388 {
389         /* Check that we should be searching this folder. */
390         /* Note that we don't try to search sub-folders. 
391          * Maybe we should, but that should be specified. */
392         if (search->folder && strlen (search->folder)) {
393                 if (!strcmp (search->folder, "outbox")) {
394                         if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
395                                 return NULL;
396                         }
397                 } else if (strcmp (tny_folder_get_id (folder), search->folder) != 0) {
398                         return NULL;
399                 }
400         }
401         
402         GList *retval = NULL;
403         TnyIterator *iter = NULL;
404         TnyList *list = NULL;
405         
406 #ifdef MODEST_HAVE_OGS
407         if (search->flags & MODEST_SEARCH_USE_OGS) {
408         
409                 if (search->text_searcher == NULL && search->query != NULL) {
410                         OgsTextSearcher *text_searcher; 
411
412                         text_searcher = ogs_text_searcher_new (FALSE);
413                         ogs_text_searcher_parse_query (text_searcher, search->query);
414                         search->text_searcher = text_searcher;
415                 }
416         }
417 #endif
418
419         list = tny_simple_list_new ();
420         GError *error = NULL;
421         tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
422         if (error) {
423                 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n", 
424                 __FUNCTION__, error->message);
425                 g_error_free (error);
426                 error = NULL;   
427         }
428
429         iter = tny_list_create_iterator (list);
430
431         while (!tny_iterator_is_done (iter)) {
432                 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
433                 const time_t t = tny_header_get_date_sent (cur);
434                 gboolean found = FALSE;
435                 
436                 /* Ignore deleted (not yet expunged) emails: */
437                 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
438                         goto go_next;
439                         
440                 if (search->flags & MODEST_SEARCH_BEFORE)
441                         if (!(t <= search->end_date))
442                                 goto go_next;
443
444                 if (search->flags & MODEST_SEARCH_AFTER)
445                         if (!(t >= search->start_date))
446                                 goto go_next;
447
448                 if (search->flags & MODEST_SEARCH_SIZE)
449                         if (tny_header_get_message_size (cur) < search->minsize)
450                                 goto go_next;
451
452                 if (search->flags & MODEST_SEARCH_SUBJECT) {
453                         const char *str = tny_header_get_subject (cur);
454
455                         if ((found = search_string (search->subject, str, search))) {
456                             retval = add_hit (retval, cur, folder);
457                         }
458                 }
459                 
460                 if (!found && search->flags & MODEST_SEARCH_SENDER) {
461                         char *str = g_strdup (tny_header_get_from (cur));
462
463                         if ((found = search_string (search->from, (const gchar *) str, search))) {
464                                 retval = add_hit (retval, cur, folder);
465                         }
466                         g_free (str);
467                 }
468                 
469                 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
470                         const char *str = tny_header_get_to (cur);
471
472                         if ((found = search_string (search->recipient, str, search))) {
473                                 retval = add_hit (retval, cur, folder);
474                         }
475                 }
476         
477                 if (!found && search->flags & MODEST_SEARCH_BODY) {
478                         TnyHeaderFlags flags;
479                         GError      *err = NULL;
480                         TnyMsg      *msg = NULL;
481
482                         flags = tny_header_get_flags (cur);
483
484                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
485                                 goto go_next;
486                         }
487
488                         msg = tny_folder_get_msg (folder, cur, &err);
489
490                         if (err != NULL || msg == NULL) {
491                                 g_warning ("%s: Could not get message.\n", __FUNCTION__);
492                                 g_error_free (err);
493
494                                 if (msg) {
495                                         g_object_unref (msg);
496                                 }
497                         } else {        
498                         
499                                 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg), 
500                                                                           search);
501                                 if (found) {
502                                         retval = add_hit (retval, cur, folder);
503                                 }
504                         }
505                         
506                         if (msg)
507                                 g_object_unref (msg);
508                 }
509
510 go_next:
511                 g_object_unref (cur);
512                 tny_iterator_next (iter);
513         }
514
515         g_object_unref (iter);
516         g_object_unref (list);
517         return retval;
518 }
519
520 GList *
521 modest_search_account (TnyAccount *account, ModestSearch *search)
522 {
523         TnyFolderStore      *store;
524         TnyIterator         *iter;
525         TnyList             *folders;
526         GList               *hits;
527         GError              *error;
528
529         error = NULL;
530         hits = NULL;
531
532         store = TNY_FOLDER_STORE (account);
533
534         folders = tny_simple_list_new ();
535         tny_folder_store_get_folders (store, folders, NULL, &error);
536         
537         if (error != NULL) {
538                 g_object_unref (folders);
539                 return NULL;
540         }
541
542         iter = tny_list_create_iterator (folders);
543         while (!tny_iterator_is_done (iter)) {
544                 TnyFolder *folder = NULL;
545                 GList     *res = NULL;
546
547                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
548                 if (folder) {
549                         /* g_debug ("DEBUG: %s: searching folder %s.", 
550                                 __FUNCTION__, tny_folder_get_name (folder)); */
551                 
552                         res = modest_search_folder (folder, search);
553
554                         if (res != NULL) {
555                                 if (hits == NULL) {
556                                         hits = res;
557                                 } else {
558                                         hits = g_list_concat (hits, res);
559                                 }
560                         }
561
562                         g_object_unref (folder);
563                 }
564
565                 tny_iterator_next (iter);
566         }
567
568         g_object_unref (iter);
569         g_object_unref (folders);
570
571         /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
572         return hits;
573 }
574
575 GList *
576 modest_search_all_accounts (ModestSearch *search)
577 {
578         /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
579         ModestTnyAccountStore *astore;
580         TnyList               *accounts;
581         TnyIterator           *iter;
582         GList                 *hits;
583
584         hits = NULL;
585         astore = modest_runtime_get_account_store ();
586
587         accounts = tny_simple_list_new ();
588         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
589                                         accounts,
590                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
591
592         iter = tny_list_create_iterator (accounts);
593         while (!tny_iterator_is_done (iter)) {
594                 TnyAccount *account = NULL;
595                 GList      *res = NULL;
596
597                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
598                 if (account) {
599                         /* g_debug ("DEBUG: %s: Searching account %s",
600                          __FUNCTION__, tny_account_get_name (account)); */
601                          
602                         /* Give the account time to go online if necessary, 
603                          * for instance if this is immediately after startup,
604                          * after D-Bus activation: */
605                         modest_platform_check_and_wait_for_account_is_online (account);
606                         
607                         /* Search: */
608                         res = modest_search_account (account, search);
609                         
610                         if (res != NULL) {      
611                                 if (hits == NULL) {
612                                         hits = res;
613                                 } else {
614                                         hits = g_list_concat (hits, res);
615                                 }
616                         }
617                         
618                         g_object_unref (account);
619                 }
620
621                 tny_iterator_next (iter);
622         }
623
624         g_object_unref (accounts);
625         g_object_unref (iter);
626
627         /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */
628         return hits;
629 }
630
631