Example #1
0
/**
 * @brief Frees an article or a table of articles.
 *    @luaparam LuaArticle article to free
 * @luafunc rm( LuaArticle )
 */
int newsL_rm( lua_State *L )
{
   LuaArticle *Larticle;

   if (lua_istable(L, 1)) {
      lua_pushnil(L);
      while (lua_next(L, -2)) {
         Larticle = luaL_validarticle(L, -1);

         free_article(*Larticle);

         lua_pop(L, 1);
      }
   }
   else {
      Larticle = luaL_validarticle(L, 1);

      free_article(*Larticle);
   }

   /* If we're landed, we should regenerate the news buffer. */
   if (landed) {
      generate_news(faction_name(land_planet->faction));
      if (land_loaded) {
         bar_regen();
      }
   }

   return 1;
}
Example #2
0
/**
 * @brief Adds an article.
 * @usage news.add(faction,title,body,[date_to_rm, [date]])
 *
 * @usage s = news.add( "Empire", "Hello world!", "The Empire wishes to say hello!", 0 ) -- Adds an Empire specific article, with date 0.
 *
 *    @luaparam faction faction of the article, "Generic" for non-factional
 *    @luaparam title Title of the article
 *    @luaparam content What's in the article
 *    @luaparam date_to_rm date to remove the article
 *    @luaparam date What time to put, defaults to current date, use 0 to not use a date
 *    @luareturn The article matching name or nil if error.
 * @luafunc add( s )
 */
int newsL_add( lua_State *L )
{
   news_t *n_article;
   char *title, *content, *faction;
   ntime_t date, date_to_rm;

   title   = NULL;
   content = NULL;
   faction = NULL;

   date = ntime_get();
   date_to_rm = 50000000000000;

   /* If a table is passed in. ugly hack */
   if (lua_istable(L, 1)) {
      lua_pushnil(L);

      /* traverse table */
      while (lua_next(L, -2)) {
         /* traverse sub table */
         if (lua_istable(L, -1)) {
            lua_pushnil(L);
            while (lua_next(L, -2)) {
               if (lua_isnumber(L, -1)) {
                  if (date_to_rm)
                     date_to_rm = lua_tonumber(L, -1);
                  else
                     date = lua_tonumber(L, -1);
               }
               else if (lua_istime(L, -1)) {
                  if (date_to_rm)
                     date_to_rm = luaL_validtime(L, -1);
                  else
                     date = luaL_validtime(L, -1);
               }
               else if (lua_isstring(L, -1)) {
                  if (!faction)
                     faction = strdup(lua_tostring(L, -1));
                  else if (!title)
                     title = strdup(lua_tostring(L, -1));
                  else if (!content)
                     content = strdup(lua_tostring(L, -1));
               }

               lua_pop(L, 1);
            }

            if (title && content && faction)
               new_article(title, content, faction, date, date_to_rm);
            else
               WARN("Bad arguments");

            free(faction);
            free(title);
            free(content);
            faction = NULL;
            title = NULL;
            content = NULL;

            date = ntime_get();
            date_to_rm = 50000000000000;
         }

         lua_pop(L, 1);
      }

      lua_pop(L, 1);

      /* If we're landed, we should regenerate the news buffer. */
      if (landed) {
         generate_news(faction_name(land_planet->faction));
         if (land_loaded)
            bar_regen();
      }

      return 0;
   }

   if (!(lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))) {
      WARN("\nBad arguments, use "
           "addArticle(\"Faction\",\"Title\",\"Content\",[date,[date_to_rm]])");
      return 0;
   }

   faction = strdup(lua_tostring(L, 1));
   title = strdup(lua_tostring(L, 2));
   content = strdup(lua_tostring(L, 3));

   /* get date and date to remove, or leave at defaults*/
   if (lua_isnumber(L, 4) || lua_istime(L, 4)) {
      if (lua_istime(L, 4))
         date_to_rm = luaL_validtime(L, 4);
      else
         date_to_rm = lua_tonumber(L, 4);
   }

   if (lua_isnumber(L, 5) || lua_istime(L, 5)) {
      if (lua_istime(L, 5))
         date = luaL_validtime(L, 5);
      else
         date = lua_tonumber(L, 5);
   }

   if (title && content && faction)
      n_article = new_article(title, content, faction, date, date_to_rm);
   else
      WARN("Bad arguments");

   lua_pusharticle(L, n_article->id);

   free(title);
   free(content);
   free(faction);

   /* If we're landed, we should regenerate the news buffer. */
   if (landed) {
      generate_news(faction_name(land_planet->faction));
      if (land_loaded)
         bar_regen();
   }

   return 1;
}
Example #3
0
/**
 * @brief Loads the news.
 *
 * @return 0 on success.
 */
static int news_load (void)
{
   generate_news(faction_name(land_planet->faction));
   return 0;
}