Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var system = require('system');
  2.  
  3. // antigate
  4. phantom.injectJs(system.env['HOME'] + '/mailer/vendor/antigatejs/antigate.js');
  5.  
  6. var LogicController = function () {
  7.  
  8.     this.abortUrl = function (url) {
  9.         return false
  10.           || /^http(s)?:\/\/([^\/]+\.)?(c|top-fwz1|rs|ad|webagent|love|filin|r3|portal|reklama|bs)\.mail\.ru/.test(url)
  11.           || /^http(s)?:\/\/([^\/]+\.)?(radar|gstat)\.imgsmail\.ru/.test(url)
  12.           || /^http(s)?:\/\/([^\/]+\.)?(mail|imgsmail)\.ru/.test(url) == false
  13.     }
  14.  
  15.     this.login = function (input) {
  16.         Casper.open('https://e.mail.ru/messages/inbox/');
  17.  
  18.         function check_login() {
  19.             Casper.waitFor(
  20.               function check() {
  21.                   return this.evaluate(function () {
  22.                       return 0
  23.                         || $('#LoginExternal').is(':visible') // cookies are empty or outdated
  24.                         || $('h1.password-recovery__remind-new-header').is(':visible') // account banned
  25.                         || $('a[data-name=compose]:first-child').is(':visible') // ok
  26.                   });
  27.               },
  28.               function () {
  29.                   if (Casper.visible('#LoginExternal')) {
  30.                       Casper.fill('#LoginExternal', {
  31.                           'Login': input.login,
  32.                           'Password': input.password
  33.                       }, true);
  34.                       Casper.waitFor(
  35.                         function () {
  36.                             return this.evaluate(function () {
  37.                                 return !$('#LoginExternal').is(':visible')
  38.                             })
  39.                         },
  40.                         check_login,
  41.                         stepTimeout,
  42.                         10000
  43.                       )
  44.                   }
  45.                   else if (Casper.visible('a[data-name=compose]:first-child'))
  46.                       Controller.operation_result = true
  47.                   else
  48.                       Controller.operation_result = Casper.getHTML('h1.password-recovery__remind-new-header').trim()
  49.               },
  50.               stepTimeout,
  51.               10000
  52.             )
  53.         }
  54.  
  55.         check_login()
  56.     }
  57.  
  58.     this.send = function (input) {
  59.         Casper.then(function () {
  60.             Casper.click('a[data-name=compose]:first-child')
  61.         });
  62.  
  63.         Casper.waitFor(
  64.           function check() {
  65.               return this.evaluate(function () {
  66.                   /*
  67.                    console.log(''
  68.                    + '=' + (typeof tinyMCE !== 'undefined')
  69.                    + '=' + (typeof tinyMCE.get(0) !== 'undefined')
  70.                    + '=' + tinyMCE.get(0).getContent().indexOf("<br />--")>=0
  71.                    + '=' + tinyMCE.get(0).getContent().indexOf("js-signature")<0
  72.                    );
  73.                    */
  74.                   return 1
  75.                     && typeof tinyMCE !== 'undefined'
  76.                     && typeof tinyMCE.get(0) !== 'undefined'
  77.                     && tinyMCE.get(0).getContent().indexOf("<br />--") >= 0
  78.                     && tinyMCE.get(0).getContent().indexOf("js-signature") < 0;
  79.               });
  80.           },
  81.           function () {
  82.               //mouse.move('textarea[data-original-name=To]')
  83.               //mouse.click('textarea[data-original-name=To]')
  84.               Casper.sendKeys('textarea[data-original-name=To]', input.to);
  85.               Casper.sendKeys('input[name=Subject]', input.subject);
  86.               Casper.evaluate(function (text) {
  87.                   tinyMCE.get(0).setContent(text);
  88.               }, input.text)
  89.               Casper.click('div[data-name=send]')
  90.           },
  91.           stepTimeout,
  92.           20000
  93.         );
  94.  
  95.         Casper.waitFor(
  96.           function check() {
  97.               return this.evaluate(function () {
  98.                   return 0
  99.                     || $('.notify-message__title__text_error').is(':visible')
  100.                     || $('.message-sent__title').is(':visible')
  101.                     || $('#MailRuConfirm').is(':visible')
  102.               });
  103.           },
  104.           function check_message_status() {
  105.               if (Casper.visible('#MailRuConfirm')) {
  106.                   Controller.operation_result = null
  107.               }
  108.               else if (Casper.visible('.message-sent__title'))
  109.                   Controller.operation_result = true
  110.               else {
  111.                   Controller.operation_result = Casper.evaluate(function () {
  112.                       var text = $('.notify-message__title__text_error:visible').html().trim()
  113.                       $('.notify').hide();
  114.                       return text;
  115.                   })
  116.               }
  117.           },
  118.           stepTimeout,
  119.           40000
  120.         )
  121.     }
  122.  
  123. // send with captcha solving - bad idea
  124.     this.send2 = function (input) {
  125.         Casper.then(function () {
  126.             Casper.click('a[data-name=compose]:first-child')
  127.         });
  128.  
  129.         Casper.waitFor(
  130.           function check() {
  131.               return this.evaluate(function () {
  132.                   /*
  133.                    console.log(''
  134.                    + '=' + (typeof tinyMCE !== 'undefined')
  135.                    + '=' + (typeof tinyMCE.get(0) !== 'undefined')
  136.                    + '=' + tinyMCE.get(0).getContent().indexOf("<br />--")>=0
  137.                    + '=' + tinyMCE.get(0).getContent().indexOf("js-signature")<0
  138.                    );
  139.                    */
  140.                   return 1
  141.                     && typeof tinyMCE !== 'undefined'
  142.                     && typeof tinyMCE.get(0) !== 'undefined'
  143.                     && tinyMCE.get(0).getContent().indexOf("<br />--") >= 0
  144.                     && tinyMCE.get(0).getContent().indexOf("js-signature") < 0;
  145.               });
  146.           },
  147.           function () {
  148.               //mouse.move('textarea[data-original-name=To]')
  149.               //mouse.click('textarea[data-original-name=To]')
  150.               Casper.sendKeys('textarea[data-original-name=To]', input.to);
  151.               Casper.sendKeys('input[name=Subject]', input.subject);
  152.               Casper.evaluate(function (text) {
  153.                   tinyMCE.get(0).setContent(text);
  154.               }, input.text)
  155.               Casper.click('div[data-name=send]')
  156.           },
  157.           stepTimeout,
  158.           20000
  159.         );
  160.  
  161.         var tries = 0;
  162.  
  163.         function checksend() {
  164.             if (tries >= 3) {
  165.                 Controller.operation_result = null
  166.                 return;
  167.             }
  168.  
  169.             tries += 1;
  170.  
  171.             Casper.waitFor(
  172.               function check() {
  173.                   return this.evaluate(function () {
  174.                       return 0
  175.                         || $('.notify-message__title__text_error').is(':visible')
  176.                         || $('.message-sent__title').is(':visible')
  177.                         || $('#MailRuConfirm').is(':visible')
  178.                   });
  179.               },
  180.               function check_message_status() {
  181.                   if (Casper.visible('#MailRuConfirm')) {
  182.                       //Controller.operation_result = null
  183.  
  184.                       Casper.evaluate(function () {
  185.                           $('#MailRuConfirm > .popup > div:visible .js-captcha-link').click()
  186.                       })
  187.  
  188.                       function captcha_load() {
  189.                           Casper.waitFor(
  190.                             function check_captcha_loaded() {
  191.                                 return this.evaluate(function () {
  192.                                     return $('#MailRuConfirm > .popup > div:visible .js-captcha-block img').get(0).complete
  193.                                 });
  194.                             },
  195.                             function captcha_loaded() {
  196.  
  197.                                 var captcha_src = Casper.evaluate(function () {
  198.                                     return $('#MailRuConfirm > .popup > div:visible .js-captcha-block img').attr('src')
  199.                                 });
  200.  
  201.                                 if (debug != "true" && debug != "false" && debug)
  202.                                     Casper.capture(debug + '_' + (new Date()).getTime() + '_captcha.png');
  203.  
  204.                                 var captcha_fname = '/tmp/mailru_captcha_' + Math.random() + ".jpg";
  205.                                 Casper.captureSelector(captcha_fname, '.js-captcha-block img[src="' + captcha_src + '"]');
  206.                                 //Casper.download(captcha_src, captcha_fname);
  207.  
  208.                                 var ag = new AntiGate(
  209.                                   'dcdfc8921d3e7ebac57d26c486bdbf16',
  210.                                   '/root/mailer/vendor/antigatejs/solve_captcha.php'
  211.                                 );
  212.                                 ag.send(
  213.                                   captcha_fname,
  214.                                   function (text) {
  215.                                       console.log('captcha=' + text)
  216.                                       Casper.evaluate(function (code) {
  217.                                           $('#MailRuConfirm > .popup > div:visible .js-captcha-block input[name=code]').val(code)
  218.                                           $('#MailRuConfirm > .popup > div:visible .confirm-ok').click()
  219.                                       }, text)
  220.                                   }
  221.                                 )
  222.  
  223.                                 Casper.waitWhileVisible(
  224.                                   '#MailRuConfirm',
  225.                                   checksend,
  226.                                   stepTimeout(),
  227.                                   120000
  228.                                 )
  229.  
  230.  
  231.                             },
  232.                             function check_captcha_timeout() {
  233.                                 console.log('check_captcha_timeout!');
  234.  
  235.                                 Casper.evaluate(function () {
  236.                                     $('#MailRuConfirm > .popup > div:visible a:contains("Не вижу код")').click()
  237.                                 })
  238.                                 captcha_load()
  239.                             },
  240.                             10000
  241.                           )
  242.                       }
  243.  
  244.                       captcha_load()
  245.  
  246.  
  247.                   }
  248.                   else if (Casper.visible('.message-sent__title'))
  249.                       Controller.operation_result = true
  250.                   else {
  251.                       Controller.operation_result = Casper.evaluate(function () {
  252.                           var text = $('.notify-message__title__text_error:visible').html().trim()
  253.                           $('.notify').hide();
  254.                           return text;
  255.                       })
  256.                   }
  257.               },
  258.               stepTimeout,
  259.               40000
  260.             )
  261.         }
  262.  
  263.         checksend()
  264.  
  265.     }
  266.  
  267.     this.getmessageslist = function (input) {
  268.         Controller.operation_result = [];
  269.  
  270.         var url = "https://e.mail.ru/messages/inbox/?back=1";
  271.         if (Casper.getCurrentUrl() != url)
  272.             Casper.open(url)
  273.  
  274.         var sel = (input.onlynew == "true") ? ".b-datalist__item_unread" : ".b-datalist__item";
  275.  
  276.         var getmessagespage = function () {
  277.             Casper.waitForSelector('#b-letters', function () {
  278.                 // fetch messages
  279.                 var messages = Casper.evaluate(function (sel) {
  280.                     var m = [];
  281.                     $('#b-letters ' + sel).each(function () {
  282.                         var a = $(this).find('a[rel=history]');
  283.                         m[m.length] = {
  284.                             'id': a.attr('href'),
  285.                             'from': a.attr('title'),
  286.                             'subject': a.attr('data-subject')
  287.                         }
  288.                     })
  289.                     return m;
  290.                 }, sel)
  291.                 for (var i in messages)
  292.                     Controller.operation_result[Controller.operation_result.length] = messages[i];
  293.  
  294.                 // goto next page if any
  295.                 var hasnextpage = Casper.evaluate(function (onlynew) {
  296.                     if (0
  297.                       || $('div[data-name=next]').length == 0
  298.                       || (onlynew == "true" && $(".b-datalist__item_unread").length < $(".b-datalist__item").length)
  299.                     )
  300.                         return false;
  301.  
  302.                     var disabled = $('div[data-name=next]').attr('aria-disabled')
  303.                     return typeof disabled === typeof undefined || disabled !== "disabled"
  304.                 }, input.onlynew);
  305.                 if (hasnextpage) {
  306.                     Casper.click('div[data-name=next]')
  307.                     Casper.waitForSelectorTextChange('#b-letters', function () {
  308.                         getmessagespage()
  309.                     }, stepTimeout, 30000)
  310.                 }
  311.             }, stepTimeout, 30000);
  312.         };
  313.  
  314.         getmessagespage();
  315.     }
  316.  
  317.     this.getmessage = function (input) {
  318.         Casper.open(input.id, function () {
  319.         }, stepTimeout, 30000)
  320.  
  321.         Casper.waitForSelector('.js-readmsg-msg', function () {
  322.             var text = Casper.evaluate(function () {
  323.                 return $('.js-readmsg-msg').html()
  324.             })
  325.             var from = Casper.evaluate(function () {
  326.                 return $('.b-letter__head__addrs__from .b-contact-informer-target').attr('data-contact-informer-email')
  327.             })
  328.             Controller.operation_result = {
  329.                 'text': text,
  330.                 'from': from
  331.             }
  332.         }, stepTimeout, 30000)
  333.     }
  334.  
  335.     this.register = function (input) {
  336.  
  337.     }
  338.  
  339. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement