My first event handler

From ISPWiki
Jump to: navigation, search

Execute the eventlist function to learn a complete list of registered events.

Task: write a module to display information about the last access to the system.

First, we need to collect the information. Create a custom handler that will be executed every time a user logs in to the system. The auth function is executed and registers a unique session id.

COREmanager delivers two ways for performing this operation:

External handler

This method provides the easiest and most flexible tools that enable you to write a handler on any programming language, such as shell / batch file or С++, Delphi, Java and so on.

Its main disadvantage is that it consumes more system resources (RAM and CPU time) in comparison with the second method, especially it may influence the event handler '*' that is called with each request.

Metadata

First you need to describe a program that should be executed if an event occurs. Create the file core_mod_lastlogin.xml. For more information about the documents' XML that are used in COREmanager refer to the article: XML.

<?xml version="1.0" encoding="UTF8"?>
<mgrdata>
  <handler name="lastlogin" type="cgi">
    <event name="auth" after="yes"/>
  </handler>
</mgrdata>

Handler

Once the auth function is completed successfully, your handler addon/lastlogin will be called.

addon/lastlogin is the name of the executable file that will be called every time when a user logs in to the system. Following is the example on shell.

#!/bin/sh
if [ "$AUTH_USER" != "" ]; then
  WD=var/lastlogin
  if [ ! -d $WD ]; then
    mkdir $WD
  fi
  date >> $WD/$AUTH_USER
fi
echo "<doc/>"

Restart the panel and log in to the system.

C++ CORE API

This method allows the best performance, however we do not recommend that you use it unless you have enough experience in programming. Besides, you won't be able to write a transferable code that will be compatible with all platforms. You should have a separate build for each of them.

Example lastlogin.cpp:

#include <api/action.h>
#include <mgr/mgrfile.h>
#include <mgr/mgrjob.h>
#include <fstream>
#include <api/module.h>

#define WD  "var/lastlogin"

namespace {
using namespace isp_api;

class LastLoginEvent : public Event {
public:
    LastLoginEvent() : Event("auth") {}
    virtual void AfterExecute(Session &ses) const {
        if (!ses.auth.name().empty()) {
            if (!mgr_file::Exists(WD))
                new mgr_job::CreateDir(WD);
            std::ofstream out((WD"/" + ses.auth.name()).c_str(), std::ios::app);
            out << "test" << std::endl;//date();
        }
    }
};

MODULE_INIT(lastlogin, "") {
    new LastLoginEvent();
}
} // end of private namespace

Upload and configure the library. For more information, please refer to the articles Building custom components and Upload custom libraries.

What's next

No you have the log in the var/lastlogin directory for each user. It provides the information when it registered in the panel. Refer to the article My first function for more information on how to display that information in the COREmanager interface.