2 * Sone - Core.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.core;
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;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
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.freenet.wot.Trust;
44 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
45 import net.pterodactylus.sone.main.SonePlugin;
46 import net.pterodactylus.util.config.Configuration;
47 import net.pterodactylus.util.config.ConfigurationException;
48 import net.pterodactylus.util.logging.Logging;
49 import net.pterodactylus.util.number.Numbers;
50 import freenet.keys.FreenetURI;
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class Core implements IdentityListener {
60 * Enumeration for the possible states of a {@link Sone}.
62 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
64 public enum SoneStatus {
66 /** The Sone is unknown, i.e. not yet downloaded. */
69 /** The Sone is idle, i.e. not being downloaded or inserted. */
72 /** The Sone is currently being inserted. */
75 /** The Sone is currently being downloaded. */
80 private static final Logger logger = Logging.getLogger(Core.class);
83 private final Options options = new Options();
85 /** The core listener manager. */
86 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
88 /** The configuration. */
89 private Configuration configuration;
91 /** The identity manager. */
92 private final IdentityManager identityManager;
94 /** Interface to freenet. */
95 private final FreenetInterface freenetInterface;
97 /** The Sone downloader. */
98 private final SoneDownloader soneDownloader;
100 /** Whether the core has been stopped. */
101 private volatile boolean stopped;
103 /** The Sones’ statuses. */
104 /* synchronize access on itself. */
105 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
107 /** Locked local Sones. */
108 /* synchronize on itself. */
109 private final Set<Sone> lockedSones = new HashSet<Sone>();
111 /** Sone inserters. */
112 /* synchronize access on this on localSones. */
113 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
115 /** All local Sones. */
116 /* synchronize access on this on itself. */
117 private Map<String, Sone> localSones = new HashMap<String, Sone>();
119 /** All remote Sones. */
120 /* synchronize access on this on itself. */
121 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
123 /** All new Sones. */
124 private Set<String> newSones = new HashSet<String>();
126 /** All known Sones. */
127 /* synchronize access on {@link #newSones}. */
128 private Set<String> knownSones = new HashSet<String>();
131 private Map<String, Post> posts = new HashMap<String, Post>();
133 /** All new posts. */
134 private Set<String> newPosts = new HashSet<String>();
136 /** All known posts. */
137 /* synchronize access on {@link #newPosts}. */
138 private Set<String> knownPosts = new HashSet<String>();
141 private Map<String, Reply> replies = new HashMap<String, Reply>();
143 /** All new replies. */
144 private Set<String> newReplies = new HashSet<String>();
146 /** All known replies. */
147 private Set<String> knownReplies = new HashSet<String>();
149 /** Trusted identities, sorted by own identities. */
150 private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
153 * Creates a new core.
155 * @param configuration
156 * The configuration of the core
157 * @param freenetInterface
158 * The freenet interface
159 * @param identityManager
160 * The identity manager
162 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
163 this.configuration = configuration;
164 this.freenetInterface = freenetInterface;
165 this.identityManager = identityManager;
166 this.soneDownloader = new SoneDownloader(this, freenetInterface);
170 // LISTENER MANAGEMENT
174 * Adds a new core listener.
176 * @param coreListener
177 * The listener to add
179 public void addCoreListener(CoreListener coreListener) {
180 coreListenerManager.addListener(coreListener);
184 * Removes a core listener.
186 * @param coreListener
187 * The listener to remove
189 public void removeCoreListener(CoreListener coreListener) {
190 coreListenerManager.removeListener(coreListener);
198 * Sets the configuration to use. This will automatically save the current
199 * configuration to the given configuration.
201 * @param configuration
202 * The new configuration to use
204 public void setConfiguration(Configuration configuration) {
205 this.configuration = configuration;
210 * Returns the options used by the core.
212 * @return The options of the core
214 public Options getOptions() {
219 * Returns whether the “Sone rescue mode” is currently activated.
221 * @return {@code true} if the “Sone rescue mode” is currently activated,
222 * {@code false} if it is not
224 public boolean isSoneRescueMode() {
225 return options.getBooleanOption("SoneRescueMode").get();
229 * Returns the identity manager used by the core.
231 * @return The identity manager
233 public IdentityManager getIdentityManager() {
234 return identityManager;
238 * Returns the status of the given Sone.
241 * The Sone to get the status for
242 * @return The status of the Sone
244 public SoneStatus getSoneStatus(Sone sone) {
245 synchronized (soneStatuses) {
246 return soneStatuses.get(sone);
251 * Sets the status of the given Sone.
254 * The Sone to set the status of
258 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
259 synchronized (soneStatuses) {
260 soneStatuses.put(sone, soneStatus);
265 * Returns whether the given Sone is currently locked.
269 * @return {@code true} if the Sone is locked, {@code false} if it is not
271 public boolean isLocked(Sone sone) {
272 synchronized (lockedSones) {
273 return lockedSones.contains(sone);
278 * Returns all Sones, remote and local.
282 public Set<Sone> getSones() {
283 Set<Sone> allSones = new HashSet<Sone>();
284 allSones.addAll(getLocalSones());
285 allSones.addAll(getRemoteSones());
290 * Returns the Sone with the given ID, regardless whether it’s local or
294 * The ID of the Sone to get
295 * @return The Sone with the given ID, or {@code null} if there is no such
298 public Sone getSone(String id) {
299 return getSone(id, true);
303 * Returns the Sone with the given ID, regardless whether it’s local or
307 * The ID of the Sone to get
309 * {@code true} to create a new Sone if none exists,
310 * {@code false} to return {@code null} if a Sone with the given
312 * @return The Sone with the given ID, or {@code null} if there is no such
315 public Sone getSone(String id, boolean create) {
316 if (isLocalSone(id)) {
317 return getLocalSone(id);
319 return getRemoteSone(id, create);
323 * Checks whether the core knows a Sone with the given ID.
327 * @return {@code true} if there is a Sone with the given ID, {@code false}
330 public boolean hasSone(String id) {
331 return isLocalSone(id) || isRemoteSone(id);
335 * Returns whether the given Sone is a local Sone.
338 * The Sone to check for its locality
339 * @return {@code true} if the given Sone is local, {@code false} otherwise
341 public boolean isLocalSone(Sone sone) {
342 synchronized (localSones) {
343 return localSones.containsKey(sone.getId());
348 * Returns whether the given ID is the ID of a local Sone.
351 * The Sone ID to check for its locality
352 * @return {@code true} if the given ID is a local Sone, {@code false}
355 public boolean isLocalSone(String id) {
356 synchronized (localSones) {
357 return localSones.containsKey(id);
362 * Returns all local Sones.
364 * @return All local Sones
366 public Set<Sone> getLocalSones() {
367 synchronized (localSones) {
368 return new HashSet<Sone>(localSones.values());
373 * Returns the local Sone with the given ID.
376 * The ID of the Sone to get
377 * @return The Sone with the given ID
379 public Sone getLocalSone(String id) {
380 return getLocalSone(id, true);
384 * Returns the local Sone with the given ID, optionally creating a new Sone.
389 * {@code true} to create a new Sone if none exists,
390 * {@code false} to return null if none exists
391 * @return The Sone with the given ID, or {@code null}
393 public Sone getLocalSone(String id, boolean create) {
394 synchronized (localSones) {
395 Sone sone = localSones.get(id);
396 if ((sone == null) && create) {
398 localSones.put(id, sone);
405 * Returns all remote Sones.
407 * @return All remote Sones
409 public Set<Sone> getRemoteSones() {
410 synchronized (remoteSones) {
411 return new HashSet<Sone>(remoteSones.values());
416 * Returns the remote Sone with the given ID.
419 * The ID of the remote Sone to get
420 * @return The Sone with the given ID
422 public Sone getRemoteSone(String id) {
423 return getRemoteSone(id, true);
427 * Returns the remote Sone with the given ID.
430 * The ID of the remote Sone to get
432 * {@code true} to always create a Sone, {@code false} to return
433 * {@code null} if no Sone with the given ID exists
434 * @return The Sone with the given ID
436 public Sone getRemoteSone(String id, boolean create) {
437 synchronized (remoteSones) {
438 Sone sone = remoteSones.get(id);
439 if ((sone == null) && create) {
441 remoteSones.put(id, sone);
448 * Returns whether the given Sone is a remote Sone.
452 * @return {@code true} if the given Sone is a remote Sone, {@code false}
455 public boolean isRemoteSone(Sone sone) {
456 synchronized (remoteSones) {
457 return remoteSones.containsKey(sone.getId());
462 * Returns whether the Sone with the given ID is a remote Sone.
465 * The ID of the Sone to check
466 * @return {@code true} if the Sone with the given ID is a remote Sone,
467 * {@code false} otherwise
469 public boolean isRemoteSone(String id) {
470 synchronized (remoteSones) {
471 return remoteSones.containsKey(id);
476 * Returns whether the given Sone is a new Sone. After this check, the Sone
477 * is marked as known, i.e. a second call with the same parameters will
478 * always yield {@code false}.
481 * The sone to check for
482 * @return {@code true} if the given Sone is new, false otherwise
484 public boolean isNewSone(Sone sone) {
485 synchronized (newSones) {
486 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
487 knownSones.add(sone.getId());
489 coreListenerManager.fireMarkSoneKnown(sone);
496 * Returns whether the given Sone has been modified.
499 * The Sone to check for modifications
500 * @return {@code true} if a modification has been detected in the Sone,
501 * {@code false} otherwise
503 public boolean isModifiedSone(Sone sone) {
504 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
508 * Returns whether the target Sone is trusted by the origin Sone.
514 * @return {@code true} if the target Sone is trusted by the origin Sone
516 public boolean isSoneTrusted(Sone origin, Sone target) {
517 return trustedIdentities.containsKey(origin) && trustedIdentities.get(origin.getIdentity()).contains(target);
521 * Returns the post with the given ID.
524 * The ID of the post to get
525 * @return The post, or {@code null} if there is no such post
527 public Post getPost(String postId) {
528 return getPost(postId, true);
532 * Returns the post with the given ID, optionally creating a new post.
535 * The ID of the post to get
537 * {@code true} it create a new post if no post with the given ID
538 * exists, {@code false} to return {@code null}
539 * @return The post, or {@code null} if there is no such post
541 public Post getPost(String postId, boolean create) {
542 synchronized (posts) {
543 Post post = posts.get(postId);
544 if ((post == null) && create) {
545 post = new Post(postId);
546 posts.put(postId, post);
553 * Returns whether the given post ID is new. After this method returns it is
554 * marked a known post ID.
558 * @return {@code true} if the post is considered to be new, {@code false}
561 public boolean isNewPost(String postId) {
562 return isNewPost(postId, true);
566 * Returns whether the given post ID is new. If {@code markAsKnown} is
567 * {@code true} then after this method returns the post ID is marked a known
573 * {@code true} to mark the post ID as known, {@code false} to
574 * not to mark it as known
575 * @return {@code true} if the post is considered to be new, {@code false}
578 public boolean isNewPost(String postId, boolean markAsKnown) {
579 synchronized (newPosts) {
580 boolean isNew = !knownPosts.contains(postId) && newPosts.contains(postId);
582 Post post = getPost(postId, false);
592 * Returns the reply with the given ID. If there is no reply with the given
593 * ID yet, a new one is created.
596 * The ID of the reply to get
599 public Reply getReply(String replyId) {
600 return getReply(replyId, true);
604 * Returns the reply with the given ID. If there is no reply with the given
605 * ID yet, a new one is created, unless {@code create} is false in which
606 * case {@code null} is returned.
609 * The ID of the reply to get
611 * {@code true} to always return a {@link Reply}, {@code false}
612 * to return {@code null} if no reply can be found
613 * @return The reply, or {@code null} if there is no such reply
615 public Reply getReply(String replyId, boolean create) {
616 synchronized (replies) {
617 Reply reply = replies.get(replyId);
618 if (create && (reply == null)) {
619 reply = new Reply(replyId);
620 replies.put(replyId, reply);
627 * Returns all replies for the given post, order ascending by time.
630 * The post to get all replies for
631 * @return All replies for the given post
633 public List<Reply> getReplies(Post post) {
634 Set<Sone> sones = getSones();
635 List<Reply> replies = new ArrayList<Reply>();
636 for (Sone sone : sones) {
637 for (Reply reply : sone.getReplies()) {
638 if (reply.getPost().equals(post)) {
643 Collections.sort(replies, Reply.TIME_COMPARATOR);
648 * Returns whether the reply with the given ID is new.
651 * The ID of the reply to check
652 * @return {@code true} if the reply is considered to be new, {@code false}
655 public boolean isNewReply(String replyId) {
656 return isNewReply(replyId, true);
660 * Returns whether the reply with the given ID is new.
663 * The ID of the reply to check
665 * {@code true} to mark the reply as known, {@code false} to not
666 * to mark it as known
667 * @return {@code true} if the reply is considered to be new, {@code false}
670 public boolean isNewReply(String replyId, boolean markAsKnown) {
671 synchronized (newReplies) {
672 boolean isNew = !knownReplies.contains(replyId) && newReplies.contains(replyId);
674 Reply reply = getReply(replyId, false);
676 markReplyKnown(reply);
684 * Returns all Sones that have liked the given post.
687 * The post to get the liking Sones for
688 * @return The Sones that like the given post
690 public Set<Sone> getLikes(Post post) {
691 Set<Sone> sones = new HashSet<Sone>();
692 for (Sone sone : getSones()) {
693 if (sone.getLikedPostIds().contains(post.getId())) {
701 * Returns all Sones that have liked the given reply.
704 * The reply to get the liking Sones for
705 * @return The Sones that like the given reply
707 public Set<Sone> getLikes(Reply reply) {
708 Set<Sone> sones = new HashSet<Sone>();
709 for (Sone sone : getSones()) {
710 if (sone.getLikedReplyIds().contains(reply.getId())) {
722 * Locks the given Sone. A locked Sone will not be inserted by
723 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
729 public void lockSone(Sone sone) {
730 synchronized (lockedSones) {
731 lockedSones.add(sone);
736 * Unlocks the given Sone.
738 * @see #lockSone(Sone)
742 public void unlockSone(Sone sone) {
743 synchronized (lockedSones) {
744 lockedSones.remove(sone);
749 * Adds a local Sone from the given ID which has to be the ID of an own
753 * The ID of an own identity to add a Sone for
754 * @return The added (or already existing) Sone
756 public Sone addLocalSone(String id) {
757 synchronized (localSones) {
758 if (localSones.containsKey(id)) {
759 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
760 return localSones.get(id);
762 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
763 if (ownIdentity == null) {
764 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
767 return addLocalSone(ownIdentity);
772 * Adds a local Sone from the given own identity.
775 * The own identity to create a Sone from
776 * @return The added (or already existing) Sone
778 public Sone addLocalSone(OwnIdentity ownIdentity) {
779 if (ownIdentity == null) {
780 logger.log(Level.WARNING, "Given OwnIdentity is null!");
783 synchronized (localSones) {
786 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
787 } catch (MalformedURLException mue1) {
788 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
791 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
792 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
793 /* TODO - load posts ’n stuff */
794 localSones.put(ownIdentity.getId(), sone);
795 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
796 soneInserters.put(sone, soneInserter);
797 setSoneStatus(sone, SoneStatus.idle);
799 if (!isSoneRescueMode()) {
800 soneInserter.start();
802 new Thread(new Runnable() {
805 @SuppressWarnings("synthetic-access")
807 if (!isSoneRescueMode()) {
808 soneDownloader.fetchSone(sone);
811 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
812 coreListenerManager.fireRescuingSone(sone);
814 long edition = sone.getLatestEdition();
815 while (!stopped && (edition >= 0) && isSoneRescueMode()) {
816 logger.log(Level.FINE, "Downloading edition " + edition + "…");
817 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
820 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
822 coreListenerManager.fireRescuedSone(sone);
823 soneInserter.start();
826 }, "Sone Downloader").start();
832 * Creates a new Sone for the given own identity.
835 * The own identity to create a Sone for
836 * @return The created Sone
838 public Sone createSone(OwnIdentity ownIdentity) {
840 ownIdentity.addContext("Sone");
841 } catch (WebOfTrustException wote1) {
842 logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
845 Sone sone = addLocalSone(ownIdentity);
850 * Adds the Sone of the given identity.
853 * The identity whose Sone to add
854 * @return The added or already existing Sone
856 public Sone addRemoteSone(Identity identity) {
857 if (identity == null) {
858 logger.log(Level.WARNING, "Given Identity is null!");
861 synchronized (remoteSones) {
862 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
863 boolean newSone = sone.getRequestUri() == null;
864 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
865 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
867 synchronized (newSones) {
868 newSone = !knownSones.contains(sone.getId());
870 newSones.add(sone.getId());
874 coreListenerManager.fireNewSoneFound(sone);
877 remoteSones.put(identity.getId(), sone);
878 soneDownloader.addSone(sone);
879 setSoneStatus(sone, SoneStatus.unknown);
880 new Thread(new Runnable() {
883 @SuppressWarnings("synthetic-access")
885 soneDownloader.fetchSone(sone);
888 }, "Sone Downloader").start();
894 * Retrieves the trust relationship from the origin to the target.
897 * The origin of the trust tree
899 * The target of the trust
900 * @return The trust relationship
902 public Trust getTrust(Sone origin, Sone target) {
903 if (!isLocalSone(origin)) {
904 logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
908 return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
909 } catch (WebOfTrustException wote1) {
910 logger.log(Level.WARNING, "Could not get trust for Sone: " + target, wote1);
916 * Updates the stores Sone with the given Sone.
921 public void updateSone(Sone sone) {
922 if (hasSone(sone.getId())) {
923 boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
924 Sone storedSone = getSone(sone.getId());
925 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
926 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
929 synchronized (posts) {
930 if (!soneRescueMode) {
931 for (Post post : storedSone.getPosts()) {
932 posts.remove(post.getId());
933 if (!sone.getPosts().contains(post)) {
934 coreListenerManager.firePostRemoved(post);
938 synchronized (newPosts) {
939 for (Post post : sone.getPosts()) {
940 post.setSone(getSone(post.getSone().getId()));
941 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
942 newPosts.add(post.getId());
943 coreListenerManager.fireNewPostFound(post);
945 posts.put(post.getId(), post);
949 synchronized (replies) {
950 if (!soneRescueMode) {
951 for (Reply reply : storedSone.getReplies()) {
952 replies.remove(reply.getId());
953 if (!sone.getReplies().contains(reply)) {
954 coreListenerManager.fireReplyRemoved(reply);
958 synchronized (newReplies) {
959 for (Reply reply : sone.getReplies()) {
960 reply.setSone(getSone(reply.getSone().getId()));
961 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
962 newReplies.add(reply.getId());
963 coreListenerManager.fireNewReplyFound(reply);
965 replies.put(reply.getId(), reply);
969 synchronized (storedSone) {
970 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
971 storedSone.setTime(sone.getTime());
973 storedSone.setClient(sone.getClient());
974 storedSone.setProfile(sone.getProfile());
975 if (soneRescueMode) {
976 for (Post post : sone.getPosts()) {
977 storedSone.addPost(post);
979 for (Reply reply : sone.getReplies()) {
980 storedSone.addReply(reply);
982 for (String likedPostId : sone.getLikedPostIds()) {
983 storedSone.addLikedPostId(likedPostId);
985 for (String likedReplyId : sone.getLikedReplyIds()) {
986 storedSone.addLikedReplyId(likedReplyId);
989 storedSone.setPosts(sone.getPosts());
990 storedSone.setReplies(sone.getReplies());
991 storedSone.setLikePostIds(sone.getLikedPostIds());
992 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
994 storedSone.setLatestEdition(sone.getLatestEdition());
1000 * Deletes the given Sone. This will remove the Sone from the
1001 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
1002 * and remove the context from its identity.
1005 * The Sone to delete
1007 public void deleteSone(Sone sone) {
1008 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1009 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
1012 synchronized (localSones) {
1013 if (!localSones.containsKey(sone.getId())) {
1014 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
1017 localSones.remove(sone.getId());
1018 soneInserters.remove(sone).stop();
1021 ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
1022 ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
1023 } catch (WebOfTrustException wote1) {
1024 logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
1027 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
1028 } catch (ConfigurationException ce1) {
1029 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
1034 * Loads and updates the given Sone from the configuration. If any error is
1035 * encountered, loading is aborted and the given Sone is not changed.
1038 * The Sone to load and update
1040 public void loadSone(Sone sone) {
1041 if (!isLocalSone(sone)) {
1042 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1047 String sonePrefix = "Sone/" + sone.getId();
1048 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1049 if (soneTime == null) {
1050 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1053 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1056 Profile profile = new Profile();
1057 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1058 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1059 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1060 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1061 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1062 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1065 Set<Post> posts = new HashSet<Post>();
1067 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1068 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1069 if (postId == null) {
1072 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1073 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1074 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1075 if ((postTime == 0) || (postText == null)) {
1076 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1079 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1080 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1081 post.setRecipient(getSone(postRecipientId));
1087 Set<Reply> replies = new HashSet<Reply>();
1089 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1090 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1091 if (replyId == null) {
1094 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1095 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1096 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1097 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1098 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1101 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1104 /* load post likes. */
1105 Set<String> likedPostIds = new HashSet<String>();
1107 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1108 if (likedPostId == null) {
1111 likedPostIds.add(likedPostId);
1114 /* load reply likes. */
1115 Set<String> likedReplyIds = new HashSet<String>();
1117 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1118 if (likedReplyId == null) {
1121 likedReplyIds.add(likedReplyId);
1125 Set<String> friends = new HashSet<String>();
1127 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1128 if (friendId == null) {
1131 friends.add(friendId);
1134 /* if we’re still here, Sone was loaded successfully. */
1135 synchronized (sone) {
1136 sone.setTime(soneTime);
1137 sone.setProfile(profile);
1138 sone.setPosts(posts);
1139 sone.setReplies(replies);
1140 sone.setLikePostIds(likedPostIds);
1141 sone.setLikeReplyIds(likedReplyIds);
1142 sone.setFriends(friends);
1143 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1145 synchronized (newSones) {
1146 for (String friend : friends) {
1147 knownSones.add(friend);
1150 synchronized (newPosts) {
1151 for (Post post : posts) {
1152 knownPosts.add(post.getId());
1155 synchronized (newReplies) {
1156 for (Reply reply : replies) {
1157 knownReplies.add(reply.getId());
1163 * Saves the given Sone. This will persist all local settings for the given
1164 * Sone, such as the friends list and similar, private options.
1169 public void saveSone(Sone sone) {
1170 if (!isLocalSone(sone)) {
1171 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1174 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1175 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1179 logger.log(Level.INFO, "Saving Sone: %s", sone);
1181 ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1183 /* save Sone into configuration. */
1184 String sonePrefix = "Sone/" + sone.getId();
1185 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1186 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1189 Profile profile = sone.getProfile();
1190 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1191 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1192 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1193 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1194 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1195 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1198 int postCounter = 0;
1199 for (Post post : sone.getPosts()) {
1200 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1201 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1202 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1203 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1204 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1206 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1209 int replyCounter = 0;
1210 for (Reply reply : sone.getReplies()) {
1211 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1212 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1213 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1214 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1215 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1217 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1219 /* save post likes. */
1220 int postLikeCounter = 0;
1221 for (String postId : sone.getLikedPostIds()) {
1222 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1224 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1226 /* save reply likes. */
1227 int replyLikeCounter = 0;
1228 for (String replyId : sone.getLikedReplyIds()) {
1229 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1231 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1234 int friendCounter = 0;
1235 for (String friendId : sone.getFriends()) {
1236 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1238 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1240 logger.log(Level.INFO, "Sone %s saved.", sone);
1241 } catch (ConfigurationException ce1) {
1242 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1243 } catch (WebOfTrustException wote1) {
1244 logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
1249 * Creates a new post.
1252 * The Sone that creates the post
1254 * The text of the post
1255 * @return The created post
1257 public Post createPost(Sone sone, String text) {
1258 return createPost(sone, System.currentTimeMillis(), text);
1262 * Creates a new post.
1265 * The Sone that creates the post
1267 * The time of the post
1269 * The text of the post
1270 * @return The created post
1272 public Post createPost(Sone sone, long time, String text) {
1273 return createPost(sone, null, time, text);
1277 * Creates a new post.
1280 * The Sone that creates the post
1282 * The recipient Sone, or {@code null} if this post does not have
1285 * The text of the post
1286 * @return The created post
1288 public Post createPost(Sone sone, Sone recipient, String text) {
1289 return createPost(sone, recipient, System.currentTimeMillis(), text);
1293 * Creates a new post.
1296 * The Sone that creates the post
1298 * The recipient Sone, or {@code null} if this post does not have
1301 * The time of the post
1303 * The text of the post
1304 * @return The created post
1306 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1307 if (!isLocalSone(sone)) {
1308 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1311 Post post = new Post(sone, time, text);
1312 if (recipient != null) {
1313 post.setRecipient(recipient);
1315 synchronized (posts) {
1316 posts.put(post.getId(), post);
1318 synchronized (newPosts) {
1319 knownPosts.add(post.getId());
1327 * Deletes the given post.
1330 * The post to delete
1332 public void deletePost(Post post) {
1333 if (!isLocalSone(post.getSone())) {
1334 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1337 post.getSone().removePost(post);
1338 synchronized (posts) {
1339 posts.remove(post.getId());
1341 saveSone(post.getSone());
1345 * Marks the given post as known, if it is currently a new post (according
1346 * to {@link #isNewPost(String)}).
1349 * The post to mark as known
1351 public void markPostKnown(Post post) {
1352 synchronized (newPosts) {
1353 if (newPosts.remove(post.getId())) {
1354 knownPosts.add(post.getId());
1355 coreListenerManager.fireMarkPostKnown(post);
1361 * Creates a new reply.
1364 * The Sone that creates the reply
1366 * The post that this reply refers to
1368 * The text of the reply
1369 * @return The created reply
1371 public Reply createReply(Sone sone, Post post, String text) {
1372 return createReply(sone, post, System.currentTimeMillis(), text);
1376 * Creates a new reply.
1379 * The Sone that creates the reply
1381 * The post that this reply refers to
1383 * The time of the reply
1385 * The text of the reply
1386 * @return The created reply
1388 public Reply createReply(Sone sone, Post post, long time, String text) {
1389 if (!isLocalSone(sone)) {
1390 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1393 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1394 synchronized (replies) {
1395 replies.put(reply.getId(), reply);
1397 synchronized (newReplies) {
1398 knownReplies.add(reply.getId());
1400 sone.addReply(reply);
1406 * Deletes the given reply.
1409 * The reply to delete
1411 public void deleteReply(Reply reply) {
1412 Sone sone = reply.getSone();
1413 if (!isLocalSone(sone)) {
1414 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1417 synchronized (replies) {
1418 replies.remove(reply.getId());
1420 sone.removeReply(reply);
1425 * Marks the given reply as known, if it is currently a new reply (according
1426 * to {@link #isNewReply(String)}).
1429 * The reply to mark as known
1431 public void markReplyKnown(Reply reply) {
1432 synchronized (newReplies) {
1433 if (newReplies.remove(reply.getId())) {
1434 knownReplies.add(reply.getId());
1435 coreListenerManager.fireMarkReplyKnown(reply);
1443 public void start() {
1444 loadConfiguration();
1450 public void stop() {
1451 synchronized (localSones) {
1452 for (SoneInserter soneInserter : soneInserters.values()) {
1453 soneInserter.stop();
1456 saveConfiguration();
1461 * Saves the current options.
1463 public void saveConfiguration() {
1464 /* store the options first. */
1466 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1467 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1468 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1469 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1471 /* save known Sones. */
1472 int soneCounter = 0;
1473 synchronized (newSones) {
1474 for (String knownSoneId : knownSones) {
1475 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1477 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1480 /* save known posts. */
1481 int postCounter = 0;
1482 synchronized (newPosts) {
1483 for (String knownPostId : knownPosts) {
1484 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1486 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1489 /* save known replies. */
1490 int replyCounter = 0;
1491 synchronized (newReplies) {
1492 for (String knownReplyId : knownReplies) {
1493 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1495 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1499 configuration.save();
1501 } catch (ConfigurationException ce1) {
1502 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1511 * Loads the configuration.
1513 @SuppressWarnings("unchecked")
1514 private void loadConfiguration() {
1515 /* create options. */
1516 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1519 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1520 SoneInserter.setInsertionDelay(newValue);
1524 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1525 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1526 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1528 /* read options from configuration. */
1529 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1530 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1531 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1532 options.getBooleanOption("ClearOnNextRestart").set(null);
1533 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1534 if (clearConfiguration) {
1535 /* stop loading the configuration. */
1539 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1540 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1542 /* load known Sones. */
1543 int soneCounter = 0;
1545 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1546 if (knownSoneId == null) {
1549 synchronized (newSones) {
1550 knownSones.add(knownSoneId);
1554 /* load known posts. */
1555 int postCounter = 0;
1557 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1558 if (knownPostId == null) {
1561 synchronized (newPosts) {
1562 knownPosts.add(knownPostId);
1566 /* load known replies. */
1567 int replyCounter = 0;
1569 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1570 if (knownReplyId == null) {
1573 synchronized (newReplies) {
1574 knownReplies.add(knownReplyId);
1581 * Generate a Sone URI from the given URI and latest edition.
1584 * The URI to derive the Sone URI from
1585 * @return The derived URI
1587 private FreenetURI getSoneUri(String uriString) {
1589 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1591 } catch (MalformedURLException mue1) {
1592 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1598 // INTERFACE IdentityListener
1605 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1606 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1607 if (ownIdentity.hasContext("Sone")) {
1608 trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
1609 addLocalSone(ownIdentity);
1617 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1618 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1619 trustedIdentities.remove(ownIdentity);
1626 public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
1627 logger.log(Level.FINEST, "Adding Identity: " + identity);
1628 trustedIdentities.get(ownIdentity).add(identity);
1629 addRemoteSone(identity);
1636 public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
1637 new Thread(new Runnable() {
1640 @SuppressWarnings("synthetic-access")
1642 Sone sone = getRemoteSone(identity.getId());
1643 soneDownloader.fetchSone(sone);
1652 public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
1653 trustedIdentities.get(ownIdentity).remove(identity);