%@ page contentType="text/html; charset=UTF-8" %>
<%--
-
- Copyright (C) 2004-2008 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--%>
<%@ page import="org.jivesoftware.util.ParamUtils,
org.jivesoftware.util.StringUtils,
org.jivesoftware.util.CookieUtils,
org.jivesoftware.openfire.SessionManager,
org.jivesoftware.openfire.session.ClientSession,
org.jivesoftware.openfire.user.User,
org.xmpp.packet.JID,
java.net.URLEncoder,
java.util.Collection,
java.util.HashMap"
errorPage="error.jsp"
%>
<%@ page import="java.util.Map" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="org.jivesoftware.openfire.XMPPServer" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="admin" uri="admin" %>
<% // Get parameters
String username = ParamUtils.getParameter(request,"username");
boolean send = ParamUtils.getBooleanParameter(request,"send");
boolean success = ParamUtils.getBooleanParameter(request,"success");
boolean sendToAll = ParamUtils.getBooleanParameter(request,"sendToAll");
boolean tabs = ParamUtils.getBooleanParameter(request,"tabs",true);
String jid = ParamUtils.getParameter(request,"jid");
String[] jids = ParamUtils.getParameters(request,"jid");
String message = ParamUtils.getParameter(request,"message");
%>
<% webManager.init(pageContext); %>
<%
// Handle a cancel
if (request.getParameter("cancel") != null) {
if (username == null) {
response.sendRedirect("session-summary.jsp");
return;
}
else {
response.sendRedirect("user-properties.jsp?username=" + URLEncoder.encode(username, StandardCharsets.UTF_8));
return;
}
}
// Get the user - a user might not be passed in if this is a system-wide message
User user = null;
if (username != null) {
user = webManager.getUserManager().getUser(username);
}
// Get the session manager
SessionManager sessionManager = webManager.getSessionManager();
// Handle the request to send a message:
Map errors = new HashMap<>();
Cookie csrfCookie = CookieUtils.getCookie(request, "csrf");
String csrfParam = ParamUtils.getParameter(request, "csrf");
if (send) {
if (csrfCookie == null || csrfParam == null || !csrfCookie.getValue().equals(csrfParam)) {
send = false;
errors.put("csrf", "CSRF Failure!");
}
}
csrfParam = StringUtils.randomString(15);
CookieUtils.setCookie(request, response, "csrf", csrfParam, -1);
pageContext.setAttribute("csrf", csrfParam);
if (send) {
// Validate the message and jid
if (jid == null && !sendToAll && user != null) {
errors.put("jid","jid");
}
if (message == null) {
errors.put("message","message");
}
if (errors.isEmpty()) {
// no errors, so continue
if (user == null) {
// system-wide message:
sessionManager.sendServerMessage(null,message);
}
else {
if (sendToAll) {
// loop through all sessions based on the user assoc with the JID, send
// message to all
for (String jid1 : jids) {
JID address = new JID(jid1);
// TODO: Do we really need this?
sessionManager.getSession(address);
sessionManager.sendServerMessage(address, null, message);
// Log the event
webManager.logEvent("send server message", "jid = all active\nmessage = "+message);
}
}
else {
sessionManager.sendServerMessage(new JID(jid),null,message);
// Log the event
webManager.logEvent("send server message", "jid = "+jid+"\nmessage = "+message);
}
}
if (username != null){
response.sendRedirect("user-message.jsp?success=true&username=" +
URLEncoder.encode(username, StandardCharsets.UTF_8) + "&tabs=" + tabs);
}
else {
response.sendRedirect("user-message.jsp?success=true");
}
return;
}
}
// Get all sessions associated with this user:
int numSessions = -1;
ClientSession sess = null;
Collection sessions = null;
if (user != null) {
numSessions = sessionManager.getSessionCount(user.getUsername());
sessions = sessionManager.getSessions(XMPPServer.getInstance().createJID(user.getUsername(), null));
if (numSessions == 1) {
sess = sessions.iterator().next();
}
}
%>
<% if (success) { %>
<% } %>