/* * Copyright (C) 2008-2017, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.juick.www.controllers; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.PMQueriesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.server.util.UserUtils; import com.juick.server.util.WebUtils; import com.juick.www.WebApp; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; import javax.inject.Inject; import java.io.IOException; import java.util.List; /** * * @author Ugnich Anton */ @Controller public class PM { private static final Logger logger = LoggerFactory.getLogger(PM.class); @Inject PMQueriesService pmQueriesService; @Inject TagService tagService; @Inject UserService userService; @Inject MessagesService messagesService; @Inject WebApp webApp; @GetMapping("/pm/inbox") protected String doGetInbox(ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { return "redirect:/login"; } String title = "PM: Inbox"; List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); model.addAttribute("title", title); model.addAttribute("visitor", visitor); model.addAttribute("msgs", msgs); model.addAttribute("tags", tagService.getPopularTags()); return "views/pm_inbox"; } @GetMapping("/pm/sent") protected String doGetSent(@RequestParam(required = false) String uname, ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { return "redirect:/login"; } String title = "PM: Sent"; List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); if (WebUtils.isNotUserName(uname)) { uname = StringUtils.EMPTY; } model.addAttribute("title", title); model.addAttribute("visitor", visitor); model.addAttribute("msgs", msgs); model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("uname", uname); return "views/pm_sent"; } @PostMapping("/pm/send") public String doPostPM(@RequestParam(name = "uname", required = false) String unameParam, @RequestParam String body) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0 || visitor.isBanned()) { throw new HttpForbiddenException(); } String uname = unameParam; if (uname.startsWith("@")) { uname = uname.substring(1); } int uid = 0; if (WebUtils.isUserName(uname)) { uid = userService.getUIDbyName(uname); } if (uid == 0 || body.length() > 10240) { throw new HttpBadRequestException(); } if (userService.isInBLAny(uid, visitor.getUid())) { throw new HttpForbiddenException(); } if (pmQueriesService.createPM(visitor.getUid(), uid, body)) { if (webApp.getXmpp() != null) { Message msg = new Message(); msg.setFrom(Jid.of("juick@juick.com")); msg.setTo(Jid.of(String.format("%d@push.juick.com", uid))); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); msg.addExtension(jmsg); webApp.getXmpp().send(msg); msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid))); webApp.getXmpp().send(msg); List jids = userService.getJIDsbyUID(uid); for (String jid : jids) { Message mm = new Message(); mm.setTo(Jid.of(jid)); mm.setType(Message.Type.CHAT); if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) { mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); mm.setBody(body); } else { mm.setFrom(Jid.of("juick", "juick.com", "Juick")); mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); } webApp.getXmpp().send(mm); } } else { logger.warn("XMPP unavailable"); } return "redirect:/pm/sent"; } else { throw new HttpBadRequestException(); } } }