Last active
August 29, 2015 14:25
-
-
Save kamataryo/47ddf5c7480e732d2591 to your computer and use it in GitHub Desktop.
YO back against Ingress portal attack in google app script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Google App Scriptの実行トリガーを時間主導型の1分ごとに設定する | |
function myFunction(){ | |
//AgentIDとYoNameの対応リストを自力で作成 | |
var AgentList = { | |
"Agent1" : "yoname1", | |
"Agent2" : "yoname2" | |
}; | |
//履歴保存用スプレッドシート指定 | |
var sheetObj = SpreadsheetApp.openById('***YOUR SHEET ID***'); | |
var sheet = sheetObj.getSheetByName('history'); | |
//未読メールを検索 | |
var q = 'label:inbox is:unread from:"ingress-support@google.com" subject:"Ingress Damage Report: Entities attacked by "'; | |
var thds = GmailApp.search(q); | |
//一度に処理できるメール数を設定 | |
var row_limit = 5; | |
//受信履歴をスプレッドシートに格納 | |
var row = sheet.getLastRow() + 1; | |
var row_first = row; | |
var oldIds = new Array(); | |
for(var i = 1; i <= row; i++) { | |
oldIds.push(sheet.getRange(i,1).getValue()); | |
}; | |
for(var n in thds){ | |
var thd = thds[n]; | |
var msgs = thd.getMessages(); | |
for(m in msgs){ | |
var msg = msgs[m]; | |
var date = msg.getDate(); | |
var id = msg.getId(); | |
//受信済みメールをスキップ | |
if (oldIds.indexOf(id) > -1) { | |
Logger.log(id + ' skipped.'); | |
continue; | |
}; | |
//攻撃してきたAgentのYo名を知ってたらYo! | |
var attackingAgent = msg.getSubject().split(" ").pop(); | |
if(attackingAgent in AgentList){ | |
sendYoTo(attackingAgent); | |
}; | |
msg.markRead(); | |
//受信ログを保存 | |
sheet.getRange(row,1).setValue(id); | |
sheet.getRange(row,2).setValue("" + date); | |
row++; | |
//一度に処理できるメール数を超えたら処理終了 | |
if(row - row_first > row_limit) { | |
break; | |
}; | |
}; | |
}; | |
} | |
var sendYoTo = function(yoname) { | |
var payload = { | |
"api_token" : "***YOUR API TOKEN***", | |
"username" : yoname, | |
"link" : "http://example.com/this/key/value/pair/can/be/ignored" | |
}; | |
var options = { | |
"method" : "post", | |
"payload" : payload | |
}; | |
UrlFetchApp.fetch("http://api.justyo.co/yo/", options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment