29 using System.Collections.Generic;
 
   30 using System.Reflection;
 
   31 using System.Text.RegularExpressions;
 
   34 using OpenSim.Framework;
 
   35 using OpenSim.Region.Framework.Interfaces;
 
   36 using OpenSim.Region.Framework.Scenes;
 
   42     internal class RegionState
 
   44         private static readonly ILog m_log =
 
   45             LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
   50         private const int DEBUG_CHANNEL = 2147483647;
 
   52         private static int _idk_ = 0;
 
   57         internal string Region = String.Empty;
 
   58         internal string Host = String.Empty;
 
   59         internal string LocX = String.Empty;
 
   60         internal string LocY = String.Empty;
 
   61         internal string IDK = String.Empty;
 
   65         internal ChannelState cs = null; 
 
   66         internal Scene scene = null; 
 
   67         internal IConfig config = null; 
 
   68         internal bool enabled = 
true;
 
   71         internal bool showAlert = 
false;
 
   72         internal string alertMessage = String.Empty;
 
   78         internal List<IClientAPI> clients = 
new List<IClientAPI>();
 
   82         public RegionState(
Scene p_scene, IConfig p_config)
 
   87             Region = scene.RegionInfo.RegionName;
 
   88             Host = scene.RegionInfo.ExternalHostName;
 
   89             LocX = Convert.ToString(scene.RegionInfo.RegionLocX);
 
   90             LocY = Convert.ToString(scene.RegionInfo.RegionLocY);
 
   91             IDK = Convert.ToString(_idk_++);
 
   93             showAlert = config.GetBoolean(
"alert_show", 
false);
 
   94             string alertServerInfo = String.Empty;
 
   98                 bool showAlertServerInfo = config.GetBoolean(
"alert_show_serverinfo", 
true);
 
  100                 if (showAlertServerInfo)
 
  101                     alertServerInfo = String.Format(
"\nServer: {0}\nPort: {1}\nChannel: {2}\n\n",
 
  102                         config.GetString(
"server", 
""), config.GetString(
"port", 
""), config.GetString(
"channel", 
""));
 
  104                 string alertPreMessage = config.GetString(
"alert_msg_pre", 
"This region is linked to Irc.");
 
  105                 string alertPostMessage = config.GetString(
"alert_msg_post", 
"Everything you say in public chat can be listened.");
 
  107                 alertMessage = String.Format(
"{0}\n{1}{2}", alertPreMessage, alertServerInfo, alertPostMessage);
 
  109                 dialogModule = scene.RequestModuleInterface<
IDialogModule>();
 
  116             ChannelState.OpenChannel(
this, config);
 
  120             scene.EventManager.OnChatFromWorld += OnSimChat;
 
  121             scene.EventManager.OnChatFromClient += OnSimChat;
 
  122             scene.EventManager.OnMakeRootAgent += OnMakeRootAgent;
 
  123             scene.EventManager.OnMakeChildAgent += OnMakeChildAgent;
 
  125             m_log.InfoFormat(
"[IRC-Region {0}] Initialization complete", 
Region);
 
  134                 cs.RemoveRegion(
this);
 
  159         private void OnClientLoggedOut(
IClientAPI client)
 
  163                 if (clients.Contains(client))
 
  165                     if (enabled && (cs.irc.Enabled) && (cs.irc.Connected) && (cs.ClientReporting))
 
  167                         m_log.InfoFormat(
"[IRC-Region {0}]: {1} has left", 
Region, client.Name);
 
  169                         if (!cs.ExcludeList.Contains(client.
Name.ToLower()))
 
  171                             cs.irc.PrivMsg(cs.NoticeMessageFormat, cs.irc.Nick, Region, String.Format(
"{0} has left", client.Name));
 
  174                     client.OnLogout -= OnClientLoggedOut;
 
  175                     client.OnConnectionClosed -= OnClientLoggedOut;
 
  176                     clients.Remove(client);
 
  181                 m_log.ErrorFormat(
"[IRC-Region {0}]: ClientLoggedOut exception: {1}", 
Region, ex.Message);
 
  192             IClientAPI client = presence.ControllingClient;
 
  196                 if (clients.Contains(client))
 
  198                     if (enabled && (cs.irc.Enabled) && (cs.irc.Connected) && (cs.ClientReporting))
 
  200                         string clientName = String.Format(
"{0} {1}", presence.Firstname, presence.Lastname);
 
  201                         m_log.DebugFormat(
"[IRC-Region {0}] {1} has left", 
Region, clientName);
 
  202                         cs.irc.PrivMsg(cs.NoticeMessageFormat, cs.irc.Nick, Region, String.Format(
"{0} has left", clientName));
 
  204                     client.OnLogout -= OnClientLoggedOut;
 
  205                     client.OnConnectionClosed -= OnClientLoggedOut;
 
  206                     clients.Remove(client);
 
  211                 m_log.ErrorFormat(
"[IRC-Region {0}]: MakeChildAgent exception: {1}", 
Region, ex.Message);
 
  222             IClientAPI client = presence.ControllingClient;
 
  226                 if (!clients.Contains(client))
 
  228                     client.OnLogout += OnClientLoggedOut;
 
  229                     client.OnConnectionClosed += OnClientLoggedOut;
 
  231                     if (enabled && (cs.irc.Enabled) && (cs.irc.Connected) && (cs.ClientReporting))
 
  233                         string clientName = String.Format(
"{0} {1}", presence.Firstname, presence.Lastname);
 
  234                         m_log.DebugFormat(
"[IRC-Region {0}] {1} has arrived", 
Region, clientName);
 
  236                         if (!cs.ExcludeList.Contains(clientName.ToLower()))
 
  238                             cs.irc.PrivMsg(cs.NoticeMessageFormat, cs.irc.Nick, Region, String.Format(
"{0} has arrived", clientName));
 
  243                 if (dialogModule != null && showAlert)
 
  244                     dialogModule.SendAlertToUser(client, alertMessage, 
true);
 
  248                 m_log.ErrorFormat(
"[IRC-Region {0}]: MakeRootAgent exception: {1}", 
Region, ex.Message);
 
  259             if (cs.irc.Equals(sender)) 
return;
 
  263             if (msg.
Message.Length == 0) 
return;
 
  268             if (cs.CommandsEnabled && msg.
Channel == cs.CommandChannel)
 
  271                 m_log.DebugFormat(
"[IRC-Region {0}] command on channel {1}: {2}", 
Region, msg.Channel, msg.Message);
 
  273                 string[] messages = msg.Message.Split(
' ');
 
  274                 string command = messages[0].ToLower();
 
  286                             cs = cs.UpdateServer(
this, messages[1]);
 
  291                             cs = cs.UpdatePort(
this, messages[1]);
 
  296                             cs = cs.UpdateChannel(
this, messages[1]);
 
  301                             cs = cs.UpdateNickname(
this, messages[1]);
 
  308                         case "client-reporting":
 
  309                             cs = cs.UpdateClientReporting(
this, messages[1]);
 
  312                             cs = cs.UpdateRelayIn(
this, messages[1]);
 
  315                             cs = cs.UpdateRelayOut(
this, messages[1]);
 
  344                             m_log.DebugFormat(
"[IRC-Region {0}] Forwarding unrecognized command to IRC : {1}",
 
  345                                             Region, msg.Message);
 
  346                             cs.irc.Send(msg.Message);
 
  352                     m_log.WarnFormat(
"[IRC-Region {0}] error processing in-world command channel input: {1}",
 
  370             if (!cs.ValidInWorldChannels.Contains(msg.
Channel))
 
  372                 m_log.DebugFormat(
"[IRC-Region {0}] dropping message {1} on channel {2}", Region, msg, msg.Channel);
 
  377             string fromName = msg.From;
 
  381                 avatar = scene.GetScenePresence(msg.Sender.AgentId);
 
  382                 if (avatar != null) fromName = avatar.Name;
 
  385             if (!cs.irc.Connected)
 
  387                 m_log.WarnFormat(
"[IRC-Region {0}] IRCConnector not connected: dropping message from {1}", Region, fromName);
 
  391             m_log.DebugFormat(
"[IRC-Region {0}] heard on channel {1} : {2}", Region, msg.Channel, msg.Message);
 
  393             if (null != avatar && cs.RelayChat && (msg.
Channel == 0 || msg.
Channel == DEBUG_CHANNEL))
 
  395                 string txt = msg.Message;
 
  396                 if (txt.StartsWith(
"/me "))
 
  397                     txt = 
String.Format(
"{0} {1}", fromName, msg.
Message.Substring(4));
 
  399                 cs.irc.PrivMsg(cs.PrivateMessageFormat, fromName, Region, txt);
 
  403             if (null == avatar && cs.RelayPrivateChannels && null != cs.AccessPassword &&
 
  404                 msg.
Channel == cs.RelayChannelOut)
 
  406                 Match m = cs.AccessPasswordRegex.Match(msg.Message);
 
  409                     m_log.DebugFormat(
"[IRC] relaying message from {0}: {1}", m.Groups[
"avatar"].ToString(),
 
  410                                       m.Groups[
"message"].ToString());
 
  411                     cs.irc.PrivMsg(cs.PrivateMessageFormat, m.Groups[
"avatar"].ToString(),
 
  412                                    scene.RegionInfo.RegionName, m.Groups[
"message"].ToString());
 
  427                 scene.EventManager.TriggerOnChatBroadcast(irc, msg);
 
  434         internal void LocalChat(
string msg)
 
  439                 osm.From = 
"IRC Agent";
 
  441                 osm.Type = ChatTypeEnum.Region;
 
  442                 osm.Position = CenterOfRegion;
 
  444                 osm.SenderUUID = OpenMetaverse.UUID.Zero; 
 
IClientAPI Sender
The client responsible for sending the message, or null. 
string Name
Returns the full name of the agent/avatar represented by this client 
string Message
The message sent by the user 
int Channel
Which channel was this message sent on? Different channels may have different listeners. Public chat is on channel zero.