Skip to content

Instantly share code, notes, and snippets.

@zeen
zeen / README
Created April 12, 2011 21:37
Scripts to convert the XMPP.org services table to XML.
XSL files to convert Wordpress XMPP services table into XML.
curl http://xmpp.org/resources/public-services/ > services.html
xsltproc --html services-full.xsl services.html > services-full.xml
xsltproc --html services.xsl services.html > services.xml
-- Recive a HTTP POST and relay it
-- Author: Waqas Hussain
-- Derived from mod_post_msg by Kim Alvefur <[email protected]>
-- Some code borrowed from mod_webpresence
--
-- Example usage:
-- curl http://example.com:5280/presence/user -d "Hello there"
-- or
-- curl http://example.com:5280/presence/[email protected] -d "Hello there"
-- This would set presence of [email protected] with status 'Hello there'
require "socket"
client = socket:tcp();
client:connect("localhost", 5582);
while true do
local ch = client:receive(1);
if string.char(0) == ch then
@zeen
zeen / mod_limit_subscription.lua
Created December 13, 2010 20:57
Limits subscription requests to admins
-- mod_limit_subscription
-- Limits subscription requests to admins
local st = require "util.stanza";
local is_admin = usermanager.is_admin;
function subscription_handler(event)
local origin, stanza = event.origin, event.stanza;
if stanza.attr.type == "subscribe" and not is_admin(origin.full_jid, module.host) then
I have DNS records with weights. Need to select/sort randomly by weight. Problem: Distribution of the random variable I have is undefined.
Current plan:
Get random number X = N uniformly random bits from rand(0,1)<0.5 (von Neumann's fair coin from unfair).
2^N >= sum(weights). X <= sum(weights). X would be uniform over sum(weights). Right?
Select kth number from list such that sum(Weight1...Weightk)>X.
1. Is this correct?
2. Is there a better way (I'd like to minimize rand() calls and processing in general)?
3. von Neumann's fair coin method works with two coin tosses for every fair toss. Can I reuse the last toss of the old pair in the next? That would cut rand() down by half.
@zeen
zeen / ev.lua
Created November 30, 2010 07:26
lua-ev emulation using socket.select
-- lua-ev emulation using socket.select
local READ, WRITE = 0x01, 0x02;
local timeout = 5;
local Loop = {};
function Loop.new()
print("Loop:new")
@zeen
zeen / waqas.MattyMath.nocache.js
Created November 28, 2010 00:24
Adds Math.frexp, Math.ldexp
function waqas_MattyMath(){
var $wnd_0 = window, $doc_0 = document, gwtOnLoad, bodyDone, base = '', metaProps = {}, values = [], providers = [], answers = [], onLoadErrorFunc, propertyErrorFunc;
if (!$wnd_0.__gwt_stylesLoaded) {
$wnd_0.__gwt_stylesLoaded = {};
}
if (!$wnd_0.__gwt_scriptsLoaded) {
$wnd_0.__gwt_scriptsLoaded = {};
}
function isHostedMode(){
var result = false;
@zeen
zeen / mod_muc_broadcast.lua
Created November 6, 2010 11:40
Messages sent to room bare JIDs by room owners get broadcasted
-- mod_muc_broadcast.lua
local jid_bare = require "util.jid".bare;
local st = require "util.stanza";
module:hook("message/bare", function(event)
local stanza = event.stanza;
if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
local from = jid_bare(stanza.attr.from);
@zeen
zeen / ini.lua
Created November 1, 2010 13:42
INI parser
function ini_parse(text)
text = "[]\n"..text:gsub(";[^\n]*\n", ""); -- remove comments and add dummy section
local result = {};
local section = "";
local lineno = 0;
for line in text:gmatch("([^\n]+)") do
lineno = lineno + 1;
local block = line:match("^%[([^%]*)%]$");
if block then
section = block;
local gettime = require "socket".gettime;
module "shaper"
local shaper = {};
function shaper:update()
local newt = gettime();
local elapsed = newt - self.t;