示例#1
0
        public IMessage ExecutePlugins(IBot bot, SocketMessage message)
        {
            var mes = new Plugins.Message(message);

            foreach (var p in Plugins)
            {
                p.Execute(bot, mes);
                if (mes.Terminated)
                {
                    break;
                }
            }
            return(mes);
        }
示例#2
0
    public static ServerPacketStructures.MessagingResponse SendMessage(GuidReference senderRef, ushort boardId, string recipient, string subject, string body)
    {
        var response = string.Empty;
        var success  = true;

        var senderSentMail = Game.World.WorldData.GetOrCreate <SentMail>(senderRef);

        // Don't allow blank title or subject
        if (string.IsNullOrWhiteSpace(subject))
        {
            success  = false;
            response = "The message had no subject.";
        }
        else if (string.IsNullOrWhiteSpace(body))
        {
            success  = false;
            response = "You can't send empty messages.";
        }
        else if (!body.IsAscii() || !subject.IsAscii())
        {
            success  = false;
            response = "You can't use special characters in a message (ASCII only).";
        }
        else if (boardId == ushort.MaxValue - 1)
        {
            success  = false;
            response = "You can't post messages here.";
        }

        // Handle plugin response

        if (!Game.World.TryGetActiveUser(senderRef.UserName, out _))
        {
            // Currently a user must be online to send mail, so if we get here, something really wacky is happening
            return(UnknownError);
        }
        try
        {
            IMessageHandler handler;
            Xml.MessageType type;
            if (boardId == 0)
            {
                type = Xml.MessageType.Mail;
            }
            else
            {
                type = Xml.MessageType.BoardMessage;
            }

            var message = new Plugins.Message(type, senderRef.UserName, recipient, subject, body);

            handler = Game.World.ResolveMessagingPlugin(type, message);

            if (handler is IProcessingMessageHandler pmh && success)
            {
                var msg  = new Plugins.Message(Xml.MessageType.Mail, senderRef.UserName, recipient, subject, body);
                var resp = pmh.Process(msg);
                if (!pmh.Passthrough)
                {
                    // TODO: implement cast / resolve duplication
                    var hmsg = new Message(recipient, senderRef.UserName, subject, body);
                    senderSentMail.ReceiveMessage((Message)hmsg);

                    // Plugin is "last destination" for message
                    return(new ServerPacketStructures.MessagingResponse()
                    {
                        ResponseType = BoardResponseType.EndResult,
                        ResponseSuccess = resp.Success,
                        ResponseString = resp.PluginResponse
                    });
                }
                else if (resp.Transformed)
                {
                    // Update message if transformed, and keep going
                    recipient = resp.Message.Recipient;
                    subject   = resp.Message.Subject;
                    body      = resp.Message.Text;
                }
            }
        }
        catch (Exception e)
        {
            Game.ReportException(e);
            success  = false;
            response = "An unknown error occurred. Sorry!";
        }

        // Annoyingly board replies use the same packet path as sending mail replies, so we need to handle
        // both here
        if (boardId == 0 && success)
        {
            var receiverRef = Game.World.WorldData.GetGuidReference(recipient);
            if (receiverRef == null)
            {
                success  = false;
                response = "Sadly, no record of that person exists in the realm.";
            }
            else
            {
                var mailbox = Game.World.WorldData.GetOrCreate <Mailbox>(receiverRef);
                var msg     = new Message(recipient, senderRef.UserName, subject, body);
                try
                {
                    if ((DateTime.Now - senderSentMail.LastMailMessageSent).TotalSeconds < Constants.MAIL_MESSAGE_COOLDOWN &&
                        senderSentMail.LastMailRecipient == recipient)
                    {
                        success  = false;
                        response = $"You've sent too much mail to {recipient} recently. Give it a rest.";
                    }
                    else if (mailbox.ReceiveMessage(msg))
                    {
                        response = $"Your letter to {recipient} was sent.";
                        GameLog.InfoFormat("mail: {0} sent message to {1}", senderRef.UserName, recipient);
                        World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.MailNotifyUser,
                                                                                 recipient));
                        senderSentMail.LastMailRecipient   = recipient;
                        senderSentMail.LastMailMessageSent = DateTime.Now;
                    }
                    else
                    {
                        success  = false;
                        response = $"{recipient}'s mailbox is full or locked. A copy was kept in your sent mailbox. Sorry!";
                    }
                }
                catch (MessageStoreLocked e)
                {
                    Game.ReportException(e);
                    success  = false;
                    response = $"{recipient} cannot receive mail at this time. Sorry!";
                }
                senderSentMail.ReceiveMessage((Message)msg.Clone());
            }
        }
        else if (success)
        {
            if (Game.World.WorldData.TryGetValueByIndex(boardId, out Board board))
            {
                if (Game.World.WorldData.TryGetAuthInfo(senderRef.UserName, out AuthInfo ainfo))
                {
                    if (ainfo.IsPrivileged || board.CheckAccessLevel(ainfo.Username, BoardAccessLevel.Write))
                    {
                        var msg = new Message(board.DisplayName, senderRef.UserName, subject, body);
                        if (board.ReceiveMessage(msg))
                        {
                            response = "The message was sent.";
                            success  = true;
                            var sentMsg = (Message)msg.Clone();
                            sentMsg.Recipient = board.DisplayName;
                            senderSentMail.ReceiveMessage(sentMsg);
                        }
                        else
                        {
                            response = "The message could not be sent.";
                        }
                    }
                    else
                    {
                        response = "You don't have permission to do that.";
                    }
                }
                else
                {
                    response = "Authentication information could not be verified.";
                }
            }
            else
            {
                response = "Board not found.";
            }
        }

        return(new ServerPacketStructures.MessagingResponse()
        {
            ResponseType = BoardResponseType.EndResult,
            ResponseString = response,
            ResponseSuccess = success
        });
    }