//【登録場所】 "V2C\script\system\threadld.js"
//【内容】threadld.jsのまとめ
//【備考】threadUpdatedとpanelCreatedの{}内で、行頭のコメント「//」を、利用する行ごとに解除することで、有効になります。
//【更新日】2013/11/24 guroNGの追加
//【スクリプト】
// ----- 次の行から -----
/* スレッドの更新が完了した時に実行するスクリプト */
function threadUpdated(th, cx) {
/* 機能を有効にしたい場合、下の各行頭//を削除してください */
// egnoreNewNGRes(th, cx); //新着がNGのみの場合、未読状態を解除
redirectNewsThread(th, cx); //速報headlineスレを開くと本スレにリダイレクト ※パーミッションにSを追加してください ※速報headlineスレのログは削除されません。
redirectLiveThread(th, cx); //テレビ番組欄のスレを開くと一番勢いのあるスレッド(恐らく本スレ)にリダイレクト ※パーミッションにSを追加してください ※テレビ番組欄スレのログは削除されません。
idConditionalNG(th, cx); //閾値以上書き込みIDをNG判定(デフォルトはニュー速と嫌儲で有効)
// checkNewTanpatsuNG(th, cx); //新着レスを対象に単発IDのレスを非表示にする。(デフォルトは実況カテゴリで有効)
guroNG(th, cx); //グロアンカーのついたレスが画像リンクを持っていた場合透明NGに指定
}
/* レス表示タブを作成した時に実行するスクリプト */
function panelCreated(th) {
/* 機能を有効にしたい場合、下の各行頭//を削除してください */
// openWritePanel(th); //開いたスレが2chの場合、書き込みパネルを自動で開く
// openFixedColumn(th); //常に特定のカラムで開く(デフォルトは0カラム)
// groupThreadPanel(th); //2カラムの状態で、特定の板(デフォルトは実況カテゴリとTwitter)を特定のカラム(デフォルトは0カラム)で開く
}
/* 関数名を添えてスクリプトコンソール出力
使用例 printFunc(arguments.callee, "要素1は「{0}」です。", 100); */
function printFunc(func, format /*, ...*/)
{
var args = arguments;
var message = format.replace(/\{(\d)\}/g, function(m, c) { return args[parseInt(c) + 2]; });
if (func && (typeof func == 'function')) {
v2c.println("[threadld.js : " + func.name + "()] " + message);
} else {
v2c.println("[threadld.js]" + message);
}
}
/* ここから各機能 */
function egnoreNewNGRes(th, cx) {
var newResi = th.localResCount - cx.numNewRes;
for (var i = newResi; i < th.localResCount; i++) {
var res = th.getRes(i);
if (res && !res.ng) {
return;
}
}
th.resetUnread();
}
function redirectLiveThread(th, cx) {
// ―┤設定項目ここから├―――――――――――――――――――――――――――――――――――――――――――
// [設定項目] スレタイから番組名を除外した部分を対象に以下の正規表現でマッチした場合除外する。
var DENY_WORDS = new RegExp("(マターリ|マターリ|酒|sage)", "i");
// [設定項目] スレと一緒に板一覧を開く場合はture開かない場合はfalse
var BOARDVIEW = true;
// [設定項目/BOARDVIEW=trueの時のみ有効] スレのDAT落ち扱いを回避するためにスレ取得開始まで一定時間(ミリ秒単位)待つ (スレ一欄と同期が取れない為。スレ一欄が更新完了するまでの時間を入力)
var THUPDATE_WAIT = 500;
// [設定項目] スクリプトコンソールに比較文字列を出力する(一致具合検証用) 出力する場合はtrueしない場合はfalse
var OUTPUT_COMPARE = false;
// [設定項目] 比較関数の閾値。この値以上のスコアになったスレは除外する
var threshold = 16;
// [設定項目] 番組名とスレタイに比較前に補正を掛けて一致しやすくする
var PRE_FUNC = (function(name) {
var ret = { 'value' : '', 'deny' : '' };
//カッコを削除(DEBY_WORDSの場合は残す
var tname = name.replace(/[\[\(\<【({<[《〔][^\]\)\>】)}」』>]》〕]+[\]\)\>】)}>]》〕]/g, ''); // カッコの種類の「『 はタイトルに使われる場合があるので除外
if (tname.length < name.length && DENY_WORDS.test(name)) {
ret.deny = RegExp.$1;
}
name = tname;
//全角英数字を半角に
name = name.replace(/[!-~]/g, function(s) {
return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
});
//半角英数字をと日本語以外は削除。小文字を大文字に
name = name.replace(/[^一-龠ぁ-んァ-ヴa-zA-Z0-9\r\n]/ig, '').toUpperCase();
var hanKana = ['ガ', 'ギ', 'グ', 'ゲ', 'ゴ', 'ザ', 'ジ', 'ズ', 'ゼ', 'ゾ', 'ダ', 'ヂ', 'ヅ', 'デ', 'ド', 'バ', 'パ', 'ビ', 'ピ', 'ブ', 'プ', 'ベ', 'ペ', 'ボ', 'ポ', 'ヴ',
'ァ', 'ア', 'ィ', 'イ', 'ゥ', 'ウ', 'ェ', 'エ', 'ォ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ', 'タ', 'チ', 'ッ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ',
'ホ', 'マ', 'ミ', 'ム', 'メ', 'モ', 'ャ', 'ヤ', 'ュ', 'ユ', 'ョ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ワ', 'ヲ', 'ン', '。', '「', '」', '、', '・', 'ー', '゙', '゚'];
var zenKana = 'ガギグゲゴザジズゼゾダヂヅデドバパビピブプベペボポヴァアィイゥウェエォオカキクケコサシスセソタチッツテトナニヌネノハヒフヘホマミムメモャヤュユョヨラリルレロワワヲン。「」、・ー゛゜';
var zenHira = 'がぎぐげござじずぜぞだぢづでどばぱびぴぶぷべぺぼぽゔぁあぃいぅうぇえぉおかきくけこさしすせそたちっつてとなにぬねのはひふへほまみむめもゃやゅゆょよらりるれろわわをん。「」、・ー゛゜';
//全角カタカナひらがなを半角カタカナに
name = name.replace(/[ぁ-んァ-ヴ「」、・ー゛゜]/g, function(s) {
var n = zenKana.indexOf(s);
if (n != -1) { return hanKana[n]; }
n = zenHira.indexOf(s);
if (n != -1) { return hanKana[n]; }
return '';
});
ret.value = name;
return ret;
});
// ――――――――――――――――――――――――――――――――――――――――――――――――――――――
if (!cx.error && th.board.key.startsWith('tv2chwiki')) {
var tmp = th.getRes(0);
var bd = v2c.getBoard(tmp.links[0]);
if (!bd) { printFunc(arguments.callee, "{0}の取得に失敗", bd); return false; }
tmp = String(tmp.message).split('\n');
if ((!tmp) || tmp.length <= 0) { printFunc(arguments.callee, "{0}の>>1の本文の取得に失敗", th); return false; }
// ヒット率を上げるために全角スペースで区切ってループ判定(その分誤判定も増える。元のロジックに戻した場合は見つからなかったで終わり)
var rawtitle = tmp[1];
var ss = v2c.readURL(bd.url + 'subject.txt');
if (v2c.interrupted) { return false; }
if (!ss) { printFunc(arguments.callee, "{0}の取得に失敗", bd); return false; }
var lines = ss.split('\n');
var now = Math.floor((new Date()).getTime() / 1000);
var maxmatches = [];
printFunc(arguments.callee, "【リダイレクト先の検索開始】番組名:{0}", rawtitle);
var title = PRE_FUNC(rawtitle);
for (var i = 0; i < lines.length; i++) {
if (/^(\d+)\.dat<>(.+) \((\d+)\)/.test(lines[i])) {
var current = RegExp.$1;
var name = RegExp.$2;
var resnum = RegExp.$3;
var speed = parseInt(resnum) / ((now - parseInt(current)) / 86400)
tmp = PRE_FUNC(name);
if (OUTPUT_COMPARE) {
v2c.println(title.value + '\t=\t' + tmp.value);
}
if (tmp.deny) {
printFunc(arguments.callee, "DENY_WORDS「{1}」にヒットしたので「{0}」を除外します。", tmp.deny, name);
continue;
}
var score = levenshtein(title.value, tmp.value);
if (score >= threshold) {
maxmatches.push({'speed': speed, key: current, 'title': name, 'resnum': parseInt(resnum), 'score' : score});
}
}
}
if (maxmatches.length > 0) {
// 1001に達していないなるべく古いスレを優先する。見つからない場合は1001しかない=新スレが立ってないってことで最も新しいスレを返す
// スレ立て時間昇順
maxmatches.sort(function(a, b) {
if (a.score === b.score) { return parseInt(a.key) - parseInt(b.key); }
else return a.score - b.score;
});
var max = maxmatches[maxmatches.length - 1]; // 最後に立ったスレを入れとく
for (var i = 0; i < maxmatches.length; i++) {
if (maxmatches[i].resnum == 1001) { continue; }
max = maxmatches[i];
break;
}
printFunc(arguments.callee, "【検索終了。スレッドの取得を試みます】スレタイ名:{0} ", max.title);
var u = new java.net.URL('http://' + bd.url.getHost() + '/test/read.cgi/' + bd.key + '/' + max.key + '/');
if (BOARDVIEW) {
v2c.openURL(bd.url);
java.lang.Thread.sleep(THUPDATE_WAIT);
v2c.openURL(u, true, false, false);
return true;
} else {
var th2 = bd.getThread(max.key, bd.url, max.title, max.resnum);
if (th2) {
th2.open(true, false, false);
return true;
} else {
printFunc(arguments.callee, "【スレッドの取得に失敗したのでスレ一覧を開きます】番組名 : {0}", rawtitle);
v2c.openURL(bd.url);
}
}
} else {
printFunc(arguments.callee, "【スレッドを見つけられなかったのでスレ一覧を開きます】番組名 : {0}", rawtitle);
v2c.openURL(bd.url);
}
}
}
// 文字列の類似比較用距離関数
function levenshtein(s1, s2) {
// http://k...content-available-to-author-only...d.net
// + original by: Carlos R. L. Rodrigues (http://w...content-available-to-author-only...l.com)
// + bugfixed by: Onno Marsman
// + revised by: Andrea Giammarchi (http://w...content-available-to-author-only...t.com)
// + reimplemented by: Brett Zamir (http://b...content-available-to-author-only...r.me)
// + reimplemented by: Alexander M Beedie
// * example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
// * returns 1: 3
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len;
}
if (s2_len === 0) {
return s1_len;
}
// BEGIN STATIC
var split = false;
try{
split=!('0')[0];
} catch (e){
split=true; // Earlier IE may not support access by string index
}
// END STATIC
if (split){
s1 = s1.split('');
s2 = s2.split('');
}
var v0 = new Array(s1_len+1);
var v1 = new Array(s1_len+1);
var s1_idx=0, s2_idx=0, cost=0;
for (s1_idx=0; s1_idx<s1_len+1; s1_idx++) {
v0[s1_idx] = s1_idx;
}
var char_s1='', char_s2='';
for (s2_idx=1; s2_idx<=s2_len; s2_idx++) {
v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx=0; s1_idx<s1_len;s1_idx++) {
char_s1 = s1[s1_idx];
cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx+1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) {
m_min = b; }
if (c < m_min) {
m_min = c; }
v1[s1_idx+1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp;
}
return v0[s1_len];
}
function redirectNewsThread(th, cx) {
if (!cx.error && th.board.key.match(/(bbynews|bbylive|bbynamazu|bbylunch)/)) {
var u = th.getRes(1).links[0];
v2c.openURL(u,true,false,false);
}
}
function openWritePanel(th) {
if (
th.bbs.is2ch
/* 2ch以外でも有効にしたい場合、下の各行頭//を削除してください */
// || th.bbs.is2cheq //BBSが2ch互換板の場合
// || th.bbs.shitaraba //BBSがしたらばの場合
// || th.bbs.machi //BBSがまちBBSの場合
// || th.bbs.twitter //BBSがTwitterの場合
) {
th.mayOpenWritePanel();
}
}
// 対象文字列のチェック
function checkThStr(str, target) {
var tlen, i;
if ((tlen = target.length) > 0) {
for (i=0; i<tlen; i++) {
if (str.indexOf(target[i]) > -1) {
return true;
}
}
}
return false;
}
// IDのNG判定
function idConditionalNG(th, cx) {
/* 設定 */
// idCountがこの値を超えたらそのIDをNG 初期値20
var threshold = 20;
// trueなら透明NGID、falseなら通常NGID
var set_t = false;
// NG登録後全てのレス表示タブのスレッドでも再チェックさせるならtrue,そうでないならfalse
// 重いようならfalseにしてそのスレだけで再チェック
var rechk_rp = false;
// IDの長さがこの値を超えたらチェック対象にする
var inflen = 7;
// 実行する対象URL、スレURLに含まれるか(th.url.toString().indexOf() > 0)を
// チェックするので ".2ch.net/test/read.cgi/news/" という風に
// 複数あるときは , で区切る
// 2chすべての板を対象にするなら ".2ch.net/test/read.cgi/" など
// すべてを対象にするなら "." など
// ↓は既にニュー速と嫌儲を登録、必要ないなら削除
var target = [
".2ch.net/test/read.cgi/news/",
".2ch.net/test/read.cgi/poverty/",
];
// プラットフォームごとの闘値機能を有効にするならtrue、そうでないならfalse
var xtargetThresholdsEnabled = true;
// プラットフォームごとの闘値 初期値20
var xtargetThresholds = {
'O' : 20, // 携帯
'o' : 20, // WiLLCOMの一部機種
'I' : 20, // iPhone
'i' : 20, // iPhone
'P' : 20, // p2.2ch.netからの投稿
'0' : 20, // PC, wifi経由等
};
/* 設定終わり */
if(!cx.error && cx.numNewRes > 0 && checkThStr(th.url.toString(), target)) {
var srtnum = th.localResCount - cx.numNewRes;
var res, i;
var rflag = false;
for (i=srtnum; i<th.localResCount; i++) {
res = th.getRes(i);
if (res != null && !res.ng && res.id && res.id.length() > inflen) {
if (xtargetThresholdsEnabled && res.id.length() == 9) {
var thresTmp = xtargetThresholds[String(res.id).charAt(8)];
if (thresTmp != undefined && res.idCount > thresTmp) {
res.addNGID(set_t);
rflag = true;
}
continue;
}
if (res.idCount > threshold) {
res.addNGID(set_t);
rflag = true;
}
}
}
// 追加があったら再チェック
if (rflag) {
v2c.resPane.checkNG(th);// ←重いようならth.boardをthにしてそのスレだけで再チェック
}
}
}
// 新着レスを対象に単発IDのレスを非表示にする。
// 既にNGとなっている場合はNGラベルの見分けがつくように何もしない。
// [through]
// 単発でもスルーするレスの範囲(through=50で>>1-50のレスを飛ばす。)
function setNewTanpatsuNG(th, cx, through) {
var lrc = th.localResCount
var format = '非表示の単発IDスカウター: '
var scouter = 0
for ( var i = lrc - cx.numNewRes; i < lrc; i++ ) {
if ( i < through ) continue;
var res = th.getRes( i )
if ( res && res.idCount == 1 && !res.ng ) {
res.setNGRes()
scouter++
v2c.setStatus( format + scouter )
}
}
}
function checkNewTanpatsuNG( th, cx ) {
var target = [ //実況カテゴリ
'live',
'endless',
'weekly',
'kokkai',
'dome',
'oonna',
'ootoko',
'dancesite',
'festival',
'jasmine',
];
var targetTanpatsu = /Firefox|Google/i
if ( cx.numNewRes > 0 ) {
if ( th.bbs.is2ch && checkThStr(th.board.key, target) ) {
setNewTanpatsuNG( th, cx, 75 );
} else if ( targetTanpatsu.test( th.title ) ) {
setNewTanpatsuNG( th, cx, 1 );
}
egnoreNewNGRes( th, cx )
}
}
function openFixedColumn(th) {
/* 設定 */
var index = 0;//スレッド開くときに常に使用するカラムIndex
/* 設定ここまで */
if (th.columnIndex>index) {
th.movePanelTo(index, -1);
th.open(false, false, false);//移動した後表示
}
}
function groupThreadPanel(th) {
/* 設定 */
var target = [ //実況カテゴリ
'live',
'endless',
'weekly',
'kokkai',
'dome',
'oonna',
'ootoko',
'dancesite',
'festival',
'jasmine',
];
var targetIndex = 0;//target1の場合に開くColumnIndex
var otherIndex = 1;//target以外で開くColumnIndex
/* 設定ここまで */
if (th.bbs.is2ch && checkThStr(th.board.key, target) || th.bbs.twitter) {
if (th.columnIndex != targetIndex) {
th.movePanelTo(targetIndex, -1);
th.open(false, false, false);//移動した後表示
}
} else {
if (th.columnIndex != otherIndex) {
th.movePanelTo(otherIndex, -1);
th.open(false, false, false);//移動した後表示
}
}
}
function guroNG(th, cx)
{
for (var i = (th.localResCount - cx.numNewRes); i < th.localResCount; i++) {
var res = th.getRes(i);
var decision = false;
if (res.refResIndex && res.refResIndex.length > 0 && res.links.length > 0) {
for (var j = 0; j < res.refResIndex.length; j++) {
decision = (th.getRes(res.refResIndex[j]).message.indexOf('グロ') >= 0) ? true : decision;
}
}
if (decision) v2c.println(res.message);
if (decision) res.setNGRes(true);
}
}
Ly/jgJDnmbvpjLLloLTmiYDjgJEgIlYyQ1xzY3JpcHRcc3lzdGVtXHRocmVhZGxkLmpzIgovL+OAkOWGheWuueOAkXRocmVhZGxkLmpz44Gu44G+44Go44KBCi8v44CQ5YKZ6ICD44CRdGhyZWFkVXBkYXRlZOOBqHBhbmVsQ3JlYXRlZOOBrnt95YaF44Gn44CB6KGM6aCt44Gu44Kz44Oh44Oz44OI44CMLy/jgI3jgpLjgIHliKnnlKjjgZnjgovooYzjgZTjgajjgavop6PpmaTjgZnjgovjgZPjgajjgafjgIHmnInlirnjgavjgarjgorjgb7jgZnjgIIKLy/jgJDmm7TmlrDml6XjgJEyMDEzLzExLzI0IGd1cm9OR+OBrui/veWKoAovL+OAkOOCueOCr+ODquODl+ODiOOAkQovLyAtLS0tLSDmrKHjga7ooYzjgYvjgokgLS0tLS0KLyog44K544Os44OD44OJ44Gu5pu05paw44GM5a6M5LqG44GX44Gf5pmC44Gr5a6f6KGM44GZ44KL44K544Kv44Oq44OX44OIICovCmZ1bmN0aW9uIHRocmVhZFVwZGF0ZWQodGgsIGN4KSB7Ci8qIOapn+iDveOCkuacieWKueOBq+OBl+OBn+OBhOWgtOWQiOOAgeS4i+OBruWQhOihjOmgrS8v44KS5YmK6Zmk44GX44Gm44GP44Gg44GV44GEICAqLwovLyAgZWdub3JlTmV3TkdSZXModGgsIGN4KTsgLy/mlrDnnYDjgYxOR+OBruOBv+OBruWgtOWQiOOAgeacquiqreeKtuaFi+OCkuino+mZpAogIHJlZGlyZWN0TmV3c1RocmVhZCh0aCwgY3gpOyAvL+mAn+WgsWhlYWRsaW5l44K544Os44KS6ZaL44GP44Go5pys44K544Os44Gr44Oq44OA44Kk44Os44Kv44OIIOKAu+ODkeODvOODn+ODg+OCt+ODp+ODs+OBq1PjgpLov73liqDjgZfjgabjgY/jgaDjgZXjgYQg4oC76YCf5aCxaGVhZGxpbmXjgrnjg6zjga7jg63jgrDjga/liYrpmaTjgZXjgozjgb7jgZvjgpPjgIIKICByZWRpcmVjdExpdmVUaHJlYWQodGgsIGN4KTsgLy/jg4bjg6zjg5PnlarntYTmrITjga7jgrnjg6zjgpLplovjgY/jgajkuIDnlarli6LjgYTjga7jgYLjgovjgrnjg6zjg4Pjg4ko5oGQ44KJ44GP5pys44K544OsKeOBq+ODquODgOOCpOODrOOCr+ODiCDigLvjg5Hjg7zjg5/jg4Pjgrfjg6fjg7PjgatT44KS6L+95Yqg44GX44Gm44GP44Gg44GV44GEIOKAu+ODhuODrOODk+eVque1hOashOOCueODrOOBruODreOCsOOBr+WJiumZpOOBleOCjOOBvuOBm+OCk+OAggogIGlkQ29uZGl0aW9uYWxORyh0aCwgY3gpOyAvL+mWvuWApOS7peS4iuabuOOBjei+vOOBv0lE44KSTkfliKTlrpoo44OH44OV44Kp44Or44OI44Gv44OL44Ol44O86YCf44Go5auM5YSy44Gn5pyJ5Yq5KQovLyAgY2hlY2tOZXdUYW5wYXRzdU5HKHRoLCBjeCk7IC8v5paw552A44Os44K544KS5a++6LGh44Gr5Y2Y55m6SUTjga7jg6zjgrnjgpLpnZ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oqK3lrprpoIXnm65dIOavlOi8g+mWouaVsOOBrumWvuWApOOAguOBk+OBruWApOS7peS4iuOBruOCueOCs+OCouOBq+OBquOBo+OBn+OCueODrOOBr+mZpOWkluOBmeOCiwogIHZhciB0aHJlc2hvbGQgPSAxNjsKICAvLyBb6Kit5a6a6aCF55uuXSDnlarntYTlkI3jgajjgrnjg6zjgr/jgqTjgavmr5TovIPliY3jgavoo5zmraPjgpLmjpvjgZHjgabkuIDoh7TjgZfjgoTjgZnjgY/jgZnjgosKICB2YXIgUFJFX0ZVTkMgPSAoZnVuY3Rpb24obmFtZSkgewogIAl2YXIgcmV0ID0geyAndmFsdWUnIDogJycsICdkZW55JyA6ICcnIH07CiAgICAvL+OCq+ODg+OCs+OCkuWJiumZpChERUJZX1dPUkRT44Gu5aC05ZCI44Gv5q6L44GZCiAgICB2YXIgdG5hbWUgPSAgbmFtZS5yZXBsYWNlKC9bXFtcKFw844CQ77yI772b77yc77y744CK44CUXVteXF1cKVw+44CR77yJ772d44CN44CP77ye77y944CL44CVXStbXF1cKVw+44CR77yJ772d77ye77y944CL44CVXS9nLCAnJyk7IC8vIOOCq+ODg+OCs+OBrueorumhnuOBruOAjOOAjiDjga/jgr/jgqTjg4jjg6vjgavkvb/jgo/jgozjgovloLTlkIjjgYzjgYLjgovjga7jgafpmaTlpJYgCiAgICBpZiAodG5hbWUubGVuZ3RoIDwgbmFtZS5sZW5ndGggJiYgREVOWV9XT1JEUy50ZXN0KG5hbWUpKSB7CiAgICAgIHJldC5kZW55ID0gUmVnRXhwLiQxOwogICAgfQogICAgbmFtZSA9IHRuYW1lOwogICAgLy/lhajop5Loi7HmlbDlrZfjgpLljYrop5LjgasKICAgIG5hbWUgPSBuYW1lLnJlcGxhY2UoL1vvvIEt772eXS9nLCBmdW5jdGlvbihzKSB7CiAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlKHMuY2hhckNvZGVBdCgwKSAtIDB4RkVFMCk7CiAgICB9KTsKICAgIC8v5Y2K6KeS6Iux5pWw5a2X44KS44Go5pel5pys6Kqe5Lul5aSW44Gv5YmK6Zmk44CC5bCP5paH5a2X44KS5aSn5paH5a2X44GrCiAgICBuYW1lID0gbmFtZS5yZXBsYWNlKC9bXuS4gC3pvqDjgYEt44KT44KhLeODtGEtekEtWjAtOVxyXG5dL2lnLCAnJykudG9VcHBlckNhc2UoKTsKCiAgICB2YXIgaGFuS2FuYSA9IFsn7722776eJywgJ++9t+++nicsICfvvbjvvp4nLCAn7725776eJywgJ++9uu++nicsICfvvbvvvp4nLCAn7728776eJywgJ++9ve++nicsICfvvb7vvp4nLCAn772/776eJywgJ+++gO++nicsICfvvoHvvp4nLCAn776C776eJywgJ+++g+++nicsICfvvoTvvp4nLCAn776K776eJywgJ+++iu++nycsICfvvovvvp4nLCAn776L776fJywgJ+++jO++nicsICfvvozvvp8nLCAn776N776eJywgJ+++je++nycsICfvvo7vvp4nLCAn776O776fJywgJ++9s+++nicsCiAgICAn772nJywgJ++9sScsICfvvagnLCAn772yJywgJ++9qScsICfvvbMnLCAn772qJywgJ++9tCcsICfvvasnLCAn7721JywgJ++9ticsICfvvbcnLCAn7724JywgJ++9uScsICfvvbonLCAn7727JywgJ++9vCcsICfvvb0nLCAn772+JywgJ++9vycsICfvvoAnLCAn776BJywgJ++9rycsICfvvoInLCAn776DJywgJ+++hCcsICfvvoUnLCAn776GJywgJ+++hycsICfvvognLCAn776JJywgJ+++iicsICfvvosnLCAn776MJywgJ+++jScsCiAgICAn776OJywgJ+++jycsICfvvpAnLCAn776RJywgJ+++kicsICfvvpMnLCAn772sJywgJ+++lCcsICfvva0nLCAn776VJywgJ++9ricsICfvvpYnLCAn776XJywgJ+++mCcsICfvvpknLCAn776aJywgJ+++mycsICfvvpwnLCAn776cJywgJ++9picsICfvvp0nLCAn772hJywgJ++9oicsICfvvaMnLCAn772kJywgJ++9pScsICfvvbAnLCAn776eJywgJ+++nyddOwogICAgdmFyIHplbkthbmEgPSAn44Ks44Ku44Kw44Ky44K044K244K444K644K844K+44OA44OC44OF44OH44OJ44OQ44OR44OT44OU44OW44OX44OZ44Oa44Oc44Od44O044Kh44Ki44Kj44Kk44Kl44Km44Kn44Ko44Kp44Kq44Kr44Kt44Kv44Kx44Kz44K144K344K544K744K944K/44OB44OD44OE44OG44OI44OK44OL44OM44ON44OO44OP44OS44OV44OY44Ob44Oe44Of44Og44Oh44Oi44Oj44Ok44Ol44Om44On44Oo44Op44Oq44Or44Os44Ot44Ov44Ov44Oy44Oz44CC44CM44CN44CB44O744O844Kb44KcJzsKICAgIHZhciB6ZW5IaXJhID0gJ+OBjOOBjuOBkOOBkuOBlOOBluOBmOOBmuOBnOOBnuOBoOOBouOBpeOBp+OBqeOBsOOBseOBs+OBtOOBtuOBt+OBueOBuuOBvOOBveOClOOBgeOBguOBg+OBhOOBheOBhuOBh+OBiOOBieOBiuOBi+OBjeOBj+OBkeOBk+OBleOBl+OBmeOBm+OBneOBn+OBoeOBo+OBpOOBpuOBqOOBquOBq+OBrOOBreOBruOBr+OBsuOBteOBuOOBu+OBvuOBv+OCgOOCgeOCguOCg+OChOOCheOChuOCh+OCiOOCieOCiuOCi+OCjOOCjeOCj+OCj+OCkuOCk+OAguOAjOOAjeOAgeODu+ODvOOCm+OCnCc7CiAgICAvL+WFqOinkuOCq+OCv+OCq+ODiuOBsuOCieOBjOOBquOCkuWNiuinkuOCq+OCv+OCq+ODiuOBqwogICAgbmFtZSA9IG5hbWUucmVwbGFjZSgvW+OBgS3jgpPjgqEt44O044CM44CN44CB44O744O844Kb44KcXS9nLCBmdW5jdGlvbihzKSB7CiAgICAgIHZhciBuID0gemVuS2FuYS5pbmRleE9mKHMpOwogICAgICBpZiAobiAhPSAtMSkgeyByZXR1cm4gaGFuS2FuYVtuXTsgfQogICAgICBuID0gemVuSGlyYS5pbmRleE9mKHMpOwogICAgICBpZiAobiAhPSAtMSkgeyByZXR1cm4gaGFuS2FuYVtuXTsgfQogICAgICByZXR1cm4gJyc7CiAgICB9KTsKICAgIHJldC52YWx1ZSA9IG5hbWU7CiAgICByZXR1cm4gcmV0OwogIH0pOwovLyDigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJXigJUKCiAgaWYgKCFjeC5lcnJvciAmJiB0aC5ib2FyZC5rZXkuc3RhcnRzV2l0aCgndHYyY2h3aWtpJykpIHsKICAgIHZhciB0bXAgPSB0aC5nZXRSZXMoMCk7CiAgICB2YXIgYmQgPSB2MmMuZ2V0Qm9hcmQodG1wLmxpbmtzWzBdKTsKICAgIGlmICghYmQpIHsgcHJpbnRGdW5jKGFyZ3VtZW50cy5jYWxsZWUsICJ7MH3jga7lj5blvpfjgavlpLHmlZciLCBiZCk7IHJldHVybiBmYWxzZTsgfQogICAgdG1wID0gU3RyaW5nKHRtcC5tZXNzYWdlKS5zcGxpdCgnXG4nKTsKICAgIGlmICgoIXRtcCkgfHwgdG1wLmxlbmd0aCA8PSAwKSB7IHByaW50RnVuYyhhcmd1bWVudHMuY2FsbGVlLCAiezB944GuPj4x44Gu5pys5paH44Gu5Y+W5b6X44Gr5aSx5pWXIiwgdGgpOyByZXR1cm4gZmFsc2U7IH0KICAgIC8vIOODkuODg+ODiOeOh+OCkuS4iuOBkuOCi+OBn+OCgeOBq+WFqOinkuOCueODmuODvOOCueOBp+WMuuWIh+OBo+OBpuODq+ODvOODl+WIpOWumu+8iOOBneOBruWIhuiqpOWIpOWumuOCguWil+OBiOOCi+OAguWFg+OBruODreOCuOODg+OCr+OBq+aIu+OBl+OBn+WgtOWQiOOBr+imi+OBpOOBi+OCieOBquOBi+OBo+OBn+OBp+e1guOCj+OCiu+8iQogICAgdmFyIHJhd3RpdGxlID0gdG1wWzFdOwogICAgdmFyIHNzID0gdjJjLnJlYWRVUkwoYmQud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JpbnRGdW5jKGFyZ3VtZW50cy5jYWxsZWUsICJERU5ZX1dPUkRT44CMezF944CN44Gr44OS44OD44OI44GX44Gf44Gu44Gn44CMezB944CN44KS6Zmk5aSW44GX44G+44GZ44CCIiwgdG1wLmRlbnksIG5hbWUpOwogICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIHZhciBzY29yZSA9IGxldmVuc2h0ZWluKHRpdGxlLnZhbHVlLCB0bXAudmFsdWUpOwogICAgICAgIGlmIChzY29yZSA+PSB0aHJlc2hvbGQpIHsKICAgICAgICAgIG1heG1hdGNoZXMucHVzaCh7J3NwZWVkJzogc3BlZWQsIGtleTogY3VycmVudCwgJ3RpdGxlJzogbmFtZSwgJ3Jlc251bSc6IHBhcnNlSW50KHJlc251bSksICdzY29yZScgOiBzY29yZX0pOwogICAgICAgIH0KICAgICAgfQogICAgfQogICAgaWYgKG1heG1hdGNoZXMubGVuZ3RoID4gMCkgewogICAgICAvLyAxMDAx44Gr6YGU44GX44Gm44GE44Gq44GE44Gq44KL44G544GP5Y+k44GE44K544Os44KS5YSq5YWI44GZ44KL44CC6KaL44Gk44GL44KJ44Gq44GE5aC05ZCI44GvMTAwMeOBl+OBi+OBquOBhD3mlrDjgrnjg6zjgYznq4vjgaPjgabjgarjgYTjgaPjgabjgZPjgajjgafmnIDjgoLmlrDjgZfjgYTjgrnjg6zjgpLov5TjgZkKICAgICAgLy8g44K544Os56uL44Gm5pmC6ZaT5piH6aCGCiAgICAgIG1heG1hdGNoZXMuc29ydChmdW5jdGlvbihhLCBiKSB7IAogICAgICAgIGlmIChhLnNjb3JlID09PSBiLnNjb3JlKSB7IHJldHVybiBwYXJzZUludChhLmtleSkgLSBwYXJzZUludChiLmtleSk7IH0KICAgICAgICBlbHNlIHJldHVybiBhLnNjb3JlIC0gYi5zY29yZTsKICAgICAgfSk7CiAgICAgIHZhciBtYXggPSBtYXhtYXRjaGVzW21heG1hdGNoZXMubGVuZ3RoIC0gMV07IC8vIOacgOW+jOOBq+eri+OBo+OBn+OCueODrOOCkuWFpeOCjOOBqOOBjwogICAgICBmb3IgKHZhciBpID0gMDsgaSA8IG1heG1hdGNoZXMubGVuZ3RoOyBpKyspIHsgIAogICAgICAgIGlmIChtYXhtYXRjaGVzW2ldLnJlc251bSA9PSAxMDAxKSB7IGNvbnRpbnVlOyB9CiAgICAgICAgbWF4ID0gbWF4bWF0Y2hlc1tpXTsKICAgICAgICBicmVhazsKICAgICAgfQogICAgICBwcmludEZ1bmMoYXJndW1lbnRzLmNhbGxlZSwgIuOAkOaknOe0oue1guS6huOAguOCueODrOODg+ODieOBruWPluW+l+OCkuippuOBv+OBvuOBmeOAkeOCueODrOOCv+OCpOWQje+8mnswfSAiLCBtYXgudGl0bGUpOwogICAgICB2YXIgdSA9IG5ldyBqYXZhLm5ldC5VUkwoJ2h0dHA6Ly8nICsgYmQudXJsLmdldEhvc3QoKSArICcvdGVzdC9yZWFkLmNnaS8nICsgYmQua2V5ICsgJy8nICsgbWF4LmtleSArICcvJyk7CiAgICAgIGlmIChCT0FSRFZJRVcpIHsKICAgICAgICB2MmMub3BlblVSTChiZC51cmwpOwogICAgICAgIGphdmEubGFuZy5UaHJlYWQuc2xlZXAoVEhVUERBVEVfV0FJVCk7CiAgICAgICAgdjJjLm9wZW5VUkwodSwgdHJ1ZSwgZmFsc2UsIGZhbHNlKTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSBlbHNlIHsKICAgICAgICB2YXIgdGgyID0gYmQuZ2V0VGhyZWFkKG1heC5rZXksIGJkLnVybCwgbWF4LnRpdGxlLCBtYXgucmVzbnVtKTsKICAgICAgICBpZiAodGgyKSB7CiAgICAgICAgICB0aDIub3Blbih0cnVlLCBmYWxzZSwgZmFsc2UpOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHByaW50RnVuYyhhcmd1bWVudHMuY2FsbGVlLCAi44CQ44K544Os44OD44OJ44Gu5Y+W5b6X44Gr5aSx5pWX44GX44Gf44Gu44Gn44K544Os5LiA6Kan44KS6ZaL44GN44G+44GZ44CR55Wq57WE5ZCNIDogezB9IiwgcmF3dGl0bGUpOwogICAgICAgICAgdjJjLm9wZW5VUkwoYmQudXJsKTsKICAgICAgICB9CiAgICAgIH0KICAgIH0gZWxzZSB7CiAgICAgIHByaW50RnVuYyhhcmd1bWVudHMuY2FsbGVlLCAi44CQ44K544Os44OD44OJ44KS6KaL44Gk44GR44KJ44KM44Gq44GL44Gj44Gf44Gu44Gn44K544Os5LiA6Kan44KS6ZaL44GN44G+44GZ44CR55Wq57WE5ZCNIDogezB9IiwgcmF3dGl0bGUpOwogICAgICB2MmMub3BlblVSTChiZC51cmwpOwogICAgfQogIH0KfQovLyDmloflrZfliJfjga7poZ7kvLzmr5TovIPnlKjot53pm6LplqLmlbAKZnVuY3Rpb24gbGV2ZW5zaHRlaW4oczEsIHMyKSB7CiAgICAvLyBodHRwOi8vay4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uZC5uZXQKICAgIC8vICsgICAgICAgICAgICBvcmlnaW5hbCBieTogQ2FybG9zIFIuIEwuIFJvZHJpZ3VlcyAoaHR0cDovL3cuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmwuY29tKQogICAgLy8gKyAgICAgICAgICAgIGJ1Z2ZpeGVkIGJ5OiBPbm5vIE1hcnNtYW4KICAgIC8vICsgICAgICAgICAgICAgcmV2aXNlZCBieTogQW5kcmVhIEdpYW1tYXJjaGkgKGh0dHA6Ly93Li4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi50LmNvbSkKICAgIC8vICsgcmVpbXBsZW1lbnRlZCBieTogQnJldHQgWmFtaXIgKGh0dHA6Ly9iLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5yLm1lKQogICAgLy8gKyByZWltcGxlbWVudGVkIGJ5OiBBbGV4YW5kZXIgTSBCZWVkaWUKICAgIC8vICogICAgICAgICAgICAgICAgZXhhbXBsZSAxOiBsZXZlbnNodGVpbignS2V2aW4gdmFuIFpvbm5ldmVsZCcsICdLZXZpbiB2YW4gU29tbWV2ZWxkJyk7CiAgICAvLyAqICAgICAgICAgICAgICAgIHJldHVybnMgMTogMwoKICAgIGlmIChzMSA9PSBzMikgewogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIHZhciBzMV9sZW4gPSBzMS5sZW5ndGg7CiAgICB2YXIgczJfbGVuID0gczIubGVuZ3RoOwogICAgaWYgKHMxX2xlbiA9PT0gMCkgewogICAgICAgIHJldHVybiBzMl9sZW47CiAgICB9CiAgICBpZiAoczJfbGVuID09PSAwKSB7CiAgICAgICAgcmV0dXJuIHMxX2xlbjsKICAgIH0KCiAgICAvLyBCRUdJTiBTVEFUSUMKICAgIHZhciBzcGxpdCA9IGZhbHNlOwogICAgdHJ5ewogICAgICAgIHNwbGl0PSEoJzAnKVswXTsKICAgIH0gY2F0Y2ggKGUpewogICAgICAgIHNwbGl0PXRydWU7IC8vIEVhcmxpZXIgSUUgbWF5IG5vdCBzdXBwb3J0IGFjY2VzcyBieSBzdHJpbmcgaW5kZXgKICAgIH0KICAgIC8vIEVORCBTVEFUSUMKICAgIGlmIChzcGxpdCl7CiAgICAgICAgczEgPSBzMS5zcGxpdCgnJyk7CiAgICAgICAgczIgPSBzMi5zcGxpdCgnJyk7CiAgICB9CgogICAgdmFyIHYwID0gbmV3IEFycmF5KHMxX2xlbisxKTsKICAgIHZhciB2MSA9IG5ldyBBcnJheShzMV9sZW4rMSk7CgogICAgdmFyIHMxX2lkeD0wLCBzMl9pZHg9MCwgY29zdD0wOwogICAgZm9yIChzMV9pZHg9MDsgczFfaWR4PHMxX2xlbisxOyBzMV9pZHgrKykgewogICAgICAgIHYwW3MxX2lkeF0gPSBzMV9pZHg7CiAgICB9CiAgICB2YXIgY2hhcl9zMT0nJywgY2hhcl9zMj0nJzsKICAgIGZvciAoczJfaWR4PTE7IHMyX2lkeDw9czJfbGVuOyBzMl9pZHgrKykgewogICAgICAgIHYxWzBdID0gczJfaWR4OwogICAgICAgIGNoYXJfczIgPSBzMltzMl9pZHggLSAxXTsKCiAgICAgICAgZm9yIChzMV9pZHg9MDsgczFfaWR4PHMxX2xlbjtzMV9pZHgrKykgewogICAgICAgICAgICBjaGFyX3MxID0gczFbczFfaWR4XTsKICAgICAgICAgICAgY29zdCA9IChjaGFyX3MxID09IGNoYXJfczIpID8gMCA6IDE7CiAgICAgICAgICAgIHZhciBtX21pbiA9IHYwW3MxX2lkeCsxXSArIDE7CiAgICAgICAgICAgIHZhciBiID0gdjFbczFfaWR4XSArIDE7CiAgICAgICAgICAgIHZhciBjID0gdjBbczFfaWR4XSArIGNvc3Q7CiAgICAgICAgICAgIGlmIChiIDwgbV9taW4pIHsKICAgICAgICAgICAgICAgIG1fbWluID0gYjsgfQogICAgICAgICAgICBpZiAoYyA8IG1fbWluKSB7CiAgICAgICAgICAgICAgICBtX21pbiA9IGM7IH0KICAgICAgICAgICAgdjFbczFfaWR4KzFdID0gbV9taW47CiAgICAgICAgfQogICAgICAgIHZhciB2X3RtcCA9IHYwOwogICAgICAgIHYwID0gdjE7CiAgICAgICAgdjEgPSB2X3RtcDsKICAgIH0KICAgIHJldHVybiB2MFtzMV9sZW5dOwp9CmZ1bmN0aW9uIHJlZGlyZWN0TmV3c1RocmVhZCh0aCwgY3gpIHsKICBpZiAoIWN4LmVycm9yICYmIHRoLmJvYXJkLmtleS5tYXRjaCgvKGJieW5ld3N8YmJ5bGl2ZXxiYnluYW1henV8YmJ5bHVuY2gpLykpIHsKICAgIHZhciB1ID0gdGguZ2V0UmVzKDEpLmxpbmtzWzBdOwogICAgdjJjLm9wZW5VUkwodSx0cnVlLGZhbHNlLGZhbHNlKTsKICB9Cn0KZnVuY3Rpb24gb3BlbldyaXRlUGFuZWwodGgpIHsKICBpZiAoCiAgICAgIHRoLmJicy5pczJjaAogICAgICAvKiAy772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c2V0X3QgPSBmYWxzZTsKICAvLyBOR+eZu+mMsuW+jOWFqOOBpuOBruODrOOCueihqOekuuOCv+ODluOBruOCueODrOODg+ODieOBp+OCguWGjeODgeOCp+ODg+OCr+OBleOBm+OCi+OBquOCiXRydWUs44Gd44GG44Gn44Gq44GE44Gq44KJZmFsc2UKICAvLyDph43jgYTjgojjgYbjgarjgolmYWxzZeOBq+OBl+OBpuOBneOBruOCueODrOOBoOOBkeOBp+WGjeODgeOCp+ODg+OCrwogIHZhciByZWNoa19ycCA9IGZhbHNlOwogIC8vIElE44Gu6ZW344GV44GM44GT44Gu5YCk44KS6LaF44GI44Gf44KJ44OB44Kn44OD44Kv5a++6LGh44Gr44GZ44KLCiAgdmFyIGluZmxlbiA9IDc7CiAgLy8g5a6f6KGM44GZ44KL5a++6LGhVVJM44CB44K544OsVVJM44Gr5ZCr44G+44KM44KL44GLKHRoLnVybC50b1N0cmluZygpLmluZGV4T2YoKSA+IDAp44KSCiAgLy8g44OB44Kn44OD44Kv44GZ44KL44Gu44GnICIuMmNoLm5ldC90ZXN0L3JlYWQuY2dpL25ld3MvIiDjgajjgYTjgYbpoqjjgasKICAvLyDopIfmlbDjgYLjgovjgajjgY3jga8gLCDjgafljLrliIfjgosKICAvLyAyY2jjgZnjgbnjgabjga7mnb/jgpLlr77osaHjgavjgZnjgovjgarjgokgIi4yY2gubmV0L3Rlc3QvcmVhZC5jZ2kvIiDjgarjgakKICAvLyDjgZnjgbnjgabjgpLlr77osaHjgavjgZnjgovjgarjgokgIi4iIOOBquOBqQogIC8vIOKGk+OBr+aXouOBq+ODi+ODpeODvOmAn+OBqOWrjOWEsuOCkueZu+mMsuOAgeW/heimgeOBquOBhOOBquOCieWJiumZpAogIHZhciB0YXJnZXQgPSBbCiAgICAiLjJjaC5uZXQvdGVzdC9yZWFkLmNnaS9uZXdzLyIsCiAgICAiLjJjaC5uZXQvdGVzdC9yZWFkLmNnaS9wb3ZlcnR5LyIsCiAgXTsKICAvLyDjg5fjg6njg4Pjg4jjg5Xjgqnjg7zjg6DjgZTjgajjga7pl5jlgKTmqZ/og73jgpLmnInlirnjgavjgZnjgovjgarjgol0cnVl44CB44Gd44GG44Gn44Gq44GE44Gq44KJZmFsc2UKICB2YXIgeHRhcmdldFRocmVzaG9sZHNFbmFibGVkID0gdHJ1ZTsKICAvLyDjg5fjg6njg4Pjg4jjg5Xjgqnjg7zjg6DjgZTjgajjga7pl5jlgKQg5Yid5pyf5YCkMjAKICB2YXIgeHRhcmdldFRocmVzaG9sZHMgPSB7CiAgICAnTycgOiAyMCwgICAgICAgICAgICAgLy8g5pC65bivCiAgICAnbycgOiAyMCwgICAgICAgICAgICAgLy8gV2lMTENPTeOBruS4gOmDqOapn+eorgogICAgJ0knIDogMjAsICAgICAgICAgICAgIC8vIGlQaG9uZQogICAgJ2knIDogMjAsICAgICAgICAgICAgIC8vIGlQaG9uZQogICAgJ1AnIDogMjAsICAgICAgICAgICAgIC8vIHAyLjJjaC5uZXTjgYvjgonjga7mipXnqL8KICAgICcwJyA6IDIwLCAgICAgICAgICAgICAvLyBQQywgd2lmaee1jOeUseetiQogIH07CiAgLyog6Kit5a6a57WC44KP44KKICovCiAgaWYoIWN4LmVycm9yICYmIGN4Lm51bU5ld1JlcyA+IDAgJiYgY2hlY2tUaFN0cih0aC51cmwudG9TdHJpbmcoKSwgdGFyZ2V0KSkgewogICAgdmFyIHNydG51bSA9IHRoLmxvY2FsUmVzQ291bnQgLSBjeC5udW1OZXdSZXM7CiAgICB2YXIgcmVzLCBpOwogICAgdmFyIHJmbGFnID0gZmFsc2U7CiAgICBmb3IgKGk9c3J0bnVtOyBpPHRoLmxvY2FsUmVzQ291bnQ7IGkrKykgewogICAgICByZXMgPSB0aC5nZXRSZXMoaSk7CiAgICAgIGlmIChyZXMgIT0gbnVsbCAmJiAhcmVzLm5nICYmIHJlcy5pZCAmJiByZXMuaWQubGVuZ3RoKCkgPiBpbmZsZW4pIHsKICAgICAgICBpZiAoeHRhcmdldFRocmVzaG9sZHNFbmFibGVkICYmIHJlcy5pZC5sZW5ndGgoKSA9PSA5KSB7CiAgICAgICAgICB2YXIgdGhyZXNUbXAgPSB4dGFyZ2V0VGhyZXNob2xkc1tTdHJpbmcocmVzLmlkKS5jaGFyQXQoOCldOwogICAgICAgICAgaWYgKHRocmVzVG1wICE9IHVuZGVmaW5lZCAmJiByZXMuaWRDb3VudCA+IHRocmVzVG1wKSB7CiAgICAgICAgICAgIHJlcy5hZGROR0lEKHNldF90KTsKICAgICAgICAgICAgcmZsYWcgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGlmIChyZXMuaWRDb3VudCA+IHRocmVzaG9sZCkgewogICAgICAgICAgcmVzLmFkZE5HSUQoc2V0X3QpOwogICAgICAgICAgcmZsYWcgPSB0cnVlOwogICAgICAgIH0KICAgICAgfQogICAgfQogICAgLy8g6L+95Yqg44GM44GC44Gj44Gf44KJ5YaN44OB44Kn44OD44KvCiAgICBpZiAocmZsYWcpIHsKICAgICAgdjJjLnJlc1BhbmUuY2hlY2tORyh0aCk7Ly8g4oaQ6YeN44GE44KI44GG44Gq44KJdGguYm9hcmTjgpJ0aOOBq+OBl+OBpuOBneOBruOCueODrOOBoOOBkeOBp+WGjeODgeOCp+ODg+OCrwogICAgfQogIH0KfQovLyDmlrDnnYDjg6zjgrnjgpLlr77osaHjgavljZjnmbpJROOBruODrOOCueOCkumdnuihqOekuuOBq+OBmeOCi+OAggovLyDml6LjgatOR+OBqOOBquOBo+OBpuOBhOOCi+WgtOWQiOOBr05H44Op44OZ44Or44Gu6KaL5YiG44GR44GM44Gk44GP44KI44GG44Gr5L2V44KC44GX44Gq44GE44CCCi8vIFt0aHJvdWdoXQovLyAgIOWNmOeZuuOBp+OCguOCueODq+ODvOOBmeOCi+ODrOOCueOBruevhOWbsu+8iHRocm91Z2g9NTDjgac+PjEtNTDjga7jg6zjgrnjgpLpo5vjgbDjgZnjgILvvIkKZnVuY3Rpb24gc2V0TmV3VGFucGF0c3VORyh0aCwgY3gsIHRocm91Z2gpIHsKICB2YXIgbHJjID0gdGgubG9jYWxSZXNDb3VudAogIHZhciBmb3JtYXQgPSAn6Z2e6KGo56S644Gu5Y2Y55m6SUTvvb3vvbbvvbPvvoDvvbA6ICcKICB2YXIgc2NvdXRlciA9IDAKCiAgZm9yICggdmFyIGkgPSBscmMgLSBjeC5udW1OZXdSZXM7IGkgPCBscmM7IGkrKyApIHsKICAgIGlmICggaSA8IHRocm91Z2ggKSBjb250aW51ZTsKCiAgICB2YXIgcmVzID0gdGguZ2V0UmVzKCBpICkKICAgIGlmICggcmVzICYmIHJlcy5pZENvdW50ID09IDEgJiYgIXJlcy5uZyApIHsKICAgICAgcmVzLnNldE5HUmVzKCkKICAgICAgc2NvdXRlcisrCiAgICAgIHYyYy5zZXRTdGF0dXMoIGZvcm1hdCArIHNjb3V0ZXIgKQogICAgfQogIH0KfQpmdW5jdGlvbiBjaGVja05ld1RhbnBhdHN1TkcoIHRoLCBjeCApIHsKICB2YXIgdGFyZ2V0ID0gWyAvL+Wun+azgeOCq+ODhuOCtOODqgogICAgJ2xpdmUnLAogICAgJ2VuZGxlc3MnLAogICAgJ3dlZWtseScsCiAgICAna29ra2FpJywKICAgICdkb21lJywKICAgICdvb25uYScsCiAgICAnb290b2tvJywKICAgICdkYW5jZXNpdGUnLAogICAgJ2Zlc3RpdmFsJywKICAgICdqYXNtaW5lJywKICBdOwogIHZhciB0YXJnZXRUYW5wYXRzdSA9IC9GaXJlZm94fEdvb2dsZS9pCgogIGlmICggY3gubnVtTmV3UmVzID4gMCApIHsKICAgIGlmICggdGguYmJzLmlzMmNoICYmIGNoZWNrVGhTdHIodGguYm9hcmQua2V5LCB0YXJnZXQpICkgewogICAgICBzZXROZXdUYW5wYXRzdU5HKCB0aCwgY3gsIDc1ICk7CiAgICB9IGVsc2UgaWYgKCB0YXJnZXRUYW5wYXRzdS50ZXN0KCB0aC50aXRsZSApICkgewogICAgICBzZXROZXdUYW5wYXRzdU5HKCB0aCwgY3gsICAxICk7CiAgICB9CiAgICBlZ25vcmVOZXdOR1JlcyggdGgsIGN4ICkKICB9Cn0KCmZ1bmN0aW9uIG9wZW5GaXhlZENvbHVtbih0aCkgewogIC8qIOioreWumiAqLwogIHZhciBpbmRleCA9IDA7Ly/jgrnjg6zjg4Pjg4nplovjgY/jgajjgY3jgavluLjjgavkvb/nlKjjgZnjgovjgqvjg6njg6BJbmRleAogIC8qIOioreWumuOBk+OBk+OBvuOBpyAqLwogIAogIGlmICh0aC5jb2x1bW5JbmRleD5pbmRleCkgewogICAgdGgubW92ZVBhbmVsVG8oaW5kZXgsIC0xKTsKICAgIHRoLm9wZW4oZmFsc2UsIGZhbHNlLCBmYWxzZSk7Ly/np7vli5XjgZfjgZ/lvozooajnpLoKICB9Cn0KZnVuY3Rpb24gZ3JvdXBUaHJlYWRQYW5lbCh0aCkgewogIC8qIOioreWumiAqLwogIHZhciB0YXJnZXQgPSBbIC8v5a6f5rOB44Kr44OG44K044OqCiAgICAnbGl2ZScsCiAgICAnZW5kbGVzcycsCiAgICAnd2Vla2x5JywKICAgICdrb2trYWknLAogICAgJ2RvbWUnLAogICAgJ29vbm5hJywKICAgICdvb3Rva28nLAogICAgJ2RhbmNlc2l0ZScsCiAgICAnZmVzdGl2YWwnLAogICAgJ2phc21pbmUnLAogIF07CiAgdmFyIHRhcmdldEluZGV4ID0gMDsvL3RhcmdldDHjga7loLTlkIjjgavplovjgY9Db2x1bW5JbmRleAogIHZhciBvdGhlckluZGV4ID0gMTsvL3RhcmdldOS7peWkluOBp+mWi+OBj0NvbHVtbkluZGV4CiAgLyog6Kit5a6a44GT44GT44G+44GnICovCgogIGlmICh0aC5iYnMuaXMyY2ggJiYgY2hlY2tUaFN0cih0aC5ib2FyZC5rZXksIHRhcmdldCkgfHwgdGguYmJzLnR3aXR0ZXIpIHsKICAgIGlmICh0aC5jb2x1bW5JbmRleCAhPSB0YXJnZXRJbmRleCkgewogIC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