public IEnumerable <RedditAnswer> ParseThing(Thing objThing, PortalKeeperContext <ScheduleEvent> actionContext) //where TEngineEvent : IConvertible { var toReturn = new List <RedditAnswer>(); if (EnableAllUsers || SimpleOrExpression <String> .GetValues(CommandUsers.ToArray(), actionContext, actionContext).Contains(objThing.AuthorName)) { var parentsParsed = false; var message = ParseThingVariables(objThing, actionContext, "", 0); foreach (var objRegex in BuiltRegexes) { var objMatch = objRegex.Match(message); if (objMatch.Success) { var previousAnswer = GetExistingAnswer(objThing); // We only proceed with messages we have not answered yet if (previousAnswer == null) { if (ParsingDepth > 0) { if (!parentsParsed) { ParseThingVariables(objThing.ParentThing, actionContext, "Parent", ParsingDepth - 1); parentsParsed = true; } } foreach (string groupName in objRegex.GetGroupNames()) { actionContext.SetVar(groupName, objMatch.Groups[groupName].Value); } foreach (var objRedditAction in RedditAnswers) { if (objRedditAction.IsMatch(actionContext)) { toReturn.Add(objRedditAction); } } } else { //var candidateThings = previousAnswer.Children; //foreach (var candidateThing in candidateThings) //{ // foreach (var command in NextCommands) // { // toReturn = toReturn | command.ParseThing(candidateThing, actionContext); // } //} } return(toReturn); } } } return(toReturn); }
public virtual CreateHttpResponseInfo ProcessInternal(PortalKeeperContext <SimpleEngineEvent> actionContext, WebMethod verb) { actionContext.SetVar("HttpVerb", verb); this.SelectedAction.ProcessRules(actionContext, SimpleEngineEvent.Run, true, true); var response = actionContext.GetResponse(); if (response == null && this.SelectedAction.DefaultResponse.Enabled) { response = this.SelectedAction.DefaultResponse.Entity; } return(response); }
public bool isSatisfiedWith(Assignment a) { var assignedVars = a.getVariables(); var dico = new Dictionary <String, Object>(); foreach (Variable assignedVar in assignedVars.toArray()) { dico[assignedVar.getName()] = a.getAssignment(assignedVar); } var context = new PortalKeeperContext <SimpleEngineEvent>(); context.InitParams(dico); return(ConstraintExpression.Evaluate(context, context)); }
protected string GetMessage(PortalKeeperContext <ScheduleEvent> actionContext) { string message = this.MainAnswer; if (this.AlternateAnswers.Count > 0) { int nextIdx = _Random.Next(this.AlternateAnswers.Count); if (nextIdx > 0) { message = this.AlternateAnswers[nextIdx]; } } var atr = actionContext.GetAdvancedTokenReplace(); message = atr.ReplaceAllTokens(message); return(message); }
public void AddVariables(IExpressionVarsProvider currentProvider, ref IDictionary <string, Type> existingVars) { try { var dumbContext = new PortalKeeperContext <SimpleEngineEvent>(); var objVariables = Variables.EvaluateTyped(dumbContext, dumbContext); foreach (object objVariable in objVariables) { var varName = ReflectionHelper.GetFriendlyName(objVariable); existingVars[varName] = typeof(object); } } catch (Exception) { } //For Each objVar As VariableInfo In Me.Variables.Instances // existingVars(objVar.Name) = ReflectionHelper.CreateType(objVar.VariableType) //Next }
private string ParseThingVariables(Thing objThing, PortalKeeperContext <ScheduleEvent> actionContext, string prefix, int depth) { string toReturn = ""; if (objThing != null && depth >= 0) { actionContext.SetVar(prefix + "Thing", objThing); actionContext.SetVar(prefix + "AuthorName", objThing.AuthorName); switch (objThing.Kind) { case "Comment": var objComment = (Comment)objThing; actionContext.SetVar(prefix + "Comment", objComment); toReturn = objComment.Body; actionContext.SetVar(prefix + "Message", objComment.Body); break; case "PrivateMessage": var pm = (PrivateMessage)objThing; actionContext.SetVar(prefix + "PrivateMessage", pm); toReturn = pm.Body; actionContext.SetVar(prefix + "Message", pm.Body); break; case "Post": var objPost = (Post)objThing; actionContext.SetVar(prefix + "Post", objPost); toReturn = objPost.SelfText; actionContext.SetVar(prefix + "Message", objPost.SelfText); break; } if (depth > 0) { ParseThingVariables(objThing.ParentThing, actionContext, "Parent" + prefix, depth - 1); } } return(toReturn); }
public override void PostMessage(Thing objThing, PortalKeeperContext <ScheduleEvent> actionContext) { var message = GetMessage(actionContext); var title = MessageTitle.GetValue(actionContext, actionContext); if ((AnswerTypes & ThingKind.Post) == ThingKind.Post) { string strTargetSubReddit = TargetSubReddit.GetValue(actionContext, actionContext); Subreddit objSubReddit = objThing.Reddit.GetSubreddit(strTargetSubReddit); objSubReddit.SubmitTextPost(title, message); } if ((AnswerTypes & ThingKind.Comment) == ThingKind.Comment) { string strParentFullName = TargetParentFullName.GetValue(actionContext, actionContext); Thing targetParent = objThing.Reddit.GetThingByFullname(strParentFullName); this.Reply(targetParent, message); } if ((AnswerTypes & ThingKind.PrivateMessage) == ThingKind.PrivateMessage) { string targetUserName = TargetUser.GetValue(actionContext, actionContext); objThing.Reddit.ComposePrivateMessage(title, message, targetUserName); } }
protected override bool Run(PortalKeeperContext <SimpleEngineEvent> actionContext, bool aSync) { //var objResponse = this.CreateResponseInfo.CreateResponse(actionContext); actionContext.SetResponse(CreateResponseInfo); return(true); }
public static HttpResponseMessage CreateResponse(this CreateHttpResponseInfo objCreateHttpResponseInfo, PortalKeeperContext <SimpleEngineEvent> objContext) { var request = objContext.GetRequest(); HttpResponseMessage toReturn = request.CreateResponse(objCreateHttpResponseInfo.StatusCode); var objContent = objCreateHttpResponseInfo.EvaluateToReturn(objContext); var configuration = WebApiConfig.GetConfiguration(); if (objContent != null) { var objContentType = objContent.GetType(); IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; MediaTypeFormatter selectedFormatter; MediaTypeHeaderValue mediaType = null; if (objCreateHttpResponseInfo.CustomFormatter.Enabled) { if (!objCreateHttpResponseInfo.CustomFormatter.Entity.KnownFormatter.IsNullOrEmpty()) { selectedFormatter = formatters.First( objFormatter => objFormatter.GetType().Name == objCreateHttpResponseInfo.CustomFormatter.Entity.KnownFormatter); } else { selectedFormatter = objCreateHttpResponseInfo.CustomFormatter.Entity.FormatterExpression.Evaluate(objContext, objContext, typeof(MediaTypeFormatter)) as MediaTypeFormatter; } } else { IContentNegotiator contentNegotiator = configuration.Services.GetContentNegotiator(); if (contentNegotiator == null) { throw new InvalidOperationException("No Content Negotiator"); } ContentNegotiationResult contentNegotiationResult = contentNegotiator.Negotiate(objContentType, request, formatters); if (contentNegotiationResult == null) { throw new System.Web.Http.HttpResponseException(HttpStatusCode.NotAcceptable); } selectedFormatter = contentNegotiationResult.Formatter; mediaType = contentNegotiationResult.MediaType; } if (objCreateHttpResponseInfo.CustomMediaType.Enabled) { var strMediaType = objCreateHttpResponseInfo.CustomMediaType.Entity.KnownHeader; if (strMediaType.IsNullOrEmpty()) { strMediaType = objCreateHttpResponseInfo.CustomMediaType.Entity.CustomHeader.GetValue(objContext, objContext); } if (!strMediaType.IsNullOrEmpty()) { mediaType = formatters.SelectMany(form => form.SupportedMediaTypes) .First( objHeader => StringComparer.OrdinalIgnoreCase.Compare(objHeader.MediaType, objCreateHttpResponseInfo.CustomMediaType.Entity.KnownHeader) == 0) ?? new MediaTypeHeaderValue(strMediaType); } } toReturn.Content = new ObjectContent(objContentType, objContent, selectedFormatter, mediaType); } if (objCreateHttpResponseInfo.CustomHttpHeaders.Enabled) { foreach (var objPair in objCreateHttpResponseInfo.CustomHttpHeaders.Entity.EvaluateVariables(objContext, objContext)) { if (objPair.Value == null) { throw new InvalidOperationException("Header \"" + objPair.Key + "\" has a null value"); } var collectionCast = objPair.Value as IEnumerable <String>; if (collectionCast != null) { toReturn.Headers.Add(objPair.Key, collectionCast); } else { toReturn.Headers.Add(objPair.Key, objPair.Value.ToString()); } } } return(toReturn); }
public static void SetResponse(this PortalKeeperContext <SimpleEngineEvent> context, CreateHttpResponseInfo objresponse) { context.SetVar("Response", objresponse); }
public static CreateHttpResponseInfo GetResponse(this PortalKeeperContext <SimpleEngineEvent> context) { return((CreateHttpResponseInfo)context.GetVar("Response")); }
public static HttpRequestMessage GetRequest(this PortalKeeperContext <SimpleEngineEvent> context) { return((HttpRequestMessage)context.GetVar("Request")); }
public virtual HttpResponseMessage Process(PortalKeeperContext <SimpleEngineEvent> actionContext, WebMethod verb) { var response = ProcessInternal(actionContext, verb); return(response?.CreateResponse(actionContext)); }
public HttpResponseMessage Options(PortalKeeperContext <SimpleEngineEvent> actionContext) { return(this.Process(actionContext, WebMethod.Options)); }
public abstract void PostMessage(Thing objThing, PortalKeeperContext <ScheduleEvent> actionContext);
public override void PostMessage(Thing objThing, PortalKeeperContext <ScheduleEvent> actionContext) { var message = GetMessage(actionContext); this.Reply(objThing, message); }
public static IHttpRouteData GetRouteData(this PortalKeeperContext <SimpleEngineEvent> context) { return((IHttpRouteData)context.GetVar("RouteData")); }
public bool IsMatch(PortalKeeperContext <ScheduleEvent> actionContext) { return(Enabled && (!Conditional || Condition.Evaluate(actionContext, actionContext))); }