public ActionResult <Anon> SetAnon(Anon anon) { try { applicationDb.Anons.Add(anon); applicationDb.SaveChanges(); } catch (Exception e) { return(BadRequest(e.InnerException.Message)); } return(anon); }
public async Task <IActionResult> Report(int postId) { var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes()) .GetString(); var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash)); ViewBag.UserIpHash = anon.IpHash; ViewBag.UserIsBanned = false; if (anon.IsBanned) { ViewBag.UserIsBanned = anon.IsBanned; var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash); ViewBag.BanReason = ban.Reason; ViewBag.BanEnd = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime(); } try { var post = _db.Post.Include(p => p.File) .Include(p => p.Admin) .First(p => p.PostId == postId) ?? throw new Exception(); ViewBag.Board = _collection.Boards.First(brd => brd.BoardId == post.BoardId); ViewBag.Thread = _db.Thread.First(t => t.ThreadId == post.ThreadId); post.Comment = PostFormatter.GetFormattedPostText(post); ViewBag.PostFiles = new KeyValuePair <Post, List <File> >(post, post.File.ToList()); return(View()); } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); return(NotFound()); } }
// Registers all command modules static void RegisterCommands(ref CommandsNextExtension commands) { // Register commands commands.RegisterCommands <Prefixes>(); commands.RegisterCommands <Meta>(); commands.RegisterCommands <Rename>(); commands.RegisterCommands <Delete>(); commands.RegisterCommands <Say>(); commands.RegisterCommands <MediaGenCommands>(); commands.RegisterCommands <Anon>(); commands.RegisterCommands <Avatar>(); commands.RegisterCommands <ReactionPins>(); commands.RegisterCommands <Spoil>(); //commands.RegisterCommands<Commands.Utilities>(); // Execute onStart scripts to register events Anon.OnStart(commands.Client, Configuration); ReactionPins.OnStart(commands.Client, Configuration); MediaGenCommands.OnStart(commands.Client, Configuration); }
static void Main(string[] args) { SimpleDel simple = () => new Random().Next(0, 100); SimpleDel[] delegates = { simple, simple, simple }; Anon anon = (deleg) => { double a = 0; foreach (var item in deleg) { a += item(); Console.WriteLine(a); } return((double)a / deleg.Length); }; Console.WriteLine(anon(delegates)); Console.Read(); }
static void Main() { TypeD1 deleg1 = Max; TypeD1 dlg1 = new TypeD1(Max); if (deleg1.Equals(dlg1)) { Console.WriteLine("Equals"); } else { Console.WriteLine("Not equals"); }; Console.WriteLine(dlg1.Method.ToString()); deleg1(3, 5.833); deleg1.Invoke(44, 39.5); deleg1 = Same; if (deleg1.Equals(dlg1)) { Console.WriteLine("Equals"); } else { Console.WriteLine("Not equals"); }; deleg1(3, 3.98); deleg1(74, 12.2); Console.WriteLine(); TypeD2 deleg2 = Len; TypeD2 dlg2 = new TypeD2(Len); if (deleg2.Equals(dlg2)) { Console.WriteLine("Equals"); } else { Console.WriteLine("Not equals"); }; Console.WriteLine(dlg2.Method.ToString()); int print = deleg2("Sfwgfsa", 9); Console.WriteLine(print); deleg2 = Word; if (deleg2.Equals(dlg2)) { Console.WriteLine("Equals"); } else { Console.WriteLine("Not equals"); }; Console.WriteLine(deleg2("I am tired", 5)); Console.WriteLine(deleg2("I am tired", 12)); Console.WriteLine(deleg2("I am tired", 4)); Console.WriteLine(deleg2("I am tired", 6)); Console.WriteLine(); deleg1 += Max; deleg1(2, 2.02); Delegate[] dlg_list = deleg1.GetInvocationList(); for (int i = 0; i < dlg_list.Length; ++i) { Console.WriteLine(dlg_list[i].Method.ToString()); } Console.WriteLine(); deleg2 += Len; Console.WriteLine(deleg2("I am tired", 7)); dlg_list = deleg2.GetInvocationList(); for (int i = 0; i < dlg_list.Length; ++i) { Console.WriteLine(dlg_list[i].Method.ToString()); } Console.WriteLine(); Anon a = delegate() { Console.WriteLine("Anonymous method"); }; a(); Fact fa = delegate(int n) { if (n < 0) { return(-1); } else { int f = 1; for (int i = 2; i <= n; ++i) { f = f * i; } return(f); } }; Console.WriteLine(fa(11)); Console.WriteLine(); Func <int, int> f = delegate(int a) { int res = 0; while (a > 0) { res = res * 10 + a % 10; a /= 10; } return(res); }; Console.WriteLine(f(35681)); }
public async Task <IActionResult> Thread(int id) { var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes()) .GetString(); var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash)); ViewBag.UserIpHash = anon.IpHash; ViewBag.UserIsBanned = false; ViewBag.Id = 0; if (anon.IsBanned) { ViewBag.UserIsBanned = anon.IsBanned; var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash); ViewBag.BanReason = ban.Reason; ViewBag.BanEnd = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime(); } if (User.Identity.IsAuthenticated) { ViewBag.CurrentAdmin = _db.Admin.First(a => a.Email == User.Identity.Name); } try { using (_db) { try { var thread = _db.Thread.Where(t => t.ThreadId == id) .Include(t => t.Board) .ToList()[0]; var posts = _db.Post.Where(p => p.ThreadId == id) .Include(p => p.File) .Include(p => p.Admin) .ToList(); var postsFiles = new Dictionary <Post, List <File> >(); ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(null, null); foreach (var post in posts) { post.Comment = PostFormatter.GetFormattedPostText(post); if (post.IsPinned) { ViewBag.PinnedPost = new KeyValuePair <Post, List <File> >(post, post.File.ToList()); continue; } postsFiles.Add(post, post.File.ToList()); } ViewBag.Board = thread.Board; ViewBag.Thread = thread; ViewBag.UserIsOp = IpCheck.UserIsOp(thread, anon.IpHash); ViewBag.PostsFiles = postsFiles; } catch (ArgumentOutOfRangeException) { return(NotFound()); } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); } } } catch (InvalidOperationException) { return(NotFound()); } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); } return(View()); }
public async Task <IActionResult> AddPost(Post post, List <IFormFile> files, bool sage, bool isWrittenByOp = false) { try { var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes()) .GetString(); var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash)); if (anon.IsBanned) { return(RedirectToAction("YouAreBanned", "Ban")); } post.Comment = PostFormatter.GetHtmlTrimmedString(post.Comment); post.AnonIpHash = ipHash; post.TimeInUnixSeconds = DateTimeOffset.Now.ToUnixTimeSeconds(); if (User.Identity.IsAuthenticated) { var admin = _db.Admin.First(a => a.Email == User.Identity.Name); post.AnonName = admin.Login; post.Admin = admin; } if (ModelState.IsValid) { DbAccess.AddPostToThread(_db, post, sage, isWrittenByOp); if (files.Count > 0) { var fileDirectory = Path.Combine(_env.WebRootPath, "postImages"); var thumbNailDirectory = Path.Combine(_env.WebRootPath, "thumbnails"); foreach (var file in files) { if (file.Length > 0) { switch (Path.GetExtension(file.FileName)) { case ".jpeg": case ".jpg": case ".png": { var imageThumbnailCreator = new ImageThumbnailCreator(file, fileDirectory, thumbNailDirectory); imageThumbnailCreator.CreateThumbnail(); DbAccess.AddFilesToPost(_db, post, imageThumbnailCreator.FileInfo); break; } case ".gif": { var gifThumbnailCreator = new GifThumbnailCreator(file, fileDirectory, thumbNailDirectory); gifThumbnailCreator.CreateThumbnail(); DbAccess.AddFilesToPost(_db, post, gifThumbnailCreator.FileInfo); break; } } } else { ModelState.AddModelError("FileLengthNotValid", "Файл пустой."); } } } await LogIntoFile(_logDirectory, string.Concat("Added new post: ", post.PostId, "at thread: ", post.ThreadId), LoggingInformationKind.Info); } else { return(StatusCode(500)); } } catch (InvalidOperationException) { return(NotFound()); } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); } return(RedirectToAction("Thread", new { id = post.ThreadId })); }
public async Task <IActionResult> AddThread(Post post, List <IFormFile> files) { try { var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes()) .GetString(); var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash)); if (anon.IsBanned) { return(RedirectToAction("YouAreBanned", "Ban")); } if (ModelState.IsValid) { var board = _db.Board.First(b => b.BoardId == post.BoardId); if (_db.Thread.Count(t => t.BoardId == post.BoardId) >= 20) { return(RedirectToAction("Board", new { prefix = board.Prefix })); } post.AnonIpHash = ipHash; if (!string.IsNullOrEmpty(post.AnonName)) { post.AnonName = PostFormatter.GetHtmlTrimmedString(post.AnonName); } post.Comment = PostFormatter.GetHtmlTrimmedString(post.Comment); post.IsWrittenByOp = true; if (User.Identity.IsAuthenticated) { var admin = _db.Admin.First(a => a.Email == User.Identity.Name); post.AnonName = admin.Login; post.Admin = admin; } DbAccess.AddThreadToBoard(_db, ref post); if (files.Count > 0) { var fileDirectory = Path.Combine(_env.WebRootPath, "postImages"); var thumbNailDirectory = Path.Combine(_env.WebRootPath, "thumbnails"); foreach (var file in files) { if (file.Length > 0) { switch (Path.GetExtension(file.FileName)) { case ".jpeg": case ".jpg": case ".png": { var imageThumbnailCreator = new ImageThumbnailCreator(file, fileDirectory, thumbNailDirectory); imageThumbnailCreator.CreateThumbnail(); DbAccess.AddFilesToPost(_db, post, imageThumbnailCreator.FileInfo); break; } case ".gif": { var gifThumbnailCreator = new GifThumbnailCreator(file, fileDirectory, thumbNailDirectory); gifThumbnailCreator.CreateThumbnail(); DbAccess.AddFilesToPost(_db, post, gifThumbnailCreator.FileInfo); break; } } } else { ModelState.AddModelError("FileLengthNotValid", "Файл пустой."); } } } await LogIntoFile(_logDirectory, string.Concat("Added new thread: ", post.ThreadId), LoggingInformationKind.Info); return(RedirectToAction("Thread", "Thread", new { id = post.ThreadId })); } } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); Console.WriteLine(e); return(StatusCode(500)); } return(RedirectToAction("Board")); }
public async Task <IActionResult> Board(string prefix, int page = 1) { var ipHash = _md5.ComputeHash(HttpContext.Connection.RemoteIpAddress.GetAddressBytes()) .GetString(); var anon = new Anon(ipHash, IpCheck.UserIsBanned(_db, ipHash)); ViewBag.UserIpHash = anon.IpHash; ViewBag.UserIsBanned = false; if (anon.IsBanned) { ViewBag.UserIsBanned = anon.IsBanned; var ban = _db.Ban.First(b => b.AnonIpHash == anon.IpHash); ViewBag.BanReason = ban.Reason; ViewBag.BanEnd = DateTimeOffset.FromUnixTimeSeconds(ban.Term).ToLocalTime(); } ViewBag.Id = 0; ViewBag.Page = page; try { ViewBag.Board = _collection.Boards.First(brd => brd.Prefix == prefix); var allThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >(); var boards = _db.Board.Where(b => b.Prefix == prefix).Include(b => b.Thread).ToList(); Board board; if (boards.Count > 0) { board = boards[0]; ViewBag.FileLimit = board.FileLimit; } else { return(NotFound()); } ViewBag.ThreadCount = board.Thread.Count; foreach (var thrd in board.Thread.OrderByDescending(t => t.BumpInUnixTime)) { var thread = _db.Thread.Include(t => t.Post).First(t => t.ThreadId == thrd.ThreadId); if (thread != null) { var postFiles = new List <KeyValuePair <Post, List <File> > >(); if (thread.Post.Count >= 4) { var posts = new List <Post> { thread.Post.ToArray()[0] }; posts.AddRange(thread.Post.ToList().OrderByDescending(p => p.TimeInUnixSeconds).Take(3)); foreach (var post in posts) { var p = _db.Post .Include(pp => pp.File) .Include(pp => pp.Admin) .First(pp => pp.PostId == post.PostId); postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList())); } allThreads.Add( new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles)); } else if (thread.Post.Count > 0 && thread.Post.Count <= 3) { var p = _db.Post .Include(pp => pp.File) .Include(pp => pp.Admin) .First(pp => pp.ThreadId == thread.ThreadId); postFiles.Add(new KeyValuePair <Post, List <File> >(p, p.File.ToList())); allThreads.Add( new KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > >(thread, postFiles)); } } } var pageInfo = new PageInfo(page, Constants.BOARD_PAGE_SIZE, allThreads.Count); var pageThreads = new List <KeyValuePair <Thread, List <KeyValuePair <Post, List <File> > > > >(); for (var i = (page - 1) * pageInfo.PageSize; i < page * pageInfo.PageSize; i++) { try { pageThreads.Add(allThreads[i]); } catch (ArgumentOutOfRangeException) { break; } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); return(StatusCode(500)); } } foreach (var pt in pageThreads) { foreach (var pf in pt.Value) { pf.Key.Comment = PostFormatter.GetFormattedPostText(pf.Key); } } ViewBag.PageInfo = pageInfo; ViewBag.BoardViewModel = pageThreads; } catch (InvalidOperationException e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); return(NotFound()); } catch (Exception e) { await LogIntoFile(_logDirectory, string.Concat(e.Message, "\n", e.StackTrace), LoggingInformationKind.Error); return(StatusCode(500)); } return(View()); }