SafeDispatch/SafeMobileLIB_DLL/MessageDecoders/SMSdecoder.cs

399 lines
13 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
namespace SafeMobileLib.MessageDecoders
{
public class SMSdecoder
{
private int suid;
private byte[] data;
private DBsmsManager DBsms;
private DBTicketingManager DBTicketing;
private String seqID;
private Boolean withDB = false;
public String messBody = "";
public String emailAddr = "";
public int delete_ticket_id = -1;
public sqlResponse sqlResp;
public bool isEmail;
private bool hyt;
public SMSdecoder(int suid, byte[] data, DBsmsManager DBsms, DBTicketingManager DBTicketing, String seqID, bool _hyt, string msg)
{
withDB = true;
this.suid = suid;
this.data = data;
this.DBsms = DBsms;
this.DBTicketing = DBTicketing;
this.seqID = seqID;
hyt = _hyt;
//decode and add to DB in DecodePacket
if (!hyt)
{
header_T hret = DecodePacket(data);
}
else
{
DecodePacketHyt(msg);
}
}
public SMSdecoder(bool _hyt)
{
withDB = false;
hyt = _hyt;
}
// -------------------------------------------------------------------
// Aux functions
// -------------------------------------------------------------------
public struct header_T
{
public bool ext, ext2, ack, cntl;
public byte pdu_type;
public byte seq_no;
public byte encoding;
public byte header;
}
public static string Static_dec(Byte[] data)
{
header_T hret = new header_T();
string str_msg="";
int i, j, pdata;
pdata = (int)data[0];
pdata <<= 8;
pdata |= (int)data[1];
Console.WriteLine("Length =" + pdata + "(0x" + data[0].ToString("X") + "," + data[1].ToString("X") + ")");
Console.Write("Data: ");
for (i = 2; i < pdata + 2; i++)
Console.Write(" 0x" + data[i].ToString("X"));
Console.WriteLine();
// parse header
int header = data[2];
hret.header = data[2];
Console.WriteLine("Header: " + header.ToString("X"));
if ((header & 0x80) != 0)
hret.ext = true;
else hret.ext = false;
if ((header & 0x40) != 0)
hret.ack = true;
else hret.ack = false;
if ((header & 0x10) != 0)
hret.cntl = true;
else hret.cntl = false; // txt message
hret.pdu_type = (byte)(header & 0x0f);
// parse address
int addrsize = data[3];
i = 4;
Console.Write("Address: ");
for (j = 0; j < addrsize; j++)
{
Console.Write(data[i + j].ToString("X"));
}
i += j;
if (addrsize == 0)
Console.WriteLine("no len");
else Console.WriteLine();
// parse rest of headers
if (hret.ext)
{
byte h2 = data[i];
if ((h2 & 0x80) != 0)
hret.ext2 = true;
else hret.ext2 = false;
hret.seq_no = (byte)(h2 & 0x1F);
i++;
if (hret.ext2)
{ // parse third header
byte seqNr_MSB = (byte)(data[i] & 0x60);
hret.seq_no += seqNr_MSB;
hret.encoding = (byte)(data[i] & 0x0f);
i++;
}
}
int crc = 0;
if ((!hret.cntl) && (hret.pdu_type == 0))
{ // the rest is the txt message
Console.Write("SMS Message: ");
Char[] cs = new Char[100];
int k;
for (j = i, k = 0; j < pdata + 2; j++)
{
Console.Write(" 0x" + data[j].ToString("X"));
if (data[j] != 0)
{
cs[k++] = (Char)data[j];
crc |= (Char)data[j];
}
}
Console.WriteLine();
// save message in inbox ht
string s = new string(cs, 0, k);
s = s.Replace(System.Environment.NewLine, string.Empty);
str_msg = s;
}
return str_msg;
}
public header_T DecodePacket(Byte[] data)
{
header_T hret = new header_T();
int i, j, pdata;
pdata = (int)data[0];
pdata <<= 8;
pdata |= (int)data[1];
Console.WriteLine("Length =" + pdata + "(0x" + data[0].ToString("X") + "," + data[1].ToString("X") + ")");
Console.Write("Data: ");
for (i = 2; i < pdata + 2; i++)
Console.Write(" 0x" + data[i].ToString("X"));
Console.WriteLine();
if (!withDB)
{
messBody = "";
emailAddr = "";
}
// parse header
int header = data[2];
hret.header = data[2];
Console.WriteLine("Header: " + header.ToString("X"));
if ((header & 0x80) != 0)
hret.ext = true;
else hret.ext = false;
if ((header & 0x40) != 0)
hret.ack = true;
else hret.ack = false;
if ((header & 0x10) != 0)
hret.cntl = true;
else hret.cntl = false; // txt message
hret.pdu_type = (byte)(header & 0x0f);
// parse address
int addrsize = data[3];
i = 4;
Console.Write("Address: ");
for (j = 0; j < addrsize; j++)
{
Console.Write(data[i + j].ToString("X"));
}
i += j;
if (addrsize == 0)
Console.WriteLine("no len");
else Console.WriteLine();
// parse rest of headers
if (hret.ext)
{
byte h2 = data[i];
if ((h2 & 0x80) != 0)
hret.ext2 = true;
else hret.ext2 = false;
hret.seq_no = (byte)(h2 & 0x1F);
i++;
if (hret.ext2)
{ // parse third header
byte seqNr_MSB = (byte)(data[i] & 0x60);
hret.seq_no += seqNr_MSB;
hret.encoding = (byte)(data[i] & 0x0f);
i++;
}
}
int crc = 0;
if ((!hret.cntl) && (hret.pdu_type == 0))
{ // the rest is the txt message
Console.Write("SMS Message: ");
Char[] cs = new Char[100];
int k;
for (j = i, k = 0; j < pdata + 2; j++)
{
Console.Write(" 0x" + data[j].ToString("X"));
if (data[j] != 0)
{
cs[k++] = (Char)data[j];
crc |= (Char)data[j];
}
}
Console.WriteLine();
// save message in inbox ht
string s = new string(cs, 0, k);
string key = suid + " | " + DateTime.Now + " | " + crc.ToString();
htSMSMessage_t msg = new htSMSMessage_t();
msg.message = s;
msg.suid = suid.ToString();
s.Replace("\r\n", "");
s = s.Replace(System.Environment.NewLine, string.Empty);
Console.WriteLine("Message [" + s + "]");
bool is_email = false;
string email_addr = "";
string email_body = "";
if (s.Contains("@") && s.Contains(":") && s.Contains("."))
{ // this may be an email so analyze further
char[] sep = { ':' };
string[] all_msg = s.Split(sep);
email_addr = all_msg[0];
email_body = all_msg[1];
if (email_addr.Contains("@") && email_addr.Contains("."))
{
messBody = email_body;
emailAddr = email_addr;
is_email = true;
this.isEmail = true;
}
}
try
{
if (withDB)
{
if (is_email)
{
sqlResp = DBsms.insert_sms_sent2email(suid.ToString(), email_body, email_addr, seqID);
}
else
{
//new db insert for job tickets
if (s.Contains("<JTS>"))
{
s = s.Replace("<JTS>", "");
sqlResp = DBTicketing.insert_ticket_response(suid.ToString(), s);
if (sqlResp == sqlResponse.noUpdate)
delete_ticket_id = Convert.ToInt32(Regex.Replace(s, "[a-zA-z]", ""));
}
else
sqlResp = DBsms.insert_sms_received(suid.ToString(), s, 3, "", seqID);
}
}
else
{
if (is_email)
{
messBody = email_body;
emailAddr = email_addr;
}
else
{
messBody = s;
emailAddr = "";
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR in insert_sms_received:\r\n" + ex.ToString());
}
}
return hret;
}
public void DecodePacketHyt(string msg)
{
messBody = "";
emailAddr = "";
string s = msg;
s.Replace("\r\n", "");
s = s.Replace(System.Environment.NewLine, string.Empty);
Console.WriteLine("Message [" + s + "]");
bool is_email = false;
string email_addr = "";
string email_body = "";
if (s.Contains("@") && s.Contains(":") && s.Contains("."))
{ // this may be an email so analyze further
char[] sep = { ':' };
string[] all_msg = s.Split(sep);
email_addr = all_msg[0];
email_body = all_msg[1];
if (email_addr.Contains("@") && email_addr.Contains("."))
{
messBody = email_body;
emailAddr = email_addr;
is_email = true;
this.isEmail = true;
}
}
try
{
if (withDB)
{
if (is_email)
{
sqlResp = DBsms.insert_sms_sent2email(suid.ToString(), email_body, email_addr, seqID);
}
else
{
if (s.Contains("<JTS>"))
{
s = s.Replace("<JTS>", "");
sqlResp = DBTicketing.insert_ticket_response(suid.ToString(), s);
if (sqlResp == sqlResponse.noUpdate)
delete_ticket_id = Convert.ToInt32(Regex.Replace(s, "[a-zA-z]", ""));
}
else
sqlResp = DBsms.insert_sms_received(suid.ToString(), s, 3, "", seqID);
}
}
else
{
if (is_email)
{
messBody = email_body;
emailAddr = email_addr;
}
else
{
messBody = s;
emailAddr = "";
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR in insert_sms_received:\r\n" + ex.ToString());
}
}
}
}