templatecmd.c
changeset 14 5d8f38cc86ac
parent 11 32564a4369f2
child 16 e7c65942ce5c
equal deleted inserted replaced
13:459a9ee3da76 14:5d8f38cc86ac
    23 #include <mcabber/compl.h>
    23 #include <mcabber/compl.h>
    24 #include <mcabber/commands.h>
    24 #include <mcabber/commands.h>
    25 #include <mcabber/utils.h>
    25 #include <mcabber/utils.h>
    26 #include <mcabber/logprint.h>
    26 #include <mcabber/logprint.h>
    27 #include <mcabber/modules.h>
    27 #include <mcabber/modules.h>
       
    28 #include <mcabber/api.h>
    28 
    29 
    29 #include "config.h"
    30 #include "config.h"
    30 
    31 
    31 static void tcmd_init (void);
    32 void tcmd_init (void);
    32 static void tcmd_uninit (void);
    33 void tcmd_uninit (void);
    33 
    34 
    34 #define DESCRIPTION ( "Templated commands\nProvides command /templatecmd (and any commands, you define with it)" )
    35 #define DESCRIPTION ( "Templated commands\nProvides command /templatecmd (and any commands, you define with it)" )
    35 
    36 
    36 static module_info_t info_templatecmd_experimental = {
    37 static module_info_t info_templatecmd_dev = {
    37 	.branch      = "experimental",
    38 	.branch      = "dev",
    38 	.api         = 28,
    39 	.api         = 20,
    39 	.version     = PROJECT_VERSION,
    40 	.version     = PROJECT_VERSION,
    40 	.description = DESCRIPTION,
    41 	.description = DESCRIPTION,
    41 	.requires    = NULL,
    42 	.requires    = NULL,
    42 	.init        = tcmd_init,
    43 	.init        = tcmd_init,
    43 	.uninit      = tcmd_uninit,
    44 	.uninit      = tcmd_uninit,
    44 	.next        = NULL,
    45 	.next        = NULL,
    45 };
    46 };
    46 
    47 
    47 static module_info_t info_templatecmd_dev = {
       
    48 	.branch      = "dev",
       
    49 	.api         = 13,
       
    50 	.version     = PROJECT_VERSION,
       
    51 	.description = DESCRIPTION,
       
    52 	.requires    = NULL,
       
    53 	.init        = tcmd_init,
       
    54 	.uninit      = tcmd_uninit,
       
    55 	.next        = &info_templatecmd_experimental,
       
    56 };
       
    57 
       
    58 module_info_t info_templatecmd = {
    48 module_info_t info_templatecmd = {
    59 	.branch      = "0.10.0",
    49 	.branch      = "0.10.1",
    60 	.api         = 1,
    50 	.api         = 1,
    61 	.version     = PROJECT_VERSION,
    51 	.version     = PROJECT_VERSION,
    62 	.description = DESCRIPTION,
    52 	.description = DESCRIPTION,
    63 	.requires    = NULL,
    53 	.requires    = NULL,
    64 	.init        = tcmd_init,
    54 	.init        = tcmd_init,
    66 	.next        = &info_templatecmd_dev,
    56 	.next        = &info_templatecmd_dev,
    67 };
    57 };
    68 
    58 
    69 typedef struct {
    59 typedef struct {
    70 	gchar    *name;
    60 	gchar    *name;
    71 #ifdef HAVE_CMD_ID
    61 #ifdef MCABBER_API_HAVE_CMD_ID
    72 	gpointer  id;
    62 	gpointer  id;
    73 #endif
    63 #endif
    74 	gchar    *template;
    64 	gchar    *template;
    75 	int       maxarg;
    65 	int       maxarg;
    76 	gboolean  stararg;
    66 	gboolean  stararg;
    77 } tcmd_t;
    67 } tcmd_t;
    78 
    68 
    79 #ifdef HAVE_CMD_ID
    69 #ifdef MCABBER_API_HAVE_CMD_ID
    80 static gpointer tcmd_cmid = NULL;
    70 static gpointer tcmd_cmid = NULL;
    81 #ifdef HAVE_CMD_SAFE
       
    82 static gboolean tcmd_set_safe = FALSE;
    71 static gboolean tcmd_set_safe = FALSE;
    83 #endif
       
    84 #endif
    72 #endif
    85 
    73 
    86 static GSList *template_commands = NULL;
    74 static GSList *template_commands = NULL;
    87 
    75 
    88 static void tcmd_callback (char *arg, gpointer udata)
    76 static void tcmd_callback (char *arg, gpointer udata)
   186 
   174 
   187 			for (;*astart && *astart == ' '; ++astart);
   175 			for (;*astart && *astart == ' '; ++astart);
   188 
   176 
   189 			if (!*astart) { // delete tcmd
   177 			if (!*astart) { // delete tcmd
   190 				if (template_command) {
   178 				if (template_command) {
   191 #ifndef HAVE_CMD_ID
   179 #ifndef MCABBER_API_HAVE_CMD_ID
   192 					cmd_del (template_command -> name);
   180 					cmd_del (template_command -> name);
   193 #else
   181 #else
   194 					if (template_command -> id)
   182 					if (template_command -> id)
   195 						cmd_del (template_command -> id);
   183 						cmd_del (template_command -> id);
   196 #endif
   184 #endif
   234 				template_command -> stararg = stararg;
   222 				template_command -> stararg = stararg;
   235 			}
   223 			}
   236 
   224 
   237 			template_commands = g_slist_append (template_commands, template_command);
   225 			template_commands = g_slist_append (template_commands, template_command);
   238 
   226 
   239 #ifndef HAVE_CMD_ID
   227 #ifndef MCABBER_API_HAVE_CMD_ID
   240 			cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
   228 			cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
   241 #else
   229 #else
   242 			template_command -> id = cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
   230 			template_command -> id = cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
   243 #endif
   231 #endif
   244 		}
   232 		}
   245 	}
   233 	}
   246 }
   234 }
   247 
   235 
   248 
   236 
   249 static void tcmd_init (void)
   237 void tcmd_init (void)
   250 {
   238 {
   251 #ifndef HAVE_CMD_ID
   239 #ifndef MCABBER_API_HAVE_CMD_ID
   252 	cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
   240 	cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
   253 #else
   241 #else
   254 	tcmd_cmid = cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
   242 	tcmd_cmid = cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
   255 #ifdef HAVE_CMD_SAFE
       
   256 	tcmd_set_safe = cmd_set_safe ("templatecmd", TRUE);
   243 	tcmd_set_safe = cmd_set_safe ("templatecmd", TRUE);
   257 #endif
   244 #endif
   258 #endif
   245 }
   259 }
   246 
   260 
   247 void tcmd_uninit (void)
   261 static void tcmd_uninit (void)
       
   262 {
   248 {
   263 	GSList *tel;
   249 	GSList *tel;
   264 
   250 
   265 #ifndef HAVE_CMD_ID
   251 #ifndef MCABBER_API_HAVE_CMD_ID
   266 	cmd_del ("templatecmd");
   252 	cmd_del ("templatecmd");
   267 #else
   253 #else
   268 	if (tcmd_cmid)
   254 	if (tcmd_cmid)
   269 		cmd_del (tcmd_cmid);
   255 		cmd_del (tcmd_cmid);
   270 #ifdef HAVE_CMD_SAFE
       
   271 	if (tcmd_set_safe)
   256 	if (tcmd_set_safe)
   272 		cmd_set_safe ("templatecmd", FALSE);
   257 		cmd_set_safe ("templatecmd", FALSE);
   273 #endif
   258 #endif
   274 #endif
       
   275 
   259 
   276 	for (tel = template_commands; tel; tel = tel -> next) {
   260 	for (tel = template_commands; tel; tel = tel -> next) {
   277 		tcmd_t *template_command = (tcmd_t *) tel -> data;
   261 		tcmd_t *template_command = (tcmd_t *) tel -> data;
   278 #ifndef HAVE_CMD_ID
   262 #ifndef MCABBER_API_HAVE_CMD_ID
   279 		cmd_del (template_command -> name);
   263 		cmd_del (template_command -> name);
   280 #else
   264 #else
   281 		if (template_command -> id)
   265 		if (template_command -> id)
   282 			cmd_del (template_command -> id);
   266 			cmd_del (template_command -> id);
   283 #endif
   267 #endif