Changeset 405

Show
Ignore:
Timestamp:
08/31/08 02:56:36 (3 months ago)
Author:
tomoyo
Message:

tig.rbを参考にTypableMapでfavれるようにした。

Location:
TwitterIrcGateway/trunk/TwitterIrcGatewayCore
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • TwitterIrcGateway/trunk/TwitterIrcGatewayCore/Config.cs

    r399 r405  
    1616        public String IMUserName { get; set; } 
    1717        public String IMEncryptoPassword { get; set; } 
     18 
     19        public Boolean EnableTypableMap { get; set; } 
     20        public Int32 TypableMapKeyColorNumber { get; set; } 
    1821         
    1922        public String GetIMPassword(String key) 
     
    3639            } 
    3740            IMEncryptoPassword = Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString())); 
     41        } 
     42         
     43        public Config() 
     44        { 
     45            EnableTypableMap = false; 
     46            TypableMapKeyColorNumber = 14; 
    3847        } 
    3948 
  • TwitterIrcGateway/trunk/TwitterIrcGatewayCore/Session.cs

    r399 r405  
    11using System; 
    22using System.Collections.Generic; 
     3using System.ComponentModel; 
    34using System.Diagnostics; 
    45using System.Globalization; 
     
    1314using System.Xml; 
    1415 
     16using TypableMap; 
    1517using Misuzilla.Net.Irc; 
    1618using Misuzilla.Applications.TwitterIrcGateway.Filter; 
     
    3234        private Filters _filter; 
    3335        private Config _config; 
     36        private TypableMap<Int32> _typableMap; 
    3437 
    3538        private List<String> _nickNames = new List<string>(); 
     
    6770            MessageReceived += new EventHandler<MessageReceivedEventArgs>(MessageReceived_TIGIMENABLE); 
    6871            MessageReceived += new EventHandler<MessageReceivedEventArgs>(MessageReceived_TIGIMDISABLE); 
     72            MessageReceived += new EventHandler<MessageReceivedEventArgs>(MessageReceived_TIGCONFIG); 
    6973 
    7074            _groups = new Groups(); 
    7175            _filter = new Filters(); 
    7276            _config = new Config(); 
     77            _typableMap = new TypableMap<Int32>(); 
    7378 
    7479            _server = server; 
     
    611616            PrivMsgMessage message = e.Message as PrivMsgMessage; 
    612617            if (message == null) return; 
     618 
     619            // fav コマンド? 
     620            if (_config.EnableTypableMap) 
     621            { 
     622                Match m = Regex.Match(message.Content, @"^\s*(unfav|fav)\s+([a-zA-Z0-9_]+)\s*$", RegexOptions.IgnoreCase); 
     623                if (m.Success) 
     624                { 
     625                    Int32 id; 
     626                    if (_typableMap.TryGetValue(m.Groups[2].Value, out id)) 
     627                    { 
     628                        // TypableMap にある 
     629                        Boolean isUnfav = (String.Compare(m.Groups[1].Value, "unfav", true) == 0); 
     630                        Status favStatus = (isUnfav ? _twitter.DestroyFavorite(id) : _twitter.CreateFavorite(id)); 
     631                        Send(new NoticeMessage 
     632                                 { 
     633                                     Sender = _clientHost, 
     634                                     Receiver = message.Receiver, 
     635                                     Content = 
     636                                         String.Format("ユーザ {0} のステータス \"{1}\"をFavorites{2}しました。", 
     637                                                       favStatus.User.ScreenName, favStatus.Text, 
     638                                                       (isUnfav ? "から削除" : "に追加")) 
     639                                 }); 
     640                    } 
     641                    return; 
     642                } 
     643            } 
    613644 
    614645            Boolean isRetry = false; 
     
    879910            DisconnectToIMService(false); 
    880911        } 
    881          
     912 
     913        void MessageReceived_TIGCONFIG(object sender, MessageReceivedEventArgs e) 
     914        { 
     915            if (String.Compare(e.Message.Command, "TIGCONFIG", true) != 0) return; 
     916 
     917            Type t = typeof(Config); 
     918             
     919            // プロパティ一覧を作る 
     920            if (String.IsNullOrEmpty(e.Message.CommandParams[0])) 
     921            { 
     922                //SendTwitterGatewayServerMessage("TIGCONFIG コマンドは1つまたは2つの引数(ConfigName, Value)が必要です。"); 
     923                foreach (var pi in t.GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.SetProperty)) 
     924                { 
     925                    SendTwitterGatewayServerMessage( 
     926                        String.Format("{0} ({1}) = {2}", pi.Name, pi.PropertyType.FullName, pi.GetValue(_config, null))); 
     927                } 
     928                return; 
     929            } 
     930             
     931            // プロパティを探す 
     932            String propName = e.Message.CommandParams[0]; 
     933            PropertyInfo propInfo = t.GetProperty(propName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.SetProperty); 
     934            if (propInfo == null) 
     935            { 
     936                SendTwitterGatewayServerMessage(String.Format("設定項目 \"{0}\" は存在しません。", propName)); 
     937                return; 
     938            } 
     939 
     940            // 2つめの引数があるときは値を設定する。 
     941            if (!String.IsNullOrEmpty(e.Message.CommandParams[1])) 
     942            { 
     943                TypeConverter tConv = TypeDescriptor.GetConverter(propInfo.PropertyType); 
     944                if (!tConv.CanConvertFrom(typeof (String))) 
     945                { 
     946                    SendTwitterGatewayServerMessage( 
     947                        String.Format("設定項目 \"{0}\" の型 \"{1}\" には適切な TypeConverter がないため、このコマンドで設定することはできません。", propName, 
     948                                      propInfo.PropertyType.FullName)); 
     949                    return; 
     950                } 
     951 
     952                try 
     953                { 
     954                    Object value = tConv.ConvertFromString(e.Message.CommandParams[1]); 
     955                    propInfo.SetValue(_config, value, null); 
     956                } 
     957                catch (Exception ex) 
     958                { 
     959                    SendTwitterGatewayServerMessage(String.Format( 
     960                                                        "設定項目 \"{0}\" の型 \"{1}\" に値を変換し設定する際にエラーが発生しました({2})。", propName, 
     961                                                        propInfo.PropertyType.FullName, ex.GetType().Name)); 
     962                    foreach (var line in ex.Message.Split('\n')) 
     963                        SendTwitterGatewayServerMessage(line); 
     964                } 
     965 
     966                SaveConfig(); 
     967            } 
     968             
     969            SendTwitterGatewayServerMessage( 
     970                String.Format("{0} ({1}) = {2}", propName, propInfo.PropertyType.FullName, propInfo.GetValue(_config, null))); 
     971             
     972        } 
     973  
    882974        private void ConnectToIMService(Boolean initialConnect) 
    883975        { 
     
    10541146        /// サーバメッセージ系 
    10551147        /// </summary> 
    1056         /// <param name="msg"></param> 
     1148        /// <param name="message"></param> 
    10571149        public void SendTwitterGatewayServerMessage(String message) 
    10581150        { 
     
    11121204            { 
    11131205                User[] friends = _twitter.GetFriends(); 
    1114                 _nickNames = new List<string>(Array.ConvertAll<User, String>(friends, delegate(User u) 
    1115                 { 
    1116                     return u.ScreenName; 
    1117                 })); 
     1206                _nickNames = new List<string>(Array.ConvertAll<User, String>(friends, u => u.ScreenName)); 
    11181207 
    11191208                SendNumericReply(NumericReply.RPL_NAMREPLY, "=", _server.ChannelName, String.Join(" ", _nickNames.ToArray())); 
     
    11441233            { 
    11451234                User[] friends = _twitter.GetFriends(); 
    1146                 List<String> screenNames = new List<string>(Array.ConvertAll<User, String>(friends, delegate(User u) 
    1147                 { 
    1148                     return u.ScreenName; 
    1149                 })); 
     1235                List<String> screenNames = new List<string>(Array.ConvertAll<User, String>(friends, u => u.ScreenName)); 
    11501236 
    11511237                // てきとうに。 
     
    12071293            String text = (_server.ResolveTinyUrl) ? Utility.ResolveTinyUrlInMessage(filterArgs.Content) : filterArgs.Content; 
    12081294 
     1295            // TypableMap 
     1296            if (_config.EnableTypableMap) 
     1297            { 
     1298                String typableMapId = _typableMap.Add(status.Id); 
     1299                text = String.Format("{0} \x0003{1}({2})", text, _config.TypableMapKeyColorNumber, typableMapId); 
     1300            } 
     1301             
    12091302            String[] lines = text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); 
    12101303            foreach (String line in lines) 
  • TwitterIrcGateway/trunk/TwitterIrcGatewayCore/TwitterIrcGatewayCore.csproj

    r399 r405  
    33    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    44    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
    5     <ProductVersion>9.0.21022</ProductVersion> 
     5    <ProductVersion>9.0.30729</ProductVersion> 
    66    <SchemaVersion>2.0</SchemaVersion> 
    77    <ProjectGuid>{8A256703-BDC7-4E96-8AC3-89A56A2AFB86}</ProjectGuid> 
     
    3333    <WarningLevel>4</WarningLevel> 
    3434    <GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies> 
     35    <DocumentationFile> 
     36    </DocumentationFile> 
    3537  </PropertyGroup> 
    3638  <ItemGroup> 
     
    5961    <Compile Include="TwitterService.cs"> 
    6062    </Compile> 
     63    <Compile Include="TypableMap.cs" /> 
    6164    <Compile Include="Utility.cs" /> 
    6265  </ItemGroup> 
  • TwitterIrcGateway/trunk/TwitterIrcGatewayCore/TwitterService.cs

    r399 r405  
    340340 
    341341                return directMessages; 
     342            }); 
     343        } 
     344 
     345        /// <summary> 
     346        /// ���b�Z�[�W��orites�ɒlj���܂��B 
     347        /// </summary> 
     348        /// <param name="id"></param> 
     349        /// <returns></returns> 
     350        public Status CreateFavorite(Int32 id) 
     351        { 
     352            return ExecuteRequest<Status>(() => 
     353            { 
     354                String responseBody = POST(String.Format("/favorites/create/{0}.xml", id), new byte[0]); 
     355                Status status; 
     356                if (NilClasses.CanDeserialize(responseBody)) 
     357                { 
     358                    return null; 
     359                } 
     360                else 
     361                { 
     362                    status = Status.Serializer.Deserialize(new StringReader(responseBody)) as Status; 
     363                    return status; 
     364                } 
     365            }); 
     366        } 
     367 
     368        /// <summary> 
     369        /// ���b�Z�[�W��orites���������܂��B 
     370        /// </summary> 
     371        /// <param name="id"></param> 
     372        /// <returns></returns> 
     373        public Status DestroyFavorite(Int32 id) 
     374        { 
     375            return ExecuteRequest<Status>(() => 
     376            { 
     377                String responseBody = POST(String.Format("/favorites/destroy/{0}.xml", id), new byte[0]); 
     378                Status status; 
     379                if (NilClasses.CanDeserialize(responseBody)) 
     380                { 
     381                    return null; 
     382                } 
     383                else 
     384                { 
     385                    status = Status.Serializer.Deserialize(new StringReader(responseBody)) as Status; 
     386                    return status; 
     387                } 
    342388            }); 
    343389        }