summaryrefslogtreecommitdiff
path: root/NET/worlds/console/MailMessage.java
blob: fbc5828cea5807fe3c129eb610c271aea89ae189 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package NET.worlds.console;

import NET.worlds.network.DNSLookup;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Vector;

public class MailMessage extends Thread {
   private String server;
   private String from;
   private String to;
   private Vector<String> cc;
   private String subject;
   private Vector<String> body;
   private boolean lock = false;

   public MailMessage(String server) {
      this(server, null, null, null, null);
   }

   public MailMessage(String server, String from, String to) {
      this(server, from, to, null, null);
   }

   public MailMessage(String server, String from, String to, String subject, String body) {
      this.server = server;
      this.from = from;
      this.to = to;
      this.cc = new Vector<String>();
      this.subject = subject;
      this.body = new Vector<String>();
      if (body != null) {
         this.body.addElement(body);
      }
   }

   public void setFrom(String f) {
      if (!this.locked()) {
         this.from = f;
      }
   }

   public void setTo(String t) {
      if (!this.locked()) {
         this.to = t;
      }
   }

   public void addCC(String c) {
      if (!this.locked()) {
         this.cc.addElement(c);
      }
   }

   public void setSubject(String s) {
      if (!this.locked()) {
         this.subject = s;
      }
   }

   public void setBody(String b) {
      if (!this.locked()) {
         this.body = new Vector<String>();
         this.body.addElement(b);
      }
   }

   public void appendBody(String b) {
      if (!this.locked()) {
         this.body.addElement(b);
      }
   }

   public void appendParagraphs(String text, String sepStr, int lineLen) {
      if (!this.locked()) {
         String b = "";
         String white = "";
         String word = "";
         boolean startParagraph = false;
         int len = 0;

         for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (word.length() > 0 && Character.isWhitespace(c)) {
               len += white.length() + word.length();
               if (len > lineLen) {
                  b = b + sepStr;
                  len = word.length();
               } else {
                  b = b + white;
               }

               b = b + word;
               word = "";
               white = "";
            }

            if (c != '\n' && c != '\r') {
               startParagraph = false;
               if (Character.isWhitespace(c)) {
                  white = white + c;
               } else {
                  word = word + c;
               }
            } else if (!startParagraph) {
               len = 0;
               b = b + sepStr;
               b = b + sepStr;
               startParagraph = true;
            }
         }

         if (word.length() > 0) {
            b = b + white;
            b = b + word;
         }

         this.body.addElement(b);
      }
   }

   public void appendParagraphs(String text) {
      this.appendParagraphs(text, "\n", 70);
   }

   private synchronized void lockMessage() {
      this.lock = true;
   }

   private synchronized void unlockMessage() {
      this.lock = false;
   }

   private synchronized boolean locked() {
      return this.lock;
   }

   protected void finished(boolean wasError) {
   }

   public void send() {
      this.start();
   }

   @Override
   public void run() {
      this.lockMessage();
      this.from = Console.parseUnicode(this.from);
      this.to = Console.parseUnicode(this.to);
      this.cc = Console.parseUnicode(this.cc);
      this.subject = Console.parseUnicode(this.subject);
      this.body = Console.parseUnicode(this.body);
      boolean wasError = !sendNote(this.server, this.from, this.to, this.cc, this.subject, this.body);
      this.unlockMessage();
      this.finished(wasError);
   }

   public static synchronized boolean sendNote(String server, String from, String to, Vector<String> cc, String subject, Vector<String> body) {
      int port = 25;
      int cindex = server.indexOf(58);
      if (cindex != -1) {
         port = Integer.parseInt(server.substring(cindex + 1));
         server = server.substring(0, cindex);
      }

      Socket smtpSocket = null;

      label123: {
         try {
            smtpSocket = new Socket(DNSLookup.lookup(server), port);
            InputStream smtpIn = smtpSocket.getInputStream();
            PrintWriter smtpOut = new PrintWriter(smtpSocket.getOutputStream());
            if (getReply(smtpIn)
               && print(smtpOut, "HELO worlds.net\r\n")
               && getReply(smtpIn)
               && print(smtpOut, "MAIL FROM:<" + from + ">\r\n")
               && getReply(smtpIn)
               && print(smtpOut, "RCPT TO:<" + to + ">\r\n")
               && getReply(smtpIn)
               && printCCcmd(smtpOut, smtpIn, cc)
               && print(smtpOut, "DATA\r\n")
               && getReply(smtpIn)
               && print(smtpOut, "MIME-Version: 1.0\r\n")
               && print(smtpOut, "MContent-Type: text/plain; charset=\"us-ascii\"\r\n")
               && print(smtpOut, "From: " + from + "\r\n")
               && print(smtpOut, "To: " + to + "\r\n")
               && printCCline(smtpOut, cc)
               && print(smtpOut, "Subject: " + subject + "\r\n\r\n")
               && print(smtpOut, body)
               && print(smtpOut, "\r\n.\r\n")) {
               getReply(smtpIn);
            }
            break label123;
         } catch (IOException var19) {
            System.out.println("Error communicating with mail server: " + var19);
         } finally {
            try {
               if (smtpSocket != null) {
                  smtpSocket.close();
               }
            } catch (IOException var18) {
            }
         }

         return false;
      }

      System.out.println("MailMessage.sendNote: complete.");
      return true;
   }

   private static boolean print(PrintWriter out, String text) {
      out.print(text);
      return !out.checkError();
   }

   private static boolean print(PrintWriter out, Vector<String> text) {
      int size = text.size();

      for (int i = 0; i < size; i++) {
         out.print(text.elementAt(i));
         if (out.checkError()) {
            return false;
         }
      }

      return true;
   }

   private static boolean printCCcmd(PrintWriter out, InputStream in, Vector<String> cc) {
      if (cc.size() > 0) {
         Enumeration<String> en = cc.elements();

         while (en.hasMoreElements()) {
            String rcp = en.nextElement();
            if (!print(out, "RCPT TO:<" + rcp + ">\r\n")) {
               return false;
            }

            if (!getReply(in)) {
               return false;
            }
         }
      }

      return true;
   }

   private static boolean printCCline(PrintWriter out, Vector<String> cc) {
      int num = cc.size();
      if (num > 0) {
         if (!print(out, "cc: ")) {
            return false;
         }

         Enumeration<String> en = cc.elements();

         while (en.hasMoreElements()) {
            String rcp = en.nextElement();
            if (num-- > 1) {
               rcp = rcp + ", ";
            }

            if (!print(out, rcp)) {
               return false;
            }
         }

         if (!print(out, "\r\n")) {
            return false;
         }
      }

      return true;
   }

   private static boolean getReply(InputStream in) {
      try {
         int result;
         while ((result = in.read()) >= 0 && result != 10) {
         }

         return true;
      } catch (IOException var2) {
         return false;
      }
   }

   public static void main(String[] args) {
      Vector<String> body = new Vector<String>();
      body.addElement("Looks like it worked...\n");
      sendNote("www.3dcd.com:25", "Gamma Mail Service", "[email protected]", new Vector<String>(), "this is a test", body);
   }
}