2 using System.Collections.Generic;
4 using System.Threading;
5 using System.Windows.Forms;
9 using System.Text.RegularExpressions;
14 namespace Radegast.Plugin.Demo
16 [Radegast.Plugin(Name="EVOVend Plugin", Description="EVO Vendor Delivery System", Version="1.0")]
17 public class DemoPlugin : IRadegastPlugin
19 private System.Threading.Timer timer;
20 private InventoryManager Manager;
21 private OpenMetaverse.Inventory Inventory;
23 private string vendURL = @"http://evosl.org/TREK/SL/index.php";
24 List<InventoryBase> searchRes = new List<InventoryBase>();
26 private RadegastInstance Instance;
27 private GridClient Client { get { return Instance.Client; } }
29 private string pluginName = "EVOVend";
30 private string version = "1.0";
37 public void StartPlugin(RadegastInstance inst)
40 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + " version " + version + " loaded");
43 timer = new System.Threading.Timer(new TimerCallback(productCallback));
44 timer.Change((30 * 1000), (60 * 1000));
45 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting 30 seconds for Inventory...");
48 public void StopPlugin(RadegastInstance instance)
54 private string m_searchString;
55 public string searchString {
58 return m_searchString;
62 m_searchString = value;
63 if(!String.IsNullOrEmpty(value))
64 PerformRecursiveSearch(0, Inventory.RootFolder.UUID);
68 void PerformRecursiveSearch(int level, UUID folderID)
70 var me = Inventory.Items[folderID].Data;
71 var sorted = Inventory.GetContents(folderID);
73 sorted.Sort((InventoryBase b1, InventoryBase b2) =>
75 if (b1 is InventoryFolder && !(b2 is InventoryFolder))
79 else if (!(b1 is InventoryFolder) && b2 is InventoryFolder)
85 return string.Compare(b1.Name, b2.Name);
89 foreach (var item in sorted)
91 if (item is InventoryFolder)
93 PerformRecursiveSearch(level + 1, item.UUID);
97 var it = item as InventoryItem;
99 if (it.UUID.ToString().Contains(searchString))
105 class DeliveryQueue {
106 public string ClassName { get; set; }
107 public string id {get;set;}
108 public string userUUID {get;set;}
109 public string objectUUID {get;set;}
110 public string price {get;set;}
111 public string created { get; set; }
112 public string delivered { get; set; }
115 private string RequestVendor(string action, Dictionary<string, string> param = null)
119 var webRequest = WebRequest.Create(this.vendURL);
121 string postData = "action=" + action;
122 if (param != null && param.Count > 0)
124 var kv = param.Select(p => "&" + p.Key + "=" +p.Value);
125 postData += String.Join("", kv.ToArray());
127 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
129 webRequest.Method = "POST";
130 webRequest.ContentType = "application/x-www-form-urlencoded";
131 webRequest.ContentLength = byteArray.Length;
133 // add post data to request
134 Stream postStream = webRequest.GetRequestStream();
135 postStream.Write(byteArray, 0, byteArray.Length);
139 using (var response = webRequest.GetResponse())
140 using (var content = response.GetResponseStream())
141 using (var reader = new System.IO.StreamReader(content))
143 return reader.ReadToEnd();
149 private List<DeliveryQueue> parseResponse(string content)
151 List<DeliveryQueue> queue = new List<DeliveryQueue>();
153 if (String.IsNullOrEmpty(content)) return queue;
155 System.Reflection.PropertyInfo[] propertyInfos = typeof(DeliveryQueue).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
157 string field_separator = "|";
159 var lines = content.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
160 foreach (string l in lines)
164 var deliveryQ = new DeliveryQueue();
165 foreach (System.Reflection.PropertyInfo pInfo in propertyInfos)
167 var nextPos = l.IndexOf(field_separator, lastPos);
169 pInfo.SetValue(deliveryQ, l.Substring(lastPos, nextPos - lastPos), null);
171 lastPos = nextPos + 1;
174 queue.Add(deliveryQ);
179 private void SendObject(DeliveryQueue p)
182 searchString = p.objectUUID;
183 if (searchRes.Count <= 0)
185 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product not found '" + searchString + "' for user '"+p.userUUID+"'", ChatBufferTextStyle.Error);
188 if (searchRes.Count > 1) {
189 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": More then one product found for '" + searchString + "'", ChatBufferTextStyle.Error);
193 var inv = searchRes[0] as InventoryItem;
195 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but not an inventory item", ChatBufferTextStyle.Error);
199 Manager.GiveItem(inv.UUID, inv.Name, inv.AssetType, OpenMetaverse.UUID.Parse(p.userUUID), false);
200 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": PRODUCT '" + searchRes[0].Name + "' SENT TO " + p.userUUID, ChatBufferTextStyle.StatusBlue);
202 Dictionary<string,string> param = new Dictionary<string,string>();
203 param.Add("id", p.id);
204 this.RequestVendor("SETDELIVERED", param);
207 private bool isSending = false;
208 private void productCallback(object obj)
210 Manager = Client.Inventory;
211 Inventory = Manager.Store;
212 Inventory.RootFolder.OwnerID = Client.Self.AgentID;
214 if (isSending == true)
216 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting...");
221 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Queue List");
223 var strContent = this.RequestVendor("GETOUTSTANDING");
224 List<DeliveryQueue> queue = this.parseResponse(strContent);
226 // check if i have something to do
227 if (queue.Count <= 0) return;
229 foreach (DeliveryQueue p in queue)
232 /*var grouped = queue.GroupBy(p => p.objectUUID).Select(t=> new { count = t.Count(), UUID = t.Key });
233 foreach (var g in grouped)
235 var userIds = queue.Where(p => p.objectUUID == g.UUID).Select(p => p.id);
236 if (userIds.Count() > 0)
238 var users = String.Join(",", userIds.ToArray());
239 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ":" + users, ChatBufferTextStyle.Normal);