📜 ⬆️ ⬇️

Blink Scroll lock'om with incoming email message (perl + bash)

Based on this question.

Task: learn to blink the keyboard LED in case there are new unread messages in our mailbox.

Add. conditions: OS - linux, language - perl (check of mail) and bash (actually blinking).
')


Actually, as advised in the original topic, we divide the task into its components:
  1. Learn to blink diode
  2. Learn to check for new mail.
  3. Make friends both items among themselves


Learning to blink diode

There is a daemon Ledcontrol , but it did not appear in my repositories, so I will blink with the cursor using the xset command
#!/bin/bash trap 'xset -led named "Scroll Lock"; exit ' SIGINT SIGTERM while(true); do xset led named "Scroll Lock" ; sleep 0.05 ; xset -led named "Scroll Lock"; sleep 0.05; done 

this code was saved to the beepScroll file, the file was put in one of the directories listed in $ PATH, and the execution rights were given. Please note that when receiving SIGINT we turn off the LED and turn off via trap.
Learning to check mail for new messages

 sub mymail(){ use Mail::IMAPClient; use IO::Socket::SSL; while (42){ my $user = 'login'; my $server = 'server.ltd'; my $pass = 'P@ssw0rd'; my $socket = IO::Socket::SSL->new( PeerAddr => $server, PeerPort => 993, ) or die "socket(): $@"; my $imap = Mail::IMAPClient->new( Socket => $socket, Server => $server, User => $user, Password => $pass, Debug => 0) or die "Cannot connect to $server as $user: $@"; my $not_read; foreach my $f ($imap->folders) { my $i = $imap->unseen_count($f)||0 ; $not_read += $i; } $imap->logout(); print $not_read; } } 

Here we connect the necessary modules (Mail :: IMAPClient is not included in the standard delivery, it will need to be delivered separately) and run an infinite loop that 1) connects to imap to the server, receives the number of unread messages in the INBOX directory and its subdirectories and returns their number, waiting minute and starts from the beginning.

Friends all together

Here I would like to stay more. We will run 2 threads. The first will check mail and send the number of unread messages to the second. The second one will receive messages from the first one to start / stop the blinking script with the cursor.
So:
 #!/usr/bin/perl #   use strict; use threads; #  use Thread::Queue; #       my $mypipe = Thread::Queue->new; #  #    threads->create(\&mymail); #   threads->create(\&myleds)->join; #     

join in the second thread means that we need to wait for the end of the work of this thread before continuing.
The code of the myleds function itself:
 sub myleds(){ my $beep = 0; #1     . my $mypid; # pid ,   while($_ = $mypipe->dequeue){ #     if ($_ > 1){ if ($beep == 0){ $mypid = `nohup beepScroll > /dev/null 2>&1 &echo \$!`; #     pid'   $beep = 1; } } else { `kill $mypid` if $mypid; #    ,      . $mypid = 0 if $mypid; $beep = 0; } } } 
, and in the mymail () function should be replaced
 prinr $not_read 
on
 $mypipe->enqueue($not_read+1) 
"$ not_read + 1" because if while receives 0, it will consider that the conditions are false.

The final version of the script
 #!/usr/bin/perl use strict; use threads; use Thread::Queue; my $mypipe = Thread::Queue->new; threads->create(\&mymail); threads->create(\&myleds)->join; sub mymail(){ use Mail::IMAPClient; use IO::Socket::SSL; while (42){ my $user = 'login'; my $server = 'server.ltd'; my $pass = 'P@ssw0rd'; my $socket = IO::Socket::SSL->new( PeerAddr => $server, PeerPort => 993, ) or die "socket(): $@"; my $imap = Mail::IMAPClient->new( Socket => $socket, Server => $server, User => $user, Password => $pass, Debug => 0) or die "Cannot connect to $server as $user: $@"; my $not_read; foreach my $f ($imap->folders) { my $i = $imap->unseen_count($f)||0 ; $not_read += $i; } $imap->logout(); $mypipe->enqueue($not_read+1); sleep 60; } #     ...     :) $mypipe->enqueue(undef); exit 1; } sub myleds(){ my $beep = 0; my $mypid; while($_ = $mypipe->dequeue){ if ($_ > 1){ if ($beep == 0){ $mypid = `nohup beepScroll > /dev/null 2>&1 &echo \$!`; $beep = 1; } } else { $beep = 0; `kill $mypid` if $mypid; $mypid = 0 if $mypid; } } } 

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


All Articles