corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1  
2 using System;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.IO;
6 using System.Net;
7 using System.Text.RegularExpressions;
8 using System.Reflection;
9 using Nwc.XmlRpc;
10 using OpenMetaverse;
11 using OpenMetaverse.Packets;
12 using GridProxy;
13 using Logger = OpenMetaverse.Logger;
14  
15  
16 namespace GridProxy
17 {
18 public class ProxyFrame
19 {
20 public Proxy proxy;
21 private Dictionary<string, CommandDelegate> commandDelegates = new Dictionary<string, CommandDelegate>();
22 private UUID agentID;
23 private UUID sessionID;
24 private UUID secureSessionID;
25 private UUID inventoryRoot;
26 private bool logLogin = false;
27 private string[] args;
28  
29 public delegate void CommandDelegate(string[] words);
30  
31 public string[] Args
32 {
33 get { return args; }
34 }
35  
36 public UUID AgentID
37 {
38 get { return agentID; }
39 }
40  
41 public UUID SessionID
42 {
43 get { return sessionID; }
44 }
45  
46 public UUID SecureSessionID
47 {
48 get { return secureSessionID; }
49 }
50  
51 public UUID InventoryRoot
52 {
53 get { return inventoryRoot; }
54 }
55  
56 public void AddCommand(string cmd, CommandDelegate deleg)
57 {
58 commandDelegates[cmd] = deleg;
59 }
60  
61 public ProxyFrame(string[] args)
62 {
63 Init(args, null);
64 }
65  
66 public ProxyFrame(string[] args, ProxyConfig proxyConfig)
67 {
68 Init(args, proxyConfig);
69 }
70  
71 private void Init(string[] args, ProxyConfig proxyConfig)
72 {
73 //bool externalPlugin = false;
74 this.args = args;
75  
76 if (proxyConfig == null)
77 {
78 proxyConfig = new ProxyConfig("GridProxy", "Austin Jennings / Andrew Ortman", args, true);
79 }
80 proxy = new Proxy(proxyConfig);
81  
82 // add delegates for login
83 proxy.AddLoginRequestDelegate(new XmlRpcRequestDelegate(LoginRequest));
84 proxy.AddLoginResponseDelegate(new XmlRpcResponseDelegate(LoginResponse));
85  
86 // add a delegate for outgoing chat
87 proxy.AddDelegate(PacketType.ChatFromViewer, Direction.Outgoing, new PacketDelegate(ChatFromViewerOut));
88  
89 // handle command line arguments
90 foreach (string arg in args)
91 if (arg == "--log-login")
92 logLogin = true;
93 else if (arg.Substring(0, 2) == "--")
94 {
95 int ipos = arg.IndexOf("=");
96 if (ipos != -1)
97 {
98 string sw = arg.Substring(0, ipos);
99 string val = arg.Substring(ipos + 1);
100  
101 Logger.Log("arg '" + sw + "' val '" + val + "'", Helpers.LogLevel.Debug);
102  
103 if (sw == "--load")
104 {
105 //externalPlugin = true;
106 LoadPlugin(val);
107 }
108 }
109 }
110  
111 commandDelegates["/load"] = new CommandDelegate(CmdLoad);
112 }
113  
114 private void CmdLoad(string[] words)
115 {
116 if (words.Length != 2)
117 SayToUser("Usage: /load <plugin name>");
118 else
119 {
120 try
121 {
122 LoadPlugin(words[1]);
123 }
124 catch (Exception e)
125 {
126 Logger.Log("LoadPlugin exception", Helpers.LogLevel.Error, e);
127 }
128 }
129 }
130  
131 public void LoadPlugin(string name)
132 {
133  
134 Assembly assembly = Assembly.LoadFile(Path.GetFullPath(name));
135 foreach (Type t in assembly.GetTypes())
136 {
137 try
138 {
139 if (t.IsSubclassOf(typeof(ProxyPlugin)))
140 {
141 ConstructorInfo info = t.GetConstructor(new Type[] { typeof(ProxyFrame) });
142 ProxyPlugin plugin = (ProxyPlugin)info.Invoke(new object[] { this });
143 plugin.Init();
144 }
145 }
146 catch (Exception e)
147 {
148 Logger.Log("LoadPlugin exception", Helpers.LogLevel.Error, e);
149 }
150 }
151  
152 }
153  
154 // LoginRequest: dump a login request to the console
155 private void LoginRequest(object sender, XmlRpcRequestEventArgs e)
156 {
157 if (logLogin)
158 {
159 Console.WriteLine("==> Login Request");
160 Console.WriteLine(e.m_Request);
161 }
162 }
163  
164 // Loginresponse: dump a login response to the console
165 private void LoginResponse(XmlRpcResponse response)
166 {
167 System.Collections.Hashtable values = (System.Collections.Hashtable)response.Value;
168 if (values.Contains("agent_id"))
169 agentID = new UUID((string)values["agent_id"]);
170 if (values.Contains("session_id"))
171 sessionID = new UUID((string)values["session_id"]);
172 if (values.Contains("secure_session_id"))
173 secureSessionID = new UUID((string)values["secure_session_id"]);
174 if (values.Contains("inventory-root"))
175 {
176 inventoryRoot = new UUID(
177 (string)((System.Collections.Hashtable)(((System.Collections.ArrayList)values["inventory-root"])[0]))["folder_id"]
178 );
179 if (logLogin)
180 {
181 Console.WriteLine("inventory root: " + inventoryRoot);
182 }
183 }
184  
185 if (logLogin)
186 {
187 Console.WriteLine("<== Login Response");
188 Console.WriteLine(response);
189 }
190 }
191  
192 // ChatFromViewerOut: outgoing ChatFromViewer delegate; check for Analyst commands
193 private Packet ChatFromViewerOut(Packet packet, IPEndPoint sim)
194 {
195 // deconstruct the packet
196 ChatFromViewerPacket cpacket = (ChatFromViewerPacket)packet;
197 string message = System.Text.Encoding.UTF8.GetString(cpacket.ChatData.Message).Replace("\0", "");
198  
199 if (message.Length > 1 && message[0] == '/')
200 {
201 string[] words = message.Split(' ');
202 if (commandDelegates.ContainsKey(words[0]))
203 {
204 // this is an Analyst command; act on it and drop the chat packet
205 ((CommandDelegate)commandDelegates[words[0]])(words);
206 return null;
207 }
208 }
209  
210 return packet;
211 }
212  
213 /// <summary>
214 /// Send a message to the viewer
215 /// </summary>
216 /// <param name="message">A string containing the message to send</param>
217 public void SayToUser(string message)
218 {
219 SayToUser("GridProxy", message);
220 }
221  
222  
223 /// <summary>
224 /// Send a message to the viewer
225 /// </summary>
226 /// <param name="fromName">A string containing text indicating the origin of the message</param>
227 /// <param name="message">A string containing the message to send</param>
228 public void SayToUser(string fromName, string message)
229 {
230 ChatFromSimulatorPacket packet = new ChatFromSimulatorPacket();
231 packet.ChatData.SourceID = UUID.Random();
232 packet.ChatData.FromName = Utils.StringToBytes(fromName);
233 packet.ChatData.OwnerID = agentID;
234 packet.ChatData.SourceType = (byte)2;
235 packet.ChatData.ChatType = (byte)1;
236 packet.ChatData.Audible = (byte)1;
237 packet.ChatData.Position = new Vector3(0, 0, 0);
238 packet.ChatData.Message = Utils.StringToBytes(message);
239 proxy.InjectPacket(packet, Direction.Incoming);
240 }
241 }
242  
243 public abstract class ProxyPlugin : MarshalByRefObject
244 {
245 // public abstract ProxyPlugin(ProxyFrame main);
246 public abstract void Init();
247 }
248 }