Example #1
0
 /// <summary>
 /// Fired when a <see cref="NotificationPacket"/> is received
 /// </summary>
 /// <param name="np">The <see cref="NotificationPacket"/> containing the information received</param>
 /// <param name="receivedFrom">The host from which the packet was received</param>
 /// <param name="extraLogInfo">Any additional information to log after the request has been processed</param>
 protected virtual void OnNotificationPacketReceived(NotificationPacket np, string receivedFrom, ref List <string> extraLogInfo)
 {
     if (this.NotificationReceived != null)
     {
         this.NotificationReceived(np, receivedFrom, ref extraLogInfo);
     }
 }
Example #2
0
        /// <summary>

        /// Sends a notification, setting all available parameters

        /// </summary>

        /// <param name="notificationType">The <see cref="NotificationType">type</see> of notification</param>

        /// <param name="title">The title of the notification</param>

        /// <param name="description">The description or extended information of the notifications</param>

        /// <param name="priority">The <see cref="Priority"/> of the notification</param>

        /// <param name="sticky"><c>true</c> to request that the notification is sticky, <c>false</c> to request the notification be not sticky</param>

        public void Notify(NotificationType notificationType, string title, string description, Growl.Connector.Priority priority, bool sticky)

        {
            NotificationPacket packet = new NotificationPacket(protocolVersion, this.applicationName, this.password, notificationType, title, description, priority, sticky);

            Send(packet);
        }
        /// <summary>
        /// Given the raw packet data, converts the data back into a <see cref="NotificationPacket"/>
        /// </summary>
        /// <param name="bytes">The raw packet data</param>
        /// <param name="passwordManager">The list of client passwords</param>
        /// <param name="passwordRequired">Indicates if the request must supply a valid password</param>
        /// <returns><see cref="NotificationPacket"/></returns>
        /// <remarks>
        /// If the client password does not match the password used to validate the sent notification,
        /// or if the packet is malformed in any way, a <c>null</c> object will be returned.
        /// </remarks>
        public static NotificationPacket FromPacket(byte[] bytes, PasswordManager passwordManager, bool passwordRequired)
        {
            NotificationPacket np = null;

            // parse the packet
            if (bytes != null && bytes.Length > 18)
            {
                // check md5 hash first
                string password = null;
                bool   valid    = BasePacket.IsPasswordValid(bytes, passwordManager, passwordRequired, out password);
                if (!valid)
                {
                    return(np);
                }

                int        protocolVersion = (int)bytes[0];
                PacketType packetType      = (PacketType)bytes[1];

                if (packetType == PacketType.Notification)
                {
                    short    flags    = BitConverter.ToInt16(new byte[] { bytes[3], bytes[2] }, 0);
                    bool     sticky   = ((flags & 1) == 1 ? true : false);
                    Priority priority = ConvertFlagToPriority(flags);

                    short notificationNameLength = BitConverter.ToInt16(new byte[] { bytes[5], bytes[4] }, 0);
                    short titleLength            = BitConverter.ToInt16(new byte[] { bytes[7], bytes[6] }, 0);
                    short descriptionLength      = BitConverter.ToInt16(new byte[] { bytes[9], bytes[8] }, 0);
                    short applicationNameLength  = BitConverter.ToInt16(new byte[] { bytes[11], bytes[10] }, 0);

                    int    index            = 12;
                    string notificationName = Encoding.UTF8.GetString(bytes, index, notificationNameLength);
                    index += notificationNameLength;
                    string title = Encoding.UTF8.GetString(bytes, index, titleLength);
                    index += titleLength;
                    string description = Encoding.UTF8.GetString(bytes, index, descriptionLength);
                    index += descriptionLength;
                    string applicationName = Encoding.UTF8.GetString(bytes, index, applicationNameLength);

                    NotificationType nt = NotificationType.GetByName(notificationName); //TODO:
                    np = new NotificationPacket(protocolVersion, applicationName, password, nt, title, description, priority, sticky);
                }
            }

            return(np);
        }
 /// <summary>
 /// Sends a notification, setting all available parameters
 /// </summary>
 /// <param name="notificationType">The <see cref="NotificationType">type</see> of notification</param>
 /// <param name="title">The title of the notification</param>
 /// <param name="description">The description or extended information of the notifications</param>
 /// <param name="priority">The <see cref="Priority"/> of the notification</param>
 /// <param name="sticky"><c>true</c> to request that the notification is sticky, <c>false</c> to request the notification be not sticky</param>
 public void Notify(NotificationType notificationType, string title, string description, Growl.Connector.Priority priority, bool sticky)
 {
     NotificationPacket packet = new NotificationPacket(protocolVersion, this.applicationName, this.password, notificationType, title, description, priority, sticky);
     Send(packet);
 }
Example #5
0
        /// <summary>

        /// Handles incoming packet data when a message is received

        /// </summary>

        /// <param name="bytes">The raw packet data</param>

        /// <param name="receivedFrom">The host that sent the packet</param>

        /// <param name="isLocal">Indicates if the request came from the local machine</param>

        /// <param name="isLAN">Indicates if the request came from the LAN</param>

        protected virtual void udp_PacketReceived(byte[] bytes, string receivedFrom, bool isLocal, bool isLAN)

        {
            // if this is a network request and we dont allow them, stop here

            if (!isLocal && !this.AllowNetworkNotifications)
            {
                return;
            }



            StringBuilder sb = new StringBuilder();

            bool processed = false;

            List <string> extraLogInfo = new List <string>();



            // parse the packet

            if (bytes != null && bytes.Length > 18)

            {
                int protocolVersion = (int)bytes[0];

                PacketType packetType = (PacketType)bytes[1];



                bool passwordRequired = true;

                if (isLocal && !this.RequireLocalPassword)
                {
                    passwordRequired = false;
                }

                else if (isLAN && !this.RequireLANPassword)
                {
                    passwordRequired = false;
                }



                if (packetType == PacketType.Registration)

                {
                    RegistrationPacket rp = RegistrationPacket.FromPacket(bytes, this.passwordManager, passwordRequired);

                    if (rp != null)

                    {
                        this.OnRegistrationPacketReceived(rp, receivedFrom, ref extraLogInfo);

                        processed = true;



                        sb.AppendFormat("Protocol Version:         {0}\r\n", rp.ProtocolVersion);

                        sb.AppendFormat("Packet Type:              {0}\r\n", rp.PacketType);

                        sb.AppendFormat("Application Name:         {0}\r\n", rp.ApplicationName);

                        sb.AppendFormat("Notifications Registered: {0}\r\n\r\n", rp.NotificationTypes.Length);

                        foreach (NotificationType nt in rp.NotificationTypes)

                        {
                            sb.AppendFormat("  Notification Type: {0}\r\n", nt.Name);

                            sb.AppendFormat("  Enabled:           {0}\r\n\r\n", nt.Enabled);
                        }
                    }

                    else

                    {
                        sb.Append("Invalid message - either the message format was incorrect or the password was incorrect");
                    }
                }

                else if (packetType == PacketType.Notification)

                {
                    NotificationPacket np = NotificationPacket.FromPacket(bytes, this.passwordManager, passwordRequired);

                    if (np != null)

                    {
                        this.OnNotificationPacketReceived(np, receivedFrom, ref extraLogInfo);

                        processed = true;



                        sb.AppendFormat("Protocol Version:  {0}\r\n", np.ProtocolVersion);

                        sb.AppendFormat("Packet Type:       {0}\r\n", np.PacketType);

                        sb.AppendFormat("Application Name:  {0}\r\n", np.ApplicationName);

                        sb.AppendFormat("Notification Type: {0}\r\n", np.NotificationType.Name);

                        sb.AppendFormat("Title:             {0}\r\n", np.Title);

                        sb.AppendFormat("Description:       {0}\r\n", np.Description);

                        sb.AppendFormat("Sticky:            {0}\r\n", np.Sticky);

                        sb.AppendFormat("Priority:          {0}\r\n", np.Priority);
                    }

                    else

                    {
                        sb.Append("Invalid message - either the message format was incorrect or the password was incorrect");
                    }
                }

                else

                {
                    sb.Append("Malformed packet - unrecognized data");
                }
            }

            else

            {
                sb.Append("Malformed packet - not enough bytes");
            }

            Log(sb.ToString(), bytes, receivedFrom, processed, extraLogInfo);
        }
        /// <summary>
        /// Given the raw packet data, converts the data back into a <see cref="NotificationPacket"/>
        /// </summary>
        /// <param name="bytes">The raw packet data</param>
        /// <param name="passwordManager">The list of client passwords</param>
        /// <param name="passwordRequired">Indicates if the request must supply a valid password</param>
        /// <returns><see cref="NotificationPacket"/></returns>
        /// <remarks>
        /// If the client password does not match the password used to validate the sent notification,
        /// or if the packet is malformed in any way, a <c>null</c> object will be returned.
        /// </remarks>
        public static NotificationPacket FromPacket(byte[] bytes, PasswordManager passwordManager, bool passwordRequired)
        {
            NotificationPacket np = null;

            // parse the packet
            if (bytes != null && bytes.Length > 18)
            {
                // check md5 hash first
                string password = null;
                bool valid = BasePacket.IsPasswordValid(bytes, passwordManager, passwordRequired, out password);
                if (!valid)
                    return np;

                int protocolVersion = (int)bytes[0];
                PacketType packetType = (PacketType)bytes[1];

                if (packetType == PacketType.Notification)
                {
                    short flags = BitConverter.ToInt16(new byte[] { bytes[3], bytes[2] }, 0);
                    bool sticky = ((flags & 1) == 1 ? true : false);
                    Priority priority = ConvertFlagToPriority(flags);

                    short notificationNameLength = BitConverter.ToInt16(new byte[] { bytes[5], bytes[4] }, 0);
                    short titleLength = BitConverter.ToInt16(new byte[] { bytes[7], bytes[6] }, 0);
                    short descriptionLength = BitConverter.ToInt16(new byte[] { bytes[9], bytes[8] }, 0);
                    short applicationNameLength = BitConverter.ToInt16(new byte[] { bytes[11], bytes[10] }, 0);

                    int index = 12;
                    string notificationName = Encoding.UTF8.GetString(bytes, index, notificationNameLength);
                    index += notificationNameLength;
                    string title = Encoding.UTF8.GetString(bytes, index, titleLength);
                    index += titleLength;
                    string description = Encoding.UTF8.GetString(bytes, index, descriptionLength);
                    index += descriptionLength;
                    string applicationName = Encoding.UTF8.GetString(bytes, index, applicationNameLength);

                    NotificationType nt = NotificationType.GetByName(notificationName); //TODO:
                    np = new NotificationPacket(protocolVersion, applicationName, password, nt, title, description, priority, sticky);
                }
            }

            return np;
        }
 /// <summary>
 /// Fired when a <see cref="NotificationPacket"/> is received
 /// </summary>
 /// <param name="np">The <see cref="NotificationPacket"/> containing the information received</param>
 /// <param name="receivedFrom">The host from which the packet was received</param>
 /// <param name="extraLogInfo">Any additional information to log after the request has been processed</param>
 protected virtual void OnNotificationPacketReceived(NotificationPacket np, string receivedFrom, ref List<string> extraLogInfo)
 {
     if (this.NotificationReceived != null) this.NotificationReceived(np, receivedFrom, ref extraLogInfo);
 }