PuttyHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // PuTTY Handler
  2. //
  3. // Douglas Thrift
  4. //
  5. // PuttyHandler.cs
  6. /* Copyright 2013 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Diagnostics;
  23. using System.IO;
  24. using System.Text;
  25. using System.Text.RegularExpressions;
  26. using Microsoft.Win32;
  27. public class PuttyHandler : AbstractHandler, Handler
  28. {
  29. private Regex regex = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
  30. private string path = null;
  31. public IList<string> Usages
  32. {
  33. get
  34. {
  35. return new string[] { "/putty[:<putty-path>] -- Use PuTTY to connect" };
  36. }
  37. }
  38. public MatchOption DoMatch(string arg)
  39. {
  40. Match match;
  41. if ((match = regex.Match(arg)).Success)
  42. {
  43. Group group = match.Groups["putty_path"];
  44. if (group.Success)
  45. path = group.Value;
  46. return MatchOption.Set;
  47. }
  48. else
  49. return MatchOption.None;
  50. }
  51. public bool Find()
  52. {
  53. if (path != null)
  54. goto Found;
  55. foreach (var hive in new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine })
  56. {
  57. var views = new List<RegistryView>(new RegistryView[] { RegistryView.Registry32 });
  58. if (Environment.Is64BitOperatingSystem)
  59. views.Insert(0, RegistryView.Registry64);
  60. foreach (RegistryView view in views)
  61. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, view), key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1"))
  62. if (key != null)
  63. {
  64. string location = (string)key.GetValue("InstallLocation");
  65. if (location == null)
  66. continue;
  67. path = Path.Combine(location, "putty.exe");
  68. if (File.Exists(path))
  69. {
  70. Debug.WriteLine("Found PuTTY in registry: {0}", path, null);
  71. goto Found;
  72. }
  73. else
  74. path = null;
  75. }
  76. }
  77. if ((path = FindInPath("putty.exe")) != null)
  78. {
  79. Debug.WriteLine("Found PuTTY in path: {0}", path, null);
  80. goto Found;
  81. }
  82. return false;
  83. Found:
  84. path = path.Trim();
  85. return true;
  86. }
  87. public void Execute(Uri uri, string user, string password)
  88. {
  89. if (!Find())
  90. throw new Exception("Could not find PuTTY executable.");
  91. StringBuilder args = new StringBuilder();
  92. if (password != null)
  93. args.AppendFormat("-pw {0} ", password);
  94. if (uri.Port != -1)
  95. args.AppendFormat("-P {0} ", uri.Port);
  96. if (user != null)
  97. args.AppendFormat("{0}@", user);
  98. args.Append(uri.Host);
  99. Debug.WriteLine("Running PuTTY command: {0} {1}", path, args);
  100. Process.Start(path, args.ToString());
  101. }
  102. }