Beispiel #1
0
    protected void btnSendMessage_Click(object sender, EventArgs e)
    {
        // This is because of ASP.NET default behaviour
        // The first empty line was trimmed after each postback
        if (BBEditor.Text.StartsWithCSafe("\n"))
        {
            BBEditor.Text = "\n" + BBEditor.Text;
        }
        // Flood protection
        if (!FloodProtectionHelper.CheckFlooding(SiteContext.CurrentSiteName, MembershipContext.AuthenticatedUser))
        {
            var currentUser = MembershipContext.AuthenticatedUser;

            // Check banned IP
            if (BannedIPInfoProvider.IsAllowed(SiteContext.CurrentSiteName, BanControlEnum.AllNonComplete))
            {
                int recipientId = ucMessageUserSelector.Visible
                                      ? ucMessageUserSelector.SelectedUserID
                                      : ValidationHelper.GetInteger(hdnUserId.Value, 0);
                string message  = string.Empty;
                string nickName = HTMLHelper.HTMLEncode(txtFrom.Text.Trim());
                if (!ValidateBody(DiscussionMacroResolver.RemoveTags(ucBBEditor.Text)))
                {
                    message = GetString("SendMessage.EmptyBody");
                }


                // Check sender nick name if anonymous
                if (isAnonymousUser && (nickName == string.Empty))
                {
                    message = GetString("SendMesage.NoNickName");
                }

                UserInfo recipient = null;

                // Check recipient
                if (recipientId == 0)
                {
                    if (string.IsNullOrEmpty(ucMessageUserSelector.UserNameTextBox.Text.Trim()))
                    {
                        message = GetString("SendMesage.NoRecipient");
                    }
                    else
                    {
                        message = GetString("SendMesage.UserDoesntExists");
                    }
                }
                else
                {
                    int defRecipientId = ValidationHelper.GetInteger(DefaultRecipient, 0);
                    recipient = UserInfoProvider.GetUserInfo(recipientId);

                    // Normal users can't send message to user from other site except for global admin
                    if ((!recipient.IsInSite(SiteContext.CurrentSiteName) && !currentUser.IsGlobalAdministrator))
                    {
                        message = GetString("SendMesage.UserDoesntExists");
                    }

                    // If default recipient selected and is same as message recipient, skip check on hidden users
                    if (recipient.UserID != defRecipientId)
                    {
                        bool recipientIsEnabled = !recipient.UserIsDisabledManually && !recipient.UserSettings.UserWaitingForApproval;
                        bool usersAreFriends    = false;
                        if (recipientIsEnabled && ModuleManager.IsModuleLoaded(ModuleName.COMMUNITY))
                        {
                            usersAreFriends = ModuleCommands.CommunityFriendshipExists(currentUser.UserID, recipient.UserID);
                        }

                        bool isRecipientAllowed = recipientIsEnabled && (!recipient.UserIsHidden || (SendMessageMode == MessageActionEnum.Reply) || usersAreFriends);

                        // If live site mode hide not allowed users for all users except for global admins and public user for all users
                        if ((IsLiveSite && !isRecipientAllowed && !currentUser.IsGlobalAdministrator) || (recipient.UserName.ToLowerCSafe() == "public"))
                        {
                            message = GetString("SendMesage.UserDoesntExists");
                        }
                    }
                }

                if (message == string.Empty)
                {
                    // Send message
                    try
                    {
                        // Check if current user is in recipient's ignore list
                        bool isIgnored = IgnoreListInfoProvider.IsInIgnoreList(recipientId, currentUser.UserID);


                        Message             = new MessageInfo();
                        Message.MessageBody = ucBBEditor.Text;
                        string subject = (txtSubject.Text.Trim() == string.Empty) ? GetString("Messaging.NoSubject") : txtSubject.Text.Trim();
                        Message.MessageSubject           = TextHelper.LimitLength(subject, 200);
                        Message.MessageRecipientUserID   = recipientId;
                        Message.MessageRecipientNickName = TextHelper.LimitLength(Functions.GetFormattedUserName(recipient.UserName, recipient.FullName, recipient.UserNickName, IsLiveSite), 200);
                        Message.MessageSent = DateTime.Now;

                        // Anonymous user
                        if (isAnonymousUser)
                        {
                            Message.MessageSenderNickName = TextHelper.LimitLength(nickName, 200);
                            Message.MessageSenderDeleted  = true;
                        }
                        else
                        {
                            Message.MessageSenderUserID   = currentUser.UserID;
                            Message.MessageSenderNickName = TextHelper.LimitLength(Functions.GetFormattedUserName(currentUser.UserName, currentUser.FullName, currentUser.UserNickName, IsLiveSite), 200);

                            // If the user is ignored, delete message automatically
                            if (isIgnored)
                            {
                                Message.MessageRecipientDeleted = true;
                            }
                        }

                        string error = string.Empty;

                        // Check bad words
                        if (!BadWordInfoProvider.CanUseBadWords(currentUser, SiteContext.CurrentSiteName))
                        {
                            // Prepare columns to check
                            Dictionary <string, int> columns = new Dictionary <string, int>();
                            columns.Add("MessageSubject", 200);
                            columns.Add("MessageBody", 0);
                            columns.Add("MessageSenderNickName", 200);
                            columns.Add("MessageRecipientNickName", 200);

                            // Perform bad word check
                            error = BadWordsHelper.CheckBadWords(Message, columns, currentUser.UserID, () => { return(ValidateBody(Message.MessageBody)); });
                        }

                        if (error != string.Empty)
                        {
                            ShowError(error);
                        }
                        else
                        {
                            // Check message subject, if empty set no subject text
                            if (Message.MessageSubject.Trim() == string.Empty)
                            {
                                Message.MessageSubject = GetString("Messaging.NoSubject");
                            }

                            // Whole text has been removed
                            if (!ValidateBody(Message.MessageBody))
                            {
                                ShowError(GetString("SendMessage.EmptyBodyBadWords"));
                            }
                            else
                            {
                                // Save the message
                                MessageInfoProvider.SetMessageInfo(Message);

                                // Send notification email, if not ignored
                                if (!isIgnored)
                                {
                                    MessageInfoProvider.SendNotificationEmail(Message, recipient, currentUser, SiteContext.CurrentSiteName);
                                }
                                ShowConfirmation(GetString("SendMesage.MessageSent"));
                                MessageId = 0;
                                ucMessageUserSelector.SelectedUserID = 0;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowError(ex.Message);
                        ErrorMessage = ex.Message;
                    }
                }
                // Error in the form
                else
                {
                    ShowError(message);
                    ErrorMessage = message;
                }
            }
            else
            {
                ShowError(GetString("General.BannedIP"));
            }
        }
        else
        {
            ShowError(GetString("General.FloodProtection"));
        }

        // External event
        if (SendButtonClick != null)
        {
            SendButtonClick(sender, e);
        }
    }