OSDN Git Service

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