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