137 lines
5.5 KiB
Plaintext
137 lines
5.5 KiB
Plaintext
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
using System.Collections;
|
|
using MySql.Data.MySqlClient;
|
|
using SafeNetLib;
|
|
|
|
namespace Hytera_SOC
|
|
{
|
|
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 (Hytera_GW.cfg.enableEmailService)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "Turning on EmailService on " + Hytera_GW.cfg.user);
|
|
sslServer = new EmailServerSSL(Hytera_GW.cfg.pop3Server, Hytera_GW.cfg.pop3Port, Hytera_GW.cfg.user, Hytera_GW.cfg.psw);
|
|
}
|
|
else
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "EmailService disabled!!!");
|
|
|
|
//start email timer... refresh time 1 min
|
|
if (Hytera_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 " + Hytera_GW.cfg.user.ToString() + " on " + Hytera_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(Hytera_GW.cfg.gatewayID + 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(Hytera_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(Hytera_GW.cfg.smtpServer, Hytera_GW.cfg.smtpPort, Hytera_GW.cfg.user, Hytera_GW.cfg.psw, message, Hytera_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
|
|
}
|
|
}
|