2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
10 using OpenMetaverse.StructuredData;
11 using System.Threading;
15 namespace Radegast.Plugin.EVOVend
17 [Radegast.Plugin(Name = "EVOvend Plugin", Description = "EVO Vendor Delivery System", Version = "1.0")]
18 public partial class EVOvendPlugin : RadegastTabControl, IRadegastPlugin
20 public int DELIVERY_INTERVAL
24 return (int)numDeliveryInterval.Value;
28 numDeliveryInterval.Value = value;
32 public int STARTUP_DELAY
36 return (int)numStartupDelay.Value;
40 numStartupDelay.Value = value;
46 public static System.Threading.Timer timer;
47 private InventoryManager Manager;
48 private OpenMetaverse.Inventory Inventory;
50 private string vendURL = @"http://evosl.org/TREK/SL/SLvendor.php";
51 private string loginURL = @"http://evosl.org/TREK/SL/vendor.php";
52 List<InventoryBase> searchRes = new List<InventoryBase>();
54 /*private GridClient Client { get { return instance.Client; } }*/
56 static string tabID = "evovend_tab";
57 static string tabLabel = "EVOvend";
59 private string pluginName = "EVOvend";
60 private string version = "1.0";
61 private ToolStripMenuItem EVOButton;
63 public EVOvendPlugin()
65 //this.InitializeComponent();
68 public EVOvendPlugin(RadegastInstance instance, bool unused) : base(instance)
70 //this.instance = instance;
72 Disposed += new EventHandler(EVOvendTab_Disposed);
73 RegisterClientEvents(client);
76 void RegisterClientEvents(GridClient client)
78 //instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
79 //client.Self.ChatFromSimulator += new EventHandler<ChatEventArgs>(Self_ChatFromSimulator);
82 void UnregisterClientEvents(GridClient client)
84 //if (client == null) return;
87 private UUID offeredObject;
88 private UUID offeredAgent;
90 void Inventory_InventoryObjectOffered(object sender, InventoryObjectOfferedEventArgs e)
92 offeredObject = e.ObjectID;
93 offeredAgent = e.Offer.FromAgentID;
96 void Inventory_ItemReceived(object sender, ItemReceivedEventArgs e)
98 if (offeredObject != e.Item.UUID) return;
99 if (offeredAgent == UUID.Zero)
104 Dictionary<string, string> param = new Dictionary<string, string>();
105 param.Add("invUUID", e.Item.UUID.ToString());
106 param.Add("userUUID", offeredAgent.ToString());
107 param.Add("name", e.Item.Name);
109 if(e.Item.SaleType != SaleType.Not)
110 param.Add("price", e.Item.SalePrice.ToString());
112 param.Add("price", "0");
113 param.Add("texture", "");
114 param.Add("botUUID", client.Self.AgentID.ToString());
115 string str = this.RequestVendor("ADDPRODUCT", param);
116 int result = Int32.Parse(str);
119 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product " + e.Item.Name + " from Agent " + offeredAgent.ToString() + " accepted and inserted", ChatBufferTextStyle.StatusBlue);
120 client.Self.InstantMessage(offeredAgent, "Your Object has been transfered successfully. Please visit " + this.loginURL + " for further steps");
122 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to insert " + e.Item.Name + " from Agent " + offeredAgent.ToString(), ChatBufferTextStyle.Error);
124 offeredAgent = UUID.Zero;
128 void EVOvendTab_Disposed(object sender, EventArgs e)
131 UnregisterClientEvents(client);
132 //instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
135 public void StartPlugin(RadegastInstance inst)
138 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + " version " + version + " loaded");
142 EVOButton = new ToolStripMenuItem(tabLabel, null, OnEVOButtonClicked);
143 instance.MainForm.PluginsMenu.DropDownItems.Add(EVOButton);
146 timer = new System.Threading.Timer(new TimerCallback(productCallback));
148 // invenotry receiving
149 client.Inventory.ItemReceived += Inventory_ItemReceived;
150 client.Inventory.InventoryObjectOffered +=Inventory_InventoryObjectOffered;
153 public void SetupTimer(){
154 if (timer == null) return;
155 timer.Change((STARTUP_DELAY * 1000), (DELIVERY_INTERVAL * 1000));
156 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting " + STARTUP_DELAY + " seconds before start...");
161 this.InitializeComponent();
162 this.RegisterClientEvents(client);
163 if (instance != null)
169 private void readConfig()
171 config = instance.GlobalSettings["plugin." + pluginName] as OSDMap;
175 config = new OSDMap();
176 config["startup_delay"] = new OSDInteger(60);
177 config["delivery_interval"] = new OSDInteger(60);
178 instance.GlobalSettings["plugin." + pluginName] = config;
181 if (!config.ContainsKey("startup_delay"))
182 config["startup_delay"] = 60;
183 if (!config.ContainsKey("delivery_interval"))
184 config["delivery_interval"] = 60;
186 STARTUP_DELAY = config["startup_delay"].AsInteger();
187 DELIVERY_INTERVAL = config["delivery_interval"].AsInteger();
190 private void writeConfig()
192 config = instance.GlobalSettings["plugin." + pluginName] as OSDMap;
196 config["startup_delay"] = STARTUP_DELAY;
197 config["delivery_interval"] = DELIVERY_INTERVAL;
198 //instance.GlobalSettings["plugin." + pluginName] = config;
201 instance.GlobalSettings.Save();
204 void OnEVOButtonClicked(object sender, EventArgs e)
206 if (instance.TabConsole.TabExists(tabID))
208 instance.TabConsole.Tabs[tabID].Select();
212 instance.TabConsole.AddTab(tabID, tabLabel, new EVOvendPlugin(instance, true));
213 instance.TabConsole.Tabs[tabID].Select();
217 public void StopPlugin(RadegastInstance instance)
222 // unregister inventory receiver events
223 client.Inventory.ItemReceived -= Inventory_ItemReceived;
224 client.Inventory.InventoryObjectOffered -= Inventory_InventoryObjectOffered;
227 private string m_searchString;
228 public string searchString
232 return m_searchString;
236 m_searchString = value;
237 if (!String.IsNullOrEmpty(value))
238 PerformRecursiveSearch(0, Inventory.RootFolder.UUID);
242 void PerformRecursiveSearch(int level, UUID folderID)
244 var me = Inventory.Items[folderID].Data;
245 var sorted = Inventory.GetContents(folderID);
247 sorted.Sort((InventoryBase b1, InventoryBase b2) =>
249 if (b1 is InventoryFolder && !(b2 is InventoryFolder))
253 else if (!(b1 is InventoryFolder) && b2 is InventoryFolder)
259 return string.Compare(b1.Name, b2.Name);
263 foreach (var item in sorted)
265 if (item is InventoryFolder)
267 PerformRecursiveSearch(level + 1, item.UUID);
271 var it = item as InventoryItem;
273 if (it.UUID.ToString().Contains(searchString))
281 public string ClassName { get; set; }
282 public string id { get; set; }
283 public string userUUID { get; set; }
284 public string objectUUID { get; set; }
285 public int price { get; set; }
286 public int nextperm { get; set; }
287 public string created { get; set; }
288 public string delivered { get; set; }
292 private string RequestVendor(string action, Dictionary<string, string> param = null)
296 var webRequest = WebRequest.Create(this.vendURL);
298 string postData = "action=" + action;
299 if (param != null && param.Count > 0)
301 var kv = param.Select(p => "&" + p.Key + "=" + p.Value);
302 postData += String.Join("", kv.ToArray());
304 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
306 webRequest.Method = "POST";
307 webRequest.ContentType = "application/x-www-form-urlencoded";
308 webRequest.ContentLength = byteArray.Length;
310 // add post data to request
311 Stream postStream = webRequest.GetRequestStream();
312 postStream.Write(byteArray, 0, byteArray.Length);
316 using (var response = webRequest.GetResponse())
317 using (var content = response.GetResponseStream())
318 using (var reader = new System.IO.StreamReader(content))
320 return reader.ReadToEnd();
327 private List<DeliveryQueue> parseResponse(string content)
329 List<DeliveryQueue> queue = new List<DeliveryQueue>();
331 if (String.IsNullOrEmpty(content)) return queue;
335 System.Reflection.PropertyInfo[] propertyInfos = typeof(DeliveryQueue).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
337 string field_separator = "|";
339 var lines = content.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
340 foreach (string l in lines)
344 var deliveryQ = new DeliveryQueue();
345 foreach (System.Reflection.PropertyInfo pInfo in propertyInfos)
347 var nextPos = l.IndexOf(field_separator, lastPos);
350 object o = Convert.ChangeType(l.Substring(lastPos, nextPos - lastPos), pInfo.PropertyType);
351 pInfo.SetValue(deliveryQ, o, null);
353 lastPos = nextPos + 1;
356 queue.Add(deliveryQ);
361 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to read DeliveryQ - " + ex.Message, ChatBufferTextStyle.Error);
366 private bool SendObject(DeliveryQueue p)
369 searchString = p.objectUUID;
370 if (searchRes.Count <= 0)
372 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product not found '" + searchString + "' for user '" + p.userUUID + "'", ChatBufferTextStyle.Error);
375 if (searchRes.Count > 1)
377 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": More then one product found for '" + searchString + "'", ChatBufferTextStyle.Error);
381 var inv = searchRes[0] as InventoryItem;
384 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but not an inventory item", ChatBufferTextStyle.Error);
389 Dictionary<string, string> param = new Dictionary<string, string>();
390 param.Add("id", p.id);
391 var str = this.RequestVendor("SETDELIVERED", param);
394 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but user " + p.userUUID + " might not have enough funds", ChatBufferTextStyle.Normal);
395 // a message to the user would be helpful later
398 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": SETDELIVERED: " + str, ChatBufferTextStyle.StatusBlue);
400 inv.Permissions.NextOwnerMask = (PermissionMask)p.nextperm;
401 client.Inventory.RequestUpdateItem(inv);
402 client.Inventory.RequestFetchInventory(inv.UUID, inv.OwnerID);
404 Manager.GiveItem(inv.UUID, inv.Name, inv.AssetType, OpenMetaverse.UUID.Parse(p.userUUID), false);
405 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": PRODUCT '" + searchRes[0].Name + "' SENT TO " + p.userUUID, ChatBufferTextStyle.StatusBlue);
410 private bool isSending = false;
411 private void productCallback(object obj)
413 Manager = client.Inventory;
414 Inventory = Manager.Store;
415 if (Inventory == null)
417 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting for Inventory...");
421 if (isSending == true)
423 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting...");
428 Inventory.RootFolder.OwnerID = client.Self.AgentID;
430 Dictionary<string, string> param = new Dictionary<string, string>();
431 param.Add("botUUID", client.Self.AgentID.ToString());
432 var strContent = this.RequestVendor("GETOUTSTANDING", param);
433 List<DeliveryQueue> queue = this.parseResponse(strContent);
434 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": " + queue.Count + " Items in Queue List");
436 // check if something neddc to be done
439 foreach (DeliveryQueue p in queue)
446 private void numStartupDelay_ValueChanged(object sender, EventArgs e)
451 private void numDeliveryInterval_ValueChanged(object sender, EventArgs e)