diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Environment/Modules/ChatModule.cs | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/ChatModule.cs b/OpenSim/Region/Environment/Modules/ChatModule.cs new file mode 100644 index 0000000..703fe65 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/ChatModule.cs | |||
@@ -0,0 +1,165 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Net; | ||
5 | using System.Net.Sockets; | ||
6 | using System.Threading; | ||
7 | using System.IO; | ||
8 | using libsecondlife; | ||
9 | using OpenSim.Region.Environment.Scenes; | ||
10 | using OpenSim.Region.Environment.Interfaces; | ||
11 | using OpenSim.Framework.Interfaces; | ||
12 | using OpenSim.Framework.Utilities; | ||
13 | |||
14 | namespace OpenSim.Region.Environment.Modules | ||
15 | { | ||
16 | public class ChatModule :IRegionModule | ||
17 | { | ||
18 | private Scene m_scene; | ||
19 | |||
20 | private string m_server = "irc2.choopa.net"; | ||
21 | |||
22 | private int m_port = 6668; | ||
23 | private string m_user = "USER OpenSimBot 8 * :I'm a OpenSim to irc bot"; | ||
24 | private string m_nick = "OpenSimBoT"; | ||
25 | private string m_channel = "#opensim"; | ||
26 | |||
27 | private NetworkStream m_stream; | ||
28 | private TcpClient m_irc; | ||
29 | private StreamWriter m_ircWriter; | ||
30 | private StreamReader m_ircReader; | ||
31 | |||
32 | private Thread pingSender; | ||
33 | |||
34 | private bool connected = false; | ||
35 | |||
36 | public ChatModule() | ||
37 | { | ||
38 | |||
39 | } | ||
40 | |||
41 | public void Initialise(Scene scene) | ||
42 | { | ||
43 | m_scene = scene; | ||
44 | m_scene.EventManager.OnNewClient += NewClient; | ||
45 | |||
46 | //should register a optional API Method, so other modules can send chat messages using this module | ||
47 | } | ||
48 | |||
49 | public void PostInitialise() | ||
50 | { | ||
51 | try | ||
52 | { | ||
53 | m_irc = new TcpClient(m_server, m_port); | ||
54 | m_stream = m_irc.GetStream(); | ||
55 | m_ircReader = new StreamReader(m_stream); | ||
56 | m_ircWriter = new StreamWriter(m_stream); | ||
57 | |||
58 | pingSender = new Thread(new ThreadStart(this.PingRun)); | ||
59 | pingSender.Start(); | ||
60 | |||
61 | m_ircWriter.WriteLine(m_user); | ||
62 | m_ircWriter.Flush(); | ||
63 | m_ircWriter.WriteLine("NICK " + m_nick); | ||
64 | m_ircWriter.Flush(); | ||
65 | m_ircWriter.WriteLine("JOIN " + m_channel); | ||
66 | m_ircWriter.Flush(); | ||
67 | connected = true; | ||
68 | } | ||
69 | catch (Exception e) | ||
70 | { | ||
71 | Console.WriteLine(e.ToString()); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | public void CloseDown() | ||
76 | { | ||
77 | m_ircWriter.Close(); | ||
78 | m_ircReader.Close(); | ||
79 | m_irc.Close(); | ||
80 | } | ||
81 | |||
82 | public string GetName() | ||
83 | { | ||
84 | return "ChatModule"; | ||
85 | } | ||
86 | |||
87 | public void NewClient(IClientAPI client) | ||
88 | { | ||
89 | client.OnChatFromViewer += SimChat; | ||
90 | } | ||
91 | |||
92 | public void PingRun() | ||
93 | { | ||
94 | while (true) | ||
95 | { | ||
96 | m_ircWriter.WriteLine("PING :" + m_server); | ||
97 | m_ircWriter.Flush(); | ||
98 | Thread.Sleep(15000); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | public void SimChat(byte[] message, byte type, LLVector3 fromPos, string fromName, LLUUID fromAgentID) | ||
103 | { | ||
104 | ScenePresence avatar = null; | ||
105 | avatar = m_scene.RequestAvatar(fromAgentID); | ||
106 | if (avatar != null) | ||
107 | { | ||
108 | fromPos = avatar.AbsolutePosition; | ||
109 | fromName = avatar.Firstname + " " + avatar.Lastname; | ||
110 | avatar = null; | ||
111 | } | ||
112 | |||
113 | if (connected) | ||
114 | { | ||
115 | m_ircWriter.WriteLine("MSG " + m_channel +" :" + fromName + ", " + Util.FieldToString(message)); | ||
116 | m_ircWriter.Flush(); | ||
117 | } | ||
118 | |||
119 | m_scene.ForEachScenePresence(delegate(ScenePresence presence) | ||
120 | { | ||
121 | int dis = -1000; | ||
122 | |||
123 | //err ??? the following code seems to be request a scenePresence when it already has a ref to it | ||
124 | avatar = m_scene.RequestAvatar(presence.ControllingClient.AgentId); | ||
125 | if (avatar != null) | ||
126 | { | ||
127 | dis = (int)avatar.AbsolutePosition.GetDistanceTo(fromPos); | ||
128 | } | ||
129 | |||
130 | switch (type) | ||
131 | { | ||
132 | case 0: // Whisper | ||
133 | if ((dis < 10) && (dis > -10)) | ||
134 | { | ||
135 | //should change so the message is sent through the avatar rather than direct to the ClientView | ||
136 | presence.ControllingClient.SendChatMessage(message, type, fromPos, fromName, | ||
137 | fromAgentID); | ||
138 | } | ||
139 | break; | ||
140 | case 1: // Say | ||
141 | if ((dis < 30) && (dis > -30)) | ||
142 | { | ||
143 | //Console.WriteLine("sending chat"); | ||
144 | presence.ControllingClient.SendChatMessage(message, type, fromPos, fromName, | ||
145 | fromAgentID); | ||
146 | } | ||
147 | break; | ||
148 | case 2: // Shout | ||
149 | if ((dis < 100) && (dis > -100)) | ||
150 | { | ||
151 | presence.ControllingClient.SendChatMessage(message, type, fromPos, fromName, | ||
152 | fromAgentID); | ||
153 | } | ||
154 | break; | ||
155 | |||
156 | case 0xff: // Broadcast | ||
157 | presence.ControllingClient.SendChatMessage(message, type, fromPos, fromName, | ||
158 | fromAgentID); | ||
159 | break; | ||
160 | } | ||
161 | }); | ||
162 | } | ||
163 | |||
164 | } | ||
165 | } | ||