comparison mod_muc_log_http/muc_log_http/mod_muc_log_http.lua @ 970:adcb751f22f3

mod_muc_log_http: Adapt to prosody 0.9 changes
author Florian Zeitz <florob@babelmonkeys.de>
date Sat, 06 Apr 2013 21:47:46 +0200
parents a634b116c694
children 61b63affd402
comparison
equal deleted inserted replaced
969:8eba9d4809d2 970:adcb751f22f3
3 -- This project is MIT/X11 licensed. Please see the 3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information. 4 -- COPYING file in the source package for more information.
5 -- 5 --
6 6
7 module:set_global(); 7 module:set_global();
8
9 module:depends("http");
8 10
9 local prosody = prosody; 11 local prosody = prosody;
10 local tabSort = table.sort; 12 local tabSort = table.sort;
11 local tonumber = _G.tonumber; 13 local tonumber = _G.tonumber;
12 local tostring = _G.tostring; 14 local tostring = _G.tostring;
18 local urlencode = require "net.http".urlencode; 20 local urlencode = require "net.http".urlencode;
19 local urldecode = require "net.http".urldecode; 21 local urldecode = require "net.http".urldecode;
20 local datamanager = require "util.datamanager"; 22 local datamanager = require "util.datamanager";
21 local data_load, data_getpath = datamanager.load, datamanager.getpath; 23 local data_load, data_getpath = datamanager.load, datamanager.getpath;
22 local datastore = "muc_log"; 24 local datastore = "muc_log";
23 local urlBase = "muc_log";
24 local config = nil; 25 local config = nil;
25 local tostring = _G.tostring; 26 local tostring = _G.tostring;
26 local tonumber = _G.tonumber; 27 local tonumber = _G.tonumber;
27 local os_date, os_time = os.date, os.time; 28 local os_date, os_time = os.date, os.time;
28 local str_format = string.format; 29 local str_format = string.format;
85 return t; 86 return t;
86 end 87 end
87 88
88 function createDoc(body, title) 89 function createDoc(body, title)
89 if not body then 90 if not body then
90 return { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; 91 return 404;
91 end 92 end
92 body = body:gsub("%%", "%%%%"); 93 body = body:gsub("%%", "%%%%");
93 return html.doc:gsub("###BODY_STUFF###", body) 94 return html.doc:gsub("###BODY_STUFF###", body)
94 :gsub("<title>muc_log</title>", "<title>"..(title and htmlEscape(title) or "Chatroom logs").."</title>"); 95 :gsub("<title>muc_log</title>", "<title>"..(title and htmlEscape(title) or "Chatroom logs").."</title>");
95 end 96 end
641 return tmp, "Chatroom logs for "..bareRoomJid.." ("..tostring(os_date("%A, %B %d, %Y", os_time(temptime)))..")"; 642 return tmp, "Chatroom logs for "..bareRoomJid.." ("..tostring(os_date("%A, %B %d, %Y", os_time(temptime)))..")";
642 end 643 end
643 end 644 end
644 end 645 end
645 646
646 function handle_request(method, body, request) 647 function handle_request(event, path)
647 local host, node, day, more = request.url.path:match("^/muc_log/+([^/]*)/*([^/]*)/*([^/]*)/*(.*)$"); 648 local request, response = event.request, event.response;
648 if more ~= "" then return { status = "404 Not found", body = "Unknown URL" }; end 649 local host, node, day, more = path:match("^([^/]*)/?([^/]*)/?([^/]*)/?(.*)$");
650 if more ~= "" then return 404; end
649 if host == "" then host = nil; end 651 if host == "" then host = nil; end
650 if node == "" then node = nil; end 652 if node == "" then node = nil; end
651 if day == "" then day = nil; end 653 if day == "" then day = nil; end
652 654
653 node = urldecode(node); 655 node = urldecode(node);
654 656
655 if not html.doc then return { status = "500 Internal Server Error", "MUC hosts or theme not loaded" }; end 657 if not html.doc then
656 658 response.status_code = 500;
657 if host and not(hosts[host] and hosts[host].modules.muc and hosts[host].modules.muc_log) then return { status = "404 Not found", body = "No such MUC component" }; end 659 return "MUC hosts or theme not loaded";
658 if host and node and not(hosts[host].modules.muc.rooms[node.."@"..host]) then return { status = "404 Not found", body = "No such MUC room" }; end 660 end
661
662 if host and not(hosts[host] and hosts[host].modules.muc and hosts[host].modules.muc_log) then
663 response.status_code = 404;
664 return "No such MUC component";
665 end
666 if host and node and not(hosts[host].modules.muc.rooms[node.."@"..host]) then
667 response.status_code = 404;
668 return "No such MUC room";
669 end
659 670
660 if not host then -- main component list 671 if not host then -- main component list
661 return createDoc(generateComponentListSiteContent()); 672 return createDoc(generateComponentListSiteContent());
662 elseif not node then -- room list for component 673 elseif not node then -- room list for component
663 return createDoc(generateRoomListSiteContent(host)); 674 return createDoc(generateRoomListSiteContent(host));
665 return createDoc(generateDayListSiteContentByRoom(node.."@"..host)); 676 return createDoc(generateDayListSiteContentByRoom(node.."@"..host));
666 else 677 else
667 if not day:match("^20(%d%d)-(%d%d)-(%d%d)$") then 678 if not day:match("^20(%d%d)-(%d%d)-(%d%d)$") then
668 local y,m,d = day:match("^(%d%d)(%d%d)(%d%d)$"); 679 local y,m,d = day:match("^(%d%d)(%d%d)(%d%d)$");
669 if not y then 680 if not y then
670 return { status = "404 Not found", body = "Unknown URL" }; 681 return 404;
671 end 682 end
672 return { status = "301 Moved Permanently", 683 response.headers.location = request.path .. "../20"..y.."-"..m.."-"..d.."/";
673 headers = { ["Location"] = request.url.path:match("^/muc_log/+[^/]*/*[^/]*").."/20"..y.."-"..m.."-"..d.."/" } }; 684 return 301;
674 end 685 end
675 local room = hosts[host].modules.muc.rooms[node.."@"..host]; 686 local room = hosts[host].modules.muc.rooms[node.."@"..host];
676 return createDoc(parseDay(node.."@"..host, room._data.subject or "", day)); 687 return createDoc(parseDay(node.."@"..host, room._data.subject or "", day));
677 end 688 end
678 end 689 end
702 end 713 end
703 end 714 end
704 return true; 715 return true;
705 end 716 end
706 717
707 function module.load() 718 function module.add_host(module)
708 config = config_get("*", "core", "muc_log_http") or {}; 719 config = config_get("*", "core", "muc_log_http") or {};
709 if config.showStatus == nil then 720 if config.showStatus == nil then
710 config.showStatus = true; 721 config.showStatus = true;
711 end 722 end
712 if config.showJoin == nil then 723 if config.showJoin == nil then
725 if not themeLoaded then 736 if not themeLoaded then
726 module:log("error", "Theme \"%s\" is missing something: %s", tostring(theme), err); 737 module:log("error", "Theme \"%s\" is missing something: %s", tostring(theme), err);
727 return false; 738 return false;
728 end 739 end
729 740
730 httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 }); 741 module:provides("http", {
731 end 742 name = "muc_log";
743 route = {
744 ["GET"] = function(event)
745 event.response.headers.location = event.request.path .. "/";
746 return 301;
747 end;
748 ["GET /*"] = handle_request;
749 }
750 });
751 end