示例#1
0
 // =============================================================================
 // Public interface to List Mailboxes, Subscriptions and Users
 // =============================================================================
 /// <summary>List all mailboxes</summary>
 public static bool ListFolders(CacheData.MBoxRef mailboxes,
                                bool bDetail, bool bRights, bool bQuota)
 {
     if(mailboxes.IsNothing) return false;
     Cache.ListedFolders = mailboxes;
     if(mailboxes.Count == 0)
     {   Message(FormatFilter("No Mailboxes"));
         return true;
     }
     return ListFolders(mailboxes, FormatFilter("Mailboxes"),
                        true, bDetail, bRights, bQuota);
 }
示例#2
0
        // =============================================================================
        //        
        // =============================================================================
        public static bool ListMails(CacheData.MailRef headers,
                           bool bTo, bool bFrom, bool bSubject, bool bDate, 
                           bool bSize, bool bFlags, bool bUID, bool bID)
        {
            if(headers.IsNothing) return false;
            ZIMapApplication.MailInfo[] mails = headers.Array(0);
            if(mails.Length < 1)
            {   Message("No mails");
                return true;
            }

            uint ucol = 0;
            if(bTo)      ucol++;
            if(bFrom)    ucol++;
            if(bSubject) ucol++;
            if(bDate)    ucol++;
            if(bSize)    ucol++;
            if(bFlags)   ucol++;
            if(bUID)     ucol++;
            if(bID)      ucol++;

            TextTool.TableBuilder tb = GetTableBuilder(ucol);
            object[] data = new object[ucol];
            ucol = 0;
            if(bID)
            {   data[ucol] = "ID";
                tb.Columns[ucol].RigthAlign = true;
                tb.Columns[ucol++].MaxWidth = 6;
            }
            if(bUID)
            {   data[ucol] = "UID";
                tb.Columns[ucol].RigthAlign = true;
                tb.Columns[ucol++].MaxWidth = 6;
            }
            if(bFrom)
            {   data[ucol] = "From";
                tb.Columns[ucol].RigthAlign = false;
                tb.Columns[ucol++].MaxWidth = 20;
            }
            if(bTo)
            {   data[ucol] = "To";
                tb.Columns[ucol].RigthAlign = false;
                tb.Columns[ucol++].MaxWidth = 20;
            }
            if(bDate)
            {   data[ucol] = "Date";
                tb.Columns[ucol].RigthAlign = true;
                tb.Columns[ucol++].MaxWidth = 20;
            }
            if(bSize)
            {   data[ucol] = "kByte";
                tb.Columns[ucol].RigthAlign = true;
                tb.Columns[ucol++].MaxWidth = 6;
            }
            if(bFlags)
            {   data[ucol] = "Flags";
                tb.Columns[ucol].RigthAlign = false;
                tb.Columns[ucol++].MaxWidth = 32;
            }
            if(bSubject)
            {   data[ucol] = "Subject";
                tb.Columns[ucol].RigthAlign = false;
                tb.Columns[ucol++].MinWidth = 32;
            }
            tb.Header(data);

            ZIMapMessage mail = new ZIMapMessage();
            for(int irun = 0; irun < mails.Length; irun++)
            {   if(!mail.Parse(mails[irun].Literal, false))
                       continue;
                ucol = 0;
                if(bID)      data[ucol++] = mails[irun].Index;
                if(bUID)     data[ucol++] = mails[irun].UID;
                if(bFrom)    data[ucol++] = mail.From;
                if(bTo)      data[ucol++] = mail.To;
                if(bDate)    data[ucol++] = mail.DateISO;
                if(bSize)    data[ucol++] = (mails[irun].Size + 1023) / 1024;
                if(bFlags)   data[ucol++] = string.Join(" ", mails[irun].Flags);
                if(bSubject) data[ucol++] = mail.Subject;
                tb.AddRow(data);
            }
            tb.Footer("");
            tb.PrintTable();
            return true;
        }
示例#3
0
        // =============================================================================
        // List Formatter for mailboxes
        // =============================================================================
        // TODO: ListFolders header text argument
        private static bool ListFolders(CacheData.MBoxRef mailboxes, string title,
                                        bool bSubscr, bool bDetail, bool bRights, bool bQuota)
        {
            uint ubox = mailboxes.Count;
            if(ubox == 0) return false;
            if(!App.EnableRights) bRights = false;
            if(!App.EnableQuota)  bQuota  = false;

            uint ucol = 1;
            if(bDetail)  ucol += 3;
            if(bSubscr)  ucol++;
            if(bRights)  ucol++;
            if(bQuota)   ucol++;
            TextTool.TableBuilder table = GetTableBuilder(ucol);
            object[] data = new object[ucol];
            ucol = 0;
            data[ucol] = title;
            table.Columns[ucol].MaxWidth   = 20;
            table.Columns[ucol++].MinWidth = 34;    // name

            if(bSubscr)
                data[ucol++] = "Sub";
            if(bDetail)                             // messages
            {   data[ucol] = "Mails";
                table.Columns[ucol++].MinWidth = 6;
                data[ucol] = "Recent";
                table.Columns[ucol++].MinWidth = 6;
                data[ucol] = "Unseen";
                table.Columns[ucol++].MinWidth = 6;
            }
            if(bRights)
            {   data[ucol] = "Rights";
                table.Columns[ucol].RigthAlign = false;
                table.Columns[ucol++].MinWidth = 6;
            }
            if(bQuota)
            {   data[ucol] = "Size Quota";
                table.Columns[ucol++].MinWidth = 12;
            }

            // print a list ...
            uint nmsg = 0;
            uint nrec = 0;
            uint nuns = 0;
            uint scnt = 0;
            uint nlas = 0;                      // last NS seen
            bool bsep = false;                  // separator on NS change

            table.Header(data);
            string subs = Ascii ? "*" : "■";    // HACK: for mono no "✓";

            mailboxes.Reset();
            while(mailboxes.Next())
            {   // get name, copy MailBox - we need it frequently ...
                ZIMapApplication.MailBox mailbox = mailboxes.Current;
                uint   nidx = ZIMapServer.Personal;
                string name = mailbox.Name;
                if(App.EnableNamespaces) name = App.Server.FriendlyName(name, out nidx);
                if(nidx != nlas)
                {   if(bsep) table.AddSeparator();
                    nlas = nidx;
                }
                bsep = true;

                // fill table ..
                ucol = 0;
                data[ucol++] = name;

                if(bSubscr)
                {   data[ucol++] = mailbox.Subscribed  ? subs : "";
                    if(mailbox.Subscribed) scnt++;
                }
                if(bDetail)
                {   if(mailbox.HasDetails)
                    {   data[ucol++] = mailbox.Messages;
                        data[ucol++] = mailbox.Recent;
                        data[ucol++] = mailbox.Unseen;
                    }
                    else
                    {   data[ucol++] = "-";
                        data[ucol++] = "-";
                        data[ucol++] = "-";
                    }
                }
                if(bRights)
                    data[ucol++] = mailboxes.ExtraRights;
                if(bQuota)
                {   ZIMapApplication.QuotaInfo info;
                    if(mailboxes.ExtraGetQuota(out info))
                    {   if(info.QuotaRoot != mailbox.Name)
                            data[ucol++] = string.Format("[Root={0}]", mailboxes.Search(info.QuotaRoot)+1);
                        else
                            data[ucol++] = string.Format("{0}k {1,3}%", info.StorageUsage,
                                (uint)((info.StorageUsage*100.0)/info.StorageLimit), info.QuotaRoot);
                    }
                    else
                        data[ucol++] = "";
                }
                table.AddRow(data);

                // build sums for footer line ...
                nmsg += mailbox.Messages;
                nrec += mailbox.Recent;
                nuns += mailbox.Unseen;
            }
            if(bSubscr && bDetail)
                table.Footer("Total", scnt, nmsg, nrec, nuns);
            else if(bDetail)
                table.Footer("Total", nmsg, nrec, nuns);
            else if(bSubscr)
                table.Footer("Total", scnt);
            else
                table.Footer("");
            table.PrintTable();
            return true;
        }
示例#4
0
        /// <summary>List subscribed mailboxes only</summary>
        public static bool ListSubscribed(CacheData.MBoxRef mailboxes,
                                          bool bDetail, bool bRights, bool bQuota)
        {
            if(mailboxes.IsNothing) return false;
            Cache.ListedFolders = mailboxes;

            // count subscriptions ...
            uint usub = 0;
            mailboxes.Reset();
            while(mailboxes.Next())
                if(mailboxes.Subscribed) usub++;
            if(usub == 0)
            {   Message(FormatFilter("No subscribed mailboxes"));
                return true;
            }

            // create subset ...
            if(usub != mailboxes.Count)
            {   ZIMapApplication.MailBox[] subs = new ZIMapApplication.MailBox[usub];
                usub = 0;
                mailboxes.Reset();
                while(mailboxes.Next())
                    if(mailboxes.Subscribed) subs[usub++] = mailboxes.Current;
                mailboxes = new CacheData.MBoxRef(subs);
            }

            return ListFolders(mailboxes, FormatFilter("Subscribed Mailboxes"),
                               false, bDetail, bRights, bQuota);
        }
示例#5
0
 public DataRefImp(CacheData parent)
 {
     this.parent = parent;
 }
示例#6
0
        // =============================================================================
        // Main
        // =============================================================================
        public static void Main(string[] args)
        {
            uint confirm = 0;
            ZIMapConnection.TlsModeEnum tlsmode = ZIMapConnection.TlsModeEnum.Automatic;

            // --- step 1: parse command line arguments

            ArgsTool.Option[] opts = ArgsTool.Parse(options, args, out Commands);
            if(opts == null)
                Fatal("Invalid command line. Try /help to get usage info.");

            foreach(ArgsTool.Option o in opts)
            {   if(o.Error == ArgsTool.OptionStatus.Ambiguous)
                    Fatal("Ambiguous option: {0}", o.Name);
                if(o.Error != ArgsTool.OptionStatus.OK)
                    Fatal("Invalid option: {0}. Try /help to get usage info", o.Name);

                switch(o.Name)
                {   case "?":
                    case "help":    Usage();
                                    return;
                    case "command": GetTableBuilder(0);         // init TextTool
                                    Execute("help -all " + o.Value);
                                    return;
                    case "ascii":   Ascii = true;
                                    TextTool.UseAscii = true;
                                    LineTool.EnableColor = false;
                                    break;
                    case "confirm": if     (o.Value == "on")  confirm = 1;
                                    else if(o.Value == "off") confirm = 2;
                                    else                      confirm = 0;
                                    break;
                    case "output":  if     (o.Value == "error") Output = OutLevel.Error;
                                    else if(o.Value == "brief") Output = OutLevel.Brief;
                                    else if(o.Value == "all")   Output = OutLevel.All;
                                    else                        Output = OutLevel.Info;
                                    break;
                    case "log":     Log = o.Value;
                                    if(string.IsNullOrEmpty(Log))
                                        Fatal("No log file specified");
                                    break;
                    case "debug":   Debug = 1;
                                    if(o.Value != null && !uint.TryParse(o.Value, out Debug))
                                        Fatal("Invalid debug level: {0}", o.Value);
                                    break;
                    case "server":  Server = o.Value;
                                    break;
                    case "protocol":
                                    Protocol = o.Value;
                                    if(o.SubValues != null)
                                    {   if(o.SubValues[0] == "tls"  )
                                            tlsmode = ZIMapConnection.TlsModeEnum.Required;
                                        if(o.SubValues[0] == "notls")
                                            tlsmode = ZIMapConnection.TlsModeEnum.Disabled;
                                    }
                                    break;
                    case "timeout":
                                    if(!uint.TryParse(o.Value, out Timeout))
                                        Fatal("Invalid timeout: {0}", o.Value);
                                    break;
                    case "account": Account = o.Value;
                                    break;
                    case "password":
                                    Password = o.Value;
                                    break;
                    case "mailbox": MailBoxName = o.Value;
                                    break;
                    case "--":      EndOption = true;
                                    break;
                }
            }
            opts = null;                                    // memory can be freed
            GetTableBuilder(0);                             // init TextTool

            if(Log != null)
            {   try
                {   LineTool.LogWriter = new System.IO.StreamWriter(Log);
                }
                catch(Exception ex)
                {   Fatal("Failed to open logfile: " + ex.Message);
                }
            }

            // --- step 2: prompt for missing parameters

            if(Commands != null && Commands.Length > 0)
            {   if(confirm != 1) LineTool.AutoConfirm = true;
                if(Output == OutLevel.Undefined) Output = OutLevel.Brief;
                Batch = true;

                string missing = null;
                if     (Server  == null)  missing = "server";
                else if(Account == null)  missing = "account";
                if(missing != null)
                    Fatal("Please add a '-{0}' option to your command line", missing);
            }
            else
            {   if(confirm == 2) LineTool.AutoConfirm = true;
                if(Output == OutLevel.Undefined) Output = OutLevel.All;
                Batch = false;

                if(Server == null)
                {   Server = LineTool.Prompt("Server  ");
                    if(string.IsNullOrEmpty(Server)) return;
                }
                if(Account == null)
                {   Account = LineTool.Prompt("Account ");
                    if(string.IsNullOrEmpty(Account)) return;
                }
            }

            if(Password == null)
                Password = System.Environment.GetEnvironmentVariable("ZIMAP_PWD");
            if(string.IsNullOrEmpty(Password))
            {   Password = LineTool.Prompt("Password");
                if(string.IsNullOrEmpty(Password)) return;
            }

            // --- step 3: Connect and configure

            uint port = ZIMapConnection.GetIMapPort(Protocol);
            if(port == ZIMapConnection.GetIMapPort()) Protocol = "imap";
            if(!Batch)
                Message(string.Format("Connecting {0}://{1}@{2} ...", Protocol, Account, Server));
            ZIMapConnection.Callback = new IMapCallback();

            App = new ZIMapApplication(Server, port);

            if(Timeout != 0) App.Timeout = Timeout;
            DebugLevel(Debug);

            if(!App.Connect(Account, Password, tlsmode))
                Fatal("Failed to connect");
            ProgressReporting = App.Connection.ProgressReporting;
            Cache = new CacheData(App);

            if(Output >= OutLevel.Info)
                Info("Server: " + App.Connection.ProtocolLayer.ServerGreeting);
            if(!App.Factory.HasCapability("IMAP4rev1"))
                Error("WARNING: This is not an IMAP4rev1 server!");

            // --- step 4: Open mailbox, Execute Commands

            if(MailBoxName == null || Execute("open -write " + MailBoxName))
            {
                // has commands from command line...
                if(Batch)
                {   // If the "--" syntax is used ...
                    if(EndOption)
                    {   // Arguments that contain spaces must be quoted
                        for(uint urun=0; urun < Commands.Length; urun++)
                            if(Commands[urun].Contains(" "))
                                ZIMapConverter.QuotedString(out Commands[urun], Commands[urun], true);
                        // build command string and split by "--" into single commands
                        string   carg = string.Join(" ", Commands);
                        Commands = carg.Split(new string[] { "-- " }, StringSplitOptions.RemoveEmptyEntries);
                    }

                    // Execute the array of commands until failure ...
                    foreach(string cmd in Commands)
                        if(!Execute(cmd)) break;
                }

                // prompt for commands...
                else
                {   Message("Entering command loop. Type 'help -list' to see the list of commands...");
                    if(App.Server.IsAdmin && App.EnableNamespaces)
                    {   Message("You are logged-in as an administrator - changing default namespace...");
                        Execute("user *");
                    }
                    while(true)
                    {   System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        string qual = Cache.Data.Qualifier;
                        if(qual == null) qual = "[no qualifier]";
                        else
                        {   uint nsid = App.Server.FindNamespace(qual, false);
                            if(nsid == ZIMapServer.Personal)    qual = "[personal]";
                            else if(nsid == ZIMapServer.Others) qual = "[other users]";
                            else if(nsid == ZIMapServer.Shared) qual = "[shared folders]";
                            else if(nsid == ZIMapServer.Search) qual = "[search results]";
                        }
                        sb.Append(qual);
                        sb.Append(Ascii ? ':' : '■');
                        string cmd = Cache.Data.Current.Name;
                        if(string.IsNullOrEmpty(cmd)) sb.Append("[no mailbox]");
                        else                          sb.Append(cmd);
                        cmd = LineTool.Prompt(sb.ToString());
                        if(string.IsNullOrEmpty(cmd)) break;
                        Execute(cmd);
                    }
                }
            }

            // --- step 5: Disconnect and exit

            App.Disconnect();
            if(LineTool.LogWriter != null) LineTool.LogWriter.Close();
            return;
        }