So the problem I had was that there wasn't a way to delay the script
until the user properties where set in serve side, since the mysql is
asynchronous the adduser even would fire without waiting on the result
from the query ..

but using the tip [email protected] we found out there is a way to
delay the events in server side Ape.. this is really important if you
are to user the Http class or the mysql class in Ape..
since both are going to be asynchronou..

Thanks a lot to Pablo because he was there when I most needed somebody
to extend me a hand..
and also to [email protected] since he just broken the big mystery in
this important issue..

And I have to say it works!!!!!!!!!!

revisited code.

var userlist = new $H;
var sql      = MySQLConnect('127.0.0.1', 'user',
'password', 'Ape_users')
Ape.registerHookCmd("connect", function(params, cmd) {
        if (!$defined(params) || !$defined(params.name)) return 0;
        if (userlist.has(params.name.toLowerCase())) return ["007",
"NICK_USED"];
        //if (params.name.length > 16 || params.name.test('[^a-zA-
Z0-9]',
'i')) return ["006", "BAD_NICK"];

        cmd.user.setProperty('name', params.name);
        cmd.user.setProperty('ip', cmd.ip);
        setloggedinProperties(cmd.user);
        return -1;

});

Ape.addEvent('adduser', function(user) {
        userlist.set(user.getProperty('name').toLowerCase(), true);

});

Ape.addEvent('deluser', function(user) {
        userlist.erase(user.getProperty('name').toLowerCase());
});

function setloggedinProperties(user){

var usernick = user.getProperty('name').toLowerCase();
sql.query('SELECT * FROM database WHERE BINARY
username="'+Ape.MySQL.escape(usernick)+'"',function(res,error){
                if(error){
                        Ape.log('There was an error in the query
'+this.errorString()+'');
                        }else{
                        var isauthorized;
                        if(res != ''){
                                isauthorized   = res[0].isauthorized;
                        }
                        if
(isauthorized)user.setProperty('isauthorized', isauthorized);


                     Ape.addUser(user);
                }
        });

}

    function MySQLConnect(ip, user, password, database) {
        var sql = new Ape.MySQL(ip + ":3306", user,
password,database);

        //onConnect callback
        sql.onConnect = function() {
            Ape.log('You are now connected to MySQL server');
        }

        //onError callback
        sql.onError = function(errorNo) {
            Ape.log('Connection error ' + errorNo +' '+
           this.errorString());
        }

        return sql;
    }

        //Set up a poller to send keep alive request each 2 minutes
          (function() {
            sql.query('SELECT 1', function(res, errorNo) {
            if (errorNo == 8) { //Something went wrong, connection has
been closed
            Ape.log('Something went wrong, connection has been
closed');
     sql = MySQLConnect('127.0.0.1', 'user',
'password','Ape_users'); //Reconnect to MySQL Server
            }
            }.bind(this));
          }).periodical(1000*60*2);

-- 
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

Reply via email to