/// <summary>
        /// Removes or returns an expired mail.
        /// </summary>
        private static void MailExpired(Character_mail mail)
        {
            if (mail.AuctionType != 0 || (mail.Money == 0 && mail.Items.Count == 0))
            {
                CharMgr.DeleteMail(mail);
                return;
            }

            Character receiver = CharMgr.GetCharacter(mail.SenderName, false);

            if (receiver != null)
            {
                ReturnMail(mail);
            }
            else
            {
                CharMgr.DeleteMail(mail);
            }
        }
        /// <summary>
        /// Returns an expired mail to its sender.
        /// </summary>
        private static MailResult ReturnMail(Character_mail mail)
        {
            // Can't return auction mail.
            if (mail.AuctionType != 0)
            {
                return(MailResult.TEXT_MAIL_RESULT11);
            }

            Character receiver = CharMgr.GetCharacter(mail.SenderName, false);

            // No one to return mail to.
            if (receiver == null)
            {
                return(MailResult.TEXT_MAIL_RESULT11);
            }

            // If mail is COD, remove the COD requirement and remove the money.
            if (mail.Cr)
            {
                mail.Cr    = false;
                mail.Money = 0;
            }

            CharMgr.DeleteMail(mail);

            Character_mail returnMail = new Character_mail
            {
                // Sender -> Reciever, Reciever -> Sender
                Guid              = CharMgr.GenerateMailGuid(),
                CharacterId       = mail.CharacterIdSender,
                CharacterIdSender = mail.CharacterId,
                SenderName        = mail.ReceiverName,
                ReceiverName      = mail.SenderName,
                Content           = "Your mail expired and has been returned to you.",
                ReadDate          = 0,
                SendDate          = (uint)TCPManager.GetTimeStamp(),
                Opened            = false
            };

            CharMgr.AddMail(returnMail);

            return(MailResult.TEXT_MAIL_UNK);
        }
        public void MailInteract(MailInteractType type, uint guid, PacketIn packet)
        {
            lock (_lockObject)
            {
                Character_mail mail = _mails.FirstOrDefault(match => match.Guid == guid);

                if (mail == null)
                {
                    return;
                }

                switch (type)
                {
                case MailInteractType.OpenMail:
                    if (!mail.Opened)
                    {
                        if (mail.ReadDate == 0)
                        {
                            mail.ReadDate = (uint)TCPManager.GetTimeStamp();
                        }
                        mail.Opened = true;
                        SendMailCount();
                        SendMailBox();
                    }
                    SendMail(mail);
                    break;

                case MailInteractType.ReturnMail:
                    SendResult(ReturnMail(mail));
                    SendMailCount();
                    SendMailBox();
                    break;

                case MailInteractType.DeleteMail:
                    CharMgr.DeleteMail(mail);
                    SendMailCount();
                    SendMailBox();
                    break;

                case MailInteractType.ChangeReadMarker:
                    packet.Skip(4);
                    mail.Opened = packet.GetUint8() == 1;
                    SendMailCount();
                    SendMailBox();
                    break;

                case MailInteractType.TakeItem:
                    if (mail.Cr && mail.Money > 0)
                    {
                        if (!_Owner.GetPlayer().RemoveMoney(mail.Money))
                        {
                            _Owner.GetPlayer().SendLocalizeString("", ChatLogFilters.CHATLOGFILTERS_USER_ERROR, Localized_text.TEXT_AUCTION_NOT_ENOUGH_MONEY);
                            return;
                        }
                        MailHandlers.SendCOD(mail.SenderName, _Owner.GetPlayer(), mail.Money);
                        mail.Money = 0;
                    }
                    packet.Skip(4);
                    byte itemnum = packet.GetUint8();
                    if (mail.Items.Count < itemnum + 1)
                    {
                        return;
                    }
                    MailItem item     = mail.Items.ElementAt(itemnum);
                    ushort   freeSlot = _Owner.GetPlayer().ItmInterface.GetFreeInventorySlot(ItemService.GetItem_Info(item.id), false);
                    if (freeSlot == 0)
                    {
                        _Owner.GetPlayer().SendLocalizeString("", ChatLogFilters.CHATLOGFILTERS_USER_ERROR, Localized_text.TEXT_OVERAGE_CANT_TAKE_ATTACHMENTS);
                        return;
                    }
                    _Owner.GetPlayer().ItmInterface.CreateItem(ItemService.GetItem_Info(item.id), item.count, item.talisman, item.primary_dye, item.secondary_dye, false);
                    mail.Items.Remove(item);
                    mail.Dirty = true;
                    CharMgr.Database.SaveObject(mail);

                    SendMailUpdate(mail);
                    SendMail(mail);
                    break;

                case MailInteractType.TakeAll:
                    if (mail.Money > 0)
                    {
                        if (mail.Cr)
                        {
                            if (!_Owner.GetPlayer().RemoveMoney(mail.Money))
                            {
                                _Owner.GetPlayer().SendLocalizeString("", ChatLogFilters.CHATLOGFILTERS_USER_ERROR, Localized_text.TEXT_AUCTION_NOT_ENOUGH_MONEY);
                                return;
                            }
                            MailHandlers.SendCOD(mail.SenderName, _Owner.GetPlayer(), mail.Money);
                        }
                        else
                        {
                            _Owner.GetPlayer().AddMoney(mail.Money);
                        }

                        mail.Money = 0;
                    }
                    // Take as many items as you can before inventory is full
                    List <MailItem> toRemove = new List <MailItem>();

                    foreach (MailItem curritem in mail.Items)
                    {
                        ushort slot = _Owner.GetPlayer().ItmInterface.GetFreeInventorySlot(ItemService.GetItem_Info(curritem.id));
                        if (slot == 0)
                        {
                            _Owner.GetPlayer().SendLocalizeString("", ChatLogFilters.CHATLOGFILTERS_USER_ERROR, Localized_text.TEXT_OVERAGE_CANT_TAKE_ATTACHMENTS);
                            break;
                        }
                        _Owner.GetPlayer().ItmInterface.CreateItem(ItemService.GetItem_Info(curritem.id), curritem.count, curritem.talisman, curritem.primary_dye, curritem.secondary_dye, false);
                        toRemove.Add(curritem);
                    }

                    foreach (MailItem remove in toRemove)
                    {
                        mail.Items.Remove(remove);
                    }

                    mail.Dirty = true;

                    CharMgr.Database.SaveObject(mail);
                    SendMailUpdate(mail);
                    SendMail(mail);
                    break;
                }
            }
        }