1e70a9ed7ab9af5d741bb8d20937da58bce6a718
[ossec-hids.git] / src / os_maild / mail_list.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12  
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "headers/debug_op.h"
18 #include "maild.h"
19 #include "mail_list.h"
20
21 #include "error_messages/error_messages.h"
22
23 MailNode *n_node;
24 MailNode *lastnode;
25
26 int _memoryused = 0;
27 int _memorymaxsize = 0;
28
29
30 /* Create the Mail List */
31 void OS_CreateMailList(int maxsize)
32 {
33     n_node = NULL;
34
35     _memorymaxsize = maxsize;
36
37     _memoryused = 0;
38     
39     return;
40 }
41
42 /* check last mail */
43 MailNode *OS_CheckLastMail()
44 {
45     return(lastnode);
46 }
47
48 /* Get the last Mail -- or first node */
49 MailNode *OS_PopLastMail()
50 {
51
52     MailNode *oldlast;
53
54     oldlast = lastnode;
55
56     if(lastnode == NULL)
57     {
58         n_node = NULL;
59         return(NULL);
60     }
61     
62     _memoryused--;
63     
64     lastnode = lastnode->prev;
65
66     /* Remove the last */
67     return(oldlast);    
68 }
69
70
71 void FreeMailMsg(MailMsg *ml)
72 {
73     if(ml == NULL)
74         return;
75     
76     if(ml->subject)
77         free(ml->subject);
78     
79     if(ml->body)
80         free(ml->body);
81         
82     free(ml);            
83 }
84
85
86 /* Free mail node */
87 void FreeMail(MailNode *ml)
88 {
89     if(ml == NULL)
90         return;
91     if(ml->mail->subject)
92         free(ml->mail->subject);
93     
94     if(ml->mail->body)
95         free(ml->mail->body);
96
97     free(ml->mail);    
98     free(ml);
99 }
100
101
102 /* Add an email to the list -- always to the begining */
103 void OS_AddMailtoList(MailMsg *ml)
104 {
105     MailNode *tmp_node = n_node;
106         
107     if(tmp_node)
108     {
109         MailNode *new_node;
110         new_node = (MailNode *)calloc(1,sizeof(MailNode));
111         
112         if(new_node == NULL)
113         {
114             ErrorExit(MEM_ERROR,ARGV0);
115         }
116
117         /* Always adding to the beginning of the list 
118          * The new node will become the first node and
119          * new_node->next will be the previous first node
120          */
121         new_node->next = tmp_node;
122         new_node->prev = NULL;
123         tmp_node->prev = new_node;
124         
125         n_node = new_node;
126
127         /* Adding the event to the node */
128         new_node->mail = ml;
129
130         _memoryused++;
131         
132         /* Need to remove the last node */
133         if(_memoryused > _memorymaxsize)
134         {
135             MailNode *oldlast;
136
137             oldlast = lastnode;
138             lastnode = lastnode->prev;
139             
140             /* free last node */
141             FreeMail(oldlast);
142             
143             _memoryused--;
144         }
145     }
146     
147     else
148     {
149         /* Adding first node */
150         n_node = (MailNode *)calloc(1,sizeof(MailNode));
151         if(n_node == NULL)
152         {
153             ErrorExit(MEM_ERROR,ARGV0);
154         }
155
156         n_node->prev = NULL;
157         n_node->next = NULL;
158         n_node->mail = ml;
159         
160         lastnode = n_node; 
161     }
162
163     return;
164 }
165
166 /* EOF */