
#include "include/smtp.h"
#include "include/smeg_pub.h"
#include "include/statistic_data.h"
#include "include/tcp_client.h"


/*************************************************
Function:        // alarm2email
Others:          // 
*************************************************/
extern char* base64_encode(const char *data);
static int recipientNum=1;
static char recipientAddr[16][128];
static char subjectContent[256];

int alarm2email(SMTP_MSG *smtp_msg, char *alarm_content)
{
	char tempcontents[8192];
	int addrLen = 0;
	int i = 0, index = 0;
	
	if(smtp_msg == NULL || alarm_content == NULL || strlen(alarm_content) > MAX_EMAIL_LENGTH)
		return PARSE_ERROR;

	
	for(index = 0;index < recipientNum; index++)
	{
		i = 0;
		
		addrLen = strlen(recipientAddr[index]);
		smtp_msg->recipient[smtp_msg->recipientNum][i++] = '<';		
		strncpy(smtp_msg->recipient[smtp_msg->recipientNum]+i, recipientAddr[index], addrLen);
		i += addrLen;
		smtp_msg->recipient[smtp_msg->recipientNum][i++] = '>';
		smtp_msg->recipient[smtp_msg->recipientNum][i++] = '\0';

		if(++smtp_msg->recipientNum > MAX_RECEIPTOR_PER_MSG)
			break;
	}

	if(smtp_msg->recipientNum == 0)
		return PARSE_ERROR;

	strcpy(tempcontents, "From: ");
	strcat(tempcontents, smtp_msg->sender);
	strcat(tempcontents, "\r\nTo: ");
	for(i = 0; i < smtp_msg->recipientNum; i++)
	{
		if(i != 0)
			strcat(tempcontents, ",");
		strcat(tempcontents, smtp_msg->recipient[i]);
	}

	{
		char *p;
		strcpy((char*)smtp_msg->subject, subjectContent);
		strcat(tempcontents, "\r\nSubject: =?UTF-8?B?");
		p = base64_encode(subjectContent);
		if(p == NULL)
			return PARSE_ERROR;
		strcat(tempcontents, p);
		strcat(tempcontents, "?=");
	}
	strcat(tempcontents, "\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=\"");
	strcat(tempcontents, "UTF-8\"\r\nContent-Transfer-Encoding: base64\r\n\r\n");					
	{
		char *p = base64_encode(alarm_content);
		if(p == NULL)
			return PARSE_ERROR;
		strcat(tempcontents, p);				
	}
	strcpy((char*)smtp_msg->contents, tempcontents);

	smeg_print("Recipient:");
	for(i = 0; i < smtp_msg->recipientNum; i++)
		smeg_print("%s,", smtp_msg->recipient[i]);
	smeg_print("\nSubject:%s\n", smtp_msg->subject);
	smeg_print("Contents:\n%s\n",smtp_msg->contents);
	
	return PARSE_SUCCESS;
}




/*************************************************
Function:        // smtp_send_alert
Others:          // 
*************************************************/
void smtp_send_alert(const char *source_addr, int to_num, const char to_addr_list[][128], char *subject, char *alarm_content )
{
	int i;
	SMTP_MSG	smtp_msg;

	if(to_num<1)
		return;

	memset(&smtp_msg, 0x00, sizeof(SMTP_MSG));
	
	recipientNum = to_num;
	for(i=0; i<to_num; i++)
		strcpy(recipientAddr[i], to_addr_list[i]);
	strcpy(subjectContent, subject);
	
	memset(&smtp_msg, 0, sizeof(SMTP_MSG));	

	strcpy(smtp_msg.sender, "<");
	strcat(smtp_msg.sender, source_addr);
	strcat(smtp_msg.sender, ">");
	
	if(alarm2email(&smtp_msg, alarm_content) == PARSE_ERROR)
		return ;  

	smtp_tx_port_assign(&smtp_msg);	
}


