Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/lib/purk/UrkLogQueryable.cs
blob: ee0611c28aea5718007a03ea67c843633399da5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;

using Beagle.Daemon;
using Beagle.Util;

namespace Beagle.Daemon.UrkLogQueryable {

    [QueryableFlavor (Name="UrkLog", Domain=QueryDomain.Local, RequireInotify=false)]
    public class UrkLogQueryable : LuceneFileQueryable {

        private static Logger log = Logger.Get ("UrkLogQueryable");

        private string config_dir, log_dir, icons_dir;

        private int polling_interval_in_seconds = 60;
        
        //private GaimBuddyListReader list = new GaimBuddyListReader ();

        public UrkLogQueryable () : base ("UrkLogIndex")
        {
            config_dir = Path.Combine (PathFinder.HomeDir, ".urk");
            log_dir = Path.Combine (config_dir, "logs");
            icons_dir = Path.Combine (config_dir, "icons");
        }

        /////////////////////////////////////////////////
                    
        private void StartWorker() 
        {    
            if (! Directory.Exists (log_dir)) {
                GLib.Timeout.Add (60000, new GLib.TimeoutHandler (CheckForExistence));
                return;
            }

            log.Info ("Starting urk log backend");

            Stopwatch stopwatch = new Stopwatch ();
            stopwatch.Start ();

            State = QueryableState.Crawling;
            Crawl ();
            State = QueryableState.Idle;

            if (!Inotify.Enabled) {
                Scheduler.Task task = Scheduler.TaskFromHook (new Scheduler.TaskHook (CrawlHook));
                task.Tag = "Crawling ~/.urk/logs to find new logfiles";
                task.Source = this;
                ThisScheduler.Add (task);
            }

            stopwatch.Stop ();

            log.Info ("urk log backend worker thread done in {0}", stopwatch); 
        }
        
        public override void Start () 
        {
            base.Start ();
            
            ExceptionHandlingThread.Start (new ThreadStart (StartWorker));
        }

        /////////////////////////////////////////////////

        private void CrawlHook (Scheduler.Task task)
        {
            Crawl ();
            task.Reschedule = true;
            task.TriggerTime = DateTime.Now.AddSeconds (polling_interval_in_seconds);
        }

        private void Crawl ()
        {
            Inotify.Subscribe (log_dir, OnInotifyNewProtocol, Inotify.EventType.Create);

            // Walk through protocol subdirs
            foreach (string proto_dir in DirectoryWalker.GetDirectories (log_dir))
                CrawlProtocolDirectory (proto_dir);
        }

        private void CrawlNetworkDirectory (string proto_dir)
        {
            Inotify.Subscribe (proto_dir, OnInotifyNewTarget, Inotify.EventType.Create);

            // Walk through accounts
            foreach (string account_dir in DirectoryWalker.GetDirectories (proto_dir))
                CrawlTargetDirectory (account_dir);
        }

        private void CrawlTargetDirectory (string account_dir)
        {
            Inotify.Subscribe (account_dir, OnInotifyNewRemote, Inotify.EventType.Create);

            // Walk through remote user conversations
            foreach (string remote_dir in DirectoryWalker.GetDirectories (account_dir))
                CrawlRemoteDirectory (remote_dir);
        }

        private void CrawlRemoteDirectory (string remote_dir)
        {
            Inotify.Subscribe (remote_dir, OnInotifyNewConversation, Inotify.EventType.CloseWrite);

            foreach (FileInfo file in DirectoryWalker.GetFileInfos (remote_dir))
                if (FileIsInteresting (file.Name))
                    IndexLog (file.FullName, Scheduler.Priority.Delayed);
        }

        /////////////////////////////////////////////////

        private bool CheckForExistence ()
        {
            if (!Directory.Exists (log_dir))
                return true;

            this.Start ();

            return false;
        }

        private bool FileIsInteresting (string filename)
        {
            // Filename must be fixed length, see below
            if (filename.Length < 21 || filename.Length > 22)
                return false;

            // Check match on regex: ^[0-9]{4}-[0-9]{2}-[0-9]{2}\\.[0-9]{6}\\.(txt|html)$
            // e.g. 2005-07-22.161521.txt
            // We'd use System.Text.RegularExpressions if they werent so much more expensive
            return Char.IsDigit (filename [0]) && Char.IsDigit (filename [1])
                && Char.IsDigit (filename [2]) && Char.IsDigit (filename [3])
                && filename [4] == '-'
                && Char.IsDigit (filename [5]) && Char.IsDigit (filename [6])
                && filename [7] == '-'
                && Char.IsDigit (filename [8]) && Char.IsDigit (filename [9])
                && filename [10] == '.'
                && Char.IsDigit (filename [11]) && Char.IsDigit (filename [12])
                && Char.IsDigit (filename [13]) && Char.IsDigit (filename [14])
                && Char.IsDigit (filename [15]) && Char.IsDigit (filename [16])
                && filename [17] == '.'
                &&  (    (filename [18] == 't' && filename [19] == 'x' && filename [20] == 't')
                    ||    (filename [18] == 'h' && filename [19] == 't' && filename [20] == 'm' && filename [21] == 'l')
                    );
        }

        /////////////////////////////////////////////////

        private void OnInotifyNewNetwork (Inotify.Watch watch,
                        string path, string subitem, string srcpath,
                        Inotify.EventType type)
        {
            if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) == 0)
                return;

            CrawlNetworkDirectory (Path.Combine (path, subitem));
        }

        private void OnInotifyNewTarget (Inotify.Watch watch,
                        string path, string subitem, string srcpath,
                        Inotify.EventType type)
        {
            if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) == 0)
                return;

            CrawlTargetDirectory (Path.Combine (path, subitem));
        }

        private void OnInotifyNewRemote (Inotify.Watch watch,
                        string path, string subitem, string srcpath,
                        Inotify.EventType type)
        {
            if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) == 0)
                return;

            CrawlRemoteDirectory (Path.Combine (path, subitem));
        }

        private void OnInotifyNewConversation (Inotify.Watch watch,
                        string path, string subitem, string srcpath,
                        Inotify.EventType type)
        {
            if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) != 0)
                return;

            if (FileIsInteresting (subitem))
                IndexLog (Path.Combine (path, subitem), Scheduler.Priority.Immediate);            
        }

        /////////////////////////////////////////////////
        
        private static Indexable IRCLogToIndexable (string filename)
        {
            Uri uri = UriFu.PathToFileUri (filename);
            Indexable indexable = new Indexable (uri);
            indexable.ContentUri = uri;
            indexable.Timestamp = File.GetLastWriteTimeUtc (filename);
            indexable.MimeType = GaimLog.MimeType; // XXX
            indexable.HitType = "IRCLog";
            indexable.CacheContent = false;

            return indexable;
        }

        private void IndexLog (string filename, Scheduler.Priority priority)
        {
            if (! File.Exists (filename))
                return;

            if (IsUpToDate (filename))
                return;

            Indexable indexable = IRCLogToIndexable (filename);
            Scheduler.Task task = NewAddTask (indexable);
            task.Priority = priority;
            task.SubPriority = 0;
            ThisScheduler.Add (task);
        }

        override protected double RelevancyMultiplier (Hit hit)
        {
            return HalfLifeMultiplierFromProperty (hit, 0.25,
                                   "fixme:endtime", "fixme:starttime");
        }

        override protected bool HitFilter (Hit hit) 
        {
            /*ImBuddy buddy = list.Search (hit ["fixme:speakingto"]);
            
            if (buddy != null) {
                if (buddy.Alias != "")
                    hit ["fixme:speakingto_alias"] = buddy.Alias;
                
                //if (buddy.BuddyIconLocation != "")
                //  hit ["fixme:speakingto_icon"] = Path.Combine (icons_dir, buddy.BuddyIconLocation);
            }*/
            
            return true;
        }

    }
}