SafeNet/.svn/pristine/1b/1b8c634b2ade1e5c1b91f8e781a...

138 lines
5.5 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Collections;
using SafeNetLib;
using MySql.Data.MySqlClient;
namespace MotoTRBO_SOC
{
public class EmailHandler
{
//emai stuff
private EmailServerSSL sslServer;
private static System.Threading.Timer tCheckMail;
private string MyConString;
private string gatewayID;
public EmailHandler(string p_dbConStr, string p_gatewayID)
{
MyConString = p_dbConStr;
gatewayID = p_gatewayID;
//emai server ini
if (MotoTRBO_GW.cfg.enableEmailService)
{
Utils.ConsWrite(DebugMSG_Type.Email, "Turning on EmailService on " + MotoTRBO_GW.cfg.user);
sslServer = new EmailServerSSL(MotoTRBO_GW.cfg.pop3Server, MotoTRBO_GW.cfg.pop3Port, MotoTRBO_GW.cfg.user, MotoTRBO_GW.cfg.psw);
}
else
Utils.ConsWrite(DebugMSG_Type.Email, "EmailService disabled!!!");
//start email timer... refresh time 1 min
if (MotoTRBO_GW.cfg.enableEmailService)
{
tCheckMail = new System.Threading.Timer(CheckMAil, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 1, 0));
}
}
#region email
private void CheckMAil(Object state)
{
// This method is executed by a thread pool thread
try
{
Utils.ConsWrite(DebugMSG_Type.Email, new string[] { "Checking mail at:" + DateTime.Now.ToString(), "For " + MotoTRBO_GW.cfg.user.ToString() + " on " + MotoTRBO_GW.cfg.smtpServer.ToString() });
ArrayList EmailList = sslServer.getEmails();
foreach (EmailtoSMS obj in EmailList)
{
Utils.ConsWrite(DebugMSG_Type.Email, new string[] {"-----START OK----",
"From: " + obj.from,
"ID: " + obj.id,
"Text: " + obj.text});
try
{
obj.text = obj.text.Replace("'", "`");
obj.text = obj.text.Replace("#", " ");
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.Email, "Error on converting email:" + ex.ToString());
}
if (obj.text.Length > 140 - 3 - obj.from.Length)
obj.text = obj.text.Remove(140 - 3 - obj.from.Length) + " [" + obj.from + "]";
else
obj.text = obj.text + " [" + obj.from + "]";
//SendSMS(obj.id, obj.text, -1); // don't wait for confirmation
InsertSMSinDB(obj.text, obj.id);
LOGS.LOG(MotoTRBO_GW.cfg.gw_id + obj.id + " Email sms:" + obj.text);
}
Utils.ConsWrite(DebugMSG_Type.Email, "Email check done!!!");
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "Error on CheckMAil:" + ex.ToString());
}
}
//sms-> email
public static void sendMail(string mailAdr, string mailBody, string radioID)
{
Utils.ConsWrite(DebugMSG_Type.Email, "Sending mail to " + mailAdr + " from radioId " + radioID.ToString());
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(MotoTRBO_GW.cfg.user);
message.To.Add(mailAdr);
message.Subject = "RadioID: " + radioID.ToString() + " Subject:" + (mailBody.Length < 25 ? mailBody : (mailBody.Remove(25) + "..."));
message.Body = "You received a message from radio id " + radioID.ToString() +
"\n------------------------------\n" +
mailBody +
"\n------------------------------\n ";
message.IsBodyHtml = true;
EmailServerSSL.sendEmail(MotoTRBO_GW.cfg.smtpServer, MotoTRBO_GW.cfg.smtpPort, MotoTRBO_GW.cfg.user, MotoTRBO_GW.cfg.psw, message, MotoTRBO_GW.cfg.sslState);
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "Exception in sendMail: "+ex.ToString());
//Console.WriteLine(e.ToString());
}
}
//insert SMS in DB for further processing
private bool InsertSMSinDB(string body, string id)
{
try
{
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "insert into SMS_OUT (message,sent,subscriber,time) "+
"values ('" + body +
"',0"+
",'" + gatewayID+id+
"','" + DateTime.Now.ToUniversalTime().ToString("yyyy:MM:dd HH:mm:ss") +"')";
connection.Open();
Reader = command.ExecuteReader();
connection.Close();
return true;
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.always, "Error inserting ARS in database");
Utils.ConsWrite(DebugMSG_Type.always, e.Message);
return false;
}
}
#endregion
}
}