gfriloux / botman

Botman is a bot that makes servers management easier.

Home Page:http://gfriloux.github.io/botman

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make it easier to write modules with commands

gfriloux opened this issue · comments

In the present time, if you wish to write a module, you have to declare the commands using gotham_modules_command_add so we can list them in the help module, for example, and you have to create a function named event_citizen_command() which will get called by lib gotham for every received message (you will then have to parse to know if this command is for you).

It would be easier if, when using gotham_modules_command_add(), we could pass a function pointer that would be called if the message match the added command.

This would remove a few lines from modules, making it easier for new comers to understand how to write modules.

Example event_citizen_command() function :

Eina_Bool
event_citizen_command(void *data,
                      int type EINA_UNUSED,
                      void *ev)
{
   Module_Notification *notification = data;
   Gotham_Citizen_Command *command = ev;
   Gotham_Citizen *citizen = command->citizen;

   if (strcmp(command->name, ".notification")) return EINA_TRUE;

   DBG("notification[%p] command[%p][%s] citizen[%p][%s]",
       notification, command, command->name, citizen, citizen->jid);

   command->handled = EINA_TRUE;

   if (!command->command[1]) alfred_group_print(notification, command);
   else if ((command->command[1]) && (!strcmp(command->command[1], "groupadd")))
     {
        AUTH(notification, gotham_modules_command_get(".notification groupadd"), citizen);
        alfred_group_add(notification, command);
     }
   else if ((command->command[1]) && (!strcmp(command->command[1], "groupdel")))
     {
        AUTH(notification, gotham_modules_command_get(".notification groupdel"), citizen);
        alfred_group_del(notification, command);
     }
   else if ((command->command[1]) && (!strcmp(command->command[1], "useradd")))
     {
        AUTH(notification, gotham_modules_command_get(".notification useradd"), citizen);
        alfred_user_add(notification, command);
     }
   else if ((command->command[1]) && (!strcmp(command->command[1], "userdel")))
     {
        AUTH(notification, gotham_modules_command_get(".notification userdel"), citizen);
        alfred_user_del(notification, command);
     }
   else if ((command->command[1]) && (!strcmp(command->command[1], "send")))
     {
        AUTH(notification, gotham_modules_command_get(".notification send"), citizen);
        alfred_send(notification, command);
     }
   else
     DBG("WHAT SHOULD I DO ?");

   return EINA_TRUE;
}

I think anyone would agree if we could just remove all those strcmp.

I am also work on moving the authentication checks into libgotham so modules doesnt deal with this themselves.

This will make a single point in the code that deals with this, so module writers can just forget about it.