main.c
changeset 9 c2517f8bf647
parent 8 fc9060b9b7cc
child 10 73f4c12b6ffb
equal deleted inserted replaced
8:fc9060b9b7cc 9:c2517f8bf647
   320 	return 0;
   320 	return 0;
   321 }
   321 }
   322 
   322 
   323 // MCABBER COMMANDS
   323 // MCABBER COMMANDS
   324 
   324 
       
   325 /// completion type
       
   326 /// Built-it completion types can be specified as string, instead of id.
       
   327 /// G:
       
   328 static const string2enum_t lua_completion_type[] = {
       
   329 	{ "cmd",       COMPL_CMD       },
       
   330 	{ "jid",       COMPL_JID       },
       
   331 	{ "urljid",    COMPL_URLJID    },
       
   332 	{ "name",      COMPL_NAME      },
       
   333 	{ "status",    COMPL_STATUS    },
       
   334 	{ "filename",  COMPL_FILENAME  },
       
   335 	{ "roster",    COMPL_ROSTER    },
       
   336 	{ "buffer",    COMPL_BUFFER    },
       
   337 	{ "group",     COMPL_GROUP     },
       
   338 	{ "groupname", COMPL_GROUPNAME },
       
   339 	{ "multiline", COMPL_MULTILINE },
       
   340 	{ "room",      COMPL_ROOM      },
       
   341 	{ "resource",  COMPL_RESOURCE  },
       
   342 	{ "auth",      COMPL_AUTH      },
       
   343 	{ "request",   COMPL_REQUEST   },
       
   344 	{ "events",    COMPL_EVENTS    },
       
   345 	{ "eventsid",  COMPL_EVENTSID  },
       
   346 	{ "pgp",       COMPL_PGP       },
       
   347 	{ "color",     COMPL_COLOR     },
       
   348 	{ "otr",       COMPL_OTR       },
       
   349 	{ "ortpolicy", COMPL_OTRPOLICY },
       
   350 	{ NULL,        0               },
       
   351 };
       
   352 
   325 typedef struct {
   353 typedef struct {
   326 	int        reference;
   354 	int        reference;
   327 	lua_State *L;
   355 	lua_State *L;
   328 } lua_command_callback_t;
   356 } lua_command_callback_t;
   329 
   357 
   364 
   392 
   365 /// main.command
   393 /// main.command
   366 /// Associates or breaks association between mcabber command name and lua function.
   394 /// Associates or breaks association between mcabber command name and lua function.
   367 /// To unregister command omit function argument.
   395 /// To unregister command omit function argument.
   368 /// If you specify a third argument, table (even empty), function will return completion group id or nothing.
   396 /// If you specify a third argument, table (even empty), function will return completion group id or nothing.
   369 /// You can also specify an integer instead of table, then no new id will be registered.
   397 /// You can also specify a string name (see completion type) instead of table, for non-builtin, you can just pass integer id.
   370 /// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily.
   398 /// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily.
   371 /// Also note, that it ignores keys in a completion table.
   399 /// Also note, that it ignores keys in a completion table.
   372 /// A: string (command name), command function (optional), table (completions, optional)/integer (comletion group id, optional)
   400 /// A: string (command name), command function (optional), table (completions, optional)/completion type (or integer comletion group id, optional)
   373 /// R: integer (completion group id, optional) or nil
   401 /// R: completion type (integer completion group id or string for builtin types, optional)
   374 static int lua_main_command (lua_State *L)
   402 static int lua_main_command (lua_State *L)
   375 {
   403 {
   376 	const char             *name = luaL_checkstring (L, 1);
   404 	const char             *name = luaL_checkstring (L, 1);
   377 	lua_command_callback_t *cb;
   405 	lua_command_callback_t *cb;
   378 	if (lua_gettop (L) > 1) { // Register
   406 	if (lua_gettop (L) > 1) { // Register
   388 						compl_add_category_word (cid, luaL_checkstring (L, -1));
   416 						compl_add_category_word (cid, luaL_checkstring (L, -1));
   389 						lua_pop (L, 1);
   417 						lua_pop (L, 1);
   390 					}
   418 					}
   391 				}
   419 				}
   392 			} else
   420 			} else
   393 				cid = luaL_checkinteger (L, 3);
   421 				cid = luaL_checkenum (L, 3, lua_completion_type);
   394 		}
   422 		}
   395 
   423 
   396 		cb = luaL_malloc (L, sizeof (lua_command_callback_t));
   424 		cb = luaL_malloc (L, sizeof (lua_command_callback_t));
   397 		lua_pushvalue (L, 2);
   425 		lua_pushvalue (L, 2);
   398 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   426 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   400 		cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
   428 		cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
   401 
   429 
   402 		lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name));
   430 		lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name));
   403 
   431 
   404 		if (cid) {
   432 		if (cid) {
   405 			lua_pushinteger (L, cid);
   433 			luaL_pushenum (L, cid, lua_completion_type);
   406 			return 1;
   434 			return 1;
   407 		}
   435 		}
   408 	} else { // Unregister
   436 	} else { // Unregister
   409 		GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0);
   437 		GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0);
   410 
   438