Examples updates (shortenurl)
authorMyhailo Danylenko <isbear@ukrpost.net>
Tue, 07 Aug 2012 02:43:46 +0300
changeset 125 3e2421384d7a
parent 124 286a034e4d5f
child 126 5f0025da31e6
Examples updates (shortenurl) * some updates to marking (freezes now) * added defunct readonly * added shortenulr
examples/help/en/hlp_shorten.txt
examples/lua.rc
examples/marking.lua
examples/mcabberrc.lua
examples/readonly.lua
examples/shortenurl.lua
lua.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/help/en/hlp_shorten.txt	Tue Aug 07 02:43:46 2012 +0300
@@ -0,0 +1,5 @@
+
+ /SHORTEN url
+
+Shortens url using goo.gl service and prints it to log.
+If option lua_shorten_post_url is set to true value and command is issued on valid buddy buffer, automatically sends resulting url to buddy.
--- a/examples/lua.rc	Sun Aug 05 20:12:30 2012 +0300
+++ b/examples/lua.rc	Tue Aug 07 02:43:46 2012 +0300
@@ -29,5 +29,8 @@
 # filter evil stanzas
 set lua_filter_evil      = 0
 
+# automatically send shortened urls to current user
+set lua_shorten_post_url = 1
+
 module load lua
 
--- a/examples/marking.lua	Sun Aug 05 20:12:30 2012 +0300
+++ b/examples/marking.lua	Tue Aug 07 02:43:46 2012 +0300
@@ -1,7 +1,23 @@
 
 -- MARKING
 
-marked_jids = {}
+-- public functions:
+--  * mark
+--  * unmark
+--  * mark_toggle
+--  * marked
+--  * foreach_marked
+--  * marked_clear
+-- binds 331 (Ins)
+-- FIXME
+--  * freezes on loading
+-- TODO:
+--  * test
+--  * check jid type
+--  * print marked/unmarked to log
+--  * check command formatting
+
+local marked_jids = {}
 
 function mark ()
 	local jid = main.current_buddy ()
@@ -64,21 +80,13 @@
 		end
 	end, true, { 'clear', 'do' } )
 
-commands_help['marked'] = "[clear | do mcabber_command]\n\nOperates on marked buddies. Without arguments prints list of marked jids.\nCommand should contain %%s in place, where jid should be inserted."
-
-mark_ins_bound = false
-
 -- Ins
 if not main.binding ( '331' ) then
 	main.binding ( '331', 'lua mark_toggle ()' )
-	mark_ins_bound = true
+	main.hook ( 'hook-lua-quit',
+		function ( args )
+			main.binding ( '331', nil )
+		end )
 end
 
-hooks_d['hook-quit'].mark =
-	function ( args )
-		if mark_ins_bound then
-			main.binding ( '331', nil )
-		end
-	end
-
--- vim: se ts=4: --
+-- vim: se ts=4 sw=4: --
--- a/examples/mcabberrc.lua	Sun Aug 05 20:12:30 2012 +0300
+++ b/examples/mcabberrc.lua	Tue Aug 07 02:43:46 2012 +0300
@@ -216,6 +216,11 @@
 
 -- require 'room_priv'
 
+-- SHORTENURL
+-- Use goo.gl to shorten urls
+
+require 'shortenurl'
+
 -- FORMS
 
 require 'forms'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/readonly.lua	Tue Aug 07 02:43:46 2012 +0300
@@ -0,0 +1,66 @@
+
+--  READONLY
+
+-- public functions:
+--  * readonly
+--  * readwrite
+--  * toggle_readonly
+--  * readonly_jids
+-- FIXME:
+--  * does not work, as mcabber ignores hook retval on hook-message-out
+-- TODO:
+--  * more accessors (merge storage with marking?)
+--  * commands?
+
+local readonly_db = {}
+
+function readonly ( jid )
+	if ( not jid ) or ( jid == '.' ) then
+		jid = main.current_buddy ()
+	end
+	if jid then
+		readonly_db [ jid ] = true
+	end
+end
+
+function readwrite ( jid )
+	if ( not jid ) or ( jid == '.' )  then
+		jid = main.current_buddy ()
+	end
+	if jid then
+		readonly_db [ jid ] = nil
+	end
+end
+
+function toggle_readonly ( jid )
+	if ( not jid ) or ( jid == '.' ) then
+		jid = main.current_buddy ()
+	end
+	if jid then
+		if readonly_db [ jid ] then
+			readwrite ( jid )
+		else
+			readonly ( jid )
+		end
+	end
+end
+
+function readonly_jids ()
+	local result = {}
+	for jid in pairs ( readonly_db ) do
+		table.insert ( result, jid )
+	end
+	return result
+end
+
+main.hook ( 'hook-message-out',
+	function ( args )
+		if readonly_db [ args.jid ] then
+			main.print_info ( args.jid, "Readonly JID, message dropped." )
+			return 'drop'
+		else
+			return 'proceed'
+		end
+	end )
+
+-- vim: se ts=4 sw=4: --
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/shortenurl.lua	Tue Aug 07 02:43:46 2012 +0300
@@ -0,0 +1,61 @@
+
+--  SHORTENURL
+
+-- uses:
+--  * goo.gl (online service)
+--  * curl (program)
+--  * lua-json (https://github.com/harningt/luajson/)
+--  * shell_escape (mcabberrc.lua)
+--  * option lua_shorten_post_url
+-- TODO:
+--  * other backends
+--  * detect curl/wget
+--  * ensure curl will not mess screen up
+--  * -s switch to send url automatically
+
+local json = require 'json'
+
+local encodeoptions = {
+	strings = {
+		encodeSet = '\\"%z\1-\031', -- do not escape /, google does not understand that
+	}
+}
+
+local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url -H 'Content-type: application/json' -d %s"
+
+main.command ( 'shorten',
+	function ( args )
+
+		if ( not args ) or not args:match ( '%w' ) then
+			print ( 'shorten: You must specify url' )
+			return
+		end
+
+		local jid = main.current_buddy ()
+		local replystring = ''
+		main.bgread ( ( curlcommand ):format ( shell_escape ( json.encode ( { longUrl = args }, encodeoptions ) ) ),
+			function ( data )
+				if not data then
+					-- eof
+					print ( 'lua reply: ' .. replystring )
+					local reply = json.decode ( replystring )
+					if reply and reply.id then
+						if jid and main.yesno ( main.option ( 'lua_shorten_post_url' ) ) then
+							main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
+						else
+							print ( ('Shortened url: %s'):format ( reply.id ) )
+						end
+					else
+						-- XXX extract message from json?
+						print ( ('Failed to shorten url: %s'):format ( replystring ) )
+					end
+					return false
+				else
+					replystring = replystring .. data
+					return true
+				end
+			end )
+
+	end )
+
+-- vim: se ts=4 sw=4: --
--- a/lua.c	Sun Aug 05 20:12:30 2012 +0300
+++ b/lua.c	Tue Aug 07 02:43:46 2012 +0300
@@ -1221,7 +1221,7 @@
 }
 
 /// background reading function
-/// Function, that processes output from pipe in asynchroneous way.
+/// Function, that processes output from pipe in asynchronous way.
 /// A: string (data) or nil (eof)
 /// R: boolean (false if reading should be terminated)
 static gboolean lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb)