main.c
changeset 10 73f4c12b6ffb
parent 9 c2517f8bf647
child 11 f74fff438888
equal deleted inserted replaced
9:c2517f8bf647 10:73f4c12b6ffb
   218 	foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data)) lua_rosterlist_callback, L);
   218 	foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data)) lua_rosterlist_callback, L);
   219 	return 1;
   219 	return 1;
   220 }
   220 }
   221 
   221 
   222 /// main.current_buddy
   222 /// main.current_buddy
   223 /// Returns jid of current selected buddy.
   223 /// Returns jid of current selected buddy or sets current buddy to buddy with specified jid.
   224 /// R: string
   224 /// A: string (optional)
       
   225 /// R: string (optional)
   225 static int lua_main_current_buddy (lua_State *L)
   226 static int lua_main_current_buddy (lua_State *L)
   226 {
   227 {
   227 	lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy)));
   228 	if (lua_gettop (L) > 0) { // Set
       
   229 		char *jid = jidtodisp (luaL_checkstring (L, 1));
       
   230 		scr_RosterSearch (jid);
       
   231 		g_free (jid);
       
   232 		return 0;
       
   233 	} else { // Get
       
   234 		lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy)));
       
   235 		return 1;
       
   236 	}
       
   237 }
       
   238 
       
   239 /// main.full_jid
       
   240 /// Returns full jid (with current resource) of specified buddy (or current, if not specified).
       
   241 /// Note, that if there are no resources online, it will return just what it got.
       
   242 /// A: string (jid, optional)
       
   243 /// R: string (jid)
       
   244 static int lua_main_full_jid (lua_State *L)
       
   245 {
       
   246 	GList *buddy;
       
   247 	GSList *resources;
       
   248 	GSList *resource;
       
   249 	if (lua_gettop (L) > 0)
       
   250 		buddy = buddy_search_jid (luaL_checkstring (L, 1));
       
   251 	else
       
   252 		buddy = current_buddy;
       
   253 	if (!buddy)
       
   254 		return 0;
       
   255 	resources = buddy_getresources (BUDDATA (buddy));
       
   256 	if (!resources) {
       
   257 		lua_pushstring (L, buddy_getjid (BUDDATA (buddy)));
       
   258 		return 1;
       
   259 	}
       
   260 	lua_pushfstring (L, "%s%c%s", buddy_getjid (BUDDATA (buddy)), JID_RESOURCE_SEPARATOR, g_slist_last(resources)->data);
       
   261 	for (resource = resources; resource; resource = g_slist_next (resource))
       
   262 		g_free (resource->data);
       
   263 	g_slist_free (resources);
   228 	return 1;
   264 	return 1;
   229 }
   265 }
   230 
   266 
   231 typedef struct {
   267 typedef struct {
   232 	lua_State *L;
   268 	lua_State *L;
   246 	lua_settable    (d->L, -3);
   282 	lua_settable    (d->L, -3);
   247 	lua_pushstring (d->L, "message");
   283 	lua_pushstring (d->L, "message");
   248 	lua_pushstring (d->L, buddy_getstatusmsg (d->buddy, resource));
   284 	lua_pushstring (d->L, buddy_getstatusmsg (d->buddy, resource));
   249 	lua_settable   (d->L, -3);
   285 	lua_settable   (d->L, -3);
   250 	lua_settable (d->L, -3);
   286 	lua_settable (d->L, -3);
       
   287 	g_free (resource);
   251 }
   288 }
   252 
   289 
   253 /// main.buddy_info
   290 /// main.buddy_info
   254 /// Returns a hash table with information on specified buddy.
   291 /// Returns a hash table with information on specified buddy.
   255 /// Table contains fields type, name, onserver and resources.
   292 /// Table contains fields type, name, onserver and resources.
   260 /// R: table
   297 /// R: table
   261 static int lua_main_buddy_info (lua_State *L)
   298 static int lua_main_buddy_info (lua_State *L)
   262 {
   299 {
   263 	char                  *jid   = jidtodisp (luaL_checkstring (L, 1));
   300 	char                  *jid   = jidtodisp (luaL_checkstring (L, 1));
   264 	GSList                *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
   301 	GSList                *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
       
   302 	GSList                *resources;
   265 	lua_state_and_buddy_t  snb;
   303 	lua_state_and_buddy_t  snb;
   266 	g_free (jid);
   304 	g_free (jid);
   267 
   305 
   268 	if (!buddy) {
   306 	if (!buddy) {
   269 		lua_pushnil (L);
   307 		lua_pushnil (L);
   282 	lua_settable    (L, -3);
   320 	lua_settable    (L, -3);
   283 	lua_pushstring  (L, "resources");
   321 	lua_pushstring  (L, "resources");
   284 	lua_createtable (L, 0, 0);
   322 	lua_createtable (L, 0, 0);
   285 	snb.L     = L;
   323 	snb.L     = L;
   286 	snb.buddy = BUDDATA (buddy);
   324 	snb.buddy = BUDDATA (buddy);
       
   325 	resources = buddy_getresources (BUDDATA (buddy));
   287 	g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb);
   326 	g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb);
   288 	lua_settable    (L, -3);
   327 	g_slist_free (resources);
       
   328 	lua_settable (L, -3);
   289 	
   329 	
   290 	return 1;
   330 	return 1;
   291 }
   331 }
   292 
   332 
   293 // XMPP DISCO FEATURES
   333 // XMPP DISCO FEATURES
   597 
   637 
   598 	g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy);
   638 	g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy);
   599 	return 0;
   639 	return 0;
   600 }
   640 }
   601 
   641 
       
   642 // BUDDY
       
   643 
       
   644 #if 0
       
   645 static int lua_buddy_name (lua_State *L)
       
   646 {
       
   647 	lua_buddy_t *buddy = luaL_checkbuddy (L, 1);
       
   648 	if (lua_gettop (L) > 1) { // Set
       
   649 		const char *name = luaL_checkstring (L, 2);
       
   650 		buddy_setname (buddy->obj, name);
       
   651 		return 0;
       
   652 	} else { // Get
       
   653 		const char *name = buddy_getname (buddy->obj);
       
   654 		if (name)
       
   655 			lua_pushstring (L, name);
       
   656 		else
       
   657 			lua_pushnil (L);
       
   658 		return 1;
       
   659 	}
       
   660 }
       
   661 #endif
       
   662 
   602 // MAIN INITIALIZATION CODE
   663 // MAIN INITIALIZATION CODE
   603 
   664 
   604 #ifdef LLM_LOG_HANDLER
   665 #ifdef LLM_LOG_HANDLER
   605 // FIXME: this should not be here
   666 // FIXME: this should not be here
   606 guint lua_lm_log_handler_id;
   667 guint lua_lm_log_handler_id;
   656 		return NULL;
   717 		return NULL;
   657 	} else
   718 	} else
   658 		return g_realloc (ptr, nsize);
   719 		return g_realloc (ptr, nsize);
   659 }
   720 }
   660 
   721 
       
   722 #define reg(NAME)                   \
       
   723 	{ #NAME, lua_main_##NAME },
   661 static const luaL_Reg lua_reg_main[] = {
   724 static const luaL_Reg lua_reg_main[] = {
   662 	{ "config_file",   lua_main_config_file   },
   725 	reg ( config_file )
   663 	{ "connection",    lua_main_connection    },
   726 	reg ( connection )
   664 	{ "log",           lua_main_log           },
   727 	reg ( log )
   665 	{ "option",        lua_main_option        },
   728 	reg ( option )
   666 	{ "add_feature",   lua_main_add_feature   },
   729 	reg ( add_feature )
   667 	{ "del_feature",   lua_main_del_feature   },
   730 	reg ( del_feature )
   668 	{ "add_completion",lua_main_add_completion},
   731 	reg ( add_completion )
   669 	{ "del_completion",lua_main_del_completion},
   732 	reg ( del_completion )
   670 	{ "command",       lua_main_command       },
   733 	reg ( command )
   671 	{ "print_info",    lua_main_print_info    },
   734 	reg ( print_info )
   672 	{ "beep",          lua_main_beep          },
   735 	reg ( beep )
   673 	{ "run",           lua_main_run           },
   736 	reg ( run )
   674 	{ "status",        lua_main_status        },
   737 	reg ( status )
   675 	{ "roster",        lua_main_roster        },
   738 	reg ( roster )
   676 	{ "current_buddy", lua_main_current_buddy },
   739 	reg ( current_buddy )
   677 	{ "buddy_info",    lua_main_buddy_info    },
   740 	reg ( full_jid )
   678 	{ "timer",         lua_main_timer         },
   741 	reg ( buddy_info )
   679 	{ "bgread",        lua_main_bgread        },
   742 	reg ( timer )
   680 	{ NULL,            NULL                   },
   743 	reg ( bgread )
       
   744 	{ NULL, NULL },
   681 };
   745 };
       
   746 #undef reg
       
   747 
       
   748 #if 0
       
   749 #define reg(name)                    \
       
   750 	{ #NAME, lua_buddy_##NAME },
       
   751 static const luaL_Reg lua_reg_buddy[] = {
       
   752 	reg ( jid )
       
   753 	reg ( group )
       
   754 	reg ( name )
       
   755 	reg ( nick )
       
   756 	reg ( in_room )
       
   757 	reg ( topic )
       
   758 	reg ( printstatus )
       
   759 	reg ( autowhois )
       
   760 	reg ( type )
       
   761 	reg ( subscription )
       
   762 	reg ( status )
       
   763 	reg ( message )
       
   764 	reg ( time )
       
   765 	reg ( priority )
       
   766 	reg ( events )
       
   767 	reg ( caps )
       
   768 	reg ( role )
       
   769 	reg ( realjid )
       
   770 	reg ( flags )
       
   771 	reg ( onserver )
       
   772 	reg ( resources )
       
   773 	{ "__gc", lua_buddy_gc },
       
   774 	{ NULL,   NULL         },
       
   775 };
       
   776 #undef reg
       
   777 #endif
       
   778 
       
   779 #if 0
       
   780 static const luaL_Reg lua_reg_roster[] = {
       
   781 	{ "__index",    lua_roster_index    },
       
   782 	{ "__newindex", lua_roster_newindex },
       
   783 	{ "__len",      lua_roster_len      },
       
   784 	{ NULL,         NULL                },
       
   785 };
       
   786 #endif
   682 
   787 
   683 const gchar *g_module_check_init (GModule *module)
   788 const gchar *g_module_check_init (GModule *module)
   684 {
   789 {
   685 	lua = lua_newstate (lua_alloc, NULL);
   790 	lua = lua_newstate (lua_alloc, NULL);
   686 	if (!lua) {
   791 	if (!lua) {
   692 
   797 
   693 	luaL_register (lua, "main", lua_reg_main);
   798 	luaL_register (lua, "main", lua_reg_main);
   694 	lua_pop (lua, 1); // XXX
   799 	lua_pop (lua, 1); // XXX
   695 	lua_register (lua, "dopath", lua_global_dopath);
   800 	lua_register (lua, "dopath", lua_global_dopath);
   696 	lua_register (lua, "print",  lua_global_print );
   801 	lua_register (lua, "print",  lua_global_print );
       
   802 
       
   803 #if 0
       
   804 	lua_newtable (lua);
       
   805 	luaL_newmetatable (lua, "mcabber.roster");
       
   806 	luaL_register (lua, NULL, lua_reg_roster);
       
   807 	lua_setmetatable (lua, -2);
       
   808 	lua_setglobal (lua, "roster");
       
   809 
       
   810 	lua_newuserdata (lua,1);
       
   811 	luaL_newmetatable (lua, "mcabber.buddy");
       
   812 	luaL_register (lua, NULL, lua_reg_buddy);
       
   813 #endif
   697 
   814 
   698 	cmd_add ("lua", "", 0, 0, (void (*) (char *p)) do_lua, lua);
   815 	cmd_add ("lua", "", 0, 0, (void (*) (char *p)) do_lua, lua);
   699 
   816 
   700 #ifdef LLM_LOG_HANDLER
   817 #ifdef LLM_LOG_HANDLER
   701 	// FIXME: this should not be here.
   818 	// FIXME: this should not be here.