aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-02-17 22:13:12 +0300
committerGravatar Vitaly Takmazov2018-02-17 22:13:12 +0300
commitdd8a51e0f342c183116d8b2af3f12394b0ac53dd (patch)
treeea88db8f0c79256f9527d1220023f2ba5d24994d /juick-server/src/main/java/com/juick/server
parentae265f84efc37619411fb21d92b51183a4744478 (diff)
server: merge xmpp
Diffstat (limited to 'juick-server/src/main/java/com/juick/server')
-rw-r--r--juick-server/src/main/java/com/juick/server/CleaningUp.java60
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPBot.java710
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPConnection.java412
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPServer.java407
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Index.java14
-rw-r--r--juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java30
6 files changed, 1632 insertions, 1 deletions
diff --git a/juick-server/src/main/java/com/juick/server/CleaningUp.java b/juick-server/src/main/java/com/juick/server/CleaningUp.java
new file mode 100644
index 00000000..6acd4bba
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/CleaningUp.java
@@ -0,0 +1,60 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.server;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.inject.Inject;
+import java.time.Duration;
+import java.time.Instant;
+
+/**
+ *
+ * @author ugnich
+ */
+@Component
+public class CleaningUp {
+
+ private static final Logger logger = LoggerFactory.getLogger(CleaningUp.class);
+
+ private static final int TIMEOUT_MINUTES = 15;
+
+ @Inject
+ XMPPServer xmpp;
+
+ @Scheduled(fixedDelay = 10000)
+ public void cleanUp() {
+ Instant now = Instant.now();
+ xmpp.getOutConnections().keySet().stream().filter(c -> Duration.between(now, c.getUpdated()).toMinutes() > TIMEOUT_MINUTES)
+ .forEach(c -> {
+ logger.info("closing idle outgoing connection to {}", c.to);
+ c.logoff();
+ xmpp.getOutConnections().remove(c);
+ });
+
+ xmpp.getInConnections().stream().filter(c -> Duration.between(now, c.updated).toMinutes() > TIMEOUT_MINUTES)
+ .forEach(c -> {
+ logger.info("closing idle incoming connection from {}", c.from);
+ c.closeConnection();
+ xmpp.getInConnections().remove(c);
+ });
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java
new file mode 100644
index 00000000..21138607
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java
@@ -0,0 +1,710 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.server;
+
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.xmpp.s2s.StanzaListener;
+import com.juick.formatters.PlainTextFormatter;
+import com.juick.server.helpers.TagStats;
+import com.juick.server.protocol.annotation.UserCommand;
+import com.juick.service.*;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+import org.ocpsoft.prettytime.PrettyTime;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.stanza.model.*;
+import rocks.xmpp.core.stanza.model.client.ClientMessage;
+import rocks.xmpp.core.stanza.model.client.ClientPresence;
+import rocks.xmpp.core.stanza.model.errors.Condition;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ *
+ * @author ugnich
+ */
+@Component
+public class XMPPBot implements StanzaListener, AutoCloseable {
+
+ private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class);
+
+ @Inject
+ private XMPPServer xmpp;
+ @Inject
+ private XMPPConnection router;
+ @Value("${xmppbot_jid}")
+ private Jid jid;
+
+ private PrettyTime pt;
+
+ @Inject
+ private MessagesService messagesService;
+ @Inject
+ private UserService userService;
+ @Inject
+ private TagService tagService;
+ @Inject
+ private PMQueriesService pmQueriesService;
+ @Inject
+ private ShowQueriesService showQueriesService;
+ @Inject
+ private PrivacyQueriesService privacyQueriesService;
+ @Inject
+ private SubscriptionService subscriptionService;
+
+ @PostConstruct
+ public void init() {
+ xmpp.addStanzaListener(this);
+ broadcastPresence(null);
+ pt = new PrettyTime(new Locale("ru"));
+ }
+
+ public Jid getJid() {
+ return jid;
+ }
+
+ public boolean incomingPresence(Presence p) {
+ final String username = p.getTo().getLocal();
+ final boolean toJuick = username.equals(jid.getLocal());
+
+ if (p.getType() == null) {
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo().asBareJid());
+ reply.setTo(p.getFrom().asBareJid());
+ reply.setType(Presence.Type.UNSUBSCRIBE);
+ xmpp.sendOut(ClientPresence.from(reply));
+ return true;
+ } else if (p.getType().equals(Presence.Type.PROBE)) {
+ int uid_to = 0;
+ if (!toJuick) {
+ uid_to = userService.getUIDbyName(username);
+ }
+
+ if (toJuick || uid_to > 0) {
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo().withResource(jid.getResource()));
+ reply.setTo(p.getFrom());
+ reply.setPriority((byte)10);
+ if (!userService.getActiveJIDs().contains(p.getFrom().asBareJid().toEscapedString())) {
+ reply.setStatus("Send ON to enable notifications");
+ }
+ xmpp.sendOut(ClientPresence.from(reply));
+ } else {
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo());
+ reply.setTo(p.getFrom());
+ reply.setType(Presence.Type.ERROR);
+ reply.setId(p.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
+ xmpp.sendOut(ClientPresence.from(reply));
+ return true;
+ }
+ return true;
+ } else if (p.getType().equals(Presence.Type.SUBSCRIBE)) {
+ boolean canSubscribe = false;
+ if (toJuick) {
+ canSubscribe = true;
+ } else {
+ int uid_to = userService.getUIDbyName(username);
+ if (uid_to > 0) {
+ pmQueriesService.addPMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString());
+ canSubscribe = true;
+ }
+ }
+
+ if (canSubscribe) {
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo());
+ reply.setTo(p.getFrom());
+ reply.setType(Presence.Type.SUBSCRIBED);
+ xmpp.sendOut(ClientPresence.from(reply));
+
+ reply.setFrom(reply.getFrom().withResource(jid.getResource()));
+ reply.setPriority((byte) 10);
+ reply.setType(null);
+ xmpp.sendOut(ClientPresence.from(reply));
+
+ return true;
+ } else {
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo());
+ reply.setTo(p.getFrom());
+ reply.setType(Presence.Type.ERROR);
+ reply.setId(p.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
+ xmpp.sendOut(ClientPresence.from(reply));
+ return true;
+ }
+ } else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) {
+ if (!toJuick) {
+ int uid_to = userService.getUIDbyName(username);
+ if (uid_to > 0) {
+ pmQueriesService.removePMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString());
+ }
+ }
+
+ Presence reply = new Presence();
+ reply.setFrom(p.getTo());
+ reply.setTo(p.getFrom());
+ reply.setType(Presence.Type.UNSUBSCRIBED);
+ xmpp.sendOut(ClientPresence.from(reply));
+ }
+
+ return false;
+ }
+
+ public boolean incomingMessage(Message msg) {
+ if (msg.getType() != null && msg.getType().equals(Message.Type.ERROR)) {
+ StanzaError error = msg.getError();
+ if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) {
+ // offline query is full, deactivating this jid
+ if (userService.setActiveStatusForJID(msg.getFrom().toEscapedString(), UserService.ActiveStatus.Inactive)) {
+ logger.info("{} is inactive now", msg.getFrom());
+ return true;
+ }
+ }
+ return false;
+ }
+ if (StringUtils.isBlank(msg.getBody())) {
+ return false;
+ }
+ String username = msg.getTo().getLocal();
+
+ User user_from;
+ String signuphash = StringUtils.EMPTY;
+ user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString());
+ if (user_from == null) {
+ signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString());
+ }
+
+ if (user_from == null) {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.CHAT);
+ if (username.equals(jid.getLocal())) {
+ reply.setBody("Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account.");
+ } else {
+ reply.setBody("Внимание, системное сообщение!\nВаш JabberID не обнаружен в списке доверенных. Для того, чтобы отправить сообщение пользователю " + username + "@juick.com, пожалуйста зарегистрируйте свой JabberID в системе: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nWarning, system message!\nYour JabberID is not found in our server's white list. To send a message to " + username + "@juick.com, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account.");
+ }
+ xmpp.sendOut(ClientMessage.from(reply));
+ return true;
+ }
+
+ if (username.equals(jid.getLocal())) {
+ try {
+ return incomingMessageJuick(user_from, msg);
+ } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
+ return false;
+ }
+ }
+
+ int uid_to = userService.getUIDbyName(username);
+
+ if (uid_to == 0) {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
+ xmpp.sendOut(ClientMessage.from(reply));
+ return true;
+ }
+
+ boolean success = false;
+ if (!userService.isInBLAny(uid_to, user_from.getUid())) {
+ success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
+ }
+
+ if (success) {
+ Message m = new Message();
+ m.setFrom(jid.asBareJid());
+ m.setTo(Jid.of(Integer.toString(uid_to), "push.juick.com", null));
+ com.juick.Message jmsg = new com.juick.Message();
+ jmsg.setUser(user_from);
+ jmsg.setText(msg.getBody());
+ m.addExtension(jmsg);
+ router.sendStanza(m);
+
+ m.setTo(Jid.of(Integer.toString(uid_to), "ws.juick.com", null));
+ router.sendStanza(m);
+
+ List<String> jids;
+ boolean inroster = false;
+ jids = userService.getJIDsbyUID(uid_to);
+ for (String userJid : jids) {
+ Message mm = new Message();
+ mm.setTo(Jid.of(userJid));
+ mm.setType(Message.Type.CHAT);
+ inroster = pmQueriesService.havePMinRoster(user_from.getUid(), userJid);
+ if (inroster) {
+ mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick"));
+ mm.setBody(msg.getBody());
+ } else {
+ mm.setFrom(jid);
+ mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + msg.getBody());
+ }
+ xmpp.sendOut(ClientMessage.from(mm));
+ }
+ } else {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
+ xmpp.sendOut(ClientMessage.from(reply));
+ }
+
+ return false;
+ }
+
+ public Optional<String> processCommand(User user, Jid from, String input) throws InvocationTargetException,
+ IllegalAccessException, NoSuchMethodException {
+ Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream()
+ .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(),
+ m.getAnnotation(UserCommand.class).patternFlags()).matcher(input).matches())
+ .findFirst();
+ if (cmd.isPresent()) {
+ Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(),
+ cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(input);
+ List<String> groups = new ArrayList<>();
+ while (matcher.find()) {
+ for (int i = 1; i <= matcher.groupCount(); i++) {
+ groups.add(matcher.group(i));
+ }
+ }
+ return Optional.of((String) getClass().getMethod(cmd.get().getName(), User.class, Jid.class, String[].class)
+ .invoke(this, user, from, groups.toArray(new String[groups.size()])));
+ }
+ return Optional.empty();
+ }
+ public boolean incomingMessageJuick(User user_from, Message msg) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
+ String command = msg.getBody().trim();
+ int commandlen = command.length();
+
+ // COMPATIBILITY
+ if (commandlen > 7 && command.substring(0, 3).equalsIgnoreCase("PM ")) {
+ command = command.substring(3).trim();
+ }
+
+ Optional<String> result = processCommand(user_from, msg.getFrom(), command);
+ result.ifPresent(r -> sendReply(msg.getFrom(), r));
+ return result.isPresent();
+ }
+
+ @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "PING - returns you a PONG")
+ public String commandPing(User user, Jid from, String[] input) {
+ Presence p = new Presence(from);
+ p.setFrom(jid);
+ p.setPriority((byte) 10);
+ xmpp.sendOut(ClientPresence.from(p));
+ return "PONG";
+ }
+
+ @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "HELP - returns this help message")
+ public String commandHelp(User user, Jid from, String[] input) {
+ return Arrays.stream(getClass().getDeclaredMethods())
+ .filter(m -> m.isAnnotationPresent(UserCommand.class))
+ .map(m -> m.getAnnotation(UserCommand.class).help())
+ .collect(Collectors.joining("\n"));
+ }
+
+ @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "LOGIN - log in to Juick website")
+ public String commandLogin(User user_from, Jid from, String[] input) {
+ return "http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid());
+ }
+ @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username")
+ public String commandPM(User user_from, Jid from, String... arguments) {
+ String body = arguments[1];
+ int ret = 0;
+
+ User user_to = userService.getUserByName(arguments[0]);
+ List<String> jids_to = null;
+ boolean haveInRoster = false;
+
+ if (user_to.getUid() > 0) {
+ if (!userService.isInBLAny(user_to.getUid(), user_from.getUid())) {
+ if (pmQueriesService.createPM(user_from.getUid(), user_to.getUid(), body)) {
+ jids_to = userService.getJIDsbyUID(user_to.getUid());
+ ret = 200;
+ } else {
+ ret = 500;
+ }
+ } else {
+ ret = 403;
+ }
+ } else {
+ ret = 404;
+ }
+
+ if (ret == 200) {
+ Message msg = new Message();
+ msg.setFrom(jid.asBareJid());
+ msg.setTo(Jid.of(Integer.toString(user_to.getUid()), "push.juick.com", null));
+ com.juick.Message jmsg = new com.juick.Message();
+ jmsg.setUser(user_from);
+ jmsg.setTo(user_to);
+ jmsg.setText(body);
+ msg.addExtension(jmsg);
+ router.sendStanza(msg);
+
+ msg.setTo(Jid.of(Integer.toString(user_to.getUid()), "ws.juick.com", null));
+ router.sendStanza(msg);
+
+ for (String userJid : jids_to) {
+ Message mm = new Message();
+ mm.setTo(Jid.of(userJid));
+ mm.setType(Message.Type.CHAT);
+ haveInRoster = pmQueriesService.havePMinRoster(user_from.getUid(), userJid);
+ if (haveInRoster) {
+ mm.setFrom(Jid.of(user_from.getName(), "juick.com", "Juick"));
+ mm.setBody(body);
+ } else {
+ mm.setFrom(jid);
+ mm.setBody("Private message from @" + user_from.getName() + ":\n" + body);
+ }
+ xmpp.sendOut(ClientMessage.from(mm));
+ }
+ }
+
+ if (ret == 200) {
+ return "Private message sent";
+ } else {
+ return "Error " + ret;
+ }
+ }
+ @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL - Show your blacklist")
+ public String commandBLShow(User user_from, Jid from, String... arguments) {
+ List<User> blusers = userService.getUserBLUsers(user_from.getUid());
+ List<String> bltags = tagService.getUserBLTags(user_from.getUid());
+
+ String txt = StringUtils.EMPTY;
+ if (bltags.size() > 0) {
+ for (String bltag : bltags) {
+ txt += "*" + bltag + "\n";
+ }
+
+ if (blusers.size() > 0) {
+ txt += "\n";
+ }
+ }
+ if (blusers.size() > 0) {
+ for (User bluser : blusers) {
+ txt += "@" + bluser.getName() + "\n";
+ }
+ }
+ if (txt.isEmpty()) {
+ txt = "You don't have any users or tags in your blacklist.";
+ }
+
+ return txt;
+ }
+
+ @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages")
+ public String commandLast(User user_from, Jid from, String... arguments) {
+ return "Last messages:\n"
+ + printMessages(messagesService.getAll(user_from.getUid(), 0), true);
+ }
+
+ @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
+ public String commandUsers(User user_from, Jid from, String... arguments) {
+ StringBuilder msg = new StringBuilder();
+ msg.append("Recommended blogs");
+ List<String> recommendedUsers = showQueriesService.getRecommendedUsers(user_from);
+ if (recommendedUsers.size() > 0) {
+ for (String user : recommendedUsers) {
+ msg.append("\n@").append(user);
+ }
+ } else {
+ msg.append("\nNo recommendations now. Subscribe to more blogs. ;)");
+ }
+ msg.append("\n\nTop 10 personal blogs:");
+ List<String> topUsers = showQueriesService.getTopUsers();
+ if (topUsers.size() > 0) {
+ for (String user : topUsers) {
+ msg.append("\n@").append(user);
+ }
+ } else {
+ msg.append("\nNo top users. Empty DB? ;)");
+ }
+ return msg.toString();
+ }
+ @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL @username - add @username to your blacklist")
+ public String blacklistUser(User user_from, Jid from, String... arguments) {
+ User blUser = userService.getUserByName(arguments[0]);
+ if (blUser != null) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(user_from, blUser);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
+ return "User added to your blacklist";
+ } else {
+ return "User removed from your blacklist";
+ }
+ }
+ return "User not found";
+ }
+ @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL *tag - add *tag to your blacklist")
+ public String blacklistTag(User user_from, Jid from, String... arguments) {
+ User blUser = userService.getUserByName(arguments[0]);
+ if (blUser != null) {
+ Tag tag = tagService.getTag(arguments[0], false);
+ if (tag != null) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(user_from, tag);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
+ return "Tag added to your blacklist";
+ } else {
+ return "Tag removed from your blacklist";
+ }
+ }
+ }
+ return "Tag not found";
+ }
+ @UserCommand(pattern = "\\*", help = "* - Show your tags")
+ public String commandTags(User currentUser, Jid from, String... args) {
+ List<TagStats> tags = tagService.getUserTagStats(currentUser.getUid());
+ String msg = "Your tags: (tag - messages)\n" +
+ tags.stream()
+ .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining());
+ return msg;
+ }
+ @UserCommand(pattern = "S", help = "S - Show your subscriptions")
+ public String commandSubscriptions(User currentUser, Jid from, String... args) {
+ List<User> friends = userService.getUserFriends(currentUser.getUid());
+ List<String> tags = subscriptionService.getSubscribedTags(currentUser);
+ String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName())
+ .collect(Collectors.joining())
+ : "You are not subscribed to any user.";
+ msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t)
+ .collect(Collectors.joining())
+ : "\nYou are not subscribed to any tag.";
+ return msg;
+ }
+ @UserCommand(pattern = "!", help = "! - Show your favorite messages")
+ public String commandFavorites(User currentUser, Jid from, String... args) {
+ List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
+ if (mids.size() > 0) {
+ return "Favorite messages: \n" + printMessages(mids, false);
+ }
+ return "No favorite messages, try to \"like\" something ;)";
+ }
+ // TODO: target notification
+ @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @username - subscribe to user" +
+ "\nU @username - unsubscribe from user", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeUser(User user, Jid from, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ User toUser = userService.getUserByName(args[1]);
+ if (subscribe) {
+ if (subscriptionService.subscribeUser(user, toUser)) {
+ return "Subscribed to @" + toUser.getName();
+ }
+ } else {
+ if (subscriptionService.unSubscribeUser(user, toUser)) {
+ return "Unsubscribed from @" + toUser.getName();
+ }
+ return "You was not subscribed to @" + toUser.getName();
+ }
+ return "Error";
+ }
+ @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" +
+ "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeTag(User user, Jid from, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ Tag tag = tagService.getTag(args[1], true);
+ if (subscribe) {
+ if (subscriptionService.subscribeTag(user, tag)) {
+ return "Subscribed";
+ }
+ } else {
+ if (subscriptionService.unSubscribeTag(user, tag)) {
+ return "Unsubscribed from " + tag.getName();
+ }
+ return "You was not subscribed to " + tag.getName();
+ }
+ return "Error";
+ }
+ @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments" +
+ "\nU #1234 - unsubscribe from comments", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeMessage(User user, Jid from, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ int mid = NumberUtils.toInt(args[1], 0);
+ if (messagesService.getMessage(mid) != null) {
+ if (subscribe) {
+ if (subscriptionService.subscribeMessage(mid, user.getUid())) {
+ return "Subscribed";
+ }
+ } else {
+ if (subscriptionService.unSubscribeMessage(mid, user.getUid())) {
+ return "Unsubscribed from #" + mid;
+ }
+ return "You was not subscribed to #" + mid;
+ }
+ }
+ return "Error";
+ }
+ @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "ON/OFF - Enable/disable subscriptions delivery")
+ public String commandOnOff(User user, Jid from, String[] input) {
+ UserService.ActiveStatus newStatus;
+ String retValUpdated;
+ if (input[0].toLowerCase().equals("on")) {
+ newStatus = UserService.ActiveStatus.Active;
+ retValUpdated = "Notifications are activated for " + from.asBareJid().toEscapedString();
+ } else {
+ newStatus = UserService.ActiveStatus.Inactive;
+ retValUpdated = "Notifications are disabled for " + from.asBareJid().toEscapedString();
+ }
+
+ if (userService.setActiveStatusForJID(from.asBareJid().toEscapedString(), newStatus)) {
+ return retValUpdated;
+ } else {
+ return String.format("Subscriptions status for %s was not changed", from.toEscapedString());
+ }
+ }
+ @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
+ help = "@username+ - Show user's info and last 20 messages")
+ public String commandUser(User user, Jid from, String... arguments) {
+ User blogUser = userService.getUserByName(arguments[0]);
+ int page = arguments[1].length();
+ if (blogUser.getUid() > 0) {
+ List<Integer> mids = messagesService.getUserBlog(blogUser.getUid(), 0, 0);
+ return String.format("Last messages from @%s:\n%s", arguments[0],
+ printMessages(mids, false));
+ }
+ return "User not found";
+ }
+ @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)")
+ public String commandShow(User user, Jid from, String... arguments) {
+ boolean showReplies = arguments[1].length() > 0;
+ int mid = NumberUtils.toInt(arguments[0], 0);
+ if (mid == 0) {
+ return "Error";
+ }
+ com.juick.Message msg = messagesService.getMessage(mid);
+ if (msg != null) {
+ if (showReplies) {
+ List<com.juick.Message> replies = messagesService.getReplies(mid);
+ replies.add(0, msg);
+ return String.join("\n",
+ replies.stream().map(PlainTextFormatter::formatPostSummary).collect(Collectors.toList()));
+ }
+ return PlainTextFormatter.formatPostSummary(msg);
+ }
+ return "Message not found";
+ }
+ @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandDeletePost(User user, Jid from, String... args) {
+ int mid = Integer.valueOf(args[0]);
+ if (messagesService.deleteMessage(user.getUid(), mid)) {
+ return "Message deleted";
+ }
+ return "This is not your message";
+ }
+ @UserCommand(pattern = "^D #(\\d+)(\\.|\\-|\\/)(\\d+)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandDeleteReply(User user, Jid from, String... args) {
+ int mid = Integer.valueOf(args[0]);
+ int rid = Integer.valueOf(args[2]);
+ if (messagesService.deleteReply(user.getUid(), mid, rid)) {
+ return "Reply deleted";
+ } else {
+ return "This is not your reply";
+ }
+ }
+ /*
+ @UserCommand(pattern = "(^(D L|DL|D LAST)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandDeleteLast(User user, Jid from, String... args) {
+
+ }
+ */
+ void sendReply(Jid jidTo, String txt) {
+ Message reply = new Message();
+ reply.setFrom(jid);
+ reply.setTo(jidTo);
+ reply.setType(Message.Type.CHAT);
+ reply.setBody(txt);
+ xmpp.sendOut(ClientMessage.from(reply));
+ }
+
+ void sendNotification(Stanza stanza) {
+ xmpp.sendOut(stanza);
+ }
+
+ @Override
+ public void stanzaReceived(Stanza xmlValue) {
+ if (xmlValue instanceof Presence) {
+ Presence p = (Presence) xmlValue;
+ if (p.getType() == null || !p.getType().equals(Presence.Type.ERROR)) {
+ incomingPresence(p);
+ }
+ } else if (xmlValue instanceof Message) {
+ Message msg = (Message) xmlValue;
+ if (!incomingMessage(msg)) {
+ router.sendStanza(msg);
+ }
+ } else if (xmlValue instanceof IQ) {
+ IQ iq = (IQ) xmlValue;
+ router.sendStanza(iq);
+ }
+ }
+
+ String printMessages(List<Integer> mids, boolean crop) {
+ return messagesService.getMessages(mids).stream()
+ .sorted(Collections.reverseOrder())
+ .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n"));
+ }
+
+ void broadcastPresence(Presence.Type type) {
+ Presence presence = new Presence();
+ presence.setFrom(jid);
+ if (type != null) {
+ presence.setType(type);
+ }
+ userService.getActiveJIDs().forEach(j -> {
+ try {
+ presence.setTo(Jid.of(j));
+ xmpp.sendOut(ClientPresence.from(presence));
+ } catch (IllegalArgumentException ex) {
+ logger.warn("Invalid jid: {}", j, ex);
+ }
+ });
+ }
+
+ @Override
+ public void close() throws Exception {
+ broadcastPresence(Presence.Type.UNAVAILABLE);
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
new file mode 100644
index 00000000..f5448880
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
@@ -0,0 +1,412 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.server;
+
+import com.juick.Attachment;
+import com.juick.User;
+import com.juick.xmpp.s2s.BasicXmppSession;
+import com.juick.server.helpers.UserInfo;
+import com.juick.service.MessagesService;
+import com.juick.service.SubscriptionService;
+import com.juick.service.UserService;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.XmppException;
+import rocks.xmpp.core.stanza.AbstractIQHandler;
+import rocks.xmpp.core.stanza.model.IQ;
+import rocks.xmpp.core.stanza.model.Message;
+import rocks.xmpp.core.stanza.model.Stanza;
+import rocks.xmpp.core.stanza.model.client.ClientMessage;
+import rocks.xmpp.core.stanza.model.errors.Condition;
+import rocks.xmpp.extensions.caps.model.EntityCapabilities;
+import rocks.xmpp.extensions.component.accept.ExternalComponent;
+import rocks.xmpp.extensions.filetransfer.FileTransfer;
+import rocks.xmpp.extensions.filetransfer.FileTransferManager;
+import rocks.xmpp.extensions.nick.model.Nickname;
+import rocks.xmpp.extensions.oob.model.x.OobX;
+import rocks.xmpp.extensions.ping.PingManager;
+import rocks.xmpp.extensions.vcard.temp.model.VCard;
+import rocks.xmpp.extensions.version.SoftwareVersionManager;
+import rocks.xmpp.extensions.version.model.SoftwareVersion;
+import rocks.xmpp.util.XmppUtils;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.xml.bind.JAXBException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * @author ugnich
+ */
+@Component
+public class XMPPConnection implements AutoCloseable {
+
+ private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class);
+
+ private ExternalComponent router;
+ @Inject
+ private XMPPBot bot;
+
+ @Value("${componentname:localhost}")
+ private String componentName;
+ @Value("${component_port:5347}")
+ private int componentPort;
+ @Value("${xmpp_password:secret}")
+ private String password;
+ @Value("${upload_tmp_dir:/tmp}")
+ private String tmpDir;
+
+ @Inject
+ public MessagesService messagesService;
+ @Inject
+ public UserService userService;
+ @Inject
+ public SubscriptionService subscriptionService;
+ @Inject
+ private BasicXmppSession session;
+ @Inject
+ private ExecutorService service;
+
+ @PostConstruct
+ public void init() {
+ logger.info("stream router start connecting to {}", componentPort);
+ router = ExternalComponent.create(componentName, password, session.getConfiguration(), "localhost", componentPort);
+ PingManager pingManager = router.getManager(PingManager.class);
+ pingManager.setEnabled(true);
+ router.disableFeature(EntityCapabilities.NAMESPACE);
+ SoftwareVersionManager softwareVersionManager = router.getManager(SoftwareVersionManager.class);
+ softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Juick", "2.x", System.getProperty("os.name", "generic")));
+ VCard vCard = new VCard();
+ vCard.setFormattedName("Juick");
+ try {
+ vCard.setUrl(new URL("http://juick.com/"));
+ vCard.setPhoto(new VCard.Image("image/png", IOUtils.toByteArray(getClass().getClassLoader().getResource("juick.png"))));
+ } catch (MalformedURLException e) {
+ logger.error("invalid url", e);
+ } catch (IOException e) {
+ logger.warn("invalid resource", e);
+ }
+ router.addIQHandler(VCard.class, new AbstractIQHandler(IQ.Type.GET) {
+ @Override
+ protected IQ processRequest(IQ iq) {
+ if (iq.getTo().equals(bot.getJid()) || iq.getTo().asBareJid().equals(bot.getJid().asBareJid())
+ || iq.getTo().asBareJid().toEscapedString().equals(bot.getJid().getDomain())) {
+ return iq.createResult(vCard);
+ }
+ User user = userService.getUserByName(iq.getTo().getLocal());
+ if (user.getUid() > 0) {
+ UserInfo info = userService.getUserInfo(user);
+ VCard userVCard = new VCard();
+ userVCard.setFormattedName(info.getFullName());
+ userVCard.setNickname(user.getName());
+ try {
+ userVCard.setPhoto(new VCard.Image(new URI("http://i.juick.com/a/" + user.getUid() + ".png")));
+ if (info.getUrl() != null) {
+ userVCard.setUrl(new URL(info.getUrl()));
+ }
+ } catch (MalformedURLException | URISyntaxException e) {
+ logger.warn("url exception", e);
+ }
+ return iq.createResult(userVCard);
+ }
+ return iq.createError(Condition.BAD_REQUEST);
+ }
+ });
+ router.addInboundMessageListener(e -> {
+ Message message = e.getMessage();
+ Jid jid = message.getTo();
+ if (jid.getDomain().equals(router.getDomain().toEscapedString())) {
+ com.juick.Message jmsg = message.getExtension(com.juick.Message.class);
+ if (jmsg != null) {
+ if (jid.getLocal().equals("recomm")) {
+ sendJuickRecommendation(jmsg);
+ } else {
+ if (jmsg.getRid() > 0) {
+ sendJuickComment(jmsg);
+ } else if (jmsg.getMid() > 0) {
+ sendJuickMessage(jmsg);
+ }
+ }
+ }
+ } else if (jid.getDomain().endsWith(bot.getJid().getDomain()) && (jid.getDomain().equals(bot.getJid().getDomain())
+ || jid.getDomain().endsWith("." + bot.getJid().getDomain()))) {
+ if (logger.isInfoEnabled()) {
+ try {
+ logger.info("unhandled message: {}", stanzaToString(message));
+ } catch (JAXBException | XMLStreamException ex) {
+ logger.error("JAXB exception", ex);
+ }
+ }
+ } else {
+ route(ClientMessage.from(message));
+ }
+ });
+ router.addInboundIQListener(e -> {
+ IQ iq = e.getIQ();
+ Jid jid = iq.getTo();
+ if (!jid.getDomain().equals(bot.getJid().getDomain())) {
+ route(iq);
+ }
+ });
+ FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class);
+ fileTransferManager.addFileTransferOfferListener(e -> {
+ try {
+ List<String> allowedTypes = new ArrayList<String>() {{
+ add("png");
+ add("jpg");
+ }};
+ String attachmentExtension = FilenameUtils.getExtension(e.getName()).toLowerCase();
+ String targetFilename = String.format("%s.%s",
+ DigestUtils.md5Hex(String.format("%s-%s",
+ e.getInitiator().toString(), e.getSessionId()).getBytes()), attachmentExtension);
+ if (allowedTypes.contains(attachmentExtension)) {
+ Path filePath = Paths.get(tmpDir, targetFilename);
+ FileTransfer ft = e.accept(filePath).get();
+ ft.addFileTransferStatusListener(st -> {
+ logger.debug("{}: received {} of {}", e.getName(), st.getBytesTransferred(), e.getSize());
+ if (st.getStatus().equals(FileTransfer.Status.COMPLETED)) {
+ logger.info("transfer completed");
+ Message msg = new Message();
+ msg.setType(Message.Type.CHAT);
+ msg.setFrom(e.getInitiator());
+ msg.setTo(bot.getJid());
+ msg.setBody(e.getDescription());
+ try {
+ String attachmentUrl = String.format("juick://%s", targetFilename);
+ msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!"));
+ router.sendMessage(msg);
+ } catch (URISyntaxException e1) {
+ logger.warn("attachment error", e1);
+ }
+ } else if (st.getStatus().equals(FileTransfer.Status.FAILED)) {
+ logger.info("transfer failed", ft.getException());
+ Message msg = new Message();
+ msg.setType(Message.Type.CHAT);
+ msg.setFrom(bot.getJid());
+ msg.setTo(e.getInitiator());
+ msg.setBody("File transfer failed, please report to us");
+ router.sendMessage(msg);
+ } else if (st.getStatus().equals(FileTransfer.Status.CANCELED)) {
+ logger.info("transfer cancelled");
+ }
+ });
+ ft.transfer();
+ logger.info("transfer started");
+ } else {
+ e.reject();
+ logger.info("transfer rejected");
+ }
+ } catch (IOException | InterruptedException | ExecutionException e1) {
+ logger.error("ft error", e1);
+ }
+ });
+ router.addConnectionListener(event -> {
+ if (event.getType().equals(rocks.xmpp.core.session.ConnectionEvent.Type.RECONNECTION_SUCCEEDED)) {
+ logger.info("component connected");
+ }
+ });
+ service.submit(() -> {
+ try {
+ Thread.sleep(3000);
+ router.connect();
+ } catch (InterruptedException | XmppException e) {
+ logger.warn("xmpp exception", e);
+ }
+ });
+ }
+
+ String stanzaToString(Stanza stanza) throws XMLStreamException, JAXBException {
+ StringWriter stanzaWriter = new StringWriter();
+ XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter(
+ router.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(stanzaWriter));
+ router.createMarshaller().marshal(stanza, xmppStreamWriter);
+ xmppStreamWriter.flush();
+ xmppStreamWriter.close();
+ return stanzaWriter.toString();
+ }
+
+ void route(Stanza stanza) {
+ try {
+ String xml = stanzaToString(stanza);
+ logger.info("stream router (out): {}", xml);
+ bot.sendNotification(stanza);
+ } catch (XMLStreamException | JAXBException e) {
+ logger.error("JAXB exception", e);
+ }
+
+ }
+
+ public void sendStanza(Stanza xml) {
+ router.send(xml);
+ }
+
+
+
+ public void sendJuickMessage(com.juick.Message jmsg) {
+ List<String> jids = new ArrayList<>();
+
+ if (jmsg.FriendsOnly) {
+ jids = subscriptionService.getJIDSubscribedToUser(jmsg.getUser().getUid(), jmsg.FriendsOnly);
+ } else {
+ List<User> users = subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid());
+ for (User user : users) {
+ for (String jid : userService.getJIDsbyUID(user.getUid())) {
+ jids.add(jid);
+ }
+ }
+ }
+ com.juick.Message fullMsg = messagesService.getMessage(jmsg.getMid());
+ String txt = "@" + jmsg.getUser().getName() + ":" + fullMsg.getTagsString() + "\n";
+ Attachment attachment = fullMsg.getAttachment();
+ if (attachment != null) {
+ txt += attachment.getMedium().getUrl() + "\n";
+ }
+ txt += StringUtils.defaultString(jmsg.getText()) + "\n\n";
+ txt += "#" + jmsg.getMid() + " http://juick.com/" + jmsg.getMid();
+
+ Nickname nick = new Nickname("@" + jmsg.getUser().getName());
+
+ Message msg = new Message();
+ msg.setFrom(bot.getJid());
+ msg.setBody(txt);
+ msg.setType(Message.Type.CHAT);
+ msg.setThread("juick-" + jmsg.getMid());
+ msg.addExtension(jmsg);
+ msg.addExtension(nick);
+ if (attachment != null) {
+ try {
+ OobX oob = new OobX(new URI(attachment.getMedium().getUrl()));
+ msg.addExtension(oob);
+ } catch (URISyntaxException e) {
+ logger.warn("uri exception", e);
+ }
+ }
+ for (String jid : jids) {
+ msg.setTo(Jid.of(jid));
+ route(ClientMessage.from(msg));
+ }
+ }
+
+ public void sendJuickComment(com.juick.Message jmsg) {
+ List<User> users;
+ String replyQuote;
+ String replyTo;
+
+ users = subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid());
+ com.juick.Message replyMessage = jmsg.getReplyto() > 0 ? messagesService.getReply(jmsg.getMid(), jmsg.getReplyto())
+ : messagesService.getMessage(jmsg.getMid());
+ replyTo = replyMessage.getUser().getName();
+ com.juick.Message fullReply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
+ replyQuote = fullReply.getReplyQuote();
+
+ String txt = "Reply by @" + jmsg.getUser().getName() + ":\n" + replyQuote + "\n@" + replyTo + " ";
+ Attachment attachment = jmsg.getAttachment();
+ if (attachment != null) {
+ txt += attachment.getMedium().getUrl() + "\n";
+ }
+ txt += StringUtils.defaultString(jmsg.getText()) + "\n\n" + "#" + jmsg.getMid() + "/" + jmsg.getRid() + " http://juick.com/" + jmsg.getMid() + "#" + jmsg.getRid();
+
+ Message msg = new Message();
+ msg.setFrom(bot.getJid());
+ msg.setBody(txt);
+ msg.setType(Message.Type.CHAT);
+ msg.addExtension(jmsg);
+ for (User user : users) {
+ for (String jid : userService.getJIDsbyUID(user.getUid())) {
+ msg.setTo(Jid.of(jid));
+ route(ClientMessage.from(msg));
+ }
+ }
+ }
+
+ public void sendJuickRecommendation(com.juick.Message recomm) {
+ List<User> users;
+ com.juick.Message jmsg = messagesService.getMessage(recomm.getMid());
+ users = subscriptionService.getUsersSubscribedToUserRecommendations(recomm.getUser().getUid(),
+ recomm.getMid(), jmsg.getUser().getUid());
+
+ String txt = "Recommended by @" + recomm.getUser().getName() + ":\n";
+ txt += "@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n";
+ Attachment attachment = jmsg.getAttachment();
+ if (attachment != null) {
+ txt += attachment.getMedium().getUrl() + "\n";
+ }
+ txt += StringUtils.defaultString(jmsg.getText()) + "\n\n";
+ txt += "#" + jmsg.getMid();
+ if (jmsg.getReplies() > 0) {
+ if (jmsg.getReplies() % 10 == 1 && jmsg.getReplies() % 100 != 11) {
+ txt += " (" + jmsg.getReplies() + " reply)";
+ } else {
+ txt += " (" + jmsg.getReplies() + " replies)";
+ }
+ }
+ txt += " http://juick.com/" + jmsg.getMid();
+
+ Nickname nick = new Nickname("@" + jmsg.getUser().getName());
+
+ Message msg = new Message();
+ msg.setFrom(bot.getJid());
+ msg.setBody(txt);
+ msg.setType(Message.Type.CHAT);
+ msg.setThread("juick-" + jmsg.getMid());
+ msg.addExtension(jmsg);
+ msg.addExtension(nick);
+ if (attachment != null) {
+ try {
+ OobX oob = new OobX(new URI(attachment.getMedium().getUrl()));
+ msg.addExtension(oob);
+ } catch (URISyntaxException e) {
+ logger.warn("uri exception", e);
+ }
+ }
+
+ for (User user : users) {
+ for (String jid : userService.getJIDsbyUID(user.getUid())) {
+ msg.setTo(Jid.of(jid));
+ route(ClientMessage.from(msg));
+ }
+ }
+ }
+
+ @Override
+ public void close() throws Exception {
+ if (router != null) {
+ router.close();
+ }
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/XMPPServer.java b/juick-server/src/main/java/com/juick/server/XMPPServer.java
new file mode 100644
index 00000000..01437232
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/XMPPServer.java
@@ -0,0 +1,407 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.server;
+
+import com.juick.xmpp.s2s.*;
+import com.juick.service.UserService;
+import com.juick.xmpp.extensions.JuickMessage;
+import com.juick.xmpp.extensions.StreamError;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.xmlpull.v1.XmlPullParserException;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.stanza.model.Stanza;
+import rocks.xmpp.util.XmppUtils;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.net.ssl.*;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.*;
+import java.net.*;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.SecureRandom;
+import java.time.Instant;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * @author ugnich
+ */
+@Component
+public class XMPPServer implements ConnectionListener, AutoCloseable {
+ private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class);
+
+ @Inject
+ public ExecutorService service;
+ @Value("${hostname}")
+ private Jid jid;
+ @Value("${s2s_port:5269}")
+ private int s2sPort;
+ @Value("${keystore}")
+ public String keystore;
+ @Value("${keystore_password}")
+ public String keystorePassword;
+ @Value("${broken_ssl_hosts}")
+ public String[] brokenSSLhosts;
+ @Value("${banned_hosts}")
+ public String[] bannedHosts;
+
+ private final List<ConnectionIn> inConnections = new CopyOnWriteArrayList<>();
+ private final Map<ConnectionOut, Optional<Socket>> outConnections = new ConcurrentHashMap<>();
+ private final List<CacheEntry> outCache = new CopyOnWriteArrayList<>();
+ private final List<StanzaListener> stanzaListeners = new CopyOnWriteArrayList<>();
+ private final AtomicBoolean closeFlag = new AtomicBoolean(false);
+
+ SSLContext sc;
+ private TrustManager[] trustAllCerts = new TrustManager[]{
+ new X509TrustManager() {
+ public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ }
+
+ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ }
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+ }
+ };
+
+
+ private ServerSocket listener;
+
+ @Inject
+ private BasicXmppSession session;
+ @Inject
+ private UserService userService;
+
+ @PostConstruct
+ public void init() throws KeyStoreException {
+ closeFlag.set(false);
+ KeyStore ks = KeyStore.getInstance("JKS");
+ try (InputStream ksIs = new FileInputStream(keystore)) {
+ ks.load(ksIs, keystorePassword.toCharArray());
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory
+ .getDefaultAlgorithm());
+ kmf.init(ks, keystorePassword.toCharArray());
+ sc = SSLContext.getInstance("TLSv1.2");
+ sc.init(kmf.getKeyManagers(), trustAllCerts, new SecureRandom());
+ } catch (Exception e) {
+ logger.warn("tls unavailable");
+ }
+ service.submit(() -> {
+ try {
+ listener = new ServerSocket(s2sPort);
+ logger.info("s2s listener ready");
+ while (!listener.isClosed()) {
+ if (Thread.currentThread().isInterrupted()) break;
+ Socket socket = listener.accept();
+ ConnectionIn client = new ConnectionIn(this, socket);
+ addConnectionIn(client);
+ service.submit(client);
+ }
+ } catch (SocketException e) {
+ // shutdown
+ } catch (IOException | XmlPullParserException e) {
+ logger.warn("xmpp exception", e);
+ }
+ });
+ }
+
+ @Override
+ public void close() throws Exception {
+ if (!listener.isClosed()) {
+ listener.close();
+ }
+ outConnections.forEach((c, s) -> {
+ c.logoff();
+ outConnections.remove(c);
+ });
+ inConnections.forEach(c -> {
+ c.closeConnection();
+ inConnections.remove(c);
+ });
+ if (!listener.isClosed()) {
+ listener.close();
+ }
+ service.shutdown();
+ logger.info("XMPP server destroyed");
+ }
+
+ public void addConnectionIn(ConnectionIn c) {
+ c.setListener(this);
+ inConnections.add(c);
+ }
+
+ public void addConnectionOut(ConnectionOut c, Optional<Socket> socket) {
+ c.setListener(this);
+ outConnections.put(c, socket);
+ }
+
+ public void removeConnectionIn(ConnectionIn c) {
+ inConnections.remove(c);
+ }
+
+ public void removeConnectionOut(ConnectionOut c) {
+ outConnections.remove(c);
+ }
+
+ public String getFromCache(Jid to) {
+ final String[] cache = new String[1];
+ outCache.stream().filter(c -> c.hostname != null && c.hostname.equals(to)).findFirst().ifPresent(c -> {
+ cache[0] = c.xml;
+ outCache.remove(c);
+ });
+ return cache[0];
+ }
+
+ public Optional<ConnectionOut> getConnectionOut(Jid hostname, boolean needReady) {
+ return outConnections.keySet().stream().filter(c -> c.to != null &&
+ c.to.equals(hostname) && (!needReady || c.streamReady)).findFirst();
+ }
+
+ public Optional<ConnectionIn> getConnectionIn(String streamID) {
+ return inConnections.stream().filter(c -> c.streamID != null && c.streamID.equals(streamID)).findFirst();
+ }
+
+ public void sendOut(Stanza s) {
+ try {
+ StringWriter stanzaWriter = new StringWriter();
+ XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter(
+ session.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(stanzaWriter));
+ session.createMarshaller().marshal(s, xmppStreamWriter);
+ xmppStreamWriter.flush();
+ xmppStreamWriter.close();
+ String xml = stanzaWriter.toString();
+ logger.info("s2s (out): {}", xml);
+ sendOut(Jid.of(s.getTo().getDomain()), xml);
+ } catch (XMLStreamException | JAXBException e1) {
+ logger.info("jaxb exception", e1);
+ }
+ }
+
+ public void sendOut(Jid hostname, String xml) {
+ boolean haveAnyConn = false;
+
+ ConnectionOut connOut = null;
+ for (ConnectionOut c : outConnections.keySet()) {
+ if (c.to != null && c.to.equals(hostname)) {
+ if (c.streamReady) {
+ connOut = c;
+ break;
+ } else {
+ haveAnyConn = true;
+ break;
+ }
+ }
+ }
+ if (connOut != null) {
+ connOut.send(xml);
+ return;
+ }
+
+ boolean haveCache = false;
+ for (CacheEntry c : outCache) {
+ if (c.hostname != null && c.hostname.equals(hostname)) {
+ c.xml += xml;
+ c.updated = Instant.now();
+ haveCache = true;
+ break;
+ }
+ }
+ if (!haveCache) {
+ outCache.add(new CacheEntry(hostname, xml));
+ }
+
+ if (!haveAnyConn && !closeFlag.get()) {
+ try {
+ createDialbackConnection(hostname.toEscapedString(), null, null);
+ } catch (Exception e) {
+ logger.warn("dialback error", e);
+ }
+ }
+ }
+
+ void createDialbackConnection(String to, String checkSID, String dbKey) throws Exception {
+ ConnectionOut connectionOut = new ConnectionOut(getJid(), Jid.of(to), null, null, checkSID, dbKey);
+ addConnectionOut(connectionOut, Optional.empty());
+ service.submit(() -> {
+ try {
+ Socket socket = new Socket();
+ socket.connect(DNSQueries.getServerAddress(to));
+ connectionOut.setInputStream(socket.getInputStream());
+ connectionOut.setOutputStream(socket.getOutputStream());
+ addConnectionOut(connectionOut, Optional.of(socket));
+ connectionOut.addChildParser(new JuickMessage());
+ connectionOut.connect();
+ } catch (IOException e) {
+ userService.getActiveJIDs().stream().filter(j -> Jid.of(j).getDomain().equals(to))
+ .forEach(j -> {
+ userService.setActiveStatusForJID(j, UserService.ActiveStatus.Inactive);
+ logger.info("{} is inactive now", j);
+ });
+ }
+ });
+ }
+
+ public void startDialback(Jid from, String streamId, String dbKey) throws Exception {
+ Optional<ConnectionOut> c = getConnectionOut(from, false);
+ if (c.isPresent()) {
+ c.get().sendDialbackVerify(streamId, dbKey);
+ } else {
+ createDialbackConnection(from.toEscapedString(), streamId, dbKey);
+ }
+ }
+
+ public void addStanzaListener(StanzaListener listener) {
+ stanzaListeners.add(listener);
+ }
+
+ public void onStanzaReceived(String xmlValue) {
+ Stanza stanza = parse(xmlValue);
+ stanzaListeners.forEach(l -> l.stanzaReceived(stanza));
+ }
+
+ public BasicXmppSession getSession() {
+ return session;
+ }
+
+ public List<ConnectionIn> getInConnections() {
+ return inConnections;
+ }
+
+ public Map<ConnectionOut, Optional<Socket>> getOutConnections() {
+ return outConnections;
+ }
+
+ @Override
+ public void starttls(ConnectionIn connection) {
+ logger.info("stream {} securing", connection.streamID);
+ connection.sendStanza("<proceed xmlns=\"" + Connection.NS_TLS + "\" />");
+ try {
+ connection.setSocket(sc.getSocketFactory().createSocket(connection.getSocket(), connection.getSocket().getInetAddress().getHostAddress(),
+ connection.getSocket().getPort(), true));
+ ((SSLSocket) connection.getSocket()).setUseClientMode(false);
+ ((SSLSocket) connection.getSocket()).startHandshake();
+ connection.setSecured(true);
+ logger.info("stream {} secured", connection.streamID);
+ connection.restartParser();
+ } catch (XmlPullParserException | IOException sex) {
+ logger.warn("stream {} ssl error {}", connection.streamID, sex);
+ connection.sendStanza("<failed xmlns\"" + Connection.NS_TLS + "\" />");
+ removeConnectionIn(connection);
+ connection.closeConnection();
+ }
+ }
+
+ @Override
+ public void proceed(ConnectionOut connection) {
+ try {
+ Socket socket = outConnections.get(connection).get();
+ socket = sc.getSocketFactory().createSocket(socket, socket.getInetAddress().getHostAddress(),
+ socket.getPort(), true);
+ ((SSLSocket) socket).startHandshake();
+ connection.setSecured(true);
+ logger.info("stream {} secured", connection.getStreamID());
+ connection.setInputStream(socket.getInputStream());
+ connection.setOutputStream(socket.getOutputStream());
+ connection.restartStream();
+ connection.sendOpenStream();
+ } catch (NoSuchElementException | XmlPullParserException | IOException sex) {
+ logger.error("s2s ssl error: {} {}, error {}", connection.to, connection.getStreamID(), sex);
+ connection.send("<failed xmlns\"" + Connection.NS_TLS + "\" />");
+ removeConnectionOut(connection);
+ connection.logoff();
+ }
+ }
+
+ @Override
+ public void verify(ConnectionOut connection, String from, String type, String sid) {
+ if (from != null && from.equals(connection.to.toEscapedString()) && sid != null && !sid.isEmpty() && type != null) {
+ getConnectionIn(sid).ifPresent(c -> c.sendDialbackResult(Jid.of(from), type));
+ }
+ }
+
+ @Override
+ public void dialbackError(ConnectionOut connection, StreamError error) {
+ logger.warn("Stream error from {}: {}", connection.getStreamID(), error.getCondition());
+ removeConnectionOut(connection);
+ connection.logoff();
+ }
+
+ @Override
+ public void finished(ConnectionOut connection, boolean dirty) {
+ logger.warn("stream to {} {} finished, dirty={}", connection.to, connection.getStreamID(), dirty);
+ removeConnectionOut(connection);
+ connection.logoff();
+ }
+
+ @Override
+ public void exception(ConnectionOut connection, Exception ex) {
+ logger.error("s2s out exception: {} {}, exception {}", connection.to, connection.getStreamID(), ex);
+ removeConnectionOut(connection);
+ connection.logoff();
+ }
+
+ @Override
+ public void ready(ConnectionOut connection) {
+ logger.info("stream to {} {} ready", connection.to, connection.getStreamID());
+ String cache = getFromCache(connection.to);
+ if (cache != null) {
+ logger.info("stream to {} {} sending cache", connection.to, connection.getStreamID());
+ connection.send(cache);
+ }
+ }
+
+ @Override
+ public boolean securing(ConnectionOut connection) {
+ return !Arrays.asList(brokenSSLhosts).contains(connection.to.toEscapedString());
+ }
+
+ public Stanza parse(String xml) {
+ try {
+ Unmarshaller unmarshaller = session.createUnmarshaller();
+ return (Stanza)unmarshaller.unmarshal(new StringReader(xml));
+ } catch (JAXBException e) {
+ logger.error("JAXB exception", e);
+ }
+ return null;
+ }
+
+ public Jid getJid() {
+ return jid;
+ }
+ @PreDestroy
+ public void preDestroy() {
+ closeFlag.set(true);
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/api/Index.java b/juick-server/src/main/java/com/juick/server/api/Index.java
index dba8357d..b1d87f67 100644
--- a/juick-server/src/main/java/com/juick/server/api/Index.java
+++ b/juick-server/src/main/java/com/juick/server/api/Index.java
@@ -19,6 +19,8 @@ package com.juick.server.api;
import com.juick.Status;
import com.juick.server.WebsocketManager;
+import com.juick.server.XMPPServer;
+import com.juick.xmpp.helpers.XMPPStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -39,16 +41,26 @@ import java.net.URI;
public class Index {
@Inject
private WebsocketManager wsHandler;
+ @Inject
+ private XMPPServer xmpp;
@RequestMapping(value = "/", method = RequestMethod.GET, headers = "Connection!=Upgrade")
public ResponseEntity<Void> description() {
URI redirectUri = ServletUriComponentsBuilder.fromCurrentRequestUri().path("/swagger-ui.html").build().toUri();
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).location(redirectUri).build();
}
-
@RequestMapping(value = "/api/status", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE, headers = "Connection!=Upgrade")
public Status status() {
return Status.getStatus(String.valueOf(wsHandler.getClients().size()));
}
+ @RequestMapping(method = RequestMethod.GET, value = "/xmpp/status", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
+ public XMPPStatus xmppStatus() {
+ XMPPStatus status = new XMPPStatus();
+ if (xmpp != null) {
+ status.setInbound(xmpp.getInConnections());
+ status.setOutbound(xmpp.getOutConnections().keySet());
+ }
+ return status;
+ }
}
diff --git a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
index 3839248d..21c1238b 100644
--- a/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
+++ b/juick-server/src/main/java/com/juick/server/configuration/ApiAppConfiguration.java
@@ -22,8 +22,12 @@ import com.juick.server.WebsocketManager;
import com.juick.server.component.JuickServerComponent;
import com.juick.server.component.JuickServerReconnectManager;
import com.juick.service.UserService;
+import com.juick.xmpp.helpers.JidConverter;
+import com.juick.xmpp.s2s.BasicXmppSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
+import org.springframework.core.convert.ConversionService;
+import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
@@ -36,6 +40,9 @@ import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.util.UriComponentsBuilder;
+import rocks.xmpp.core.session.Extension;
+import rocks.xmpp.core.session.XmppSessionConfiguration;
+import rocks.xmpp.core.session.debug.LogbackDebugger;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
@@ -46,6 +53,8 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.Collections;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
/**
* Created by aalexeev on 11/12/16.
@@ -120,6 +129,27 @@ public class ApiAppConfiguration extends BaseWebConfiguration implements WebSock
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
+ @Value("${hostname}")
+ private String hostname;
+
+ @Bean
+ public ExecutorService service() {
+ return Executors.newCachedThreadPool();
+ }
+ @Bean
+ public BasicXmppSession session() {
+ XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
+ .extensions(Extension.of(com.juick.Message.class))
+ .debugger(LogbackDebugger.class)
+ .build();
+ return BasicXmppSession.create(hostname, configuration);
+ }
+ @Bean
+ public static ConversionService conversionService() {
+ DefaultFormattingConversionService cs = new DefaultFormattingConversionService();
+ cs.addConverter(new JidConverter());
+ return cs;
+ }
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();