annotate libs/utils.lua @ 0:0d4d8b432980 default tip master

Initial commit.
author Kooda <kooda@upyum.com>
date Fri, 27 Aug 2010 17:00:32 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1 --
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2 --------------------------------------------------------------------------------
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3 -- FILE: utils.lua
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
4 -- USAGE: ./utils.lua
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
5 -- DESCRIPTION: A couple of functions
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
6 -- OPTIONS: ---
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
7 -- REQUIREMENTS: ---
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
8 -- BUGS: ---
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
9 -- NOTES: ---
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
10 -- AUTHOR: Adrien Ramos (M), <adrien.ramos@upyum.com>
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
11 -- COMPANY:
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
12 -- VERSION: 1.0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
13 -- CREATED: 07/08/2010 23:40:24 CEST
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
14 -- REVISION: ---
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
15 --------------------------------------------------------------------------------
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
16 --
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
17
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
18 local verse = require("verse");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
19 local st = require("util.stanza");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
20
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
21 -- Lit le fichier de configuration (par défaut config.lua) et stocke la configuration
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
22 -- dans la table config
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
23 function config_load(filename)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
24 local filename = filename or "config.lua"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
25 -- Config loading
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
26 local chunk, err = loadfile(filename);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
27 if not chunk then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
28 print("File or syntax error:", err);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
29 return 1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
30 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
31
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
32 local config = {};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
33 setfenv(chunk, setmetatable(config, {__index = _G}));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
34 local ok, err = pcall(chunk);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
35 if not ok then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
36 print("Error while processing config:", err);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
37 return 1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
38 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
39 setmetatable(config, nil)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
40 return config;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
41 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
42
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
43 function room_join(stream, room, nick)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
44 if not nick or not room or not stream then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
45 error("room_join needs the following arguments : stream room nick");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
46 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
47 s = st.presence{from=stream.username.."@"..stream.host.."/"..stream.resource, to = room.jid.."/"..nick};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
48 s:tag("x", {xmlns="http://jabber.org/protocol/muc"});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
49 if room.password then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
50 s:tag("password"):text(room.password);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
51 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
52 print("XML OUT:", s);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
53 stream:send(s);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
54 end