diff options
author | Vitaly Takmazov | 2018-03-21 16:33:12 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-21 16:33:12 +0300 |
commit | 01818b95286bc2bf96a4adad42c2194e487f373a (patch) | |
tree | 8d61ec1527dfd74f6cac6d560c44b20eb01d2a36 /juick-xmpp-wip/src/main/java/com/juick | |
parent | 77c7232a3ca354e009dfdd5a5eb0d390d7e627f5 (diff) |
drop xmpp-wip
Diffstat (limited to 'juick-xmpp-wip/src/main/java/com/juick')
5 files changed, 0 insertions, 414 deletions
diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java deleted file mode 100644 index 27206a32..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.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.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -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 javax.annotation.PostConstruct; -import javax.inject.Inject; -import java.io.IOException; -import java.net.URL; - -/** - * Created by vt on 12/11/2016. - */ -@SpringBootApplication -public class XMPPBot implements AutoCloseable { - private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); - @Inject - private UserService userService; - @Inject - private PMQueriesService pmQueriesService; - @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")); - try { - 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); - } - } - }); - 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"); - } - public static void main(String[] args) { - SpringApplication.run(XMPPBot.class, args); - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java deleted file mode 100644 index eb00229d..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * 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.server.xmpp.s2s.BasicXmppSession; -import com.juick.xmpp.Message; -import com.juick.xmpp.StreamComponentServer; -import com.juick.xmpp.StreamListener; -import com.juick.server.xmpp.extensions.JuickMessage; -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.inject.Inject; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.ExecutorService; - -/** - * @author ugnich - */ -@Component -public class XMPPRouter implements Message.MessageListener, AutoCloseable, StreamListener { - private static final Logger logger = LoggerFactory.getLogger(XMPPRouter.class); - - @Inject - private ExecutorService service; - - private final List<StreamComponentServer> connections = Collections.synchronizedList(new ArrayList<>()); - - private ServerSocket listener; - - @Inject - private BasicXmppSession session; - - @Value("${router_port:5347}") - private int routerPort; - - @PostConstruct - public void init() { - - logger.info("component router initialized"); - service.submit(() -> { - try { - listener = new ServerSocket(routerPort); - logger.info("component router listening on {}", routerPort); - while (!listener.isClosed()) { - if (Thread.currentThread().isInterrupted()) break; - Socket socket = listener.accept(); - service.submit(() -> { - try { - StreamComponentServer client = new StreamComponentServer(socket.getInputStream(), socket.getOutputStream(), "secret"); - addConnectionIn(client); - client.addChildParser(new JuickMessage()); - client.addListener((Message.MessageListener) this); - client.addListener((StreamListener) this); - client.connect(); - } catch (IOException e) { - logger.error("component error", e); - } catch (XmlPullParserException e) { - e.printStackTrace(); - } - }); - } - } catch (SocketException e) { - // shutdown - } catch (IOException e) { - logger.warn("io exception", e); - } - }); - } - - @Override - public void close() throws Exception { - if (!listener.isClosed()) { - listener.close(); - } - synchronized (getConnections()) { - for (Iterator<StreamComponentServer> i = getConnections().iterator(); i.hasNext(); ) { - StreamComponentServer c = i.next(); - c.logoff(); - i.remove(); - } - } - service.shutdown(); - logger.info("XMPP router destroyed"); - } - - private void addConnectionIn(StreamComponentServer c) { - synchronized (getConnections()) { - getConnections().add(c); - } - } - - private 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("XMPPRouter (out): {}", xml); - sendOut(s.getTo().getDomain(), xml); - } catch (XMLStreamException | JAXBException e1) { - logger.info("jaxb exception", e1); - } - } - - private void sendOut(String hostname, String xml) { - boolean haveAnyConn = false; - - StreamComponentServer connOut = null; - synchronized (getConnections()) { - for (StreamComponentServer c : getConnections()) { - if (c.to != null && c.to.getDomain().equals(hostname)) { - if (c.isLoggedIn()) { - connOut = c; - break; - } - } - } - } - if (connOut != null) { - connOut.send(xml); - return; - } - logger.error("component unavailable: {}", hostname); - - } - - public List<StreamComponentServer> getConnections() { - return connections; - } - - private 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; - } - @Override - public void onMessage(Message message) { - sendOut(parse(message.toString())); - } - - @Override - public void ready() { - - } - - @Override - public void fail(Exception e) { - - } - - @Override - public boolean filter(Jid jid, Jid jid1) { - return false; - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java deleted file mode 100644 index 7806347d..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.configuration; - -import com.juick.components.XMPPBot; -import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.server.protocol.JuickProtocol; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -/** - * Created by aalexeev on 11/12/16. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.components"}) -public class BotAppConfiguration extends BaseWebConfiguration { - @Bean - public XMPPBot xmpp() { - return new XMPPBot(); - } - @Bean - public JuickProtocol juickProtocol() { - return new JuickProtocol("http://juick.com"); - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java b/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java deleted file mode 100644 index e57d0a82..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/StatusController.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.controllers; - -import com.juick.components.XMPPRouter; -import com.juick.components.controllers.helpers.RouterStatus; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -import javax.inject.Inject; - -/** - * Created by vitalyster on 24.10.2016. - */ -@Controller -@ResponseBody -public class StatusController { - @Inject - private XMPPRouter router; - - @RequestMapping(method = RequestMethod.GET, value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public RouterStatus status() { - RouterStatus status = new RouterStatus(); - status.setConnections(router.getConnections()); - return status; - } -} diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java b/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java deleted file mode 100644 index 11148dd7..00000000 --- a/juick-xmpp-wip/src/main/java/com/juick/components/controllers/helpers/RouterStatus.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.juick.components.controllers.helpers; - -import com.juick.xmpp.StreamComponentServer; - -import java.util.List; - -public class RouterStatus { - private List<StreamComponentServer> connections; - - public List<StreamComponentServer> getConnections() { - return connections; - } - - public void setConnections(List<StreamComponentServer> connections) { - this.connections = connections; - } -} |