1 /* Copyright (C) 2000-2005  Thomas Bopp, Thorsten Hampel, Ludger Merkens
     3  *  This program is free software; you can redistribute it and/or modify
     4  *  it under the terms of the GNU General Public License as published by
     5  *  the Free Software Foundation; either version 2 of the License, or
     6  *  (at your option) any later version.
     8  *  This program is distributed in the hope that it will be useful,
     9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  *  GNU General Public License for more details.
    13  *  You should have received a copy of the GNU General Public License
    14  *  along with this program; if not, write to the Free Software
    15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    17  * $Id: collect_users.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
    19 inherit "/kernel/module";
    22 #include <attributes.h>
    25 //! checks active users and possible moves them home
    26 class collect_users : public module{
    35 //#define DEBUG_COLLECT
    38 #define LOG_COLLECT(s, args...) werror("Collecting: " + s+ "\n", args)
    40 #define LOG_COLLECT(s, args...) 
    43  Thread.Queue userQueue = Thread.Queue();
    47     add_global_event(EVENT_LOGIN, user_login, PHASE_NOTIFY);
    48     start_thread(collect);
    51 void user_login(object obj, object user, int feature, int prev_features)
    53     LOG_COLLECT("User %O logged in ...", user);
    54     userQueue->write(user);
    58  * Check if a user needs cleanup and should be moved into her
    61  * @param object user - the user to check.
    64 void check_user_cleanup(object user)
    66     userQueue->write(user);
    69 void check_users_cleanup(array users)
    71     foreach(users, object u)
    77  int check_user(object user)
    79     LOG_COLLECT("Checking user %O", user);
    80     if ( !stringp(user->get_identifier()) )
    82     if ( user->get_status() == 0 ) {
    83    if ( user->get_environment() != user->query_attribute(USER_WORKROOM) ){
    84        LOG_COLLECT("Collect: Moving user %s", user->get_identifier());
    85        object wr = user->query_attribute(USER_WORKROOM);
    86        LOG_COLLECT("Found Workroom %O", wr);
    89            LOG_COLLECT("Moved !");
   104        LOG_COLLECT(" Checking users ...");
   108        while ( userQueue->size() > 0 ) {
   109            user = userQueue->read();
   110            if ( search(check_users, user) == -1 && 
   112                check_users += ({ user });
   114        foreach ( check_users, user)
   115            userQueue->write(user);
   116        // also check idle connections!
   118        foreach ( master()->get_users(), object socket ) {
   119          if ( !objectp(socket) ) continue;
   121          // do not close service connections
   122          if ( functionp(socket->get_user_object) && 
   123               socket->get_user_object() == USER("service") )
   126          if ( functionp(socket->get_last_response) ) 
   128              int t = time() - socket->get_last_response();
   129              if ( t > COAL_TIMEOUT ) {
   130                if ( !functionp(socket->get_ip) )
   131                    werror("Socket without IP function !");
   132                mixed e = catch(socket->close_connection());
   138      FATAL("Error on collect_users():\n %s\n%s", 
   139              err[0], describe_backtrace(err[1]));
   150 string get_identifier() { return "collect_users"; }