<commit_before>﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Xml.Linq;

namespace Core.ListItemChangeMonitor
{
    class Program
    {

        static string url;
        static string listName;
        static string userName;
        static SecureString password;
        static DateTime lastRunTime;
        const int WaitSeconds = 30;

        static void Main(string[] args)
        {


            // Request Office365 site from the user
             url = GetSite();
             listName = GetListName();

            /* Prompt for Credentials */
            Console.WriteLine("Enter Credentials for {0}", url);
            userName = GetUserName();
            password = GetPassword();

            /* End Program if no Credentials */
            if (string.IsNullOrEmpty(userName) || (password == null) || string.IsNullOrEmpty(listName ))
                return;

            DoWork();

        }
        private static void DoWork()
        {
            Console.WriteLine("");
            Console.WriteLine("Url: " + url);
            Console.WriteLine("User Name: " + userName);
            Console.WriteLine("List Name: " + listName);
            Console.WriteLine("");
            Console.WriteLine(string.Format("Ctrl+c to terminate. Press \"r\" key to force run without waiting {0} seconds.",WaitSeconds));
            try
            {

                ClientContext cc = new ClientContext(url);
                cc.AuthenticationMode = ClientAuthenticationMode.Default;
                cc.Credentials = new SharePointOnlineCredentials(userName, password);

                ListCollection lists = cc.Web.Lists;
                IEnumerable<List> results = cc.LoadQuery<List>(lists.Where(lst => lst.Title == listName));
                cc.ExecuteQuery();
                List list = results.FirstOrDefault();
                if (list == null)
                {

                    Console.WriteLine("A list named \"{0}\" does not exist. Press any key to exit...", listName);
                    Console.ReadKey();
                    return;
                }


                lastRunTime = DateTime.Now.ToUniversalTime();

                ChangeQuery cq = new ChangeQuery(false, false);
                cq.Item = true;
                cq.DeleteObject = true;
                cq.Add = true;
                cq.Update = true;
                do
                {
                    do
                    {
                        if (Console.KeyAvailable && Console.ReadKey(true).KeyChar == 'r') { break; }
                    }
                    while (lastRunTime.AddSeconds(WaitSeconds) > DateTime.Now.ToUniversalTime());

                    cq.ChangeTokenStart = new ChangeToken();
                    cq.ChangeTokenStart.StringValue = string.Format("1;3;{0};{1};-1", list.Id.ToString(), lastRunTime.Ticks.ToString());
                    lastRunTime = lastRunTime.AddSeconds(WaitSeconds).AddMilliseconds(1);
                    ChangeCollection coll = list.GetChanges(cq);
                    cc.Load(coll);
                    cc.ExecuteQuery();
                    DisplayChanges(coll);

                } while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

            }
        }
        private static void DisplayChanges(ChangeCollection coll)
        {
            if (coll.Count == 0)
            {
                Console.WriteLine(string.Format("No changes to {0} since {1} {2} UTC.", listName, lastRunTime.ToShortDateString(), lastRunTime.ToLongTimeString()));
                return;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            foreach (ChangeItem itm in coll)
            {

                Console.WriteLine("");
                Console.WriteLine(string.Format("List {0} had a Change of type \"{1}\" on the item with Id {2}.", listName, itm.ChangeType.ToString(), itm.ItemId));
            }
            Console.ResetColor();
        }

        static SecureString GetPassword()
        {
            SecureString sStrPwd = new SecureString();
            try
            {
                Console.Write("SharePoint Password : ");<commit_msg>Mofify logic to use ChangeToken<commit_after>﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Xml.Linq;

namespace Core.ListItemChangeMonitor
{
    class Program
    {

        static string url;
        static string listName;
        static string userName;
        static SecureString password;
        static DateTime lastRunTime;
        const int WaitSeconds = 30;

        static void Main(string[] args)
        {

            // Request Office365 site from the user
            url = GetSite();
            listName = GetListName();

            /* Prompt for Credentials */
            Console.WriteLine("Enter Credentials for {0}", url);
            userName = GetUserName();
            password = GetPassword();

            /* End Program if no Credentials */
            if (string.IsNullOrEmpty(userName) || (password == null) || string.IsNullOrEmpty(listName))
                return;

            DoWork();

        }
        private static void DoWork()
        {
            Console.WriteLine("");
            Console.WriteLine("Url: " + url);
            Console.WriteLine("User Name: " + userName);
            Console.WriteLine("List Name: " + listName);
            Console.WriteLine("");
            try
            {

                Console.WriteLine(string.Format("Connecting to {0}", url));
                Console.WriteLine("");
                ClientContext cc = new ClientContext(url);
                cc.AuthenticationMode = ClientAuthenticationMode.Default;
                cc.Credentials = new SharePointOnlineCredentials(userName, password);

                ListCollection lists = cc.Web.Lists;
                IEnumerable<List> results = cc.LoadQuery<List>(lists.Where(lst => lst.Title == listName));
                cc.ExecuteQuery();
                List list = results.FirstOrDefault();
                if (list == null)
                {

                    Console.WriteLine("A list named \"{0}\" does not exist. Press any key to exit...", listName);
                    Console.ReadKey();
                    return;
                }


                lastRunTime = DateTime.Now.ToUniversalTime();

                ChangeQuery cq = new ChangeQuery(false, false);
                cq.Item = true;
                cq.DeleteObject = true;
                cq.Add = true;
                cq.Update = true;
                Console.WriteLine(string.Format("Ctrl+c to terminate. Press \"r\" key to force run without waiting {0} seconds.", WaitSeconds));
                do
                {
                    do
                    {
                        if (Console.KeyAvailable && Console.ReadKey(true).KeyChar == 'r') { break; }
                    }
                    while (lastRunTime.AddSeconds(WaitSeconds) > DateTime.Now.ToUniversalTime());

                    lastRunTime = lastRunTime.AddSeconds(WaitSeconds).AddMilliseconds(1);
                    ChangeCollection coll = list.GetChanges(cq);
                    cc.Load(coll);
                    cc.ExecuteQuery();

                    // if we find any changes to the list take the last change
                    // and use the ChangeToken as the start time for our next query
                    // The ChangeToken will contain the Date/time of the last change to any item in the list
                    cq.ChangeTokenStart = coll.Count > 0 ? coll.Last().ChangeToken : cq.ChangeTokenStart;

                    DisplayChanges(coll);

                } while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

            }
        }

        private static void DisplayChanges(ChangeCollection coll)
        {
            if (coll.Count == 0)
            {
                Console.WriteLine(string.Format("No changes to {0} since {1} {2} UTC.", listName, lastRunTime.ToShortDateString(), lastRunTime.ToLongTimeString()));
                return;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            foreach (ChangeItem itm in coll)
            {

                Console.WriteLine("");
                Console.WriteLine(string.Format("List {0} had a Change of type \"{1}\" on the item with Id {2}.", listName, itm.ChangeType.ToString(), itm.ItemId));
            }
            Console.WriteLine("");
            Console.ResetColor();
        }

        static SecureString GetPassword()
        {
            SecureString sStrPwd = new SecureString();
            try
            {
                Console.Write("SharePoint Password : ");
{'fraction_non_alphanumeric': 0.10111866637420487, 'fraction_numerical': 0.004277253783724501, 'mean_word_length': 1.5197568389057752, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6890865', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2120, 'n_words': 547}