using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Jayrock.Json;
using Jayrock.Json.Conversion;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
using Newtonsoft.Json;
using anmar.SharpMimeTools;
using log4net;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using SiteOffice.Business.ExternalMail;
namespace SiteOffice.ExternalMail.Services{
/// <summary>
/// Author: Joe Audette
/// Created: 2007-07-07
/// Last Modified: 2007-11-15
///
/// The use and distribution terms for this software are covered by the
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
/// </summary>
[JsonRpcHelp("This is a JSON-RPC service that serializes messages retrieved from pop mail accounts to JSON")]
public class PopToJson : JsonRpcHandler
{
#region Private Properties
private static readonly ILog log
= LogManager.GetLogger(typeof(PopToJson));
private Guid accountGuid = Guid.Empty;
#endregion
#region Public Methods
[JsonRpcMethod("GetMessageCount", Idempotent = true)]
[JsonRpcHelp("Returns count of messages for the provided account.")]
public int GetMessageCount(Guid accountId)
{
SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
if (currentUser == null) return -1;
UserEmailAccount userEmailAccount
= new UserEmailAccount(accountId);
if (userEmailAccount.ID == Guid.Empty) return -1;
if (userEmailAccount.UserGuid != currentUser.UserGuid) return -1;
EmailAccountHelper emailHelper
= new EmailAccountHelper(
userEmailAccount.UserName,
userEmailAccount.Password,
userEmailAccount.Pop3Server,
userEmailAccount.Pop3Port);
return emailHelper.GetMessageCount();
}
[JsonRpcMethod("GetPageCount", Idempotent = true)]
[JsonRpcHelp("Returns count of pages of messages for the provided account and page size.")]
public int GetPageCount(Guid accountId, int pageSize)
{
SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
if (currentUser == null) return -1;
int totalPages = 1;
UserEmailAccount userEmailAccount
= new UserEmailAccount(accountId);
if (userEmailAccount.ID == Guid.Empty) return -1;
if (userEmailAccount.UserGuid != currentUser.UserGuid) return -1;
EmailAccountHelper emailHelper
= new EmailAccountHelper(
userEmailAccount.UserName,
userEmailAccount.Password,
userEmailAccount.Pop3Server,
userEmailAccount.Pop3Port);
int messageCount
= emailHelper.GetMessageCount(pageSize, out totalPages);
return totalPages;
}
[JsonRpcMethod("GetTop5Messages", Idempotent = true)]
[JsonRpcHelp("Returns top 5 messages for the provided account.")]
public string GetTop5Messages(Guid accountId)
{
int pageNumber = 1;
int pageSize = 5;
return GetPopMail(
accountGuid,
pageNumber,
pageSize);
}
[JsonRpcMethod("GetPopMail", Idempotent = true)]
[JsonRpcHelp("Returns page of messages for the provided account.")]
public string GetPopMail(
Guid accountId,
int pageNumber,
int pageSize)
{
int totalPages = 1;
SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
if (currentUser == null) return "[]";
UserEmailAccount userEmailAccount
= new UserEmailAccount(accountId);
if (userEmailAccount.ID == Guid.Empty) return "[]";
if (userEmailAccount.UserGuid != currentUser.UserGuid) return "[]";
//StringBuilder jsonMessages = new StringBuilder();
//jsonMessages.Append("{\"messages\":[");
EmailAccountHelper emailHelper
= new EmailAccountHelper(
userEmailAccount.UserName,
userEmailAccount.Password,
userEmailAccount.Pop3Server,
userEmailAccount.Pop3Port);
try
{
List<SharpMessage> inboxMessages
= emailHelper.GetPage(
pageNumber,
pageSize,
out totalPages);
return JavaScriptConvert.SerializeObject(inboxMessages);
}
catch { }
return null;
}
//[JsonRpcMethod("GetCurrentUser", Idempotent = true)]
//[JsonRpcHelp("Returns the current user")]
//public string GetCurrentUser()
//{
// return JavaScriptConvert.SerializeObject(SiteUtils.GetCurrentSiteUser());
//}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
}
}
|