main.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 22 Feb 2009 23:14:24 +0200
changeset 0 65cbecad22b4
child 1 7d87d323c889
permissions -rw-r--r--
Initial commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
#include <glib.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
#include <gmodule.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
#include <lua.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
#include <lauxlib.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
#include <lualib.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
#include <stdio.h>       // popen, pclose, fileno
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
#include "logprint.h"    // scr_LogPrint
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
#include "screen.h"      // scr_Beep, scr_WriteIncomingMessage
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
#include "hbuf.h"        // HBB_PREFIX_INFO
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
#include "commands.h"    // process_command
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
#include "xmpp.h"        // xmpp_getstatus, xmpp_getstatusmsg, lconnection
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
#include "roster.h"      // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_*
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
#include "utils.h"       // from_utf8
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
#include "lua_util.h"    // mcabber_config_file, string2enum
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
#include "hooks.h"
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
#include "settings.h"
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
lua_State *lua = NULL;    // global lua state object
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
char *mcabber_config_filename (const char *file)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
  char *home = getenv("HOME");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
  if (!home)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
    return NULL;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
  return g_strconcat (home, "/.mcabber/", file ? file : "", NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
/// print
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
/// Prints it's arguments to log with default priority.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
/// A: message, message...
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
static int lua_global_print (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
  const char *message;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
  lua_concat (L, lua_gettop (L));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
  message = lua_tostring (L, -1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
  scr_LogPrint (LPRINT_LOGNORM, message);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
  lua_pop (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
/// dopath
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
/// Loads lua file from default location.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
/// A: string (filename, without ".lua")
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
static int lua_global_dopath (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
 // const char *name = luaL_checkstring (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
  // FIXME
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
/// main.config_file
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
/// Adds mcabber default config location path to config file name.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
/// Note: not sure, that this function will stay alive long.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
/// A: string (filename)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
/// R: string (full path)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
static int lua_main_config_file (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
  char *home = mcabber_config_filename (lua_isstring (L, 1) ?
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
                                        lua_tostring (L, 1) : NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
  lua_pop (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
  if (!home) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
    lua_pushstring (L, "Cannot find home dir!");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
    lua_error (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
  lua_pushstring (L, home);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
  g_free (home); // XXX
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
  return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
/// log print type
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
/// V: normal, log, debug, notutf8, lognorm
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
static const string2enum_t lua_lprint[] = {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
  { "normal",  LPRINT_NORMAL  },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
  { "log",     LPRINT_LOG     },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
  { "debug",   LPRINT_DEBUG   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
  { "notutf0", LPRINT_NOTUTF8 },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
  { NULL,      0              },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
static const string2enum_t lua_roster_type[] = {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
  { "user",    ROSTER_TYPE_USER    },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
  { "group",   ROSTER_TYPE_GROUP   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
  { "agent",   ROSTER_TYPE_AGENT   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
  { "room",    ROSTER_TYPE_ROOM    },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
  { "special", ROSTER_TYPE_SPECIAL },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
  { NULL,      0                   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
/// main.log
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
/// Prints message to log.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
/// A: log print type, message, message...
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
static int lua_main_log (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
  const char *message;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
  int type = luaL_checkenum_multi (L, 1, lua_lprint);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
  lua_concat (L, lua_gettop (L) - 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
  message = lua_tostring (L, -1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
  scr_LogPrint (type, message);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
/// main.option
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
/// Sets or gets value of mcabber option.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
/// You can specify nil as a value to delete option.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
/// XXX: Should we do types here?
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
/// A: string (option name), string (value, optional)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
/// R: string (value, optional)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
static int lua_main_option (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
  const char *name = luaL_checkstring (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
  if (lua_gettop (L) > 1) { // Set
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
    if (lua_type (L, 2) == LUA_TNIL)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
      settings_del (SETTINGS_TYPE_OPTION, name);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
    else {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
      const char *value = luaL_checkstring (L, 2);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
      settings_set (SETTINGS_TYPE_OPTION, name, value);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
    }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
    return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
  } else {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
    const char *value = settings_get (SETTINGS_TYPE_OPTION, name);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
    if (value)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
      lua_pushstring (L, value);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
    else
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
      lua_pushnil (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
    return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
/// main.connection
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
/// Returns lightuserdata of mcabber's loudmouth connection.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
/// This can be very useful with lua-loudmouth, and not much otherwise.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
/// R: lightuserdata
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
static int lua_main_connection (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
  lua_pushlightuserdata (L, lconnection);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
  return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
// Well, for now
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
// I cannot get how
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
// To use caps.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
/// main.add_feature
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
/// A: string (xmlns)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
static int lua_main_add_feature (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
//  xmpp_add_feature (luaL_checkstring (L, 1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
/// main.del_feature
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
/// A: stirng (xmlns)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
static int lua_main_del_feature (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
//  xmpp_del_feature (luaL_checkstring (L, 1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
typedef struct {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
  int reference;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
  lua_State *L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
} lua_command_callback_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
/// command function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
/// A: argument string
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
void lua_main_command_handler (char *args, lua_command_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
  lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
  lua_pushstring (cb->L, args);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
  if (lua_pcall (cb->L, 1, 0, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
    scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
                  lua_tostring (cb->L, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
    lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
/// main.add_command
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
/// A: string (command name), command function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
static int lua_main_add_command (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
  const char *name = luaL_checkstring (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
  lua_command_callback_t *cb;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
  luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
  cb = luaL_malloc (L, sizeof (lua_command_callback_t));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
  cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
  cb->L = L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
  cmd_add (name, "", 0, 0, (void (*) (char *p)) lua_main_command_handler, cb);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
/// main.print_info
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
/// Prints a system message to buddy's window.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
/// A: string (jid), string (message)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
static int lua_main_print_info (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
  char *to = jidtodisp (luaL_checkstring (L, 1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
  const char *message = luaL_checkstring (L, 2);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
  scr_WriteIncomingMessage (to, message, 0, HBB_PREFIX_INFO, 0);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   202
  g_free (to);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   205
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   206
/// main.beep
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   207
/// Beeps with system speaker.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
static int lua_main_beep (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   209
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
  scr_Beep ();
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   211
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
/// main.run
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
/// Runs specified mcabber command.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   216
/// A: string
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   217
static int lua_main_run (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
  const char *command = luaL_checkstring (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
  process_command (command, TRUE);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
/// main.status
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
/// Returns your current status.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
/// R: char (status letter), string (status message)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
static int lua_main_status (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
  char *sm = from_utf8 (xmpp_getstatusmsg ());
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
  lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
  lua_pushstring  (L, sm);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
  g_free (sm);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
  return 2;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   235
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   236
// expects table on top!
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
static void lua_rosterlist_callback (gpointer buddy, lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
  lua_pushnumber (L, lua_objlen (L, -1) + 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   240
  lua_pushstring (L, buddy_getjid (buddy));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   241
  lua_settable (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   242
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
/// main.roster
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   245
/// Returns array of jids of buddies in roster.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   246
/// R: table
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   247
static int lua_main_roster (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   248
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   249
  lua_newtable (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   250
  foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data))lua_rosterlist_callback, L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   251
  return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   252
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   253
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
/// main.current_buddy
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
/// Returns jid of current selected buddy.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
/// R: string
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
static int lua_main_current_buddy (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   259
  lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy)));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
  return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
typedef struct {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
  lua_State *L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   265
  gpointer buddy;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   266
} lua_state_and_buddy_t; // :)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   267
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   268
// expects table on top!
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   269
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   270
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   271
  lua_pushstring  (d->L, resource);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   272
  lua_createtable (d->L, 0, 3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   273
  lua_pushstring  (d->L, "priority");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   274
  lua_pushnumber  (d->L, buddy_getresourceprio (d->buddy, resource));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   275
  lua_settable    (d->L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   276
  lua_pushstring  (d->L, "status");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   277
  lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   278
  lua_settable    (d->L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   279
  lua_pushstring  (d->L, "message");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   280
  lua_pushstring  (d->L, buddy_getstatusmsg (d->buddy, resource));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   281
  lua_settable    (d->L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   282
  lua_settable    (d->L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   283
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   284
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   285
/// main.buddy_info
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   286
/// Returns a hash table with information on specified buddy.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   287
/// Table contains fields type, name, onserver and resources.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   288
/// Resources is also a hash table, that contains tables with information,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   289
/// specific for each resource. In each resource table there are fields
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   290
/// priority, status and message.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   291
/// A: string (jid)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   292
/// R: table
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   293
static int lua_main_buddy_info (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   294
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   295
  char *jid = jidtodisp (luaL_checkstring (L, 1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   296
  GSList *buddy = roster_find (jid, jidsearch,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   297
                          ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   298
  lua_state_and_buddy_t snb;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   299
  g_free (jid);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   300
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   301
  if (!buddy) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   302
    lua_pushnil (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   303
    return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   304
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   305
  
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   306
  lua_createtable (L, 0, 3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   307
  lua_pushstring  (L, "type");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   308
  lua_pushstring  (L, enum2string (buddy_gettype (BUDDATA (buddy)),
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   309
                                   lua_roster_type));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   310
  lua_settable    (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   311
  lua_pushstring  (L, "name");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   312
  lua_pushstring  (L, buddy_getname (BUDDATA (buddy)));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   313
  lua_settable    (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   314
  lua_pushstring  (L, "onserver");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   315
  lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy)));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   316
  lua_settable    (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   317
  lua_pushstring  (L, "resources");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   318
  lua_createtable (L, 0, 0);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   319
  snb.L     = L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   320
  snb.buddy = BUDDATA (buddy);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   321
  g_slist_foreach (buddy_getresources (BUDDATA (buddy)),
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   322
                   (GFunc)lua_buddy_resources_callback, &snb);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   323
  lua_settable    (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   324
  
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   325
  return 1;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   326
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   327
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   328
// TIMER
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   329
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   330
#define LUA_TIMER_PRIORITY ( G_PRIORITY_HIGH_IDLE )
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   331
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   332
typedef struct {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   333
  int reference;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   334
  lua_State *L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   335
} lua_timer_callback_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   336
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   337
static void lua_timer_callback_destroy (lua_timer_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   338
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   339
  luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   340
  luaL_free (cb->L, cb);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   341
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   342
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   343
/// timer function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   344
/// Function, that will be called periodically until it returns false.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   345
/// R: boolean
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   346
static gboolean lua_timer_callback (lua_timer_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   347
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   348
  int ret;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   349
  lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   350
  if (lua_pcall (cb->L, 0, 1, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   351
    scr_LogPrint (LPRINT_LOGNORM, "lua: Timer callback execution error: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   352
                  lua_tostring (cb->L, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   353
    lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   354
    return FALSE;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   355
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   356
  ret = lua_toboolean (cb->L, -1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   357
  lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   358
  return ret;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   359
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   360
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   361
/// main.timer
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   362
/// Creates new timer function, that will be called periodically.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   363
/// A: integer (interval, seconds), timer function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   364
static int lua_main_timer (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   365
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   366
  int interval = luaL_checkint (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   367
  lua_timer_callback_t *cb;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   368
  luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   369
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   370
  cb = luaL_malloc (L, sizeof (lua_timer_callback_t));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   371
  cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   372
  cb->L         = L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   373
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   374
  g_timeout_add_seconds_full (LUA_TIMER_PRIORITY, interval,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   375
                              (GSourceFunc)lua_timer_callback, cb,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   376
                              (GDestroyNotify)lua_timer_callback_destroy);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   377
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   378
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   379
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   380
// BACKGROUND PIPE READING
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   381
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   382
#define LUA_BGREAD_BUFFER ( 4096 )
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   383
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   384
typedef struct {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   385
  lua_State *L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   386
  FILE      *fd;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   387
  int        reference;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   388
} lua_bgread_callback_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   389
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   390
static gchar lua_bgread_buffer[LUA_BGREAD_BUFFER];
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   391
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   392
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   393
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   394
  luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   395
  pclose (cb->fd);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   396
  luaL_free (cb->L, cb);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   397
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   398
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   399
/// background reading function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   400
/// Function, that processes output from pipe in asynchroneous way.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   401
/// A: string (data) or nil (eof)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   402
/// R: boolean (false if reading should be terminated)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   403
static gboolean
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   404
lua_bgread_callback (GIOChannel *source, GIOCondition condition,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   405
                     lua_bgread_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   406
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   407
  int ret = TRUE;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   408
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   409
  if (condition | G_IO_IN) { // data
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   410
    while (TRUE) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   411
      gsize read = 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   412
      g_io_channel_read_chars(source, lua_bgread_buffer, LUA_BGREAD_BUFFER,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   413
                              &read, NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   414
      if (!read) // exausted
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   415
        break;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   416
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   417
      lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   418
      lua_pushlstring (cb->L, lua_bgread_buffer, read);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   419
      if (lua_pcall (cb->L, 1, 1, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   420
        scr_LogPrint (LPRINT_LOGNORM,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   421
                      "lua: Bgread callback execution error: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   422
                      lua_tostring (cb->L, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   423
        lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   424
        return FALSE;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   425
      }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   426
      ret = lua_toboolean (cb->L, -1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   427
      lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   428
      if (!ret) // enough
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   429
        return FALSE;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   430
    }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   431
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   432
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   433
  if (condition & ~G_IO_IN) { // err or hup
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   434
    lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   435
    lua_pushnil (cb->L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   436
    if (lua_pcall (cb->L, 1, 1, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   437
      scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   438
                    lua_tostring (cb->L, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   439
      lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   440
      return FALSE;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   441
    }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   442
    ret = lua_toboolean (cb->L, -1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   443
    lua_pop (cb->L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   444
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   445
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   446
  return ret;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   447
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   448
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   449
/// main.bgread
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   450
/// Runs specified command and passes it's output to given function.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   451
/// A: string (command), background reading function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   452
static int lua_main_bgread (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   453
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   454
  const char *command = luaL_checkstring (L, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   455
  lua_bgread_callback_t *cb;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   456
  FILE *fd;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   457
  GIOChannel *channel;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   458
  const char *charset = NULL;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   459
  luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   460
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   461
  fd = popen (command, "r");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   462
  if (!fd) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   463
    lua_pushstring (L, "Error opening pipe");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   464
    lua_error (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   465
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   466
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   467
  channel = g_io_channel_unix_new (fileno (fd));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   468
// We, most likely, need this,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   469
// But we cannot use this,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   470
// It will block.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   471
//if (!g_get_charset (&charset))
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   472
  g_io_channel_set_encoding (channel, charset, NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   473
  g_io_channel_set_buffered (channel, FALSE);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   474
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   475
  g_io_channel_set_close_on_unref (channel, TRUE);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   476
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   477
  cb = luaL_malloc (L, sizeof (lua_bgread_callback_t));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   478
  cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   479
  cb->L         = L;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   480
  cb->fd        = fd;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   481
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   482
  g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   483
                       G_IO_IN|G_IO_HUP|G_IO_ERR,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   484
                       (GIOFunc)lua_bgread_callback, cb,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   485
                       (GDestroyNotify)lua_bgread_callback_destroy);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   486
  return 0;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   487
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   488
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   489
// MAIN INITIALIZATION CODE
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   490
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   491
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   492
  if (nsize == 0) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   493
    g_free (ptr);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   494
    return NULL;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   495
  } else
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   496
    return g_realloc (ptr, nsize);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   497
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   498
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   499
static void lua_hook (hk_arg_t *args, lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   500
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   501
  hk_arg_t *arg = args;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   502
  lua_getglobal (lua, "hook_handler");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   503
  if (!lua_isfunction (lua, -1)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   504
    lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   505
    return;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   506
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   507
  lua_newtable (L);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   508
  while (arg->name != NULL) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   509
    lua_pushstring (L, arg->name);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   510
    lua_pushstring (L, arg->value);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   511
    lua_settable (L, -3);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   512
    arg++;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   513
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   514
  if (lua_pcall (lua, 1, 0, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   515
    scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_handler: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   516
                  lua_tostring (lua, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   517
    lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   518
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   519
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   520
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   521
static const luaL_Reg lua_reg_main[] = {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   522
  { "config_file",   lua_main_config_file   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   523
  { "connection",    lua_main_connection    },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   524
  { "log",           lua_main_log           },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   525
  { "option",        lua_main_option        },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   526
  { "add_feature",   lua_main_add_feature   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   527
  { "del_feature",   lua_main_del_feature   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   528
  { "add_command",   lua_main_add_command   },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   529
  { "print_info",    lua_main_print_info    },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   530
  { "beep",          lua_main_beep          },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   531
  { "run",           lua_main_run           },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   532
  { "status",        lua_main_status        },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   533
  { "roster",        lua_main_roster        },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   534
  { "current_buddy", lua_main_current_buddy },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   535
  { "buddy_info",    lua_main_buddy_info    },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   536
  { "timer",         lua_main_timer         },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   537
  { "bgread",        lua_main_bgread        },
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   538
  { NULL,            NULL                   }, // end
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   539
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   540
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   541
const gchar *g_module_check_init (GModule *module)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   542
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   543
  char *initfile;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   544
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   545
  lua = lua_newstate(lua_alloc, NULL);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   546
  if (!lua) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   547
    scr_LogPrint(LPRINT_LOGNORM, "lua: Initialization error");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   548
    return;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   549
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   550
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   551
  luaL_openlibs(lua);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   552
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   553
  luaL_register(lua, "main", lua_reg_main);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   554
  lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   555
// lua_register(lua, "dopath", lua_global_dopath);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   556
  lua_register(lua, "print", lua_global_print);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   557
  
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   558
  initfile = mcabber_config_filename ("mcabberrc.lua");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   559
  if (!initfile)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   560
    scr_LogPrint(LPRINT_LOGNORM, "lua: Cannot determine config file name");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   561
  else {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   562
    int err = luaL_loadfile(lua, initfile);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   563
    if (err)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   564
      scr_LogPrint(LPRINT_LOGNORM, "lua: Unable to compile rc file (%d)", err);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   565
    else if (lua_pcall(lua, 0, LUA_MULTRET, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   566
      scr_LogPrint(LPRINT_LOGNORM, "lua: Runtime error in rc file: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   567
          lua_tostring(lua, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   568
      lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   569
    } else
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   570
      scr_LogPrint(LPRINT_LOGNORM, "Loaded mcabberrc.lua");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   571
    g_free(initfile); // XXX
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   572
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   573
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   574
  hk_add_handler ((hk_handler_t) lua_hook, lua);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   575
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   576
  lua_getglobal (lua, "hook_start");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   577
  if (!lua_isfunction (lua, -1))
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   578
    lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   579
  else if (lua_pcall (lua, 0, 0, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   580
    scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_start: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   581
                  lua_tostring (lua, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   582
    lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   583
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   584
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   585
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   586
void g_module_unload (GModule *module)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   587
{
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   588
  if (lua) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   589
    lua_getglobal (lua, "hook_quit");
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   590
    if (!lua_isfunction (lua, -1))
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   591
      lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   592
    else if (lua_pcall (lua, 0, 0, 0)) {
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   593
      scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook_quit: %s",
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   594
                    lua_tostring (lua, -1));
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   595
      lua_pop (lua, 1);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   596
    }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   597
  
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   598
    hk_del_handler ((hk_handler_t) lua_hook, lua);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   599
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   600
    lua_close (lua);
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   601
    lua = NULL;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   602
  }
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   603
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   604
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   605
/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */