Implement a “Sone rescue mode.”
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * Sone - Core.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Client;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.freenet.wot.Identity;
40 import net.pterodactylus.sone.freenet.wot.IdentityListener;
41 import net.pterodactylus.sone.freenet.wot.IdentityManager;
42 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
43 import net.pterodactylus.sone.main.SonePlugin;
44 import net.pterodactylus.util.config.Configuration;
45 import net.pterodactylus.util.config.ConfigurationException;
46 import net.pterodactylus.util.logging.Logging;
47 import net.pterodactylus.util.number.Numbers;
48 import freenet.keys.FreenetURI;
49
50 /**
51  * The Sone core.
52  *
53  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54  */
55 public class Core implements IdentityListener {
56
57         /**
58          * Enumeration for the possible states of a {@link Sone}.
59          *
60          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61          */
62         public enum SoneStatus {
63
64                 /** The Sone is unknown, i.e. not yet downloaded. */
65                 unknown,
66
67                 /** The Sone is idle, i.e. not being downloaded or inserted. */
68                 idle,
69
70                 /** The Sone is currently being inserted. */
71                 inserting,
72
73                 /** The Sone is currently being downloaded. */
74                 downloading,
75         }
76
77         /** The logger. */
78         private static final Logger logger = Logging.getLogger(Core.class);
79
80         /** The options. */
81         private final Options options = new Options();
82
83         /** The core listener manager. */
84         private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
85
86         /** The configuration. */
87         private final Configuration configuration;
88
89         /** The identity manager. */
90         private final IdentityManager identityManager;
91
92         /** Interface to freenet. */
93         private final FreenetInterface freenetInterface;
94
95         /** The Sone downloader. */
96         private final SoneDownloader soneDownloader;
97
98         /** Whether the core has been stopped. */
99         private volatile boolean stopped;
100
101         /** The Sones’ statuses. */
102         /* synchronize access on itself. */
103         private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
104
105         /** Sone inserters. */
106         /* synchronize access on this on localSones. */
107         private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
108
109         /** All local Sones. */
110         /* synchronize access on this on itself. */
111         private Map<String, Sone> localSones = new HashMap<String, Sone>();
112
113         /** All remote Sones. */
114         /* synchronize access on this on itself. */
115         private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
116
117         /** All new Sones. */
118         private Set<String> newSones = new HashSet<String>();
119
120         /** All known Sones. */
121         /* synchronize access on {@link #newSones}. */
122         private Set<String> knownSones = new HashSet<String>();
123
124         /** All posts. */
125         private Map<String, Post> posts = new HashMap<String, Post>();
126
127         /** All new posts. */
128         private Set<String> newPosts = new HashSet<String>();
129
130         /** All known posts. */
131         /* synchronize access on {@link #newPosts}. */
132         private Set<String> knownPosts = new HashSet<String>();
133
134         /** All replies. */
135         private Map<String, Reply> replies = new HashMap<String, Reply>();
136
137         /** All new replies. */
138         private Set<String> newReplies = new HashSet<String>();
139
140         /** All known replies. */
141         private Set<String> knownReplies = new HashSet<String>();
142
143         /**
144          * Creates a new core.
145          *
146          * @param configuration
147          *            The configuration of the core
148          * @param freenetInterface
149          *            The freenet interface
150          * @param identityManager
151          *            The identity manager
152          */
153         public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
154                 this.configuration = configuration;
155                 this.freenetInterface = freenetInterface;
156                 this.identityManager = identityManager;
157                 this.soneDownloader = new SoneDownloader(this, freenetInterface);
158         }
159
160         //
161         // LISTENER MANAGEMENT
162         //
163
164         /**
165          * Adds a new core listener.
166          *
167          * @param coreListener
168          *            The listener to add
169          */
170         public void addCoreListener(CoreListener coreListener) {
171                 coreListenerManager.addListener(coreListener);
172         }
173
174         /**
175          * Removes a core listener.
176          *
177          * @param coreListener
178          *            The listener to remove
179          */
180         public void removeCoreListener(CoreListener coreListener) {
181                 coreListenerManager.removeListener(coreListener);
182         }
183
184         //
185         // ACCESSORS
186         //
187
188         /**
189          * Returns the options used by the core.
190          *
191          * @return The options of the core
192          */
193         public Options getOptions() {
194                 return options;
195         }
196
197         /**
198          * Returns whether the “Sone rescue mode” is currently activated.
199          *
200          * @return {@code true} if the “Sone rescue mode” is currently activated,
201          *         {@code false} if it is not
202          */
203         public boolean isSoneRescueMode() {
204                 return options.getBooleanOption("SoneRescueMode").get();
205         }
206
207         /**
208          * Returns the identity manager used by the core.
209          *
210          * @return The identity manager
211          */
212         public IdentityManager getIdentityManager() {
213                 return identityManager;
214         }
215
216         /**
217          * Returns the status of the given Sone.
218          *
219          * @param sone
220          *            The Sone to get the status for
221          * @return The status of the Sone
222          */
223         public SoneStatus getSoneStatus(Sone sone) {
224                 synchronized (soneStatuses) {
225                         return soneStatuses.get(sone);
226                 }
227         }
228
229         /**
230          * Sets the status of the given Sone.
231          *
232          * @param sone
233          *            The Sone to set the status of
234          * @param soneStatus
235          *            The status to set
236          */
237         public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
238                 synchronized (soneStatuses) {
239                         soneStatuses.put(sone, soneStatus);
240                 }
241         }
242
243         /**
244          * Returns all Sones, remote and local.
245          *
246          * @return All Sones
247          */
248         public Set<Sone> getSones() {
249                 Set<Sone> allSones = new HashSet<Sone>();
250                 allSones.addAll(getLocalSones());
251                 allSones.addAll(getRemoteSones());
252                 return allSones;
253         }
254
255         /**
256          * Returns the Sone with the given ID, regardless whether it’s local or
257          * remote.
258          *
259          * @param id
260          *            The ID of the Sone to get
261          * @return The Sone with the given ID, or {@code null} if there is no such
262          *         Sone
263          */
264         public Sone getSone(String id) {
265                 return getSone(id, true);
266         }
267
268         /**
269          * Returns the Sone with the given ID, regardless whether it’s local or
270          * remote.
271          *
272          * @param id
273          *            The ID of the Sone to get
274          * @param create
275          *            {@code true} to create a new Sone if none exists,
276          *            {@code false} to return {@code null} if a Sone with the given
277          *            ID does not exist
278          * @return The Sone with the given ID, or {@code null} if there is no such
279          *         Sone
280          */
281         public Sone getSone(String id, boolean create) {
282                 if (isLocalSone(id)) {
283                         return getLocalSone(id);
284                 }
285                 return getRemoteSone(id, create);
286         }
287
288         /**
289          * Checks whether the core knows a Sone with the given ID.
290          *
291          * @param id
292          *            The ID of the Sone
293          * @return {@code true} if there is a Sone with the given ID, {@code false}
294          *         otherwise
295          */
296         public boolean hasSone(String id) {
297                 return isLocalSone(id) || isRemoteSone(id);
298         }
299
300         /**
301          * Returns whether the given Sone is a local Sone.
302          *
303          * @param sone
304          *            The Sone to check for its locality
305          * @return {@code true} if the given Sone is local, {@code false} otherwise
306          */
307         public boolean isLocalSone(Sone sone) {
308                 synchronized (localSones) {
309                         return localSones.containsKey(sone.getId());
310                 }
311         }
312
313         /**
314          * Returns whether the given ID is the ID of a local Sone.
315          *
316          * @param id
317          *            The Sone ID to check for its locality
318          * @return {@code true} if the given ID is a local Sone, {@code false}
319          *         otherwise
320          */
321         public boolean isLocalSone(String id) {
322                 synchronized (localSones) {
323                         return localSones.containsKey(id);
324                 }
325         }
326
327         /**
328          * Returns all local Sones.
329          *
330          * @return All local Sones
331          */
332         public Set<Sone> getLocalSones() {
333                 synchronized (localSones) {
334                         return new HashSet<Sone>(localSones.values());
335                 }
336         }
337
338         /**
339          * Returns the local Sone with the given ID.
340          *
341          * @param id
342          *            The ID of the Sone to get
343          * @return The Sone with the given ID
344          */
345         public Sone getLocalSone(String id) {
346                 return getLocalSone(id, true);
347         }
348
349         /**
350          * Returns the local Sone with the given ID, optionally creating a new Sone.
351          *
352          * @param id
353          *            The ID of the Sone
354          * @param create
355          *            {@code true} to create a new Sone if none exists,
356          *            {@code false} to return null if none exists
357          * @return The Sone with the given ID, or {@code null}
358          */
359         public Sone getLocalSone(String id, boolean create) {
360                 synchronized (localSones) {
361                         Sone sone = localSones.get(id);
362                         if ((sone == null) && create) {
363                                 sone = new Sone(id);
364                                 localSones.put(id, sone);
365                         }
366                         return sone;
367                 }
368         }
369
370         /**
371          * Returns all remote Sones.
372          *
373          * @return All remote Sones
374          */
375         public Set<Sone> getRemoteSones() {
376                 synchronized (remoteSones) {
377                         return new HashSet<Sone>(remoteSones.values());
378                 }
379         }
380
381         /**
382          * Returns the remote Sone with the given ID.
383          *
384          * @param id
385          *            The ID of the remote Sone to get
386          * @return The Sone with the given ID
387          */
388         public Sone getRemoteSone(String id) {
389                 return getRemoteSone(id, true);
390         }
391
392         /**
393          * Returns the remote Sone with the given ID.
394          *
395          * @param id
396          *            The ID of the remote Sone to get
397          * @param create
398          *            {@code true} to always create a Sone, {@code false} to return
399          *            {@code null} if no Sone with the given ID exists
400          * @return The Sone with the given ID
401          */
402         public Sone getRemoteSone(String id, boolean create) {
403                 synchronized (remoteSones) {
404                         Sone sone = remoteSones.get(id);
405                         if ((sone == null) && create) {
406                                 sone = new Sone(id);
407                                 remoteSones.put(id, sone);
408                         }
409                         return sone;
410                 }
411         }
412
413         /**
414          * Returns whether the given Sone is a remote Sone.
415          *
416          * @param sone
417          *            The Sone to check
418          * @return {@code true} if the given Sone is a remote Sone, {@code false}
419          *         otherwise
420          */
421         public boolean isRemoteSone(Sone sone) {
422                 synchronized (remoteSones) {
423                         return remoteSones.containsKey(sone.getId());
424                 }
425         }
426
427         /**
428          * Returns whether the Sone with the given ID is a remote Sone.
429          *
430          * @param id
431          *            The ID of the Sone to check
432          * @return {@code true} if the Sone with the given ID is a remote Sone,
433          *         {@code false} otherwise
434          */
435         public boolean isRemoteSone(String id) {
436                 synchronized (remoteSones) {
437                         return remoteSones.containsKey(id);
438                 }
439         }
440
441         /**
442          * Returns whether the given Sone is a new Sone. After this check, the Sone
443          * is marked as known, i.e. a second call with the same parameters will
444          * always yield {@code false}.
445          *
446          * @param sone
447          *            The sone to check for
448          * @return {@code true} if the given Sone is new, false otherwise
449          */
450         public boolean isNewSone(Sone sone) {
451                 synchronized (newSones) {
452                         boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
453                         knownSones.add(sone.getId());
454                         if (isNew) {
455                                 coreListenerManager.fireMarkSoneKnown(sone);
456                         }
457                         return isNew;
458                 }
459         }
460
461         /**
462          * Returns whether the given Sone has been modified.
463          *
464          * @param sone
465          *            The Sone to check for modifications
466          * @return {@code true} if a modification has been detected in the Sone,
467          *         {@code false} otherwise
468          */
469         public boolean isModifiedSone(Sone sone) {
470                 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
471         }
472
473         /**
474          * Returns the post with the given ID.
475          *
476          * @param postId
477          *            The ID of the post to get
478          * @return The post, or {@code null} if there is no such post
479          */
480         public Post getPost(String postId) {
481                 synchronized (posts) {
482                         Post post = posts.get(postId);
483                         if (post == null) {
484                                 post = new Post(postId);
485                                 posts.put(postId, post);
486                         }
487                         return post;
488                 }
489         }
490
491         /**
492          * Returns whether the given post ID is new. After this method returns it is
493          * marked a known post ID.
494          *
495          * @param postId
496          *            The post ID
497          * @return {@code true} if the post is considered to be new, {@code false}
498          *         otherwise
499          */
500         public boolean isNewPost(String postId) {
501                 synchronized (newPosts) {
502                         boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
503                         knownPosts.add(postId);
504                         if (isNew) {
505                                 coreListenerManager.fireMarkPostKnown(getPost(postId));
506                         }
507                         return isNew;
508                 }
509         }
510
511         /**
512          * Returns the reply with the given ID.
513          *
514          * @param replyId
515          *            The ID of the reply to get
516          * @return The reply, or {@code null} if there is no such reply
517          */
518         public Reply getReply(String replyId) {
519                 synchronized (replies) {
520                         Reply reply = replies.get(replyId);
521                         if (reply == null) {
522                                 reply = new Reply(replyId);
523                                 replies.put(replyId, reply);
524                         }
525                         return reply;
526                 }
527         }
528
529         /**
530          * Returns all replies for the given post, order ascending by time.
531          *
532          * @param post
533          *            The post to get all replies for
534          * @return All replies for the given post
535          */
536         public List<Reply> getReplies(Post post) {
537                 Set<Sone> sones = getSones();
538                 List<Reply> replies = new ArrayList<Reply>();
539                 for (Sone sone : sones) {
540                         for (Reply reply : sone.getReplies()) {
541                                 if (reply.getPost().equals(post)) {
542                                         replies.add(reply);
543                                 }
544                         }
545                 }
546                 Collections.sort(replies, Reply.TIME_COMPARATOR);
547                 return replies;
548         }
549
550         /**
551          * Returns whether the reply with the given ID is new.
552          *
553          * @param replyId
554          *            The ID of the reply to check
555          * @return {@code true} if the reply is considered to be new, {@code false}
556          *         otherwise
557          */
558         public boolean isNewReply(String replyId) {
559                 synchronized (newReplies) {
560                         boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
561                         knownReplies.add(replyId);
562                         if (isNew) {
563                                 coreListenerManager.fireMarkReplyKnown(getReply(replyId));
564                         }
565                         return isNew;
566                 }
567         }
568
569         /**
570          * Returns all Sones that have liked the given post.
571          *
572          * @param post
573          *            The post to get the liking Sones for
574          * @return The Sones that like the given post
575          */
576         public Set<Sone> getLikes(Post post) {
577                 Set<Sone> sones = new HashSet<Sone>();
578                 for (Sone sone : getSones()) {
579                         if (sone.getLikedPostIds().contains(post.getId())) {
580                                 sones.add(sone);
581                         }
582                 }
583                 return sones;
584         }
585
586         /**
587          * Returns all Sones that have liked the given reply.
588          *
589          * @param reply
590          *            The reply to get the liking Sones for
591          * @return The Sones that like the given reply
592          */
593         public Set<Sone> getLikes(Reply reply) {
594                 Set<Sone> sones = new HashSet<Sone>();
595                 for (Sone sone : getSones()) {
596                         if (sone.getLikedReplyIds().contains(reply.getId())) {
597                                 sones.add(sone);
598                         }
599                 }
600                 return sones;
601         }
602
603         //
604         // ACTIONS
605         //
606
607         /**
608          * Adds a local Sone from the given ID which has to be the ID of an own
609          * identity.
610          *
611          * @param id
612          *            The ID of an own identity to add a Sone for
613          * @return The added (or already existing) Sone
614          */
615         public Sone addLocalSone(String id) {
616                 synchronized (localSones) {
617                         if (localSones.containsKey(id)) {
618                                 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
619                                 return localSones.get(id);
620                         }
621                         OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
622                         if (ownIdentity == null) {
623                                 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
624                                 return null;
625                         }
626                         return addLocalSone(ownIdentity);
627                 }
628         }
629
630         /**
631          * Adds a local Sone from the given own identity.
632          *
633          * @param ownIdentity
634          *            The own identity to create a Sone from
635          * @return The added (or already existing) Sone
636          */
637         public Sone addLocalSone(OwnIdentity ownIdentity) {
638                 if (ownIdentity == null) {
639                         logger.log(Level.WARNING, "Given OwnIdentity is null!");
640                         return null;
641                 }
642                 synchronized (localSones) {
643                         final Sone sone;
644                         try {
645                                 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
646                         } catch (MalformedURLException mue1) {
647                                 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
648                                 return null;
649                         }
650                         sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
651                         sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
652                         /* TODO - load posts ’n stuff */
653                         localSones.put(ownIdentity.getId(), sone);
654                         final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
655                         soneInserters.put(sone, soneInserter);
656                         setSoneStatus(sone, SoneStatus.idle);
657                         loadSone(sone);
658                         if (!isSoneRescueMode()) {
659                                 soneInserter.start();
660                         }
661                         new Thread(new Runnable() {
662
663                                 @Override
664                                 @SuppressWarnings("synthetic-access")
665                                 public void run() {
666                                         if (!isSoneRescueMode()) {
667                                                 soneDownloader.fetchSone(sone);
668                                                 return;
669                                         }
670                                         logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
671                                         long edition = sone.getLatestEdition();
672                                         while (!stopped && (edition >= 0) && isSoneRescueMode()) {
673                                                 logger.log(Level.FINE, "Downloading edition " + edition + "…");
674                                                 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
675                                                 --edition;
676                                         }
677                                         logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
678                                         soneInserter.start();
679                                 }
680
681                         }, "Sone Downloader").start();
682                         return sone;
683                 }
684         }
685
686         /**
687          * Creates a new Sone for the given own identity.
688          *
689          * @param ownIdentity
690          *            The own identity to create a Sone for
691          * @return The created Sone
692          */
693         public Sone createSone(OwnIdentity ownIdentity) {
694                 identityManager.addContext(ownIdentity, "Sone");
695                 Sone sone = addLocalSone(ownIdentity);
696                 return sone;
697         }
698
699         /**
700          * Adds the Sone of the given identity.
701          *
702          * @param identity
703          *            The identity whose Sone to add
704          * @return The added or already existing Sone
705          */
706         public Sone addRemoteSone(Identity identity) {
707                 if (identity == null) {
708                         logger.log(Level.WARNING, "Given Identity is null!");
709                         return null;
710                 }
711                 synchronized (remoteSones) {
712                         final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
713                         boolean newSone = sone.getRequestUri() == null;
714                         sone.setRequestUri(getSoneUri(identity.getRequestUri()));
715                         sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
716                         if (newSone) {
717                                 synchronized (newSones) {
718                                         newSone = !knownSones.contains(sone.getId());
719                                         if (newSone) {
720                                                 newSones.add(sone.getId());
721                                         }
722                                 }
723                                 if (newSone) {
724                                         coreListenerManager.fireNewSoneFound(sone);
725                                 }
726                         }
727                         remoteSones.put(identity.getId(), sone);
728                         soneDownloader.addSone(sone);
729                         setSoneStatus(sone, SoneStatus.unknown);
730                         new Thread(new Runnable() {
731
732                                 @Override
733                                 @SuppressWarnings("synthetic-access")
734                                 public void run() {
735                                         soneDownloader.fetchSone(sone);
736                                 }
737
738                         }, "Sone Downloader").start();
739                         return sone;
740                 }
741         }
742
743         /**
744          * Updates the stores Sone with the given Sone.
745          *
746          * @param sone
747          *            The updated Sone
748          */
749         public void updateSone(Sone sone) {
750                 if (hasSone(sone.getId())) {
751                         boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
752                         Sone storedSone = getSone(sone.getId());
753                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
754                                 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
755                                 return;
756                         }
757                         synchronized (posts) {
758                                 if (!soneRescueMode) {
759                                         for (Post post : storedSone.getPosts()) {
760                                                 posts.remove(post.getId());
761                                         }
762                                 }
763                                 synchronized (newPosts) {
764                                         for (Post post : sone.getPosts()) {
765                                                 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
766                                                         newPosts.add(post.getId());
767                                                         coreListenerManager.fireNewPostFound(post);
768                                                 }
769                                                 posts.put(post.getId(), post);
770                                         }
771                                 }
772                         }
773                         synchronized (replies) {
774                                 if (!soneRescueMode) {
775                                         for (Reply reply : storedSone.getReplies()) {
776                                                 replies.remove(reply.getId());
777                                         }
778                                 }
779                                 synchronized (newReplies) {
780                                         for (Reply reply : sone.getReplies()) {
781                                                 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
782                                                         newReplies.add(reply.getId());
783                                                         coreListenerManager.fireNewReplyFound(reply);
784                                                 }
785                                                 replies.put(reply.getId(), reply);
786                                         }
787                                 }
788                         }
789                         synchronized (storedSone) {
790                                 storedSone.setTime(sone.getTime());
791                                 storedSone.setClient(sone.getClient());
792                                 storedSone.setProfile(sone.getProfile());
793                                 if (soneRescueMode) {
794                                         for (Post post : sone.getPosts()) {
795                                                 storedSone.addPost(post);
796                                         }
797                                         for (Reply reply : sone.getReplies()) {
798                                                 storedSone.addReply(reply);
799                                         }
800                                         for (String likedPostId : sone.getLikedPostIds()) {
801                                                 storedSone.addLikedPostId(likedPostId);
802                                         }
803                                         for (String likedReplyId : sone.getLikedReplyIds()) {
804                                                 storedSone.addLikedReplyId(likedReplyId);
805                                         }
806                                 } else {
807                                         storedSone.setPosts(sone.getPosts());
808                                         storedSone.setReplies(sone.getReplies());
809                                         storedSone.setLikePostIds(sone.getLikedPostIds());
810                                         storedSone.setLikeReplyIds(sone.getLikedReplyIds());
811                                 }
812                                 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
813                         }
814                 }
815         }
816
817         /**
818          * Deletes the given Sone. This will remove the Sone from the
819          * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
820          * and remove the context from its identity.
821          *
822          * @param sone
823          *            The Sone to delete
824          */
825         public void deleteSone(Sone sone) {
826                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
827                         logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
828                         return;
829                 }
830                 synchronized (localSones) {
831                         if (!localSones.containsKey(sone.getId())) {
832                                 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
833                                 return;
834                         }
835                         localSones.remove(sone.getId());
836                         soneInserters.remove(sone).stop();
837                 }
838                 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
839                 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
840                 try {
841                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
842                 } catch (ConfigurationException ce1) {
843                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
844                 }
845         }
846
847         /**
848          * Loads and updates the given Sone from the configuration. If any error is
849          * encountered, loading is aborted and the given Sone is not changed.
850          *
851          * @param sone
852          *            The Sone to load and update
853          */
854         public void loadSone(Sone sone) {
855                 if (!isLocalSone(sone)) {
856                         logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
857                         return;
858                 }
859
860                 /* load Sone. */
861                 String sonePrefix = "Sone/" + sone.getId();
862                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
863                 if (soneTime == null) {
864                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
865                         return;
866                 }
867                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
868
869                 /* load profile. */
870                 Profile profile = new Profile();
871                 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
872                 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
873                 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
874                 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
875                 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
876                 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
877
878                 /* load posts. */
879                 Set<Post> posts = new HashSet<Post>();
880                 while (true) {
881                         String postPrefix = sonePrefix + "/Posts/" + posts.size();
882                         String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
883                         if (postId == null) {
884                                 break;
885                         }
886                         long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
887                         String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
888                         if ((postTime == 0) || (postText == null)) {
889                                 logger.log(Level.WARNING, "Invalid post found, aborting load!");
890                                 return;
891                         }
892                         posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
893                 }
894
895                 /* load replies. */
896                 Set<Reply> replies = new HashSet<Reply>();
897                 while (true) {
898                         String replyPrefix = sonePrefix + "/Replies/" + replies.size();
899                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
900                         if (replyId == null) {
901                                 break;
902                         }
903                         String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
904                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
905                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
906                         if ((postId == null) || (replyTime == 0) || (replyText == null)) {
907                                 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
908                                 return;
909                         }
910                         replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
911                 }
912
913                 /* load post likes. */
914                 Set<String> likedPostIds = new HashSet<String>();
915                 while (true) {
916                         String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
917                         if (likedPostId == null) {
918                                 break;
919                         }
920                         likedPostIds.add(likedPostId);
921                 }
922
923                 /* load reply likes. */
924                 Set<String> likedReplyIds = new HashSet<String>();
925                 while (true) {
926                         String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
927                         if (likedReplyId == null) {
928                                 break;
929                         }
930                         likedReplyIds.add(likedReplyId);
931                 }
932
933                 /* load friends. */
934                 Set<String> friends = new HashSet<String>();
935                 while (true) {
936                         String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
937                         if (friendId == null) {
938                                 break;
939                         }
940                         friends.add(friendId);
941                 }
942
943                 /* if we’re still here, Sone was loaded successfully. */
944                 synchronized (sone) {
945                         sone.setTime(soneTime);
946                         sone.setProfile(profile);
947                         sone.setPosts(posts);
948                         sone.setReplies(replies);
949                         sone.setLikePostIds(likedPostIds);
950                         sone.setLikeReplyIds(likedReplyIds);
951                         sone.setFriends(friends);
952                         soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
953                 }
954                 synchronized (newSones) {
955                         for (String friend : friends) {
956                                 knownSones.add(friend);
957                         }
958                 }
959                 synchronized (newPosts) {
960                         for (Post post : posts) {
961                                 knownPosts.add(post.getId());
962                         }
963                 }
964                 synchronized (newReplies) {
965                         for (Reply reply : replies) {
966                                 knownReplies.add(reply.getId());
967                         }
968                 }
969         }
970
971         /**
972          * Saves the given Sone. This will persist all local settings for the given
973          * Sone, such as the friends list and similar, private options.
974          *
975          * @param sone
976          *            The Sone to save
977          */
978         public void saveSone(Sone sone) {
979                 if (!isLocalSone(sone)) {
980                         logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
981                         return;
982                 }
983                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
984                         logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
985                         return;
986                 }
987
988                 logger.log(Level.INFO, "Saving Sone: %s", sone);
989                 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
990                 try {
991                         /* save Sone into configuration. */
992                         String sonePrefix = "Sone/" + sone.getId();
993                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
994                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
995
996                         /* save profile. */
997                         Profile profile = sone.getProfile();
998                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
999                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1000                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1001                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1002                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1003                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1004
1005                         /* save posts. */
1006                         int postCounter = 0;
1007                         for (Post post : sone.getPosts()) {
1008                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1009                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1010                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1011                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1012                         }
1013                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1014
1015                         /* save replies. */
1016                         int replyCounter = 0;
1017                         for (Reply reply : sone.getReplies()) {
1018                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1019                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1020                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1021                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1022                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1023                         }
1024                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1025
1026                         /* save post likes. */
1027                         int postLikeCounter = 0;
1028                         for (String postId : sone.getLikedPostIds()) {
1029                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1030                         }
1031                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1032
1033                         /* save reply likes. */
1034                         int replyLikeCounter = 0;
1035                         for (String replyId : sone.getLikedReplyIds()) {
1036                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1037                         }
1038                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1039
1040                         /* save friends. */
1041                         int friendCounter = 0;
1042                         for (String friendId : sone.getFriends()) {
1043                                 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1044                         }
1045                         configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1046
1047                         logger.log(Level.INFO, "Sone %s saved.", sone);
1048                 } catch (ConfigurationException ce1) {
1049                         logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1050                 }
1051         }
1052
1053         /**
1054          * Creates a new post.
1055          *
1056          * @param sone
1057          *            The Sone that creates the post
1058          * @param text
1059          *            The text of the post
1060          */
1061         public void createPost(Sone sone, String text) {
1062                 createPost(sone, System.currentTimeMillis(), text);
1063         }
1064
1065         /**
1066          * Creates a new post.
1067          *
1068          * @param sone
1069          *            The Sone that creates the post
1070          * @param time
1071          *            The time of the post
1072          * @param text
1073          *            The text of the post
1074          */
1075         public void createPost(Sone sone, long time, String text) {
1076                 if (!isLocalSone(sone)) {
1077                         logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1078                         return;
1079                 }
1080                 Post post = new Post(sone, time, text);
1081                 synchronized (posts) {
1082                         posts.put(post.getId(), post);
1083                 }
1084                 synchronized (newPosts) {
1085                         knownPosts.add(post.getId());
1086                 }
1087                 sone.addPost(post);
1088                 saveSone(sone);
1089         }
1090
1091         /**
1092          * Deletes the given post.
1093          *
1094          * @param post
1095          *            The post to delete
1096          */
1097         public void deletePost(Post post) {
1098                 if (!isLocalSone(post.getSone())) {
1099                         logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1100                         return;
1101                 }
1102                 post.getSone().removePost(post);
1103                 synchronized (posts) {
1104                         posts.remove(post.getId());
1105                 }
1106                 saveSone(post.getSone());
1107         }
1108
1109         /**
1110          * Creates a new reply.
1111          *
1112          * @param sone
1113          *            The Sone that creates the reply
1114          * @param post
1115          *            The post that this reply refers to
1116          * @param text
1117          *            The text of the reply
1118          * @return The created reply
1119          */
1120         public Reply createReply(Sone sone, Post post, String text) {
1121                 return createReply(sone, post, System.currentTimeMillis(), text);
1122         }
1123
1124         /**
1125          * Creates a new reply.
1126          *
1127          * @param sone
1128          *            The Sone that creates the reply
1129          * @param post
1130          *            The post that this reply refers to
1131          * @param time
1132          *            The time of the reply
1133          * @param text
1134          *            The text of the reply
1135          * @return The created reply
1136          */
1137         public Reply createReply(Sone sone, Post post, long time, String text) {
1138                 if (!isLocalSone(sone)) {
1139                         logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1140                         return null;
1141                 }
1142                 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1143                 synchronized (replies) {
1144                         replies.put(reply.getId(), reply);
1145                 }
1146                 synchronized (newReplies) {
1147                         knownReplies.add(reply.getId());
1148                 }
1149                 sone.addReply(reply);
1150                 saveSone(sone);
1151                 return reply;
1152         }
1153
1154         /**
1155          * Deletes the given reply.
1156          *
1157          * @param reply
1158          *            The reply to delete
1159          */
1160         public void deleteReply(Reply reply) {
1161                 Sone sone = reply.getSone();
1162                 if (!isLocalSone(sone)) {
1163                         logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1164                         return;
1165                 }
1166                 synchronized (replies) {
1167                         replies.remove(reply.getId());
1168                 }
1169                 sone.removeReply(reply);
1170                 saveSone(sone);
1171         }
1172
1173         /**
1174          * Starts the core.
1175          */
1176         public void start() {
1177                 loadConfiguration();
1178         }
1179
1180         /**
1181          * Stops the core.
1182          */
1183         public void stop() {
1184                 synchronized (localSones) {
1185                         for (SoneInserter soneInserter : soneInserters.values()) {
1186                                 soneInserter.stop();
1187                         }
1188                 }
1189                 saveConfiguration();
1190                 stopped = true;
1191         }
1192
1193         //
1194         // PRIVATE METHODS
1195         //
1196
1197         /**
1198          * Loads the configuration.
1199          */
1200         @SuppressWarnings("unchecked")
1201         private void loadConfiguration() {
1202                 /* create options. */
1203                 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1204
1205                         @Override
1206                         public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1207                                 SoneInserter.setInsertionDelay(newValue);
1208                         }
1209
1210                 }));
1211                 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1212                 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1213                 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1214
1215                 /* read options from configuration. */
1216                 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1217                 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1218                 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1219                 options.getBooleanOption("ClearOnNextRestart").set(null);
1220                 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1221                 if (clearConfiguration) {
1222                         /* stop loading the configuration. */
1223                         return;
1224                 }
1225
1226                 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1227                 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1228
1229                 /* load known Sones. */
1230                 int soneCounter = 0;
1231                 while (true) {
1232                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1233                         if (knownSoneId == null) {
1234                                 break;
1235                         }
1236                         synchronized (newSones) {
1237                                 knownSones.add(knownSoneId);
1238                         }
1239                 }
1240
1241                 /* load known posts. */
1242                 int postCounter = 0;
1243                 while (true) {
1244                         String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1245                         if (knownPostId == null) {
1246                                 break;
1247                         }
1248                         synchronized (newPosts) {
1249                                 knownPosts.add(knownPostId);
1250                         }
1251                 }
1252
1253                 /* load known replies. */
1254                 int replyCounter = 0;
1255                 while (true) {
1256                         String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1257                         if (knownReplyId == null) {
1258                                 break;
1259                         }
1260                         synchronized (newReplies) {
1261                                 knownReplies.add(knownReplyId);
1262                         }
1263                 }
1264
1265         }
1266
1267         /**
1268          * Saves the current options.
1269          */
1270         private void saveConfiguration() {
1271                 /* store the options first. */
1272                 try {
1273                         configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1274                         configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1275                         configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1276                         configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1277
1278                         /* save known Sones. */
1279                         int soneCounter = 0;
1280                         synchronized (newSones) {
1281                                 for (String knownSoneId : knownSones) {
1282                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1283                                 }
1284                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1285                         }
1286
1287                         /* save known posts. */
1288                         int postCounter = 0;
1289                         synchronized (newPosts) {
1290                                 for (String knownPostId : knownPosts) {
1291                                         configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1292                                 }
1293                                 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1294                         }
1295
1296                         /* save known replies. */
1297                         int replyCounter = 0;
1298                         synchronized (newReplies) {
1299                                 for (String knownReplyId : knownReplies) {
1300                                         configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1301                                 }
1302                                 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1303                         }
1304
1305                 } catch (ConfigurationException ce1) {
1306                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1307                 }
1308         }
1309
1310         /**
1311          * Generate a Sone URI from the given URI and latest edition.
1312          *
1313          * @param uriString
1314          *            The URI to derive the Sone URI from
1315          * @return The derived URI
1316          */
1317         private FreenetURI getSoneUri(String uriString) {
1318                 try {
1319                         FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1320                         return uri;
1321                 } catch (MalformedURLException mue1) {
1322                         logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1323                         return null;
1324                 }
1325         }
1326
1327         //
1328         // INTERFACE IdentityListener
1329         //
1330
1331         /**
1332          * {@inheritDoc}
1333          */
1334         @Override
1335         public void ownIdentityAdded(OwnIdentity ownIdentity) {
1336                 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1337                 if (ownIdentity.hasContext("Sone")) {
1338                         addLocalSone(ownIdentity);
1339                 }
1340         }
1341
1342         /**
1343          * {@inheritDoc}
1344          */
1345         @Override
1346         public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1347                 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1348         }
1349
1350         /**
1351          * {@inheritDoc}
1352          */
1353         @Override
1354         public void identityAdded(Identity identity) {
1355                 logger.log(Level.FINEST, "Adding Identity: " + identity);
1356                 addRemoteSone(identity);
1357         }
1358
1359         /**
1360          * {@inheritDoc}
1361          */
1362         @Override
1363         public void identityUpdated(final Identity identity) {
1364                 new Thread(new Runnable() {
1365
1366                         @Override
1367                         @SuppressWarnings("synthetic-access")
1368                         public void run() {
1369                                 Sone sone = getRemoteSone(identity.getId());
1370                                 soneDownloader.fetchSone(sone);
1371                         }
1372                 }).start();
1373         }
1374
1375         /**
1376          * {@inheritDoc}
1377          */
1378         @Override
1379         public void identityRemoved(Identity identity) {
1380                 /* TODO */
1381         }
1382
1383 }