View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.util;
25  
26  /* Author:           Lyndon Lee
27     Program:          MMX.java 
28     Created Date:     130597
29     Last Update Date: 130597
30     Function:         Agent's Multimedia Extension.
31     Modifications:
32  */
33  
34  import java.util.*;
35  import java.awt.*;
36  import java.awt.event.*;
37  import java.io.*;
38  import java.net.*;
39  
40  
41  public class MMX {
42  
43      private String   SessionID;
44      private String   MMXTarget;
45      private String   AgentID;
46      private int      mediaType;  
47      private String   string;
48  
49      public final static int EMAIL = 1;
50      public final static int PHONE = 2;
51      public final static int VOICE = 3;
52      public final static int POPUP = 4;
53  
54      public MMX() {}
55  
56  
57      public void setSessionID(String s)  { SessionID = s; }
58      public void setAgentID(String s)    { AgentID = s; }
59      public void setString(String s)     { string = s; }
60      public void setMMXTarget(String s)  { MMXTarget = s; }
61      public void setMediaType(int i)     { mediaType = i; }
62  
63      public void informClient(String session, int i, String target) {
64          if ( string == null ) return;
65          setSessionID(session); 
66          setMediaType(i); 
67          setMMXTarget(target);
68          informClient();
69      }
70      public void informClient() {
71          if ( string == null ) return;
72          switch (mediaType) {
73              case EMAIL:
74                  sendEmail();
75                  break;
76              case PHONE:
77                  phone();
78                  break;
79              case VOICE:
80                  speak(true);
81                  break;
82              case POPUP:
83                  speak(false);
84          }
85      }
86  
87      public void speak(boolean Voice) {
88  
89          Process child = null;
90          Socket speakSocket = null;
91          DataInputStream in = null;
92          PrintStream out = null;
93  
94          InetAddress ip = null;
95          try {
96              ip = InetAddress.getByName(MMXTarget);
97          } catch (UnknownHostException e) {
98              System.out.println("IP address of " + MMXTarget + " cannot be found");
99          }
100         // System.out.println("IP address of " + MMXTarget + " is " + ip);
101 
102         while (speakSocket == null) {
103             try {
104                 speakSocket = new Socket (ip,1234);
105                 in = new DataInputStream(speakSocket.getInputStream());
106                 out = new PrintStream(speakSocket.getOutputStream());
107 	    } catch (IOException e1) {
108 
109                 if (child == null) {
110                     System.out.println("creating remote process...\n");
111                     try {
112                         child = Runtime.getRuntime().exec("rsh " + MMXTarget + " jjj");
113                     } catch(IOException e) {
114                         e.printStackTrace();
115                     }
116                     // System.out.println(" process OK...\n");
117                 } // end if (child = null)
118                 else {
119 
120                     System.out.println("Waiting to connect to MMX2");
121                     try {
122                         Thread.sleep(2000);
123                     } catch(InterruptedException e ) {}
124                     //System.out.println("woke up");
125                 } // end else
126             } // end catch (IOException e1) 
127         } // end while (speakSocket == null)
128 
129 // System.out.println("1234 opened");
130         try {
131             String ss;
132 
133             if ((ss = in.readLine()) != null) {
134                 //System.out.println("MMX2 says:: " + ss);
135                 //System.out.println("sending " + s);
136 
137                 out.println(Voice? "Voice": "PopUp");
138                 out.flush();
139 
140                 out.println("AgentID");
141                 out.flush();
142                 out.println(AgentID);
143                 out.flush();
144 
145                 out.println("Messages");
146                 out.flush();
147                 //System.out.println("sending " + string + " " + i);
148                 out.println(string);
149                 out.flush();
150                 out.println("End");
151                 out.flush();
152                 out.println("Bye");
153                 out.flush();
154             } // end if ((ss = in.readLine()) != null)
155 
156             if ((ss = in.readLine()) != null) {
157                 //System.out.println("MMX2 says:: " + ss);
158             }
159 
160             out.close();
161             in.close();
162             speakSocket.close();
163         } catch (IOException e) {
164                 System.out.println("Unable to speak");
165         }
166     }
167 
168     public void phone() {
169         Socket phoneSocket;
170         PrintStream out;
171 
172         String message = 
173                        "\r\nrequest_identity= "                 
174                      + null                             
175                      + "\r\nservice_instance_identity= "
176                      + "0005"
177                      + "\r\nstart_date_time= "
178                      + null
179                      + "\r\nstop_date_time= "
180                      + null
181                      + "\r\nsender_telephone_number= "
182                      + "01473649733"
183                      + "\r\nsender_name= "
184                      + AgentID                 //200597
185                      + "\r\nsender_e-mail_address= "
186                      + "abc@xyz"
187                      + "\r\nsender_fax_number= "
188                      + null
189                      + "\r\nsender_pager_number= "
190                      + null
191                      + "\r\nsender_SMS_number= "
192                      + null
193                      + "\r\nnumber_of_receivers= "
194                      + "1"
195                      + "\r\nreceivers_telephone_number(s)= "
196                      + MMXTarget               //200597
197                      + "\r\nreceivers_name= "
198                      + null
199                      + "\r\nreceivers_e-mail_address= "
200                      + null
201                      + "\r\nreceivers_fax_number= "
202                      + null
203                      + "\r\nreceivers_pager_number= "
204                      + null
205                      + "\r\nreceivers_SMS_number= "
206                      + null
207                      + "\r\nreceivers_status= "
208                      + null
209                      + "\r\nconfirmation_e-mail_address= "
210                      + null
211                      + "\r\nstart_now= "
212                      + "YES"
213                      + "\r\ndelete_service_instance= "
214                      + null
215                      + "\r\ncredit_card_pin= "   
216                      + null
217                      + "\r\ncredit_card_number= "
218                      + null
219                      + "\r\nmessage_start=\r\n"
220                      + string                    //200597
221                      + "\r\nmessage_stop=\r\n";
222 
223 
224         try {
225             phoneSocket = new Socket ("132.146.209.102", 4444);
226             out = new PrintStream(phoneSocket.getOutputStream());
227 
228             try {
229 
230                 out.println(message);
231                 out.flush();
232 
233                 out.close();
234                 phoneSocket.close();
235                 //System.out.println("Client closing ...");
236             } catch (IOException e) {
237                 System.out.println("Unable to make phone call");
238             }
239 
240 	} catch (IOException e1) {
241             System.out.println("Unable to connect to MAP");
242         }
243     }
244 
245 
246     public void sendEmail() {
247         Socket mailSession;
248 // establish connection
249         try {
250             mailSession = new Socket("mailhost", 25);
251 
252             DataInputStream in = 
253                 new DataInputStream(mailSession.getInputStream());
254             DataOutputStream out = 
255                 new DataOutputStream(mailSession.getOutputStream());
256 
257 // check dialogue from SMTP
258 
259         String resp = getResponse(in);
260         if (resp.charAt(0) != '2')
261             throw new IOException("Bad SMTP dialogue\n");
262 
263         resp = mailCommand("HELO", in, out);
264         if (resp.charAt(0) != '2')
265             throw new IOException("Bad SMTP dialogue\n");
266 
267         resp = mailCommand("MAIL FROM:" + AgentID, in, out);
268         if (resp.charAt(0) != '2')
269             throw new IOException("Bad SMTP dialogue\n");
270 
271         resp = mailCommand("RCPT TO:" + MMXTarget, in, out);
272         if (resp.charAt(0) != '2')
273             throw new IOException("Bad SMTP dialogue\n");
274 
275         resp = mailCommand("DATA", in, out);
276         if (resp.charAt(0) != '3')
277             throw new IOException("Bad SMTP dialogue\n");
278 
279         out.writeBytes(string+"\n");
280 
281         resp = mailCommand(".", in, out);
282         if (resp.charAt(0) != '2')
283             throw new IOException("Bad SMTP dialogue\n");
284 
285 // close connection
286         mailSession.close();
287         } 
288         catch (IOException e )
289         {  System.err.println("Unable to open SMTP connection.\n"); }
290 
291     }
292 
293     public String getResponse(DataInputStream in) {
294         String resp = "";
295         for (;;) {
296             try {
297                 String line = in.readLine();
298 
299                 if (line == null) 
300                     throw new IOException("Bad SMTP response\n");
301                 if (line.length() < 3)
302                     throw new IOException("Bad SMTP response\n");
303                 resp += line + "\n";
304                 if (line.length() == 3 || line.charAt(3) != '-')
305                     return resp;
306             }
307             catch (IOException e ) {}
308         }
309     }
310 
311     public String mailCommand(String s, DataInputStream in, DataOutputStream out) {
312         try {
313             out.writeBytes(s + "\n");
314             return getResponse(in);
315         } 
316         catch (IOException e )
317         {  System.err.println("Unable to send command to SMTP server.\n"); }
318         return null;
319     }
320 
321     public static void main (String argv[]) {
322         MMX mmx = new MMX();
323         mmx.setAgentID("WonderAgent");
324         String a = "hello\nthis is second\nend";
325         mmx.setString(a);
326         mmx.informClient("Session Wonder", PHONE, "605666");
327         mmx.informClient("Session Wonder", EMAIL, "ndumudt@zion");
328     } 
329 
330 }