1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Map;
|
---|
6 |
|
---|
7 | import org.expeditee.agents.mail.MailSession;
|
---|
8 | import org.expeditee.gui.AttributeValuePair;
|
---|
9 | import org.expeditee.gui.Frame;
|
---|
10 | import org.expeditee.gui.FrameIO;
|
---|
11 | import org.expeditee.gui.FreeItems;
|
---|
12 | import org.expeditee.items.Item;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 |
|
---|
15 | public class MailTree extends DefaultAgent {
|
---|
16 |
|
---|
17 | @Override
|
---|
18 | public boolean initialise(Frame frame, Item item) {
|
---|
19 | return super.initialise(frame, item);
|
---|
20 | }
|
---|
21 |
|
---|
22 | private String getFrameText(Frame frame, String prefix) {
|
---|
23 | StringBuffer sb = new StringBuffer();
|
---|
24 | // Get the text to mail
|
---|
25 | for (Text t : frame.getBodyTextItems(false)) {
|
---|
26 | if (t.hasLink()) {
|
---|
27 | Frame linkedFrame = FrameIO.LoadFrame(t.getAbsoluteLink());
|
---|
28 | if (linkedFrame == null)
|
---|
29 | continue;
|
---|
30 |
|
---|
31 | String frameTitle = linkedFrame.getTitle();
|
---|
32 | sb.append('\n').append(prefix).append(frameTitle).append('\n')
|
---|
33 | .append(prefix);
|
---|
34 | // for (int i = 0; i < frameTitle.length(); i++) {
|
---|
35 | // sb.append('-');
|
---|
36 | // }
|
---|
37 | sb.append("----------------");
|
---|
38 |
|
---|
39 | sb.append("\n");
|
---|
40 | sb.append(getFrameText(linkedFrame, prefix));
|
---|
41 | } else {
|
---|
42 | sb.append(prefix).append(t.getText()).append("\n\n");
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | Text original = frame.getAnnotation("original");
|
---|
47 | if (original != null) {
|
---|
48 | Frame linkedFrame = FrameIO.LoadFrame(original.getAbsoluteLink());
|
---|
49 | if (linkedFrame != null) {
|
---|
50 | sb.append("\n\n");
|
---|
51 | String date = linkedFrame.getAnnotationValue("date");
|
---|
52 | if (date != null) {
|
---|
53 | sb.append("On ").append(date);
|
---|
54 | }
|
---|
55 | String from = linkedFrame.getAnnotationValue("from");
|
---|
56 | if (from != null) {
|
---|
57 | if (sb.length() > 0)
|
---|
58 | sb.append(", ");
|
---|
59 | //else
|
---|
60 | // sb.append("> ");
|
---|
61 | sb.append(from);
|
---|
62 | sb.append(" wrote:");
|
---|
63 | }
|
---|
64 | sb.append("\n\n");
|
---|
65 | sb.append(getFrameText(linkedFrame, ">"));
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | return sb.toString();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Email's a frame. The email addy's the mail will be sent to are taken from
|
---|
74 | * the frame tags of the form \@to:<AddressList>, \@cc:<AddressList> or
|
---|
75 | * \@bcc:<AddressList>. If such frame tags do not exist the agent looks for
|
---|
76 | * items on the end of the cursor. Different types of addresses can be
|
---|
77 | * grouped in boxes with a corner labelled, cc, bc or to. If a box does not
|
---|
78 | * have a label it is assumed to contain SendTo addresses.
|
---|
79 | */
|
---|
80 | @Override
|
---|
81 | protected Frame process(Frame frame) {
|
---|
82 | String subject = frame.getTitle();
|
---|
83 |
|
---|
84 | // Get the text to mail
|
---|
85 | String body = getFrameText(frame, "");
|
---|
86 |
|
---|
87 | String to = frame.getAnnotationValue("to");
|
---|
88 | String cc = frame.getAnnotationValue("cc");
|
---|
89 | String bcc = frame.getAnnotationValue("bcc");
|
---|
90 |
|
---|
91 | // Check for the address on the end of the cursor
|
---|
92 | if (to == null) {
|
---|
93 | StringBuffer ccString = new StringBuffer();
|
---|
94 | StringBuffer toString = new StringBuffer();
|
---|
95 | StringBuffer bccString = new StringBuffer();
|
---|
96 | // Check for a group of text items attached to the cursor
|
---|
97 | Map<String, Collection<String>> groupedText = FreeItems
|
---|
98 | .getGroupedText();
|
---|
99 | Collection<String> ccStrings = groupedText.get("cc");
|
---|
100 | Collection<String> toStrings = groupedText.get("to");
|
---|
101 | Collection<String> bccStrings = groupedText.get("bcc");
|
---|
102 |
|
---|
103 | if (cc == null && ccStrings != null) {
|
---|
104 | for (String address : ccStrings) {
|
---|
105 | if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
|
---|
106 | address = address.substring(1);
|
---|
107 | ccString.append(address);
|
---|
108 | ccString.append(',');
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (bcc == null && bccStrings != null) {
|
---|
113 | for (String address : bccStrings) {
|
---|
114 | if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
|
---|
115 | address = address.substring(1);
|
---|
116 | bccString.append(address);
|
---|
117 | bccString.append(',');
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (toStrings != null) {
|
---|
122 | for (String address : toStrings) {
|
---|
123 | if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
|
---|
124 | address = address.substring(1);
|
---|
125 | toString.append(address);
|
---|
126 | toString.append(',');
|
---|
127 | }
|
---|
128 | } else {
|
---|
129 | Collection<String> otherStrings = groupedText.get("");
|
---|
130 | for (String address : otherStrings) {
|
---|
131 | if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
|
---|
132 | address = address.substring(1);
|
---|
133 | if (address.startsWith("cc:") && address.length() > 5) {
|
---|
134 | ccString.append(address.substring(3).trim());
|
---|
135 | ccString.append(',');
|
---|
136 | } else if (address.startsWith("bcc:")
|
---|
137 | && address.length() > 6) {
|
---|
138 | bccString.append(address.substring(4).trim());
|
---|
139 | bccString.append(',');
|
---|
140 | } else {
|
---|
141 | toString.append(address);
|
---|
142 | toString.append(',');
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | if (toString.length() > 0) {
|
---|
147 | toString.deleteCharAt(toString.length() - 1);
|
---|
148 | to = toString.toString();
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (ccString.length() > 0) {
|
---|
152 | ccString.deleteCharAt(ccString.length() - 1);
|
---|
153 | cc = ccString.toString();
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (bccString.length() > 0) {
|
---|
157 | bccString.deleteCharAt(ccString.length() - 1);
|
---|
158 | bcc = bccString.toString();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Last chance for the user to stop
|
---|
163 | if (_stop)
|
---|
164 | return null;
|
---|
165 | // Allow the user to do other stuff while the message gets sent
|
---|
166 | _running = false;
|
---|
167 | MailSession.sendTextMessage(to, cc, bcc, subject, body, null);
|
---|
168 |
|
---|
169 | return null;
|
---|
170 | }
|
---|
171 | }
|
---|