//=-- AFDomainify.cpp - Prepend domain name onto URL of access ----------------=
//
//  PLUGIN DESCRIPTION:
//
//  This plugin is designed to prepend the domain name of an access to the URL
//  field of the access, causing it to be shown in reports like any other hit.
//  
//  This plugin can optionally use a Filter to only domainify certain accesses,
//  which would be useful, because this allows you to do something like this:
//
//  [ "Domainify", "/[^~]*" ]   # Only domainify non user hits...
//
//  Which would allow all hits to user directories to remain unmodified, because
//  in this case, they all get mapped to the same file being served anyways.
//
//  PLUGIN AUTHOR:
//    Chris Lattner, Author of Magicstats  (sabre@nondot.org)
//
//  PLUGIN HOMEPAGE:
//     http://www.magicstats.com/
//
//  PLUGIN VERSION / COMPATIBILITY:
//     Domainify v1.0 / MagicStats 2.0+
//
//=----------------------------------------------------------------------------=
//  This file is copyright (c) 1999-2000 Chris Lattner
//=----------------------------------------------------------------------------=

#include "AccessFilterPlugin.h"
#include "AccessFormatPlugin.h"
#include "FilterManager.h"
#include "VarTable.h"

class AFDomainify : public PluginTemplate<AccessFilterPlugin,AFDomainify> {
private:
  FilterHandle Handle;
 
public:
  inline AFDomainify(int &CE) { CE = 0; }

  inline static unsigned int GetCurrentVersion() {
    return 100;                       // Version 1.00
  } 

  inline static const char *GetPluginName() {
    return "Domainify";
  }

  // Initialize - Initialize plugin, based on the arguments received...
  //
  virtual int Initialize(const VTListExp *Params) {
    if (Params->GetNumElements() == 3) {       // Filter specified...
      VTExp *I = Params->GetListElement(1), 
            *E = Params->GetListElement(2);
      Handle = FilterManager::GetFilter(I->GetStringValue(), 
                                        E->GetStringValue());

    } else if (Params->GetNumElements() == 2) {
      VTExp *Exp = Params->GetListElement(1);

      if (Exp->GetExpType() == VTExp::ListTy) {
        Handle = FilterManager::GetFilter((VTListExp*)Exp);
      } else {
        Handle = FilterManager::GetFilter(Exp->GetStringValue(), "");
      }

    } else if (Params->GetNumElements() != 1) {
      cout << "Domainify AccessFilter only recognizes two parameters, a "
              "filter specification!\nIgnoring filter.\n";
      return 1;
    }

    return 0;
  }


  // FilterAccess - Process the access that is coming in and domainify if 
  // neccesary...
  //
  virtual int FilterAccess(AccessFormatPlugin &A) {
    if (Handle.Match(A))     // If the filter matches, prepend domain to URL...
      A.GetURL() = A.GetDomain() + A.GetURL();
    return 0;
  }
};

INIT_PLUGIN(AFDomainify);

