⬆️ ⬇️

We win valuable prizes with exim and mail piping

Good luck to all!

So, the purpose of the article: a story about how to automatically parse incoming mail with examples of processing.

Along the way: I counted and won as many as 8 out of 10 sets of tablets from DX and surrendered to the Chinese.



So, the goal number is zero: to learn how to disassemble the Incas with methods other than “log in via POP / IMAP once a minute”.

I decided that helloWorld is not so interesting, especially since a lottery from DX (dx.com/newsletters) came across.

In short, it works like this:

0. Enter email on subscription page (HTTP POST)

1. It receives a link to confirm the email

2. We pass on the link

3. If email is registered for the first time, we get lucky number by email, which participates in the drawing



The attentive reader has already understood the essence of a fairly simple "tink of ears", but the purpose of the article is in practical details.



So resources: cheap test vds .

Unnecessary domain ocrd.ru (A and MX records now point to nowhere)

About one o'clock in the evening.

')

Step1: generation.



Since we need unique email, we write a generator. (We will have something like AvksentijIvanov@ocrd.ru)

(The list of 100 popular Slavic names and surnames is taken from Wikipedia)

Generator code
function ngen(){ /*  ,   @*/ $names=file(dirname(__FILE__).'/names.txt'); $surnames=file(dirname(__FILE__).'/surnames.txt'); return trl($names[rand(0,count($names)-1)]).'-'.trl($surnames[rand(0,count($surnames)-1)]); //          "-"   , ,    _. } function trl($text) {/* ,  - */} function _l($v){/*-  $v   (    ), */} //    .   N email-  , N=2; for ($i=0; $i<2;$i++){ $email=ngen(); $email=preg_replace('/[^A-Za-z-.]/','',$email); //   ,       . $ch = curl_init('http://dx.com/newsletters'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //  ,     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.0; rv:16.0) Gecko/20100101 Firefox/16.0'); curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$email.'%40ocrd.ru&CheckedItems=5555&CheckedItems=4444&CheckedItems=8888&CheckedItems=9999&CheckedItems=3001&CheckedItems=3002&CheckedItems=3003&CheckedItems=3004&CheckedItems=199&CheckedItems=499&CheckedItems=799&CheckedItems=899&CheckedItems=599&CheckedItems=399&CheckedItems=699&CheckedItems=999&CheckedItems=1099&CheckedItems=1299&CheckedItems=1499&CheckedItems=1599&CheckedItems=1699&CheckedItems=1799'); //   firebug $r= curl_exec($ch); _l("-in {$email}"); sleep(10); } 




Step2: Set up Exim.



We put it as an ap-gett (for convenience of understanding, I indicated the split configuration into files), I will not describe it in detail.

Add transport:

 # /etc/exim4/conf.d/transport/01-ocrd_pipe ocrd_pipe: driver = pipe return_fail_output command = /usr/bin/php5 -f /var/mail/ocrd_mail.php $local_part 


As you may have guessed, the contents of the letter will come in the STDIN script. I send the local part (the one before @) as an argument, just to avoid engaging in the process of extracting it from the message, although this is not difficult.



Add a router

 # /etc/exim4/conf.d/router/01-ocrd-pipe #      ,     ( , , )       fwla: driver = redirect no_verify local_parts = tst:test:shilovyaroslav:valeryanyakushev:rogovanisim:potapovignatij:lukakarpov:avksentijuvarov:voronovfilat:kondratgavrilov data = ocrdmail@ya.ru unseen #    . #   --    ocrd: driver = accept domains = ocrd.ru transport = ocrd_pipe 


Re-generate config (update-exim4.conf and restart)



Step 3: parse the incoming message.



Message parsit utility munpack (apt-get [or whatever you have] it)

Attention, hell govnokod detekted. If you are too impressionable just do not unwrap the spoiler.



Handler code
 <?php if (!isset($argv[1])) die(); $msg = ''; while(!feof(STDIN)) { $msg .= fread(STDIN, 1024); } //      file_get_contents  php://input =) $dir='/var/mail/ocrd.ru/'; $to=$argv[1]; //   $fname=$to.'.'.date('dmy.His.').rand(1000,9999); $mf=$dir.'/'.$fname.'.msg'; file_put_contents($mf,$msg,FILE_APPEND); //   ,    munpack //       . $f=$dir.'/tempdesc.txt'; unlink($f); //      mime messages,        . $v=exec('munpack -f -C '.$dir.' '.$mf); _l("msg for {$to} {$fname}"); $c=file_get_contents($f); //       . $v=activate_nl($to,$c) ; // fail?    if (!$v) $v1= get_number($to,$c); // epic fail is so epic? if (!$v and !$v1) _l("err: not_detected ".$fname); //     ddmmyy/<action> $t='err'; if ($v) $t='act'; if ($v1)$t='nbr'; $dc=$dir.'/'.date('dmy').'/'.$t; if ( !is_dir($dc)) mkdir($dc,0750,true); rename($mf,$dc.'/'.$fname.'.msg'); function activate_nl($to,$c){ // ,      global $dir,$fname; //  .    ? echo "Act_NL\n"; $regex='#/newsletters/confirmSubscribe\?e=(.+)%40ocrd.ru&c=(\d+)"#'; $m=array(); if(! preg_match($regex,$c,$m)) return false; //  --    (   , ) _l("cnf for {$to}: e{$m[1]} c{$m[2]}"); file_get_contents("http://dx.com/newsletters/confirmSubscribe\?e={$m[1]}%40ocrd.ru&c={$m[2]}"); return true; } function get_number($to,$c){ //   ID  ,    . global $fname,$dir; echo "Act_GN\n"; $regex='#Your\s+lucky\s+number\s+is\D+(\d+)\s*<#'; $m=array(); if(! preg_match($regex,$c,$m)) return false; _l("lnm for {$to}: {$m[1]}"); return true; } function _l($v){ /*   */} 






In general, it was left to work for about half a month with N = 3;

In November, about 37,000 lucky numbers were received from about 45,000 participating in the draw (lucky autoincrement numbers, so everything is clearly visible).

I proceeded from the fact that one set of prizes is being played out, therefore I set N = 3 generations per minute.



Results



Further develop the events: “Do you think that everyone has gone? No, mate, you oh ... how was wrong. "



In early December, the winners were announced , which caused a reasonable bathert at the DX community.

Of the 10 selected winners (may Gauss arrive with us), 8 turned out to be "mine" (which is not surprising, thirty-seven-forty-fifths are 82%).

I turned on the winners forward striker (see fwla router) and began to monitor the situation. All 8 virtuals were notified that "congratulations, you won."

Then I surrendered to the Chinese. They say I cheater, distribute to someone else, but here is my address (I thought at least one, but they will send me, I'm naive). But no, a few days later the Chinese literally wrote the following.

Since we hadn’t been able to check all the winners email addresses. To remove the fairness.


Well, in general, they are, the Chinese ...



Questions - in kamenty, typos - in a personal, habrakat - in the post, the rays of hatred - the Chinese.

Thank you for reading to the end.

Source: https://habr.com/ru/post/164661/



All Articles