root/Scripts/branches/0180_TM_ENHANCE_MUDBOT_QUESTING/Lua/experiments/mudbot/logging.lua @ 3445

Revision 3445, 2.9 KB (checked in by trichards, 7 months ago)

Added support for logging to mudbot.

Line 
1
2
3function reload(packageName)
4    package.loaded[packageName] = nil;
5    require(packageName);
6   
7    if package.loaded[packageName] ~= nil then
8        print("Reloaded " .. packageName);
9    else
10        print("Error reloading " .. packageName);
11    end
12end
13
14local channels = {}
15
16local function logHandler()
17    local text = bot.connection.getText();
18    print("Got message " .. text)
19
20    -- Parse the message
21    local first, last, nick = string.find(bot.connection.getCmd(1), "(.-)!")
22    --local first, last, part1, part2 = string.find(bot.connection.getText(), "^(.-):(.-)$")
23    local channel = bot.connection.getChannel();
24    local nick;
25
26    if first ~= nil then
27        if part1 == bot.nickname then
28            first, last, nick = string.find(bot.connection.getCmd(1), "(.-)!")
29            if nick == nil then 
30                print("Error parsing nick")
31            else
32                if channel == nickname then
33                    channel = nick;
34                end
35            end
36        end
37    end
38   
39    print("first: " .. (first or ""));
40    print("last: " .. (last or ""));
41    print("nick: " .. (nick or ""));
42   
43    -- Skip non-channel messages
44    if (channel == nil) then
45        return;
46    end
47   
48    function getLogFileName()
49        local fixedChannel = channel;
50       
51        -- If on Windows, chomp the "#" character
52        if (os.getenv("OS") == "Windows_NT") then
53            fixedChannel = string.sub(channel, 2);
54        else
55            fixedChannel = channel .. ".";
56        end
57        return bot.logDirectory .. channel .. "." .. os.date("%Y%m%d") .. ".log";
58    end
59
60    local logChannelData = channels[channel]
61
62    -- Check to see if this is the first time we've seen the channel.
63    if (logChannelData == nil) then
64        logChannelData = 
65        {
66            channel = channel;
67        }
68        channels[channel] = logChannelData;
69    end
70
71    -- Always recompute the file name
72    logChannelData.fileName = getLogFileName();
73
74    -- Open the file
75    local file = io.open(logChannelData.fileName, "a");
76
77    if (file ~= nil) then
78   
79        local message = os.date("%Y-%m-%dT%X  ");
80       
81        first, last, action = string.find(text, "^\001ACTION (.-)\001$");
82       
83        if (action ~= nil) then
84            message = message .. "* " .. nick .. " " .. action;
85        else
86            message = message .. "<" .. nick .. "> " .. text;
87        end
88       
89        -- Write the data
90        file:write(message .. "\n");
91
92        -- Close the file.
93        -- Opening and closing the file each time is less efficient, but
94        -- that keeps us from having too many files open, and it
95        -- prevents the file from being locked all of the time.
96        file:close();
97    else
98        print("Error opening file " .. logChannelData.fileName);
99    end
100end
101
102bot.connection.setLogHandler(logHandler);
Note: See TracBrowser for help on using the browser.