/* -*-C-*-
*******************************************************************************
*
* File:         DisplayFilter.m
* RCS:          $Header: /usr/local/lib/cvs/EnhanceMail/DisplayFilter.m,v 1.1.1.5 1996/06/25 13:44:07 cedman Exp $
* Description:  
* Author:       Carl Edman
* Created:      Fri Oct 13 11:48:05 1995
* Modified:     Tue Jun 25 09:33:13 1996 (Carl Edman) cedman@capitalist.princeton.edu
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
* (C) Copyright 1995, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import "EnhanceMail.h"
#import "DisplayFilter.h"
#import "Preferences.h"
#import "PGP.h"
#import "XImageURL.h"

@implementation EnhanceDisplayFilter
+ finishLoading:(struct mach_header *)header
   {
   [NXApp addDisplayFilter:[[self alloc] init]];
   return self;
   }

- (void)doSmilies:(Text *)txt
   {
   int start,end,ostart,oend;
   const char *c,*d;
   char *smilyreg,*smilyname;
   Cell *smilycell;

   for(c=EnhanceSmilies;*c;)
      {
      for(d=c;(*d!='\0') && (*d!='\n');d++);
      smilyreg=strncpy(alloca(d-c+1),c,d-c);
      smilyreg[d-c]='\0';
      if (!*d) break;
      c=d+1;
      for(d=c;(*d!='\0') && (*d!='\n');d++);
      smilyname=strncpy(alloca(d-c+1),c,d-c);
      smilyname[d-c]='\0';
      if (*d) d++;
      c=d;

      start=end=0;
      smilycell=nil;
      [txt setSel:start:end];
      while(1)
         {
         ostart=start;
         oend=end;
         if ([txt findRegExpr:smilyreg ignoreCase:NO loc1:&start loc2:&end]) break;
         if (ostart>=end) break;
         [txt setSel:start:end];
         if (smilycell==nil)
            {
            NXImage *smilyimage=[[NXImage alloc] initNameInPath:smilyname];
            if (smilyimage==nil) continue;
            [smilyimage setName:smilyname];
            smilycell=[[Cell alloc] initIconCell:smilyname];
            }
         if (smilycell==nil)
            break;
         [txt replaceSelWithCell:smilycell];
         }
      }
   }

- (void)willDisplayText:(Text *)txt
   {
   if (EnhanceExpandSmilies) [self doSmilies:txt];
   }

- (void)willDisplayMessage:(MailMessage *)mes
   {
   if (EnhanceUsePGP) [[EnhancePGP new] decodePGP:mes];
   }
@end
