Ejemplo n.º 1
0
        public static TextWriter HtmlStreamWriter(HttpListenerContext context)
        {
            if (tl_MultiPage != null)
            {
                return(tl_MultiPage);
            }
            Stream     s      = context.Response.OutputStream;
            TextWriter writer = new StreamWriter(s);

            if (tl_AsHTML)
            {
                var writer1 = writer;
                WriteHtmlPreBody(writer, tl_title);
                WebLinksWriter writer2 = WebLinksWriter.EnsureWriteLinksWriter(writer, null);
                writer          = writer2;
                writer2.OnClose = () =>
                {
                    tl_AsHTML    = true;
                    tl_BodyDepth = 1;
                    WriteHtmlPostBody(writer1);
                };
            }
            return(AddWarnWriter(writer));
        }
Ejemplo n.º 2
0
        public virtual void HandleTheClientNew(HttpListenerContext context, int requestNumber)
        {
            HttpListenerRequest request = context.Request;
            bool useHtml = false;

            if (DLRConsole.IsDougsMachine)
            {
                useHtml = true;
            }

            string requestData = GetRequestString(request);
            //  UUID capsID;
            bool success;

            if (requestData == null)
            {
                requestData = request.Url.PathAndQuery;
            }

            string path  = requestData;
            string pathd = HttpUtility.UrlDecode(path);

            LogInfo("_listener " + path + " from " + request.RemoteEndPoint);

            if (request.Url.AbsolutePath.EndsWith(".ico"))
            {
                WriteCodeAndClose(context, HttpStatusCode.NotFound);
                return;
            }

            NameValueCollection getvars  = request.QueryString;
            NameValueCollection postvars = requestData == null ? null : WebLinksWriter.ParseQueryString(requestData);

            string botname = GetVariable(getvars, postvars, "bot", () => GetVariable(getvars, postvars, "botid", null));

            ScriptExecutor _botClient = clientManager.GetScriptExecuter(botname);

            if (_botClient == null)
            {
                WriteCodeAndClose(context, HttpStatusCode.ServiceUnavailable);
                return;
            }

            var response = new StringWriter(); //new WriteLineToResponse(this, response);

            // Micro-posterboard
            if (pathd.StartsWith("/posterboard"))
            {
                string slot  = path;
                string value = "";
                value = _botClient.getPosterBoard(slot) as string;
                if (value != null)
                {
                    if (value.Length > 0)
                    {
                        LogInfo(String.Format(" board response: {0} = {1}", slot, value));
                    }
                }
                AddToBody(response, "<xml>");
                AddToBody(response, "<slot>");
                AddToBody(response, "<path>" + path + "</path>");
                AddToBody(response, "<value>" + (value ?? "") + "</value>");
                AddToBody(response, "</slot>");
                AddToBody(response, "</xml>");

                WriteResponse(context, response.ToString());
                return;
            }



            if (path.StartsWith("/?") || path.ToLower().StartsWith("/test"))
            {
                useHtml = true;
            }
            try
            {
                if (useHtml)
                {
                    AddToBody(response, "<html>");
                    AddToBody(response, "<head>");
                    botname = GetVariable(getvars, postvars, "bot", () => _botClient.GetName());

                    AddToBody(response, "<title>" + botname + "</title>");
                    AddToBody(response, "</head>");
                    AddToBody(response, "<body>");
                    AddToBody(response, "<pre>");
                    WriteKeyValues(getvars, response);
                    WriteKeyValues(postvars, response);
                    AddToBody(response, "</pre>");
                    AddToBody(response, "<a href='" + request.Url.PathAndQuery + "'>"
                              + request.Url.PathAndQuery + "</a>");
                    AddToBody(response, "<pre>");
                }


                string cmd = GetVariable(getvars, postvars, "cmd", () => "MeNe");

                CmdResult res;
                // this is our default handler
                if (cmd != "MeNe")
                {
                    res = _botClient.ExecuteXmlCommand(cmd + " " + GetVariable(getvars, postvars, "args", () => ""),
                                                       request, response.WriteLine);
                }
                else
                {
                    AddToBody(response, "<xml>");
                    AddToBody(response, "\n<!-- Begin Response !-->");
                    // this is our MeNe handler
                    string username = GetVariable(getvars, postvars, "username",
                                                  () => GetVariable(getvars, postvars, "ident", null));
                    string saytext = GetVariable(getvars, postvars, "saytext", () => "missed the post");
                    string text    = GetVariable(getvars, postvars, "text",
                                                 () => GetVariable(getvars, postvars, "entry", () => pathd.TrimStart('/')));
                    if (text.Contains("<sapi>"))
                    {
                        // example fragment
                        // <sapi> <silence msec="100" /> <bookmark mark="anim:hello.csv"/> Hi there </sapi>
                        text = text.Replace("<sapi>", "");
                        text = text.Replace("</sapi>", "");
                        while (text.Contains("<"))
                        {
                            int p1 = text.IndexOf("<");
                            int p2 = text.IndexOf(">", p1);
                            if (p2 > p1)
                            {
                                string fragment = text.Substring(p1, (p2 + 1) - p1);
                                text = text.Replace(fragment, " ");
                            }
                        }
                    }

                    if (String.IsNullOrEmpty(username))
                    {
                        //res = _botClient.ExecuteCommand(cmd + " " + text, wrresp.WriteLine);
                        res = _botClient.ExecuteCommand("aiml @withuser " + defaultUser + " - " + text, request,
                                                        response.WriteLine, CMDFLAGS.Foregrounded);
                    }
                    else
                    {
                        res = _botClient.ExecuteCommand("aiml @withuser " + username + " - " + text, request,
                                                        response.WriteLine, CMDFLAGS.Foregrounded);
                    }
                    AddToBody(response, "");
                    AddToBody(response, "\n<!-- End Response !-->");
                    AddToBody(response, "</xml>");
                }
                if (useHtml)
                {
                    AddToBody(response, "</pre>");
                    AddToBody(response, "</body>");
                    AddToBody(response, "</html>");
                }
            }
            finally
            {
                try
                {
                    WriteResponse(context, response.ToString());
                }
                catch (Exception e)
                {
                    LogInfo("Exception sening respose: " + e);
                }
            }
        }