Changeset 319

Show
Ignore:
Timestamp:
09/29/07 03:03:46 (16 months ago)
Author:
tomoyo
Message:

* チャンネル名を設定できるようにした
* CookieログインモードをMonoでも使えるようにした

Location:
TwitterIrcGateway
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • TwitterIrcGateway/TwitterIrcGateway/App.config

    r85 r319  
    4141                <value>False</value> 
    4242            </setting> 
     43            <setting name="TwitterChannelName" serializeAs="String"> 
     44                <value>twitter</value> 
     45            </setting> 
    4346        </Misuzilla.Applications.TwitterIrcGateway.Settings> 
    4447    </userSettings> 
  • TwitterIrcGateway/TwitterIrcGateway/Program.cs

    r84 r319  
    5555            _server.IntervalDirectMessage = _settings.IntervalDirectMessage; 
    5656            _server.CookieLoginMode = _settings.CookieLoginMode; 
     57            _server.ChannelName = "#"+_settings.TwitterChannelName; 
    5758            _server.SessionStartedRecieved += new EventHandler<SessionStartedEventArgs>(_server_SessionStartedRecieved); 
    5859            try 
  • TwitterIrcGateway/TwitterIrcGateway/Settings.Designer.cs

    r85 r319  
    155155            } 
    156156        } 
     157         
     158        [global::System.Configuration.UserScopedSettingAttribute()] 
     159        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
     160        [global::System.Configuration.DefaultSettingValueAttribute("twitter")] 
     161        public string TwitterChannelName { 
     162            get { 
     163                return ((string)(this["TwitterChannelName"])); 
     164            } 
     165            set { 
     166                this["TwitterChannelName"] = value; 
     167            } 
     168        } 
    157169    } 
    158170} 
  • TwitterIrcGateway/TwitterIrcGateway/Settings.settings

    r85 r319  
    3636      <Value Profile="(Default)">False</Value> 
    3737    </Setting> 
     38    <Setting Name="TwitterChannelName" Type="System.String" Scope="User"> 
     39      <Value Profile="(Default)">twitter</Value> 
     40    </Setting> 
    3841  </Settings> 
    3942</SettingsFile> 
  • TwitterIrcGateway/TwitterIrcGatewayCLI/Program.cs

    r85 r319  
    2424            IPAddress bindAddress = IPAddress.Loopback; 
    2525            Encoding encoding = Encoding.GetEncoding("ISO-2022-JP"); 
     26            String channelName = "#twitter"; 
    2627 
    2728            foreach (String arg in args) 
     
    105106                    } 
    106107                } 
     108                else if (arg.StartsWith("--channel-name=")) 
     109                { 
     110                    String channelNameT = arg.Substring("--channel-name=".Length).Replace(" ", ""); 
     111                    if (channelName.Length != 0) 
     112                    { 
     113                        channelName = "#"+channelNameT; 
     114                    } 
     115                } 
    107116                else if (arg.StartsWith("--help")) 
    108117                { 
     
    122131            _server.IntervalDirectMessage = intervalDirectMessage; 
    123132            _server.CookieLoginMode = cookieLoginMode; 
     133            _server.ChannelName = channelName; 
    124134            _server.SessionStartedRecieved += new EventHandler<SessionStartedEventArgs>(_server_SessionStartedRecieved); 
    125135 
     
    135145            Console.WriteLine("[Configuration] IntervalDirectMessage: {0}", _server.IntervalDirectMessage); 
    136146            Console.WriteLine("[Configuration] CookieLoginMode: {0}", _server.CookieLoginMode); 
     147            Console.WriteLine("[Configuration] ChannelName: {0}", _server.ChannelName); 
    137148 
    138149            _server.Start(bindAddress, port); 
  • TwitterIrcGateway/TwitterIrcGatewayCore/Server.cs

    r84 r319  
    5454        public Boolean CookieLoginMode = false; 
    5555 
     56        /// <summary> 
     57        /// Twitter�̃X�e�[�^�X�������`�����l���� 
     58        /// </summary> 
     59        public String ChannelName = "#twitter"; 
     60 
    5661        public const String ServerName = "localhost"; 
    57         public const String ChannelName = "#twitter"; 
    5862        public const String ServerNick = "$twitterGatewayServer$"; 
    5963 
  • TwitterIrcGateway/TwitterIrcGatewayCore/Session.cs

    r84 r319  
    373373            foreach (String channelName in channelNames) 
    374374            { 
    375                 if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, Server.ChannelName, true) == 0) 
     375                if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, _server.ChannelName, true) == 0) 
    376376                { 
    377377                    SendErrorReply(ErrorReply.ERR_NOSUCHCHANNEL, "No such nick/channel"); 
     
    410410            foreach (String channelName in channelNames) 
    411411            { 
    412                 if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, Server.ChannelName, true) == 0) 
     412                if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, _server.ChannelName, true) == 0) 
    413413                { 
    414414                    SendErrorReply(ErrorReply.ERR_NOSUCHCHANNEL, "No such nick/channel"); 
     
    536536            String channelName = e.Message.CommandParams[1]; 
    537537            Trace.WriteLine(String.Format("Invite: {0} -> {1}", userName, channelName)); 
    538             if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, Server.ChannelName, true) == 0) 
     538            if (!channelName.StartsWith("#") || channelName.Length < 3 || String.Compare(channelName, _server.ChannelName, true) == 0) 
    539539            { 
    540540                SendErrorReply(ErrorReply.ERR_NOSUCHCHANNEL, "No such nick/channel"); 
     
    591591                , String.Format("{0} {1}-{2} {3} {4}", Environment.MachineName, t.FullName, t.Assembly.GetName().Version, "", "")); 
    592592 
    593             JoinMessage joinMsg = new JoinMessage(Server.ChannelName, ""); 
     593            JoinMessage joinMsg = new JoinMessage(_server.ChannelName, ""); 
    594594            PrivMsgMessage autoMsg = new PrivMsgMessage(); 
    595595            autoMsg.SenderNick = Server.ServerNick; 
    596596            autoMsg.SenderHost = "twitter@" + Server.ServerName; 
    597             autoMsg.Receiver = Server.ChannelName; 
     597            autoMsg.Receiver = _server.ChannelName; 
    598598            autoMsg.Content = "Twitter IRC Gateway Server Connected."; 
    599599 
     
    651651            { 
    652652                // チャンネル宛は自分のメッセージを書き換え 
    653                 if (message.Receiver == Server.ChannelName || message.Receiver.StartsWith("#")) 
     653                if ((String.Compare(message.Receiver, _server.ChannelName, true) == 0) || message.Receiver.StartsWith("#")) 
    654654                { 
    655655                    try 
     
    675675                    if (_server.SetTopicOnStatusChanged) 
    676676                    { 
    677                         TopicMessage topicMsg = new TopicMessage(Server.ChannelName, message.Content); 
     677                        TopicMessage topicMsg = new TopicMessage(_server.ChannelName, message.Content); 
    678678                        topicMsg.Sender = _clientHost; 
    679679                        Send(topicMsg); 
     
    877877                })); 
    878878 
    879                 SendNumericReply(NumericReply.RPL_NAMREPLY, "=", Server.ChannelName, String.Join(" ", _nickNames.ToArray())); 
    880                 SendNumericReply(NumericReply.RPL_ENDOFNAMES, Server.ChannelName, "End of NAMES list"); 
     879                SendNumericReply(NumericReply.RPL_NAMREPLY, "=", _server.ChannelName, String.Join(" ", _nickNames.ToArray())); 
     880                SendNumericReply(NumericReply.RPL_ENDOFNAMES, _server.ChannelName, "End of NAMES list"); 
    881881            } 
    882882            catch (WebException ex) 
     
    927927                    if (!_nickNames.Contains(screenName)) 
    928928                    { 
    929                         JoinMessage joinMsg = new JoinMessage(Server.ChannelName, ""); 
     929                        JoinMessage joinMsg = new JoinMessage(_server.ChannelName, ""); 
    930930                        joinMsg.SenderNick = screenName; 
    931931                        joinMsg.SenderHost = String.Format("{0}@{1}", "twitter", Server.ServerName); 
     
    938938                    if (!screenNames.Contains(screenName)) 
    939939                    { 
    940                         PartMessage partMsg = new PartMessage(Server.ChannelName, ""); 
     940                        PartMessage partMsg = new PartMessage(_server.ChannelName, ""); 
    941941                        partMsg.SenderNick = screenName; 
    942942                        partMsg.SenderHost = String.Format("{0}@{1}", "twitter", Server.ServerName); 
     
    10351035                            noticeMsg.SenderNick = status.User.ScreenName; 
    10361036                            noticeMsg.SenderHost = "twitter@" + Server.ServerName; 
    1037                             noticeMsg.Receiver = Server.ChannelName; 
     1037                            noticeMsg.Receiver = _server.ChannelName; 
    10381038                            noticeMsg.Content = String.Format("{0}: {1}", status.CreatedAt.ToString("HH:mm"), line); 
    10391039                            Send(noticeMsg); 
     
    10451045                            { 
    10461046                                case "NOTICE": 
    1047                                     msg = new NoticeMessage(Server.ChannelName, line); 
     1047                                    msg = new NoticeMessage(_server.ChannelName, line); 
    10481048                                    break; 
    10491049                                case "PRIVMSG": 
    10501050                                default: 
    1051                                     msg = new PrivMsgMessage(Server.ChannelName, line); 
     1051                                    msg = new PrivMsgMessage(_server.ChannelName, line); 
    10521052                                    break; 
    10531053                            } 
  • TwitterIrcGateway/TwitterIrcGatewayCore/TwitterIrcGatewayCore.csproj

    r60 r319  
    4848    <Compile Include="Server.cs" /> 
    4949    <Compile Include="Session.cs" /> 
    50     <Compile Include="TwitterService.cs" /> 
     50    <Compile Include="TwitterService.cs"> 
     51    </Compile> 
    5152    <Compile Include="Utility.cs" /> 
    5253  </ItemGroup> 
  • TwitterIrcGateway/TwitterIrcGatewayCore/TwitterService.cs

    r84 r319  
    2424        public static readonly String ServiceServerPrefix = "http://twitter.com"; 
    2525        public static readonly String Referer = "http://twitter.com/home"; 
    26         public static readonly String ClientUrl = "http://www.misuzilla.org/dist/net/twitterircgateway/client-info.xml"; 
     26        public static readonly String ClientUrl = "http://www.misuzilla.org/dist/net/twitterircgateway/"; 
    2727        public static readonly String ClientVersion = typeof(TwitterService).Assembly.GetName().Version.ToString(); 
    2828        public static readonly String ClientName = "TwitterIrcGateway"; 
     
    402402            url = TwitterService.ServiceServerPrefix + url; 
    403403        Retry: 
    404             using (CookieEnabledWebClient webClient = new CookieEnabledWebClient(_cookies)) 
    405             { 
    406                 try 
    407                 { 
    408                     System.Diagnostics.Trace.WriteLine(String.Format("GET(Cookie): {0}", url)); 
    409                     return webClient.DownloadString(url); 
    410                 } 
    411                 catch (WebException we) 
    412                 { 
    413                     HttpWebResponse wResponse = we.Response as HttpWebResponse; 
    414                     if (wResponse == null || wResponse.StatusCode != HttpStatusCode.Unauthorized || isRetry) 
    415                         throw; 
    416  
    417                     _cookies = Login(_userName, _credential.GetCredential(new Uri("http://twitter.com"), "Basic").Password); 
    418  
    419                     isRetry = true; 
    420                     goto Retry; 
    421                 } 
    422             } 
    423         } 
    424  
     404            try 
     405            { 
     406                System.Diagnostics.Trace.WriteLine(String.Format("GET(Cookie): {0}", url)); 
     407                return DownloadString(url); 
     408            } 
     409            catch (WebException we) 
     410            { 
     411                HttpWebResponse wResponse = we.Response as HttpWebResponse; 
     412                if (wResponse == null || wResponse.StatusCode != HttpStatusCode.Unauthorized || isRetry) 
     413                    throw; 
     414 
     415                _cookies = Login(_userName, _credential.GetCredential(new Uri("http://twitter.com"), "Basic").Password); 
     416 
     417                isRetry = true; 
     418                goto Retry; 
     419            } 
     420        } 
     421 
     422        private CookieCollection Login(String userNameOrEmail, String password) 
     423        { 
     424            System.Diagnostics.Trace.WriteLine(String.Format("Cookie Login: {0}", userNameOrEmail)); 
     425 
     426            HttpWebRequest request = CreateWebRequest("http://twitter.com/sessions") as HttpWebRequest; 
     427            request.AllowAutoRedirect = false; 
     428            request.Method = "POST"; 
     429            using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) 
     430            { 
     431                sw.Write("username_or_email={0}&password={1}&remember_me=1&commit=Sign%20In", userNameOrEmail, password); 
     432            } 
     433             
     434            HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
     435            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 
     436            { 
     437                String responseBody = sr.ReadToEnd(); 
     438 
     439                if (response.Cookies.Count == 0) 
     440                { 
     441                    throw new ApplicationException("���O�C���Ɏ��s���܂����B���[�U���܂��̓p�X���[�h���Ԉ��Ă����\�����������B"); 
     442                } 
     443 
     444                foreach (Cookie cookie in response.Cookies) 
     445                { 
     446                    cookie.Domain = "twitter.com"; 
     447                } 
     448                return response.Cookies; 
     449            } 
     450        } 
     451 
     452        WebRequest CreateWebRequest(String uri) 
     453        { 
     454            WebRequest request = WebRequest.Create(uri); 
     455            if (request is HttpWebRequest) 
     456            { 
     457                HttpWebRequest httpRequest = request as HttpWebRequest; 
     458                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
     459                httpRequest.Referer = Referer; 
     460                httpRequest.PreAuthenticate = false; 
     461                httpRequest.Accept = "*/*"; 
     462                httpRequest.CookieContainer = new CookieContainer(); 
     463                if (_cookies != null) 
     464                { 
     465                    httpRequest.CookieContainer.Add(_cookies); 
     466                } 
     467            } 
     468            return request; 
     469        } 
     470            
     471#if FALSE 
    425472        private CookieCollection Login(String userNameOrEmail, String password) 
    426473        { 
     
    447494            } 
    448495        } 
    449  
    450496        class CookieEnabledWebClient : WebClient 
    451497        { 
     
    501547                return response; 
    502548            } 
     549        } 
     550#endif 
     551         
     552        String DownloadString(String url) 
     553        { 
     554            WebRequest request = CreateWebRequest(url); 
     555            WebResponse response = null; 
     556            try 
     557            { 
     558                response = request.GetResponse(); 
     559                using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
     560                { 
     561                    return sr.ReadToEnd(); 
     562                } 
     563            } 
     564            finally 
     565            { 
     566                if (response != null) 
     567                { 
     568                    response.Close(); 
     569                } 
     570            } 
     571#if FALSE 
     572            using (CookieEnabledWebClient webClient = new CookieEnabledWebClient(_cookies)) 
     573            { 
     574                return webClient.DownloadString(url); 
     575            } 
     576#endif 
    503577        } 
    504578        #endregion