public static void WriteBody(Action <string> writer, QHFMessage msg, string myNick, string contactNick) { string sender = msg.IsMy ? myNick : contactNick; writer.Invoke($"{sender} [{msg.Time:dd.MM.yyyy HH:mm:ss}]"); writer.Invoke(msg.Text); writer.Invoke(""); }
public int LoadToDb(string qhfFilePath, out int totalMsgs) { var messages = new List <IcqMessage>(); var account = new IcqAccount(); using (QHFReader reader = new QHFReader(qhfFilePath)) { account.Nickname = reader.Nick; account.Uin = Convert.ToInt32(reader.Uin); var message = new QHFMessage(); while (reader.GetNextMessage(message)) { messages.Add(new IcqMessage(account.Uin, message.IsMy, message.Time, message.Text)); } } messages = messages.OrderBy(ks => ks.DateTime).ToList(); totalMsgs = messages.Count; var loader = IcqMessageLoader.GetInstance(_cnString); IEnumerable <IcqMessage> uploadedMessages = null; var accountLoader = IcqAccountLoader.GetInstance(_cnString); if (accountLoader.Get(account.Uin) == null) { accountLoader.Add(account); } else { if (messages.First().DateTime < loader.GetLatest(account).DateTime) { if (messages.Last().DateTime > loader.GetEarliest(account).DateTime) { uploadedMessages = loader.GetRange(account, messages.First().DateTime, messages.Last().DateTime); } } } int uploadCount = 0; if (uploadedMessages == null) { uploadCount = loader.AddRange(messages); } else { var selectedMessages = messages.Where(m => !uploadedMessages.Any(um => um.Equals(m))).ToList(); uploadCount = loader.AddRange(selectedMessages); } return(uploadCount); }
public static List <QHFMessage> ReadMessages(string fileName) { List <QHFMessage> result = new List <QHFMessage>(); string uin = Path.GetFileNameWithoutExtension(fileName); QHFMessage msg = null; using (FileStream fs = new FileStream(fileName, FileMode.Open)) using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(1251))) { string nextLine = sr.ReadLine(); while (nextLine != null) { if (nextLine == separatorFrom) { msg = new QHFMessage(); msg.ID = result.Count + 1; msg.IsMy = false; msg.Time = ParseDateTime(sr.ReadLine()); nextLine = sr.ReadLine(); } else if (nextLine == separatorTo) { msg = new QHFMessage(); msg.ID = result.Count + 1; msg.IsMy = true; msg.Time = ParseDateTime(sr.ReadLine()); nextLine = sr.ReadLine(); } else { string previousLine = nextLine; nextLine = sr.ReadLine(); if (nextLine == separatorFrom || nextLine == separatorTo || nextLine == null) { result.Add(msg); } else { if (msg.Text != null) { msg.Text += Environment.NewLine; } msg.Text += previousLine; } } } } return(result); }
public bool GetNextMessage(QHFMessage msg) { if (fs.Position >= fs.Length - 24) { return(false); } byte[] msgBytes = null; //Trace.WriteLine("Block start: " + fs.Position); msg.Signature = br.ReadInt16(); if (msg.SignatureMismatch) { if (br.ReadByte() == 1) { // В предыдущем сообщении были ошибки и структура съехала. msg.Signature = 1; } else { // Сдвигаем позицию на исходную для достоверности отладки. fs.Position -= 1; Trace.WriteLine($"{Uin} ({Nick}). Signature mismatch. (sign={msg.Signature})"); return(true); } } var size = br.ReadInt32(); var endOfBlock = size + fs.Position; try { msg.ID = GetNextBlock(); msg.Time = UnixTimeStampToDateTime(GetNextBlock()); msg.IsMy = GetNextBlock() > 0; var msgLength = GetNextBlock(); if (msgLength < 0) { fs.Position -= 4; msgBytes = ReadBytesUntilNextMessage(msg.ID + 1); msgLength = msgBytes.Length; } else { if (msgLength > size) { fs.Position -= 4; msgLength = br.ReadInt16(); if (msgLength > size) { fs.Position -= 2; msgLength = br.ReadByte(); } } msgBytes = br.ReadBytes(msgLength); } msg.Text = encoding.GetString(DecodeBytes(msgBytes)); return(true); } catch (ArgumentException ex) { Trace.WriteLine(ex.Message); if (fs.Position == fs.Length) { msg.Text = "Сообщение потеряно."; } return(true); } }