📄 nospamdlg.cpp
字号:
// NoSpamDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NoSpam.h"
#include "NoSpamDlg.h"
#include "myimap.h"
#include "setrule.h"
#include "rulelist.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNoSpamDlg dialog
CNoSpamDlg::CNoSpamDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNoSpamDlg::IDD, pParent),
m_listMargin(0, 0, 0, 0)
{
//{{AFX_DATA_INIT(CNoSpamDlg)
m_bUseTimer = TRUE;
m_minutes = 15;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNoSpamDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNoSpamDlg)
DDX_Control(pDX, IDC_MAILLIST, m_mailList);
DDX_Check(pDX, IDC_USE_TIMER, m_bUseTimer);
DDX_Text(pDX, IDC_MINUTES, m_minutes);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNoSpamDlg, CDialog)
//{{AFX_MSG_MAP(CNoSpamDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CHECK, OnCheck)
ON_BN_CLICKED(IDC_ACCOUNT, OnAccount)
ON_WM_SIZE()
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickMaillist)
ON_BN_CLICKED(IDC_SETRULE, OnSetrule)
ON_BN_CLICKED(IDC_RULELIST, OnRulelist)
ON_WM_CREATE()
ON_COMMAND(ID_POPUP_OPEN, OnPopupOpen)
ON_BN_CLICKED(IDC_USE_TIMER, OnUseTimer)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_MINUTES, OnChangeMinutes)
ON_NOTIFY(NM_DBLCLK, IDC_MAILLIST, OnDblclkMaillist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNoSpamDlg message handlers
BOOL CNoSpamDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// caculate margin
CRect cliRect, listRect;
GetClientRect(cliRect);
m_mailList.GetWindowRect(listRect);
ScreenToClient(listRect);
m_listMargin = listRect - (LPCRECT)cliRect;
// init columns
m_mailList.InsertColumn(0, "From Name", LVCFMT_LEFT, 100);
m_mailList.InsertColumn(1, "From Address", LVCFMT_RIGHT, 180);
m_mailList.InsertColumn(2, "Subject", LVCFMT_LEFT, 220);
LoadRules();
m_minuteCounter = 0;
m_nTimer = SetTimer(1, 60 * 1000, 0); // 1 minute timer
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CNoSpamDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CNoSpamDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNoSpamDlg::OnCheck()
{
CWaitCursor waitCursor;
CMyImap myImap;
int idxItem = 0;
m_mailList.DeleteAllItems();
m_arrFromAddrs.RemoveAll();
m_arrFromNames.RemoveAll();
m_arrSubjects.RemoveAll();
BOOL bOK = FALSE;
if (myImap.Login(m_account.m_host, m_account.m_user, m_account.m_passwd))
{
if (myImap.SelectMailBox("inbox"))
{
int numOfMsgs = myImap.GetNumOfMsgs();
for (int i = 1; i <= numOfMsgs; ++i)
{
CString from, subject;
BOOL bDelete;
if (myImap.ReadMsgHeader(i, from, subject, bDelete))
{
if (!bDelete)
{
CString name, addr;
GetNameAddr(from, name, addr);
if (IsSpam(addr, name, subject)) {
myImap.DeleteMsg(i);
}
else
{
m_arrFromAddrs.Add(addr);
m_arrFromNames.Add(name);
m_arrSubjects.Add(subject);
m_mailList.InsertItem(idxItem, name);
//m_mailList.SetItemText(idxItem, 0, name);
m_mailList.SetItemText(idxItem, 1, addr);
m_mailList.SetItemText(idxItem, 2, subject);
m_mailList.SetItemData(idxItem, idxItem);
++idxItem;
}
}
}
}
SaveRules(); // save because activity date may be changed
bOK = TRUE;
}
myImap.Logout();
}
if (!bOK)
{
m_mailList.InsertItem(idxItem, "ERROR");
m_mailList.SetItemText(idxItem, 1, "Cannot read messages. ");
m_mailList.SetItemText(idxItem, 2, "Check your password and other account info.");
}
}
void CNoSpamDlg::OnAccount()
{
m_account.DoModal();
}
BOOL CNoSpamDlg::IsAddrChar(char ch)
{
switch (ch)
{
case '<':
case '>':
case '[':
case ']':
case '{':
case '}':
case '"':
return FALSE;
}
return TRUE;
}
void CNoSpamDlg::GetNameAddr(LPCTSTR cszNameAddr, CString &name, CString &addr)
{
CString org = cszNameAddr;
org.TrimRight();
org.TrimLeft();
name.Empty();
addr.Empty();
int idx = org.Find('@');
if (idx >= 0) // find an email address
{
// the first part of the email address
for (int i = idx-1; i >= 0; --i)
{
if (org[i] == ' ' || org[i] == '\t' || !IsAddrChar(org[i]))
{
if (i > 0) { // find a name leading email address
name = org.Left(i); // not including the delimiter
}
break;
}
}
// the second part of the email address
for (int j = idx+1; j < org.GetLength(); ++j)
{
if (org[j] == ' ' || org[j] == '\t' || !IsAddrChar(org[j])) {
break;
}
}
addr = org.Mid(i+1, j-i-1);
}
else {
name = org; // it is just a name
}
for (idx = 0; idx < name.GetLength(); ++idx)
{
if (name[idx] == '"') {
name.SetAt(idx, ' ');
}
}
name.TrimRight();
name.TrimLeft();
}
void CNoSpamDlg::OnSize(UINT nType, int cx, int cy)
{
if (nType == SIZE_MINIMIZED)
{
ShowWindow(SW_HIDE);
m_trayIcon.ShowIcon();
}
else
{
CDialog::OnSize(nType, cx, cy);
if (m_listMargin.top != 0)
{
CRect cliRect, listRect;
GetClientRect(cliRect);
listRect.top = cliRect.top + m_listMargin.top;
listRect.bottom = cliRect.bottom + m_listMargin.bottom;
listRect.left = cliRect.left + m_listMargin.left;
listRect.right = cliRect.right + m_listMargin.right;
m_mailList.MoveWindow(listRect);
}
}
}
static int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
return ((CNoSpamDlg*)AfxGetMainWnd())->Sort(lParam1, lParam2, lParamSort);
}
void CNoSpamDlg::OnItemclickMaillist(NMHDR* pNMHDR, LRESULT* pResult)
{
//HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
m_mailList.SortItems(SortFunc, pLV->iItem);
*pResult = 0;
}
int CNoSpamDlg::Sort(int idx1, int idx2, int idxCol)
{
int nRetVal = 0;
switch(idxCol)
{
case 0: // name
nRetVal = m_arrFromNames[idx1].CompareNoCase(m_arrFromNames[idx2]);
break;
case 1: // addr
nRetVal = SortAddr(m_arrFromAddrs[idx1], m_arrFromAddrs[idx2]);
break;
case 2: // subject
nRetVal = m_arrSubjects[idx1].CompareNoCase(m_arrSubjects[idx2]);
break;
}
return nRetVal;
}
int CNoSpamDlg::SortRule(int idx1, int idx2, int idxCol)
{
if (idxCol == 2) {
return m_ruleDate[idx1] - m_ruleDate[idx2];
}
if (m_ruleTypes[idx1] != m_ruleTypes[idx2]) {
return m_ruleTypes[idx1] - m_ruleTypes[idx2];
}
int nRetVal = 0;
switch (m_ruleTypes[idx1])
{
case 0: // addr
nRetVal = SortAddr(m_rulePatterns[idx1], m_rulePatterns[idx2]);
break;
case 1: // name
nRetVal = m_rulePatterns[idx1].CompareNoCase(m_rulePatterns[idx2]);
break;
case 2: // subject
nRetVal = m_rulePatterns[idx1].CompareNoCase(m_rulePatterns[idx2]);
break;
}
return nRetVal;
}
int CNoSpamDlg::SortAddr(LPCTSTR addr1, LPCTSTR addr2)
{
CStringArray domains1, domains2;
GetDomains(addr1, domains1);
GetDomains(addr2, domains2);
for (int i = 0;; ++i)
{
if (domains1.GetSize() <= i) {
return -1;
}
if (domains2.GetSize() <= i) {
return -1;
}
int result = domains1[i].CompareNoCase(domains2[i]);
if (result != 0) {
return result;
}
}
return 0;
}
void CNoSpamDlg::GetDomains(LPCTSTR addr, CStringArray &domains)
{
domains.RemoveAll();
CString org = addr;
for (int i = org.GetLength()-1; i >= 0; --i)
{
switch (org[i])
{
case '.':
domains.Add(org.Right(org.GetLength() - i - 1));
org = org.Left(i);
break;
case '@':
domains.Add(org.Right(org.GetLength() - i - 1));
domains.Add(CString(" ") + org.Left(i));
return;
}
}
}
void CNoSpamDlg::OnSetrule()
{
LPCTSTR email = NULL;
LPCTSTR name = NULL;
LPCTSTR subject = NULL;
int idxSel = m_mailList.GetSelectionMark();
if (idxSel >= 0)
{
int idxMsg = m_mailList.GetItemData(idxSel);
if (idxMsg >= 0)
{
email = m_arrFromAddrs[idxMsg];
name = m_arrFromNames[idxMsg];
subject = m_arrSubjects[idxMsg];
}
}
DlgAddRule(email, name, subject);
}
void CNoSpamDlg::SaveRules()
{
CString fname;
GetRuleFileName(fname);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -