import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

public class send 
{
        private String username ;
        private String password ;
        private String toAddress ;
        private String proxyServer;
        private int proxyPort;
        private String Ms;
        
        /** 接続先 */
        private String SERVER_ADDRSS = "penta-02.ptgw.net";
        private int SERVER_PORT = 5222;
        private String SERVICE_NAME = "penta-02.ptgw.net";
        
        private ConnectionConfiguration connectionConfiguration ;
        private XMPPConnection xmppConnection;
        
        public static void main( String[] args ){
                send main = new send(args);
        }
        
        public send( String[] args ){
                if( args.length < 4){
                        System.out.println("usage : java XmppTest [UserAddress] [Password] [toAddress] [message]");
                }
                username = args[0];
                password = args[1];
                toAddress = args[2] + "@" + SERVER_ADDRSS;
                Ms = args[3];
                
                if( args.length >= 6 ){
                        proxyServer = args[3];
                        proxyPort = Integer.parseInt(args[4]);
                }
                
                try {
                        System.out.println("Connecting...");
                        connect();  // 接続
                        
                } catch (XMPPException e) {
                        System.out.print("connection failed. message="+e.getMessage());
                        return;
                }
                
                //送信結果をログに出力
                try {
	                	File file = new File("/www/ansys/xmpp/log/log.txt");
	                	
						if (checkBeforeWritefile(file))
						{
							PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
							
							Calendar now = Calendar.getInstance(); //インスタンス化
        					SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
							//pw.println("メッセージ送信完了");
							pw.println("[UserAddress]" + username);
							pw.println("[ToAddress]" + toAddress);
							pw.println("[Message]" + Ms);
							pw.println("[SendTime]" + sdf.format(now.getTime()));
							//pw.println();
        		        	pw.close();
        		        }
        		        else
        		        {
        		        	//System.out.println("ファイルに書き込めません");
        		        }
  				}catch(IOException e){
      					System.out.println(e);
    			}
                
                //System.out.println("Success!");
		    	//String post = "test";
		    	///Log.d(TAG, "send=>" + post);

		        Message msg = new Message(toAddress, Message.Type.chat);
		        System.out.println(Ms);
		        msg.setBody(Ms);
				xmppConnection.sendPacket(msg);
				
				
                /*
                try {
                        System.out.println("Sending message...");
                        //sendMessage(); // メッセージ送受信
                } catch (XMPPException e) {
                        System.out.print("Failed to send message. message="+e.getMessage());
                        return;
                } catch (IOException e) {
                        System.out.print("IOException occured. message="+e.getMessage());
                        return;
                }
                */
        }
        
         //接続する。
         //@throws XMPPException
        private void connect() throws XMPPException{
                ProxyInfo proxyInfo = null; 
                if( proxyServer != null ){
                  proxyInfo = new ProxyInfo(ProxyType.HTTP, proxyServer , proxyPort , "", "");
                }
                if( proxyInfo == null){
                        connectionConfiguration = 
                                new ConnectionConfiguration(SERVER_ADDRSS, SERVER_PORT,SERVICE_NAME);
                }else{
                        connectionConfiguration = 
                                new ConnectionConfiguration(SERVER_ADDRSS, SERVER_PORT,SERVICE_NAME,proxyInfo);
                }
                xmppConnection = new XMPPConnection(connectionConfiguration);
                xmppConnection.connect();
                xmppConnection.login(username, password);
        }
        
        //ファイル有無をチェック
		private static boolean checkBeforeWritefile(File file){
			if (file.exists()){
				if (file.isFile() && file.canWrite())
				{
					return true;
				}
    		}
		    return false;
		}

}
