actions/action_alert_file.c

Go to the documentation of this file.
00001 #include "action_alert_file.h"
00002 #include <stdio.h>
00003 #include "../engine/message.h"
00004 #include <stdlib.h>
00005 #include <string.h>
00006 
00007 //#define DEBUG
00008 
00009 extern GlobalVars       Globals;
00010 
00011 FILE*   fp;
00012 
00016 void* AlertFileParseArgs(char* Args)
00017 {
00018 #ifndef KEEP_LOGFILE_OPEN
00019         FILE*           fp;
00020 #endif
00021         LogFileRec*     data;
00022         char            FileName[1024];
00023 
00024         DEBUGPATH;
00025 
00026         snprintf(FileName,1024,"%s%s",Globals.LogDir, Args);
00027         fp = fopen(FileName, "a");
00028         if (!fp) {
00029                 PRINTERROR1("Couldn't open file \"%s\" for appending\n", FileName);
00030                 return NULL;
00031         }
00032         fclose(fp);
00033 
00034         data = (LogFileRec*)calloc(sizeof(LogFileRec), 1);
00035         snprintf(data->fname, 1024, "%s", FileName);
00036 
00037         return data;
00038 }
00039 
00040 
00046 int AlertFileMessage(char* Message, void* Data)
00047 {
00048         LogFileRec*     data;
00049         
00050         DEBUGPATH;
00051 
00052         if (!Data) {
00053                 PRINTERROR("I must have a filename to write to!\n");
00054                 return FALSE;
00055         }
00056         
00057         data = (LogFileRec*)Data;
00058 
00059         fp = fopen(data->fname, "a");
00060         if (!LogFile(data)) {
00061                 return FALSE;
00062         }
00063 
00064         fwrite(Message, strlen(Message), 1, data->fp);
00065         fwrite("\n", 1, 1, data->fp);
00066         
00067         CloseLogFile(data);
00068         
00069         return TRUE;
00070 }
00071 
00075 int AlertFileAction(int RuleNum, int PacketSlot, void* Data)
00076 {
00077         char            Buff[1024];
00078         FILE*           fp;
00079         LogFileRec*     data;
00080         PacketRec*      p;
00081         
00082         DEBUGPATH;
00083 
00084         if (!Data) {
00085                 PRINTERROR("AlertFileAction: Must have a filename to write to!\n");
00086                 return FALSE;
00087         }
00088         
00089         p = &Globals.Packets[PacketSlot];
00090         data = (LogFileRec*)Data;
00091 
00092         fp = fopen(data->fname, "a");
00093         if (!fp) {
00094                 PRINTERROR1("AlertFileAction: Couldn't open \"%s\" for writing\n",data->fname);
00095                 return FALSE;
00096         }
00097 
00098         if (!ApplyMessage(Globals.AlertHeader, PacketSlot, Buff, 1024)) {
00099                 PRINTERROR("AlertFileAction: Couldn't alert header to packet\n");
00100                 return FALSE;
00101         }
00102 
00103         fwrite(Buff, strlen(Buff), 1, fp);
00104         fwrite(" ", 1, 1, fp);
00105 
00106 
00107         if (!ApplyMessage(Globals.Rules[RuleNum].MessageFormat, PacketSlot, Buff, 1024)) {
00108                 PRINTERROR("AlertFileAction: Couldn't apply message to packet\n");
00109                 return FALSE;
00110         }
00111 
00112         fwrite(Buff, strlen(Buff), 1, fp);
00113         fwrite("\n", 1, 1, fp);
00114         
00115         fclose(fp);
00116         
00117         return TRUE;
00118 }
00119 
00123 int InitActionAlertFile()
00124 {
00125         int ActionID;
00126 
00127         DEBUGPATH;
00128 
00129         ActionID = CreateAction("alert file");
00130         if (ActionID == ACTION_NONE) {
00131                 PRINTERROR("InitActionAlertFile: Couldn't allocate action alert file\n");
00132                 return FALSE;
00133         }
00134         
00135         Globals.ActionItems[ActionID].ActionFunc = AlertFileAction;
00136         Globals.ActionItems[ActionID].MessageFunc = AlertFileMessage;
00137         Globals.ActionItems[ActionID].ParseArgs = AlertFileParseArgs;
00138 
00139         return TRUE;
00140 }

Generated on Sat Jul 7 23:33:09 2007 for HLBR by  doxygen 1.5.2