From 63d74f8e014f491df3d9ff8e26c39b7c0bb3561e Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <me@adhisimon.org>
Date: Thu, 27 Oct 2016 18:29:21 +0700
Subject: [PATCH] add friend if not friend

---
 adaptor-xmpp.js | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/adaptor-xmpp.js b/adaptor-xmpp.js
index 49ece0a..8b7e0d2 100644
--- a/adaptor-xmpp.js
+++ b/adaptor-xmpp.js
@@ -61,7 +61,7 @@ function onUnsubscribe(sender) {
 
 function onBuddy(jid, state, statusText, resource) {
     if (jid == 'undefined') {return; }
-    
+
     logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
 
     if (!matrix) {
@@ -86,6 +86,10 @@ function onBuddy(jid, state, statusText, resource) {
 function isPartnerOffline(partner) {
     if (!matrix) { return false; }
 
+    if (!isAFriend(partner)) {
+        addFriend(partner);
+    }
+
     if (!matrix.buddies[partner]) { return false; }
     if (!matrix.buddies[partner].resources) { return false; };
 
@@ -164,7 +168,15 @@ function sendMessage(destination, msg) {
 }
 
 function addFriend(friend) {
-        xmpp.subscribe(friend);
+    xmpp.subscribe(friend);
+}
+
+function isAFriend(jid) {
+    if (!matrix) { return false; };
+    if (!matrix.buddies) { return false; }
+    if (!matrix.buddies[jid]) { return false; }
+
+    return true;
 }
 
 exports.init = init;
@@ -172,3 +184,4 @@ exports.sendMessage = sendMessage;
 exports.setOptions = setOptions;
 exports.addFriend = addFriend;
 exports.isPartnerOffline = isPartnerOffline;
+exports.isAFriend = isAFriend;
-- 
1.9.0