public cResponse BuildFromBuffer(byte[] pBuffer, ref int pBufferPosition, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cResponseBuilder), nameof(BuildFromBuffer)); while (pBufferPosition < pBuffer.Length) { byte lByte = pBuffer[pBufferPosition++]; if (mBytesToGo == 0) { if (mBufferedCR) { mBufferedCR = false; if (lByte == cASCII.LF) { if (ZBuildLineFromBytes(lContext)) { cResponse lResponse = new cResponse(mLines); mLines = new List <cResponseLine>(); return(lResponse); } continue; } mBytes.Add(cASCII.CR); } if (lByte == cASCII.CR) { mBufferedCR = true; } else { mBytes.Add(lByte); } } else { mBytes.Add(lByte); if (--mBytesToGo == 0) { ZAddWholeLine(true, lContext); } } } return(null); }
public void InvokeNetworkReceive(cResponse pResponse, cTrace.cContext pParentContext) { if (NetworkReceive == null) { return; // pre-check for efficiency only } var lContext = pParentContext.NewMethod(nameof(cCallbackSynchroniser), nameof(NetworkReceive)); if (mDisposed) { throw new ObjectDisposedException(nameof(cCallbackSynchroniser)); } ZInvokeAndForget(new cNetworkReceiveEventArgs(pResponse), lContext); // NOTE the event is fired by parallel code in the ZInvokeEvents routine: when adding an event you must put code there also }
public cBytesCursor(string pString) { if (pString == null) { throw new ArgumentNullException(nameof(pString)); } mLines = new cResponse(new cResponseLine[] { new cResponseLine(false, Encoding.UTF8.GetBytes(pString)) }); Position.BytesLine = mLines[0]; Position.LineNumber = 0; Position.Byte = -1; Position.AtEnd = false; ZAdvance(ref Position); }
public cBytesCursor(IList <byte> pBytes) { if (pBytes == null) { throw new ArgumentNullException(nameof(pBytes)); } mLines = new cResponse(new cResponseLine[] { new cResponseLine(false, pBytes) }); Position.BytesLine = mLines[0]; Position.LineNumber = 0; Position.Byte = -1; Position.AtEnd = false; ZAdvance(ref Position); }
public cBytesCursor(cResponse pResponse) { mLines = pResponse ?? throw new ArgumentNullException(nameof(pResponse)); if (mLines.Count == 0) { Position.AtEnd = true; return; } Position.BytesLine = mLines[0]; Position.LineNumber = 0; Position.Byte = -1; Position.AtEnd = false; ZAdvance(ref Position); }
public async Task <cResponse> ZBuildResponseAsync(cTrace.cContext pParentContext) { // SUPERVERBOSE var lContext = pParentContext.NewMethod(true, nameof(cConnection), nameof(ZBuildResponseAsync)); while (true) { if (mBuffer != null && mBufferPosition < mBuffer.Length) { cResponse lResponse = mBuilder.BuildFromBuffer(mBuffer, ref mBufferPosition, lContext); if (lResponse != null) { return(lResponse); } } var lBuffer = await ZReadAsync(lContext).ConfigureAwait(false); ZNewBuffer(lBuffer, lContext); } }
private void ZLogResponse(cResponse pResponse, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cConnection), nameof(ZLogResponse)); cByteList lLogBytes = new cByteList(); foreach (var lLine in pResponse) { if (lLine.Literal) { lLogBytes.Add(cASCII.LBRACE); } else { lLogBytes.Add(cASCII.LBRACKET); } foreach (var lByte in lLine) { if (lLogBytes.Count == 60) { lContext.TraceVerbose($"received: {cTools.BytesToLoggableString(lLogBytes)}..."); return; } lLogBytes.Add(lByte); } if (lLine.Literal) { lLogBytes.Add(cASCII.RBRACE); } else { lLogBytes.Add(cASCII.RBRACKET); } } lContext.TraceVerbose($"received: {cTools.BytesToLoggableString(lLogBytes)}"); }
public cResponse GetResponse(cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cConnection), nameof(GetResponse)); if (mState != eState.connected) { throw new InvalidOperationException(kInvalidOperationExceptionMessage.NotConnected); } if (mBuildResponseTask == null || !mBuildResponseTask.IsCompleted) { throw new InvalidOperationException(); } cResponse lResult = mBuildResponseTask.Result; mBuildResponseTask.Dispose(); mBuildResponseTask = null; if (lContext.EmitsVerbose) { ZLogResponse(lResult, lContext); } return(lResult); }
internal cNetworkReceiveEventArgs(cResponse pResponse) { Response = pResponse; }
public void MessageHandler(Connection conn, InstantMessage im) { Log.Instance.WriteStatus("INMSG (" + conn.m_type.ToString() + ") << " + im.User + " : " + im.Text); string strUniqueID = im.User + conn.m_type.ToString(); if (m_bLoadingUsers || m_bLoadingReminders) { conn.SendMessage(im.User, "RemindMe is performing maintenance. Please try again in a few minutes. If the problem persists, please contact [email protected]"); // TODO: fiond out why the bot gets stuck with one of these two being true all the time Log.Instance.WriteLine("m_bLoadingUsers = {0} , m_bLoadingReminders = {1}", m_bLoadingUsers.ToString(), m_bLoadingReminders.ToString()); return; } User user = userMgr.GetUserByService(conn.m_type, im.User); // unknown user? Then get out ohere quickly if (user == null) { // we don't want to send email back to spammers if ((m_imers[strUniqueID] == null || (int)m_imers[strUniqueID] < 1)) // && conn.m_type != ConnectionType.EMAIL) { conn.SendMessage(im.User, m_rm.GetString("unknown_user") + adMgr.GetNextAdText(null, conn.m_type)); } // protect against spamming the bot, max of 1 im if (m_imers[strUniqueID] == null) { m_imers[strUniqueID] = 1; } else { m_imers[strUniqueID] = (int)m_imers[strUniqueID] + 1; } return; } if ((im.Text.ToLower() == @"/reset aim" || im.Text.ToLower() == @"/reset icq" || im.Text.ToLower() == @"/reset yahoo" || im.Text.ToLower() == @"/reset msn") && user.Class == UserClassType.ADMIN) { string [] strList = im.Text.Split(' '); ConnectionType ct = (ConnectionType)Enum.Parse(typeof(ConnectionType), strList[1], true); Connection resetcon = conMgr.GetConnection(ct); if (resetcon != null) { resetcon.Disconnect(); Thread.Sleep(1000); resetcon.Connect(); } return; } if (im.Text.ToLower() == @"/whoami") { conn.SendMessage(im.User, "\r\nUser: "******"(" + user.UserID + ")" + "\r\nClass: " + user.Class.ToString() + "\r\nEmail: " + user.Email + "\r\nPlanNumber: " + user.PlanNumber); return; } Regex reg = new Regex(@"^f**k\s+you", RegexOptions.IgnoreCase); Match m = reg.Match(im.Text); if (m.Success) { conn.SendMessage(im.User, "That's really not nice!"); m_fuers[im.User] = 1; return; } // /help commands reg = new Regex(@"^[\/\-\:]+help\s*[\w\s]*$", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { string strResponse = helpMgr.GetResponse(im.Text); if (strResponse.Length > 1) { conn.SendMessage(im.User, strResponse); } return; } // /list reg = new Regex(@"^\/list", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { string strResponse = "\nCurrent Pending Reminders:\n"; foreach (Reminder aRem in DataManagerObj.GetUsersReminders(user.UserID)) { strResponse += aRem.ID + " " + aRem.UserTimeString + "\n"; } conn.SendMessage(im.User, strResponse); return; } // /cancel 1234 -- show a reminder reg = new Regex(@"^/cancel\s+(\d+)\s*$", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { string remId = m.Groups[1].Captures[0].ToString(); int iRemID = int.Parse(remId); Reminder aRem = DataManagerObj.GetReminder(iRemID); if (aRem != null && aRem.UserID == user.UserID) { aRem.Delivered = true; DataManagerObj.SaveReminder(aRem); string strResponse = m_rm.GetString("cancel_reminder_conf"); strResponse = strResponse.Replace("%d", aRem.ID); conn.SendMessage(im.User, strResponse); } return; } // /show 1234 - display the details of reminder 1234 reg = new Regex(@"^/show\s+(\d+)\s*$", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { string remId = m.Groups[1].Captures[0].ToString(); int iRemID = int.Parse(remId); Reminder aRem = DataManagerObj.GetReminder(iRemID); if (aRem != null && aRem.UserID == user.UserID) { string strResponse = m_rm.GetString(@"reminder_info_IM"); User creatorObj = _dataManager.GetUser(aRem.CreatorID); strResponse = strResponse.Replace("%d", aRem.ID.ToString()); strResponse = strResponse.Replace("%a", creatorObj.Username); strResponse = strResponse.Replace("%b", aRem.UserTimeString); strResponse = strResponse.Replace("%c", aRem.Message); conn.SendMessage(im.User, strResponse); } return; } // remind me ....... reg = new Regex(@"^remind\s+\w+\s+\w+", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { ProcessParse(conn, im, user, @"creation_parse"); return; } // in 20 minutes remind me to transfer reg = new Regex(@"^.*?\s+remind\s+[\w_,]+\s+.*", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { ProcessParse(conn, im, user, @"creation_parse"); return; } reg = new Regex(@"^repeat\s+\w+", RegexOptions.IgnoreCase); m = reg.Match(im.Text); if (m.Success) { ProcessParse(conn, im, user, @"repeat_parse"); return; } if (user != null && conn.m_type != ConnectionType.EMAIL) { if (_aliceBotReady) { cResponse reply = _aliceBot.chat(im.Text, user.Username); conn.SendMessage(im.User, reply.getOutput()); } else { string strMsg = m_rm.GetString("known_user_default"); strMsg = strMsg.Replace("%u", user.Username); conn.SendMessage(im.User, strMsg); } } }