Xchat Logs backend



I grabbed an older version of Wade's patch for the xchat logs backend
and more for my own benefit than anything else ported them to use
resultsets. I'm aware that Wade probably has some changes, but I thought
I'd through this out there in case it's any use ...

There's a few issues, namely:
The IconPath is hard-coded, and dashboard doesn't actually tell you what
content matched, just the file [Sorry I don't know enough about the
Indexer to work out how to grab the matching content]

Backend attached ...

Cheers
Lee
-- 
Lee Willis                                lee leewillis co uk
//
// backend xchat logs
//
// Authors:
//		Wade Mealing <wmealing subverted net>
//		Kevin Godby <godbyk yahoo com>
// 

// TODO:
//	Recurse through all directories in the xchat log files.		DONE
// 	Check for logfile permissions.
//	Write me if there is something else you can think of.
//	Convert all Console.WriteLine to Trace
//	new files during dashboard existance wont get indexed... 	DONE 

using System;
using System.IO;
using System.Collections;
using System.Text;
using Dashboard.Index;
using System.Threading;


namespace Dashboard {

	class XchatLogs : BackendSimple
	{
		
		//  Holds the settings for the log files.
		private DirectoryInfo LogDir;

		// last update
		private ArrayList FileList = new ArrayList();


		public override bool Startup ()
		{

			this.Name = "XchatBackend";
			this.Category = "Chat";
			this.IconPath = "/usr/share/pixmaps/xchat.png";

			this.SubscribeToClues ("im_name", "textblock" );

			// get a list of the files in the directory.
			string home_dir = Environment.GetEnvironmentVariable ("HOME");
			string path = Path.Combine (home_dir, ".xchat2/xchatlogs/");
			LogDir = new DirectoryInfo(path);

			GetAllFiles(LogDir, FileList);
		
			IndexFiles( FileList );

			// start an indexing thread.
			Thread t1 = new Thread ( new ThreadStart ( IndexThread ) ) ;				
			t1.Start();	
	
			this.Initialized = true;
			return true;
		}

		protected override ArrayList ProcessClueSimple (Clue clue)
		{
			
			Console.WriteLine("processing clue in xchat");

			ArrayList clues;
			IndexManager mgr = new IndexManager ("xchatlogs");

			ArrayList results = new ArrayList();
			ArrayList matches = mgr.Retrieve (new StringReader( clue.Text ) , "text/plain", out clues);

			foreach (IndexMatch m in matches) 
			{
				Console.WriteLine ("[{0}] {1}", m.Relevance, m.Source);
			
				// pull it out of string	
				string tmp  = m.Source.ToString();

				// grab everything, excluding the (.....) at the end
				// file:///home/wmealing/.xchat2/xchatlogs/whatever.log
				string FileURI = tmp.Substring( 0, tmp.LastIndexOf("("));

				// The unix path to the local file <-- uh.. nasty hack.. easier way ?
				string UnixPath = FileURI.Substring (11, (FileURI.Length - 11));
		
				Console.WriteLine("Attempting to use " + UnixPath ) ;	

				FileInfo fi = new FileInfo(UnixPath);	
				DateTime dt = fi.CreationTime ;

				BackendResult result = new BackendResult();
				result.AddItem(new BackendResultItem("Log File", Path.GetFileName(UnixPath), "text", "plain", "exec: gedit " + FileURI));
				results.Add(result);

			}		
				
			if (results.Count > 0) {
				return results;
			} else {
				return null;
			}
		}

		public void IndexFiles ( ArrayList a ) {

			foreach (FileInfo file  in a)
			{

				string FileName = file.FullName;

				System.Console.WriteLine("Indexing file:" + FileName );

				TextReader text = new StreamReader ( FileName , Encoding.ASCII);
				string uri = "file://" + FileName;

				ArrayList extracted_clues;

				IndexManager mgr = new IndexManager ("xchatlogs");
				ArrayList matches = mgr.IndexAndRetrieve (text, uri, "text/plain", out extracted_clues);

			}

		}

		public void IndexThread ( ) {

			while(true)
			{
				Thread.Sleep(50000);
				ArrayList IndexThese = GetChangedFiles();
				IndexFiles( IndexThese );
			}

		}

		/// returns an array of strings of files in the passed directory and all below.
		public void GetAllFiles( DirectoryInfo dir, ArrayList a )
		{

			// get a list of files in the current dir, add them to the list
			FileInfo[] tmp = dir.GetFiles();

			foreach (FileInfo logfile in tmp )
			{
				a.Add ( logfile );

				// go on, remove this line, its pointless, but the code wont work without it, oohers magikal.
				System.DateTime holder = logfile.LastWriteTime;
				// System.Console.WriteLine("Adding file " + logfile.Name + " - " + logfile.LastWriteTime);

			}

			// go into each directory, and recursively call this function.
			DirectoryInfo[] di = dir.GetDirectories();

			foreach (DirectoryInfo WorkingDir in di)
			{
				GetAllFiles ( WorkingDir , a );
			}

		}
	

		public ArrayList GetChangedFiles ( ) {

			// all the files in this loop
			ArrayList ThisLoop = new ArrayList();
			GetAllFiles( LogDir, ThisLoop);

			// Array list of new files
			ArrayList ChangedFiles = new ArrayList();

			// the files that are changed

			// compare the two
			foreach ( FileInfo file in FileList )
			{

				foreach (FileInfo newfile in ThisLoop) 
				{

					if (file.FullName == newfile.FullName)
					{
						if (file.LastWriteTime != newfile.LastWriteTime)
						{
							ChangedFiles.Add(file);
						}
					}

				}

			}

			FileList  = ThisLoop;

			return ChangedFiles;
		}

	}

}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]