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/index.php";
51 List<InventoryBase> searchRes = new List<InventoryBase>();
53 /*private GridClient Client { get { return instance.Client; } }*/
55 static string tabID = "evovend_tab";
56 static string tabLabel = "EVOvend";
58 private string pluginName = "EVOvend";
59 private string version = "1.0";
60 private ToolStripMenuItem EVOButton;
62 public EVOvendPlugin()
64 //this.InitializeComponent();
67 public EVOvendPlugin(RadegastInstance instance, bool unused) : base(instance)
69 //this.instance = instance;
71 Disposed += new EventHandler(EVOvendTab_Disposed);
72 RegisterClientEvents(client);
75 void RegisterClientEvents(GridClient client)
77 //instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
78 //client.Self.ChatFromSimulator += new EventHandler<ChatEventArgs>(Self_ChatFromSimulator);
81 void UnregisterClientEvents(GridClient client)
83 //if (client == null) return;
86 private UUID offeredObject;
87 private UUID offeredAgent;
89 void Inventory_InventoryObjectOffered(object sender, InventoryObjectOfferedEventArgs e)
91 offeredObject = e.ObjectID;
92 offeredAgent = e.Offer.FromAgentID;
95 void Inventory_ItemReceived(object sender, ItemReceivedEventArgs e)
97 if (offeredObject != e.Item.UUID) return;
98 if (offeredAgent == UUID.Zero)
100 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to insert. Agent UUID not found", ChatBufferTextStyle.Error);
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", "");
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);
121 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to insert " + e.Item.Name + " from Agent " + offeredAgent.ToString(), ChatBufferTextStyle.Error);
123 offeredAgent = UUID.Zero;
127 void EVOvendTab_Disposed(object sender, EventArgs e)
130 UnregisterClientEvents(client);
131 //instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
134 public void StartPlugin(RadegastInstance inst)
137 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + " version " + version + " loaded");
141 EVOButton = new ToolStripMenuItem(tabLabel, null, OnEVOButtonClicked);
142 instance.MainForm.PluginsMenu.DropDownItems.Add(EVOButton);
145 timer = new System.Threading.Timer(new TimerCallback(productCallback));
147 // invenotry receiving
148 client.Inventory.ItemReceived += Inventory_ItemReceived;
149 client.Inventory.InventoryObjectOffered +=Inventory_InventoryObjectOffered;
152 public void SetupTimer(){
153 if (timer == null) return;
154 timer.Change((STARTUP_DELAY * 1000), (DELIVERY_INTERVAL * 1000));
155 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting " + STARTUP_DELAY + " seconds before start...");
160 this.InitializeComponent();
161 this.RegisterClientEvents(client);
162 if (instance != null)
168 private void readConfig()
170 config = instance.GlobalSettings["plugin." + pluginName] as OSDMap;
174 config = new OSDMap();
175 config["startup_delay"] = new OSDInteger(60);
176 config["delivery_interval"] = new OSDInteger(60);
177 instance.GlobalSettings["plugin." + pluginName] = config;
180 if (!config.ContainsKey("startup_delay"))
181 config["startup_delay"] = 60;
182 if (!config.ContainsKey("delivery_interval"))
183 config["delivery_interval"] = 60;
185 STARTUP_DELAY = config["startup_delay"].AsInteger();
186 DELIVERY_INTERVAL = config["delivery_interval"].AsInteger();
189 private void writeConfig()
191 config = instance.GlobalSettings["plugin." + pluginName] as OSDMap;
195 config["startup_delay"] = STARTUP_DELAY;
196 config["delivery_interval"] = DELIVERY_INTERVAL;
197 //instance.GlobalSettings["plugin." + pluginName] = config;
200 instance.GlobalSettings.Save();
203 void OnEVOButtonClicked(object sender, EventArgs e)
205 if (instance.TabConsole.TabExists(tabID))
207 instance.TabConsole.Tabs[tabID].Select();
211 instance.TabConsole.AddTab(tabID, tabLabel, new EVOvendPlugin(instance, true));
212 instance.TabConsole.Tabs[tabID].Select();
216 public void StopPlugin(RadegastInstance instance)
221 // unregister inventory receiver events
222 client.Inventory.ItemReceived -= Inventory_ItemReceived;
223 client.Inventory.InventoryObjectOffered -= Inventory_InventoryObjectOffered;
226 private string m_searchString;
227 public string searchString
231 return m_searchString;
235 m_searchString = value;
236 if (!String.IsNullOrEmpty(value))
237 PerformRecursiveSearch(0, Inventory.RootFolder.UUID);
241 void PerformRecursiveSearch(int level, UUID folderID)
243 var me = Inventory.Items[folderID].Data;
244 var sorted = Inventory.GetContents(folderID);
246 sorted.Sort((InventoryBase b1, InventoryBase b2) =>
248 if (b1 is InventoryFolder && !(b2 is InventoryFolder))
252 else if (!(b1 is InventoryFolder) && b2 is InventoryFolder)
258 return string.Compare(b1.Name, b2.Name);
262 foreach (var item in sorted)
264 if (item is InventoryFolder)
266 PerformRecursiveSearch(level + 1, item.UUID);
270 var it = item as InventoryItem;
272 if (it.UUID.ToString().Contains(searchString))
280 public string ClassName { get; set; }
281 public string id { get; set; }
282 public string userUUID { get; set; }
283 public string objectUUID { get; set; }
284 public int price { get; set; }
285 public string created { get; set; }
286 public string delivered { get; set; }
289 private string RequestVendor(string action, Dictionary<string, string> param = null)
293 var webRequest = WebRequest.Create(this.vendURL);
295 string postData = "action=" + action;
296 if (param != null && param.Count > 0)
298 var kv = param.Select(p => "&" + p.Key + "=" + p.Value);
299 postData += String.Join("", kv.ToArray());
301 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
303 webRequest.Method = "POST";
304 webRequest.ContentType = "application/x-www-form-urlencoded";
305 webRequest.ContentLength = byteArray.Length;
307 // add post data to request
308 Stream postStream = webRequest.GetRequestStream();
309 postStream.Write(byteArray, 0, byteArray.Length);
313 using (var response = webRequest.GetResponse())
314 using (var content = response.GetResponseStream())
315 using (var reader = new System.IO.StreamReader(content))
317 return reader.ReadToEnd();
324 private List<DeliveryQueue> parseResponse(string content)
326 List<DeliveryQueue> queue = new List<DeliveryQueue>();
328 if (String.IsNullOrEmpty(content)) return queue;
332 System.Reflection.PropertyInfo[] propertyInfos = typeof(DeliveryQueue).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
334 string field_separator = "|";
336 var lines = content.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
337 foreach (string l in lines)
341 var deliveryQ = new DeliveryQueue();
342 foreach (System.Reflection.PropertyInfo pInfo in propertyInfos)
344 var nextPos = l.IndexOf(field_separator, lastPos);
347 object o = Convert.ChangeType(l.Substring(lastPos, nextPos - lastPos), pInfo.PropertyType);
348 pInfo.SetValue(deliveryQ, o, null);
350 lastPos = nextPos + 1;
353 queue.Add(deliveryQ);
358 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to read DeliveryQ - " + ex.Message, ChatBufferTextStyle.Error);
363 private bool SendObject(DeliveryQueue p)
366 searchString = p.objectUUID;
367 if (searchRes.Count <= 0)
369 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product not found '" + searchString + "' for user '" + p.userUUID + "'", ChatBufferTextStyle.Error);
372 if (searchRes.Count > 1)
374 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": More then one product found for '" + searchString + "'", ChatBufferTextStyle.Error);
378 var inv = searchRes[0] as InventoryItem;
381 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but not an inventory item", ChatBufferTextStyle.Error);
386 Dictionary<string, string> param = new Dictionary<string, string>();
387 param.Add("id", p.id);
388 var str = this.RequestVendor("SETDELIVERED", param);
391 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but user " + p.userUUID + " might not have enough funds", ChatBufferTextStyle.Normal);
392 // a message to the user would be helpful later
395 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": SETDELIVERED: " + str, ChatBufferTextStyle.StatusBlue);
397 Manager.GiveItem(inv.UUID, inv.Name, inv.AssetType, OpenMetaverse.UUID.Parse(p.userUUID), false);
398 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": PRODUCT '" + searchRes[0].Name + "' SENT TO " + p.userUUID, ChatBufferTextStyle.StatusBlue);
403 private bool isSending = false;
404 private void productCallback(object obj)
406 Manager = client.Inventory;
407 Inventory = Manager.Store;
408 if (Inventory == null)
410 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting for Inventory...");
414 if (isSending == true)
416 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting...");
421 Inventory.RootFolder.OwnerID = client.Self.AgentID;
423 var strContent = this.RequestVendor("GETOUTSTANDING");
424 List<DeliveryQueue> queue = this.parseResponse(strContent);
425 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": " + queue.Count + " Items in Queue List");
427 // check if something neddc to be done
430 foreach (DeliveryQueue p in queue)
437 private void numStartupDelay_ValueChanged(object sender, EventArgs e)
442 private void numDeliveryInterval_ValueChanged(object sender, EventArgs e)