Skip to content

Instantly share code, notes, and snippets.

@jakedouglas
Created June 24, 2009 09:04
Show Gist options
  • Save jakedouglas/135109 to your computer and use it in GitHub Desktop.
Save jakedouglas/135109 to your computer and use it in GitHub Desktop.
Simple UDP Growl packets in Lua
require "luarocks.require"
require "pack"
require "socket"
require "md5"
Growl = {version = 1, registration = 0, notification = 1}
function Growl.new(hostname, appname)
local mysock = assert(socket.udp())
local hostip = assert(socket.dns.toip(hostname))
return {host = hostip, port = 9887, sock = mysock, register = Growl.register, send = Growl.send, name = appname}
end
function Growl:register()
local len = string.len(self.name)
local data = string.pack(">AhAb", self.name, len, self.name, 0)
local packet = {Growl.version, Growl.registration, len, 1, 1, data}
local encpacket = string.pack(">bbhbbA", packet[1], packet[2], packet[3], packet[4], packet[5], packet[6])
encpacket = encpacket .. md5.sum(encpacket)
assert(self.sock:sendto(encpacket, self.host, self.port))
end
function Growl:send(title, message)
local data = self.name .. title .. message .. self.name
local packet = {Growl.version, Growl.notification, 0, string.len(self.name), string.len(title), string.len(message), string.len(self.name), data}
local encpacket = string.pack(">bbhhhhhA", packet[1], packet[2], packet[3], packet[4], packet[5], packet[6], packet[7], packet[8])
encpacket = encpacket .. md5.sum(encpacket)
assert(self.sock:sendto(encpacket, self.host, self.port))
end
g = Growl.new("localhost", "Lua")
g:register()
g:send("Lua", "Lua sez hi!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment