Tokenize 001234 as 1234. Include a testing function in NoiseFilter to figure out...
[beagle.git] / beagled / ThunderbirdQueryable / Mail.cs
blob831eb0b2cbed817d1ef1c84b9c6f7cf05a312ae7
1 //
2 // Mail.cs: Adds POP3 and IMAP indexing support to the Thunderbird backend
3 //
4 // Copyright (C) 2006 Pierre Östlund
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Collections;
30 using System.Reflection;
31 using System.Text.RegularExpressions;
33 using Beagle.Util;
34 using Beagle.Daemon;
35 using TB = Beagle.Util.Thunderbird;
37 using GMime;
39 namespace Beagle.Daemon.ThunderbirdQueryable {
41 [ThunderbirdIndexableGenerator (TB.AccountType.Pop3, "POP3 Support", true)]
42 [ThunderbirdIndexableGenerator (TB.AccountType.Imap, "IMAP Support", true)]
43 public class MailIndexableGenerator : ThunderbirdIndexableGenerator {
44 private string mailbox_name = null;
46 public MailIndexableGenerator (ThunderbirdIndexer indexer, TB.Account account, string mork_file)
47 : base (indexer, account, mork_file)
51 public override bool HasNextIndexable ()
53 do {
54 while (DbEnumerator == null || !DbEnumerator.MoveNext ()) {
55 Done = true;
56 indexer.NotificationEvent -= OnNotification;
57 indexer.ChildComplete ();
58 return false;
60 } while (IsUpToDate ((DbEnumerator.Current as TB.Mail).Uri));
62 return true;
65 public override Indexable GetNextIndexable ()
67 TB.Mail mail = DbEnumerator.Current as TB.Mail;
69 // If status is different, than something happend when loading this mail and we dont'
70 // want to change it's status.
71 if (mail.GetObject ("FullIndex") == null)
72 mail.SetObject ("FullIndex", (object) FullIndex);
74 return MailToIndexable (mail);
77 public override void LoadDatabase ()
79 try {
80 db = new TB.Database (account, DbFile);
81 db.Load ();
83 Hashtable tbl = db.Db.Compile ("1", "ns:msg:db:row:scope:dbfolderinfo:all");
84 mailbox_name = (string) tbl ["mailboxName"];
85 } catch (Exception e) {
86 Logger.Log.Warn (e, "Failed to load {0}:", DbFile);
87 return;
90 if (db.Count <= 0) {
91 Logger.Log.Debug ("Empty file {0}; skipping", DbFile);
92 return;
95 FullIndex = (Thunderbird.IsFullyIndexable (DbFile) ? true : false);
96 Logger.Log.Info ("Indexing {0} containing {1} mails ({2})", RelativePath, db.Count, (FullIndex ? "Full" : "Partial"));
99 private Indexable MailToIndexable (TB.Mail mail)
101 Indexable indexable;
102 GMime.Message message = mail.Message;
103 FullIndex = mail.GetBool ("FullIndex"); // Make sure this is up to date
104 string mailbox = (MailboxName != null ? MailboxName : (string) mail.GetString ("mailbox"));
106 indexable = NewIndexable (mail.Uri, message.Date.ToUniversalTime (), "MailMessage");
107 indexable.MimeType = "message/rfc822";
108 indexable.CacheContent = true;
109 indexable.AddProperty (Property.NewKeyword ("fixme:folder", mailbox));
110 indexable.SetBinaryStream (message.Stream);
112 if (mail.GetBool ("FullIndex"))
113 indexable.ContentUri = UriFu.PathToFileUri (Thunderbird.GetFullyIndexableFile (DbFile));
115 message.Dispose ();
117 return indexable;
120 private string MailboxName {
121 get {
122 if (mailbox_name == null)
123 return null;
125 int dot = mailbox_name.LastIndexOf (".");
126 return (dot > 0 ? mailbox_name.Substring (dot+1) : mailbox_name);