import java.io.*; import java.util.Vector; import java.util.StringTokenizer; import java.net.*; import java.awt.*; import java.awt.event.*; import sun.audio.*; public final class JavaCMSetup implements WindowListener, ActionListener, ItemListener, TextListener, KeyListener { private static final boolean OS_Windows = System.getProperty("os.name", "").toUpperCase().indexOf("WINDOWS") != -1; private static final AudioPlayer ap = AudioPlayer.player; private static final Runtime runtime = Runtime.getRuntime(); private static FileInputStream current_stream = null; private static final String default_useragent = "Chat Monitor version 0.08a"; private static final int default_iotimeout = 60; private static String configfile = "cm.dat"; private static String configfile_encoding = null; private static final boolean DefaultMS932 = sun.io.ByteToCharConverter.getDefault().getCharacterEncoding().equals("MS932"); private static final String crlf = System.getProperty("line.separator", "\n"); private static String user_agent = default_useragent; private static int io_timeout = default_iotimeout * 1000; private static boolean use_proxy = false; private static String proxy_server = null; private static int proxy_port = 0; private static String currentpage = null; private static String cached_data = null; private static Vector global_title = new Vector(1, 1), global_url = new Vector(1, 1), global_encode = new Vector(1, 1), global_interval = new Vector(1, 1), global_sound = new Vector(1, 1), global_shell = new Vector(1, 1), global_ignoreuntil = new Vector(1, 1), global_ignoreafter = new Vector(1, 1), global_ignorelist = new Vector(1, 1); private static String local_title = null, local_url = null, local_encode = null, local_interval = null, local_sound = null, local_shell = null, local_ignoreuntil = null, local_ignoreafter = null, local_ignorelist = null; private static int listpos = 0; private static int setupstep = 0; private static int left = 0, top = 0; private static Vector item0 = new Vector(1, 1), item1 = new Vector(1, 1), item2 = new Vector(1, 1), item3 = new Vector(1, 1), item4 = new Vector(1, 1), item5 = new Vector(1, 1), item6 = new Vector(1, 1); //メインフォーム private static final Frame f = new Frame("Chat Monitor環境設定"); private static Font font = null; private static TextArea guide = null; private static Dimension fsize = null; //ダイアログ画面 private static final Dialog d1 = new Dialog(f, "Chat Monitor環境設定:終了", true), d2 = new Dialog(f, "Chat Monitor環境設定:確認", true); private static final Label MessageBox_TEXT = new Label(); //ステップ1 private static TextField text_useragent = null; private static TextField text_iotimeout = null; private static Checkbox check_proxy = null; private static TextField text_proxyserver = null; private static TextField text_proxyport = null; //ステップ2 private static TextField text_newtitle = null; private static List list_title = null; //ステップ3 private static TextField text_targeturl = null; private static TextArea text_responseheader = null; private static List list_responsebody = null; private static Choice choice_encoding = null; //ステップ4 private static TextField text_checkinterval = null; private static TextField text_soundfile = null; private static TextField text_ignorelist = null; private static TextField text_openpage = null; //ステップ5 private static TextField text_ignoreuntil = null; private static TextField text_ignoreafter = null; private static List list_comparebody = null; //ステップ6 private static TextArea text_localconfig = null; public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { if (e.getSource() == f) confirm_dialog(); else if (e.getSource() == d1) d1.dispose(); else d2.dispose(); } public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) { if (e.getKeyChar() == e.VK_ENTER) buttonAction(((Button) e.getSource()).getActionCommand()); } public void actionPerformed(ActionEvent e) { buttonAction(e.getActionCommand()); } private void buttonAction(String caption) { if (caption.equals("次へ")) { switch (setupstep) { case 1: user_agent = text_useragent.getText().trim(); if (user_agent.length() == 0) { MessageBox("UserAgentは省略できません。"); return; } try { io_timeout = Integer.parseInt(text_iotimeout.getText().trim()) * 1000; if (io_timeout <= 0) throw new IllegalArgumentException(); } catch (Exception e2) { MessageBox("タイムアウトの指定に誤りがあります。"); return; } use_proxy = false; if (check_proxy.getState()) { try { proxy_server = text_proxyserver.getText().trim(); proxy_port = Integer.parseInt(text_proxyport.getText().trim()); if (proxy_server.length() == 0 || proxy_port <= 0 || proxy_port > 65535) throw new IllegalArgumentException(); } catch (Exception e2) { MessageBox("プロキシの設定に誤りがあります。"); return; } use_proxy = true; } break; case 3: if (list_responsebody.getItemCount() == 0) { MessageBox("ページの内容がありません。"); return; } break; case 4: local_interval = text_checkinterval.getText().trim(); local_sound = text_soundfile.getText().trim(); local_shell = text_openpage.getText().trim(); try { int i = Integer.parseInt(local_interval); if (i < 10 || i > 3600) throw new IllegalArgumentException(); } catch (Exception e2) { MessageBox("チェックする時間間隔の指定が不適切です。"); return; } if (local_sound.length() == 0) { MessageBox("サウンドは省略できません。"); return; } File f = new File(local_sound); if (!f.isFile()) MessageBox("ファイル " + local_sound + " が見つかりません。音による通知はできません。"); break; case 5: if (list_comparebody.getItemCount() == 0) { MessageBox("チェックの対象となる範囲がありません。"); return; } break; } setupstep++; showWindow(); } else if (caption.equals("完了")) { if (listpos == -1) { list_title.add(local_title); global_title.addElement(local_title); text_newtitle.setText(""); global_url.addElement(local_url); global_encode.addElement(local_encode); global_interval.addElement(local_interval); global_sound.addElement(local_sound); global_shell.addElement(local_shell); global_ignoreuntil.addElement(local_ignoreuntil); global_ignoreafter.addElement(local_ignoreafter); global_ignorelist.addElement(local_ignorelist); } else { global_url.setElementAt(local_url, listpos); global_encode.setElementAt(local_encode, listpos); global_interval.setElementAt(local_interval, listpos); global_sound.setElementAt(local_sound, listpos); global_shell.setElementAt(local_shell, listpos); global_ignoreuntil.setElementAt(local_ignoreuntil, listpos); global_ignoreafter.setElementAt(local_ignoreafter, listpos); global_ignorelist.setElementAt(local_ignorelist, listpos); } setupstep = 2; showWindow(); } else if (caption.equals("中止")) { setupstep = 2; showWindow(); } else if (caption.equals("前へ")) { if (setupstep > 0) { setupstep--; showWindow(); } } else if (caption.equals("取得")) { local_url = text_targeturl.getText().trim(); if (local_url.toLowerCase().startsWith("http://") && local_url.length() > 8) { text_responseheader.setText("ページを取得しています。"); list_responsebody.removeAll(); if ((currentpage = GET(local_url)) != null) { local_encode = choice_encoding.getSelectedItem(); if (local_encode == null) local_encode = choice_encoding.getItem(0); showPage(local_encode); } else { text_responseheader.setText("ページを取得できませんでした。"); } } else { MessageBox("更新をチェックしたいURLを指定してください。"); } } else if (caption.equals("はい")) { d1.dispose(); saveSetting(); System.exit(0); } else if (caption.equals("いいえ")) { d1.dispose(); System.exit(0); } else if (caption.equals("キャンセル")) { d1.dispose(); } else if (caption.equals("登録")) { local_title = text_newtitle.getText().trim(); local_url = "http://"; local_encode = choice_encoding.getItem(0); local_interval = "30"; local_sound = local_shell = local_ignoreuntil = local_ignoreafter = local_ignorelist = ""; if (local_title.length() != 0) { listpos = -1; text_responseheader.setText(""); list_responsebody.removeAll(); setupstep = 3; showWindow(); } else { MessageBox("タイトルは省略できません。"); } } else if (caption.equals("変更")) { int i = list_title.getSelectedIndex(); if (i != -1) { listpos = i; local_title = (String) global_title.elementAt(i); local_url = (String) global_url.elementAt(i); local_encode = (String) global_encode.elementAt(i); local_interval = (String) global_interval.elementAt(i); local_sound = (String) global_sound.elementAt(i); local_shell = (String) global_shell.elementAt(i); local_ignoreuntil = (String) global_ignoreuntil.elementAt(i); local_ignoreafter = (String) global_ignoreafter.elementAt(i); local_ignorelist = (String) global_ignorelist.elementAt(i); text_responseheader.setText(""); list_responsebody.removeAll(); setupstep = 3; showWindow(); } else { MessageBox("変更したいタイトルを選択してください。"); } } else if (caption.equals("削除")) { int i = list_title.getSelectedIndex(); if (i != -1) { list_title.remove(i); global_title.removeElementAt(i); global_url.removeElementAt(i); global_encode.removeElementAt(i); global_interval.removeElementAt(i); global_sound.removeElementAt(i); global_shell.removeElementAt(i); global_ignoreuntil.removeElementAt(i); global_ignoreafter.removeElementAt(i); global_ignorelist.removeElementAt(i); } else { MessageBox("削除したいタイトルを選択してください。"); } } else if (caption.equals("確認")) { d2.dispose(); } else if (caption.equals("テスト")) { local_sound = text_soundfile.getText().trim(); if (local_sound.length() == 0) MessageBox("ファイルを指定してください。"); else { File f = new File(local_sound); if (!f.isFile()) MessageBox("ファイル " + local_sound + " が見つかりません。"); else play(local_sound); } } } public void itemStateChanged(ItemEvent e) { local_encode = (String) e.getItem(); showPage(local_encode); } public void textValueChanged(TextEvent e) { String result = cached_data; int i = 0; list_comparebody.removeAll(); local_ignoreuntil = text_ignoreuntil.getText().trim(); local_ignoreafter = text_ignoreafter.getText().trim(); local_ignorelist = text_ignorelist.getText().trim(); if (local_ignoreuntil.length() != 0) { if ((i = result.indexOf(local_ignoreuntil)) == -1) return; result = result.substring(i + local_ignoreuntil.length()); } if (local_ignoreafter.length() != 0) { if ((i = result.indexOf(local_ignoreafter)) == -1) return; result = result.substring(0, i); } if (local_ignorelist.length() != 0) { StringBuffer sb = new StringBuffer(result.length()); StringTokenizer st = new StringTokenizer(result, local_ignorelist); while (st.hasMoreTokens()) sb.append(st.nextToken()); result = sb.toString(); } if (result.length() == 0) return; StringTokenizer st1 = new StringTokenizer(result, "\r\n"); list_comparebody.setVisible(false); while (st1.hasMoreTokens()) list_comparebody.add(st1.nextToken()); list_comparebody.setVisible(true); } private static void play(String soundfile) { if (soundfile != null) { if (OS_Windows && soundfile.toUpperCase().endsWith(".WAV")) { synchronized (runtime) { try { runtime.exec("RUNDLL MMSYSTEM,sndPlaySound " + soundfile); } catch (Exception e) { System.out.println(e); } } } else { synchronized (ap) { try { if (current_stream != null) { ap.stop(current_stream); current_stream.close(); } current_stream = new FileInputStream(soundfile); ap.start(current_stream); } catch (Exception e) { System.out.println(e); } System.gc(); } } } } private void showPage(String encoding) { String contents = ""; int i = 0; byte temp[] = null; list_responsebody.removeAll(); if (currentpage == null) { text_responseheader.setText("文字コードを選ぶ前に、ページを取得してください。"); return; } try { if ((i = currentpage.indexOf("\r\n\r\n")) != -1) { contents = currentpage.substring(0, i); temp = currentpage.substring(i + 4).getBytes("8859_1"); } else if ((i = currentpage.indexOf("\n\n")) != -1) { contents = currentpage.substring(0, i); temp = currentpage.substring(i + 2).getBytes("8859_1"); } else { contents = currentpage; text_responseheader.setText("取得に失敗しました。"); return; } StringBuffer sb = new StringBuffer(contents.length()); StringTokenizer st = new StringTokenizer(contents, "\r\n"); while (st.hasMoreTokens()) sb.append(st.nextToken() + crlf); contents = sb.toString(); if ((i = contents.lastIndexOf(crlf)) != -1) contents = contents.substring(0, i); text_responseheader.setText(contents); contents = new String(temp, encoding); } catch (UnsupportedEncodingException e) { if (encoding.equals("EUC_JP_LINUX") && temp != null) { contents = EUC2UnicodeSJIS(temp); } else { text_responseheader.setText("文字コードの変換に失敗しました。"); return; } } if (DefaultMS932) { if (encoding.startsWith("JIS") || encoding.equals("SJIS") || encoding.startsWith("EUC_JP") || encoding.equals("ISO-2022-JP") || encoding.startsWith("CP943")) contents = UNICODEforMS932(contents); } contents = contents.replace('\t', ' '); StringTokenizer st1 = new StringTokenizer(contents, "\r\n"); list_responsebody.setVisible(false); while (st1.hasMoreTokens()) list_responsebody.add(st1.nextToken()); list_responsebody.setVisible(true); cached_data = contents; } JavaCMSetup() { //メインフォーム f.setBackground(Color.lightGray); f.setLayout(null); f.setLocation(50, 50); f.setVisible(true); font = new Font(f.getFont().getName(), Font.PLAIN, 14); f.setFont(font); f.validate(); Insets in = f.getInsets(); fsize = new Dimension(640 + in.left + in.right + 6, 383 + in.top + in.bottom + 6); f.addWindowListener(this); left = in.left + 3; top = in.top + 3; f.setSize(640 + in.left + in.right + 6, in.top + in.bottom + 6); f.validate(); f.setResizable(false); guide = newTextArea(0, 0, 0, 0, 0, 0); //終了確認画面 d1.setLayout(new FlowLayout()); Panel backp = new Panel(); backp.setLayout(new GridLayout(2, 1)); Panel buttonp = new Panel(); Label msg = new Label("変更を保存しますか?"); Button yes = new Button("はい"); Button no = new Button("いいえ"); Button cancel = new Button("キャンセル"); buttonp.add(yes); buttonp.add(no); buttonp.add(cancel); backp.add(msg); backp.add(buttonp); yes.addActionListener(this); yes.addKeyListener(this); no.addActionListener(this); no.addKeyListener(this); cancel.addActionListener(this); cancel.addKeyListener(this); d1.addWindowListener(this); d1.add(backp); d1.setFont(font); d1.pack(); d1.setResizable(false); //メッセージ画面 d2.setLayout(new FlowLayout()); backp = new Panel(); backp.setLayout(new GridLayout(2, 1)); buttonp = new Panel(); Button b = new Button("確認"); b.addActionListener(this); b.addKeyListener(this); buttonp.add(b); backp.add(MessageBox_TEXT); backp.add(buttonp); d2.add(backp); d2.addWindowListener(this); d2.setFont(font); d2.pack(); //最初の画面 item0.addElement(newButton("次へ", 530, 340)); //ステップ1 item1.addElement(newLabel("(1)User Agent:", 0, 110)); item1.addElement(text_useragent = newTextField(150, 110, 400, 28)); text_useragent.setText(default_useragent); item1.addElement(newLabel("(2)タイムアウト(秒):", 0, 190)); item1.addElement(text_iotimeout = newTextField(220, 190, 80, 28)); text_iotimeout.setText(Integer.toString(default_iotimeout)); item1.addElement(newLabel("(3)", 0, 270)); item1.addElement(check_proxy = newCheckbox("HTTPプロキシサーバを使用する。", 60, 270)); item1.addElement(newLabel("アドレス:", 60, 300)); item1.addElement(text_proxyserver = newTextField(180, 300, 400, 28)); item1.addElement(newLabel("ポート番号:", 60, 330)); item1.addElement(text_proxyport = newTextField(180, 330, 100, 28)); item1.addElement(newButton("前へ", 480, 340)); item1.addElement(newButton("次へ", 530, 340)); //ステップ2 item2.addElement(newLabel("(1)タイトル:", 0, 70)); item2.addElement(text_newtitle = newTextField(220, 70, 300, 28)); item2.addElement(newButton("登録", 540, 72)); item2.addElement(newLabel("   登録済みのタイトル:", 0, 110)); item2.addElement(list_title = newList(220, 110, 300, 200, null)); item2.addElement(newButton("変更", 540, 160)); item2.addElement(newButton("削除", 540, 190)); item2.addElement(newButton("前へ", 480, 340)); //ステップ3 item3.addElement(newLabel("(1)URL:", 0, 110)); item3.addElement(text_targeturl = newTextField(120, 108, 400, 28)); item3.addElement(newButton("取得", 540, 110)); item3.addElement(text_responseheader = newTextArea(3, 0, 0, 150, 640, 40)); item3.addElement(list_responsebody = newList(0, 190, 640, 130, null)); item3.addElement(newLabel("(2)文字コード:", 0, 340)); item3.addElement(choice_encoding = newChoiceLang(150, 340)); item3.addElement(newButton("次へ", 530, 340)); item3.addElement(newButton("中止", 580, 340)); //ステップ4 item4.addElement(newLabel("(1)更新をチェックする間隔(秒):", 0, 120)); item4.addElement(text_checkinterval = newTextField(290, 120, 80, 28)); item4.addElement(newLabel("(2)更新時に再生するサウンド:", 0, 210)); item4.addElement(text_soundfile = newTextField(260, 210, 280, 28)); item4.addElement(newButton("テスト", 560, 210)); item4.addElement(newLabel("(3)ブラウザで表示する設定:", 0, 300)); item4.addElement(text_openpage = newTextField(250, 300, 370, 28)); item4.addElement(newButton("前へ", 480, 340)); item4.addElement(newButton("次へ", 530, 340)); item4.addElement(newButton("中止", 580, 340)); //ステップ5 item5.addElement(newLabel("(1)開始位置:", 0, 90)); item5.addElement(text_ignoreuntil = newTextField(140, 90, 480, 28)); text_ignoreuntil.addTextListener(this); item5.addElement(newLabel("   終了位置:", 0, 120)); item5.addElement(text_ignoreafter = newTextField(140, 120, 480, 28)); text_ignoreafter.addTextListener(this); item5.addElement(newLabel("(2)無視する文字:", 0, 150)); item5.addElement(text_ignorelist = newTextField(170, 150, 450, 28)); text_ignorelist.addTextListener(this); item5.addElement(newLabel("更新チェックの対象となる範囲:", 0, 180)); item5.addElement(list_comparebody = newList(0, 210, 640, 120, null)); item5.addElement(newButton("前へ", 480, 340)); item5.addElement(newButton("次へ", 530, 340)); item5.addElement(newButton("中止", 580, 340)); //ステップ6 item6.addElement(text_localconfig = newTextArea(3, 0, 0, 50, 640, 120)); item6.addElement(newLabel("更新チェックの対象となる範囲:", 0, 180)); item6.addElement(list_comparebody); item6.addElement(newButton("前へ", 480, 340)); item6.addElement(newButton("完了", 530, 340)); item6.addElement(newButton("中止", 580, 340)); showWindow(); loadSetting(); } private void showWindow() { Component c[] = f.getComponents(); String temp = null; int i = 0; for (i = 0; i < c.length; i++) c[i].setVisible(false); switch (setupstep) { case 0: temp = "Chat Monitor の環境設定をします。設定の手順は以下の通りです。\n" + " <ステップ1>\n" + "   UserAgent文字列の指定をする。\n" + "   応答待ちでのタイムアウトの指定をする。\n" + "   (必要であれば)HTTPプロキシサーバの指定をする。\n" + " <ステップ2>\n" + "   タイトルを入力して、「登録」をクリックする。\n" + " <ステップ3>\n" + "   チェックするページのURLを指定して、ページを取得する。\n" + "   (必要であれば)文字コードの選択をする。\n" + " <ステップ4>\n" + "   チェックする時間間隔(10〜3600秒)を指定する。\n" + "   更新時に再生するサウンドファイルを指定する。\n" + "   (必要であれば)ブラウザで表示する設定をする。\n" + " <ステップ5>\n" + "   (必要であれば)チェックする範囲の開始位置と終了位置を指定する。\n" + "   (必要であれば)特定の文字を無視する指定をする。\n" + " <ステップ6>\n" + "   確認画面で登録を完了し、ステップ2へ戻る。"; if (DefaultMS932) temp = UNICODEforMS932(temp); guide.setText(temp); guide.setSize(640, 320); showComponents(item0); break; case 1: guide.setText("<ステップ1>\n" + " (1)UserAgent文字列を変更したい場合は指定してください。\n" + " (2)応答待ちでのタイムアウトにするまでの秒数を指定してください。\n" + " (3)HTTPプロキシサーバを使う場合は、アドレスとポート番号を指定してください。\n"); guide.setSize(640, 80); showComponents(item1); break; case 2: guide.setText("<ステップ2>\n" + " (1)タイトルを入力して、「登録」をクリックしてください。\n" + "  または、登録済みのタイトルを選択して、「変更」をクリックしてください。"); guide.setSize(640, 60); list_title.removeAll(); for (i = 0; i < global_title.size(); i++) list_title.add((String) global_title.elementAt(i)); showComponents(item2); break; case 3: guide.setText("<ステップ3>\n" + " (1)更新をチェックしたいURLを入力して、「取得」を押してください。\n" + "  取得結果とページの内容が表示されます。\n" + " (2)文字が化けている場合、正しいと思われる文字コードを選択してください。\n" + "  正しい結果が得られたら、次へ進んでください。"); guide.setSize(640, 100); text_targeturl.setText(local_url); choice_encoding.select(local_encode); showComponents(item3); break; case 4: temp = "<ステップ4>\n" + " (1)更新をチェックする間隔を10秒〜3600秒の間で指定してください。\n" + " (2)更新時に再生するサウンドファイルを指定してください。\n" + " (3)Chat Monitor からブラウザを起動したい場合は、ブラウザへの完全なパス名と\n" + "  表示するURLを指定してください。"; if (DefaultMS932) temp = UNICODEforMS932(temp); guide.setText(temp); guide.setSize(640, 100); text_checkinterval.setText(local_interval); text_soundfile.setText(local_sound); text_openpage.setText(local_shell); showComponents(item4); break; case 5: guide.setText("<ステップ5>\n" + " (1)ページ全体ではなく、特定の範囲のみを更新チェックの対象にしたい場合は、\n" + "   開始位置と終了位置を文字列で指定してください。\n" + " (2)特定の文字を無視する場合は、その文字を指定してください。"); guide.setSize(640, 80); text_ignoreuntil.setText(local_ignoreuntil); text_ignoreafter.setText(local_ignoreafter); text_ignorelist.setText(local_ignorelist); showComponents(item5); textValueChanged(null); break; case 6: guide.setText("<ステップ6>\n" + " (1)内容を確認し、これで良ければ「完了」をクリックしてください。"); guide.setSize(640, 40); text_localconfig.setText("タイトル:" + local_title + crlf + "チェックするURL:" + local_url + crlf + "文字コードセット:" + local_encode + crlf + "チェックする間隔:" + local_interval + crlf + "更新時に再生するサウンド:" + local_sound + crlf + (local_shell.length() != 0 ? "ブラウザで表示する設定:" + local_shell + crlf : "") + (local_ignoreuntil.length() != 0 ? "開始位置:" + local_ignoreuntil + crlf : "") + (local_ignoreafter.length() != 0 ? "終了位置:" + local_ignoreafter + crlf : "") + (local_ignorelist.length() != 0 ? "無視する文字:" + local_ignorelist + crlf : "")); showComponents(item6); break; } f.setResizable(true); f.setSize(fsize); f.setResizable(false); } private void showComponents(Vector v) { guide.setVisible(true); int i = 0; for (i = 0; i < v.size(); i++) ((Component) v.elementAt(i)).setVisible(true); f.validate(); } private Checkbox newCheckbox(String label, int x, int y) { Checkbox cb = new Checkbox(label); cb.setVisible(false); f.add(cb); cb.setLocation(left + x, top + y); cb.setSize(cb.getPreferredSize()); return cb; } private List newList(int x, int y, int width, int height, String item[]) { List l = new List(); if (item != null) { int i = 0; for (i = 0; i < item.length; i++) l.add(item[i]); } l.setVisible(false); f.add(l); l.setBounds(left + x, top + y, width, height); return l; } private Choice newChoiceLang(int x, int y) { Choice c = new Choice(); int i = 0; byte dummydata[] = new byte[0]; for (i = 0; i < langID.length; i++) { try { new String(dummydata, langID[i]); c.add(langID[i]); } catch (UnsupportedEncodingException e) { if (langID[i].equals("EUC_JP_LINUX")) c.add("EUC_JP_LINUX"); } } c.setVisible(false); f.add(c); c.setLocation(left + x, top + y); c.setSize(c.getPreferredSize()); c.addItemListener(this); c.select(0); return c; } private TextArea newTextArea(int rows, int columns, int x, int y, int width, int height) { TextArea te = new TextArea("", rows, columns, TextArea.SCROLLBARS_NONE); te.setVisible(false); f.add(te); te.setBounds(left + x, top + y, width, height); te.setEditable(false); return te; } private TextField newTextField(int x, int y, int width, int height) { TextField tf = new TextField(); tf.setVisible(false); f.add(tf); tf.setBounds(left + x, top + y, width, height); return tf; } private Button newButton(String text, int x, int y) { Button b = new Button(text); b.setVisible(false); f.add(b); b.setLocation(left + x, top + y); b.setSize(b.getPreferredSize()); b.addActionListener(this); b.addKeyListener(this); return b; } private Label newLabel(String text, int x, int y) { Label l = new Label(text); l.setVisible(false); f.add(l); l.setLocation(left + x, top + y); l.setSize(l.getPreferredSize()); return l; } private void confirm_dialog() { Dimension dsize = d1.getSize(); Point fpos = f.getLocation(); d1.setLocation(fpos.x + (fsize.width - dsize.width) / 2, fpos.y + (fsize.height - dsize.height) / 2); d1.setVisible(true); } private void MessageBox(String text) { MessageBox_TEXT.setText(text); MessageBox_TEXT.setSize(MessageBox_TEXT.getPreferredSize()); d2.setResizable(true); d2.pack(); d2.setResizable(false); Dimension dsize = d2.getSize(); Point fpos = f.getLocation(); d2.setLocation(fpos.x + (fsize.width - dsize.width) / 2, fpos.y + (fsize.height - dsize.height) / 2); d2.setVisible(true); } private String EUC2UnicodeSJIS(byte byte_source[]) { if (byte_source == null) return null; if (byte_source.length == 0) return ""; int i = 0, j = byte_source.length, v = 0, v2 = 0; char source[] = new char[j]; ByteArrayOutputStream buf = new ByteArrayOutputStream(source.length); for (i = 0; i < j; i++) source[i] = (char) (byte_source[i] & 0xFF); i = 0; try { while (i < j) { if ((v = source[i]) <= 0x7E) { // ASCII buf.write(v); } else if (v == 0x8E) { // 1 byte KANA v = source[i + 1]; if (v < 0xA1 || v > 0xDF) v = '?'; buf.write(v); i++; } else if (v == 0x8F) { // JIS X 0212-1990 buf.write('?'); buf.write('?'); i += 2; } else if (v <= 0xA0 || (v2 = source[i + 1]) <= 0xA0) { // Out of range buf.write('?'); } else { v = (((v & 0x7F) - 0x21) * 94) + ((v2 & 0x7F) - 0x21); i++; v2 = (v / 188) + 0x81; if (v2 >= 0xA0) v2 += 0x40; buf.write(v2); v2 = (v % 188) + 0x40; if (v2 >= 0x7F) v2++; buf.write(v2); } i++; } } catch (ArrayIndexOutOfBoundsException e) { } try { return new String(buf.toByteArray(), "SJIS"); } catch (UnsupportedEncodingException e) { return ""; } } private String UNICODEforMS932(String s) { if (s == null) return null; return s.replace((char) 0xA5, (char) 0x5C).replace((char) 0x203E, (char) 0x7E).replace((char) 0x2014, (char) 0x2015).replace((char) 0x301C, (char) 0xFF5E).replace((char) 0x2016, (char) 0x2225).replace((char) 0x22EF, (char) 0x2026).replace((char) 0x2212, (char) 0xFF0D).replace((char) 0xA2, (char) 0xFFE0).replace((char) 0xA3, (char) 0xFFE1).replace((char) 0xAC, (char) 0xFFE2); } public static void main(String args[]) { System.out.println("Chat Monitor version 0.08a\nCopyright (C) 1999-2001 PANDA (a5497108@anet.ne.jp)"); if (args.length > 0) configfile = args[0]; if (args.length > 1) configfile_encoding = args[1]; new JavaCMSetup(); } private static final String langID[] = { "JISAutoDetect", "SJIS", "EUC_JP", "ISO-2022-JP", "EUC_JP_LINUX", "JIS0201", "JIS0208", "JIS0212", "UTF8", "UTF16", "UnicodeBig", "UnicodeBigUnmarked", "UnicodeLittle", "UnicodeLittleUnmarked", "ASCII", "Big5", "Big5_HKSCS", "Cp037", "Cp1006", "Cp1025", "Cp1026", "Cp1046", "Cp1097", "Cp1098", "Cp1112", "Cp1122", "Cp1123", "Cp1124", "Cp1140", "Cp1141", "Cp1142", "Cp1143", "Cp1144", "Cp1145", "Cp1146", "Cp1147", "Cp1148", "Cp1149", "Cp1250", "Cp1251", "Cp1252", "Cp1253", "Cp1254", "Cp1255", "Cp1256", "Cp1257", "Cp1258", "Cp1381", "Cp1383", "Cp273", "Cp277", "Cp278", "Cp280", "Cp284", "Cp285", "Cp297", "Cp33722", "Cp420", "Cp424", "Cp437", "Cp500", "Cp737", "Cp775", "Cp838", "Cp850", "Cp852", "Cp855", "Cp856", "Cp857", "Cp858", "Cp860", "Cp861", "Cp862", "Cp863", "Cp864", "Cp865", "Cp866", "Cp868", "Cp869", "Cp870", "Cp871", "Cp874", "Cp875", "Cp918", "Cp921", "Cp922", "Cp930", "Cp933", "Cp935", "Cp937", "Cp939", "Cp942", "Cp942C", "Cp943", "Cp943C", "Cp948", "Cp949", "Cp949C", "Cp950", "Cp964", "Cp970", "EUC_CN", "EUC_KR", "EUC_TW", "GBK", "ISO2022CN", "ISO2022KR", "8859_1", "8859_13", "ISO8859_15_FDIS", "8859_2", "8859_3", "8859_4", "8859_5", "8859_6", "8859_7", "8859_8", "8859_9", "Johab", "KOI8_R", "MacArabic", "MacCentralEurope", "MacCroatian", "MacCyrillic", "MacDingbat", "MacGreek", "MacHebrew", "MacIceland", "MacRoman", "MacRomania", "MacSymbol", "MacThai", "MacTurkish", "MacUkraine", "MS874", "MS936", "MS949", "MS950", "TIS620" }; private String GET(String url) { String contents = null; boolean success = false, iotimeout = false; try { URL url1 = new URL(url); String host = url1.getHost(); int port = url1.getPort(); if (port == -1) port = 80; Socket socket = null; if (!use_proxy) socket = new Socket(host, port); else socket = new Socket(proxy_server, proxy_port); socket.setSoTimeout(io_timeout); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); StringBuffer request = new StringBuffer(256); request.append("GET "); if (!use_proxy) request.append(url1.getFile()); else request.append(url1.toString()); request.append(" HTTP/1.0\r\n"); request.append("User-Agent: "); request.append(user_agent); request.append("\r\n"); request.append("Host: "); request.append(host); if (port != 80) { request.append(':'); request.append(port); } request.append("\r\n"); if (use_proxy) request.append("Pragma: No-Cache\r\nProxy-"); request.append("Connection: Close\r\n"); request.append("\r\n"); os.write(request.toString().getBytes()); byte buffer[] = new byte [1024]; ByteArrayOutputStream buf = new ByteArrayOutputStream(1024 * 10); int len = 0; try { while ((len = is.read(buffer)) != -1) buf.write(buffer, 0, len); } catch (InterruptedIOException e) { iotimeout = true; } socket.close(); contents = buf.toString("8859_1"); success = true; } catch (IOException e) { System.out.println(e); } if (!success || iotimeout) contents = null; return contents; } private void loadSetting() { boolean common = false; String caption = null, url = null, interval = null, encode = null, soundfile = null; String ignoreuntil = "", ignoreafter = "", ignorelist = "", openpage = ""; StringTokenizer st = null; File f = new File(configfile); if (!f.exists()) return; if (!f.canWrite()) { System.out.println(f.getAbsolutePath() + "は書き込みが禁止されています。"); System.exit(1); } System.out.println(f.getAbsolutePath() + "を読み込んでいます。"); try { FileInputStream fis = new FileInputStream(f); byte buf[] = new byte[fis.available()]; fis.read(buf); fis.close(); String data = null; if (configfile_encoding != null) data = new String(buf, configfile_encoding); else data = new String(buf); st = new StringTokenizer(data, "\r\n"); } catch (UnsupportedEncodingException e) { System.out.println("文字コードの指定が無効です。" + configfile_encoding); System.exit(1); } catch (IOException e) { System.out.println(f.getAbsolutePath() + "の読み込みに失敗しました。"); System.exit(1); } while (st.hasMoreTokens()) { int i = 0; String buffer = st.nextToken().trim(); if (buffer.toUpperCase().equals("[COMMON]")) common = true; else if (buffer.startsWith("[") && buffer.endsWith("]")) { common = false; if (caption != null && url != null && interval != null && encode != null && soundfile != null) { global_title.addElement(caption); global_url.addElement(url); global_interval.addElement(interval); global_encode.addElement(encode); global_ignoreuntil.addElement(ignoreuntil); global_ignoreafter.addElement(ignoreafter); global_ignorelist.addElement(ignorelist); global_sound.addElement(soundfile); global_shell.addElement(openpage); } caption = buffer.substring(1, buffer.length() - 1); url = interval = encode = soundfile = null; ignoreuntil = ignoreafter = ignorelist = openpage = ""; } else if ((i = buffer.indexOf('=')) != -1) { String value = buffer.substring(i + 1).trim(); if (value.length() != 0) { buffer = buffer.toUpperCase(); if (common) { if (buffer.startsWith("USERAGENT")) text_useragent.setText(value); else if (buffer.startsWith("USEPROXY")) { boolean state = false; try { state = (Integer.parseInt(value) != 0); } catch (Exception e) { } check_proxy.setState(state); } else if (buffer.startsWith("PROXYSERVER")) text_proxyserver.setText(value); else if (buffer.startsWith("PROXYPORT")) text_proxyport.setText(value); else if (buffer.startsWith("TIMEOUT")) text_iotimeout.setText(value); } else if (caption != null) { if (buffer.startsWith("URL")) url = value; else if (buffer.startsWith("INTERVAL")) interval = value; else if (buffer.startsWith("ENCODE")) encode = value; else if (buffer.startsWith("IGNOREUNTIL")) ignoreuntil = value; else if (buffer.startsWith("IGNOREAFTER")) ignoreafter = value; else if (buffer.startsWith("IGNORELIST")) ignorelist = value; else if (buffer.startsWith("OPEN")) openpage = value; else if (buffer.startsWith("SOUND")) soundfile = value; } } } } if (caption != null && url != null && interval != null && encode != null && soundfile != null) { global_title.addElement(caption); global_url.addElement(url); global_interval.addElement(interval); global_encode.addElement(encode); global_ignoreuntil.addElement(ignoreuntil); global_ignoreafter.addElement(ignoreafter); global_ignorelist.addElement(ignorelist); global_sound.addElement(soundfile); global_shell.addElement(openpage); } } private void saveSetting() { int i = 0; StringBuffer data = new StringBuffer(1024); String s = null; File f = new File(configfile); data.append("[COMMON]" + crlf); s = text_useragent.getText().trim(); if (s.length() == 0) s = default_useragent; data.append("UserAgent=" + user_agent + crlf); s = check_proxy.getState() ? "1" : "0"; data.append("UseProxy=" + s + crlf); data.append("ProxyServer=" + text_proxyserver.getText().trim() + crlf); data.append("ProxyPort=" + text_proxyport.getText().trim() + crlf); s = text_iotimeout.getText().trim(); if (s.length() == 0) s = Integer.toString(default_iotimeout); data.append("Timeout=" + s + crlf); data.append(crlf); for (i = 0; i < global_title.size(); i++) { data.append("[" + (String) global_title.elementAt(i) + "]" + crlf); data.append("URL=" + (String) global_url.elementAt(i) + crlf); data.append("Interval=" + (String) global_interval.elementAt(i) + crlf); data.append("Encode=" + (String) global_encode.elementAt(i) + crlf); s = (String) global_ignoreuntil.elementAt(i); if (s.length() != 0) data.append("IgnoreUntil=" + s + crlf); s = (String) global_ignoreafter.elementAt(i); if (s.length() != 0) data.append("IgnoreAfter=" + s + crlf); s = (String) global_ignorelist.elementAt(i); if (s.length() != 0) data.append("IgnoreList=" + s + crlf); data.append("Sound=" + (String) global_sound.elementAt(i) + crlf); s = (String) global_shell.elementAt(i); if (s.length() != 0) data.append("Open=" + s + crlf); data.append(crlf); } try { byte buf[] = null; if (configfile_encoding != null) buf = data.toString().getBytes(configfile_encoding); else buf = data.toString().getBytes(); FileOutputStream fos = new FileOutputStream(f); fos.write(buf); fos.close(); } catch (UnsupportedEncodingException e) { System.out.println("文字コードの指定が無効です。" + configfile_encoding); MessageBox("文字コードの指定が無効です。" + configfile_encoding); return; } catch (IOException e) { System.out.println(f.getAbsolutePath() + "の保存中にエラーが発生しました。"); MessageBox(f.getAbsolutePath() + "の保存中にエラーが発生しました。"); return; } System.out.println(f.getAbsolutePath() + "に保存しました。"); } }