1package eu.siacs.conversations.entities;
  2
  3import android.content.Context;
  4import androidx.annotation.NonNull;
  5import androidx.annotation.Nullable;
  6import com.google.common.base.Strings;
  7import com.google.common.collect.ImmutableList;
  8import eu.siacs.conversations.utils.StringUtils;
  9import eu.siacs.conversations.utils.UIHelper;
 10import eu.siacs.conversations.xml.Element;
 11import eu.siacs.conversations.xml.Namespace;
 12import eu.siacs.conversations.xmpp.Jid;
 13import im.conversations.android.xmpp.model.bookmark.Storage;
 14import im.conversations.android.xmpp.model.bookmark2.Conference;
 15import im.conversations.android.xmpp.model.pubsub.PubSub;
 16import java.lang.ref.WeakReference;
 17import java.util.ArrayList;
 18import java.util.Collections;
 19import java.util.HashMap;
 20import java.util.List;
 21import java.util.Locale;
 22import java.util.Map;
 23
 24public class Bookmark extends Element implements ListItem {
 25
 26    private final Account account;
 27    private WeakReference<Conversation> conversation;
 28    private Jid jid;
 29    protected Element extensions = new Element("extensions", Namespace.BOOKMARKS2);
 30
 31    public Bookmark(final Account account, final Jid jid) {
 32        super("conference");
 33        this.jid = jid;
 34        this.setAttribute("jid", jid);
 35        this.account = account;
 36    }
 37
 38    private Bookmark(Account account) {
 39        super("conference");
 40        this.account = account;
 41    }
 42
 43    public static Map<Jid, Bookmark> parseFromStorage(
 44            final Storage storage, final Account account) {
 45        if (storage == null) {
 46            return Collections.emptyMap();
 47        }
 48        final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
 49        for (final Element item : storage.getChildren()) {
 50            if (item.getName().equals("conference")) {
 51                final Bookmark bookmark = Bookmark.parse(item, account);
 52                if (bookmark != null) {
 53                    final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
 54                    if (old != null
 55                            && old.getBookmarkName() != null
 56                            && bookmark.getBookmarkName() == null) {
 57                        bookmark.setBookmarkName(old.getBookmarkName());
 58                    }
 59                }
 60            }
 61        }
 62        return bookmarks;
 63    }
 64
 65    public static Map<Jid, Bookmark> parseFromPubSub(final PubSub pubSub, final Account account) {
 66        if (pubSub == null) {
 67            return Collections.emptyMap();
 68        }
 69        final var items = pubSub.getItems();
 70        if (items == null || !Namespace.BOOKMARKS2.equals(items.getNode())) {
 71            return Collections.emptyMap();
 72        }
 73        final Map<Jid, Bookmark> bookmarks = new HashMap<>();
 74        for (final var item : items.getItemMap(Conference.class).entrySet()) {
 75            final Bookmark bookmark =
 76                    Bookmark.parseFromItem(item.getKey(), item.getValue(), account);
 77            if (bookmark == null) {
 78                continue;
 79            }
 80            bookmarks.put(bookmark.jid, bookmark);
 81        }
 82        return bookmarks;
 83    }
 84
 85    public static Bookmark parse(Element element, Account account) {
 86        Bookmark bookmark = new Bookmark(account);
 87        bookmark.setAttributes(element.getAttributes());
 88        bookmark.setChildren(element.getChildren());
 89        bookmark.jid = Jid.Invalid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
 90        if (bookmark.jid == null) {
 91            return null;
 92        }
 93        return bookmark;
 94    }
 95
 96    public static Bookmark parseFromItem(
 97            final String id, final Conference conference, final Account account) {
 98        if (id == null || conference == null) {
 99            return null;
100        }
101        final Bookmark bookmark = new Bookmark(account);
102        bookmark.jid = Jid.Invalid.getNullForInvalid(Jid.ofOrInvalid(id));
103        // TODO verify that we only use bare jids and ignore full jids
104        if (bookmark.jid == null) {
105            return null;
106        }
107        bookmark.setBookmarkName(conference.getAttribute("name"));
108        bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
109        bookmark.setNick(conference.findChildContent("nick"));
110        bookmark.setPassword(conference.findChildContent("password"));
111        final Element extensions = conference.findChild("extensions", Namespace.BOOKMARKS2);
112        if (extensions != null) {
113            for (final Element ext : extensions.getChildren()) {
114                if (ext.getName().equals("group") && ext.getNamespace().equals("jabber:iq:roster")) {
115                    bookmark.addGroup(ext.getContent());
116                }
117            }
118            bookmark.extensions = extensions;
119        }
120        return bookmark;
121    }
122
123    public Element getExtensions() {
124        return extensions;
125    }
126
127	public void addGroup(final String group) {
128		addChild("group", "jabber:iq:roster").setContent(group);
129		extensions.addChild("group", "jabber:iq:roster").setContent(group);
130	}
131
132	public void setGroups(List<String> groups) {
133		final List<Element> children = ImmutableList.copyOf(getChildren());
134		for (final Element el : children) {
135			if (el.getName().equals("group")) {
136				removeChild(el);
137			}
138		}
139
140		final List<Element> extChildren = ImmutableList.copyOf(extensions.getChildren());
141		for (final Element el : extChildren) {
142			if (el.getName().equals("group")) {
143				extensions.removeChild(el);
144			}
145		}
146
147		for (final String group : groups) {
148			addGroup(group);
149		}
150	}
151
152	public void setAutojoin(boolean autojoin) {
153		if (autojoin) {
154			this.setAttribute("autojoin", "true");
155		} else {
156			this.setAttribute("autojoin", "false");
157		}
158	}
159
160	@Override
161	public int compareTo(final @NonNull ListItem another) {
162		if (getJid().isDomainJid() && !another.getJid().isDomainJid()) {
163			return -1;
164		} else if (!getJid().isDomainJid() && another.getJid().isDomainJid()) {
165			return 1;
166		}
167
168		if (getDisplayName().equals(another.getDisplayName())) {
169			return getJid().compareTo(another.getJid());
170		}
171
172		return this.getDisplayName().compareToIgnoreCase(
173				another.getDisplayName());
174	}
175
176    @Override
177    public String getDisplayName() {
178        final Conversation c = getConversation();
179        final String name = getBookmarkName();
180        if (c != null) {
181            return c.getName().toString();
182        } else if (printableValue(name, false)) {
183            return name.trim();
184        } else {
185            Jid jid = this.getJid();
186            return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
187        }
188    }
189
190    public static boolean printableValue(@Nullable String value, boolean permitNone) {
191        return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
192    }
193
194    public static boolean printableValue(@Nullable String value) {
195        return printableValue(value, true);
196    }
197
198    @Override
199    public Jid getJid() {
200        return this.jid;
201    }
202
203	public Jid getFullJid() {
204		return getFullJid(getNick(), true);
205	}
206
207	private Jid getFullJid(final String nick, boolean tryFix) {
208		try {
209			return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
210		} catch (final IllegalArgumentException e) {
211			try {
212				return tryFix ? getFullJid(gnu.inet.encoding.Punycode.encode(nick), false) : null;
213			} catch (final Exception e2) {
214				return null;
215			}
216		}
217	}
218
219	public List<Tag> getGroupTags() {
220		ArrayList<Tag> tags = new ArrayList<>();
221
222		for (Element element : getChildren()) {
223			if (element.getName().equals("group") && element.getContent() != null) {
224				String group = element.getContent();
225				tags.add(new Tag(group));
226			}
227		}
228
229		return tags;
230	}
231
232	@Override
233	public List<Tag> getTags(Context context) {
234		ArrayList<Tag> tags = new ArrayList<>();
235		tags.add(new Tag("Channel"));
236		tags.addAll(getGroupTags());
237		return tags;
238	}
239
240    public String getNick() {
241        return Strings.emptyToNull(this.findChildContent("nick"));
242    }
243
244    public void setNick(String nick) {
245        Element element = this.findChild("nick");
246        if (element == null) {
247            element = this.addChild("nick");
248        }
249        element.setContent(nick);
250    }
251
252    public boolean autojoin() {
253        return this.getAttributeAsBoolean("autojoin");
254    }
255
256    public String getPassword() {
257        return this.findChildContent("password");
258    }
259
260    public void setPassword(String password) {
261        Element element = this.findChild("password");
262        if (element != null) {
263            element.setContent(password);
264        }
265    }
266
267	@Override
268	public boolean match(Context context, String needle) {
269		if (needle == null) {
270			return true;
271		}
272		needle = needle.toLowerCase(Locale.US);
273		String[] parts = needle.split("[,\\s]+");
274		if (parts.length > 1) {
275			for (String part : parts) {
276				if (!match(context, part)) {
277					return false;
278				}
279			}
280			return true;
281		} else if (parts.length > 0) {
282			final Jid jid = getJid();
283			return (jid != null && jid.toString().contains(parts[0])) ||
284				getDisplayName().toLowerCase(Locale.US).contains(parts[0]) ||
285				matchInTag(context, parts[0]);
286		} else {
287			final Jid jid = getJid();
288			return (jid != null && jid.toString().contains(needle)) ||
289				getDisplayName().toLowerCase(Locale.US).contains(needle);
290		}
291	}
292
293    private boolean matchInTag(Context context, String needle) {
294        needle = needle.toLowerCase(Locale.US);
295        for (Tag tag : getTags(context)) {
296            if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
297                return true;
298            }
299        }
300        return false;
301    }
302
303    public Account getAccount() {
304        return this.account;
305    }
306
307    public synchronized Conversation getConversation() {
308        return this.conversation != null ? this.conversation.get() : null;
309    }
310
311    public synchronized void setConversation(Conversation conversation) {
312        if (this.conversation != null) {
313            this.conversation.clear();
314        }
315        if (conversation == null) {
316            this.conversation = null;
317        } else {
318            this.conversation = new WeakReference<>(conversation);
319        }
320    }
321
322    public String getBookmarkName() {
323        return this.getAttribute("name");
324    }
325
326    public boolean setBookmarkName(String name) {
327        String before = getBookmarkName();
328        if (name != null) {
329            this.setAttribute("name", name);
330        } else {
331            this.removeAttribute("name");
332        }
333        return StringUtils.changed(before, name);
334    }
335
336    @Override
337    public int getAvatarBackgroundColor() {
338        return UIHelper.getColorForName(
339                jid != null ? jid.asBareJid().toString() : getDisplayName());
340    }
341
342    @Override
343    public String getAvatarName() {
344        return getDisplayName();
345    }
346}