examples/mcabberrc.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 136 2b04fad2f61a
permissions -rw-r--r--
Fix module loading problem



--[[

DESCRIPTION

This is a demo config file to show, what you can do with lua.

Feature list:
XEP-0004 Forms parsing and filling
XEP-0030 Info/items discovery requests (mcabber already can do replies)
XEP-0047 In-Bound Byte Streams (sending, receiving, saving with specified name, rejecting)
XEP-0077 In-Band Registration (only registration, but with data forms too)
XEP-0146 Remote commands requests
Jobs (actions, fired when some event occurs, now in one file, updated on exit)
Room nicks completion
MPD status polling (can be turned off)
Beep on all messages, even on chatroom ones
Url saving to file (for urlview)
Transported buddies availability indication
Actions on multiple marked buddies
Fallback commands (localized also! :) )
Help for fallback commands (well, I know, that it can be done with mcabber's help system, but it requires access to system files...)
All features have native mcabber interface

REQUIREMENTS

liblua-loudmouth (lm.lua and loudmouth.so)
liblua-socket - mpd

NOTES

Ibb uses own iq handler. This is the laziest way to implement this.

BUILTINS

print (global)   - prints to log/status/logwindow
dofile (global)  - loads lua file from default mcabber location

main methods:
- run            - run literal mcabber command
- beep           - beep
- log            - print to log w/specified priority
- print_info     - print info into specified buffer
- config_file    - format full file name from relative to mcabber's config dir
- status         - get current user status and message
- roster         - get list of roster jids (rooms, buddies and agents)
- current_buddy  - get jid of current buddy
- buddy_info     - get table with info about jid and its resources
- connection     - get lightuserdata for mcabber's lm connection
- timer          - run function periodically
- bgread         - run command and read it's output in background
- add_feature    - add string to feature list (for disco#info)
- del_feature    - delete string from feature list
- add_completion - adds word to completions
- del_completion - removes word from completions
- command        - adds/removes mcabber command
- option         - sets/gets mcabber option
- hook           - creates hook handler object

STRUCTURE

To allow your chunk of configuration (eg foo.lua) to play friendly with other, do:

- place it into a separate file, eg foo.lua
- if you need hooks, do 
  hook_d[hookname].foo = function ( args ) foo ( ) end
- if you register commands, also do
  commands_help['foo'] = "arg1 arg2\n\nFoo does bar."
- append to the end of this file
  dopath 'foo'
- try to be friendly to module reloads, eg unregister handlers, where appropriate,
  reset bindings
- you can omit unregistering of commands and xmpp features - module will unregister
  them automatically.

--]]

-- This is a hack to allow loading of lm.lua and loudmouth.so from ~/.mcabber
-- instead of installing them system-wide
-- FIXME to C. we now require this by require'ing interdependencies between modules.
if main.option ( 'lua_extra_include' ) then
	package.path = main.fileoption ( 'lua_extra_include' ) .. '/?.lua;' .. package.path
	package.cpath = main.fileoption ( 'lua_extra_include' ) .. '/?.so;' .. package.cpath
end

-- COMMON SUPPORT ROUTINES

function shell_escape ( str )
	if str then
		return "'" .. str:gsub ( "'", "'\\''" ) .. "'"
	else
		return "''"
	end
end

-- This is for debugging purposes, not for reloading. For that see jobs.
function table_to_string ( tab, pre )
	local prefix = pre or ""
	local tbls, jk = "", ""

	for key, val in pairs ( tab ) do
		if type ( val ) == 'table' then
			tbls = string.format ( "%s  %s%s = %s,\n", tbls, prefix, tostring(key), table_to_string ( val, "  " .. prefix ) )
		else
			jk = string.format ( "%s %s = \"%s\",", jk, tostring(key), tostring(val) )
		end
	end

	if tbls == "" then
		return string.format ( "{%s }", jk:sub ( 1, -2 ) )
	else
		return string.format ( "{%s\n%s%s}", jk, tbls, prefix )
	end
end

-- COMMANDS

--[[ Help strings should not contain command, only arguments. This is necessary to support soft aliases.
commands_help = {
	post      = "filename\n\nSends file as a message. Just shorthand.",
	beep      = "[enable|disable|on|off|yes|no|true|false]\n\nEnables or disables beeping on all messages.\nIf state is omitted, prints current state.",
	cmd       = "shell_command\n\nRuns shell command in background and sends output to current buddy.\nWorks asynchroneously, and may break long output in the middle of line",
	exthelp   = "[command]\n\nPrints help for a given command, or list of available help topics.",
	['join!'] = "\n\nForcibly joins to current buddy. Just saves you typing of full room name (that can be quite long) in a case of a non-bookmarked rooms.",
	count     = "\n\nPrints number of resources of current buddy. Useful to determine member count of large room.",
	toggle    = "\n\nToggles away/online status.",
}
--]]

main.command ( 'post',
	function ( args )
		main.run ( 'say_to -f ' .. args .. ' .' )
	end, false, 'filename' )
--[[
main.command ( 'cmd',
	function ( args )
		local to = main.current_buddy ()
		main.run ( ('say_to -q %q $ %s'):format ( to, args ) )
		main.bgread ( args,
			function ( data )
				if data then
					main.run ( ('say_to -q %q %s'):format ( to, data ) )
					return true
				else
					return false
				end
			end )
	end, false, 'filename' )
main.command ( 'exthelp',
	function ( args )
		if commands_help[args] then
			print ( "\n /" .. args .. ' ' .. commands_help[args] )
		else
			print ( "No help for this command." )
			local list = "Help available for commands: "
			for k in pairs (commands_help) do
				list = list .. k .. ', '
			end
			print ( list:sub ( 1, -3 ) )
			print ( "For built-in mcabber commands see /help" )
		end
	end, false, 'cmd' )
main.command ( 'join!',
	function ( args )
		main.run ( 'room join ' .. main.current_buddy () )
	end )
main.command ( 'count',
	function ( args )
		local count = 0
		for resource in pairs ( main.buddy_info ( main.current_buddy () ).resources ) do
			count = count + 1
		end
		print ( "Resource count: " .. count )
	end )
--]]
main.command ( 'toggle',
	function ( args )
		local stat, mess = main.status ()
		if stat == 'o' then
			main.run ( 'status away ' .. mess )
		else
			main.run ( 'status online ' .. mess )
		end
	end )

-- SAVING URLS TO FILE
-- These are implemented in C in urlopen and openurl modules

-- require 'urls'

-- TRANSPORTED BUDDIES AVAILABILITY INDICATION
-- After all, I do not use this anymore :/
-- But you can try.

-- require 'transports'

-- BEEPING ON ALL MESSAGES
-- Implemented in C by in-tree beep module

-- require 'beep'

-- MARKING
-- Implemented in C in marking module

-- require 'marking'

-- JOBS
-- Somewhat error-prone, requires jobs file existance, never seriously used by me :(

-- require 'jobs'

-- ROOM NICK COMPLETION
-- Well, it's sorta hacky, but I do use it

-- require 'room_priv'

-- SHORTENURL
-- Use goo.gl to shorten urls

require 'shortenurl'

-- FORMS

require 'forms'

-- DATA FORMS (XEP-0004)

-- PRIVACY LISTS (XEP-0016)

require 'privacy'

-- SERVICE DISCOVERY (XEP-0030)

require 'disco'

-- MULTI-USER CHAT (XEP-0045)

require 'muc'

-- IN-BAND BYTESTREAMS (XEP-0047)
-- Implemented in C in streams module

-- require 'ibb'

-- VCARD-TEMP (XEP-0054)

require 'vcard'

-- PUBLISH-SUBSRIBE (XEP-0060)

require 'pubsub'

-- OUT OF BAND DATA (XEP-0066)

require 'oob'

-- MALICIOUS STANZAS (XEP-0076)
-- Well, that's joke XEP and joke module

-- require 'evil'

-- IN-BAND REGISTRATION (XEP-0077)

require 'register'

-- USER LOCATION (XEP-0080)
-- Implemented in C in geoloc module

-- require 'geoloc'

-- USER AVATAR (XEP-0084)
-- Implemented in C in avatar module

-- require 'avatar'

-- USER MOOD (XEP-0107)
-- Implemented in C in mood module

-- require 'mood'

-- USER ACTIVITY (XEP-0108)
-- Implemented in C in activity module

-- require 'activity'

-- USER TUNE (XEP-0118)
-- Implemented in C in tune module

-- require 'tune'

-- REMOTE CONTROLLING CLIENTS (XEP-0146)

require 'remote'

-- PERSONAL EVENTING PROTOCOL (XEP-0163)
-- Implemented in C in pep module

-- XMPP PING (XEP-0199)
-- Included into upstream as /request ping

-- require 'ping'

-- ATTENTION (XEP-0224)

require 'attention'

-- The End -- vim: se ts=4: --