using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Diagnostics; namespace Misuzilla.Applications.TwitterIrcGateway { public class Server { private TcpListener _tcpListener; private List _sessions; private Encoding _encoding = Encoding.GetEncoding("ISO-2022-JP"); /// /// チェックする間隔 /// public Int32 Interval = 60; /// /// ダイレクトメッセージをチェックする間隔 /// public Int32 IntervalDirectMessage = 60 * 5; /// /// Repliesをチェックするかどうか /// public Boolean EnableRepliesCheck = false; /// /// Repliesチェックする間隔 /// public Int32 IntervalReplies = 60 * 5; /// /// エラーを無視するかどうか /// public Boolean IgnoreWatchError = false; /// /// TinyURLを展開するかどうか /// public Boolean ResolveTinyUrl = true; /// /// 取りこぼし防止を利用するかどうか /// public Boolean EnableDropProtection = true; /// /// ステータスを更新したときにトピックを変更するかどうか /// public Boolean SetTopicOnStatusChanged = false; /// /// トレースを有効にするかどうか /// public Boolean EnableTrace = false; /// /// Cookie ログインでタイムラインを取得するかどうか /// public Boolean CookieLoginMode = false; /// /// Twitterのステータスが流れるチャンネル名 /// public String ChannelName = "#twitter"; /// /// ユーザ一覧を取得するかどうか /// public Boolean DisableUserList = false; /// /// アップデートをすべてのチャンネルに投げるかどうか /// public Boolean BroadcastUpdate = false; /// /// クライアントにメッセージを送信するときのウェイト /// public Int32 ClientMessageWait = 0; /// /// アップデートをすべてのチャンネルに投げるときNOTICEにするかどうか /// public Boolean BroadcastUpdateMessageIsNotice = false; /// /// APIアクセスに利用するプロクシサーバの設定 /// public IWebProxy Proxy = null; /// /// データの取得にPOSTメソッドを利用するかどうか /// public Boolean POSTFetchMode = false; public const String ServerName = "localhost"; public const String ServerNick = "$TwitterIrcGatewayServer$"; public event EventHandler SessionStartedReceived; void AcceptHandled(IAsyncResult ar) { if (_tcpListener != null && ar.IsCompleted) { TcpClient tcpClient = _tcpListener.EndAcceptTcpClient(ar); _tcpListener.BeginAcceptTcpClient(AcceptHandled, this); Trace.WriteLine(String.Format("Client Connected: RemoteEndPoint={0}", tcpClient.Client.RemoteEndPoint)); Session session = new Session(this, tcpClient); lock (_sessions) { _sessions.Add(session); } session.SessionStarted += new EventHandler(session_SessionStartedReceived); session.SessionEnded += new EventHandler(session_SessionEnded); session.Start(); } } void session_SessionEnded(object sender, EventArgs e) { lock (_sessions) { _sessions.Remove(sender as Session); } } void session_SessionStartedReceived(object sender, SessionStartedEventArgs e) { // 中継 if (SessionStartedReceived != null) { SessionStartedReceived(sender, e); } } public Encoding Encoding { get { return _encoding; } set { _encoding = value; } } public Boolean IsRunning { get { return _tcpListener != null; } } public void Start(IPAddress ipAddr, Int32 port) { if (IsRunning) { throw new InvalidOperationException(); } _sessions = new List(); Trace.WriteLine(String.Format("Starting IRC Server: IPAddress = {0}, port = {1}", ipAddr, port)); _tcpListener = new TcpListener(ipAddr, port); _tcpListener.Start(); _tcpListener.BeginAcceptTcpClient(AcceptHandled, this); } public void Stop() { lock (_sessions) { foreach (Session session in _sessions) { session.Close(); } } if (_tcpListener != null) { _tcpListener.Stop(); _tcpListener = null; } } } }