CommandManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 available 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.Output("{0}) {1} {2} rev. {3}",
  161. null,
  162. k,
  163. enabled == true ? "[ ]" : "[X]",
  164. plugin["name"], plugin["version"]);
  165. }
  166. }
  167. }
  168. return;
  169. }
  170. // Remove installed plugin
  171. private void HandleConsoleUnInstallPlugin(string module, string[] cmd)
  172. {
  173. if (cmd.Length == 3)
  174. {
  175. int ndx = Convert.ToInt16(cmd[2]);
  176. PluginManager.UnInstall(ndx);
  177. }
  178. return;
  179. }
  180. // List installed plugins
  181. private void HandleConsoleListInstalledPlugin(string module, string[] cmd)
  182. {
  183. Dictionary<string, object> result = new Dictionary<string, object>();
  184. PluginManager.ListInstalledAddins(out result);
  185. ArrayList s = new ArrayList();
  186. s.AddRange(result.Keys);
  187. s.Sort();
  188. var list = result.Keys.ToList();
  189. list.Sort();
  190. foreach (var k in list)
  191. {
  192. Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
  193. bool enabled = (bool)plugin["enabled"];
  194. MainConsole.Instance.Output("{0}) {1} {2} rev. {3}",
  195. null,
  196. k,
  197. enabled == true ? "[ ]" : "[X]",
  198. plugin["name"], plugin["version"]);
  199. }
  200. return;
  201. }
  202. // List available plugins on registered repositories
  203. private void HandleConsoleListAvailablePlugin(string module, string[] cmd)
  204. {
  205. Dictionary<string, object> result = new Dictionary<string, object>();
  206. PluginManager.ListAvailable(out result);
  207. var list = result.Keys.ToList();
  208. list.Sort();
  209. foreach (var k in list)
  210. {
  211. // name, version, repository
  212. Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
  213. MainConsole.Instance.Output("{0}) {1} rev. {2} {3}",
  214. null,
  215. k,
  216. plugin["name"],
  217. plugin["version"],
  218. plugin["repository"]);
  219. }
  220. return;
  221. }
  222. // List available updates **not ready
  223. private void HandleConsoleListUpdates(string module, string[] cmd)
  224. {
  225. PluginManager.ListUpdates();
  226. return;
  227. }
  228. // Update plugin **not ready
  229. private void HandleConsoleUpdatePlugin(string module, string[] cmd)
  230. {
  231. MainConsole.Instance.Output(PluginManager.Update());
  232. return;
  233. }
  234. // Register repository
  235. private void HandleConsoleAddRepo(string module, string[] cmd)
  236. {
  237. if ( cmd.Length == 3)
  238. {
  239. PluginManager.AddRepository(cmd[2]);
  240. }
  241. return;
  242. }
  243. // Get repository status **not working
  244. private void HandleConsoleGetRepo(string module, string[] cmd)
  245. {
  246. PluginManager.GetRepository();
  247. return;
  248. }
  249. // Remove registered repository
  250. private void HandleConsoleRemoveRepo(string module, string[] cmd)
  251. {
  252. if (cmd.Length == 3)
  253. PluginManager.RemoveRepository(cmd);
  254. return;
  255. }
  256. // Enable repository
  257. private void HandleConsoleEnableRepo(string module, string[] cmd)
  258. {
  259. PluginManager.EnableRepository(cmd);
  260. return;
  261. }
  262. // Disable repository
  263. private void HandleConsoleDisableRepo(string module, string[] cmd)
  264. {
  265. PluginManager.DisableRepository(cmd);
  266. return;
  267. }
  268. // List repositories
  269. private void HandleConsoleListRepos(string module, string[] cmd)
  270. {
  271. Dictionary<string, object> result = new Dictionary<string, object>();
  272. PluginManager.ListRepositories(out result);
  273. var list = result.Keys.ToList();
  274. list.Sort();
  275. foreach (var k in list)
  276. {
  277. Dictionary<string, object> repo = (Dictionary<string, object>)result[k];
  278. bool enabled = (bool)repo["enabled"];
  279. MainConsole.Instance.Output("{0}) {1} {2}",
  280. null,
  281. k,
  282. enabled == true ? "[ ]" : "[X]",
  283. repo["name"], repo["url"]);
  284. }
  285. return;
  286. }
  287. // Show description information
  288. private void HandleConsoleShowAddinInfo(string module, string[] cmd)
  289. {
  290. if (cmd.Length >= 3)
  291. {
  292. Dictionary<string, object> result = new Dictionary<string, object>();
  293. int ndx = Convert.ToInt16(cmd[2]);
  294. PluginManager.AddinInfo(ndx, out result);
  295. MainConsole.Instance.Output("Name: {0}\nURL: {1}\nFile: {2}\nAuthor: {3}\nCategory: {4}\nDesc: {5}",
  296. null,
  297. result["name"],
  298. result["url"],
  299. result["file_name"],
  300. result["author"],
  301. result["category"],
  302. result["description"]);
  303. return;
  304. }
  305. }
  306. // Disable plugin
  307. private void HandleConsoleDisablePlugin(string module, string[] cmd)
  308. {
  309. PluginManager.DisablePlugin(cmd);
  310. return;
  311. }
  312. // Enable plugin
  313. private void HandleConsoleEnablePlugin(string module, string[] cmd)
  314. {
  315. PluginManager.EnablePlugin(cmd);
  316. return;
  317. }
  318. #endregion
  319. }
  320. }