Ejemplo n.º 1
0
        public void Deletes(ApiCall apiCall)
        {
            string idsJson = apiCall.GetValue("ids");

            if (string.IsNullOrEmpty(idsJson))
            {
                idsJson = apiCall.Context.Request.Body;
            }

            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("ids", idsJson);
                EmailForwardManager.Get <bool>(this.ModelName, nameof(EmailAddressApi.Deletes), apiCall.Context.User, dic);
                return;
            }

            var orgdb = Kooboo.Mail.Factory.DBFactory.OrgDb(apiCall.Context.User.CurrentOrgId);
            var ids   = JsonConvert.DeserializeObject <int[]>(idsJson);

            foreach (var id in ids)
            {
                orgdb.EmailAddress.Delete(id);
            }
        }
Ejemplo n.º 2
0
        public void MarkReads(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("ids", call.GetValue("ids"));
                dic.Add("value", call.GetValue("value"));
                EmailForwardManager.Get <bool>(this.ModelName, nameof(EmailMessageApi.MarkReads), call.Context.User, dic);
            }

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var idsJson = call.GetValue("ids");
            var ids     = Lib.Helper.JsonHelper.Deserialize <List <int> >(idsJson);

            var value = call.GetValue <bool>("value");

            foreach (var id in ids)
            {
                var msg = maildb.Messages.Get(id);
                if (msg != null)
                {
                    maildb.Messages.MarkAsRead(msg.Id, value);
                }
            }
        }
Ejemplo n.º 3
0
        public List <Message> List(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("address", call.GetValue("address"));
                dic.Add("folder", call.GetValue("folder"));
                return(EmailForwardManager.Get <List <Message> >(this.ModelName, nameof(EmailMessageApi.List), call.Context.User, dic));
            }

            var maildb = Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            int addressid = EmailAddress.ToId(call.GetValue("address"));

            var folderid = Folder.ToId(call.GetValue("Folder"));

            var query = maildb.Messages.Query().Where(o => o.FolderId == folderid);

            if (addressid != 0)
            {
                query.Where(o => o.AddressId == addressid);
            }
            var list = query.OrderByDescending(o => o.Id).Take(PageSize);

            MarkStatus(maildb, list);

            return(list);
        }
Ejemplo n.º 4
0
        public int Post(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <int>(this.ModelName, nameof(EmailDraft.Post), apiCall.Context.User, json, null));
            }

            var user  = apiCall.Context.User;
            var model = apiCall.Context.Request.Model as Mail.ViewModel.ComposeViewModel;
            var msg   = Kooboo.Mail.Utility.ComposeUtility.FromComposeViewModel(model, user);

            msg.FolderId = Folder.ToId(Folder.Drafts);

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(user);

            if (msg.Id > 0)
            {
                maildb.Messages.Update(msg, Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user));
            }
            else
            {
                maildb.Messages.Add(msg, Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user));
            }

            return(msg.Id);
        }
Ejemplo n.º 5
0
        public void UpdateForward(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("id", call.GetValue("id"));
                dic.Add("forwardAddress", call.GetValue("forwardAddress"));
                EmailForwardManager.Get <bool>(this.ModelName, nameof(EmailAddressApi.UpdateForward), call.Context.User, dic);
                return;
            }

            int    id  = call.GetValue <int>("id");
            string add = call.GetValue("forwardAddress");

            if (id != 0 && !string.IsNullOrEmpty(add))
            {
                var orgdb = Kooboo.Mail.Factory.DBFactory.OrgDb(call.Context.User.CurrentOrgId);
                var email = orgdb.EmailAddress.Get(id);
                if (email != null && email.AddressType == EmailAddressType.Forward)
                {
                    email.ForwardAddress = add;
                    orgdb.EmailAddress.AddOrUpdate(email);
                }
            }
        }
Ejemplo n.º 6
0
        public void Moves(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("ids", call.GetValue("ids"));
                dic.Add("folder", call.Context.Request.GetValue("folder"));
                EmailForwardManager.Get <bool>(this.ModelName, nameof(EmailMessageApi.Moves), call.Context.User, dic);
                return;
            }

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var idsJson = call.GetValue("ids");
            var ids     = Lib.Helper.JsonHelper.Deserialize <List <int> >(idsJson);

            var folder = call.Context.Request.GetValue("folder");

            var newfolder = new Folder(folder);

            foreach (var id in ids)
            {
                var msg = maildb.Messages.Get(id);
                if (msg != null)
                {
                    maildb.Messages.Move(msg, newfolder);
                }
            }
        }
Ejemplo n.º 7
0
        public object MemberPost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("addressId", call.GetValue("addressId"));
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(call.Context.Request.Model);
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAddressApi.MemberPost), call.Context.User, json, dic));
            }

            int addressid = call.GetValue <int>("addressId");
            var orgdb     = Kooboo.Mail.Factory.DBFactory.OrgDb(call.Context.User.CurrentOrgId);

            var member = call.Context.Request.Model as ListMemberModel;

            member.MemberAddress = member.MemberAddress.ToLower();

            var address = orgdb.EmailAddress.Get(addressid);

            if (Lib.Helper.StringHelper.IsSameValue(member.MemberAddress, address.Address))
            {
                throw new Exception(Data.Language.Hardcoded.GetValue("Member address should not be same as group address", call.Context));
            }

            orgdb.EmailAddress.AddMember(addressid, member.MemberAddress);

            return(member);
        }
Ejemplo n.º 8
0
        public Kooboo.Mail.ViewModel.ComposeViewModel Compose(ApiCall call)
        {
            int messageid = call.GetValue <int>("messageId");

            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("messageid", messageid.ToString());
                return(EmailForwardManager.Get <Kooboo.Mail.ViewModel.ComposeViewModel>(this.ModelName, nameof(EmailDraft.Compose), call.Context.User, dic));
            }

            if (messageid <= 0)
            {
                return(new Mail.ViewModel.ComposeViewModel());
            }
            else
            {
                var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);
                var msg    = maildb.Messages.Get(messageid);

                var result = Kooboo.Mail.Utility.ComposeUtility.ToComposeViewModel(msg, call.Context.User);
                result.Html = Kooboo.Mail.Utility.ComposeUtility.RestoreHtmlOrText(call.Context.User, messageid);
                return(result);
            }
        }
Ejemplo n.º 9
0
        //attachment image.
        public BinaryResponse File(ApiCall call)
        {
            string filename = call.Command.Value;

            if (EmailForwardManager.RequireForward(call.Context))
            {
                var method = nameof(EmailAttachmentApi.MsgFile) + "/" + filename;
                if (!string.IsNullOrEmpty(filename))
                {
                    method += "/" + filename;
                }

                var filebytes = EmailForwardManager.Post(this.ModelName, method, call.Context.User, null, null);
                var response  = new BinaryResponse();
                response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                response.BinaryBytes = filebytes;
                return(response);
            }

            var bytes = Kooboo.Mail.MultiPart.FileService.Get(call.Context.User, filename);

            if (bytes != null && bytes.Length > 0)
            {
                var response = new BinaryResponse();

                response.ContentType = Kooboo.Lib.Compatible.CompatibleManager.Instance.Framework.GetMimeMapping(filename);
                response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                response.BinaryBytes = bytes;
                return(response);
            }
            return(null);
        }
Ejemplo n.º 10
0
        public List <Domain> Domains(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                return(EmailForwardManager.Get <List <Domain> >(this.ModelName, nameof(EmailAddressApi.Domains), apiCall.Context.User, null));
            }

            return(Data.GlobalDb.Domains.ListForEmail(apiCall.Context.User));
        }
Ejemplo n.º 11
0
        public void Send(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("messageId", call.GetValue("messageId"));

                var json    = Kooboo.Lib.Helper.JsonHelper.Serialize(call.Context.Request.Model);
                var message = EmailForwardManager.Post <string>(this.ModelName, nameof(EmailMessageApi.Send), call.Context.User, json, dic);
                if (!string.IsNullOrEmpty(message))
                {
                    throw new Exception(message);
                }
                return;
            }

            var user  = call.Context.User;
            var model = call.Context.Request.Model as Mail.ViewModel.ComposeViewModel;
            var msg   = Kooboo.Mail.Utility.ComposeUtility.FromComposeViewModel(model, user);

            string messagebody = Kooboo.Mail.Utility.ComposeUtility.ComposeMessageBody(model, user);

            List <string> rcpttos = new List <string>(model.To);

            rcpttos.AddRange(model.Cc);
            rcpttos.AddRange(model.Bcc);

            string fromaddress = Mail.Utility.AddressUtility.GetAddress(msg.From);

            var msginfo = Kooboo.Mail.Utility.MessageUtility.ParseMeta(messagebody);

            if (rcpttos.Count > 0)
            {
                // verify sending quota.
                if (!Kooboo.Data.Infrastructure.InfraManager.Test(call.Context.User.CurrentOrgId, Data.Infrastructure.InfraType.Email, rcpttos.Count))
                {
                    throw new Exception(Data.Language.Hardcoded.GetValue("you have no enough credit to send emails", call.Context));
                }

                // save sent..
                Kooboo.Mail.Transport.Incoming.SaveSent(fromaddress, msginfo, messagebody);

                Kooboo.Mail.Transport.Incoming.Receive(fromaddress, rcpttos, messagebody, msginfo);

                // draft message id.
                var messageid = call.GetValue <int>("messageId");
                if (messageid > 0)
                {
                    var usermaildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);
                    usermaildb.Messages.Delete(messageid);
                }

                Kooboo.Data.Infrastructure.InfraManager.Add(call.Context.User.CurrentOrgId, Data.Infrastructure.InfraType.Email, rcpttos.Count, string.Join(",", rcpttos));
            }
        }
Ejemplo n.º 12
0
 public void DeleteAttachment(string filename, ApiCall call)
 {
     if (EmailForwardManager.RequireForward(call.Context))
     {
         var dic = new Dictionary <string, string>();
         dic.Add("filename", filename);
         EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailAttachmentApi.DeleteAttachment), call.Context.User, dic);
         return;
     }
     Kooboo.Mail.MultiPart.FileService.DeleteFile(call.Context.User, filename);
 }
Ejemplo n.º 13
0
        public ComposeViewModel Forward(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("sourceId", call.GetValue("sourceId"));
                return(EmailForwardManager.Get <ComposeViewModel>(this.ModelName, nameof(EmailMessageApi.Forward), call.Context.User, dic));
            }
            int messageId = call.GetValue <int>("sourceId");

            return(Kooboo.Mail.Multipart.ReferenceComposer.ComposeForward(messageId, call.Context));
        }
Ejemplo n.º 14
0
        public bool IsUniqueName(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailAddressApi.IsUniqueName), apiCall.Context.User, json, null));
            }
            var model = apiCall.Context.Request.Model as AddressModel;
            var orgdb = Mail.Factory.DBFactory.OrgDb(apiCall.Context.User.CurrentOrgId);

            return(orgdb.EmailAddress.Get(model.ToEmail()) == null);
        }
Ejemplo n.º 15
0
        public List <AddressItemModel> List(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                return(EmailForwardManager.Get <List <AddressItemModel> >(this.ModelName, nameof(EmailAddressApi.List), apiCall.Context.User));
            }
            var user  = apiCall.Context.User;
            var orgdb = Kooboo.Mail.Factory.DBFactory.OrgDb(user.CurrentOrgId);

            var addlist = orgdb.EmailAddress.Query().Where(o => o.UserId == user.Id).SelectAll();

            return(addlist.Select(o => AddressItemModel.FromAddress(o)).OrderBy(o => o.Address).ToList());
        }
Ejemplo n.º 16
0
        public List <string> MemberList(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("addressId", call.GetValue("addressId"));
                return(EmailForwardManager.Get <List <string> >(this.ModelName, nameof(EmailAddressApi.MemberList), call.Context.User, dic));
            }

            int addressid = call.GetValue <int>("addressId");
            var orgdb     = Kooboo.Mail.Factory.DBFactory.OrgDb(call.Context.User.CurrentOrgId);

            return(orgdb.EmailAddress.GetMembers(addressid).OrderBy(o => o).ToList());
        }
Ejemplo n.º 17
0
        public object Post(ApiCall apiCall)
        {
            if (EmailForwardManager.RequireForward(apiCall.Context))
            {
                var json = Kooboo.Lib.Helper.JsonHelper.Serialize(apiCall.Context.Request.Model);
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAddressApi.Post), apiCall.Context.User, json, null));
            }

            var model   = apiCall.Context.Request.Model as AddressModel;
            var user    = apiCall.Context.User;
            var orgdb   = Kooboo.Mail.Factory.DBFactory.OrgDb(user.CurrentOrgId);
            var address = new EmailAddress
            {
                Address        = model.ToEmail().ToLower(),
                ForwardAddress = model.ForwardAddress,
                AddressType    = model.AddressType,
                UserId         = user.Id
            };

            if (!Kooboo.Mail.Utility.AddressUtility.IsValidEmailAddress(address.Address))
            {
                return(new Kooboo.Api.ApiResponse.JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        Kooboo.Data.Language.Hardcoded.GetValue("Invalid address format", apiCall.Context)
                    }
                });
            }

            if (address.AddressType == EmailAddressType.Forward)
            {
                address.ForwardAddress = address.ForwardAddress.ToLower();
                if (address.Address == address.ForwardAddress)
                {
                    return(new Kooboo.Api.ApiResponse.JsonResponse
                    {
                        Success = false,
                        Messages = new List <string> {
                            "Forward to address should not be same as forward from"
                        }
                    });
                }
            }

            orgdb.EmailAddress.AddOrUpdate(address);

            return(AddressItemModel.FromAddress(address));
        }
Ejemplo n.º 18
0
        public object ImagePost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                //string convert to object will throw exception
                var url = EmailForwardManager.Post <string>(this.ModelName, nameof(EmailAttachmentApi.ImagePost), call.Context.User, call.Context.Request.PostData, null);
                if (!string.IsNullOrEmpty(url))
                {
                    return(url);
                }

                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxImageSize / 1024 / 1024}"
                    }
                });
            }

            var form = Kooboo.Lib.NETMultiplePart.FormReader.ReadForm(call.Context.Request.PostData);

            if (!form.Files.Any())
            {
                return(null);
            }

            var file = form.Files[0];

            if (file.Bytes.Length > MaxImageSize)
            {
                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxImageSize / 1024 / 1024}"
                    }
                });
            }

            var fileName = form.FormData["fileName"];

            fileName = Lib.Helper.StringHelper.ToValidFileName(fileName);

            Kooboo.Mail.MultiPart.FileService.Upload(call.Context.User, fileName, file.Bytes);

            return($"/_api/emailattachment/file/" + System.Web.HttpUtility.UrlEncode(fileName));
        }
Ejemplo n.º 19
0
        public void MemberDelete(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("addressId", call.GetValue("addressId"));
                dic.Add("memberAddress", call.GetValue("memberAddress"));
                EmailForwardManager.Get <bool>(this.ModelName, nameof(EmailAddressApi.MemberDelete), call.Context.User, dic);
                return;
            }

            int addressid = call.GetValue <int>("addressId");
            var orgdb     = Kooboo.Mail.Factory.DBFactory.OrgDb(call.Context.User.CurrentOrgId);

            var memberAddress = call.GetValue("memberAddress");

            orgdb.EmailAddress.DeleteMember(addressid, memberAddress);
        }
Ejemplo n.º 20
0
        public void Deletes(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("ids", call.GetValue("ids"));
                EmailForwardManager.Post <bool>(this.ModelName, nameof(EmailMessageApi.Deletes), call.Context.User, dic);
                return;
            }

            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var idsJson = call.GetValue("ids");
            var ids     = Lib.Helper.JsonHelper.Deserialize <List <int> >(idsJson);

            foreach (var id in ids)
            {
                maildb.Messages.Delete(id);
            }
        }
Ejemplo n.º 21
0
        public ContentViewModel Content(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                var dic = new Dictionary <string, string>();
                dic.Add("messageId", call.GetValue("messageId"));
                return(EmailForwardManager.Get <ContentViewModel>(this.ModelName, nameof(EmailMessageApi.Content), call.Context.User, dic));
            }
            int messageid = call.GetValue <int>("messageId");

            if (messageid <= 0)
            {
                return(new ContentViewModel());
            }
            else
            {
                var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);
                maildb.Messages.MarkAsRead(messageid);

                return(Mail.Utility.MessageUtility.GetContentViewModel(call.Context.User, messageid));
            }
        }
Ejemplo n.º 22
0
        public object AttachmentPost(ApiCall call)
        {
            if (EmailForwardManager.RequireForward(call.Context))
            {
                return(EmailForwardManager.Post <object>(this.ModelName, nameof(EmailAttachmentApi.AttachmentPost), call.Context.User, call.Context.Request.PostData, null));
            }

            var form = Kooboo.Lib.NETMultiplePart.FormReader.ReadForm(call.Context.Request.PostData);

            if (!form.Files.Any())
            {
                return(null);
            }

            var file = form.Files[0];

            if (file.Bytes.Length > MaxAttachmentSize)
            {
                return(new JsonResponse
                {
                    Success = false,
                    Messages = new List <string> {
                        $"File size must be less than {MaxAttachmentSize / 1024 / 1024}"
                    }
                });
            }

            var fileName = form.FormData["filename"];

            fileName = Lib.Helper.StringHelper.ToValidFileName(fileName);

            Kooboo.Mail.MultiPart.FileService.Upload(call.Context.User, fileName, file.Bytes);
            return(new Mail.Models.Attachment()
            {
                FileName = fileName
            });
        }
Ejemplo n.º 23
0
        // formate. msgfile/{messageid}/filename.
        public BinaryResponse MsgFile(ApiCall call)
        {
            string filename  = null;
            int    messageid = 0;

            var para = call.Command.Parameters;

            if (para.Count() == 2)
            {
                messageid = Convert.ToInt32(para[0]);
                filename  = System.Web.HttpUtility.UrlDecode(para[1]);
            }
            else
            {
                messageid = Convert.ToInt32(call.Command.Value);
            }

            if (EmailForwardManager.RequireForward(call.Context))
            {
                var method = nameof(EmailAttachmentApi.MsgFile) + "/" + messageid;
                if (!string.IsNullOrEmpty(filename))
                {
                    method += "/" + filename;
                }

                var bytes = EmailForwardManager.Post(this.ModelName, method, call.Context.User, null, null);

                filename = !string.IsNullOrEmpty(filename) ? System.Web.HttpUtility.UrlEncode(filename) : "attachment.zip";
                var response = new BinaryResponse();
                response.ContentType = "application/zip";
                response.Headers.Add("Content-Disposition", $"filename={filename}");
                response.BinaryBytes = bytes;
                return(response);
            }
            var maildb = Kooboo.Mail.Factory.DBFactory.UserMailDb(call.Context.User);

            var content = maildb.Messages.GetContent(messageid);


            if (!string.IsNullOrEmpty(content))
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    var bytes = Kooboo.Mail.Utility.MessageUtility.GetFileBinary(content, filename);
                    if (bytes != null && bytes.Length > 0)
                    {
                        var response = new BinaryResponse();

                        response.ContentType = Kooboo.Lib.Compatible.CompatibleManager.Instance.Framework.GetMimeMapping(filename);
                        response.Headers.Add("Content-Disposition", $"filename={System.Web.HttpUtility.UrlEncode(filename)}");
                        response.BinaryBytes = bytes;
                        return(response);
                    }
                }
                else
                {
                    var bytes    = Mail.Utility.MessageUtility.GenerateAllAttachmentZip(content);
                    var response = new BinaryResponse();
                    response.ContentType = "application/zip";
                    response.Headers.Add("Content-Disposition", $"filename=attachment.zip");
                    response.BinaryBytes = bytes;
                    return(response);
                }
            }

            return(null);
        }