-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacidmail.cpp
174 lines (154 loc) · 4.46 KB
/
acidmail.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "acidmail.h"
DWORD WINAPI StubCheckMail(LPVOID pData);
extern HWND hwndMain;
Acidmail::Acidmail():
CheckThread(0),
iNumMsgsOld(0),
bError(false),
bAbort(false)
{
// Get .rc vars
iTimer = GetRCInt("AcidMailTimer", 5);
iNumSubjects = GetRCInt("AcidMailSubjects", 0);
GetRCLine("AcidMailMailCmd", sMailCmd, MAX_LINE_LENGTH, "!none");
GetRCLine("AcidMailNewMailCmd", sNewMailCmd, MAX_LINE_LENGTH, "!none");
GetRCLine("AcidMailNoMailCmd", sNoMailCmd, MAX_LINE_LENGTH, "!none");
GetRCLine("AcidMailZeroMailCmd", sZeroMailCmd, MAX_LINE_LENGTH, "!none");;
GetRCLine("AcidMailFoundMailCmd", sFoundMailCmd, MAX_LINE_LENGTH, "!none");;
GetRCLine("AcidMailCheckMailCmd", sCheckMailCmd, MAX_LINE_LENGTH, "!none");
GetRCLine("AcidMailDoneCheckCmd", sDoneCheckCmd, MAX_LINE_LENGTH, "!none");
GetRCLine("AcidMailErrorCmd", sErrorCmd, MAX_LINE_LENGTH, "!none");
// Set evars
LSSetVariable("AcidMailNumMail", "0");
// Get *AcidMailbox lines, parse them and make new mailboxes
if (GetMailboxes())
{
// check mail for the first time if timer != 0
if (iTimer != 0)
CheckMail();
}
}
Acidmail::~Acidmail()
{
// Kill timer
KillTimer(hwndMain, 0);
// Kill thread
if (CheckThread)
{
bAbort = true;
WaitForSingleObject(CheckThread, INFINITE);
CloseHandle(CheckThread);
}
// Delete Mailboxes
for (mailboxiter = mailboxlist.begin(); mailboxiter != mailboxlist.end(); mailboxiter++ )
delete *mailboxiter;
}
int Acidmail::GetMailboxes()
{
FILE* f = LCOpen(NULL);
if(f != NULL)
{
char lineBuffer[MAX_LINE_LENGTH] = {0};
int count = 0;
for(;LCReadNextConfig(f, "*AcidMailbox", lineBuffer, MAX_LINE_LENGTH); count++)
{
char temp[16], name[64], rest[MAX_LINE_LENGTH-80] = {0};
char *buffers[] = {temp, name ,rest};
if(LCTokenize(lineBuffer, buffers, 3, rest) == 2)
{
Mailbox *newmailbox = new Mailbox(name, iNumSubjects);
if (newmailbox)
{
mailboxlist.insert(mailboxlist.end(), newmailbox);
}
}
}
LCClose(f);
return count;
}
return 0;
}
int Acidmail::CheckMail()
{
DWORD dwDummy = 0;
if (GetExitCodeThread(CheckThread, &dwDummy) != STILL_ACTIVE)
{
if (CheckThread)
{
WaitForSingleObject(CheckThread, INFINITE);
CloseHandle(CheckThread);
}
CheckThread = CreateThread(NULL, 0, ::StubCheckMail,
(LPVOID)this, 0, &dwDummy);
}
return (CheckThread != NULL);
}
int Acidmail::CheckMailThread()
{
KillTimer(hwndMain, 0); // Kill the timer to prevent the multiple threads from running
char snum[8];
iNumMsgs = 0;
if (!mailboxlist.empty())
{
LSExecute(NULL, sCheckMailCmd, 0); //Fire CheckMailCmd before checking
for (mailboxiter = mailboxlist.begin(); mailboxiter != mailboxlist.end(); mailboxiter++ )
{
if (bAbort) // Don't check mail if the main thread wants this thread to close
return 0;
Mail mail;
mail = (*mailboxiter)->CheckMail();
iNumMsgs += mail.iNumMail;
if (!bError)
bError = mail.bError;
}
sprintf(snum, "%d", iNumMsgs);
LSSetVariable("AcidMailNumMail", snum);
if (iNumMsgsOld != iNumMsgs) // Fire some other commands after checking
{
if (iNumMsgsOld == 0)
LSExecute(NULL, sMailCmd, 0);
if (iNumMsgs == 0)
LSExecute(NULL, sNoMailCmd, 0);
if (iNumMsgs != 0)
LSExecute(NULL, sNewMailCmd, 0);
iNumMsgsOld = iNumMsgs;
}
if (iNumMsgs == 0)
LSExecute(NULL, sZeroMailCmd, 0);
else
LSExecute(NULL, sFoundMailCmd, 0);
LSExecute(NULL, sDoneCheckCmd, 0);
if (bError)
LSExecute(NULL, sErrorCmd, 0);
bError = false;
}
SetTimer(hwndMain, 0, iTimer*60000, NULL); //(Re)Set timer when thread is done. Sends WM_TIMER message after time
return 0;
}
// Function called by thread
DWORD WINAPI StubCheckMail(LPVOID pData)
{
Acidmail* pMail = reinterpret_cast<Acidmail*>(pData);
if (pMail)
{
return pMail->CheckMailThread() ? 1 : 0;
}
return 0;
}
void Acidmail::ClearMail()
{
iNumMsgs = 0; iNumMsgsOld = 0;
for (mailboxiter = mailboxlist.begin(); mailboxiter != mailboxlist.end(); mailboxiter++ )
{
(*mailboxiter)->ClearMail();
}
LSSetVariable("AcidMailNumMail", "0");
LSExecute(NULL, sNoMailCmd, 0);
}
void Acidmail::SetPass(char* server, char* user, char* pass)
{
char temp[256];
sprintf(temp, "%s/%s", server, user);
Mailbox::Encrypt(pass);
WriteProfileString("AcidMail", temp, pass);
}