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