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 client.Inventory.ItemReceived += Inventory_ItemReceived;
78 client.Inventory.InventoryObjectOffered +=Inventory_InventoryObjectOffered;
79 //instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
80 //client.Self.ChatFromSimulator += new EventHandler<ChatEventArgs>(Self_ChatFromSimulator);
83 void UnregisterClientEvents(GridClient client)
85 //if (client == null) return;
86 client.Inventory.ItemReceived -= Inventory_ItemReceived;
87 client.Inventory.InventoryObjectOffered -= Inventory_InventoryObjectOffered;
90 private UUID offeredObject;
91 private UUID offeredAgent;
93 void Inventory_InventoryObjectOffered(object sender, InventoryObjectOfferedEventArgs e)
95 offeredObject = e.ObjectID;
96 offeredAgent = e.Offer.FromAgentID;
99 void Inventory_ItemReceived(object sender, ItemReceivedEventArgs e)
101 if (offeredObject != e.Item.UUID) return;
102 if (offeredAgent == UUID.Zero)
104 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to insert. Agent UUID not found", ChatBufferTextStyle.Error);
108 Dictionary<string, string> param = new Dictionary<string, string>();
109 param.Add("invUUID", e.Item.UUID.ToString());
110 param.Add("userUUID", offeredAgent.ToString());
111 param.Add("name", e.Item.Name);
113 if(e.Item.SaleType != SaleType.Not)
114 param.Add("price", e.Item.SalePrice.ToString());
116 param.Add("price", "0");
117 param.Add("texture", "");
119 string str = this.RequestVendor("ADDPRODUCT", param);
120 int result = Int32.Parse(str);
123 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product " + e.Item.Name + " from Agent " + offeredAgent.ToString() + " accepted and inserted", ChatBufferTextStyle.StatusBlue);
125 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to insert " + e.Item.Name + " from Agent " + offeredAgent.ToString(), ChatBufferTextStyle.Error);
127 offeredAgent = UUID.Zero;
131 void EVOvendTab_Disposed(object sender, EventArgs e)
134 UnregisterClientEvents(client);
135 //instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
138 public void StartPlugin(RadegastInstance inst)
141 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + " version " + version + " loaded");
145 EVOButton = new ToolStripMenuItem(tabLabel, null, OnEVOButtonClicked);
146 instance.MainForm.PluginsMenu.DropDownItems.Add(EVOButton);
149 timer = new System.Threading.Timer(new TimerCallback(productCallback));
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)
224 private string m_searchString;
225 public string searchString
229 return m_searchString;
233 m_searchString = value;
234 if (!String.IsNullOrEmpty(value))
235 PerformRecursiveSearch(0, Inventory.RootFolder.UUID);
239 void PerformRecursiveSearch(int level, UUID folderID)
241 var me = Inventory.Items[folderID].Data;
242 var sorted = Inventory.GetContents(folderID);
244 sorted.Sort((InventoryBase b1, InventoryBase b2) =>
246 if (b1 is InventoryFolder && !(b2 is InventoryFolder))
250 else if (!(b1 is InventoryFolder) && b2 is InventoryFolder)
256 return string.Compare(b1.Name, b2.Name);
260 foreach (var item in sorted)
262 if (item is InventoryFolder)
264 PerformRecursiveSearch(level + 1, item.UUID);
268 var it = item as InventoryItem;
270 if (it.UUID.ToString().Contains(searchString))
278 public string ClassName { get; set; }
279 public string id { get; set; }
280 public string userUUID { get; set; }
281 public string objectUUID { get; set; }
282 public int price { get; set; }
283 public string created { get; set; }
284 public string delivered { get; set; }
287 private string RequestVendor(string action, Dictionary<string, string> param = null)
291 var webRequest = WebRequest.Create(this.vendURL);
293 string postData = "action=" + action;
294 if (param != null && param.Count > 0)
296 var kv = param.Select(p => "&" + p.Key + "=" + p.Value);
297 postData += String.Join("", kv.ToArray());
299 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
301 webRequest.Method = "POST";
302 webRequest.ContentType = "application/x-www-form-urlencoded";
303 webRequest.ContentLength = byteArray.Length;
305 // add post data to request
306 Stream postStream = webRequest.GetRequestStream();
307 postStream.Write(byteArray, 0, byteArray.Length);
311 using (var response = webRequest.GetResponse())
312 using (var content = response.GetResponseStream())
313 using (var reader = new System.IO.StreamReader(content))
315 return reader.ReadToEnd();
322 private List<DeliveryQueue> parseResponse(string content)
324 List<DeliveryQueue> queue = new List<DeliveryQueue>();
326 if (String.IsNullOrEmpty(content)) return queue;
330 System.Reflection.PropertyInfo[] propertyInfos = typeof(DeliveryQueue).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
332 string field_separator = "|";
334 var lines = content.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
335 foreach (string l in lines)
339 var deliveryQ = new DeliveryQueue();
340 foreach (System.Reflection.PropertyInfo pInfo in propertyInfos)
342 var nextPos = l.IndexOf(field_separator, lastPos);
345 object o = Convert.ChangeType(l.Substring(lastPos, nextPos - lastPos), pInfo.PropertyType);
346 pInfo.SetValue(deliveryQ, o, null);
348 lastPos = nextPos + 1;
351 queue.Add(deliveryQ);
356 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Failed to read DeliveryQ - " + ex.Message, ChatBufferTextStyle.Error);
361 private bool SendObject(DeliveryQueue p)
364 searchString = p.objectUUID;
365 if (searchRes.Count <= 0)
367 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product not found '" + searchString + "' for user '" + p.userUUID + "'", ChatBufferTextStyle.Error);
370 if (searchRes.Count > 1)
372 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": More then one product found for '" + searchString + "'", ChatBufferTextStyle.Error);
376 var inv = searchRes[0] as InventoryItem;
379 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but not an inventory item", ChatBufferTextStyle.Error);
384 Dictionary<string, string> param = new Dictionary<string, string>();
385 param.Add("id", p.id);
386 var str = this.RequestVendor("SETDELIVERED", param);
389 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but user " + p.userUUID + " might not have enough funds", ChatBufferTextStyle.Normal);
390 // a message to the user would be helpful later
393 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": SETDELIVERED: " + str, ChatBufferTextStyle.StatusBlue);
395 Manager.GiveItem(inv.UUID, inv.Name, inv.AssetType, OpenMetaverse.UUID.Parse(p.userUUID), false);
396 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": PRODUCT '" + searchRes[0].Name + "' SENT TO " + p.userUUID, ChatBufferTextStyle.StatusBlue);
401 private bool isSending = false;
402 private void productCallback(object obj)
404 Manager = client.Inventory;
405 Inventory = Manager.Store;
406 if (Inventory == null)
408 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting for Inventory...");
412 if (isSending == true)
414 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting...");
419 Inventory.RootFolder.OwnerID = client.Self.AgentID;
421 var strContent = this.RequestVendor("GETOUTSTANDING");
422 List<DeliveryQueue> queue = this.parseResponse(strContent);
423 instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": " + queue.Count + " Items in Queue List");
425 // check if something neddc to be done
428 foreach (DeliveryQueue p in queue)
435 private void numStartupDelay_ValueChanged(object sender, EventArgs e)
440 private void numDeliveryInterval_ValueChanged(object sender, EventArgs e)