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