{"action":"created","comment":{"url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/comments/41268105","id":41268105,"diff_hunk":"@@ -0,0 +1,350 @@\n+var Client = require(\"imap\");\n+var async = require(\"async\");\n+var _ = require(\"lodash\");\n+var inspect = require(\"util\").inspect;\n+\n+/**\n+ * Constructor\n+ *\n+ */\n+\n+var Imap = function(credentials) {\n+  this.client = new Client(credentials);\n+}\n+\n+/**\n+ * Connect to IMAP server\n+ * \n+ */\n+\n+Imap.prototype.connect = function() {\n+  var self = this;\n+  return new Promise(function(resolve, reject){\n+    self.client.connect();\n+    self.client.once(\"ready\", function(){\n+      console.log(\"Connection ready\");\n+      resolve();\n+    })\n+    self.client.once(\"error\", function(err) {\n+      console.log(err);\n+      reject(err);\n+    })\n+    self.client.once(\"end\", function() {\n+      console.log(\"Connection ended\");\n+    })\n+  })\n+}\n+\n+/**\n+ * Lists the mail boxes\n+ * \n+ */\n+\n+Imap.prototype.getSpecialBoxes = function() {\n+  var self = this;\n+  return new Promise(function(resolve, reject){\n+    var specials = {};\n+    var specialBoxes = [\"Drafts\", \"Sent\", \"Spam\", \"Inbox\", \"Trash\"];\n+    self.client.getBoxes(function(err, mboxes){\n+      if (err) {\n+        return reject(err);\n+      }\n+      var boxes = Object.keys(mboxes);\n+      async.each(specialBoxes, function iterator(special, callback) {\n+        async.each(boxes, function iterator(index, cb){\n+          if (mboxes[index].children !=null) {\n+            var childrens = Object.keys(mboxes[index].children);\n+            async.each(childrens, function iterator(child, cb){\n+              if (child == special) {\n+                specials[child] = {\n+                  path : index + mboxes[index].delimiter + child\n+                }\n+              }\n+            }, function(err) {\n+              cb();\n+            })\n+          }\n+          cb();\n+        }, function(err){\n+          callback();\n+        })\n+      }, function(err){\n+        self.specials = specials;\n+        resolve(specials)\n+      })\n+    })\n+  })\n+}\n+\n+/**\n+ * Lists the mail boxes\n+ * \n+ */\n+\n+Imap.prototype.getBoxes = function() {\n+  var self = this;\n+  return new Promise(function(resolve, reject){\n+    self.client.getBoxes(function(err, boxes){\n+      if (err) {\n+        return reject(err);\n+      }\n+      resolve(boxes);\n+    })\n+  })\n+}\n+\n+/**\n+ * Lists the contents of the mail box\n+ *\n+ * @param {String} name - the name of the box\n+ * @param {Number} start - the offset\n+ * @param {Number} limit - the expected number of result\n+ * @param {Object} searchParams - search parameters\n+ * \n+ */\n+Imap.prototype.listBox = function(name, start, limit, searchParams) {\n+  var self = this;\n+  return new Promise(function(resolve, reject){\n+    var bodies = searchParams || 'HEADER';\n+    var result = [];\n+    self.client.openBox(name, true, function(err, box){\n+      if (err) {\n+        return reject(err);\n+      }\n+      console.log(\"total message \" + box.messages.total);\n+      var fetchLimit = box.messages.total;\n+      if (limit) {\n+        fetchLimit = start + limit - 1;\n+      }\n+      var seqArray = []\n+      for (var i = start || 1; i <= fetchLimit; i++) {\n+        seqArray.push(i);\n+      }\n+      async.each(seqArray, function iterator(seq, callback) {\n+        var mail = {}\n+        var f = self.client.seq.fetch(seq, {\n+          bodies : bodies,\n+          struct : true\n+        });\n+        f.on(\"message\", function(msg, seqno){\n+          var prefix = \"(#\" + seqno + \")\";\n+    \n+          msg.on(\"body\", function(stream, info) {\n+            var buffer = \"\";\n+            stream.on(\"data\", function(chunk) {\n+              buffer += chunk.toString(\"utf8\");\n+            });\n+            stream.once(\"end\", function(attrs){\n+              mail.buffer = buffer;\n+            });\n+          })\n+          msg.once(\"attributes\", function(attrs) {\n+              mail.attributes = attrs;\n+          })\n+          msg.once(\"end\", function(){\n+            result.push(mail);\n+          })\n+        });\n+        f.once(\"error\", function(err) {\n+          console.log(\"Fetch error : \" + err);\n+        })\n+        f.once(\"end\", function(err) {\n+          callback();","path":"api/surelia/imap.js","position":152,"original_position":152,"commit_id":"30c2a21c58c1080d292dfdaa917480a7a5ad5664","original_commit_id":"30c2a21c58c1080d292dfdaa917480a7a5ad5664","user":{"login":"mdamt","id":641261,"avatar_url":"https://avatars.githubusercontent.com/u/641261?v=3","gravatar_id":"","url":"https://api.github.com/users/mdamt","html_url":"https://github.com/mdamt","followers_url":"https://api.github.com/users/mdamt/followers","following_url":"https://api.github.com/users/mdamt/following{/other_user}","gists_url":"https://api.github.com/users/mdamt/gists{/gist_id}","starred_url":"https://api.github.com/users/mdamt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdamt/subscriptions","organizations_url":"https://api.github.com/users/mdamt/orgs","repos_url":"https://api.github.com/users/mdamt/repos","events_url":"https://api.github.com/users/mdamt/events{/privacy}","received_events_url":"https://api.github.com/users/mdamt/received_events","type":"User","site_admin":false},"body":"*Pass* `err`-nya","created_at":"2015-10-06T14:09:04Z","updated_at":"2015-10-06T14:09:04Z","html_url":"https://github.com/KodeKreatif/surelia-web-client/pull/4#discussion_r41268105","pull_request_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/comments/41268105"},"html":{"href":"https://github.com/KodeKreatif/surelia-web-client/pull/4#discussion_r41268105"},"pull_request":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4","id":46879186,"html_url":"https://github.com/KodeKreatif/surelia-web-client/pull/4","diff_url":"https://github.com/KodeKreatif/surelia-web-client/pull/4.diff","patch_url":"https://github.com/KodeKreatif/surelia-web-client/pull/4.patch","issue_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/4","number":4,"state":"open","locked":false,"title":"IMAP & SMTP module","user":{"login":"herpiko","id":2534060,"avatar_url":"https://avatars.githubusercontent.com/u/2534060?v=3","gravatar_id":"","url":"https://api.github.com/users/herpiko","html_url":"https://github.com/herpiko","followers_url":"https://api.github.com/users/herpiko/followers","following_url":"https://api.github.com/users/herpiko/following{/other_user}","gists_url":"https://api.github.com/users/herpiko/gists{/gist_id}","starred_url":"https://api.github.com/users/herpiko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/herpiko/subscriptions","organizations_url":"https://api.github.com/users/herpiko/orgs","repos_url":"https://api.github.com/users/herpiko/repos","events_url":"https://api.github.com/users/herpiko/events{/privacy}","received_events_url":"https://api.github.com/users/herpiko/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-10-06T09:19:45Z","updated_at":"2015-10-06T14:09:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0aa2964cd8f751e373cb54fd1065f9e00cc2cb99","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4/commits","review_comments_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4/comments","review_comment_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/4/comments","statuses_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/statuses/30c2a21c58c1080d292dfdaa917480a7a5ad5664","head":{"label":"herpiko:smtp-2","ref":"smtp-2","sha":"30c2a21c58c1080d292dfdaa917480a7a5ad5664","user":{"login":"herpiko","id":2534060,"avatar_url":"https://avatars.githubusercontent.com/u/2534060?v=3","gravatar_id":"","url":"https://api.github.com/users/herpiko","html_url":"https://github.com/herpiko","followers_url":"https://api.github.com/users/herpiko/followers","following_url":"https://api.github.com/users/herpiko/following{/other_user}","gists_url":"https://api.github.com/users/herpiko/gists{/gist_id}","starred_url":"https://api.github.com/users/herpiko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/herpiko/subscriptions","organizations_url":"https://api.github.com/users/herpiko/orgs","repos_url":"https://api.github.com/users/herpiko/repos","events_url":"https://api.github.com/users/herpiko/events{/privacy}","received_events_url":"https://api.github.com/users/herpiko/received_events","type":"User","site_admin":false},"repo":{"id":43644460,"name":"surelia-web-client","full_name":"herpiko/surelia-web-client","owner":{"login":"herpiko","id":2534060,"avatar_url":"https://avatars.githubusercontent.com/u/2534060?v=3","gravatar_id":"","url":"https://api.github.com/users/herpiko","html_url":"https://github.com/herpiko","followers_url":"https://api.github.com/users/herpiko/followers","following_url":"https://api.github.com/users/herpiko/following{/other_user}","gists_url":"https://api.github.com/users/herpiko/gists{/gist_id}","starred_url":"https://api.github.com/users/herpiko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/herpiko/subscriptions","organizations_url":"https://api.github.com/users/herpiko/orgs","repos_url":"https://api.github.com/users/herpiko/repos","events_url":"https://api.github.com/users/herpiko/events{/privacy}","received_events_url":"https://api.github.com/users/herpiko/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/herpiko/surelia-web-client","description":"","fork":true,"url":"https://api.github.com/repos/herpiko/surelia-web-client","forks_url":"https://api.github.com/repos/herpiko/surelia-web-client/forks","keys_url":"https://api.github.com/repos/herpiko/surelia-web-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/herpiko/surelia-web-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/herpiko/surelia-web-client/teams","hooks_url":"https://api.github.com/repos/herpiko/surelia-web-client/hooks","issue_events_url":"https://api.github.com/repos/herpiko/surelia-web-client/issues/events{/number}","events_url":"https://api.github.com/repos/herpiko/surelia-web-client/events","assignees_url":"https://api.github.com/repos/herpiko/surelia-web-client/assignees{/user}","branches_url":"https://api.github.com/repos/herpiko/surelia-web-client/branches{/branch}","tags_url":"https://api.github.com/repos/herpiko/surelia-web-client/tags","blobs_url":"https://api.github.com/repos/herpiko/surelia-web-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/herpiko/surelia-web-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/herpiko/surelia-web-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/herpiko/surelia-web-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/herpiko/surelia-web-client/statuses/{sha}","languages_url":"https://api.github.com/repos/herpiko/surelia-web-client/languages","stargazers_url":"https://api.github.com/repos/herpiko/surelia-web-client/stargazers","contributors_url":"https://api.github.com/repos/herpiko/surelia-web-client/contributors","subscribers_url":"https://api.github.com/repos/herpiko/surelia-web-client/subscribers","subscription_url":"https://api.github.com/repos/herpiko/surelia-web-client/subscription","commits_url":"https://api.github.com/repos/herpiko/surelia-web-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/herpiko/surelia-web-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/herpiko/surelia-web-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/herpiko/surelia-web-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/herpiko/surelia-web-client/contents/{+path}","compare_url":"https://api.github.com/repos/herpiko/surelia-web-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/herpiko/surelia-web-client/merges","archive_url":"https://api.github.com/repos/herpiko/surelia-web-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/herpiko/surelia-web-client/downloads","issues_url":"https://api.github.com/repos/herpiko/surelia-web-client/issues{/number}","pulls_url":"https://api.github.com/repos/herpiko/surelia-web-client/pulls{/number}","milestones_url":"https://api.github.com/repos/herpiko/surelia-web-client/milestones{/number}","notifications_url":"https://api.github.com/repos/herpiko/surelia-web-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/herpiko/surelia-web-client/labels{/name}","releases_url":"https://api.github.com/repos/herpiko/surelia-web-client/releases{/id}","created_at":"2015-10-04T17:08:10Z","updated_at":"2015-10-04T17:08:11Z","pushed_at":"2015-10-06T10:14:46Z","git_url":"git://github.com/herpiko/surelia-web-client.git","ssh_url":"git@github.com:herpiko/surelia-web-client.git","clone_url":"https://github.com/herpiko/surelia-web-client.git","svn_url":"https://github.com/herpiko/surelia-web-client","homepage":null,"size":87,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"KodeKreatif:master","ref":"master","sha":"88cbe29e09749e83fc47f088c2a55d0d8ff82d43","user":{"login":"KodeKreatif","id":6791688,"avatar_url":"https://avatars.githubusercontent.com/u/6791688?v=3","gravatar_id":"","url":"https://api.github.com/users/KodeKreatif","html_url":"https://github.com/KodeKreatif","followers_url":"https://api.github.com/users/KodeKreatif/followers","following_url":"https://api.github.com/users/KodeKreatif/following{/other_user}","gists_url":"https://api.github.com/users/KodeKreatif/gists{/gist_id}","starred_url":"https://api.github.com/users/KodeKreatif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KodeKreatif/subscriptions","organizations_url":"https://api.github.com/users/KodeKreatif/orgs","repos_url":"https://api.github.com/users/KodeKreatif/repos","events_url":"https://api.github.com/users/KodeKreatif/events{/privacy}","received_events_url":"https://api.github.com/users/KodeKreatif/received_events","type":"Organization","site_admin":false},"repo":{"id":43272860,"name":"surelia-web-client","full_name":"KodeKreatif/surelia-web-client","owner":{"login":"KodeKreatif","id":6791688,"avatar_url":"https://avatars.githubusercontent.com/u/6791688?v=3","gravatar_id":"","url":"https://api.github.com/users/KodeKreatif","html_url":"https://github.com/KodeKreatif","followers_url":"https://api.github.com/users/KodeKreatif/followers","following_url":"https://api.github.com/users/KodeKreatif/following{/other_user}","gists_url":"https://api.github.com/users/KodeKreatif/gists{/gist_id}","starred_url":"https://api.github.com/users/KodeKreatif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KodeKreatif/subscriptions","organizations_url":"https://api.github.com/users/KodeKreatif/orgs","repos_url":"https://api.github.com/users/KodeKreatif/repos","events_url":"https://api.github.com/users/KodeKreatif/events{/privacy}","received_events_url":"https://api.github.com/users/KodeKreatif/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/KodeKreatif/surelia-web-client","description":"","fork":false,"url":"https://api.github.com/repos/KodeKreatif/surelia-web-client","forks_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/forks","keys_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/teams","hooks_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/hooks","issue_events_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/events{/number}","events_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/events","assignees_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/assignees{/user}","branches_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/branches{/branch}","tags_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/tags","blobs_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/statuses/{sha}","languages_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/languages","stargazers_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/stargazers","contributors_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/contributors","subscribers_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/subscribers","subscription_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/subscription","commits_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/contents/{+path}","compare_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/merges","archive_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/downloads","issues_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues{/number}","pulls_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls{/number}","milestones_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/milestones{/number}","notifications_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/labels{/name}","releases_url":"https://api.github.com/repos/KodeKreatif/surelia-web-client/releases{/id}","created_at":"2015-09-28T01:11:13Z","updated_at":"2015-09-28T01:11:33Z","pushed_at":"2015-10-06T10:14:46Z","git_url":"git://github.com/KodeKreatif/surelia-web-client.git","ssh_url":"git@github.com:KodeKreatif/surelia-web-client.git","clone_url":"https://github.com/KodeKreatif/surelia-web-client.git","svn_url":"https://github.com/KodeKreatif/surelia-web-client","homepage":null,"size":135,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4"},"html":{"href":"https://github.com/KodeKreatif/surelia-web-client/pull/4"},"issue":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/4"},"comments":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/KodeKreatif/surelia-web-client/statuses/30c2a21c58c1080d292dfdaa917480a7a5ad5664"}}}}