├── .setupimages ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png └── 9.png ├── LICENSE ├── README.md └── Code.gs /.setupimages/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/1.png -------------------------------------------------------------------------------- /.setupimages/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/2.png -------------------------------------------------------------------------------- /.setupimages/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/3.png -------------------------------------------------------------------------------- /.setupimages/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/4.png -------------------------------------------------------------------------------- /.setupimages/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/5.png -------------------------------------------------------------------------------- /.setupimages/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/6.png -------------------------------------------------------------------------------- /.setupimages/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/7.png -------------------------------------------------------------------------------- /.setupimages/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/8.png -------------------------------------------------------------------------------- /.setupimages/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benjojo/Gmail_GeoIPTagger/HEAD/.setupimages/9.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Ben Cox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gmail_GeoIPTagger 2 | ================= 3 | 4 | An app script that will filter your email from where in the world it was sent from 5 | 6 | It labels your email like this: 7 | 8 | ![alt text](http://i.imgur.com/aDptePC.png "Screenshot") 9 | 10 | **This code is experimental and not feature complete. If it breaks, you get 11 | to keep both pieces.** 12 | 13 | ##Setup 14 | First open your google drive and make a new script 15 | 16 | ![alt text](.setupimages/1.png "sc1") 17 | 18 | Then make a blank project 19 | 20 | ![alt text](.setupimages/2.png "sc2") 21 | 22 | Copy and paste the contents of `Code.gs` into the code editor 23 | 24 | ![alt text](.setupimages/3.png "sc3") 25 | 26 | Set your function to run as `TagInbox` and then hit run to make sure it works 27 | 28 | ![alt text](.setupimages/4.png "sc4") 29 | 30 | The first time that it runs it will ask permissions to read and write to your gmail inbox and to connect to other services 31 | 32 | It needs gmail access to tag things as labels, it needs to connect to external services to do GeoIP lookups. 33 | 34 | ![alt text](.setupimages/5.png "sc5") 35 | 36 | ![alt text](.setupimages/6.png "sc6") 37 | 38 | If that runs smoothly we can now set it to run automatically 39 | Resources > Current Project Triggers 40 | 41 | ![alt text](.setupimages/7.png "sc7") 42 | 43 | Then set the function to run every 10 mins 44 | 45 | ![alt text](.setupimages/8.png "sc8") 46 | 47 | ![alt text](.setupimages/9.png "sc9") 48 | -------------------------------------------------------------------------------- /Code.gs: -------------------------------------------------------------------------------- 1 | function TagInbox() { 2 | // get all threads in inbox 3 | var threads = GmailApp.getInboxThreads(); 4 | var GlobalLables = GmailApp.getUserLabels(); 5 | // Check if there are the GEOIP label sets in here. 6 | var HasGeoIP = false; 7 | for (var i = 0; i < GlobalLables.length; i++) { 8 | if(GlobalLables[i].getName() === "geoip") { 9 | HasGeoIP = true; 10 | } 11 | } 12 | if(!HasGeoIP) { 13 | GmailApp.createLabel("geoip"); 14 | GlobalLables = GmailApp.getUserLabels(); 15 | } 16 | 17 | for (var i = 0; i < 10; i++) { 18 | // get all messages in a given thread 19 | var messages = threads[i].getMessages(); 20 | if(threads[i].getLabels().length != 0) { 21 | var ContainsGeoIP = false; 22 | var labels = threads[i].getLabels(); 23 | for (var j = 0; j < labels.length; j++) { 24 | if(labels[j].getName().indexOf("geoip") != -1 ) { 25 | Logger.log(labels[j].getName()) 26 | ContainsGeoIP = true; 27 | } 28 | } 29 | if(ContainsGeoIP) { 30 | break; 31 | } 32 | if(!ContainsGeoIP) { 33 | ProcessMsg(messages[0],GlobalLables,threads[i]) 34 | } 35 | } else { 36 | ProcessMsg(messages[0],GlobalLables,threads[i]) 37 | } 38 | } 39 | }; 40 | 41 | function ProcessMsg(msg,GlobalLables,thread) { 42 | 43 | var RawMsg = msg.getRawContent().split("\n"); 44 | // We are looking for 45 | // Received: from 46 | for (var j = 0; j < RawMsg.length; j++) { 47 | if(RawMsg[j].indexOf("Received: from") != -1 && RawMsg[j].indexOf("[") != -1) { 48 | Logger.log(RawMsg[j]) 49 | 50 | var re1='.*?((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])'; // IPv4 IP Address 1 51 | var p = new RegExp(re1,["i"]); 52 | var m = p.exec(RawMsg[j]); 53 | if (m != null) { 54 | var ipaddress1=m[1]; 55 | var Orign = (GetEmailLocation(ipaddress1)) 56 | var Res = EnsureWeHaveLabel(GlobalLables,Orign); 57 | GlobalLables = Res[1]; 58 | Label = Res[0]; 59 | if (Orign != "Reserved" && Orign != "Unknown") { 60 | thread.addLabel(Label); 61 | } 62 | break; 63 | } 64 | } 65 | } 66 | } 67 | 68 | function GetEmailLocation(IP) { 69 | try{ 70 | var rawjson = UrlFetchApp.fetch("http://ipinfo.io/" + IP + "/json"); 71 | var obj = JSON.parse(rawjson); 72 | return obj.country; 73 | } catch (e) { 74 | var rawjson = UrlFetchApp.fetch("https://freegeoip.app/json/" + IP); 75 | var obj = JSON.parse(rawjson); 76 | return obj.country_code; 77 | } 78 | } 79 | 80 | function EnsureWeHaveLabel(GlobalLables,TargetLabel) { 81 | for (var i = 0; i < GlobalLables.length; i++) { 82 | if (GlobalLables[i].getName() === "geoip/" + TargetLabel) { 83 | return [GlobalLables[i],GlobalLables]; 84 | } 85 | } 86 | var NewLabel = GmailApp.createLabel("geoip/" + TargetLabel); 87 | GlobalLables = GmailApp.getUserLabels(); 88 | return [NewLabel,GlobalLables] 89 | } 90 | --------------------------------------------------------------------------------