OSDN Git Service

プロフィール画面へのD&Dによるアイコン変更時に確認ダイアログを表示するように変更
[opentween/open-tween.git] / OpenTween / Twitter.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2013      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System.Diagnostics;
29 using System.IO;
30 using System.Linq;
31 using System.Net;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.Serialization;
34 using System.Runtime.Serialization.Json;
35 using System.Text;
36 using System.Text.RegularExpressions;
37 using System.Threading;
38 using System.Threading.Tasks;
39 using System.Web;
40 using System.Xml;
41 using System.Xml.Linq;
42 using System;
43 using System.Reflection;
44 using System.Collections.Generic;
45 using System.Drawing;
46 using System.Windows.Forms;
47 using OpenTween.Api;
48 using OpenTween.Connection;
49
50 namespace OpenTween
51 {
52     public class Twitter : IDisposable
53     {
54         #region Regexp from twitter-text-js
55
56         // The code in this region code block incorporates works covered by
57         // the following copyright and permission notices:
58         //
59         //   Copyright 2011 Twitter, Inc.
60         //
61         //   Licensed under the Apache License, Version 2.0 (the "License"); you
62         //   may not use this work except in compliance with the License. You
63         //   may obtain a copy of the License in the LICENSE file, or at:
64         //
65         //   http://www.apache.org/licenses/LICENSE-2.0
66         //
67         //   Unless required by applicable law or agreed to in writing, software
68         //   distributed under the License is distributed on an "AS IS" BASIS,
69         //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
70         //   implied. See the License for the specific language governing
71         //   permissions and limitations under the License.
72
73         //Hashtag用正規表現
74         private const string LATIN_ACCENTS = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
75         private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
76         //private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u3096\u3400-\u4DBF\u4E00-\u9FFF\u20000-\u2A6DF\u2A700-\u2B73F\u2B740-\u2B81F\u2F800-\u2FA1F";
77         private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
78         private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
79         private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
80         private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
81         private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
82         public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
83         //URL正規表現
84         private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
85         public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
86         private const string url_invalid_domain_chars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
87         private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
88         private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
89         private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
90         private const string url_valid_GTLD = @"(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)(?=[^0-9a-zA-Z]|$))";
91         private const string url_valid_CCTLD = @"(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^0-9a-zA-Z]|$))";
92         private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
93         private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
94         public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
95         public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
96         private const string url_valid_port_number = @"[0-9]+";
97
98         private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
99         private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
100         private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
101         private const string pth = "(?:" +
102             "(?:" +
103                 url_valid_general_path_chars + "*" +
104                 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
105                 url_valid_path_ending_chars +
106                 ")|(?:@" + url_valid_general_path_chars + "+/)" +
107             ")";
108         private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
109         public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
110                                     "(?<url>(?<protocol>https?://)?" +
111                                     "(?<domain>" + url_valid_domain + ")" +
112                                     "(?::" + url_valid_port_number + ")?" +
113                                     "(?<path>/" + pth + "*)?" +
114                                     qry +
115                                     ")";
116
117         #endregion
118
119         /// <summary>
120         /// Twitter API のステータスページのURL
121         /// </summary>
122         public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
123
124         /// <summary>
125         /// ツイートへのパーマリンクURLを判定する正規表現
126         /// </summary>
127         public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
128
129         /// <summary>
130         /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
131         /// </summary>
132         public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
133   favstar\.fm/users/[a-zA-Z0-9_]+/status/       # Favstar
134 | favstar\.fm/t/                                # Favstar (short)
135 | aclog\.koba789\.com/i/                        # aclog
136 | frtrt\.net/solo_status\.php\?status=          # RtRT
137 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
138
139         /// <summary>
140         /// DM送信かどうかを判定する正規表現
141         /// </summary>
142         public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
143
144         public TwitterConfiguration Configuration { get; private set; }
145
146         delegate void GetIconImageDelegate(PostClass post);
147         private readonly object LockObj = new object();
148         private List<long> followerId = new List<long>();
149         private bool _GetFollowerResult = false;
150         private long[] noRTId = new long[0];
151         private bool _GetNoRetweetResult = false;
152
153         private int _followersCount = 0;
154         private int _friendsCount = 0;
155         private int _statusesCount = 0;
156         private string _location = "";
157         private string _bio = "";
158
159         //プロパティからアクセスされる共通情報
160         private string _uname;
161
162         private bool _restrictFavCheck;
163
164         private bool _readOwnPost;
165         private List<string> _hashList = new List<string>();
166
167         //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
168         private long minHomeTimeline = long.MaxValue;
169         private long minMentions = long.MaxValue;
170         private long minDirectmessage = long.MaxValue;
171         private long minDirectmessageSent = long.MaxValue;
172
173         //private FavoriteQueue favQueue;
174
175         private HttpTwitter twCon = new HttpTwitter();
176
177         //private List<PostClass> _deletemessages = new List<PostClass>();
178
179         public Twitter()
180         {
181             this.Configuration = TwitterConfiguration.DefaultConfiguration();
182         }
183
184         public TwitterApiAccessLevel AccessLevel
185         {
186             get
187             {
188                 return MyCommon.TwitterApiInfo.AccessLevel;
189             }
190         }
191
192         protected void ResetApiStatus()
193         {
194             MyCommon.TwitterApiInfo.Reset();
195         }
196
197         public string Authenticate(string username, string password)
198         {
199             this.ResetApiStatus();
200
201             HttpStatusCode res;
202             var content = "";
203             try
204             {
205                 res = twCon.AuthUserAndPass(username, password, ref content);
206             }
207             catch(Exception ex)
208             {
209                 return "Err:" + ex.Message;
210             }
211
212             var err = this.CheckStatusCode(res, content);
213             if (err != null) return err;
214
215             _uname = username.ToLower();
216             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
217             return "";
218         }
219
220         public string StartAuthentication(ref string pinPageUrl)
221         {
222             //OAuth PIN Flow
223             this.ResetApiStatus();
224             try
225             {
226                 var res = twCon.AuthGetRequestToken(ref pinPageUrl);
227                 if (!res)
228                     return "Err:Failed to access auth server.";
229             }
230             catch(Exception)
231             {
232                 return "Err:" + "Failed to access auth server.";
233             }
234
235             return "";
236         }
237
238         public string Authenticate(string pinCode)
239         {
240             this.ResetApiStatus();
241
242             HttpStatusCode res;
243             try
244             {
245                 res = twCon.AuthGetAccessToken(pinCode);
246             }
247             catch(Exception)
248             {
249                 return "Err:" + "Failed to access auth acc server.";
250             }
251
252             var err = this.CheckStatusCode(res, null);
253             if (err != null) return err;
254
255             _uname = Username.ToLower();
256             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
257             return "";
258         }
259
260         public void ClearAuthInfo()
261         {
262             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
263             this.ResetApiStatus();
264             twCon.ClearAuthInfo();
265         }
266
267         public void VerifyCredentials()
268         {
269             HttpStatusCode res;
270             var content = "";
271             try
272             {
273                 res = twCon.VerifyCredentials(ref content);
274             }
275             catch(Exception)
276             {
277                 return;
278             }
279
280             if (res == HttpStatusCode.OK)
281             {
282                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
283                 TwitterUser user;
284                 try
285                 {
286                     user = TwitterUser.ParseJson(content);
287                 }
288                 catch(SerializationException)
289                 {
290                     return;
291                 }
292                 twCon.AuthenticatedUserId = user.Id;
293             }
294         }
295
296         public void Initialize(string token, string tokenSecret, string username, long userId)
297         {
298             //OAuth認証
299             if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
300             {
301                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
302             }
303             this.ResetApiStatus();
304             twCon.Initialize(token, tokenSecret, username, userId);
305             _uname = username.ToLower();
306             if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
307         }
308
309         public string PreProcessUrl(string orgData)
310         {
311             int posl1;
312             var posl2 = 0;
313             //var IDNConveter = new IdnMapping();
314             var href = "<a href=\"";
315
316             while (true)
317             {
318                 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
319                 {
320                     var urlStr = "";
321                     // IDN展開
322                     posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
323                     posl1 += href.Length;
324                     posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
325                     urlStr = orgData.Substring(posl1, posl2 - posl1);
326
327                     if (!urlStr.StartsWith("http://") && !urlStr.StartsWith("https://") && !urlStr.StartsWith("ftp://"))
328                     {
329                         continue;
330                     }
331
332                     var replacedUrl = MyCommon.IDNEncode(urlStr);
333                     if (replacedUrl == null) continue;
334                     if (replacedUrl == urlStr) continue;
335
336                     orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
337                     posl2 = 0;
338                 }
339                 else
340                 {
341                     break;
342                 }
343             }
344             return orgData;
345         }
346
347         private string GetPlainText(string orgData)
348         {
349             return WebUtility.HtmlDecode(Regex.Replace(orgData, "(?<tagStart><a [^>]+>)(?<text>[^<]+)(?<tagEnd></a>)", "${text}"));
350         }
351
352         // htmlの簡易サニタイズ(詳細表示に不要なタグの除去)
353
354         private string SanitizeHtml(string orgdata)
355         {
356             var retdata = orgdata;
357
358             retdata = Regex.Replace(retdata, "<(script|object|applet|image|frameset|fieldset|legend|style).*" +
359                 "</(script|object|applet|image|frameset|fieldset|legend|style)>", "", RegexOptions.IgnoreCase);
360
361             retdata = Regex.Replace(retdata, "<(frame|link|iframe|img)>", "", RegexOptions.IgnoreCase);
362
363             return retdata;
364         }
365
366         private string AdjustHtml(string orgData)
367         {
368             var retStr = orgData;
369             //var m = Regex.Match(retStr, "<a [^>]+>[#|#](?<1>[a-zA-Z0-9_]+)</a>");
370             //while (m.Success)
371             //{
372             //    lock (LockObj)
373             //    {
374             //        _hashList.Add("#" + m.Groups(1).Value);
375             //    }
376             //    m = m.NextMatch;
377             //}
378             retStr = Regex.Replace(retStr, "<a [^>]*href=\"/", "<a href=\"https://twitter.com/");
379             retStr = retStr.Replace("<a href=", "<a target=\"_self\" href=");
380             retStr = Regex.Replace(retStr, @"(\r\n?|\n)", "<br>"); // CRLF, CR, LF は全て <br> に置換する
381
382             //半角スペースを置換(Thanks @anis774)
383             var ret = false;
384             do
385             {
386                 ret = EscapeSpace(ref retStr);
387             } while (!ret);
388
389             return SanitizeHtml(retStr);
390         }
391
392         private bool EscapeSpace(ref string html)
393         {
394             //半角スペースを置換(Thanks @anis774)
395             var isTag = false;
396             for (int i = 0; i < html.Length; i++)
397             {
398                 if (html[i] == '<')
399                 {
400                     isTag = true;
401                 }
402                 if (html[i] == '>')
403                 {
404                     isTag = false;
405                 }
406
407                 if ((!isTag) && (html[i] == ' '))
408                 {
409                     html = html.Remove(i, 1);
410                     html = html.Insert(i, "&nbsp;");
411                     return false;
412                 }
413             }
414             return true;
415         }
416
417         private struct PostInfo
418         {
419             public string CreatedAt;
420             public string Id;
421             public string Text;
422             public string UserId;
423             public PostInfo(string Created, string IdStr, string txt, string uid)
424             {
425                 CreatedAt = Created;
426                 Id = IdStr;
427                 Text = txt;
428                 UserId = uid;
429             }
430             public bool Equals(PostInfo dst)
431             {
432                 if (this.CreatedAt == dst.CreatedAt && this.Id == dst.Id && this.Text == dst.Text && this.UserId == dst.UserId)
433                 {
434                     return true;
435                 }
436                 else
437                 {
438                     return false;
439                 }
440             }
441         }
442
443         static private PostInfo _prev = new PostInfo("", "", "", "");
444         private bool IsPostRestricted(TwitterStatus status)
445         {
446             var _current = new PostInfo("", "", "", "");
447
448             _current.CreatedAt = status.CreatedAt;
449             _current.Id = status.IdStr;
450             if (status.Text == null)
451             {
452                 _current.Text = "";
453             }
454             else
455             {
456                 _current.Text = status.Text;
457             }
458             _current.UserId = status.User.IdStr;
459
460             if (_current.Equals(_prev))
461             {
462                 return true;
463             }
464             _prev.CreatedAt = _current.CreatedAt;
465             _prev.Id = _current.Id;
466             _prev.Text = _current.Text;
467             _prev.UserId = _current.UserId;
468
469             return false;
470         }
471
472         public string PostStatus(string postStr, long? reply_to, List<long> mediaIds = null)
473         {
474             if (MyCommon._endingFlag) return "";
475
476             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
477
478             if (mediaIds == null &&
479                 Twitter.DMSendTextRegex.IsMatch(postStr))
480             {
481                 return SendDirectMessage(postStr);
482             }
483
484             HttpStatusCode res;
485             var content = "";
486             try
487             {
488                 res = twCon.UpdateStatus(postStr, reply_to, mediaIds, ref content);
489             }
490             catch(Exception ex)
491             {
492                 return "Err:" + ex.Message;
493             }
494
495             // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
496             if (res == HttpStatusCode.NotFound) return "";
497
498             var err = this.CheckStatusCode(res, content);
499             if (err != null) return err;
500
501             TwitterStatus status;
502             try
503             {
504                 status = TwitterStatus.ParseJson(content);
505             }
506             catch(SerializationException ex)
507             {
508                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
509                 return "Err:Json Parse Error(DataContractJsonSerializer)";
510             }
511             catch(Exception ex)
512             {
513                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
514                 return "Err:Invalid Json!";
515             }
516             _followersCount = status.User.FollowersCount;
517             _friendsCount = status.User.FriendsCount;
518             _statusesCount = status.User.StatusesCount;
519             _location = status.User.Location;
520             _bio = status.User.Description;
521
522             if (IsPostRestricted(status))
523             {
524                 return "OK:Delaying?";
525             }
526             return "";
527         }
528
529         public string PostStatusWithMedia(string postStr, long? reply_to, FileInfo mediaFile)
530         {
531             if (MyCommon._endingFlag) return "";
532
533             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
534
535             HttpStatusCode res;
536             var content = "";
537             try
538             {
539                 res = twCon.UpdateStatusWithMedia(postStr, reply_to, mediaFile, ref content);
540             }
541             catch(Exception ex)
542             {
543                 return "Err:" + ex.Message;
544             }
545
546             // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
547             if (res == HttpStatusCode.NotFound) return "";
548
549             var err = this.CheckStatusCode(res, content);
550             if (err != null) return err;
551
552             TwitterStatus status;
553             try
554             {
555                 status = TwitterStatus.ParseJson(content);
556             }
557             catch(SerializationException ex)
558             {
559                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
560                 return "Err:Json Parse Error(DataContractJsonSerializer)";
561             }
562             catch(Exception ex)
563             {
564                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
565                 return "Err:Invalid Json!";
566             }
567             _followersCount = status.User.FollowersCount;
568             _friendsCount = status.User.FriendsCount;
569             _statusesCount = status.User.StatusesCount;
570             _location = status.User.Location;
571             _bio = status.User.Description;
572
573             if (IsPostRestricted(status))
574             {
575                 return "OK:Delaying?";
576             }
577             return "";
578         }
579
580         public string PostStatusWithMultipleMedia(string postStr, long? reply_to, List<FileInfo> mediaFiles)
581         {
582             if (MyCommon._endingFlag) return "";
583
584             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
585
586             if (Twitter.DMSendTextRegex.IsMatch(postStr))
587             {
588                 return SendDirectMessage(postStr);
589             }
590
591             var mediaIds = new List<long>();
592
593             foreach (var mediaFile in mediaFiles)
594             {
595                 long? mediaId = null;
596                 var err = UploadMedia(mediaFile, ref mediaId);
597                 if (!mediaId.HasValue || !string.IsNullOrEmpty(err)) return err;
598                 mediaIds.Add(mediaId.Value);
599             }
600
601             if (mediaIds.Count == 0)
602                 return "Err:Invalid Files!";
603
604             return PostStatus(postStr, reply_to, mediaIds);
605         }
606
607         public string UploadMedia(FileInfo mediaFile, ref long? mediaId)
608         {
609             if (MyCommon._endingFlag) return "";
610
611             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
612
613             HttpStatusCode res;
614             var content = "";
615             try
616             {
617                 res = twCon.UploadMedia(mediaFile, ref content);
618             }
619             catch (Exception ex)
620             {
621                 return "Err:" + ex.Message;
622             }
623
624             var err = this.CheckStatusCode(res, content);
625             if (err != null) return err;
626
627             TwitterUploadMediaResult status;
628             try
629             {
630                 status = TwitterUploadMediaResult.ParseJson(content);
631             }
632             catch (SerializationException ex)
633             {
634                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
635                 return "Err:Json Parse Error(DataContractJsonSerializer)";
636             }
637             catch (Exception ex)
638             {
639                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
640                 return "Err:Invalid Json!";
641             }
642
643             mediaId = status.MediaId;
644             return "";
645         }
646
647         public string SendDirectMessage(string postStr)
648         {
649             if (MyCommon._endingFlag) return "";
650
651             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
652
653             if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
654             {
655                 return "Auth Err:try to re-authorization.";
656             }
657
658             var mc = Twitter.DMSendTextRegex.Match(postStr);
659
660             HttpStatusCode res;
661             var content = "";
662             try
663             {
664                 res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content);
665             }
666             catch(Exception ex)
667             {
668                 return "Err:" + ex.Message;
669             }
670
671             var err = this.CheckStatusCode(res, content);
672             if (err != null) return err;
673
674             TwitterDirectMessage status;
675             try
676             {
677                 status = TwitterDirectMessage.ParseJson(content);
678             }
679             catch(SerializationException ex)
680             {
681                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
682                 return "Err:Json Parse Error(DataContractJsonSerializer)";
683             }
684             catch(Exception ex)
685             {
686                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
687                 return "Err:Invalid Json!";
688             }
689             _followersCount = status.Sender.FollowersCount;
690             _friendsCount = status.Sender.FriendsCount;
691             _statusesCount = status.Sender.StatusesCount;
692             _location = status.Sender.Location;
693             _bio = status.Sender.Description;
694
695             return "";
696         }
697
698         public string RemoveStatus(long id)
699         {
700             if (MyCommon._endingFlag) return "";
701
702             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
703
704             HttpStatusCode res;
705             try
706             {
707                 res = twCon.DestroyStatus(id);
708             }
709             catch(Exception ex)
710             {
711                 return "Err:" + ex.Message;
712             }
713
714             return this.CheckStatusCode(res, null) ?? "";
715         }
716
717         public string PostRetweet(long id, bool read)
718         {
719             if (MyCommon._endingFlag) return "";
720             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
721
722             //データ部分の生成
723             var target = id;
724             var post = TabInformations.GetInstance()[id];
725             if (post == null)
726             {
727                 return "Err:Target isn't found.";
728             }
729             if (TabInformations.GetInstance()[id].RetweetedId != null)
730             {
731                 target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT
732             }
733
734             HttpStatusCode res;
735             var content = "";
736             try
737             {
738                 res = twCon.RetweetStatus(target, ref content);
739             }
740             catch(Exception ex)
741             {
742                 return "Err:" + ex.Message;
743             }
744
745             var err = this.CheckStatusCode(res, content);
746             if (err != null) return err;
747
748             TwitterStatus status;
749             try
750             {
751                 status = TwitterStatus.ParseJson(content);
752             }
753             catch(SerializationException ex)
754             {
755                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
756                 return "Err:Json Parse Error(DataContractJsonSerializer)";
757             }
758             catch(Exception ex)
759             {
760                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
761                 return "Err:Invalid Json!";
762             }
763
764             //ReTweetしたものをTLに追加
765             post = CreatePostsFromStatusData(status);
766             if (post == null) return "Invalid Json!";
767
768             //二重取得回避
769             lock (LockObj)
770             {
771                 if (TabInformations.GetInstance().ContainsKey(post.StatusId)) return "";
772             }
773             //Retweet判定
774             if (post.RetweetedId == null) return "Invalid Json!";
775             //ユーザー情報
776             post.IsMe = true;
777
778             post.IsRead = read;
779             post.IsOwl = false;
780             if (_readOwnPost) post.IsRead = true;
781             post.IsDm = false;
782
783             TabInformations.GetInstance().AddPost(post);
784
785             return "";
786         }
787
788         public string RemoveDirectMessage(long id, PostClass post)
789         {
790             if (MyCommon._endingFlag) return "";
791
792             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
793
794             if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
795             {
796                 return "Auth Err:try to re-authorization.";
797             }
798
799             //if (post.IsMe)
800             //    _deletemessages.Add(post)
801             //}
802
803             HttpStatusCode res;
804             try
805             {
806                 res = twCon.DestroyDirectMessage(id);
807             }
808             catch(Exception ex)
809             {
810                 return "Err:" + ex.Message;
811             }
812
813             return this.CheckStatusCode(res, null) ?? "";
814         }
815
816         public string PostFollowCommand(string screenName)
817         {
818             if (MyCommon._endingFlag) return "";
819
820             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
821
822             HttpStatusCode res;
823             var content = "";
824             try
825             {
826                 res = twCon.CreateFriendships(screenName, ref content);
827             }
828             catch(Exception ex)
829             {
830                 return "Err:" + ex.Message;
831             }
832
833             return this.CheckStatusCode(res, content) ?? "";
834         }
835
836         public string PostRemoveCommand(string screenName)
837         {
838             if (MyCommon._endingFlag) return "";
839
840             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
841
842             HttpStatusCode res;
843             var content = "";
844             try
845             {
846                 res = twCon.DestroyFriendships(screenName, ref content);
847             }
848             catch(Exception ex)
849             {
850                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
851             }
852
853             return this.CheckStatusCode(res, content) ?? "";
854         }
855
856         public string PostCreateBlock(string screenName)
857         {
858             if (MyCommon._endingFlag) return "";
859
860             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
861
862             HttpStatusCode res;
863             var content = "";
864             try
865             {
866                 res = twCon.CreateBlock(screenName, ref content);
867             }
868             catch(Exception ex)
869             {
870                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
871             }
872
873             return this.CheckStatusCode(res, content) ?? "";
874         }
875
876         public string PostDestroyBlock(string screenName)
877         {
878             if (MyCommon._endingFlag) return "";
879
880             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
881
882             HttpStatusCode res;
883             var content = "";
884             try
885             {
886                 res = twCon.DestroyBlock(screenName, ref content);
887             }
888             catch(Exception ex)
889             {
890                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
891             }
892
893             return this.CheckStatusCode(res, content) ?? "";
894         }
895
896         public string PostReportSpam(string screenName)
897         {
898             if (MyCommon._endingFlag) return "";
899
900             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
901
902             HttpStatusCode res;
903             var content = "";
904             try
905             {
906                 res = twCon.ReportSpam(screenName, ref content);
907             }
908             catch(Exception ex)
909             {
910                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
911             }
912
913             return this.CheckStatusCode(res, content) ?? "";
914         }
915
916         public string GetFriendshipInfo(string screenName, ref bool isFollowing, ref bool isFollowed)
917         {
918             if (MyCommon._endingFlag) return "";
919
920             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
921
922             HttpStatusCode res;
923             var content = "";
924             try
925             {
926                 res = twCon.ShowFriendships(_uname, screenName, ref content);
927             }
928             catch(Exception ex)
929             {
930                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
931             }
932
933             var err = this.CheckStatusCode(res, content);
934             if (err != null) return err;
935
936             try
937             {
938                 var friendship = TwitterFriendship.ParseJson(content);
939                 isFollowing = friendship.Relationship.Source.Following;
940                 isFollowed = friendship.Relationship.Source.FollowedBy;
941                 return "";
942             }
943             catch(SerializationException ex)
944             {
945                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
946                 return "Err:Json Parse Error(DataContractJsonSerializer)";
947             }
948             catch(Exception ex)
949             {
950                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
951                 return "Err:Invalid Json!";
952             }
953         }
954
955         public string GetUserInfo(string screenName, ref TwitterUser user)
956         {
957             if (MyCommon._endingFlag) return "";
958
959             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
960
961             user = null;
962
963             HttpStatusCode res;
964             var content = "";
965             try
966             {
967                 res = twCon.ShowUserInfo(screenName, ref content);
968             }
969             catch(Exception ex)
970             {
971                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
972             }
973
974             var err = this.CheckStatusCode(res, content);
975             if (err != null) return err;
976
977             try
978             {
979                 user = TwitterUser.ParseJson(content);
980             }
981             catch (SerializationException ex)
982             {
983                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
984                 return "Err:Json Parse Error(DataContractJsonSerializer)";
985             }
986             catch (Exception ex)
987             {
988                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
989                 return "Err:Invalid Json!";
990             }
991             return "";
992         }
993
994         public string GetStatus_Retweeted_Count(long StatusId, ref int retweeted_count)
995         {
996             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
997
998             if (MyCommon._endingFlag) return "";
999
1000             HttpStatusCode res;
1001             var content = "";
1002             try
1003             {
1004                 res = twCon.ShowStatuses(StatusId, ref content);
1005             }
1006             catch (Exception ex)
1007             {
1008                 return "Err:" + ex.Message;
1009             }
1010
1011             var err = this.CheckStatusCode(res, content);
1012             if (err != null) return err;
1013
1014             TwitterStatus status;
1015             try
1016             {
1017                 status = TwitterStatus.ParseJson(content);
1018             }
1019             catch (SerializationException ex)
1020             {
1021                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1022                 return "Json Parse Error(DataContractJsonSerializer)";
1023             }
1024             catch (Exception ex)
1025             {
1026                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1027                 return "Invalid Json!";
1028             }
1029             retweeted_count = status.RetweetCount;
1030             return "";
1031         }
1032
1033         public string PostFavAdd(long id)
1034         {
1035             if (MyCommon._endingFlag) return "";
1036
1037             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1038
1039             //if (this.favQueue == null) this.favQueue = new FavoriteQueue(this)
1040
1041             //if (this.favQueue.Contains(id)) this.favQueue.Remove(id)
1042
1043             HttpStatusCode res;
1044             var content = "";
1045             try
1046             {
1047                 res = twCon.CreateFavorites(id, ref content);
1048             }
1049             catch(Exception ex)
1050             {
1051                 //this.favQueue.Add(id)
1052                 //return "Err:->FavoriteQueue:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1053                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1054             }
1055
1056             var err = this.CheckStatusCode(res, content);
1057             if (err != null) return err;
1058
1059             if (!_restrictFavCheck) return "";
1060
1061             //http://twitter.com/statuses/show/id.xml APIを発行して本文を取得
1062
1063             try
1064             {
1065                 res = twCon.ShowStatuses(id, ref content);
1066             }
1067             catch(Exception ex)
1068             {
1069                 return "Err:" + ex.Message;
1070             }
1071
1072             err = this.CheckStatusCode(res, content);
1073             if (err != null) return err;
1074
1075             TwitterStatus status;
1076             try
1077             {
1078                 status = TwitterStatus.ParseJson(content);
1079             }
1080             catch (SerializationException ex)
1081             {
1082                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1083                 return "Err:Json Parse Error(DataContractJsonSerializer)";
1084             }
1085             catch (Exception ex)
1086             {
1087                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1088                 return "Err:Invalid Json!";
1089             }
1090             if (status.Favorited == true)
1091             {
1092                 return "";
1093             }
1094             else
1095             {
1096                 return "NG(Restricted?)";
1097             }
1098         }
1099
1100         public string PostFavRemove(long id)
1101         {
1102             if (MyCommon._endingFlag) return "";
1103
1104             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1105
1106             //if (this.favQueue == null) this.favQueue = new FavoriteQueue(this)
1107
1108             //if (this.favQueue.Contains(id))
1109             //    this.favQueue.Remove(id)
1110             //    return "";
1111             //}
1112
1113             HttpStatusCode res;
1114             var content = "";
1115             try
1116             {
1117                 res = twCon.DestroyFavorites(id, ref content);
1118             }
1119             catch(Exception ex)
1120             {
1121                 return "Err:" + ex.Message;
1122             }
1123
1124             return this.CheckStatusCode(res, content) ?? "";
1125         }
1126
1127         public TwitterUser PostUpdateProfile(string name, string url, string location, string description)
1128         {
1129             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1130                 throw new WebApiException("AccountState invalid");
1131
1132             HttpStatusCode res;
1133             var content = "";
1134             try
1135             {
1136                 res = twCon.UpdateProfile(name, url, location, description, ref content);
1137             }
1138             catch(Exception ex)
1139             {
1140                 throw new WebApiException("Err:" + ex.Message, content, ex);
1141             }
1142
1143             var err = this.CheckStatusCode(res, content);
1144             if (err != null)
1145                 throw new WebApiException(err, content);
1146
1147             try
1148             {
1149                 return TwitterUser.ParseJson(content);
1150             }
1151             catch (SerializationException e)
1152             {
1153                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
1154                 MyCommon.TraceOut(ex);
1155                 throw ex;
1156             }
1157             catch (Exception e)
1158             {
1159                 var ex = new WebApiException("Err:Invalid Json!", content, e);
1160                 MyCommon.TraceOut(ex);
1161                 throw ex;
1162             }
1163         }
1164
1165         public string PostUpdateProfileImage(string filename)
1166         {
1167             if (MyCommon._endingFlag) return "";
1168
1169             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1170
1171             HttpStatusCode res;
1172             var content = "";
1173             try
1174             {
1175                 res = twCon.UpdateProfileImage(new FileInfo(filename), ref content);
1176             }
1177             catch(Exception ex)
1178             {
1179                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1180             }
1181
1182             return this.CheckStatusCode(res, content) ?? "";
1183         }
1184
1185         public string Username
1186         {
1187             get
1188             {
1189                 return twCon.AuthenticatedUsername;
1190             }
1191         }
1192
1193         public long UserId
1194         {
1195             get
1196             {
1197                 return twCon.AuthenticatedUserId;
1198             }
1199         }
1200
1201         public string Password
1202         {
1203             get
1204             {
1205                 return twCon.Password;
1206             }
1207         }
1208
1209         private static MyCommon.ACCOUNT_STATE _accountState = MyCommon.ACCOUNT_STATE.Valid;
1210         public static MyCommon.ACCOUNT_STATE AccountState
1211         {
1212             get
1213             {
1214                 return _accountState;
1215             }
1216             set
1217             {
1218                 _accountState = value;
1219             }
1220         }
1221
1222         public bool RestrictFavCheck
1223         {
1224             set
1225             {
1226                 _restrictFavCheck = value;
1227             }
1228         }
1229
1230 #region "バージョンアップ"
1231         public string GetTweenBinary(string strVer)
1232         {
1233             try
1234             {
1235                 //本体
1236                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/Tween" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1237                                                     Path.Combine(MyCommon.settingPath, "TweenNew.exe")))
1238                 {
1239                     return "Err:Download failed";
1240                 }
1241                 //英語リソース
1242                 if (!Directory.Exists(Path.Combine(MyCommon.settingPath, "en")))
1243                 {
1244                     Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, "en"));
1245                 }
1246                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenResEn" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1247                                                     Path.Combine(Path.Combine(MyCommon.settingPath, "en"), "Tween.resourcesNew.dll")))
1248                 {
1249                     return "Err:Download failed";
1250                 }
1251                 //その他言語圏のリソース。取得失敗しても継続
1252                 //UIの言語圏のリソース
1253                 var curCul = "";
1254                 if (!Thread.CurrentThread.CurrentUICulture.IsNeutralCulture)
1255                 {
1256                     var idx = Thread.CurrentThread.CurrentUICulture.Name.LastIndexOf('-');
1257                     if (idx > -1)
1258                     {
1259                         curCul = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, idx);
1260                     }
1261                     else
1262                     {
1263                         curCul = Thread.CurrentThread.CurrentUICulture.Name;
1264                     }
1265                 }
1266                 else
1267                 {
1268                     curCul = Thread.CurrentThread.CurrentUICulture.Name;
1269                 }
1270                 if (!string.IsNullOrEmpty(curCul) && curCul != "en" && curCul != "ja")
1271                 {
1272                     if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul)))
1273                     {
1274                         Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul));
1275                     }
1276                     if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1277                                                         Path.Combine(Path.Combine(MyCommon.settingPath, curCul), "Tween.resourcesNew.dll")))
1278                     {
1279                         //return "Err:Download failed";
1280                     }
1281                 }
1282                 //スレッドの言語圏のリソース
1283                 string curCul2;
1284                 if (!Thread.CurrentThread.CurrentCulture.IsNeutralCulture)
1285                 {
1286                     var idx = Thread.CurrentThread.CurrentCulture.Name.LastIndexOf('-');
1287                     if (idx > -1)
1288                     {
1289                         curCul2 = Thread.CurrentThread.CurrentCulture.Name.Substring(0, idx);
1290                     }
1291                     else
1292                     {
1293                         curCul2 = Thread.CurrentThread.CurrentCulture.Name;
1294                     }
1295                 }
1296                 else
1297                 {
1298                     curCul2 = Thread.CurrentThread.CurrentCulture.Name;
1299                 }
1300                 if (!string.IsNullOrEmpty(curCul2) && curCul2 != "en" && curCul2 != curCul)
1301                 {
1302                     if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul2)))
1303                     {
1304                         Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul2));
1305                     }
1306                     if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul2 + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1307                                                     Path.Combine(Path.Combine(MyCommon.settingPath, curCul2), "Tween.resourcesNew.dll")))
1308                     {
1309                         //return "Err:Download failed";
1310                     }
1311                 }
1312
1313                 //アップデータ
1314                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenUp3.gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1315                                                     Path.Combine(MyCommon.settingPath, "TweenUp3.exe")))
1316                 {
1317                     return "Err:Download failed";
1318                 }
1319                 //シリアライザDLL
1320                 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenDll" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1321                                                     Path.Combine(MyCommon.settingPath, "TweenNew.XmlSerializers.dll")))
1322                 {
1323                     return "Err:Download failed";
1324                 }
1325                 return "";
1326             }
1327             catch(Exception)
1328             {
1329                 return "Err:Download failed";
1330             }
1331         }
1332 #endregion
1333
1334         public bool ReadOwnPost
1335         {
1336             get
1337             {
1338                 return _readOwnPost;
1339             }
1340             set
1341             {
1342                 _readOwnPost = value;
1343             }
1344         }
1345
1346         public int FollowersCount
1347         {
1348             get
1349             {
1350                 return _followersCount;
1351             }
1352         }
1353
1354         public int FriendsCount
1355         {
1356             get
1357             {
1358                 return _friendsCount;
1359             }
1360         }
1361
1362         public int StatusesCount
1363         {
1364             get
1365             {
1366                 return _statusesCount;
1367             }
1368         }
1369
1370         public string Location
1371         {
1372             get
1373             {
1374                 return _location;
1375             }
1376         }
1377
1378         public string Bio
1379         {
1380             get
1381             {
1382                 return _bio;
1383             }
1384         }
1385
1386         public string GetTimelineApi(bool read,
1387                                 MyCommon.WORKERTYPE gType,
1388                                 bool more,
1389                                 bool startup)
1390         {
1391             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1392
1393             if (MyCommon._endingFlag) return "";
1394
1395             HttpStatusCode res;
1396             var content = "";
1397             var count = SettingCommon.Instance.CountApi;
1398             if (gType == MyCommon.WORKERTYPE.Reply) count = SettingCommon.Instance.CountApiReply;
1399             if (SettingCommon.Instance.UseAdditionalCount)
1400             {
1401                 if (more && SettingCommon.Instance.MoreCountApi != 0)
1402                 {
1403                     count = SettingCommon.Instance.MoreCountApi;
1404                 }
1405                 else if (startup && SettingCommon.Instance.FirstCountApi != 0 && gType == MyCommon.WORKERTYPE.Timeline)
1406                 {
1407                     count = SettingCommon.Instance.FirstCountApi;
1408                 }
1409             }
1410             try
1411             {
1412                 if (gType == MyCommon.WORKERTYPE.Timeline)
1413                 {
1414                     if (more)
1415                     {
1416                         res = twCon.HomeTimeline(count, this.minHomeTimeline, null, ref content);
1417                     }
1418                     else
1419                     {
1420                         res = twCon.HomeTimeline(count, null, null, ref content);
1421                     }
1422                 }
1423                 else
1424                 {
1425                     if (more)
1426                     {
1427                         res = twCon.Mentions(count, this.minMentions, null, ref content);
1428                     }
1429                     else
1430                     {
1431                         res = twCon.Mentions(count, null, null, ref content);
1432                     }
1433                 }
1434             }
1435             catch(Exception ex)
1436             {
1437                 return "Err:" + ex.Message;
1438             }
1439
1440             var err = this.CheckStatusCode(res, content);
1441             if (err != null) return err;
1442
1443             if (gType == MyCommon.WORKERTYPE.Timeline)
1444             {
1445                 return CreatePostsFromJson(content, gType, null, read, count, ref this.minHomeTimeline);
1446             }
1447             else
1448             {
1449                 return CreatePostsFromJson(content, gType, null, read, count, ref this.minMentions);
1450             }
1451         }
1452
1453         public string GetUserTimelineApi(bool read,
1454                                          int count,
1455                                          string userName,
1456                                          TabClass tab,
1457                                          bool more)
1458         {
1459             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1460
1461             if (MyCommon._endingFlag) return "";
1462
1463             HttpStatusCode res;
1464             var content = "";
1465
1466             if (count == 0) count = 20;
1467             try
1468             {
1469                 if (string.IsNullOrEmpty(userName))
1470                 {
1471                     var target = tab.User;
1472                     if (string.IsNullOrEmpty(target)) return "";
1473                     userName = target;
1474                     res = twCon.UserTimeline(null, target, count, null, null, ref content);
1475                 }
1476                 else
1477                 {
1478                     if (more)
1479                     {
1480                         res = twCon.UserTimeline(null, userName, count, tab.OldestId, null, ref content);
1481                     }
1482                     else
1483                     {
1484                         res = twCon.UserTimeline(null, userName, count, null, null, ref content);
1485                     }
1486                 }
1487             }
1488             catch(Exception ex)
1489             {
1490                 return "Err:" + ex.Message;
1491             }
1492
1493             if (res == HttpStatusCode.Unauthorized)
1494                 return "Err:@" + userName + "'s Tweets are protected.";
1495
1496             var err = this.CheckStatusCode(res, content);
1497             if (err != null) return err;
1498
1499             TwitterStatus[] items;
1500             try
1501             {
1502                 items = TwitterStatus.ParseJsonArray(content);
1503             }
1504             catch(SerializationException ex)
1505             {
1506                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1507                 return "Json Parse Error(DataContractJsonSerializer)";
1508             }
1509             catch(Exception ex)
1510             {
1511                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1512                 return "Invalid Json!";
1513             }
1514
1515             foreach (var status in items)
1516             {
1517                 var item = CreatePostsFromStatusData(status);
1518                 if (item == null) continue;
1519                 if (item.StatusId < tab.OldestId) tab.OldestId = item.StatusId;
1520                 item.IsRead = read;
1521                 if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
1522                 if (tab != null) item.RelTabName = tab.TabName;
1523                 //非同期アイコン取得&StatusDictionaryに追加
1524                 TabInformations.GetInstance().AddPost(item);
1525             }
1526
1527             return "";
1528         }
1529
1530         public string GetStatusApi(bool read,
1531                                    Int64 id,
1532                                    ref PostClass post)
1533         {
1534             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1535
1536             if (MyCommon._endingFlag) return "";
1537
1538             HttpStatusCode res;
1539             var content = "";
1540             try
1541             {
1542                 res = twCon.ShowStatuses(id, ref content);
1543             }
1544             catch(Exception ex)
1545             {
1546                 return "Err:" + ex.Message;
1547             }
1548
1549             if (res == HttpStatusCode.Forbidden)
1550                 return "Err:protected user's tweet";
1551
1552             var err = this.CheckStatusCode(res, content);
1553             if (err != null) return err;
1554
1555             TwitterStatus status;
1556             try
1557             {
1558                 status = TwitterStatus.ParseJson(content);
1559             }
1560             catch(SerializationException ex)
1561             {
1562                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1563                 return "Json Parse Error(DataContractJsonSerializer)";
1564             }
1565             catch(Exception ex)
1566             {
1567                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1568                 return "Invalid Json!";
1569             }
1570
1571             var item = CreatePostsFromStatusData(status);
1572             if (item == null) return "Err:Can't create post";
1573             item.IsRead = read;
1574             if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
1575
1576             post = item;
1577             return "";
1578         }
1579
1580         public string GetStatusApi(bool read,
1581                                    Int64 id,
1582                                    TabClass tab)
1583         {
1584             PostClass post = null;
1585             var r = this.GetStatusApi(read, id, ref post);
1586
1587             if (string.IsNullOrEmpty(r))
1588             {
1589                 if (tab != null) post.RelTabName = tab.TabName;
1590                 //非同期アイコン取得&StatusDictionaryに追加
1591                 TabInformations.GetInstance().AddPost(post);
1592             }
1593
1594             return r;
1595         }
1596
1597         private PostClass CreatePostsFromStatusData(TwitterStatus status)
1598         {
1599             var post = new PostClass();
1600             TwitterEntities entities;
1601
1602             post.StatusId = status.Id;
1603             if (status.RetweetedStatus != null)
1604             {
1605                 var retweeted = status.RetweetedStatus;
1606
1607                 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
1608
1609                 //Id
1610                 post.RetweetedId = retweeted.Id;
1611                 //本文
1612                 post.TextFromApi = retweeted.Text;
1613                 entities = retweeted.MergedEntities;
1614                 //Source取得(htmlの場合は、中身を取り出し)
1615                 post.Source = retweeted.Source;
1616                 //Reply先
1617                 post.InReplyToStatusId = retweeted.InReplyToStatusId;
1618                 post.InReplyToUser = retweeted.InReplyToScreenName;
1619                 post.InReplyToUserId = status.InReplyToUserId;
1620
1621                 //幻覚fav対策
1622                 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1623                 post.IsFav = tc.Contains(retweeted.Id);
1624
1625                 if (retweeted.Coordinates != null) post.PostGeo = new PostClass.StatusGeo { Lng = retweeted.Coordinates.Coordinates[0], Lat = retweeted.Coordinates.Coordinates[1] };
1626
1627                 //以下、ユーザー情報
1628                 var user = retweeted.User;
1629
1630                 if (user == null || user.ScreenName == null || status.User.ScreenName == null) return null;
1631
1632                 post.UserId = user.Id;
1633                 post.ScreenName = user.ScreenName;
1634                 post.Nickname = user.Name.Trim();
1635                 post.ImageUrl = user.ProfileImageUrlHttps;
1636                 post.IsProtect = user.Protected;
1637
1638                 //Retweetした人
1639                 post.RetweetedBy = status.User.ScreenName;
1640                 post.RetweetedByUserId = status.User.Id;
1641                 post.IsMe = post.RetweetedBy.ToLower().Equals(_uname);
1642             }
1643             else
1644             {
1645                 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
1646                 //本文
1647                 post.TextFromApi = status.Text;
1648                 entities = status.MergedEntities;
1649                 //Source取得(htmlの場合は、中身を取り出し)
1650                 post.Source = status.Source;
1651                 post.InReplyToStatusId = status.InReplyToStatusId;
1652                 post.InReplyToUser = status.InReplyToScreenName;
1653                 post.InReplyToUserId = status.InReplyToUserId;
1654
1655                 if (status.Coordinates != null) post.PostGeo = new PostClass.StatusGeo { Lng = status.Coordinates.Coordinates[0], Lat = status.Coordinates.Coordinates[1] };
1656
1657                 //以下、ユーザー情報
1658                 var user = status.User;
1659
1660                 if (user == null || user.ScreenName == null) return null;
1661
1662                 post.UserId = user.Id;
1663                 post.ScreenName = user.ScreenName;
1664                 post.Nickname = user.Name.Trim();
1665                 post.ImageUrl = user.ProfileImageUrlHttps;
1666                 post.IsProtect = user.Protected;
1667                 post.IsMe = post.ScreenName.ToLower().Equals(_uname);
1668
1669                 //幻覚fav対策
1670                 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1671                 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
1672             }
1673             //HTMLに整形
1674             string textFromApi = post.TextFromApi;
1675             post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
1676             post.TextFromApi = textFromApi;
1677             post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
1678             post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1679             post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1680
1681             //Source整形
1682             CreateSource(post);
1683
1684             post.IsReply = post.ReplyToList.Contains(_uname);
1685             post.IsExcludeReply = false;
1686
1687             if (post.IsMe)
1688             {
1689                 post.IsOwl = false;
1690             }
1691             else
1692             {
1693                 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
1694             }
1695
1696             post.IsDm = false;
1697             return post;
1698         }
1699
1700         private string CreatePostsFromJson(string content, MyCommon.WORKERTYPE gType, TabClass tab, bool read, int count, ref long minimumId)
1701         {
1702             TwitterStatus[] items;
1703             try
1704             {
1705                 items = TwitterStatus.ParseJsonArray(content);
1706             }
1707             catch(SerializationException ex)
1708             {
1709                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1710                 return "Json Parse Error(DataContractJsonSerializer)";;
1711             }
1712             catch(Exception ex)
1713             {
1714                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1715                 return "Invalid Json!";
1716             }
1717
1718             foreach (var status in items)
1719             {
1720                 PostClass post = null;
1721                 post = CreatePostsFromStatusData(status);
1722                 if (post == null) continue;
1723
1724                 if (minimumId > post.StatusId) minimumId = post.StatusId;
1725                 //二重取得回避
1726                 lock (LockObj)
1727                 {
1728                     if (tab == null)
1729                     {
1730                         if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1731                     }
1732                     else
1733                     {
1734                         if (TabInformations.GetInstance().ContainsKey(post.StatusId, tab.TabName)) continue;
1735                     }
1736                 }
1737
1738                 //RT禁止ユーザーによるもの
1739                 if (post.RetweetedByUserId != null && this.noRTId.Contains(post.RetweetedByUserId.Value)) continue;
1740
1741                 post.IsRead = read;
1742                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1743
1744                 if (tab != null) post.RelTabName = tab.TabName;
1745                 //非同期アイコン取得&StatusDictionaryに追加
1746                 TabInformations.GetInstance().AddPost(post);
1747             }
1748
1749             return "";
1750         }
1751
1752         private string CreatePostsFromSearchJson(string content, TabClass tab, bool read, int count, ref long minimumId, bool more)
1753         {
1754             TwitterSearchResult items;
1755             try
1756             {
1757                 items = TwitterSearchResult.ParseJson(content);
1758             }
1759             catch (SerializationException ex)
1760             {
1761                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1762                 return "Json Parse Error(DataContractJsonSerializer)";
1763             }
1764             catch (Exception ex)
1765             {
1766                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1767                 return "Invalid Json!";
1768             }
1769             foreach (var result in items.Statuses)
1770             {
1771                 PostClass post = null;
1772                 post = CreatePostsFromStatusData(result);
1773
1774                 if (post == null)
1775                 {
1776                     // Search API は相変わらずぶっ壊れたデータを返すことがあるため、必要なデータが欠如しているものは取得し直す
1777                     var ret = this.GetStatusApi(read, result.Id, ref post);
1778                     if (!string.IsNullOrEmpty(ret)) continue;
1779                 }
1780
1781                 if (minimumId > post.StatusId) minimumId = post.StatusId;
1782                 if (!more && post.StatusId > tab.SinceId) tab.SinceId = post.StatusId;
1783                 //二重取得回避
1784                 lock (LockObj)
1785                 {
1786                     if (tab == null)
1787                     {
1788                         if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1789                     }
1790                     else
1791                     {
1792                         if (TabInformations.GetInstance().ContainsKey(post.StatusId, tab.TabName)) continue;
1793                     }
1794                 }
1795
1796                 post.IsRead = read;
1797                 if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
1798
1799                 if (tab != null) post.RelTabName = tab.TabName;
1800                 //非同期アイコン取得&StatusDictionaryに追加
1801                 TabInformations.GetInstance().AddPost(post);
1802             }
1803
1804             return "";
1805         }
1806
1807         public string GetListStatus(bool read,
1808                                 TabClass tab,
1809                                 bool more,
1810                                 bool startup)
1811         {
1812             if (MyCommon._endingFlag) return "";
1813
1814             HttpStatusCode res;
1815             var content = "";
1816             int count;
1817             if (SettingCommon.Instance.UseAdditionalCount)
1818             {
1819                 count = SettingCommon.Instance.ListCountApi;
1820                 if (count == 0)
1821                 {
1822                     if (more && SettingCommon.Instance.MoreCountApi != 0)
1823                     {
1824                         count = SettingCommon.Instance.MoreCountApi;
1825                     }
1826                     else if (startup && SettingCommon.Instance.FirstCountApi != 0)
1827                     {
1828                         count = SettingCommon.Instance.FirstCountApi;
1829                     }
1830                     else
1831                     {
1832                         count = SettingCommon.Instance.CountApi;
1833                     }
1834                 }
1835             }
1836             else
1837             {
1838                 count = SettingCommon.Instance.CountApi;
1839             }
1840             try
1841             {
1842                 if (more)
1843                 {
1844                     res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, tab.OldestId, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1845                 }
1846                 else
1847                 {
1848                     res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, null, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1849                 }
1850             }
1851             catch(Exception ex)
1852             {
1853                 return "Err:" + ex.Message;
1854             }
1855
1856             var err = this.CheckStatusCode(res, content);
1857             if (err != null) return err;
1858
1859             return CreatePostsFromJson(content, MyCommon.WORKERTYPE.List, tab, read, count, ref tab.OldestId);
1860         }
1861
1862         /// <summary>
1863         /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
1864         /// </summary>
1865         /// <returns>posts の中から検索されたリプライチェインの末端</returns>
1866         internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
1867         {
1868             if (!posts.ContainsKey(startStatusId))
1869                 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。");
1870
1871             var nextPost = posts[startStatusId];
1872             while (nextPost.InReplyToStatusId != null)
1873             {
1874                 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
1875                     break;
1876                 nextPost = posts[nextPost.InReplyToStatusId.Value];
1877             }
1878
1879             return nextPost;
1880         }
1881
1882         public string GetRelatedResult(bool read, TabClass tab)
1883         {
1884             var rslt = "";
1885             var relPosts = new Dictionary<Int64, PostClass>();
1886             if (tab.RelationTargetPost.TextFromApi.Contains("@") && tab.RelationTargetPost.InReplyToStatusId == null)
1887             {
1888                 //検索結果対応
1889                 var p = TabInformations.GetInstance()[tab.RelationTargetPost.StatusId];
1890                 if (p != null && p.InReplyToStatusId != null)
1891                 {
1892                     tab.RelationTargetPost = p;
1893                 }
1894                 else
1895                 {
1896                     rslt = this.GetStatusApi(read, tab.RelationTargetPost.StatusId, ref p);
1897                     if (!string.IsNullOrEmpty(rslt)) return rslt;
1898                     tab.RelationTargetPost = p;
1899                 }
1900             }
1901             relPosts.Add(tab.RelationTargetPost.StatusId, tab.RelationTargetPost.Clone());
1902
1903             // in_reply_to_status_id を使用してリプライチェインを辿る
1904             var nextPost = FindTopOfReplyChain(relPosts, tab.RelationTargetPost.StatusId);
1905             var loopCount = 1;
1906             while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1907             {
1908                 var inReplyToId = nextPost.InReplyToStatusId.Value;
1909
1910                 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1911                 if (inReplyToPost != null)
1912                 {
1913                     inReplyToPost = inReplyToPost.Clone();
1914                 }
1915                 else
1916                 {
1917                     var errorText = this.GetStatusApi(read, inReplyToId, ref inReplyToPost);
1918                     if (!string.IsNullOrEmpty(errorText))
1919                     {
1920                         rslt = errorText;
1921                         break;
1922                     }
1923                 }
1924
1925                 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1926
1927                 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1928             }
1929
1930             //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1931             var text = tab.RelationTargetPost.Text;
1932             var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1933                 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1934             foreach (var _match in ma)
1935             {
1936                 Int64 _statusId;
1937                 if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
1938                 {
1939                     if (relPosts.ContainsKey(_statusId))
1940                         continue;
1941
1942                     PostClass p = null;
1943                     var _post = TabInformations.GetInstance()[_statusId];
1944                     if (_post == null)
1945                     {
1946                         this.GetStatusApi(read, _statusId, ref p);
1947                     }
1948                     else
1949                     {
1950                         p = _post.Clone();
1951                     }
1952
1953                     if (p != null)
1954                         relPosts.Add(p.StatusId, p);
1955                 }
1956             }
1957
1958             relPosts.Values.ToList().ForEach(p =>
1959             {
1960                 if (p.IsMe && !read && this._readOwnPost)
1961                     p.IsRead = true;
1962                 else
1963                     p.IsRead = read;
1964
1965                 p.RelTabName = tab.TabName;
1966                 TabInformations.GetInstance().AddPost(p);
1967             });
1968
1969             return rslt;
1970         }
1971
1972         public string GetSearch(bool read,
1973                             TabClass tab,
1974                             bool more)
1975         {
1976             if (MyCommon._endingFlag) return "";
1977
1978             HttpStatusCode res;
1979             var content = "";
1980             long? maxId = null;
1981             long? sinceId = null;
1982             var count = 100;
1983             if (SettingCommon.Instance.UseAdditionalCount &&
1984                 SettingCommon.Instance.SearchCountApi != 0)
1985             {
1986                 count = SettingCommon.Instance.SearchCountApi;
1987             }
1988             else
1989             {
1990                 count = SettingCommon.Instance.CountApi;
1991             }
1992             if (more)
1993             {
1994                 maxId = tab.OldestId - 1;
1995             }
1996             else
1997             {
1998                 sinceId = tab.SinceId;
1999             }
2000
2001             try
2002             {
2003                 // TODO:一時的に40>100件に 件数変更UI作成の必要あり
2004                 res = twCon.Search(tab.SearchWords, tab.SearchLang, count, maxId, sinceId, ref content);
2005             }
2006             catch(Exception ex)
2007             {
2008                 return "Err:" + ex.Message;
2009             }
2010             switch (res)
2011             {
2012                 case HttpStatusCode.BadRequest:
2013                     return "Invalid query";
2014                 case HttpStatusCode.NotFound:
2015                     return "Invalid query";
2016                 case HttpStatusCode.PaymentRequired: //API Documentには420と書いてあるが、該当コードがないので402にしてある
2017                     return "Search API Limit?";
2018                 case HttpStatusCode.OK:
2019                     break;
2020                 default:
2021                     return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
2022             }
2023
2024             if (!TabInformations.GetInstance().ContainsTab(tab)) return "";
2025
2026             return this.CreatePostsFromSearchJson(content, tab, read, count, ref tab.OldestId, more);
2027         }
2028
2029         private string CreateDirectMessagesFromJson(string content, MyCommon.WORKERTYPE gType, bool read)
2030         {
2031             TwitterDirectMessage[] item;
2032             try
2033             {
2034                 if (gType == MyCommon.WORKERTYPE.UserStream)
2035                 {
2036                     item = new[] { TwitterStreamEventDirectMessage.ParseJson(content).DirectMessage };
2037                 }
2038                 else
2039                 {
2040                     item = TwitterDirectMessage.ParseJsonArray(content);
2041                 }
2042             }
2043             catch(SerializationException ex)
2044             {
2045                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2046                 return "Json Parse Error(DataContractJsonSerializer)";
2047             }
2048             catch(Exception ex)
2049             {
2050                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2051                 return "Invalid Json!";
2052             }
2053
2054             foreach (var message in item)
2055             {
2056                 var post = new PostClass();
2057                 try
2058                 {
2059                     post.StatusId = message.Id;
2060                     if (gType != MyCommon.WORKERTYPE.UserStream)
2061                     {
2062                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2063                         {
2064                             if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
2065                         }
2066                         else
2067                         {
2068                             if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
2069                         }
2070                     }
2071
2072                     //二重取得回避
2073                     lock (LockObj)
2074                     {
2075                         if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
2076                     }
2077                     //sender_id
2078                     //recipient_id
2079                     post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
2080                     //本文
2081                     var textFromApi = message.Text;
2082                     //HTMLに整形
2083                     post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, message.Entities, post.Media);
2084                     post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
2085                     post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
2086                     post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
2087                     post.IsFav = false;
2088
2089                     //以下、ユーザー情報
2090                     TwitterUser user;
2091                     if (gType == MyCommon.WORKERTYPE.UserStream)
2092                     {
2093                         if (twCon.AuthenticatedUsername.Equals(message.Recipient.ScreenName, StringComparison.CurrentCultureIgnoreCase))
2094                         {
2095                             user = message.Sender;
2096                             post.IsMe = false;
2097                             post.IsOwl = true;
2098                         }
2099                         else
2100                         {
2101                             user = message.Recipient;
2102                             post.IsMe = true;
2103                             post.IsOwl = false;
2104                         }
2105                     }
2106                     else
2107                     {
2108                         if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2109                         {
2110                             user = message.Sender;
2111                             post.IsMe = false;
2112                             post.IsOwl = true;
2113                         }
2114                         else
2115                         {
2116                             user = message.Recipient;
2117                             post.IsMe = true;
2118                             post.IsOwl = false;
2119                         }
2120                     }
2121
2122                     post.UserId = user.Id;
2123                     post.ScreenName = user.ScreenName;
2124                     post.Nickname = user.Name.Trim();
2125                     post.ImageUrl = user.ProfileImageUrlHttps;
2126                     post.IsProtect = user.Protected;
2127                 }
2128                 catch(Exception ex)
2129                 {
2130                     MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2131                     MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
2132                     continue;
2133                 }
2134
2135                 post.IsRead = read;
2136                 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
2137                 post.IsReply = false;
2138                 post.IsExcludeReply = false;
2139                 post.IsDm = true;
2140
2141                 TabInformations.GetInstance().AddPost(post);
2142             }
2143
2144             return "";
2145
2146         }
2147
2148         public string GetDirectMessageApi(bool read,
2149                                 MyCommon.WORKERTYPE gType,
2150                                 bool more)
2151         {
2152             if (MyCommon._endingFlag) return "";
2153
2154             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2155
2156             if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
2157             {
2158                 return "Auth Err:try to re-authorization.";
2159             }
2160
2161             HttpStatusCode res;
2162             var content = "";
2163
2164             try
2165             {
2166                 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2167                 {
2168                     if (more)
2169                     {
2170                         res = twCon.DirectMessages(20, minDirectmessage, null, ref content);
2171                     }
2172                     else
2173                     {
2174                         res = twCon.DirectMessages(20, null, null, ref content);
2175                     }
2176                 }
2177                 else
2178                 {
2179                     if (more)
2180                     {
2181                         res = twCon.DirectMessagesSent(20, minDirectmessageSent, null, ref content);
2182                     }
2183                     else
2184                     {
2185                         res = twCon.DirectMessagesSent(20, null, null, ref content);
2186                     }
2187                 }
2188             }
2189             catch(Exception ex)
2190             {
2191                 return "Err:" + ex.Message;
2192             }
2193
2194             var err = this.CheckStatusCode(res, content);
2195             if (err != null) return err;
2196
2197             return CreateDirectMessagesFromJson(content, gType, read);
2198         }
2199
2200         static int page_ = 1;
2201         public string GetFavoritesApi(bool read,
2202                             MyCommon.WORKERTYPE gType,
2203                             bool more)
2204         {
2205             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2206
2207             if (MyCommon._endingFlag) return "";
2208
2209             var count = SettingCommon.Instance.CountApi;
2210             if (SettingCommon.Instance.UseAdditionalCount &&
2211                 SettingCommon.Instance.FavoritesCountApi != 0)
2212             {
2213                 count = SettingCommon.Instance.FavoritesCountApi;
2214             }
2215
2216             // 前ページ取得の場合はページカウンタをインクリメント、それ以外の場合はページカウンタリセット
2217             if (more)
2218             {
2219                 page_++;
2220             }
2221             else
2222             {
2223                 page_ = 1;
2224             }
2225
2226             HttpStatusCode res;
2227             var content = "";
2228             try
2229             {
2230                 res = twCon.Favorites(count, page_, ref content);
2231             }
2232             catch(Exception ex)
2233             {
2234                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2235             }
2236
2237             var err = this.CheckStatusCode(res, content);
2238             if (err != null) return err;
2239
2240             TwitterStatus[] item;
2241             try
2242             {
2243                 item = TwitterStatus.ParseJsonArray(content);
2244             }
2245             catch(SerializationException ex)
2246             {
2247                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2248                 return "Json Parse Error(DataContractJsonSerializer)";
2249             }
2250             catch(Exception ex)
2251             {
2252                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2253                 return "Invalid Json!";
2254             }
2255
2256             foreach (var status in item)
2257             {
2258                 var post = new PostClass();
2259                 TwitterEntities entities;
2260
2261                 try
2262                 {
2263                     post.StatusId = status.Id;
2264                     //二重取得回避
2265                     lock (LockObj)
2266                     {
2267                         if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).Contains(post.StatusId)) continue;
2268                     }
2269                     //Retweet判定
2270                     if (status.RetweetedStatus != null)
2271                     {
2272                         var retweeted = status.RetweetedStatus;
2273                         post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
2274
2275                         //Id
2276                         post.RetweetedId = post.StatusId;
2277                         //本文
2278                         post.TextFromApi = retweeted.Text;
2279                         entities = retweeted.MergedEntities;
2280                         //Source取得(htmlの場合は、中身を取り出し)
2281                         post.Source = retweeted.Source;
2282                         //Reply先
2283                         post.InReplyToStatusId = retweeted.InReplyToStatusId;
2284                         post.InReplyToUser = retweeted.InReplyToScreenName;
2285                         post.InReplyToUserId = retweeted.InReplyToUserId;
2286                         post.IsFav = true;
2287
2288                         //以下、ユーザー情報
2289                         var user = retweeted.User;
2290                         post.UserId = user.Id;
2291                         post.ScreenName = user.ScreenName;
2292                         post.Nickname = user.Name.Trim();
2293                         post.ImageUrl = user.ProfileImageUrlHttps;
2294                         post.IsProtect = user.Protected;
2295
2296                         //Retweetした人
2297                         post.RetweetedBy = status.User.ScreenName;
2298                         post.IsMe = post.RetweetedBy.ToLower().Equals(_uname);
2299                     }
2300                     else
2301                     {
2302                         post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
2303
2304                         //本文
2305                         post.TextFromApi = status.Text;
2306                         entities = status.MergedEntities;
2307                         //Source取得(htmlの場合は、中身を取り出し)
2308                         post.Source = status.Source;
2309                         post.InReplyToStatusId = status.InReplyToStatusId;
2310                         post.InReplyToUser = status.InReplyToScreenName;
2311                         post.InReplyToUserId = status.InReplyToUserId;
2312
2313                         post.IsFav = true;
2314
2315                         //以下、ユーザー情報
2316                         var user = status.User;
2317                         post.UserId = user.Id;
2318                         post.ScreenName = user.ScreenName;
2319                         post.Nickname = user.Name.Trim();
2320                         post.ImageUrl = user.ProfileImageUrlHttps;
2321                         post.IsProtect = user.Protected;
2322                         post.IsMe = post.ScreenName.ToLower().Equals(_uname);
2323                     }
2324                     //HTMLに整形
2325                     string textFromApi = post.TextFromApi;
2326                     post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
2327                     post.TextFromApi = textFromApi;
2328                     post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
2329                     post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
2330                     post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
2331                     //Source整形
2332                     CreateSource(post);
2333
2334                     post.IsRead = read;
2335                     post.IsReply = post.ReplyToList.Contains(_uname);
2336                     post.IsExcludeReply = false;
2337
2338                     if (post.IsMe)
2339                     {
2340                         post.IsOwl = false;
2341                     }
2342                     else
2343                     {
2344                         if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
2345                     }
2346
2347                     post.IsDm = false;
2348                 }
2349                 catch(Exception ex)
2350                 {
2351                     MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2352                     continue;
2353                 }
2354
2355                 TabInformations.GetInstance().AddPost(post);
2356
2357             }
2358
2359             return "";
2360         }
2361
2362         private string ReplaceTextFromApi(string text, TwitterEntities entities)
2363         {
2364             if (entities != null)
2365             {
2366                 if (entities.Urls != null)
2367                 {
2368                     foreach (var m in entities.Urls)
2369                     {
2370                         if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
2371                     }
2372                 }
2373                 if (entities.Media != null)
2374                 {
2375                     foreach (var m in entities.Media)
2376                     {
2377                         if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
2378                     }
2379                 }
2380             }
2381             return text;
2382         }
2383
2384         /// <summary>
2385         /// フォロワーIDを更新します
2386         /// </summary>
2387         /// <exception cref="WebApiException"/>
2388         public void RefreshFollowerIds()
2389         {
2390             if (MyCommon._endingFlag) return;
2391
2392             var cursor = -1L;
2393             var newFollowerIds = new List<long>();
2394             do
2395             {
2396                 var ret = this.GetFollowerIdsApi(ref cursor);
2397                 newFollowerIds.AddRange(ret.Ids);
2398                 cursor = ret.NextCursor;
2399             } while (cursor != 0);
2400
2401             this.followerId = newFollowerIds;
2402             TabInformations.GetInstance().RefreshOwl(this.followerId);
2403
2404             this._GetFollowerResult = true;
2405         }
2406
2407         public bool GetFollowersSuccess
2408         {
2409             get
2410             {
2411                 return _GetFollowerResult;
2412             }
2413         }
2414
2415         private TwitterIds GetFollowerIdsApi(ref long cursor)
2416         {
2417             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2418                 throw new WebApiException("AccountState invalid");
2419
2420             HttpStatusCode res;
2421             var content = "";
2422             try
2423             {
2424                 res = twCon.FollowerIds(cursor, ref content);
2425             }
2426             catch(Exception e)
2427             {
2428                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2429             }
2430
2431             var err = this.CheckStatusCode(res, content);
2432             if (err != null)
2433                 throw new WebApiException(err, content);
2434
2435             try
2436             {
2437                 var ret = TwitterIds.ParseJson(content);
2438
2439                 if (ret.Ids == null)
2440                 {
2441                     var ex = new WebApiException("Err: ret.id == null (GetFollowerIdsApi)", content);
2442                     MyCommon.ExceptionOut(ex);
2443                     throw ex;
2444                 }
2445
2446                 return ret;
2447             }
2448             catch(SerializationException e)
2449             {
2450                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2451                 MyCommon.TraceOut(ex);
2452                 throw ex;
2453             }
2454             catch(Exception e)
2455             {
2456                 var ex = new WebApiException("Err:Invalid Json!", content, e);
2457                 MyCommon.TraceOut(ex);
2458                 throw ex;
2459             }
2460         }
2461
2462         /// <summary>
2463         /// RT 非表示ユーザーを更新します
2464         /// </summary>
2465         /// <exception cref="WebApiException"/>
2466         public void RefreshNoRetweetIds()
2467         {
2468             if (MyCommon._endingFlag) return;
2469
2470             this.noRTId = this.NoRetweetIdsApi();
2471
2472             this._GetNoRetweetResult = true;
2473         }
2474
2475         private long[] NoRetweetIdsApi()
2476         {
2477             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2478                 throw new WebApiException("AccountState invalid");
2479
2480             HttpStatusCode res;
2481             var content = "";
2482             try
2483             {
2484                 res = twCon.NoRetweetIds(ref content);
2485             }
2486             catch(Exception e)
2487             {
2488                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2489             }
2490
2491             var err = this.CheckStatusCode(res, content);
2492             if (err != null)
2493                 throw new WebApiException(err, content);
2494
2495             try
2496             {
2497                 return MyCommon.CreateDataFromJson<long[]>(content);
2498             }
2499             catch(SerializationException e)
2500             {
2501                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2502                 MyCommon.TraceOut(ex);
2503                 throw ex;
2504             }
2505             catch(Exception e)
2506             {
2507                 var ex = new WebApiException("Err:Invalid Json!", content, e);
2508                 MyCommon.TraceOut(ex);
2509                 throw ex;
2510             }
2511         }
2512
2513         public bool GetNoRetweetSuccess
2514         {
2515             get
2516             {
2517                 return _GetNoRetweetResult;
2518             }
2519         }
2520
2521         /// <summary>
2522         /// t.co の文字列長などの設定情報を更新します
2523         /// </summary>
2524         /// <exception cref="WebApiException"/>
2525         public void RefreshConfiguration()
2526         {
2527             this.Configuration = this.ConfigurationApi();
2528         }
2529
2530         private TwitterConfiguration ConfigurationApi()
2531         {
2532             HttpStatusCode res;
2533             var content = "";
2534             try
2535             {
2536                 res = twCon.GetConfiguration(ref content);
2537             }
2538             catch(Exception e)
2539             {
2540                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2541             }
2542
2543             var err = this.CheckStatusCode(res, content);
2544             if (err != null)
2545                 throw new WebApiException(err, content);
2546
2547             try
2548             {
2549                 return TwitterConfiguration.ParseJson(content);
2550             }
2551             catch(SerializationException e)
2552             {
2553                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2554                 MyCommon.TraceOut(ex);
2555                 throw ex;
2556             }
2557             catch(Exception e)
2558             {
2559                 var ex = new WebApiException("Err:Invalid Json!", content, e);
2560                 MyCommon.TraceOut(ex);
2561                 throw ex;
2562             }
2563         }
2564
2565         public string GetListsApi()
2566         {
2567             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2568
2569             HttpStatusCode res;
2570             IEnumerable<ListElement> lists;
2571             var content = "";
2572
2573             try
2574             {
2575                 res = twCon.GetLists(this.Username, ref content);
2576             }
2577             catch (Exception ex)
2578             {
2579                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2580             }
2581
2582             var err = this.CheckStatusCode(res, content);
2583             if (err != null) return err;
2584
2585             try
2586             {
2587                 lists = TwitterList.ParseJsonArray(content)
2588                     .Select(x => new ListElement(x, this));
2589             }
2590             catch (SerializationException ex)
2591             {
2592                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2593                 return "Err:Json Parse Error(DataContractJsonSerializer)";
2594             }
2595             catch (Exception ex)
2596             {
2597                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2598                 return "Err:Invalid Json!";
2599             }
2600
2601             try
2602             {
2603                 res = twCon.GetListsSubscriptions(this.Username, ref content);
2604             }
2605             catch (Exception ex)
2606             {
2607                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2608             }
2609
2610             err = this.CheckStatusCode(res, content);
2611             if (err != null) return err;
2612
2613             try
2614             {
2615                 lists = lists.Concat(TwitterList.ParseJsonArray(content)
2616                     .Select(x => new ListElement(x, this)));
2617             }
2618             catch (SerializationException ex)
2619             {
2620                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2621                 return "Err:Json Parse Error(DataContractJsonSerializer)";
2622             }
2623             catch (Exception ex)
2624             {
2625                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2626                 return "Err:Invalid Json!";
2627             }
2628
2629             TabInformations.GetInstance().SubscribableLists = lists.ToList();
2630             return "";
2631         }
2632
2633         public string DeleteList(string list_id)
2634         {
2635             HttpStatusCode res;
2636             var content = "";
2637
2638             try
2639             {
2640                 res = twCon.DeleteListID(this.Username, list_id, ref content);
2641             }
2642             catch(Exception ex)
2643             {
2644                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2645             }
2646
2647             var err = this.CheckStatusCode(res, content);
2648             if (err != null) return err;
2649
2650             return "";
2651         }
2652
2653         public string EditList(string list_id, string new_name, bool isPrivate, string description, ref ListElement list)
2654         {
2655             HttpStatusCode res;
2656             var content = "";
2657
2658             try
2659             {
2660                 res = twCon.UpdateListID(this.Username, list_id, new_name, isPrivate, description, ref content);
2661             }
2662             catch(Exception ex)
2663             {
2664                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2665             }
2666
2667             var err = this.CheckStatusCode(res, content);
2668             if (err != null) return err;
2669
2670             try
2671             {
2672                 var le = TwitterList.ParseJson(content);
2673                 var newList = new ListElement(le, this);
2674                 list.Description = newList.Description;
2675                 list.Id = newList.Id;
2676                 list.IsPublic = newList.IsPublic;
2677                 list.MemberCount = newList.MemberCount;
2678                 list.Name = newList.Name;
2679                 list.SubscriberCount = newList.SubscriberCount;
2680                 list.Slug = newList.Slug;
2681                 list.Nickname = newList.Nickname;
2682                 list.Username = newList.Username;
2683                 list.UserId = newList.UserId;
2684                 return "";
2685             }
2686             catch(SerializationException ex)
2687             {
2688                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2689                 return "Err:Json Parse Error(DataContractJsonSerializer)";
2690             }
2691             catch(Exception ex)
2692             {
2693                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2694                 return "Err:Invalid Json!";
2695             }
2696
2697         }
2698
2699         public string GetListMembers(string list_id, List<UserInfo> lists, ref long cursor)
2700         {
2701             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2702
2703             HttpStatusCode res;
2704             var content = "";
2705             try
2706             {
2707                 res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
2708             }
2709             catch(Exception ex)
2710             {
2711                 return "Err:" + ex.Message;
2712             }
2713
2714             var err = this.CheckStatusCode(res, content);
2715             if (err != null) return err;
2716
2717             try
2718             {
2719                 var users = TwitterUsers.ParseJson(content);
2720                 Array.ForEach<TwitterUser>(
2721                     users.Users,
2722                     u => lists.Add(new UserInfo(u)));
2723                 cursor = users.NextCursor;
2724                 return "";
2725             }
2726             catch(SerializationException ex)
2727             {
2728                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2729                 return "Err:Json Parse Error(DataContractJsonSerializer)";
2730             }
2731             catch(Exception ex)
2732             {
2733                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2734                 return "Err:Invalid Json!";
2735             }
2736         }
2737
2738         public string CreateListApi(string listName, bool isPrivate, string description)
2739         {
2740             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2741
2742             HttpStatusCode res;
2743             var content = "";
2744             try
2745             {
2746                 res = twCon.CreateLists(listName, isPrivate, description, ref content);
2747             }
2748             catch(Exception ex)
2749             {
2750                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2751             }
2752
2753             var err = this.CheckStatusCode(res, content);
2754             if (err != null) return err;
2755
2756             try
2757             {
2758                 var le = TwitterList.ParseJson(content);
2759                 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(le, this));
2760                 return "";
2761             }
2762             catch(SerializationException ex)
2763             {
2764                 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2765                 return "Err:Json Parse Error(DataContractJsonSerializer)";
2766             }
2767             catch(Exception ex)
2768             {
2769                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2770                 return "Err:Invalid Json!";
2771             }
2772         }
2773
2774         public string ContainsUserAtList(string listId, string user, ref bool value)
2775         {
2776             value = false;
2777
2778             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
2779
2780             HttpStatusCode res;
2781             var content = "";
2782
2783             try
2784             {
2785                 res = this.twCon.ShowListMember(listId, user, ref content);
2786             }
2787             catch(Exception ex)
2788             {
2789                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2790             }
2791
2792             if (res == HttpStatusCode.NotFound)
2793             {
2794                 value = false;
2795                 return "";
2796             }
2797
2798             var err = this.CheckStatusCode(res, content);
2799             if (err != null) return err;
2800
2801             try
2802             {
2803                 TwitterUser.ParseJson(content);
2804                 value = true;
2805                 return "";
2806             }
2807             catch(Exception)
2808             {
2809                 value = false;
2810                 return "";
2811             }
2812         }
2813
2814         public string AddUserToList(string listId, string user)
2815         {
2816             HttpStatusCode res;
2817             var content = "";
2818
2819             try
2820             {
2821                 res = twCon.CreateListMembers(listId, user, ref content);
2822             }
2823             catch(Exception ex)
2824             {
2825                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2826             }
2827
2828             var err = this.CheckStatusCode(res, content);
2829             if (err != null) return err;
2830
2831             return "";
2832         }
2833
2834         public string RemoveUserToList(string listId, string user)
2835         {
2836             HttpStatusCode res;
2837             var content = "";
2838
2839             try
2840             {
2841                 res = twCon.DeleteListMembers(listId, user, ref content);
2842             }
2843             catch(Exception ex)
2844             {
2845                 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
2846             }
2847
2848             var err = this.CheckStatusCode(res, content);
2849             if (err != null) return err;
2850
2851             return "";
2852         }
2853
2854         private class range
2855         {
2856             public int fromIndex { get; set; }
2857             public int toIndex { get; set; }
2858             public range(int fromIndex, int toIndex)
2859             {
2860                 this.fromIndex = fromIndex;
2861                 this.toIndex = toIndex;
2862             }
2863         }
2864         public async Task<string> CreateHtmlAnchorAsync(string Text, List<string> AtList, Dictionary<string, string> media)
2865         {
2866             if (Text == null) return null;
2867             var retStr = Text.Replace("&gt;", "<<<<<tweenだいなり>>>>>").Replace("&lt;", "<<<<<tweenしょうなり>>>>>");
2868             //uriの正規表現
2869             //const string url_valid_domain = "(?<domain>(?:[^\p{P}\s][\.\-_](?=[^\p{P}\s])|[^\p{P}\s]){1,}\.[a-z]{2,}(?::[0-9]+)?)"
2870             //const string url_valid_general_path_chars = "[a-z0-9!*';:=+$/%#\[\]\-_,~]"
2871             //const string url_balance_parens = "(?:\(" + url_valid_general_path_chars + "+\))"
2872             //const string url_valid_url_path_ending_chars = "(?:[a-z0-9=_#/\-\+]+|" + url_balance_parens + ")"
2873             //const string pth = "(?:" + url_balance_parens +
2874             //    "|@" + url_valid_general_path_chars + "+/" +
2875             //    "|[.,]?" + url_valid_general_path_chars + "+" +
2876             //    ")"
2877             //const string pth2 = "(/(?:" +
2878             //    pth + "+" + url_valid_url_path_ending_chars + "|" +
2879             //    pth + "+" + url_valid_url_path_ending_chars + "?|" +
2880             //    url_valid_url_path_ending_chars +
2881             //    ")?)?"
2882             //const string qry = "(?<query>\?[a-z0-9!*'();:&=+$/%#\[\]\-_.,~]*[a-z0-9_&=#])?"
2883             //const string rgUrl = "(?<before>(?:[^\""':!=#]|^|\:/))" +
2884             //                            "(?<url>(?<protocol>https?://)" +
2885             //                            url_valid_domain +
2886             //                            pth2 +
2887             //                            qry +
2888             //                            ")"
2889             //const string rgUrl = "(?<before>(?:[^\""':!=#]|^|\:/))" +
2890             //                            "(?<url>(?<protocol>https?://|www\.)" +
2891             //                            url_valid_domain +
2892             //                            pth2 +
2893             //                            qry +
2894             //                            ")"
2895             //絶対パス表現のUriをリンクに置換
2896             retStr = await new Regex(rgUrl, RegexOptions.IgnoreCase).ReplaceAsync(retStr, async mu =>
2897             {
2898                 var sb = new StringBuilder(mu.Result("${before}<a href=\""));
2899                 //if (mu.Result("${protocol}").StartsWith("w", StringComparison.OrdinalIgnoreCase))
2900                 //    sb.Append("http://");
2901                 //}
2902                 var url = mu.Result("${url}");
2903                 var title = await ShortUrl.Instance.ExpandUrlAsync(url);
2904                 sb.Append(url + "\" title=\"" + MyCommon.ConvertToReadableUrl(title) + "\">").Append(url).Append("</a>");
2905                 if (media != null && !media.ContainsKey(url)) media.Add(url, title);
2906                 return sb.ToString();
2907             });
2908
2909             //@先をリンクに置換(リスト)
2910             retStr = Regex.Replace(retStr,
2911                                    @"(^|[^a-zA-Z0-9_/])([@@]+)([a-zA-Z0-9_]{1,20}/[a-zA-Z][a-zA-Z0-9\p{IsLatin-1Supplement}\-]{0,79})",
2912                                    "$1$2<a href=\"/$3\">$3</a>");
2913
2914             var m = Regex.Match(retStr, "(^|[^a-zA-Z0-9_])[@@]([a-zA-Z0-9_]{1,20})");
2915             while (m.Success)
2916             {
2917                 if (!AtList.Contains(m.Result("$2").ToLower())) AtList.Add(m.Result("$2").ToLower());
2918                 m = m.NextMatch();
2919             }
2920             //@先をリンクに置換
2921             retStr = Regex.Replace(retStr,
2922                                    "(^|[^a-zA-Z0-9_/])([@@])([a-zA-Z0-9_]{1,20})",
2923                                    "$1$2<a href=\"/$3\">$3</a>");
2924
2925             //ハッシュタグを抽出し、リンクに置換
2926             var anchorRange = new List<range>();
2927             for (int i = 0; i < retStr.Length; i++)
2928             {
2929                 var index = retStr.IndexOf("<a ", i);
2930                 if (index > -1 && index < retStr.Length)
2931                 {
2932                     i = index;
2933                     var toIndex = retStr.IndexOf("</a>", index);
2934                     if (toIndex > -1)
2935                     {
2936                         anchorRange.Add(new range(index, toIndex + 3));
2937                         i = toIndex;
2938                     }
2939                 }
2940             }
2941             //retStr = Regex.Replace(retStr,
2942             //                       "(^|[^a-zA-Z0-9/&])([##])([0-9a-zA-Z_]*[a-zA-Z_]+[a-zA-Z0-9_\xc0-\xd6\xd8-\xf6\xf8-\xff]*)",
2943             //                       new MatchEvaluator(Function(mh As Match)
2944             //                                              foreach (var rng in anchorRange)
2945             //                                              {
2946             //                                                  if (mh.Index >= rng.fromIndex &&
2947             //                                                   mh.Index <= rng.toIndex) return mh.Result("$0");
2948             //                                              }
2949             //                                              if (IsNumeric(mh.Result("$3"))) return mh.Result("$0");
2950             //                                              lock (LockObj)
2951             //                                              {
2952             //                                                  _hashList.Add("#" + mh.Result("$3"))
2953             //                                              }
2954             //                                              return mh.Result("$1") + "<a href=\"" + _protocol + "twitter.com/search?q=%23" + mh.Result("$3") + "\">" + mh.Result("$2$3") + "</a>";
2955             //                                          }),
2956             //                                      RegexOptions.IgnoreCase)
2957             retStr = Regex.Replace(retStr,
2958                                    HASHTAG,
2959                                    new MatchEvaluator(mh =>
2960                                                       {
2961                                                           foreach (var rng in anchorRange)
2962                                                           {
2963                                                               if (mh.Index >= rng.fromIndex &&
2964                                                                mh.Index <= rng.toIndex) return mh.Result("$0");
2965                                                           }
2966                                                           lock (LockObj)
2967                                                           {
2968                                                               _hashList.Add("#" + mh.Result("$3"));
2969                                                           }
2970                                                           return mh.Result("$1") + "<a href=\"https://twitter.com/search?q=%23" + mh.Result("$3") + "\">" + mh.Result("$2$3") + "</a>";
2971                                                       }),
2972                                                   RegexOptions.IgnoreCase);
2973
2974
2975             retStr = Regex.Replace(retStr, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
2976
2977             retStr = retStr.Replace("<<<<<tweenだいなり>>>>>", "&gt;").Replace("<<<<<tweenしょうなり>>>>>", "&lt;");
2978
2979             //retStr = AdjustHtml(ShortUrl.Resolve(PreProcessUrl(retStr), true)) //IDN置換、短縮Uri解決、@リンクを相対→絶対にしてtarget属性付与
2980             retStr = AdjustHtml(PreProcessUrl(retStr)); //IDN置換、短縮Uri解決、@リンクを相対→絶対にしてtarget属性付与
2981             return retStr;
2982         }
2983
2984         public async Task<string> CreateHtmlAnchorAsync(string text, List<string> AtList, TwitterEntities entities, List<string> media)
2985         {
2986             if (entities != null)
2987             {
2988                 if (entities.Urls != null)
2989                 {
2990                     foreach (var ent in entities.Urls)
2991                     {
2992                         ent.ExpandedUrl = await ShortUrl.Instance.ExpandUrlAsync(ent.ExpandedUrl)
2993                             .ConfigureAwait(false);
2994
2995                         if (media != null && !media.Contains(ent.ExpandedUrl))
2996                             media.Add(ent.ExpandedUrl);
2997                     }
2998                 }
2999                 if (entities.Hashtags != null)
3000                 {
3001                     lock (this.LockObj)
3002                     {
3003                         this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
3004                     }
3005                 }
3006                 if (entities.UserMentions != null)
3007                 {
3008                     foreach (var ent in entities.UserMentions)
3009                     {
3010                         var screenName = ent.ScreenName.ToLower();
3011                         if (!AtList.Contains(screenName))
3012                             AtList.Add(screenName);
3013                     }
3014                 }
3015                 if (entities.Media != null)
3016                 {
3017                     if (media != null)
3018                     {
3019                         foreach (var ent in entities.Media)
3020                         {
3021                             if (!media.Contains(ent.MediaUrl))
3022                                 media.Add(ent.MediaUrl);
3023                         }
3024                     }
3025                 }
3026             }
3027
3028             text = TweetFormatter.AutoLinkHtml(text, entities);
3029
3030             text = Regex.Replace(text, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
3031             text = PreProcessUrl(text); //IDN置換
3032
3033             return text;
3034         }
3035
3036         [Obsolete]
3037         public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<string> media)
3038         {
3039             return this.CreateHtmlAnchorAsync(text, AtList, entities, media).Result;
3040         }
3041
3042         //Source整形
3043         private void CreateSource(PostClass post)
3044         {
3045             if (string.IsNullOrEmpty(post.Source)) return;
3046
3047             if (post.Source.StartsWith("<"))
3048             {
3049                 if (!post.Source.Contains("</a>"))
3050                 {
3051                     post.Source += "</a>";
3052                 }
3053                 var mS = Regex.Match(post.Source, ">(?<source>.+)<");
3054                 if (mS.Success)
3055                 {
3056                     post.SourceHtml = ShortUrl.Instance.ExpandUrlHtml(PreProcessUrl(post.Source));
3057                     post.Source = WebUtility.HtmlDecode(mS.Result("${source}"));
3058                 }
3059                 else
3060                 {
3061                     post.Source = "";
3062                     post.SourceHtml = "";
3063                 }
3064             }
3065             else
3066             {
3067                 if (post.Source == "web")
3068                 {
3069                     post.SourceHtml = Properties.Resources.WebSourceString;
3070                 }
3071                 else if (post.Source == "Keitai Mail")
3072                 {
3073                     post.SourceHtml = Properties.Resources.KeitaiMailSourceString;
3074                 }
3075                 else
3076                 {
3077                     post.SourceHtml = post.Source;
3078                 }
3079             }
3080         }
3081
3082         public TwitterApiStatus GetInfoApi()
3083         {
3084             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
3085
3086             if (MyCommon._endingFlag) return null;
3087
3088             HttpStatusCode res;
3089             var content = "";
3090             try
3091             {
3092                 res = twCon.RateLimitStatus(ref content);
3093             }
3094             catch (Exception)
3095             {
3096                 this.ResetApiStatus();
3097                 return null;
3098             }
3099
3100             var err = this.CheckStatusCode(res, content);
3101             if (err != null) return null;
3102
3103             try
3104             {
3105                 MyCommon.TwitterApiInfo.UpdateFromJson(content);
3106                 return MyCommon.TwitterApiInfo;
3107             }
3108             catch (Exception ex)
3109             {
3110                 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
3111                 MyCommon.TwitterApiInfo.Reset();
3112                 return null;
3113             }
3114         }
3115
3116         /// <summary>
3117         /// ブロック中のユーザーを更新します
3118         /// </summary>
3119         /// <exception cref="WebApiException"/>
3120         public void RefreshBlockIds()
3121         {
3122             if (MyCommon._endingFlag) return;
3123
3124             var cursor = -1L;
3125             var newBlockIds = new List<long>();
3126             do
3127             {
3128                 var ret = this.GetBlockIdsApi(cursor);
3129                 newBlockIds.AddRange(ret.Ids);
3130                 cursor = ret.NextCursor;
3131             } while (cursor != 0);
3132
3133             newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
3134
3135             TabInformations.GetInstance().BlockIds = newBlockIds;
3136         }
3137
3138         public TwitterIds GetBlockIdsApi(long cursor)
3139         {
3140             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
3141                 throw new WebApiException("AccountState invalid");
3142
3143             HttpStatusCode res;
3144             var content = "";
3145             try
3146             {
3147                 res = twCon.GetBlockUserIds(ref content, cursor);
3148             }
3149             catch(Exception e)
3150             {
3151                 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
3152             }
3153
3154             var err = this.CheckStatusCode(res, content);
3155             if (err != null)
3156                 throw new WebApiException(err, content);
3157
3158             try
3159             {
3160                 return TwitterIds.ParseJson(content);
3161             }
3162             catch(SerializationException e)
3163             {
3164                 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
3165                 MyCommon.TraceOut(ex);
3166                 throw ex;
3167             }
3168             catch(Exception e)
3169             {
3170                 var ex = new WebApiException("Err:Invalid Json!", content, e);
3171                 MyCommon.TraceOut(ex);
3172                 throw ex;
3173             }
3174         }
3175
3176         /// <summary>
3177         /// ミュート中のユーザーIDを更新します
3178         /// </summary>
3179         /// <exception cref="WebApiException"/>
3180         public async Task RefreshMuteUserIdsAsync()
3181         {
3182             if (MyCommon._endingFlag) return;
3183
3184             var ids = await TwitterIds.GetAllItemsAsync(this.GetMuteUserIdsApiAsync)
3185                 .ConfigureAwait(false);
3186
3187             TabInformations.GetInstance().MuteUserIds = ids.ToList();
3188         }
3189
3190         public async Task<TwitterIds> GetMuteUserIdsApiAsync(long cursor)
3191         {
3192             var content = "";
3193
3194             try
3195             {
3196                 var res = await Task.Run(() => twCon.GetMuteUserIds(ref content, cursor))
3197                     .ConfigureAwait(false);
3198
3199                 var err = this.CheckStatusCode(res, content);
3200                 if (err != null)
3201                     throw new WebApiException(err, content);
3202
3203                 return TwitterIds.ParseJson(content);
3204             }
3205             catch (WebException ex)
3206             {
3207                 var ex2 = new WebApiException("Err:" + ex.Message, ex);
3208                 MyCommon.TraceOut(ex2);
3209                 throw ex2;
3210             }
3211             catch (SerializationException ex)
3212             {
3213                 var ex2 = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
3214                 MyCommon.TraceOut(ex2);
3215                 throw ex2;
3216             }
3217         }
3218
3219         public string[] GetHashList()
3220         {
3221             string[] hashArray;
3222             lock (LockObj)
3223             {
3224                 hashArray = _hashList.ToArray();
3225                 _hashList.Clear();
3226             }
3227             return hashArray;
3228         }
3229
3230         public string AccessToken
3231         {
3232             get
3233             {
3234                 return twCon.AccessToken;
3235             }
3236         }
3237
3238         public string AccessTokenSecret
3239         {
3240             get
3241             {
3242                 return twCon.AccessTokenSecret;
3243             }
3244         }
3245
3246         private string CheckStatusCode(HttpStatusCode httpStatus, string responseText,
3247             [CallerMemberName] string callerMethodName = "")
3248         {
3249             if (httpStatus == HttpStatusCode.OK)
3250             {
3251                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3252                 return null;
3253             }
3254
3255             // 404エラーの挙動が変なので無視: https://dev.twitter.com/discussions/1213
3256             if (httpStatus == HttpStatusCode.NotFound) return null;
3257
3258             if (string.IsNullOrWhiteSpace(responseText))
3259             {
3260                 if (httpStatus == HttpStatusCode.Unauthorized)
3261                     Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3262
3263                 return "Err:" + httpStatus + "(" + callerMethodName + ")";
3264             }
3265
3266             try
3267             {
3268                 var errors = TwitterError.ParseJson(responseText).Errors;
3269                 if (errors == null || !errors.Any())
3270                 {
3271                     return "Err:" + httpStatus + "(" + callerMethodName + ")";
3272                 }
3273
3274                 foreach (var error in errors)
3275                 {
3276                     if (error.Code == TwitterErrorCode.InvalidToken ||
3277                         error.Code == TwitterErrorCode.SuspendedAccount)
3278                     {
3279                         Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3280                     }
3281                 }
3282
3283                 return "Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethodName + ")";
3284             }
3285             catch (SerializationException) { }
3286
3287             return "Err:" + httpStatus + "(" + callerMethodName + ")";
3288         }
3289
3290 #region "UserStream"
3291         private string trackWord_ = "";
3292         public string TrackWord
3293         {
3294             get
3295             {
3296                 return trackWord_;
3297             }
3298             set
3299             {
3300                 trackWord_ = value;
3301             }
3302         }
3303         private bool allAtReply_ = false;
3304         public bool AllAtReply
3305         {
3306             get
3307             {
3308                 return allAtReply_;
3309             }
3310             set
3311             {
3312                 allAtReply_ = value;
3313             }
3314         }
3315
3316         public event EventHandler NewPostFromStream;
3317         public event EventHandler UserStreamStarted;
3318         public event EventHandler UserStreamStopped;
3319         public event EventHandler<PostDeletedEventArgs> PostDeleted;
3320         public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
3321         private DateTime _lastUserstreamDataReceived;
3322         private TwitterUserstream userStream;
3323
3324         public class FormattedEvent
3325         {
3326             public MyCommon.EVENTTYPE Eventtype { get; set; }
3327             public DateTime CreatedAt { get; set; }
3328             public string Event { get; set; }
3329             public string Username { get; set; }
3330             public string Target { get; set; }
3331             public Int64 Id { get; set; }
3332             public bool IsMe { get; set; }
3333         }
3334
3335         public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
3336         public List<FormattedEvent> StoredEvent
3337         {
3338             get
3339             {
3340                 return storedEvent_;
3341             }
3342             set
3343             {
3344                 storedEvent_ = value;
3345             }
3346         }
3347
3348         private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
3349         {
3350             { "favorite", MyCommon.EVENTTYPE.Favorite },
3351             { "unfavorite", MyCommon.EVENTTYPE.Unfavorite },
3352             { "follow", MyCommon.EVENTTYPE.Follow },
3353             { "list_member_added", MyCommon.EVENTTYPE.ListMemberAdded },
3354             { "list_member_removed", MyCommon.EVENTTYPE.ListMemberRemoved },
3355             { "block", MyCommon.EVENTTYPE.Block },
3356             { "unblock", MyCommon.EVENTTYPE.Unblock },
3357             { "user_update", MyCommon.EVENTTYPE.UserUpdate },
3358             { "deleted", MyCommon.EVENTTYPE.Deleted },
3359             { "list_created", MyCommon.EVENTTYPE.ListCreated },
3360             { "list_destroyed", MyCommon.EVENTTYPE.ListDestroyed },
3361             { "list_updated", MyCommon.EVENTTYPE.ListUpdated },
3362             { "unfollow", MyCommon.EVENTTYPE.Unfollow },
3363             { "list_user_subscribed", MyCommon.EVENTTYPE.ListUserSubscribed },
3364             { "list_user_unsubscribed", MyCommon.EVENTTYPE.ListUserUnsubscribed },
3365             { "mute", MyCommon.EVENTTYPE.Mute },
3366             { "unmute", MyCommon.EVENTTYPE.Unmute }
3367         };
3368
3369         public bool IsUserstreamDataReceived
3370         {
3371             get
3372             {
3373                 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
3374             }
3375         }
3376
3377         private void userStream_StatusArrived(string line)
3378         {
3379             this._lastUserstreamDataReceived = DateTime.Now;
3380             if (string.IsNullOrEmpty(line)) return;
3381
3382             if (line.First() != '{' || line.Last() != '}')
3383             {
3384                 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
3385                 return;
3386             }
3387
3388             var isDm = false;
3389
3390             try
3391             {
3392                 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
3393                 {
3394                     var xElm = XElement.Load(jsonReader);
3395                     if (xElm.Element("friends") != null)
3396                     {
3397                         Debug.WriteLine("friends");
3398                         return;
3399                     }
3400                     else if (xElm.Element("delete") != null)
3401                     {
3402                         Debug.WriteLine("delete");
3403                         Int64 id;
3404                         if (xElm.Element("delete").Element("direct_message") != null &&
3405                             xElm.Element("delete").Element("direct_message").Element("id") != null)
3406                         {
3407                             id = 0;
3408                             long.TryParse(xElm.Element("delete").Element("direct_message").Element("id").Value, out id);
3409
3410                             if (this.PostDeleted != null)
3411                                 this.PostDeleted(this, new PostDeletedEventArgs(id));
3412                         }
3413                         else if (xElm.Element("delete").Element("status") != null &&
3414                             xElm.Element("delete").Element("status").Element("id") != null)
3415                         {
3416                             id = 0;
3417                             long.TryParse(xElm.Element("delete").Element("status").Element("id").Value, out id);
3418
3419                             if (this.PostDeleted != null)
3420                                 this.PostDeleted(this, new PostDeletedEventArgs(id));
3421                         }
3422                         else
3423                         {
3424                             MyCommon.TraceOut("delete:" + line);
3425                             return;
3426                         }
3427                         for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
3428                         {
3429                             var sEvt = this.StoredEvent[i];
3430                             if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
3431                             {
3432                                 this.StoredEvent.RemoveAt(i);
3433                             }
3434                         }
3435                         return;
3436                     }
3437                     else if (xElm.Element("limit") != null)
3438                     {
3439                         Debug.WriteLine(line);
3440                         return;
3441                     }
3442                     else if (xElm.Element("event") != null)
3443                     {
3444                         Debug.WriteLine("event: " + xElm.Element("event").Value);
3445                         CreateEventFromJson(line);
3446                         return;
3447                     }
3448                     else if (xElm.Element("direct_message") != null)
3449                     {
3450                         Debug.WriteLine("direct_message");
3451                         isDm = true;
3452                     }
3453                     else if (xElm.Element("scrub_geo") != null)
3454                     {
3455                         try
3456                         {
3457                             TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
3458                                                                         long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
3459                         }
3460                         catch(Exception)
3461                         {
3462                             MyCommon.TraceOut("scrub_geo:" + line);
3463                         }
3464                         return;
3465                     }
3466                 }
3467
3468                 if (isDm)
3469                 {
3470                     CreateDirectMessagesFromJson(line, MyCommon.WORKERTYPE.UserStream, false);
3471                 }
3472                 else
3473                 {
3474                     long dummy = 0;
3475                     CreatePostsFromJson("[" + line + "]", MyCommon.WORKERTYPE.Timeline, null, false, 0, ref dummy);
3476                 }
3477             }
3478             catch(NullReferenceException)
3479             {
3480                 MyCommon.TraceOut("NullRef StatusArrived: " + line);
3481             }
3482
3483             if (this.NewPostFromStream != null)
3484                 this.NewPostFromStream(this, EventArgs.Empty);
3485         }
3486
3487         private void CreateEventFromJson(string content)
3488         {
3489             TwitterStreamEvent eventData = null;
3490             try
3491             {
3492                 eventData = TwitterStreamEvent.ParseJson(content);
3493             }
3494             catch(SerializationException ex)
3495             {
3496                 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
3497             }
3498             catch(Exception ex)
3499             {
3500                 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
3501             }
3502
3503             var evt = new FormattedEvent();
3504             evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
3505             evt.Event = eventData.Event;
3506             evt.Username = eventData.Source.ScreenName;
3507             evt.IsMe = evt.Username.ToLower().Equals(this.Username.ToLower());
3508
3509             MyCommon.EVENTTYPE eventType;
3510             eventTable.TryGetValue(eventData.Event, out eventType);
3511             evt.Eventtype = eventType;
3512
3513             switch (eventData.Event)
3514             {
3515                 case "access_revoked":
3516                 case "access_unrevoked":
3517                 case "user_delete":
3518                 case "user_suspend":
3519                     return;
3520                 case "follow":
3521                     if (eventData.Target.ScreenName.ToLower().Equals(_uname))
3522                     {
3523                         if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
3524                     }
3525                     else
3526                     {
3527                         return;    //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
3528                     }
3529                     evt.Target = "";
3530                     break;
3531                 case "unfollow":
3532                     evt.Target = "@" + eventData.Target.ScreenName;
3533                     break;
3534                 case "favorite":
3535                 case "unfavorite":
3536                     var tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
3537                     evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
3538                     evt.Id = tweetEvent.TargetObject.Id;
3539                     if (SettingCommon.Instance.IsRemoveSameEvent)
3540                     {
3541                         if (StoredEvent.Any(ev =>
3542                                            {
3543                                                return ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target;
3544                                            })) return;
3545                     }
3546                     if (TabInformations.GetInstance().ContainsKey(tweetEvent.TargetObject.Id))
3547                     {
3548                         var post = TabInformations.GetInstance()[tweetEvent.TargetObject.Id];
3549                         if (eventData.Event == "favorite")
3550                         {
3551                             if (evt.Username.ToLower().Equals(_uname))
3552                             {
3553                                 post.IsFav = true;
3554                                 TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).Add(post.StatusId, post.IsRead, false);
3555                             }
3556                             else
3557                             {
3558                                 post.FavoritedCount++;
3559                                 if (!TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).Contains(post.StatusId))
3560                                 {
3561                                     if (SettingCommon.Instance.FavEventUnread && post.IsRead)
3562                                     {
3563                                         post.IsRead = false;
3564                                     }
3565                                     TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).Add(post.StatusId, post.IsRead, false);
3566                                 }
3567                                 else
3568                                 {
3569                                     if (SettingCommon.Instance.FavEventUnread)
3570                                     {
3571                                         TabInformations.GetInstance().SetRead(false, TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).TabName, TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites).IndexOf(post.StatusId));
3572                                     }
3573                                 }
3574                             }
3575                         }
3576                         else
3577                         {
3578                             if (evt.Username.ToLower().Equals(_uname))
3579                             {
3580                                 post.IsFav = false;
3581                             }
3582                             else
3583                             {
3584                                 post.FavoritedCount--;
3585                                 if (post.FavoritedCount < 0) post.FavoritedCount = 0;
3586                             }
3587                         }
3588                     }
3589                     break;
3590                 case "list_member_added":
3591                 case "list_member_removed":
3592                 case "list_created":
3593                 case "list_destroyed":
3594                 case "list_updated":
3595                 case "list_user_subscribed":
3596                 case "list_user_unsubscribed":
3597                     var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
3598                     evt.Target = listEvent.TargetObject.FullName;
3599                     break;
3600                 case "block":
3601                     if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
3602                     evt.Target = "";
3603                     break;
3604                 case "unblock":
3605                     if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
3606                     evt.Target = "";
3607                     break;
3608                 case "user_update":
3609                     evt.Target = "";
3610                     break;
3611                 
3612                 // Mute / Unmute
3613                 case "mute":
3614                     evt.Target = "@" + eventData.Target.ScreenName;
3615                     if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
3616                     {
3617                         TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
3618                     }
3619                     break;
3620                 case "unmute":
3621                     evt.Target = "@" + eventData.Target.ScreenName;
3622                     if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
3623                     {
3624                         TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
3625                     }
3626                     break;
3627
3628                 default:
3629                     MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
3630                     break;
3631             }
3632             this.StoredEvent.Insert(0, evt);
3633
3634             if (this.UserStreamEventReceived != null)
3635                 this.UserStreamEventReceived(this, new UserStreamEventReceivedEventArgs(evt));
3636         }
3637
3638         private void userStream_Started()
3639         {
3640             if (this.UserStreamStarted != null)
3641                 this.UserStreamStarted(this, EventArgs.Empty);
3642         }
3643
3644         private void userStream_Stopped()
3645         {
3646             if (this.UserStreamStopped != null)
3647                 this.UserStreamStopped(this, EventArgs.Empty);
3648         }
3649
3650         public bool UserStreamEnabled
3651         {
3652             get
3653             {
3654                 return userStream == null ? false : userStream.Enabled;
3655             }
3656         }
3657
3658         public void StartUserStream()
3659         {
3660             if (userStream != null)
3661             {
3662                 StopUserStream();
3663             }
3664             userStream = new TwitterUserstream(twCon);
3665             userStream.StatusArrived += userStream_StatusArrived;
3666             userStream.Started += userStream_Started;
3667             userStream.Stopped += userStream_Stopped;
3668             userStream.Start(this.AllAtReply, this.TrackWord);
3669         }
3670
3671         public void StopUserStream()
3672         {
3673             if (userStream != null) userStream.Dispose();
3674             userStream = null;
3675             if (!MyCommon._endingFlag)
3676             {
3677                 if (this.UserStreamStopped != null)
3678                     this.UserStreamStopped(this, EventArgs.Empty);
3679             }
3680         }
3681
3682         public void ReconnectUserStream()
3683         {
3684             if (userStream != null)
3685             {
3686                 this.StartUserStream();
3687             }
3688         }
3689
3690         private class TwitterUserstream : IDisposable
3691         {
3692             public event Action<string> StatusArrived;
3693             public event Action Stopped;
3694             public event Action Started;
3695             private HttpTwitter twCon;
3696
3697             private Thread _streamThread;
3698             private bool _streamActive;
3699
3700             private bool _allAtreplies = false;
3701             private string _trackwords = "";
3702
3703             public TwitterUserstream(HttpTwitter twitterConnection)
3704             {
3705                 twCon = (HttpTwitter)twitterConnection.Clone();
3706             }
3707
3708             public void Start(bool allAtReplies, string trackwords)
3709             {
3710                 this.AllAtReplies = allAtReplies;
3711                 this.TrackWords = trackwords;
3712                 _streamActive = true;
3713                 if (_streamThread != null && _streamThread.IsAlive) return;
3714                 _streamThread = new Thread(UserStreamLoop);
3715                 _streamThread.Name = "UserStreamReceiver";
3716                 _streamThread.IsBackground = true;
3717                 _streamThread.Start();
3718             }
3719
3720             public bool Enabled
3721             {
3722                 get
3723                 {
3724                     return _streamActive;
3725                 }
3726             }
3727
3728             public bool AllAtReplies
3729             {
3730                 get
3731                 {
3732                     return _allAtreplies;
3733                 }
3734                 set
3735                 {
3736                     _allAtreplies = value;
3737                 }
3738             }
3739
3740             public string TrackWords
3741             {
3742                 get
3743                 {
3744                     return _trackwords;
3745                 }
3746                 set
3747                 {
3748                     _trackwords = value;
3749                 }
3750             }
3751
3752             private void UserStreamLoop()
3753             {
3754                 var sleepSec = 0;
3755                 do
3756                 {
3757                     Stream st = null;
3758                     StreamReader sr = null;
3759                     try
3760                     {
3761                         if (!MyCommon.IsNetworkAvailable())
3762                         {
3763                             sleepSec = 30;
3764                             continue;
3765                         }
3766
3767                         if (Started != null)
3768                         {
3769                             Started();
3770                         }
3771                         var res = twCon.UserStream(ref st, _allAtreplies, _trackwords, Networking.GetUserAgentString());
3772
3773                         switch (res)
3774                         {
3775                             case HttpStatusCode.OK:
3776                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3777                                 break;
3778                             case HttpStatusCode.Unauthorized:
3779                                 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3780                                 sleepSec = 120;
3781                                 continue;
3782                         }
3783
3784                         if (st == null)
3785                         {
3786                             sleepSec = 30;
3787                             //MyCommon.TraceOut("Stop:stream is null")
3788                             continue;
3789                         }
3790
3791                         sr = new StreamReader(st);
3792
3793                         while (_streamActive && !sr.EndOfStream && Twitter.AccountState == MyCommon.ACCOUNT_STATE.Valid)
3794                         {
3795                             if (StatusArrived != null)
3796                             {
3797                                 StatusArrived(sr.ReadLine());
3798                             }
3799                             //this.LastTime = Now;
3800                         }
3801
3802                         if (sr.EndOfStream || Twitter.AccountState == MyCommon.ACCOUNT_STATE.Invalid)
3803                         {
3804                             sleepSec = 30;
3805                             //MyCommon.TraceOut("Stop:EndOfStream")
3806                             continue;
3807                         }
3808                         break;
3809                     }
3810                     catch(WebException ex)
3811                     {
3812                         if (ex.Status == WebExceptionStatus.Timeout)
3813                         {
3814                             sleepSec = 30;                        //MyCommon.TraceOut("Stop:Timeout")
3815                         }
3816                         else if (ex.Response != null && (int)((HttpWebResponse)ex.Response).StatusCode == 420)
3817                         {
3818                             //MyCommon.TraceOut("Stop:Connection Limit")
3819                             break;
3820                         }
3821                         else
3822                         {
3823                             sleepSec = 30;
3824                             //MyCommon.TraceOut("Stop:WebException " + ex.Status.ToString())
3825                         }
3826                     }
3827                     catch(ThreadAbortException)
3828                     {
3829                         break;
3830                     }
3831                     catch(IOException)
3832                     {
3833                         sleepSec = 30;
3834                         //MyCommon.TraceOut("Stop:IOException with Active." + Environment.NewLine + ex.Message)
3835                     }
3836                     catch(ArgumentException ex)
3837                     {
3838                         //System.ArgumentException: ストリームを読み取れませんでした。
3839                         //サーバー側もしくは通信経路上で切断された場合?タイムアウト頻発後発生
3840                         sleepSec = 30;
3841                         MyCommon.TraceOut(ex, "Stop:ArgumentException");
3842                     }
3843                     catch(Exception ex)
3844                     {
3845                         MyCommon.TraceOut("Stop:Exception." + Environment.NewLine + ex.Message);
3846                         MyCommon.ExceptionOut(ex);
3847                         sleepSec = 30;
3848                     }
3849                     finally
3850                     {
3851                         if (_streamActive)
3852                         {
3853                             if (Stopped != null)
3854                             {
3855                                 Stopped();
3856                             }
3857                         }
3858                         twCon.RequestAbort();
3859                         if (sr != null) sr.Close();
3860                         if (sleepSec > 0)
3861                         {
3862                             var ms = 0;
3863                             while (_streamActive && ms < sleepSec * 1000)
3864                             {
3865                                 Thread.Sleep(500);
3866                                 ms += 500;
3867                             }
3868                         }
3869                         sleepSec = 0;
3870                     }
3871                 } while (this._streamActive);
3872
3873                 if (_streamActive)
3874                 {
3875                     if (Stopped != null)
3876                     {
3877                         Stopped();
3878                     }
3879                 }
3880                 MyCommon.TraceOut("Stop:EndLoop");
3881             }
3882
3883 #region "IDisposable Support"
3884             private bool disposedValue; // 重複する呼び出しを検出するには
3885
3886             // IDisposable
3887             protected virtual void Dispose(bool disposing)
3888             {
3889                 if (!this.disposedValue)
3890                 {
3891                     if (disposing)
3892                     {
3893                         _streamActive = false;
3894                         if (_streamThread != null && _streamThread.IsAlive)
3895                         {
3896                             _streamThread.Abort();
3897                         }
3898                     }
3899                 }
3900                 this.disposedValue = true;
3901             }
3902
3903             //protected Overrides void Finalize()
3904             //{
3905             //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3906             //    Dispose(false)
3907             //    MyBase.Finalize()
3908             //}
3909
3910             // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3911             public void Dispose()
3912             {
3913                 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3914                 Dispose(true);
3915                 GC.SuppressFinalize(this);
3916             }
3917 #endregion
3918
3919         }
3920 #endregion
3921
3922 #region "IDisposable Support"
3923         private bool disposedValue; // 重複する呼び出しを検出するには
3924
3925         // IDisposable
3926         protected virtual void Dispose(bool disposing)
3927         {
3928             if (!this.disposedValue)
3929             {
3930                 if (disposing)
3931                 {
3932                     this.StopUserStream();
3933                 }
3934             }
3935             this.disposedValue = true;
3936         }
3937
3938         //protected Overrides void Finalize()
3939         //{
3940         //    // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3941         //    Dispose(false)
3942         //    MyBase.Finalize()
3943         //}
3944
3945         // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3946         public void Dispose()
3947         {
3948             // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3949             Dispose(true);
3950             GC.SuppressFinalize(this);
3951         }
3952 #endregion
3953     }
3954
3955     public class PostDeletedEventArgs : EventArgs
3956     {
3957         public long StatusId
3958         {
3959             get { return this.statusId; }
3960         }
3961
3962         private readonly long statusId;
3963
3964         public PostDeletedEventArgs(long statusId)
3965         {
3966             this.statusId = statusId;
3967         }
3968     }
3969
3970     public class UserStreamEventReceivedEventArgs : EventArgs
3971     {
3972         public Twitter.FormattedEvent EventData
3973         {
3974             get { return this.eventData; }
3975         }
3976
3977         private readonly Twitter.FormattedEvent eventData;
3978
3979         public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
3980         {
3981             this.eventData = eventData;
3982         }
3983     }
3984 }