// このファイルはC#のソースリストです。ライセンスフリーで改造推奨でヨロ! /* SKK「ぐぐれ」 名前通りの辞書検索拡張アプリです。 実行すると常駐し、再実行すると常駐が解除されます。 */ using System; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; // 1ファイルで実現するため、サテライトアセンブリは未使用 [assembly: AssemblyTitle("SKK「ぐぐれ」")] // 説明 [assembly: AssemblyCompany("SKK普及委員会")] // 組織 [assembly: AssemblyCopyright("\u00A92013 名無し")] // 作者 [assembly: AssemblyVersion("0")] // 版数 [assembly: AssemblyFileVersion("0")] // ファイル版数 // [assembly: AssemblyProduct("製品名")] // [assembly: AssemblyDescription("コメント")] // [assembly: AssemblyTrademark("商標")] /// SKKゲート操作用 class SKKGate { object g; /// 生成時 public SKKGate() { g = Activator.CreateInstance(Type.GetTypeFromProgID("SKKGate")); } /// 呼出 object call(string name, object[] arg) { return g.GetType().InvokeMember(name, BindingFlags.InvokeMethod, null, g, arg); } /// 待機 public int Sync(int host, int target) { return (int)call("Sync", new object[] { host, target }); } /// 情報取得 public string Get(int mode) { return (string)call("Get", new object[] { mode }); } /// 数値取得 public int GetInt(int mode) { return (int)call("Get", new object[] { mode }); } /// 候補追加 public void Put(string candidate, string capsule) { call("Put", new object[] { candidate, capsule }); } } /// ぐぐれ public class SKKggr { /// 通信 static ArrayList ggr(string text) { ArrayList result = new ArrayList(); byte[] buf; try { WebClient w = new WebClient(); w.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"); buf = w.DownloadData("http://www.google.com/transliterate?langpair=ja-Hira|ja&text=" + text + ","); } catch (WebException) { return result; } string work = Encoding.UTF8.GetString(buf); // フォーマット変更攻撃に無力なので注意 work = Regex.Replace(work, "^\\[\\[.*,\\[\"", ""); work = Regex.Replace(work, "\"\\]\\]\\]$", ""); work = Regex.Replace(work, "\",\"", "\r"); foreach (string one in work.Split('\r')) { if (!Regex.IsMatch(one, "^([ぁ-\u309C]|・|ー)+$") && !Regex.IsMatch(one, "^[\uFF61-\uFF9F]+$")) result.Add(one); } return result; } /// 検索 static ArrayList Search(string index) { int n = index.IndexOf('\t'); // 送りなし if (n < 0) return ggr(index); // 送りあり ArrayList result = new ArrayList(); if (n > 0) { string yomi = index.Substring(0, n - 1); string okuri = index.Substring(n + 1); ArrayList work = ggr(yomi + okuri); foreach (string one in work) if (one.EndsWith(okuri)) result.Add(one.Substring(0, one.Length - okuri.Length)); } return result; } /// メイン static void Main(string[] args) { SKKGate gate = new SKKGate(); const int target = 2; if (args.Length > 0) ggr(args[0]); // 引数指定で起動時に仮接続 (初回の速度低下を回避) for (;;) { // 待機 int n = gate.Sync(target, -1); if (n < 0) { if (n == -2) { MessageBox.Show("停止します", "SKKggr"); gate.Sync(-1, target); } break; } if (n != 0) continue; // 変換 string index = gate.Get(0); int attribute = gate.GetInt(2); string annotation = attribute == 8 ? "" : "\f外部辞書"; ArrayList result = Search(index); foreach (string one in result) gate.Put(one + annotation, ""); } } }