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