package com.juick.command; import com.juick.User; import com.juick.service.PMQueriesService; import com.juick.service.UserService; import lombok.Getter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; import java.util.List; import java.util.regex.Pattern; /** * @author ma1uta */ @Component @Getter public class PrivateMessage extends MultiArgsCommand { private static final Pattern PATTERN = Pattern.compile("^@(\\S+)\\s+([\\s\\S]+)$", Pattern.CASE_INSENSITIVE); private final UserService userService; private final PMQueriesService pmQueriesService; @Value("${xmppbot_jid}") private Jid jid; @Autowired public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) { this.userService = userService; this.pmQueriesService = pmQueriesService; } @Override public Pattern pattern() { return PATTERN; } @Override public String help() { return "@username message - send PM to username"; } @Override protected String execute(User sender, MessageListener protocolListener, String... arguments) { String addressee = arguments[0]; String body = arguments[1]; int ret; int addresseeUid; List jids_to = null; boolean haveInRoster; addresseeUid = addressee.indexOf('@') > 0 ? getUserService().getUIDbyJID(addressee) : getUserService().getUIDbyName(addressee); if (addresseeUid > 0) { if (!getUserService().isInBLAny(addresseeUid, sender.getUid())) { if (getPmQueriesService().createPM(sender.getUid(), addresseeUid, body)) { jids_to = getUserService().getJIDsbyUID(addresseeUid); ret = 200; } else { ret = 500; } } else { ret = 403; } } else { ret = 404; } if (ret == 200) { Message msg = new Message(); msg.setFrom(getJid().asBareJid()); msg.setTo(Jid.of(Integer.toString(addresseeUid), "push.juick.com", null)); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(sender); jmsg.setText(body); msg.addExtension(jmsg); protocolListener.sendStanza(msg); msg.setTo(Jid.of(Integer.toString(addresseeUid), "ws.juick.com", null)); protocolListener.sendStanza(msg); for (String userJid : jids_to) { Message mm = new Message(); mm.setTo(Jid.of(userJid)); mm.setType(Message.Type.CHAT); haveInRoster = getPmQueriesService().havePMinRoster(sender.getUid(), userJid); if (haveInRoster) { mm.setFrom(Jid.of(sender.getName(), "juick.com", "Juick")); mm.setBody(body); } else { mm.setFrom(getJid()); mm.setBody("Private message from @" + sender.getName() + ":\n" + body); } protocolListener.sendMessage(mm); } } return ret == 200 ? "Private message sent" : "Error " + ret; } }