aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/PrivateMessage.java
blob: 6deaed45cf9d91a9ea210a7c4e2174477cc958cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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<String> 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;
    }
}