Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.67.by mice / tube / src / ctb_hole

.cpp
Скачиваний:
10
Добавлен:
17.04.2013
Размер:
10 Кб
Скачать
/*******************************************************************************
* file:         ctb_hole.cpp                                                    *
* version:      0.0.1                                                          *
* author:       d-evil [tmd] (mailto:d-evil.tmd@mail.ru)                       *
* description:  not available                                                  *
*******************************************************************************/

#include "ctb_hole.h"


////////////////////////////////////////////////////////////////////////////////
// ctb_hole public definitions
ctb_hole::ctb_hole(): CDialog(IDD_DLG_MAIN) {
	_sending = false;
	_recving = false;

	_icon = NULL;
}


ctb_hole::~ctb_hole() {
	// dummy
}


BOOL ctb_hole::OnInitDialog() {
	// getting controls
	_btn_send = (CButton *)GetDlgItem(IDC_BTN_SEND);
	_btn_recv = (CButton *)GetDlgItem(IDC_BTN_RECV);
	_btn_show = (CButton *)GetDlgItem(IDC_BTN_SHOW);
	_btn_exit = (CButton *)GetDlgItem(IDC_BTN_EXIT);

	_edit_smtp_host = (CEdit *)GetDlgItem(IDC_EDIT_SMTP_HOST);
	_edit_smtp_port = (CEdit *)GetDlgItem(IDC_EDIT_SMTP_PORT);
	_edit_smtp_from = (CEdit *)GetDlgItem(IDC_EDIT_SMTP_FROM);
	_edit_pop_host = (CEdit *)GetDlgItem(IDC_EDIT_POP_HOST);
	_edit_pop_port = (CEdit *)GetDlgItem(IDC_EDIT_POP_PORT);
	_edit_pop_from = (CEdit *)GetDlgItem(IDC_EDIT_POP_FROM);
	_edit_to = (CEdit *)GetDlgItem(IDC_EDIT_TO);
	_edit_from = (CEdit *)GetDlgItem(IDC_EDIT_FROM);
	_edit_copy = (CEdit *)GetDlgItem(IDC_EDIT_COPY);
	_edit_subj = (CEdit *)GetDlgItem(IDC_EDIT_SUBJ);
	_edit_text  = (CEdit *)GetDlgItem(IDC_EDIT_TEXT);
	_edit_login  = (CEdit *)GetDlgItem(IDC_EDIT_LOGIN);
	_edit_pass  = (CEdit *)GetDlgItem(IDC_EDIT_PASS);
	_tree = (CTreeCtrl *)GetDlgItem(IDC_TREE);

	// setting defaults
	SetIcon(_icon, TRUE);

	_edit_smtp_host->SetWindowText("smtp.mail.ru");
	cdm_addr_info ai;
	ai.set_to_local(25);
	_edit_smtp_port->SetWindowText(ai.port_str());
	_edit_smtp_from->SetWindowText(ai.host());

	_edit_pop_host->SetWindowText("pop3.mail.ru");
	ai.set_port(110);
	_edit_pop_port->SetWindowText(ai.port_str());

	_edit_from->SetWindowText("inba@mail.ru");
	_edit_to->SetWindowText("d-evil.tmd@mail.ru");
	_edit_copy->SetWindowText("not_putin@mail.ru");
	_edit_subj->SetWindowText("Subject 001");
	_edit_text->SetWindowText("Text 002");
	_edit_login->SetWindowText("d-evil.tmd@mail.ru");
	_edit_pass->SetWindowText("hz_kakoj");

	return TRUE;
}


void ctb_hole::OnClose() {
	EndDialog(0);
}


int ctb_hole::send_mail() {
	DWORD id;
	HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_send_proc_helper, this, 0, &id);
	if (NULL == h) return -1;

	return 0;
}


int ctb_hole::recv_mail() {
	DWORD id;
	HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_recv_proc_helper, this, 0, &id);
	if (NULL == h) return -1;

	return 0;
}


////////////////////////////////////////////////////////////////////////////////
// ctb_hole protected definitions
BEGIN_MESSAGE_MAP(ctb_hole, CDialog)
	ON_WM_CLOSE()
	ON_COMMAND(IDC_BTN_SEND, _on_btn_send)
	ON_COMMAND(IDC_BTN_RECV, _on_btn_recv)
	ON_COMMAND(IDC_BTN_SHOW, _on_btn_show)
	ON_COMMAND(IDC_BTN_EXIT, _on_btn_exit)
END_MESSAGE_MAP()


void ctb_hole::_on_btn_send() {
	if (0 != send_mail()) {
		show_error_msg("Can't create thread for sending", 0, this);
	}
}


void ctb_hole::_on_btn_recv() {
	if (0 != recv_mail()) {
		show_error_msg("Can't create thread for recving", 0, this);
	}
}


void ctb_hole::_on_btn_show() {
	HTREEITEM item = _tree->GetSelectedItem();
	_stb_message *msg = (_stb_message *)_tree->GetItemData(item);
	
	MessageBox(msg->text->c_str());
}


void ctb_hole::_on_btn_exit() {
	PostMessage(WM_CLOSE);
}


void ctb_hole::_lock_ctrls_serv(const bool lock) {
	if (lock) {
		_edit_smtp_host->EnableWindow(FALSE);
		_edit_smtp_port->EnableWindow(FALSE);
		_edit_smtp_from->EnableWindow(FALSE);
		_edit_pop_host->EnableWindow(FALSE);
		_edit_pop_port->EnableWindow(FALSE);
		_edit_pop_from->EnableWindow(FALSE);
	} else {
		_edit_smtp_host->EnableWindow(TRUE);
		_edit_smtp_port->EnableWindow(TRUE);
		_edit_smtp_from->EnableWindow(TRUE);
		_edit_pop_host->EnableWindow(TRUE);
		_edit_pop_port->EnableWindow(TRUE);
		_edit_pop_from->EnableWindow(TRUE);
	}
}


void ctb_hole::_lock_ctrls_send(const bool lock) {
	if (lock) {
		_edit_to->EnableWindow(FALSE);
		_edit_from->EnableWindow(FALSE);
		_edit_copy->EnableWindow(FALSE);
		_edit_subj->EnableWindow(FALSE);
		_edit_text->EnableWindow(FALSE);
		_btn_send->EnableWindow(FALSE);
	} else {
		_edit_to->EnableWindow(TRUE);
		_edit_from->EnableWindow(TRUE);
		_edit_copy->EnableWindow(TRUE);
		_edit_subj->EnableWindow(TRUE);
		_edit_text->EnableWindow(TRUE);
		_btn_send->EnableWindow(TRUE);
	}
}


void ctb_hole::_lock_ctrls_recv(const bool lock) {
	if (lock) {
		_edit_login->EnableWindow(FALSE);
		_edit_pass->EnableWindow(FALSE);
		_tree->EnableWindow(FALSE);
		_btn_recv->EnableWindow(FALSE);
		_btn_show->EnableWindow(FALSE);
	} else {
		_edit_login->EnableWindow(TRUE);
		_edit_pass->EnableWindow(TRUE);
		_tree->EnableWindow(TRUE);
		_btn_recv->EnableWindow(TRUE);
		_btn_show->EnableWindow(TRUE);
	}
}


void ctb_hole::_lock_all(const bool lock) {
	_lock_ctrls_serv(lock);
	_lock_ctrls_send(lock);
	_lock_ctrls_recv(lock);

	if (lock) {
		_btn_exit->EnableWindow(FALSE);
	} else {
		_btn_exit->EnableWindow(TRUE);
	}
}


unsigned short ctb_hole::_ushort_from_edit(CEdit *const edit) {
	CString str;
	edit->GetWindowText(str);

	unsigned short port = atoi(str);

	str.Format("%i", (int)port);
	edit->SetWindowText(str);

	return port;
}


int ctb_hole::_send_proc_helper(ctb_hole *const hole) {
	if (hole->_is_sending(false)) return -1;
	hole->_lock_all();

	int rev = hole->_send_proc();

	ReleaseMutex(hole->_send_mtx);
	hole->_sending = false;
	hole->_lock_all(false);

	return rev;
}


int ctb_hole::_send_proc() {
	ctb_sender sender;
	sender.open_log("smtp.log");

	CString host;
	_edit_smtp_host->GetWindowText(host);
	unsigned short port = _ushort_from_edit(_edit_smtp_port);
	CString open_as;
	_edit_smtp_from->GetWindowText(open_as);

	if (0 != sender.connect(host, port)) {
		show_error_msg("Can't connect to smtp mail server", 0, this);
		return -1;
	}

	if (0 != sender.open(open_as)) {
		show_error_msg("Can't open session with smtp mail server", 0, this);
		return -1;
	}

	CString mail_to;
	_edit_to->GetWindowText(mail_to);
	CString mail_from;
	_edit_from->GetWindowText(mail_from);
	CString mail_copy;
	_edit_copy->GetWindowText(mail_copy);
	CString mail_subj;
	_edit_subj->GetWindowText(mail_subj);
	CString mail_text;
	_edit_text->GetWindowText(mail_text);

	sender.set_from(mail_from);
	sender.set_to(mail_to);
	sender.set_copy(mail_copy);
	sender.set_subj(mail_subj);
	sender.set_text(mail_text);

	if (0 != sender.send_mail()) {
		show_error_msg("Can't send mail to smtp mail server", 0, this);
		return -1;
	}
	if (0 != sender.close()) {
		show_error_msg("Error while closing session with smtp mail server", 0, this);
		return -1;
	}

	return 0;
}


int ctb_hole::_recv_proc_helper(ctb_hole *const hole) {
	if (hole->_is_recving(false)) return -1;
	hole->_lock_all();

	int rev = hole->_recv_proc();

	ReleaseMutex(hole->_recv_mtx);
	hole->_recving = false;
	hole->_lock_all(false);

	return rev;
}


int ctb_hole::_recv_proc() {
	ctb_recver recver;
	recver.open_log("pop3.log");

	CString host;
	_edit_pop_host->GetWindowText(host);
	unsigned short port = _ushort_from_edit(_edit_pop_port);
	// CString open_as;
	// _edit_pop_from->GetWindowText(open_as);

	if (0 != recver.connect(host, port)) {
		show_error_msg("Can't connect to pop3 mail server", 0, this);
		return -1;
	}

	if (0 != recver.open()) {
		show_error_msg("Can't open session with smtp mail server", 0, this);
		return -1;
	}

	CString user;
	_edit_login->GetWindowText(user);
	CString pass;
	_edit_pass->GetWindowText(pass);

	recver.set_user(user);
	recver.set_pass(pass);
	if (0 != recver.auth()) {
		show_error_msg("Failed authorization to pop3 mail server.", 0, this);
		return -1;
	}

	// get stat (useless now)
	int total_num, total_sz;
	if (0 != recver.stat(&total_num, &total_sz)) {
		show_error_msg("Can't get stat from pop3 mail server.", 0, this);
		return -1;
	}

	// get list
	ctb_recver::ctb_list list;
	recver.list(&list);
	int list_sz = (int)list.size();
	for (int i = 0; list_sz > i; ++i) {
		std::string &li = list[i];

		// get mail info
		int m_num, m_sz;
		sscanf(li.c_str(), "%i%i", &m_num, &m_sz);

		// load and add mail to tree
		std::string *text = new std::string;
		if (0 != recver.retr2(m_num, *text)) {
			show_error_msg("Error recving one of the messages from pop3 mail server", 0, this);
			continue;
		}

		_stb_message *msg = new _stb_message;
		msg->text = text;
		CString mfc_str;
		mfc_str.Format("message #%i (%i octets) at %i", m_num, m_sz, clock());
		HTREEITEM titem = _tree->InsertItem(mfc_str);
		_tree->SetItemData(titem, DWORD_PTR(msg));

		// delete mail from server
		if (0 != recver.dele(m_num)) {
			show_error_msg("Error deleting one of the messages from pop3 mail server", 0, this);
		}

	}

	if (0 != recver.close()) {
		show_error_msg("Error while closing session with pop3 mail server", 0, this);
		return -1;
	}

	return 0;
}


bool ctb_hole::_is_sending(const bool release) {
	if (_sending) return true;

	if (WAIT_TIMEOUT == WaitForSingleObject(_send_mtx, 0)) return true;
	if (release) ReleaseMutex(_send_mtx);

	return false;
}


bool ctb_hole::_is_recving(const bool release) {
	if (_recving) return true;

	if (WAIT_TIMEOUT == WaitForSingleObject(_recv_mtx, 0)) return true;
	if (release) ReleaseMutex(_recv_mtx);

	return false;
}
Соседние файлы в папке src