File: browse.rb

package info (click to toggle)
weechat-scripts 20071011
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 400 kB
  • ctags: 377
  • sloc: python: 3,470; perl: 1,305; ruby: 270; makefile: 37
file content (127 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright (c) 2007 by Javier Valencia <jvalencia@log01.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

def weechat_init
	Weechat.register("browse", "0.1", "", "Fast url browsing for Weechat.")
	
	Weechat.add_command_handler(
		"browse", 
		"do_browse", 
		"Use your preferred browser to navigate an url.", 
		"[number of url] (-1 to browse all) (leave empty to browse first one)")
	Weechat.add_command_handler(
		"webbrowser", 
		"set_browser", 
		"Get/Set browser command", 
		"[command] (leavy empty to get current command)")
	Weechat.add_command_handler(
		"urls", 
		"get_url_list", 
		"Get a list of urls in a channel.")
		
	Weechat.print("")
	Weechat.print("See help for 'browse', 'webbrowser' and 'urls' commands.")
	if Weechat.get_plugin_config("webbrowser").empty?
		Weechat.print("")
		Weechat.print("WARNING: you must define a web browser with the command /webbrowser.")
	end
	return Weechat::PLUGIN_RC_OK
end

def set_browser(server, args)
	if args.empty?
		Weechat.print("current web browser -> #{Weechat.get_plugin_config('webbrowser')}")
	else
		Weechat.set_plugin_config("webbrowser", args)
		Weechat.print("web browser set to -> #{args}")
	end
	return Weechat::PLUGIN_RC_OK
end

def get_url_array(buffer_data)
	urls = Array.new
	buffer_data.each do |line|
		line["data"].split.each do |word|
			if word =~ /^http\:\/\/.*/
				urls << word
			end
		end
	end
	return urls.uniq.reverse
end

def get_url_list(server,args)
	channel = Weechat.get_info("channel")
	if channel.empty?
		Weechat.print("You must be inside a channel.")
		return Weechat::PLUGIN_RC_KO
	end
	buffer = Weechat.get_buffer_data(server, channel)
	unless buffer
		Weechat.print("Error retrieving window buffer, may be a bug?.")
		return Weechat::PLUGIN_RC_KO
	end
	Weechat.print("")
	Weechat.print("URL listing:")
	pos = 0
	get_url_array(buffer).each do |url|
		Weechat.print("#{pos}) #{url}")
		pos += 1
	end
	return Weechat::PLUGIN_RC_OK
end

def do_browse(server, args)
	num = args.to_i
	channel = Weechat.get_info("channel")
	if channel.empty?
		Weechat.print("You must be inside a channel.")
		return Weechat::PLUGIN_RC_KO
	end
	buffer = Weechat.get_buffer_data(server, channel)
	unless buffer
		Weechat.print("Error retrieving window buffer, may be a bug?.")
		return Weechat::PLUGIN_RC_KO
	end
	browser = Weechat.get_plugin_config("webbrowser")
	if browser.empty?
		Weechat.print("")
		Weechat.print("WARNING: you must define a web browser with the command /webbrowser.")
		return Weechat::PLUGIN_RC_KO
	end
	urls = get_url_array(buffer)
	if urls.empty?
		Weechat.print("No urls in current channel.")
	else
		if num == -1
			Weechat.print("Starting browser...")
			fork do	# only for unix/posix
				exec("#{browser} #{urls.join(' ')} &> /dev/null")
			end
		else
			if urls[num]
				Weechat.print("Starting browser...")
				fork do	# only for unix/posix
					exec("#{browser} #{urls[num]} &> /dev/null")
				end
			else
				Weechat.print("Invalid url number.")
			end
		end
	end
	return Weechat::PLUGIN_RC_OK
end