| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | using System.Net; |
|---|
| 5 | using System.Net.Sockets; |
|---|
| 6 | using System.Diagnostics; |
|---|
| 7 | |
|---|
| 8 | namespace Misuzilla.Applications.TwitterIrcGateway |
|---|
| 9 | { |
|---|
| 10 | public class Server |
|---|
| 11 | { |
|---|
| 12 | private TcpListener _tcpListener; |
|---|
| 13 | private List<Session> _sessions; |
|---|
| 14 | private Encoding _encoding = Encoding.GetEncoding("ISO-2022-JP"); |
|---|
| 15 | |
|---|
| 16 | /// <summary> |
|---|
| 17 | /// �`�F�b�N�����u |
|---|
| 18 | /// </summary> |
|---|
| 19 | public Int32 Interval = 60; |
|---|
| 20 | |
|---|
| 21 | /// <summary> |
|---|
| 22 | /// �_�C���N�g���b�Z�[�W��F�b�N�����u |
|---|
| 23 | /// </summary> |
|---|
| 24 | public Int32 IntervalDirectMessage = 60 * 5; |
|---|
| 25 | |
|---|
| 26 | /// <summary> |
|---|
| 27 | /// Replies��F�b�N���邩�ǂ��� |
|---|
| 28 | /// </summary> |
|---|
| 29 | public Boolean EnableRepliesCheck = false; |
|---|
| 30 | |
|---|
| 31 | /// <summary> |
|---|
| 32 | /// Replies�`�F�b�N�����u |
|---|
| 33 | /// </summary> |
|---|
| 34 | public Int32 IntervalReplies = 60 * 5; |
|---|
| 35 | |
|---|
| 36 | /// <summary> |
|---|
| 37 | /// �G���[�����邩�ǂ��� |
|---|
| 38 | /// </summary> |
|---|
| 39 | public Boolean IgnoreWatchError = false; |
|---|
| 40 | |
|---|
| 41 | /// <summary> |
|---|
| 42 | /// TinyURL��J���邩�ǂ��� |
|---|
| 43 | /// </summary> |
|---|
| 44 | public Boolean ResolveTinyUrl = true; |
|---|
| 45 | |
|---|
| 46 | /// <summary> |
|---|
| 47 | /// �����ڂ��h�~�𗘗p���邩�ǂ��� |
|---|
| 48 | /// </summary> |
|---|
| 49 | public Boolean EnableDropProtection = true; |
|---|
| 50 | |
|---|
| 51 | /// <summary> |
|---|
| 52 | /// �X�e�[�^�X��V�����Ƃ��Ƀg�s�b�N��X���邩�ǂ��� |
|---|
| 53 | /// </summary> |
|---|
| 54 | public Boolean SetTopicOnStatusChanged = false; |
|---|
| 55 | |
|---|
| 56 | /// <summary> |
|---|
| 57 | /// �g���[�X����邩�ǂ��� |
|---|
| 58 | /// </summary> |
|---|
| 59 | public Boolean EnableTrace = false; |
|---|
| 60 | |
|---|
| 61 | /// <summary> |
|---|
| 62 | /// Cookie ���O�C���Ń^�C�����C��������邩�ǂ��� |
|---|
| 63 | /// </summary> |
|---|
| 64 | public Boolean CookieLoginMode = false; |
|---|
| 65 | |
|---|
| 66 | /// <summary> |
|---|
| 67 | /// Twitter�̃X�e�[�^�X�������`�����l���� |
|---|
| 68 | /// </summary> |
|---|
| 69 | public String ChannelName = "#twitter"; |
|---|
| 70 | |
|---|
| 71 | /// <summary> |
|---|
| 72 | /// ���[�U�ꗗ������邩�ǂ��� |
|---|
| 73 | /// </summary> |
|---|
| 74 | public Boolean DisableUserList = false; |
|---|
| 75 | |
|---|
| 76 | /// <summary> |
|---|
| 77 | /// �A�b�v�f�[�g��ׂẴ`�����l���ɓ����邩�ǂ��� |
|---|
| 78 | /// </summary> |
|---|
| 79 | public Boolean BroadcastUpdate = false; |
|---|
| 80 | |
|---|
| 81 | /// <summary> |
|---|
| 82 | /// �N���C�A���g�Ƀ��b�Z�[�W�𑗐M�������̃E�F�C�g |
|---|
| 83 | /// </summary> |
|---|
| 84 | public Int32 ClientMessageWait = 0; |
|---|
| 85 | |
|---|
| 86 | /// <summary> |
|---|
| 87 | /// �A�b�v�f�[�g��ׂẴ`�����l���ɓ�������NOTICE�ɂ��邩�ǂ��� |
|---|
| 88 | /// </summary> |
|---|
| 89 | public Boolean BroadcastUpdateMessageIsNotice = false; |
|---|
| 90 | |
|---|
| 91 | /// <summary> |
|---|
| 92 | /// API�A�N�Z�X�ɗ��p�������N�V�T�[�o�̐ݒ� /// </summary> |
|---|
| 93 | public IWebProxy Proxy = null; |
|---|
| 94 | |
|---|
| 95 | /// <summary> |
|---|
| 96 | /// �f�[�^�̎擾��OST���\�b�h�𗘗p���邩�ǂ��� |
|---|
| 97 | /// </summary> |
|---|
| 98 | public Boolean POSTFetchMode = false; |
|---|
| 99 | |
|---|
| 100 | public const String ServerName = "localhost"; |
|---|
| 101 | public const String ServerNick = "$TwitterIrcGatewayServer$"; |
|---|
| 102 | |
|---|
| 103 | public event EventHandler<SessionStartedEventArgs> SessionStartedReceived; |
|---|
| 104 | |
|---|
| 105 | void AcceptHandled(IAsyncResult ar) |
|---|
| 106 | { |
|---|
| 107 | if (_tcpListener != null && ar.IsCompleted) |
|---|
| 108 | { |
|---|
| 109 | TcpClient tcpClient = _tcpListener.EndAcceptTcpClient(ar); |
|---|
| 110 | _tcpListener.BeginAcceptTcpClient(AcceptHandled, this); |
|---|
| 111 | |
|---|
| 112 | Trace.WriteLine(String.Format("Client Connected: RemoteEndPoint={0}", tcpClient.Client.RemoteEndPoint)); |
|---|
| 113 | Session session = new Session(this, tcpClient); |
|---|
| 114 | lock (_sessions) |
|---|
| 115 | { |
|---|
| 116 | _sessions.Add(session); |
|---|
| 117 | } |
|---|
| 118 | session.SessionStarted += new EventHandler<SessionStartedEventArgs>(session_SessionStartedReceived); |
|---|
| 119 | session.SessionEnded += new EventHandler<EventArgs>(session_SessionEnded); |
|---|
| 120 | session.Start(); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | void session_SessionEnded(object sender, EventArgs e) |
|---|
| 125 | { |
|---|
| 126 | lock (_sessions) |
|---|
| 127 | { |
|---|
| 128 | _sessions.Remove(sender as Session); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void session_SessionStartedReceived(object sender, SessionStartedEventArgs e) |
|---|
| 133 | { |
|---|
| 134 | // ���p |
|---|
| 135 | if (SessionStartedReceived != null) |
|---|
| 136 | { |
|---|
| 137 | SessionStartedReceived(sender, e); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | public Encoding Encoding |
|---|
| 142 | { |
|---|
| 143 | get { return _encoding; } |
|---|
| 144 | set { _encoding = value; } |
|---|
| 145 | } |
|---|
| 146 | public Boolean IsRunning |
|---|
| 147 | { |
|---|
| 148 | get { return _tcpListener != null; } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | public void Start(IPAddress ipAddr, Int32 port) |
|---|
| 152 | { |
|---|
| 153 | if (IsRunning) |
|---|
| 154 | { |
|---|
| 155 | throw new InvalidOperationException(); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | _sessions = new List<Session>(); |
|---|
| 159 | |
|---|
| 160 | Trace.WriteLine(String.Format("Starting IRC Server: IPAddress = {0}, port = {1}", ipAddr, port)); |
|---|
| 161 | _tcpListener = new TcpListener(ipAddr, port); |
|---|
| 162 | _tcpListener.Start(); |
|---|
| 163 | _tcpListener.BeginAcceptTcpClient(AcceptHandled, this); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public void Stop() |
|---|
| 167 | { |
|---|
| 168 | lock (_sessions) |
|---|
| 169 | { |
|---|
| 170 | foreach (Session session in _sessions) |
|---|
| 171 | { |
|---|
| 172 | session.Close(); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | if (_tcpListener != null) |
|---|
| 176 | { |
|---|
| 177 | _tcpListener.Stop(); |
|---|
| 178 | _tcpListener = null; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|