Notification of new letters in jabber

    Gtalk has a nice option to notify users about new messages, but since I use another jabber server, I thought it would be convenient to stir up such a feature for myself. Maybe I certainly looked bad, and in gmail there is such an opportunity, but I did not find it and wrote a small script that I hung in cron. And now he happily notifies me of new letters every 15 minutes, unless of course there are such letters.

    For work with jabber used library XMPPHP .
    Further, the code and comments ...

    First, we describe in the config from where and where to the notification helmet Code of the main script:
    1. // config.php
    2.  $jabberServer = ‘jabber.ru’; // Сервер на котором находится аккаунт-уведомитель
    3.  $jabberPort = 5223; // Порт сервера
    4.  $jabberLogin = ‘mailnotify’; // Логин уведомителя
    5.  $jabberPassword = ‘123′; // Соответствующий пароль
    6.  $jabberID = “you@jabber.ru”; // JabberID получателя
    7.  $email = “yourEmail”; // Ваше мыло
    8.  $emailPassword = “emailPassword”; // Ну и пароль от него



    1. include_once(‘config.php’);
    2. $mbox = imap_open(“{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX”,
    3.     $email, $emailPassword
    4.     );
    5. $recentMails = imap_num_recent($mbox);
    6. if($recentMails > 0) {
    7.  include_once(“XMPPHP/XMPP.php”);
    8.  $notifySent = false;
    9.  
    10.  $jabberConnection = new XMPPHP_XMPP($jabberServer,
    11.      $jabberPort,
    12.      $jabberLogin,
    13.      $jabberPassword,
    14.      ‘xmpphp’,
    15.      $jabberServer,
    16.      $printlog=false,
    17.      $loglevel=LEVEL_ERROR
    18.      );
    19.  $jabberConnection->useSSL(true);
    20.  $jabberConnection->connect();
    21.  $jabberConnection->processUntil(’session_start’);
    22.  $jabberConnection->presence();
    23.  $jabberConnection->message($jabberID, ‘В ящике ‘.$recentMails.‘ новых писем, иди читай’);
    24.  
    25.  for($i=1;$i<=$recentMails;$i++)
    26.   imap_fetchbody($mbox, $i, 1);
    27.  
    28.  $jabberConnection->disconnect();
    29. }


    That's all, a simple warning light is ready. You can also add a little interactivity to it, which will allow for example reading letters directly in the toad, it is enough to add a design similar to this: It remains only to add your own commands and handlers and you get such a jabber bot.
    1. $notifySent = false;
    2. while(!$jabberConnection->isDisconnected()) {
    3.  $loads = $jabberConnection->processUntil(array(‘message’, ’session_start’));
    4.   foreach($loads as $event) {
    5.    $pl = $event[1];
    6.    switch($event[0]) {
    7.     case “message”:
    8.      if($pl[‘body’] == ‘!read’)
    9.       $jabberConnection->message(‘user@jabber.ru’, getEmails($mbox, $recentMails));
    10.      elseif($pl[‘body’] == ‘!exit’)
    11.       $jabberConnection->disconnect();
    12.      break;
    13.     default:
    14.      if(!$notifySent) {
    15.       $jabberConnection->message(‘user@jabber.ru’, ‘В ящике ‘.$recentMails.‘ новых писем, иди читай’);
    16.       $notifySent = true;
    17.      }
    18.      break;
    19.    }
    20.   }



    Also popular now: