aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Server/Base/CommandManager.cs
blob: 88aac00fb5a39949801fa9cb26089b7cd7598004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Mono.Addins;
// using Mono.Addins.Setup;
using Mono.Addins.Description;
using OpenSim.Framework;

namespace OpenSim.Server.Base
{
    /// <summary>
    /// Command manager -
    /// Wrapper for OpenSim.Framework.PluginManager to allow 
    /// us to add commands to the console to perform operations
    /// on our repos and plugins
    /// </summary>
	public class CommandManager
	{
		public AddinRegistry PluginRegistry;
		protected PluginManager PluginManager;

		public CommandManager(AddinRegistry registry)
        {
			PluginRegistry = registry;
			PluginManager = new PluginManager(PluginRegistry);
			AddManagementCommands();
		}

		private void AddManagementCommands()
		{
			// add plugin
			MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin add", "plugin add \"plugin index\"",
			                                         "Install plugin from repository.",
                                                     HandleConsoleInstallPlugin);

			// remove plugin
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin remove", "plugin remove \"plugin index\"",
			                                         "Remove plugin from repository",
                                                     HandleConsoleUnInstallPlugin);

			// list installed plugins
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin list installed", 
			                                         "plugin list installed","List install plugins",
                                                     HandleConsoleListInstalledPlugin);

			// list plugins available from registered repositories
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin list available",
			                                         "plugin list available","List available plugins",
                                                     HandleConsoleListAvailablePlugin);
			// List available updates
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin updates", "plugin updates","List availble updates",
                                                     HandleConsoleListUpdates);

			// Update plugin
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin update", "plugin update \"plugin index\"","Update the plugin",
                                                     HandleConsoleUpdatePlugin);

			// Add repository
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo add", "repo add \"url\"","Add repository",
                                                     HandleConsoleAddRepo);

			// Refresh repo
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo refresh", "repo refresh \"url\"", "Sync with a registered repository",
                                                     HandleConsoleGetRepo);

			// Remove repository from registry
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo remove",
			                                         "repo remove \"[url | index]\"",
			                                         "Remove repository from registry",
                                                     HandleConsoleRemoveRepo);

			// Enable repo
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo enable", "repo enable \"[url | index]\"",
			                                         "Enable registered repository",
                                                     HandleConsoleEnableRepo);

			// Disable repo
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo disable", "repo disable\"[url | index]\"",
			                                         "Disable registered repository",
                                                     HandleConsoleDisableRepo);

			// List registered repositories
            MainConsole.Instance.Commands.AddCommand("Repository", true,
                                                     "repo list", "repo list",
			                                         "List registered repositories",
                                                     HandleConsoleListRepos);

			// *
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin info", "plugin info \"plugin index\"","Show detailed information for plugin",
                                                     HandleConsoleShowAddinInfo);

			// Plugin disable
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin disable", "plugin disable \"plugin index\"",
			                                         "Disable a plugin",
                                                     HandleConsoleDisablePlugin);

			// Enable plugin
            MainConsole.Instance.Commands.AddCommand("Plugin", true,
                                                     "plugin enable", "plugin enable \"plugin index\"",
			                                         "Enable the selected plugin plugin",
                                                     HandleConsoleEnablePlugin);
        }
		
        #region console handlers
        // Handle our console commands
        //
        // Install plugin from registered repository
        /// <summary>
        /// Handles the console install plugin command. Attempts to install the selected plugin
        /// and 
        /// </summary>
        /// <param name='module'>
        /// Module.
        /// </param>
        /// <param name='cmd'>
        /// Cmd.
        /// </param>
        private void HandleConsoleInstallPlugin(string module, string[] cmd)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            if (cmd.Length == 3)
            {
                int ndx = Convert.ToInt16(cmd[2]);
                if (PluginManager.InstallPlugin(ndx, out result) == true)
                {
                    ArrayList s = new ArrayList();
                    s.AddRange(result.Keys);
                    s.Sort();

                    var list = result.Keys.ToList();
                    list.Sort();
                    foreach (var k in list)
                    {
                        Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
                        bool enabled = (bool)plugin["enabled"];
                        MainConsole.Instance.OutputFormat("{0}) {1} {2} rev. {3}",
                                                  k,
                                                  enabled == true ? "[ ]" : "[X]",
                                                  plugin["name"], plugin["version"]);
                    }
                }
            }
            return;
        }

        // Remove installed plugin
        private void HandleConsoleUnInstallPlugin(string module, string[] cmd)
        {
            if (cmd.Length == 3)
            {
                int ndx = Convert.ToInt16(cmd[2]);
                PluginManager.UnInstall(ndx);
            }
            return;
        }

        // List installed plugins
        private void HandleConsoleListInstalledPlugin(string module, string[] cmd)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            PluginManager.ListInstalledAddins(out result);

            ArrayList s = new ArrayList();
            s.AddRange(result.Keys);
            s.Sort();

            var list = result.Keys.ToList();
            list.Sort();
            foreach (var k in list)
            {
                Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
                bool enabled = (bool)plugin["enabled"];
                MainConsole.Instance.OutputFormat("{0}) {1} {2} rev. {3}",
                                                  k,
                                                  enabled == true ? "[ ]" : "[X]",
                                                  plugin["name"], plugin["version"]);
            }
            return;
        }

        // List available plugins on registered repositories
        private void HandleConsoleListAvailablePlugin(string module, string[] cmd)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            PluginManager.ListAvailable(out result);

            var list = result.Keys.ToList();
            list.Sort();
            foreach (var k in list)
            {
                // name, version, repository
                Dictionary<string, object> plugin = (Dictionary<string, object>)result[k];
                MainConsole.Instance.OutputFormat("{0}) {1} rev. {2} {3}",
                                                  k,
                                                  plugin["name"],
                                                  plugin["version"],
                                                  plugin["repository"]);
            }
            return;
        }

        // List available updates **not ready
        private void HandleConsoleListUpdates(string module, string[] cmd)
        {
            PluginManager.ListUpdates();
            return;
        }

        // Update plugin **not ready
        private void HandleConsoleUpdatePlugin(string module, string[] cmd)
        {
            MainConsole.Instance.Output(PluginManager.Update());
            return;
        }

        // Register repository
        private void HandleConsoleAddRepo(string module, string[] cmd)
        {
            if ( cmd.Length == 3)
            {
                PluginManager.AddRepository(cmd[2]);
            }
            return;
        }

        // Get repository status **not working
        private void HandleConsoleGetRepo(string module, string[] cmd)
        {
            PluginManager.GetRepository();
            return;
        }

        // Remove registered repository
        private void HandleConsoleRemoveRepo(string module, string[] cmd)
        {
            if (cmd.Length == 3)
                PluginManager.RemoveRepository(cmd);
            return;
        }

        // Enable repository
        private void HandleConsoleEnableRepo(string module, string[] cmd)
        {
            PluginManager.EnableRepository(cmd);
            return;
        }

        // Disable repository
        private void HandleConsoleDisableRepo(string module, string[] cmd)
        {
            PluginManager.DisableRepository(cmd);
            return;
        }

        // List repositories
        private void HandleConsoleListRepos(string module, string[] cmd)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            PluginManager.ListRepositories(out result);

            var list = result.Keys.ToList();
            list.Sort();
            foreach (var k in list)
            {
                Dictionary<string, object> repo = (Dictionary<string, object>)result[k];
                bool enabled = (bool)repo["enabled"];
                MainConsole.Instance.OutputFormat("{0}) {1} {2}",
                                                  k,
                                                  enabled == true ? "[ ]" : "[X]",
                                                  repo["name"], repo["url"]);
            }

            return;
        }

        // Show description information
        private void HandleConsoleShowAddinInfo(string module, string[] cmd)
        {
            if (cmd.Length >= 3)
            {
                
                Dictionary<string, object> result = new Dictionary<string, object>();

                int ndx = Convert.ToInt16(cmd[2]);
                PluginManager.AddinInfo(ndx, out result);

                MainConsole.Instance.OutputFormat("Name: {0}\nURL: {1}\nFile: {2}\nAuthor: {3}\nCategory: {4}\nDesc: {5}",
                                                  result["name"],
                                                  result["url"],
                                                  result["file_name"],
                                                  result["author"],
                                                  result["category"],
                                                  result["description"]);

                return;
            }
        }

        // Disable plugin
        private void HandleConsoleDisablePlugin(string module, string[] cmd)
        {
            PluginManager.DisablePlugin(cmd);
            return;
        }

        // Enable plugin
        private void HandleConsoleEnablePlugin(string module, string[] cmd)
        {
            PluginManager.EnablePlugin(cmd);
            return;
        }
        #endregion
	}
}