1
0

CommandManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Text;
  29. using System.Linq;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using Mono.Addins.Setup;
  34. using Mono.Addins;
  35. using Mono.Addins.Description;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Server.Base
  38. {
  39. /// <summary>
  40. /// Command manager -
  41. /// Wrapper for OpenSim.Framework.PluginManager to allow
  42. /// us to add commands to the console to perform operations
  43. /// on our repos and plugins
  44. /// </summary>
  45. public class CommandManager
  46. {
  47. public AddinRegistry PluginRegistry;
  48. protected PluginManager PluginManager;
  49. public CommandManager(AddinRegistry registry)
  50. {
  51. PluginRegistry = registry;
  52. PluginManager = new PluginManager(PluginRegistry);
  53. AddManagementCommands();
  54. }
  55. private void AddManagementCommands()
  56. {
  57. // add plugin
  58. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  59. "plugin add", "plugin add \"plugin index\"",
  60. "Install plugin from repository.",
  61. HandleConsoleInstallPlugin);
  62. // remove plugin
  63. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  64. "plugin remove", "plugin remove \"plugin index\"",
  65. "Remove plugin from repository",
  66. HandleConsoleUnInstallPlugin);
  67. // list installed plugins
  68. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  69. "plugin list installed",
  70. "plugin list installed","List install plugins",
  71. HandleConsoleListInstalledPlugin);
  72. // list plugins available from registered repositories
  73. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  74. "plugin list available",
  75. "plugin list available","List available plugins",
  76. HandleConsoleListAvailablePlugin);
  77. // List available updates
  78. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  79. "plugin updates", "plugin updates","List availble updates",
  80. HandleConsoleListUpdates);
  81. // Update plugin
  82. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  83. "plugin update", "plugin update \"plugin index\"","Update the plugin",
  84. HandleConsoleUpdatePlugin);
  85. // Add repository
  86. MainConsole.Instance.Commands.AddCommand("Repository", true,
  87. "repo add", "repo add \"url\"","Add repository",
  88. HandleConsoleAddRepo);
  89. // Refresh repo
  90. MainConsole.Instance.Commands.AddCommand("Repository", true,
  91. "repo refresh", "repo refresh \"url\"", "Sync with a registered repository",
  92. HandleConsoleGetRepo);
  93. // Remove repository from registry
  94. MainConsole.Instance.Commands.AddCommand("Repository", true,
  95. "repo remove",
  96. "repo remove \"[url | index]\"",
  97. "Remove repository from registry",
  98. HandleConsoleRemoveRepo);
  99. // Enable repo
  100. MainConsole.Instance.Commands.AddCommand("Repository", true,
  101. "repo enable", "repo enable \"[url | index]\"",
  102. "Enable registered repository",
  103. HandleConsoleEnableRepo);
  104. // Disable repo
  105. MainConsole.Instance.Commands.AddCommand("Repository", true,
  106. "repo disable", "repo disable\"[url | index]\"",
  107. "Disable registered repository",
  108. HandleConsoleDisableRepo);
  109. // List registered repositories
  110. MainConsole.Instance.Commands.AddCommand("Repository", true,
  111. "repo list", "repo list",
  112. "List registered repositories",
  113. HandleConsoleListRepos);
  114. // *
  115. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  116. "plugin info", "plugin info \"plugin index\"","Show detailed information for plugin",
  117. HandleConsoleShowAddinInfo);
  118. // Plugin disable
  119. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  120. "plugin disable", "plugin disable \"plugin index\"",
  121. "Disable a plugin",
  122. HandleConsoleDisablePlugin);
  123. // Enable plugin
  124. MainConsole.Instance.Commands.AddCommand("Plugin", true,
  125. "plugin enable", "plugin enable \"plugin index\"",
  126. "Enable the selected plugin plugin",
  127. HandleConsoleEnablePlugin);
  128. }
  129. #region console handlers
  130. // Handle our console commands
  131. //
  132. // Install plugin from registered repository
  133. /// <summary>
  134. /// Handles the console install plugin command. Attempts to install the selected plugin
  135. /// and
  136. /// </summary>
  137. /// <param name='module'>
  138. /// Module.
  139. /// </param>
  140. /// <param name='cmd'>
  141. /// Cmd.
  142. /// </param>
  143. private void HandleConsoleInstallPlugin(string module, string[] cmd)
  144. {
  145. Dictionary<string, object> result = new Dictionary<string, object>();
  146. if (cmd.Length == 3)
  147. {
  148. int ndx = Convert.ToInt16(cmd[2]);
  149. if (PluginManager.InstallPlugin(ndx, out result) == true)
  150. {
  151. ArrayList s = new ArrayList();
  152. s.AddRange(result.Keys);
  153. s.Sort();
  154. var list = result.Keys.ToList();
  155. list.Sort();
  156. foreach (var k in list)
  157. {
  158. Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
  159. bool enabled = (bool)plugin["enabled"];
  160. MainConsole.Instance.OutputFormat("{0}) {1} {2} rev. {3}",
  161. k,
  162. enabled == true ? "[ ]" : "[X]",
  163. plugin["name"], plugin["version"]);
  164. }
  165. }
  166. }
  167. return;
  168. }
  169. // Remove installed plugin
  170. private void HandleConsoleUnInstallPlugin(string module, string[] cmd)
  171. {
  172. if (cmd.Length == 3)
  173. {
  174. int ndx = Convert.ToInt16(cmd[2]);
  175. PluginManager.UnInstall(ndx);
  176. }
  177. return;
  178. }
  179. // List installed plugins
  180. private void HandleConsoleListInstalledPlugin(string module, string[] cmd)
  181. {
  182. Dictionary<string, object> result = new Dictionary<string, object>();
  183. PluginManager.ListInstalledAddins(out result);
  184. ArrayList s = new ArrayList();
  185. s.AddRange(result.Keys);
  186. s.Sort();
  187. var list = result.Keys.ToList();
  188. list.Sort();
  189. foreach (var k in list)
  190. {
  191. Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
  192. bool enabled = (bool)plugin["enabled"];
  193. MainConsole.Instance.OutputFormat("{0}) {1} {2} rev. {3}",
  194. k,
  195. enabled == true ? "[ ]" : "[X]",
  196. plugin["name"], plugin["version"]);
  197. }
  198. return;
  199. }
  200. // List available plugins on registered repositories
  201. private void HandleConsoleListAvailablePlugin(string module, string[] cmd)
  202. {
  203. Dictionary<string, object> result = new Dictionary<string, object>();
  204. PluginManager.ListAvailable(out result);
  205. var list = result.Keys.ToList();
  206. list.Sort();
  207. foreach (var k in list)
  208. {
  209. // name, version, repository
  210. Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
  211. MainConsole.Instance.OutputFormat("{0}) {1} rev. {2} {3}",
  212. k,
  213. plugin["name"],
  214. plugin["version"],
  215. plugin["repository"]);
  216. }
  217. return;
  218. }
  219. // List available updates **not ready
  220. private void HandleConsoleListUpdates(string module, string[] cmd)
  221. {
  222. PluginManager.ListUpdates();
  223. return;
  224. }
  225. // Update plugin **not ready
  226. private void HandleConsoleUpdatePlugin(string module, string[] cmd)
  227. {
  228. MainConsole.Instance.Output(PluginManager.Update());
  229. return;
  230. }
  231. // Register repository
  232. private void HandleConsoleAddRepo(string module, string[] cmd)
  233. {
  234. if ( cmd.Length == 3)
  235. {
  236. PluginManager.AddRepository(cmd[2]);
  237. }
  238. return;
  239. }
  240. // Get repository status **not working
  241. private void HandleConsoleGetRepo(string module, string[] cmd)
  242. {
  243. PluginManager.GetRepository();
  244. return;
  245. }
  246. // Remove registered repository
  247. private void HandleConsoleRemoveRepo(string module, string[] cmd)
  248. {
  249. if (cmd.Length == 3)
  250. PluginManager.RemoveRepository(cmd);
  251. return;
  252. }
  253. // Enable repository
  254. private void HandleConsoleEnableRepo(string module, string[] cmd)
  255. {
  256. PluginManager.EnableRepository(cmd);
  257. return;
  258. }
  259. // Disable repository
  260. private void HandleConsoleDisableRepo(string module, string[] cmd)
  261. {
  262. PluginManager.DisableRepository(cmd);
  263. return;
  264. }
  265. // List repositories
  266. private void HandleConsoleListRepos(string module, string[] cmd)
  267. {
  268. Dictionary<string, object> result = new Dictionary<string, object>();
  269. PluginManager.ListRepositories(out result);
  270. var list = result.Keys.ToList();
  271. list.Sort();
  272. foreach (var k in list)
  273. {
  274. Dictionary<string, object> repo = (Dictionary<string, object>)result[k];
  275. bool enabled = (bool)repo["enabled"];
  276. MainConsole.Instance.OutputFormat("{0}) {1} {2}",
  277. k,
  278. enabled == true ? "[ ]" : "[X]",
  279. repo["name"], repo["url"]);
  280. }
  281. return;
  282. }
  283. // Show description information
  284. private void HandleConsoleShowAddinInfo(string module, string[] cmd)
  285. {
  286. if (cmd.Length >= 3)
  287. {
  288. Dictionary<string, object> result = new Dictionary<string, object>();
  289. int ndx = Convert.ToInt16(cmd[2]);
  290. PluginManager.AddinInfo(ndx, out result);
  291. MainConsole.Instance.OutputFormat("Name: {0}\nURL: {1}\nFile: {2}\nAuthor: {3}\nCategory: {4}\nDesc: {5}",
  292. result["name"],
  293. result["url"],
  294. result["file_name"],
  295. result["author"],
  296. result["category"],
  297. result["description"]);
  298. return;
  299. }
  300. }
  301. // Disable plugin
  302. private void HandleConsoleDisablePlugin(string module, string[] cmd)
  303. {
  304. PluginManager.DisablePlugin(cmd);
  305. return;
  306. }
  307. // Enable plugin
  308. private void HandleConsoleEnablePlugin(string module, string[] cmd)
  309. {
  310. PluginManager.EnablePlugin(cmd);
  311. return;
  312. }
  313. #endregion
  314. }
  315. }