1 43 package net.jforum.view.forum; 44 45 import java.util.List ; 46 47 import net.jforum.Command; 48 import net.jforum.SessionFacade; 49 import net.jforum.dao.DataAccessDriver; 50 import net.jforum.dao.UserDAO; 51 import net.jforum.entities.Post; 52 import net.jforum.entities.PrivateMessage; 53 import net.jforum.entities.PrivateMessageType; 54 import net.jforum.entities.User; 55 import net.jforum.entities.UserSession; 56 import net.jforum.repository.SmiliesRepository; 57 import net.jforum.util.I18n; 58 import net.jforum.util.concurrent.executor.QueuedExecutor; 59 import net.jforum.util.mail.EmailSenderTask; 60 import net.jforum.util.mail.PrivateMessageSpammer; 61 import net.jforum.util.preferences.ConfigKeys; 62 import net.jforum.util.preferences.SystemGlobals; 63 import net.jforum.util.preferences.TemplateKeys; 64 import net.jforum.view.forum.common.PostCommon; 65 import net.jforum.view.forum.common.ViewCommon; 66 67 71 public class PrivateMessageAction extends Command 72 { 73 public void inbox() throws Exception 74 { 75 if (!SessionFacade.isLogged()) { 76 this.setTemplateName(ViewCommon.contextToLogin()); 77 return; 78 } 79 80 User user = new User(); 81 user.setId(SessionFacade.getUserSession().getUserId()); 82 83 List pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromInbox(user); 84 85 this.context.put("inbox", true); 86 this.context.put("pmList", pmList); 87 this.setTemplateName(TemplateKeys.PM_INBOX); 88 this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.inbox")); 89 this.putTypes(); 90 } 91 92 public void sentbox() throws Exception 93 { 94 if (!SessionFacade.isLogged()) { 95 this.setTemplateName(ViewCommon.contextToLogin()); 96 return; 97 } 98 99 User user = new User(); 100 user.setId(SessionFacade.getUserSession().getUserId()); 101 102 List pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromSent(user); 103 104 this.context.put("sentbox", true); 105 this.context.put("pmList", pmList); 106 this.setTemplateName(TemplateKeys.PM_SENTBOX); 107 this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.sentbox")); 108 this.putTypes(); 109 } 110 111 private void putTypes() 112 { 113 this.context.put("NEW", new Integer (PrivateMessageType.NEW)); 114 this.context.put("READ", new Integer (PrivateMessageType.READ)); 115 this.context.put("UNREAD", new Integer (PrivateMessageType.UNREAD)); 116 } 117 118 public void send() throws Exception 119 { 120 if (!SessionFacade.isLogged()) { 121 this.setTemplateName(ViewCommon.contextToLogin()); 122 return; 123 } 124 125 User user = DataAccessDriver.getInstance().newUserDAO().selectById( 126 SessionFacade.getUserSession().getUserId()); 127 user.setSignature(PostCommon.processText(user.getSignature())); 128 user.setSignature(PostCommon.processSmilies(user.getSignature(), SmiliesRepository.getSmilies())); 129 130 this.sendFormCommon(user); 131 } 132 133 public void sendTo() throws Exception 134 { 135 if (!SessionFacade.isLogged()) { 136 this.setTemplateName(ViewCommon.contextToLogin()); 137 return; 138 } 139 140 User user = DataAccessDriver.getInstance().newUserDAO().selectById( 141 SessionFacade.getUserSession().getUserId()); 142 143 int userId = this.request.getIntParameter("user_id"); 144 145 if (userId > 0){ 146 User recipient = DataAccessDriver.getInstance().newUserDAO().selectById(userId); 147 148 this.context.put("pmRecipient", recipient); 149 this.context.put("toUserId", new Integer (recipient.getId())); 150 this.context.put("toUsername", recipient.getUsername()); 151 this.context.put("pageTitle", I18n.getMessage("PrivateMessage.title") 152 + " " + I18n.getMessage("PrivateMessage.to") 153 + " " + recipient.getUsername()); 154 } 155 156 this.sendFormCommon(user); 157 } 158 159 private void sendFormCommon(User user) 160 { 161 this.setTemplateName(TemplateKeys.PM_SENDFORM); 162 163 this.context.put("user", user); 164 this.context.put("moduleName", "pm"); 165 this.context.put("action", "sendSave"); 166 this.context.put("htmlAllowed", true); 167 this.context.put("attachmentsEnabled", false); 168 this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST)); 169 this.context.put("attachmentsEnabled", false); 170 this.context.put("maxAttachmentsSize", new Integer (0)); 171 this.context.put("smilies", SmiliesRepository.getSmilies()); 172 } 173 174 public void sendSave() throws Exception 175 { 176 if (!SessionFacade.isLogged()) { 177 this.setTemplateName(ViewCommon.contextToLogin()); 178 return; 179 } 180 181 UserDAO userDao = DataAccessDriver.getInstance().newUserDAO(); 182 183 String toUserIdStr = this.request.getParameter("toUserId"); 184 String toUsername = this.request.getParameter("toUsername"); 185 String userEmail; 186 187 int toUserId = -1; 188 189 if (toUserIdStr == null || "".equals(toUserIdStr.trim())) { 192 List l = userDao.findByName(toUsername, true); 193 194 if (l.size() > 0) { 195 User u = (User)l.get(0); 196 toUserId = u.getId(); 197 userEmail = u.getEmail(); 198 } 199 } 200 else { 201 toUserId = Integer.parseInt(toUserIdStr); 202 } 203 204 if (toUserId == -1) { 206 this.setTemplateName(TemplateKeys.PM_SENDSAVE_USER_NOTFOUND); 207 this.context.put("message", I18n.getMessage("PrivateMessage.userIdNotFound")); 208 return; 209 } 210 211 PrivateMessage pm = new PrivateMessage(); 212 pm.setPost(PostCommon.fillPostFromRequest()); 213 214 User fromUser = new User(); 216 fromUser.setId(SessionFacade.getUserSession().getUserId()); 217 pm.setFromUser(fromUser); 218 219 User toUser = userDao.selectById(toUserId); 221 pm.setToUser(toUser); 222 223 boolean preview = ("1".equals(this.request.getParameter("preview"))); 224 225 if (!preview) { 226 DataAccessDriver.getInstance().newPrivateMessageDAO().send(pm); 227 228 this.setTemplateName(TemplateKeys.PM_SENDSAVE); 229 this.context.put("message", I18n.getMessage("PrivateMessage.messageSent", 230 new String [] { this.request.getContextPath() +"/pm/inbox" 231 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)})); 232 233 String sid = SessionFacade.isUserInSession(toUserId); 236 if (sid != null) { 237 UserSession us = SessionFacade.getUserSession(sid); 238 us.setPrivateMessages(us.getPrivateMessages() + 1); 239 } 240 241 if (toUser.getEmail() != null && toUser.getEmail().trim().length() > 0) { 242 if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) { 243 try { 244 QueuedExecutor.getInstance().execute(new EmailSenderTask(new PrivateMessageSpammer(toUser))); 245 } 246 catch (Exception e) { 247 System.out.println(e); 248 } 249 } 250 } 251 } 252 else { 253 this.context.put("preview", true); 254 this.context.put("post", pm.getPost()); 255 256 Post postPreview = new Post(pm.getPost()); 257 this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview)); 258 this.context.put("pm", pm); 259 260 this.send(); 261 } 262 } 263 264 public void findUser() throws Exception 265 { 266 boolean showResult = false; 267 String username = this.request.getParameter("username"); 268 269 if (username != null && !username.equals("")) { 270 List namesList = DataAccessDriver.getInstance().newUserDAO().findByName(username, false); 271 this.context.put("namesList", namesList); 272 showResult = true; 273 } 274 275 this.setTemplateName(TemplateKeys.PM_FIND_USER); 276 277 this.context.put("username", username); 278 this.context.put("showResult", showResult); 279 } 280 281 public void read() throws Exception 282 { 283 if (!SessionFacade.isLogged()) { 284 this.setTemplateName(ViewCommon.contextToLogin()); 285 return; 286 } 287 288 int id = this.request.getIntParameter("id"); 289 290 PrivateMessage pm = new PrivateMessage(); 291 pm.setId(id); 292 293 pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm); 294 295 UserSession us = SessionFacade.getUserSession(); 298 int userId = us.getUserId(); 299 300 if (pm.getToUser().getId() == userId || pm.getFromUser().getId() == userId) { 301 pm.getPost().setText(PostCommon.preparePostForDisplay(pm.getPost()).getText()); 302 303 if (pm.getType() == PrivateMessageType.NEW) { 305 pm.setType(PrivateMessageType.READ); 306 DataAccessDriver.getInstance().newPrivateMessageDAO().updateType(pm); 307 us.setPrivateMessages(us.getPrivateMessages() - 1); 308 } 309 310 User u = pm.getFromUser(); 311 ViewCommon.prepareUserSignature(u); 312 313 this.context.put("pm", pm); 314 this.setTemplateName(TemplateKeys.PM_READ); 315 } 316 else { 317 this.setTemplateName(TemplateKeys.PM_READ_DENIED); 318 this.context.put("message", I18n.getMessage("PrivateMessage.readDenied")); 319 } 320 } 321 322 public void review() throws Exception 323 { 324 this.read(); 325 this.setTemplateName(TemplateKeys.PM_READ_REVIEW); 326 } 327 328 public void delete() throws Exception 329 { 330 if (!SessionFacade.isLogged()) { 331 this.setTemplateName(ViewCommon.contextToLogin()); 332 return; 333 } 334 335 String ids[] = this.request.getParameterValues("id"); 336 337 if (ids != null && ids.length > 0) { 338 PrivateMessage[] pm = new PrivateMessage[ids.length]; 339 User u = new User(); 340 u.setId(SessionFacade.getUserSession().getUserId()); 341 342 for (int i = 0; i < ids.length; i++) { 343 pm[i] = new PrivateMessage(); 344 pm[i].setFromUser(u); 345 pm[i].setId(Integer.parseInt(ids[i])); 346 } 347 348 DataAccessDriver.getInstance().newPrivateMessageDAO().delete(pm); 349 } 350 351 this.setTemplateName(TemplateKeys.PM_DELETE); 352 this.context.put("message", I18n.getMessage("PrivateMessage.deleteDone", 353 new String [] { this.request.getContextPath() + "/pm/inbox" 354 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) })); 355 } 356 357 public void reply() throws Exception 358 { 359 if (!SessionFacade.isLogged()) { 360 this.setTemplateName(ViewCommon.contextToLogin()); 361 return; 362 } 363 364 int id = this.request.getIntParameter("id"); 365 366 PrivateMessage pm = new PrivateMessage(); 367 pm.setId(id); 368 pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm); 369 370 int userId = SessionFacade.getUserSession().getUserId(); 371 372 if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) { 373 this.setTemplateName(TemplateKeys.PM_READ_DENIED); 374 this.context.put("message", I18n.getMessage("PrivateMessage.readDenied")); 375 return; 376 } 377 378 pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject()); 379 380 this.context.put("pm", pm); 381 this.context.put("pmReply", true); 382 383 this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById( 384 SessionFacade.getUserSession().getUserId())); 385 } 386 387 public void quote() throws Exception 388 { 389 if (!SessionFacade.isLogged()) { 390 this.setTemplateName(ViewCommon.contextToLogin()); 391 return; 392 } 393 394 int id = this.request.getIntParameter("id"); 395 396 PrivateMessage pm = new PrivateMessage(); 397 pm.setId(id); 398 pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm); 399 400 int userId = SessionFacade.getUserSession().getUserId(); 401 402 if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) { 403 this.setTemplateName(TemplateKeys.PM_READ_DENIED); 404 this.context.put("message", I18n.getMessage("PrivateMessage.readDenied")); 405 return; 406 } 407 408 pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject()); 409 410 this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(userId)); 411 412 this.context.put("quote", "true"); 413 this.context.put("quoteUser", pm.getFromUser().getUsername()); 414 this.context.put("post", pm.getPost()); 415 this.context.put("pm", pm); 416 } 417 418 421 public void list() throws Exception 422 { 423 this.inbox(); 424 } 425 } 426 | Popular Tags |