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