Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/alicebot/server/core/processor/SystemProcessor.java


1   // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2   // Jad home page: http://www.geocities.com/kpdus/jad.html
3   // Decompiler options: packimports(3) 
4   
5   package org.alicebot.server.core.processor;
6   
7   import java.io.*;
8   import java.util.ArrayList;
9   import java.util.StringTokenizer;
10  import org.alicebot.server.core.Globals;
11  import org.alicebot.server.core.logging.Log;
12  import org.alicebot.server.core.parser.TemplateParser;
13  import org.alicebot.server.core.parser.XMLNode;
14  import org.alicebot.server.core.util.Toolkit;
15  
16  // Referenced classes of package org.alicebot.server.core.processor:
17  //            AIMLProcessor, AIMLProcessorException
18  
19  public class SystemProcessor extends AIMLProcessor
20  {
21  
22      public SystemProcessor()
23      {
24      }
25  
26      public String process(int i, XMLNode xmlnode, TemplateParser templateparser)
27          throws AIMLProcessorException
28      {
29          if(!Globals.osAccessAllowed())
30          {
31              Log.userinfo("Use of <system> prohibited!", Log.SYSTEM);
32              return "";
33          }
34          String s = Globals.getSystemDirectory();
35          String s1 = Globals.getSystemPrefix();
36          if(xmlnode.XMLType == 0)
37          {
38              String s2 = templateparser.evaluate(i++, xmlnode.XMLChild);
39              if(s1 != null)
40                  s2 = s1 + s2;
41              String s3 = "";
42              s2 = s2.trim();
43              Log.log("<system> call:", Log.SYSTEM);
44              Log.log(s2, Log.SYSTEM);
45              try
46              {
47                  File file = null;
48                  if(s != null)
49                  {
50                      Log.log("Executing <system> call in \"" + s + "\"", Log.SYSTEM);
51                      file = new File(s);
52                      if(!file.isDirectory())
53                      {
54                          Log.userinfo("programd.interpreter.system.directory (\"" + s + "\") does not exist or is not a directory.", Log.SYSTEM);
55                          return "";
56                      }
57                  } else
58                  {
59                      Log.userinfo("No programd.interpreter.system.directory defined!", Log.SYSTEM);
60                      return "";
61                  }
62                  Process process1;
63                  if(useArrayExecForm)
64                      process1 = Runtime.getRuntime().exec((String[])Toolkit.wordSplit(s2).toArray(new String[0]), null, file);
65                  else
66                      process1 = Runtime.getRuntime().exec(s2, null, file);
67                  if(process1 == null)
68                  {
69                      Log.userinfo("Could not get separate process for <system> command.", Log.SYSTEM);
70                      return "";
71                  }
72                  try
73                  {
74                      process1.waitFor();
75                  }
76                  catch(InterruptedException interruptedexception)
77                  {
78                      Log.userinfo("System process interruped; could not complete.", Log.SYSTEM);
79                      return "";
80                  }
81                  InputStream inputstream = process1.getInputStream();
82                  BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream));
83                  String s4;
84                  while((s4 = bufferedreader.readLine()) != null) 
85                      s3 = s3 + s4 + "\n";
86                  Log.log("output:", Log.SYSTEM);
87                  Log.log(s3, Log.SYSTEM);
88                  s2 = s3;
89                  inputstream.close();
90                  Log.userinfo("System process exit value: " + process1.exitValue(), Log.SYSTEM);
91              }
92              catch(IOException ioexception)
93              {
94                  Log.userinfo("Cannot execute <system> command.  Response logged.", Log.SYSTEM);
95                  for(StringTokenizer stringtokenizer = new StringTokenizer(ioexception.getMessage(), System.getProperty("line.separator")); stringtokenizer.hasMoreTokens(); Log.log(stringtokenizer.nextToken(), Log.SYSTEM));
96              }
97              return s2.trim();
98          } else
99          {
100             throw new AIMLProcessorException("<system></system> must have content!");
101         }
102     }
103 
104     public static final String label = "system";
105     private static final String arrayFormOSnames[] = {
106         "mac os x", "linux", "solaris", "sunos", "mpe", "hp-ux", "pa_risc", "aix", "freebsd", "irix", 
107         "unix"
108     };
109     private static boolean useArrayExecForm;
110 
111     static 
112     {
113         String s = System.getProperty("os.name").toLowerCase();
114         for(int i = arrayFormOSnames.length; --i >= 0;)
115             if(s.indexOf(arrayFormOSnames[i]) != -1)
116                 useArrayExecForm = true;
117 
118     }
119 }