--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - 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.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero 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
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.utils;
+
+import org.jivesoftware.smack.AccountManager;
+import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.Roster;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.packet.Presence;
+
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.User;
+import com.pentila.entSavoie.userProperties.model.UserProperties;
+import com.pentila.entSavoie.userProperties.service.UserPropertiesLocalServiceUtil;
+
+public class XmppUtil {
+
+ private static Log _log = LogFactoryUtil.getLog(XmppUtil.class);
+
+ public static void createAccount(User user) {
+
+ XMPPConnection con = getXMPPConnection(user);
+ AccountManager acm = new AccountManager(con);
+ try {
+
+ String userName = user.getLogin();
+ String password = getPassword(8);
+
+ acm.createAccount(userName, password);
+
+ UserProperties prop = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user.getUserId());
+ prop.setJabberId(userName);
+ prop.setJabberPwd(password);
+
+ UserPropertiesLocalServiceUtil.updateUserProperties(prop, true);
+ _log.info("Created Jabber account for user "+user.getFullName());
+
+ } catch (Exception e) {
+ _log.error("Create account error for user "+user.getFullName());
+ }
+
+ disconnect(con);
+ }
+
+
+ private static String getPassword(int n) {
+ char[] pw = new char[n];
+ int c = 'A';
+ int r1 = 0;
+ for (int i=0; i < n; i++)
+ {
+ r1 = (int)(Math.random() * 3);
+ switch(r1) {
+ case 0: c = '0' + (int)(Math.random() * 10); break;
+ case 1: c = 'a' + (int)(Math.random() * 26); break;
+ case 2: c = 'A' + (int)(Math.random() * 26); break;
+ }
+ pw[i] = (char)c;
+ }
+ return new String(pw);
+ }
+
+ public static void deleteAccount(User user) {
+
+ XMPPConnection con = getXMPPConnection(user);
+
+ try {
+ UserProperties prop = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user.getUserId());
+
+ if (prop.getJabberId() != null && !prop.getJabberId().equals("")) {
+ con.login(prop.getJabberId(), prop.getJabberPwd());
+ con.getAccountManager().deleteAccount();
+ }
+ _log.info("Deleted Jabber account for user "+user.getFullName());
+
+ } catch (Exception e) {
+ _log.error("Delete account error : ", e);
+ }
+
+ disconnect(con);
+ }
+
+
+ public static void subscribeUsersToRoster(User user1, User user2) {
+ XMPPConnection conUser1 = getXMPPConnection(user1);
+ XMPPConnection conUser2 = getXMPPConnection(user2);
+
+ try {
+ UserProperties propUser1 = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user1.getUserId());
+ UserProperties propUser2 = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user2.getUserId());
+
+ conUser1.login(propUser1.getJabberId(), propUser1.getJabberPwd(),"Cartable-de-savoie-server");
+ conUser2.login(propUser2.getJabberId(), propUser2.getJabberPwd(),"Cartable-de-savoie-server");
+
+ Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
+
+ Roster r1 = conUser1.getRoster();
+ Roster r2 = conUser2.getRoster();
+
+ r1.createEntry(propUser2.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user1.getCompanyId()), user2.getFullName(), new String[0]);
+ r2.createEntry(propUser1.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user2.getCompanyId()), user1.getFullName(), new String[0]);
+
+ // ADded this commented which runs the full subscription process
+ // This works but the nickname in ofroster table was not set, and that prevents the roster to be connected
+ // The workaround found was to add a trigger in the database, which updates the 3 colons of ofroster table like the following:
+ // sub = 3
+ // ask = -1
+ // recv = -1
+ // These values enable the chat between 2 friends
+
+// String user1Name = propUser1.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user1.getCompanyId());
+// String user2Name = propUser2.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user2.getCompanyId());
+//
+// // User1 sends a request to user2
+// Presence subscribe = new Presence(Presence.Type.subscribe);
+// subscribe.setTo(user2Name);
+// conUser1.sendPacket(subscribe);
+//
+// // User2 accepts it and send a request to User1
+// Presence subscribed = new Presence(Presence.Type.subscribed);
+// subscribed.setTo(user1Name);
+// conUser2.sendPacket(subscribed);
+//
+// subscribe = new Presence(Presence.Type.subscribe);
+// subscribe.setTo(user1Name);
+// conUser2.sendPacket(subscribe);
+//
+// // User1 accepts it
+// subscribed = new Presence(Presence.Type.subscribed);
+// subscribed.setTo(user2Name);
+// conUser1.sendPacket(subscribed);
+
+ _log.info("User "+user1.getFullName()+" and user "+user2.getFullName()+" are now friends in Jabber");
+ }
+ catch(Exception e) {
+ _log.error("subscribeUsersToRoster Error for users "+user1.getFullName()+" and "+user2.getFullName());
+ }
+
+
+ disconnect(conUser1);
+ disconnect(conUser2);
+
+ }
+
+ public static void unsubscribeUsersToRoster(User user1, User user2) {
+ XMPPConnection conUser1 = getXMPPConnection(user1);
+ XMPPConnection conUser2 = getXMPPConnection(user2);
+
+ try {
+ UserProperties propUser1 = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user1.getUserId());
+ UserProperties propUser2 = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user2.getUserId());
+
+ conUser1.login(propUser1.getJabberId(), propUser1.getJabberPwd(),"Cartable-de-savoie-server");
+ conUser2.login(propUser2.getJabberId(), propUser2.getJabberPwd(),"Cartable-de-savoie-server");
+
+ Roster r1 = conUser1.getRoster();
+ Roster r2 = conUser2.getRoster();
+
+ r1.removeEntry(r1.getEntry(propUser2.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user1.getCompanyId())));
+ r2.removeEntry(r2.getEntry(propUser1.getJabberId() + "@" + ENTMainUtilsLocalServiceUtil.getXmppDomain(user2.getCompanyId())));
+ }
+ catch(Exception e) {
+ _log.error("unsubscribeUsersToRoster Error");
+ }
+
+
+ disconnect(conUser1);
+ disconnect(conUser2);
+ }
+
+ public static void logoutUser(User user) {
+
+ XMPPConnection connection = getXMPPConnection(user);
+
+ Presence offlinePres = new Presence(Presence.Type.unavailable, "", 1, Presence.Mode.away);
+ offlinePres.setStatus(connection.getUser() + "unavailable");
+
+ try {
+ connection.disconnect(offlinePres);
+ } catch (Exception exception) {
+ connection.disconnect();
+ }
+
+ disconnect(connection);
+ }
+
+
+ private static XMPPConnection getXMPPConnection(User user) {
+ ConnectionConfiguration config = new ConnectionConfiguration(ENTMainUtilsLocalServiceUtil.getXmppHost(user.getCompanyId()), 5222);
+ config.setSendPresence(true);
+ config.setRosterLoadedAtLogin(true);
+
+ XMPPConnection connection = new XMPPConnection(config);
+ try {
+ connection.connect();
+ } catch (XMPPException e) {
+ _log.error("XMPP connection creation in error for user "+user.getFullName());
+ }
+
+ return connection;
+ }
+
+ private static void disconnect(XMPPConnection con) {
+ if (con != null) {
+ con.disconnect();
+ }
+ }
+}