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:
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.
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:
- // config.php
- $jabberServer = ‘jabber.ru’; // Сервер на котором находится аккаунт-уведомитель
- $jabberPort = 5223; // Порт сервера
- $jabberLogin = ‘mailnotify’; // Логин уведомителя
- $jabberPassword = ‘123′; // Соответствующий пароль
- $jabberID = “you@jabber.ru”; // JabberID получателя
- $email = “yourEmail”; // Ваше мыло
- $emailPassword = “emailPassword”; // Ну и пароль от него
- include_once(‘config.php’);
- $mbox = imap_open(“{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX”,
- $email, $emailPassword
- );
- $recentMails = imap_num_recent($mbox);
- if($recentMails > 0) {
- include_once(“XMPPHP/XMPP.php”);
- $notifySent = false;
-
- $jabberConnection = new XMPPHP_XMPP($jabberServer,
- $jabberPort,
- $jabberLogin,
- $jabberPassword,
- ‘xmpphp’,
- $jabberServer,
- $printlog=false,
- $loglevel=LEVEL_ERROR
- );
- $jabberConnection->useSSL(true);
- $jabberConnection->connect();
- $jabberConnection->processUntil(’session_start’);
- $jabberConnection->presence();
- $jabberConnection->message($jabberID, ‘В ящике ‘.$recentMails.‘ новых писем, иди читай’);
-
- for($i=1;$i<=$recentMails;$i++)
- imap_fetchbody($mbox, $i, 1);
-
- $jabberConnection->disconnect();
- }
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.
- $notifySent = false;
- while(!$jabberConnection->isDisconnected()) {
- $loads = $jabberConnection->processUntil(array(‘message’, ’session_start’));
- foreach($loads as $event) {
- $pl = $event[1];
- switch($event[0]) {
- case “message”:
- if($pl[‘body’] == ‘!read’)
- $jabberConnection->message(‘user@jabber.ru’, getEmails($mbox, $recentMails));
- elseif($pl[‘body’] == ‘!exit’)
- $jabberConnection->disconnect();
- break;
- default:
- if(!$notifySent) {
- $jabberConnection->message(‘user@jabber.ru’, ‘В ящике ‘.$recentMails.‘ новых писем, иди читай’);
- $notifySent = true;
- }
- break;
- }
- }