Skip to content

Instantly share code, notes, and snippets.

@Leandros
Last active December 24, 2015 23:09
Show Gist options
  • Save Leandros/6877512 to your computer and use it in GitHub Desktop.
Save Leandros/6877512 to your computer and use it in GitHub Desktop.
Check the maildirs for unread mails and notifies the user about it.
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
string directories[] = { "/var/vmail/<domain>/<user>/cur",
"/var/vmail/<domain>/<user>/cur",
"/var/vmail/<domain>/<user>/cur",
"/var/vmail/<domain>/<user>/cur",
"/var/vmail/<domain>/<user>/cur" };
string fileNames[] = { "count_john",
"count_john",
"count_john",
"count_john",
"count_john" };
string deliveredTo[] = { "john@doe.com",
"john@doe.com",
"john@doe.com",
"john@doe.com",
"john@doe.com" };
void writeCurrentFileCount(const char *name, int count)
{
ofstream file(name);
if (file.is_open()) {
file << count;
file.close();
}
}
int readCurrentFileCount(const char *name)
{
ifstream file(name);
if (file.is_open()) {
string line;
getline(file, line);
file.close();
return atoi(line.c_str());
}
return 0;
}
void sendPush(int i)
{
string command ("/path/to/notifier ");
const char *exec = (command + deliveredTo[i]).c_str();
system(exec);
}
int main(int argc, char *argv[]) {
for (int i = 0; i < 5; i++) {
DIR *dir = opendir(directories[i].c_str());
if (dir) {
struct dirent *ent;
string fileName;
int count = 0;
while((ent = readdir(dir)) != NULL) {
fileName = ent->d_name;
if ((fileName.compare(".") == 0) || (fileName.compare("..") == 0)) {
// Skip!
} else {
count++;
}
}
if (count > readCurrentFileCount(fileNames[i].c_str())) {
sendPush(i);
writeCurrentFileCount(fileNames[i].c_str(), count);
}
} else {
cout << "Error opening directory" << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment