OSDN Git Service

EVOVend.cs bot script to deliver stuff from vendor scripts
[radegast/radegast.git] / plugins / Radegast.Plugin.EVOVend / EVOVend.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Threading;
5 using System.Windows.Forms;
6 using Radegast;
7 using OpenMetaverse;
8
9 using System.Text.RegularExpressions;
10 using System.Net;
11 using System.Linq;
12 using System.IO;
13
14 namespace Radegast.Plugin.Demo
15 {
16     [Radegast.Plugin(Name="EVOVend Plugin", Description="EVO Vendor Delivery System", Version="1.0")]
17     public class DemoPlugin : IRadegastPlugin
18     {
19         private System.Threading.Timer timer;
20         private InventoryManager Manager;
21         private OpenMetaverse.Inventory Inventory;
22
23         private string vendURL = @"http://evosl.org/TREK/SL/index.php";
24         List<InventoryBase> searchRes = new List<InventoryBase>();
25
26         private RadegastInstance Instance;
27         private GridClient Client { get { return Instance.Client; } }
28
29         private string pluginName = "EVOVend";
30         private string version = "1.0";
31
32         public DemoPlugin ()
33             {
34             
35             }
36
37         public void StartPlugin(RadegastInstance inst)
38         {
39             Instance = inst;
40             Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + " version " + version + " loaded");
41
42             // setup timer
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...");
46         }
47
48         public void StopPlugin(RadegastInstance instance)
49         {
50             // kill timer
51             timer.Dispose();
52         }
53
54         private string m_searchString;
55         public string searchString { 
56             get 
57             {
58                 return m_searchString;
59             }
60             set 
61             {
62                 m_searchString = value;
63                 if(!String.IsNullOrEmpty(value))
64                     PerformRecursiveSearch(0, Inventory.RootFolder.UUID);
65             }
66         }
67         
68         void PerformRecursiveSearch(int level, UUID folderID)
69         {
70             var me = Inventory.Items[folderID].Data;
71             var sorted = Inventory.GetContents(folderID);
72
73             sorted.Sort((InventoryBase b1, InventoryBase b2) =>
74             {
75                 if (b1 is InventoryFolder && !(b2 is InventoryFolder))
76                 {
77                     return -1;
78                 }
79                 else if (!(b1 is InventoryFolder) && b2 is InventoryFolder)
80                 {
81                     return 1;
82                 }
83                 else
84                 {
85                     return string.Compare(b1.Name, b2.Name);
86                 }
87             });
88
89             foreach (var item in sorted)
90             {
91                 if (item is InventoryFolder)
92                 {
93                     PerformRecursiveSearch(level + 1, item.UUID);
94                 }
95                 else
96                 {
97                     var it = item as InventoryItem;
98
99                     if (it.UUID.ToString().Contains(searchString))
100                         searchRes.Add(it); 
101                 }
102             }
103         }
104
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; }
113         }
114
115         private string RequestVendor(string action, Dictionary<string, string> param = null)
116         {
117             try
118             {
119                 var webRequest = WebRequest.Create(this.vendURL);
120
121                 string postData = "action=" + action;
122                 if (param != null && param.Count > 0)
123                 {
124                     var kv = param.Select(p => "&" + p.Key + "=" +p.Value);
125                     postData += String.Join("", kv.ToArray());
126                 }
127                 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
128
129                 webRequest.Method = "POST";
130                 webRequest.ContentType = "application/x-www-form-urlencoded";
131                 webRequest.ContentLength = byteArray.Length;
132
133                 // add post data to request
134                 Stream postStream = webRequest.GetRequestStream();
135                 postStream.Write(byteArray, 0, byteArray.Length);
136                 postStream.Flush();
137                 postStream.Close();
138
139                 using (var response = webRequest.GetResponse())
140                 using (var content = response.GetResponseStream())
141                 using (var reader = new System.IO.StreamReader(content))
142                 {
143                     return reader.ReadToEnd();
144                 }
145             }catch { }
146             return null;
147         }
148
149         private List<DeliveryQueue> parseResponse(string content)
150         {
151             List<DeliveryQueue> queue = new List<DeliveryQueue>();
152
153             if (String.IsNullOrEmpty(content)) return queue;
154
155             System.Reflection.PropertyInfo[] propertyInfos = typeof(DeliveryQueue).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
156
157             string field_separator = "|";
158
159             var lines = content.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
160             foreach (string l in lines)
161             {
162                 int lastPos = 0;
163
164                 var deliveryQ = new DeliveryQueue();
165                 foreach (System.Reflection.PropertyInfo pInfo in propertyInfos)
166                 {
167                     var nextPos = l.IndexOf(field_separator, lastPos);
168                     if(nextPos > -1){
169                         pInfo.SetValue(deliveryQ, l.Substring(lastPos, nextPos - lastPos), null);
170                     }
171                     lastPos = nextPos + 1;
172                 }
173
174                 queue.Add(deliveryQ);
175             }
176             return queue;
177         }
178
179         private void SendObject(DeliveryQueue p)
180         {
181             searchRes.Clear();
182             searchString = p.objectUUID;
183             if (searchRes.Count <= 0)
184             {
185                 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product not found '" + searchString + "' for user '"+p.userUUID+"'", ChatBufferTextStyle.Error);
186                 return;
187             }
188             if (searchRes.Count > 1) {
189                 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": More then one product found for '" + searchString + "'", ChatBufferTextStyle.Error);
190                 return;
191             }
192             
193             var inv = searchRes[0] as InventoryItem;
194             if(inv == null) {
195                 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Product found, but not an inventory item", ChatBufferTextStyle.Error);
196                 return;
197             }
198
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);
201
202             Dictionary<string,string> param = new Dictionary<string,string>();
203             param.Add("id", p.id);
204             this.RequestVendor("SETDELIVERED", param);
205         }
206
207         private bool isSending = false;
208         private void productCallback(object obj)
209         {
210             Manager = Client.Inventory;
211             Inventory = Manager.Store;
212             Inventory.RootFolder.OwnerID = Client.Self.AgentID;
213
214             if (isSending == true)
215             {
216                 Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Waiting...");
217                 return;
218             }
219             isSending = true;
220
221             Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ": Queue List");
222
223             var strContent = this.RequestVendor("GETOUTSTANDING");
224             List<DeliveryQueue> queue = this.parseResponse(strContent);
225
226             // check if i have something to do
227             if (queue.Count <= 0) return;
228
229             foreach (DeliveryQueue p in queue)
230                 this.SendObject(p);
231
232             /*var grouped = queue.GroupBy(p => p.objectUUID).Select(t=> new { count = t.Count(), UUID = t.Key });
233             foreach (var g in grouped)
234             {
235                 var userIds = queue.Where(p => p.objectUUID == g.UUID).Select(p => p.id);
236                 if (userIds.Count() > 0)
237                 {
238                     var users = String.Join(",", userIds.ToArray());
239                     Instance.MainForm.TabConsole.DisplayNotificationInChat(pluginName + ":" + users, ChatBufferTextStyle.Normal);
240                 }
241             }*/
242
243             isSending = false;
244         }
245     }
246 }