aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java
blob: fa09758474d3c3c77b8da52d712043a1b2141d23 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * 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.components;

import com.juick.User;
import com.juick.server.helpers.UserInfo;
import com.juick.server.protocol.JuickProtocol;
import com.juick.server.protocol.ProtocolListener;
import com.juick.service.PMQueriesService;
import com.juick.service.UserService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.XmppException;
import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
import rocks.xmpp.extensions.vcard.temp.VCardManager;
import rocks.xmpp.extensions.vcard.temp.model.VCard;
import rocks.xmpp.extensions.version.SoftwareVersionManager;
import rocks.xmpp.extensions.version.model.SoftwareVersion;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.List;

/**
 * Created by vt on 12/11/2016.
 */
public class XMPPBot implements AutoCloseable, ProtocolListener {
    private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class);
    @Inject
    private UserService userService;
    @Inject
    private PMQueriesService pmQueriesService;
    @Inject
    private JuickProtocol juickProtocol;
    @Inject
    private Environment env;

    Jid juickJid;

    private ExternalComponent component;

    @PostConstruct
    @Inject
    public void init() {
        component = ExternalComponent.create(env.getProperty("component_name", "juick.com"),
                env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"),
                NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347));
        juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick"));
        juickProtocol.setListener(this);
        try {
            SoftwareVersionManager softwareVersionManager = component.getManager(SoftwareVersionManager.class);
            softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Juick", "git", System.getProperty("os.name", "generic")));
            VCardManager vCardManager = component.getManager(VCardManager.class);
            vCardManager.setEnabled(true);
            VCard ownVCard = new VCard();
            ownVCard.setNickname("Juick");
            ownVCard.setUrl(new URL("http://juick.com/"));
            ownVCard.setPhoto(new VCard.Image("image/png", IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("vCard.png"))));
            vCardManager.setVCard(ownVCard);
            component.addInboundMessageListener(e -> {
                Message message = e.getMessage();
                if (message.getType().equals(Message.Type.ERROR) || message.getType().equals(Message.Type.GROUPCHAT)) {
                    return;
                }
                String text = message.getBody().trim();
                String command = text.toUpperCase();
                User user = userService.getUserByJID(message.getFrom().asBareJid().toString());
                if (command.equals("VCARD")) {
                    try {
                        VCard vCard = vCardManager.getVCard(message.getFrom().asBareJid()).getResult();
                        UserInfo info = new UserInfo();
                        info.setFullName(vCard.getFormattedName());
                        info.setCountry(vCard.getAddresses().get(0).getCountry());
                        info.setUrl(vCard.getUrl().toString());
                        userService.updateUserInfo(user, info);
                        component.sendMessage(new Message(message.getFrom(), Message.Type.CHAT, "vCard updated"));
                    } catch (XmppException vce) {
                        logger.warn("vcard exception", vce);
                    }
                } else {
                    try {
                        String reply = juickProtocol.getReply(user, text);
                        Message replyMessage = new Message(message.getFrom(), Message.Type.CHAT);
                        replyMessage.setBody(reply);
                        replyMessage.setFrom(juickJid);
                        component.send(replyMessage);
                    } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException ex) {
                        logger.warn("unhandled error", ex);
                    }
                }
            });
            component.connect();
        } catch (XmppException | IOException e) {
            logger.error("bot connection error", e);
        }
    }

    @Override
    public void close() throws Exception {
        if (component != null)
            component.close();

        logger.info("ExternalComponent on xmpp-bot destroyed");
    }

    @Override
    public void privateMessage(User from, User to, String body) {
        List<String> toJids = userService.getJIDsbyUID(to.getUid());
        toJids.forEach(jid -> {
            Message mm = new Message();
            mm.setTo(Jid.of(jid));
            mm.setType(Message.Type.CHAT);
            boolean haveInRoster = pmQueriesService.havePMinRoster(from.getUid(), jid);
            if (haveInRoster) {
                mm.setFrom(Jid.of(from.getName(), juickJid.getDomain(), "Juick"));
                mm.setBody(body);
            } else {
                mm.setFrom(Jid.of("juick", juickJid.getDomain(), "Juick"));
                mm.setBody("Private message from @" + from.getName() + ":\n" + body);
            }
            component.send(mm);
        });
    }

    @Override
    public void userSubscribed(User from, User to) {
        String notification = String.format("%s subscribed to your blog", from.getName());
        List<String> toJids = userService.getJIDsbyUID(to.getUid());
        toJids.forEach(jid -> {
            Message mm = new Message();
            mm.setTo(Jid.of(jid));
            mm.setType(Message.Type.CHAT);
            mm.setFrom(juickJid);
            mm.setBody(notification);
            component.send(mm);
        });
    }

    @Override
    public void messagePosted(com.juick.Message msg) {

    }
}