<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sixserv blog &#187; Ruby</title>
	<atom:link href="http://sixserv.org/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://sixserv.org</link>
	<description>A Blog about Linux, Networking, Development and Security.</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:45:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Kleine Ruby Logger Klasse</title>
		<link>http://sixserv.org/2009/06/13/kleine-ruby-logger-klasse/</link>
		<comments>http://sixserv.org/2009/06/13/kleine-ruby-logger-klasse/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 04:04:09 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=439</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/>Normalerweise sind die Artikel hier ja ziemlich umfangreich, was ja auch gut ist schließlich geht das Sixserv Blog ja schon sehr ins technische Detail. Doch diesmal wird es eher kurz, ich möchte eine kleine Logger Klasse vorstellen die ich in Ruby geschrieben habe und seither häufiger benutze. 1 2 3 4 5 6 7 8 [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/><p>Normalerweise sind die Artikel hier ja ziemlich umfangreich, was ja auch gut ist schließlich geht das Sixserv Blog ja schon sehr ins technische Detail. Doch diesmal wird es eher kurz, ich möchte eine kleine Logger Klasse vorstellen die ich in Ruby geschrieben habe und seither häufiger benutze.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Logger</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">init</span>
    <span style="color:#0066ff; font-weight:bold;">@logfile</span> = LOG_FILE
    <span style="color:#0066ff; font-weight:bold;">@buffer</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;"># logging lines for buffering</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#006600; font-weight:bold;">&lt;&lt;</span> msg
    msg = <span style="color:#CC0066; font-weight:bold;">format</span> msg
&nbsp;
    <span style="color:#008000; font-style:italic;"># append message to buffer</span>
    <span style="color:#0066ff; font-weight:bold;">@buffer</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> msg
&nbsp;
    <span style="color:#008000; font-style:italic;"># flush the buffer if possible</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">flush</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">flush</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@buffer</span>.<span style="color:#9900CC;">empty</span>?
&nbsp;
    <span style="color:#008000; font-style:italic;"># try to lock the file for writing</span>
    log = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@logfile, <span style="color:#996600;">'a+'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> log.<span style="color:#9900CC;">flock</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">File::LOCK_EX</span> <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#6666ff; font-weight:bold;">File::LOCK_NB</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#0000FF; font-weight:bold;">false</span>
      <span style="color:#008000; font-style:italic;"># Logfile is locked</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Logfile is locked!&quot;</span>
      log.<span style="color:#9900CC;">close</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># ==&gt; not locked, write buffer to logfile</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0066ff; font-weight:bold;">@buffer</span>.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
      log.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#0066ff; font-weight:bold;">@buffer</span>.<span style="color:#9900CC;">shift</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    log.<span style="color:#9900CC;">flock</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">File::LOCK_UN</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    log.<span style="color:#9900CC;">close</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">format</span> msg
    time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">strftime</span> <span style="color:#996600;">'%H:%M:%S - %d.%m.%Y'</span>
    <span style="color:#996600;">&quot;[#{time}] #{msg}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Nichts großes also, es gibt z.B. keine Log-Level usw. Außerdem kann man sich, wenn man etwas mehr Features braucht, auch eine Logging Bibliothek aus dem Gems bedienen. Die Anwendung ist ganz einfach:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'/pfad/zu/logger.rb'</span>
<span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">init</span>
<span style="color:#008000; font-style:italic;"># das kann man dann überall im Code verteilen:</span>
<span style="color:#CC00FF; font-weight:bold;">Logger</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;Irgendeine Nachricht!&quot;</span>
<span style="color:#008000; font-style:italic;"># hat man eine große Anwendung mit vielen Prozessen/Threads</span>
<span style="color:#008000; font-style:italic;"># kann man auch an strategischen Stellen ein</span>
<span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">flush</span>
<span style="color:#008000; font-style:italic;"># einbauen :)</span></pre></td></tr></table></div>

<p>Hier noch eine kleine Erweiterung die einen Log-Server implementiert. Er öffnet einen TCP Port, jeder der sich verbindet(z.B. per Telnet/Netcat) kann daraufhin die Log-Nachrichten mitlesen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Logger</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">init</span>
    ...
    <span style="color:#0066ff; font-weight:bold;">@port</span> = LOG_PORT
    <span style="color:#0066ff; font-weight:bold;">@query</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@query_mutex</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#0066ff; font-weight:bold;">@running</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#0066ff; font-weight:bold;">@server</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">start_server</span>
    <span style="color:#0066ff; font-weight:bold;">@query_mutex</span> = <span style="color:#CC00FF; font-weight:bold;">Mutex</span>.<span style="color:#9900CC;">new</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Start logging console @ #{LOG_HOST}:#{LOG_PORT}&quot;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@server</span> = TCPServer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>LOG_HOST, LOG_PORT<span style="color:#006600; font-weight:bold;">&#41;</span>
    clients = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@running</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">start</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      client = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0066ff; font-weight:bold;">@running</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#9966CC; font-weight:bold;">begin</span>
          client = <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">accept_nonblock</span>
          clients <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> client <span style="color:#9966CC; font-weight:bold;">if</span> client != <span style="color:#0000FF; font-weight:bold;">nil</span>
        <span style="color:#9966CC; font-weight:bold;">rescue</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">begin</span>
          message = <span style="color:#0000FF; font-weight:bold;">nil</span>
          <span style="color:#0066ff; font-weight:bold;">@query_mutex</span>.<span style="color:#9900CC;">synchronize</span> <span style="color:#006600; font-weight:bold;">&#123;</span> message = <span style="color:#0066ff; font-weight:bold;">@query</span>.<span style="color:#9900CC;">pop</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
          <span style="color:#9966CC; font-weight:bold;">if</span> message != <span style="color:#0000FF; font-weight:bold;">nil</span>
            clients.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>client<span style="color:#006600; font-weight:bold;">|</span>
                <span style="color:#9966CC; font-weight:bold;">begin</span>
                    client.<span style="color:#CC0066; font-weight:bold;">puts</span> message
                <span style="color:#9966CC; font-weight:bold;">rescue</span>
                    clients.<span style="color:#9900CC;">delete</span> client
                <span style="color:#9966CC; font-weight:bold;">end</span> 
            <span style="color:#9966CC; font-weight:bold;">end</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#0066ff; font-weight:bold;">@query</span>.<span style="color:#9900CC;">empty</span>?
        <span style="color:#CC0066; font-weight:bold;">sleep</span> LOG_SERVER_INTERVAL
      <span style="color:#9966CC; font-weight:bold;">end</span>
      clients.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>client<span style="color:#006600; font-weight:bold;">|</span> client.<span style="color:#9900CC;">close</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#0066ff; font-weight:bold;">@server</span>.<span style="color:#9900CC;">close</span>
      <span style="color:#0066ff; font-weight:bold;">@server</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">destroy</span>
    <span style="color:#0066ff; font-weight:bold;">@running</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#0066ff; font-weight:bold;">@fs</span>.<span style="color:#9900CC;">close</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># wait until server closed</span>
    <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0066ff; font-weight:bold;">@server</span> != <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#006600; font-weight:bold;">&lt;&lt;</span> msg
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@query_mutex</span> != <span style="color:#0000FF; font-weight:bold;">nil</span>
      <span style="color:#0066ff; font-weight:bold;">@query_mutex</span>.<span style="color:#9900CC;">synchronize</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#0066ff; font-weight:bold;">@query</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC0066; font-weight:bold;">format</span><span style="color:#006600; font-weight:bold;">&#40;</span>msg<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&gt;&gt;&gt; #{msg}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">flush</span>
    <span style="color:#008000; font-style:italic;"># just wait until everything is sent/written</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@query</span>.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
      <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0066ff; font-weight:bold;">@query</span>.<span style="color:#9900CC;">length</span> != <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>So das wars schon, ich hoffe das ich das mit dem Mutex richtig gemacht habe und das hier überhaupt notwendig war, ich habe auf dem Gebiet nicht viel Erfahrung und würde mich über einen entsprechenden Kommentar freuen.<br />
// I&#8217;m not sure I used the Mutex in this example correctly, I would appreciate a comment about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2009/06/13/kleine-ruby-logger-klasse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web scraping mit Ruby/Mechanize</title>
		<link>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/</link>
		<comments>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/#comments</comments>
		<pubDate>Wed, 27 May 2009 12:44:13 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[mechanize]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=398</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/>Praktisch jede Interaktion mit einer Website oder Webapplikation kann gescriptet, d.h. automatisiert werden. Das Abgrasen von Webseiten nach bestimmten Informationen wird auch als Scraping bezeichnet(für die nicht menschlichen Besucher dieser Seite sei das erwähnt *g*) Scripte können einem eine ganze Menge Arbeit abnehmen und sogar Dinge tun, die manuell unmöglich wären. Ich beschäftige mich mit [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/><p>Praktisch jede Interaktion mit einer Website oder Webapplikation kann gescriptet, d.h. automatisiert werden. Das Abgrasen von Webseiten nach bestimmten Informationen wird auch als <a href="http://en.wikipedia.org/wiki/Web_scraping">Scraping</a> bezeichnet(für die nicht menschlichen Besucher dieser Seite sei das erwähnt *g*) Scripte können einem eine ganze Menge Arbeit abnehmen und sogar Dinge tun, die manuell unmöglich wären. Ich beschäftige mich mit dem Thema schon seit einer ganzen Weile und möchte hier nun die von mir favorisierte Methode dafür vorstellen, die Bibliothek <a href="http://mechanize.rubyforge.org/mechanize/">Mechanize</a> für die Scriptsprache <a href="http://www.ruby-lang.org/en/">Ruby</a>.</p>
<p>Mechanize hat seinen <a href="http://search.cpan.org/dist/WWW-Mechanize/">Ursprung</a> in Perl, mittlerweile gibt es jedoch auch Implementierungen der API für <a href="http://wwwsearch.sourceforge.net/mechanize/">Python</a> und eben Ruby. Für PHP gibt es mit <a href="http://sourceforge.net/projects/snoopy/">Snoopy</a> ein ähnliches Projekt, wenn es auch bei weitem nicht so fortgeschritten ist. Mechanize bietet die Möglichkeit mit einfachen Methoden eine art Webbrowser zu simulieren. Alle Beispiele wurden mit Mechanize Version 0.9.2 und Ruby 1.8.7 getestet.</p>
<p><strong>Installation / Initialisierung</strong></p>
<p>Mechanize kann mit <a href="http://rubygems.org/">Gems</a>(ähnlich CPAN oder PEAR) installiert werden(<code># gem install mechanize --remote</code>), einige Distributionen bieten aber auch eigene Pakete an. Ein Ruby-Script kann daraufhin Mechanize inkludieren und ein Objekt erstellen:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span> <span style="color:#008000; font-style:italic;"># ist unter Umständen notwendig</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mechanize'</span>
agent = <span style="color:#6666ff; font-weight:bold;">WWW::Mechanize</span>.<span style="color:#9900CC;">new</span></pre></div></div>

<p>Jetzt ist Mechanize einsatzbereit, die folgenden Beispiele bauen darauf auf. Außerdem können mit dem <code>agent</code> nun noch grundlegende Einstellungen vorgenommen werden:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">set_proxy</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'localhost'</span>, <span style="color:#996600;">'8000'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
agent.<span style="color:#9900CC;">user_agent</span> = <span style="color:#996600;">'Individueller User-Agent'</span>
agent.<span style="color:#9900CC;">user_agent_alias</span> = <span style="color:#996600;">'Linux Mozilla'</span></pre></div></div>

<p>Die Einstellung &#8216;<code>user_agent_alias</code>&#8216; wählt einen User-Agent String aus dem folgenden Satz von Beispielen aus: Windows IE 6, Windows IE 7, Windows Mozilla, Mac Safari, Mac FireFox, Mac Mozilla, Linux Mozilla, Linux Konqueror, iPhone und Mechanize. Die Timing Einstellungen können ebenfalls sehr wichtig sein:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">open_timeout</span> = <span style="color:#006666;">3</span> <span style="color:#008000; font-style:italic;"># setzt timeouts</span>
agent.<span style="color:#9900CC;">read_timeout</span> = <span style="color:#006666;">4</span>
agent.<span style="color:#9900CC;">keep_alive</span> = <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#008000; font-style:italic;"># default ist true</span></pre></div></div>

<p>Hier folgen nun einige Beispiele, vielleicht werde ich mit der Zeit auch noch ein paar ergänzen, falls jemand Vorschläge hat, immer her damit. Ich habe auf <a href="http://apoc.sixserv.org/requestinfo">http://apoc.sixserv.org/requestinfo</a> ein kleines Skript am laufen das nützliche Informationen zum HTTP-Request liefert, das kann zum Experimentieren mit Mechanize sehr nützlich sein. Einige Beispiele findet man auch in den <a href="http://mechanize.rubyforge.org/mechanize/GUIDE_rdoc.html">GUIDE</a> und <a href="http://mechanize.rubyforge.org/mechanize/EXAMPLES_rdoc.html">EXAMPLES</a> Dateien des Mechanize Pakets. <span id="more-398"></span></p>
<p><strong>#01 &#8211; Manuelle Get Requests</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">url = <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>
page = agent.<span style="color:#9900CC;">get</span> url <span style="color:#008000; font-style:italic;"># einfacher get request</span>
<span style="color:#008000; font-style:italic;"># parameter können auch als Hash mitgeliefert werden:</span>
page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;value&quot;</span>, <span style="color:#996600;">&quot;key&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;val&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>#02 &#8211; Manuelle Post Requests</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">url = <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>
page = agent.<span style="color:#9900CC;">post</span><span style="color:#006600; font-weight:bold;">&#40;</span>url, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;value&quot;</span>, <span style="color:#996600;">&quot;key&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;val&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In diesem Fall werden die Post-Daten manuell erzeugt, häufig ist es jedoch besser das eigentliche Formular zu verwenden. z.B. ein Login oder Suchformular:</p>
<p><strong>#03 &#8211; Post Requests(/Submits) aus Formular erzeugen</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'https://twitter.com/login'</span>
login_form = page.<span style="color:#9900CC;">form_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'https://twitter.com/sessions'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
login_form<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'session[username_or_email]'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'[Username]'</span>
login_form<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'session[password]'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'[Password]'</span>
page = agent.<span style="color:#9900CC;">submit</span> login_form</pre></div></div>

<p>Dieses kleine Beispiel loggt sich bei Twitter ein.</p>
<p><strong>#04 &#8211; Navigation über Links und der History</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://www.heise.de/'</span>
page = agent.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">link_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>Telepolis<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
page = agent.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">link_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:href</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>artikel<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
agent.<span style="color:#9900CC;">back</span>
agent.<span style="color:#9900CC;">back</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">body</span></pre></div></div>

<p><strong>#05 &#8211; Exceptions</strong></p>
<p>Wird z.B. eine nicht vorhandene Seite aufgerufen, wirft Mechanize eine Exception, diese sollte Abgefangen werden da das Skript sonst an dieser Stelle abbricht.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://apoc.sixserv.org/diese/seite/gibt/es/nicht/'</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">WWW::Mechanize::ResponseCodeError</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ResponseCodeError - Code: #{$!}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>#06 &#8211; Referer</strong></p>
<p>Mechanize benutzt seine Navigations-History auch zum senden eines passenden Referers, diesen kann man allerdings auch manuell bestimmen, hier für GET und POST Requests:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>,
<span style="color:#ff3333; font-weight:bold;">:referer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://google.com/this/is/a/custom/referer'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">body</span></pre></div></div>

<p><strong>#07 &#8211; Headers / HEAD / Redirect</strong></p>
<p>Der Request Header kann manuell erweitert werden, bspw. den &#8220;X-Requested-With&#8221; so zu setzen um einen Ajax-Aufruf zu simulieren. Es gibt leider keine wirklich gute Möglichkeit eigene Header zu setzen, das ganze wurde in den vergangenen Versionen oft geändert, die folgende Methode funktioniert mit älteren Versionen von Mechanize nicht.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">pre_connect_hooks</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>params<span style="color:#006600; font-weight:bold;">|</span>
  params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:request</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'X-Requested-With'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'XMLHttpRequest'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Dies ist übrigens auch die einzig wirklich einfache Möglichkeit manuell Cookies zu setzen/ zu übertragen. Die andere Seite, also das Abfragen des Response-Headers ist trivial, hier wird z.B. per HEAD-Request die Server und evtl. der PHP-Version abgefragt:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">head</span> <span style="color:#996600;">'http://sixserv.org'</span>
server_version = page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'server'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Server: #{server_version}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> page.<span style="color:#9900CC;">header</span>.<span style="color:#9900CC;">key</span>? <span style="color:#996600;">'x-powered-by'</span>
  php_version = page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'x-powered-by'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;X-Powered-By: #{php_version}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Will man einen HTTP-Redirect(302) abfragen, muss man zunächst das automatische Weiterleiten im Mechanize deaktivieren, dann kann man den Zielort abfragen ohne das Ziel zu besuchen. (Nützlich z.B. wenn man auf einen Datei-Download weitergeleitet wird)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">redirect_ok</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://www.sixserv.org/'</span> <span style="color:#008000; font-style:italic;"># leitet auf http://sixserv.org weiter</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'location'</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p><strong>#08 &#8211; Threads</strong></p>
<p>Will man ein Script etwas beschleunigen kann man mit Threads arbeiten um mehrere Inhalte gleichzeitig abzufragen. Das folgende Grundgerüst für z.B. Foren mit vielen Seiten, bearbeitet 10 Seiten gleichzeitig und kann so ein Skript erheblich beschleunigen. In diesem Fall würde man allerdings wahrscheinlich eher nicht Mechanize einsetzen sondern eine Low-Level API fürs Netzwerk-Zugriff, das wäre deutlich effektiver ich will nur das Prinzip deutlich machen.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff6633; font-weight:bold;">$threads</span> = <span style="color:#006666;">0</span>
last_page = <span style="color:#006666;">200</span>
max_threads = <span style="color:#006666;">10</span>
<span style="color:#9966CC; font-weight:bold;">for</span> page <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">last_page</span>
  <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>page<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Create new Thread for scanning Page ##{page}&quot;</span>
&nbsp;
    page = <span style="color:#ff6633; font-weight:bold;">$agent</span>.<span style="color:#9900CC;">get</span> <span style="color:#996600;">&quot;http://example.com/datalist.php?page=#{page}&quot;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># process / save received data</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Destroy Thread&quot;</span>
    <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">&gt;</span> max_threads
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Next Page&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>#09 &#8211; Parsing / XPath</strong></p>
<p>Hat man eine Seite empfangen und will bestimmte Inhalte herausfiltern gibt es mehrere Möglichkeiten, meistens werden mehr oder weniger umfangreiche Reguläre Ausdrücke zum parsen verwendet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://example.com/'</span>
page.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">match</span> <span style="color:#006600; font-weight:bold;">/&lt;</span> h3<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&lt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span> \<span style="color:#006600; font-weight:bold;">/</span>h3<span style="color:#006600; font-weight:bold;">&gt;/</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Überschrift 3: #{$1}&quot;</span></pre></div></div>

<p>Wesentlich stabiler sind jedoch <a href="http://en.wikipedia.org/wiki/XPath">XPath</a> Ausdrücke die sich direkt auf die Struktur eines XML-Dokuments beziehen. Werden nur Kleinigkeiten am HTML-Code geändert sind RegEx&#8217;s häuft aufgeschmissen und müssen mühsam angepasst werden, XPath ausdrücke dagegen funktionieren häufig auch dann noch. Eine Einführung gibt es z.B. auf <a href="http://www.w3schools.com/XPath/default.asp">w3schools.com</a> oder der offizielle <a href="http://www.w3.org/TR/xpath">Standard der W3C</a>. Allerdings geht das alles auch sehr einfach mit entsprechenden Werkzeugen, so gibt es z.B. eine Menge Firefox-Addons(nützlich ist z.B. der <a href="https://addons.mozilla.org/en-US/firefox/addon/1095">XPath Checker</a>) die einem, zu markierten Elementen einer Seite einen passenden XPath Ausdruck anzeigt oder umgekehrt, wenn auch die nur einfachen Ausdrücke gelegentlich Nachbearbeitung erfordern.</p>
<div id="attachment_427" class="wp-caption aligncenter" style="width: 690px"><img class="size-full wp-image-427" title="XPath mit Firebug" src="http://sixserv.org/wp-content/uploads/2009/05/xpath_mit_firebug_small.png" alt="XPath mit Firebug" width="680" height="585" /><p class="wp-caption-text">Firebug kann einem zu beliebigen Elementen einer Seite einen passenden XPath(Rot unterstrichen) anzeigen.</p></div>
<p>Der von Mechanize verwendete HTML-Parser <a href="http://nokogiri.rubyforge.org/nokogiri/">Nokogiri</a>(鋸) ist sehr robust und kann auch &#8220;kaputtes&#8221; HTML parsen, was XPath zu einem auserordentlich mächtigen Werkzeug macht, um jede beliebige Information aus noch so komplexen Seiten zu extrahieren.</p>
<p>Mit <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a>: Ein beliebiges Element auf einer Seite markieren und im Kontextmenü, &#8220;Inspect Element&#8221; aufrufen. In der oberen Zeile von Firebug(siehe Screenshot) kann im Kontextmenü mit &#8220;Copy XPath&#8221; der XPath geholt werden. Im Beispiel wird der XPath zum Comic-Strip auf <a href="http://xkcd.com/">XKCD</a> angezeigt: <code>/html/body/div/div[2]/div/div[2]/div/div/img</code> In Mechanize kann so der IMG-Tag extrahiert werden:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://xkcd.com/'</span>
img = page.<span style="color:#9900CC;">search</span> <span style="color:#996600;">'/html/body/div/div[2]/div/div[2]/div/div/img'</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> img
<span style="color:#008000; font-style:italic;"># Ausgabe: &lt; img src=&quot;http://imgs.xkcd.com/comics/designated_drivers.png&quot; title=&quot;Calling a cab means cutting into beer money.&quot; alt=&quot;Designated Drivers&quot; &gt;</span></pre></div></div>

<p><em>Das wars damit vorerst. Anmerkungen oder Fragen zu den Beispielen oder Ideen für weitere, bitte in den Kommentaren hinterlassen.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Soup.io rbot Plugin/ ruby API</title>
		<link>http://sixserv.org/2009/02/11/soupio-rbot-plugin-ruby-api/</link>
		<comments>http://sixserv.org/2009/02/11/soupio-rbot-plugin-ruby-api/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 20:14:21 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[RBot]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[soup.io]]></category>
		<category><![CDATA[web2.0]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=313</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/>Mit Twitter kann ich sehr leicht über meinen rbot publizieren, damit ich in der selben Frequenz auch soup.io verwende, was für spezielleren Content besser geeignet ist, muss es schon ebenfalls über den rbot funktionieren. Nach langem suchen stellte ich überrascht fest das das große Web 2.0 Projekt Soup.io keine API zum leichten publizieren(außerhalb des Browsers) [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/><p>Mit <a href="https://twitter.com/nServ">Twitter</a> kann ich sehr leicht über meinen <a href="http://ruby-rbot.org/">rbot</a> publizieren, damit ich in der selben Frequenz auch <a href="http://apoc-sixserv.soup.io/">soup.io</a> verwende, was für spezielleren Content besser geeignet ist, muss es schon ebenfalls über den rbot funktionieren. Nach langem suchen stellte ich überrascht fest das das große Web 2.0 Projekt <a href="http://www.soup.io">Soup.io</a> <a href="http://getsatisfaction.com/soup/topics/will_there_be_some_api_for_soup_io_like_those_of_twitters">keine API</a> zum leichten publizieren(außerhalb des Browsers) besitzt. Da muss also erstmal eine art API gestrickt werden. Mit Hilfe von Mechanize baute ich die Browseranfragen nach und fasste es in einer kleinen Ruby Klasse zusammen. Es wird nicht jeder Content-Typ unterstützt aber für meine Zwecke reicht dies völlig aus. Dann noch schnell eine rbot-Plugin Klasse zum bedienen der eigenen &#8220;API&#8221; und fertig war die erste Version meines <a href="http://rbot.noway.ratry.ru/plugins/show/19">soupio-Plugins</a>.<br />
Hier eine Liste der möglichen Befehle(Argumente in eckigen Klammern sind Optional):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">soup identify <span style="color:#006600; font-weight:bold;">&lt;</span>username<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span>password<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Jeder Benutzer im Channel kann dem Bot seine 
Soup.<span style="color:#9900CC;">io</span><span style="color:#006600; font-weight:bold;">-</span>Zugangsdaten im Query mitteilen.
&nbsp;
<span style="color:#9900CC;">soup</span> login <span style="color:#006600; font-weight:bold;">=&gt;</span> Neuer Login falls die gespeicherte SessionId verloren 
oder ungültig wird. <span style="color:#006600; font-weight:bold;">&#40;</span>Normalerweise nicht notwendig.<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
soup link <span style="color:#006600; font-weight:bold;">&lt;</span>url<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&lt;</span>title<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
soup image <span style="color:#006600; font-weight:bold;">&lt;</span>url<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&lt;</span>description<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
soup text <span style="color:#006600; font-weight:bold;">&lt;</span>text<span style="color:#006600; font-weight:bold;">&gt;</span>
soup quote <span style="color:#006600; font-weight:bold;">&lt;</span>source<span style="color:#006600; font-weight:bold;">&gt;</span>: <span style="color:#006600; font-weight:bold;">&lt;</span>quote<span style="color:#006600; font-weight:bold;">&gt;</span>
soup video <span style="color:#006600; font-weight:bold;">&lt;</span>youtube<span style="color:#006600; font-weight:bold;">-</span>url<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&lt;</span>description<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>Die SoupIoClass-Klasse kann übrigens auch außerhalb des Plugins, in jeder Ruby-Anwendung verwendet werden. Hier eine kleine Referenz, diesmal die optionalen Argumente in spitzen Klammern:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">soup = SoupIoClass.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'[Username]'</span>, <span style="color:#996600;">'[Password]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Domain]'</span>, <span style="color:#996600;">'[Session-ID]'</span><span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
soup.<span style="color:#9900CC;">new_link</span> <span style="color:#996600;">'[URL]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Title]'</span>, <span style="color:#996600;">'[Description]'</span><span style="color:#006600; font-weight:bold;">&gt;</span>
soup.<span style="color:#9900CC;">new_image</span> <span style="color:#996600;">'[URL]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Description]'</span><span style="color:#006600; font-weight:bold;">&gt;</span>
soup.<span style="color:#9900CC;">new_text</span> <span style="color:#996600;">'[Text]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Title]'</span><span style="color:#006600; font-weight:bold;">&gt;</span>
soup.<span style="color:#9900CC;">new_quote</span> <span style="color:#996600;">'[Quote]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Source]'</span><span style="color:#006600; font-weight:bold;">&gt;</span>
soup.<span style="color:#9900CC;">new_video</span> <span style="color:#996600;">'[Youtube-URL]'</span><span style="color:#006600; font-weight:bold;">&lt;</span>, <span style="color:#996600;">'[Description]'</span><span style="color:#006600; font-weight:bold;">&gt;</span></pre></td></tr></table></div>

<p>Die Domain und SessionId kann mit soup.sessid und soup.domain abgefragt werden. Die SessionId ist praktisch unbegrenzt lange haltbar, weshalb diese beiden Daten zwischengespeichert werden können um bei häufiger Nutzung der Klasse sich nicht ständig neu Anmelden zu müssen.</p>
<p>Die Version 0.1 ist bereits <a href="http://rbot.noway.ratry.ru/plugins/show/19">veröffentlicht</a>, ich muss mich noch um die Validierung und Fehlerabfragen kümmern aber sonst sollte das Plugin schon funktionieren. Fehler bitte bei mir Melden.<br />
<strong>Update:</strong> Version 0.2 veröffentlicht. (nur kleine Änderungen)<br />
<strong>Update:</strong> Version 0.3 veröffentlicht. (Bugfix für eigene Domains)<br />
<strong>Update:</strong> <a href="http://rbot.noway.ratry.ru/files/soupio-0.4.rb">Version 0.4</a> veröffentlicht. (Änderungen an soup.io)</p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2009/02/11/soupio-rbot-plugin-ruby-api/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>xinetd: info script</title>
		<link>http://sixserv.org/2009/01/22/xinetd-info-script/</link>
		<comments>http://sixserv.org/2009/01/22/xinetd-info-script/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 18:00:29 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RBot]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[hddtemp]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[sensors]]></category>
		<category><![CDATA[xinetd]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=142</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_linux.png" width="50" height="51" alt="" title="Linux" /><br/>Ich wollte von unterwegs aus den Status meines Heimservers abrufen können. Dabei ging es mir vorallem um die Temperatur von CPU, Mainboard und den Festplatten. Der auf sixserv.org laufende rbot(im Freenode idled der in #sixserv) soll auf Kommando den Status anzeigen. Soweit so gut. Ein kleines Ruby-Skript das auf dem Server zuhause läuft erfasst die [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_linux.png" width="50" height="51" alt="" title="Linux" /><br/><p>Ich wollte von unterwegs aus den Status meines Heimservers abrufen können. Dabei ging es mir vorallem um die Temperatur von CPU, Mainboard und den Festplatten. Der auf sixserv.org laufende rbot(im Freenode idled der in #sixserv) soll auf Kommando den Status anzeigen. Soweit so gut. Ein kleines Ruby-Skript das auf dem Server zuhause läuft erfasst die Temperaturen per &#8220;sensors&#8221; und &#8220;hddtemp&#8221;. Der xinetd-Daemon konfigurierte ich daraufhin so das auf einen Port das Skript gebunden wird. Es erwartet bevor es die Daten übermittelt ein Passwort, einfach zum zusätzlichen Schutz auch wenn das vielleicht gar nicht nötig ist. Jemand der einen Portscan durchführt könnte eben so informationen zum System gelangen, die Passwortabfrage verhindert dies.</p>
<p>Zunächst zu dem Ruby-Script(z.B. /opt/botinfo.rb):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
i = <span style="color:#CC00FF; font-weight:bold;">Kernel</span>.<span style="color:#CC0066; font-weight:bold;">gets</span>
<span style="color:#9966CC; font-weight:bold;">if</span> i.<span style="color:#CC0066; font-weight:bold;">chomp</span> != <span style="color:#996600;">'DASGEHEIMEPASSWORT'</span> <span style="color:#9966CC; font-weight:bold;">then</span>
	<span style="color:#008000; font-style:italic;"># puts 'Wrong Password'</span>
	<span style="color:#CC0066; font-weight:bold;">exit</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">`uptime`</span>.<span style="color:#9900CC;">lstrip</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># HDD Temps:</span>
matches = <span style="color:#996600;">`cat /proc/partitions`</span>.<span style="color:#9900CC;">scan</span> <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>s<span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">&#93;</span>d<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>
matches.<span style="color:#9900CC;">uniq</span>!
matches.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>disk<span style="color:#006600; font-weight:bold;">|</span>
	<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{disk}: #{`hddtemp -n /dev/#{disk}`.chomp}.0*C (#{$1}GB) &quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> 
&nbsp;
systemp = <span style="color:#996600;">`sensors`</span>
&nbsp;
temp1 = systemp.<span style="color:#9900CC;">scan</span> <span style="color:#006600; font-weight:bold;">/</span>CPU Temp:    \<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\.0.<span style="color:#9900CC;">C</span><span style="color:#006600; font-weight:bold;">/</span>
temp2 = systemp.<span style="color:#9900CC;">scan</span> <span style="color:#006600; font-weight:bold;">/</span>M\<span style="color:#006600; font-weight:bold;">/</span>B Temp:    \<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\.0.<span style="color:#9900CC;">C</span><span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;System: #{temp2[0]}.0*C #{temp2[1]}.0*C | CPUs: #{temp1[0]}.0*C #{temp1[1]}.0*C&quot;</span></pre></td></tr></table></div>

<p>Hier muss natürlich sensors und hddtemp installiert sein, aber dieses Script kann praktisch alles mögliche an Informationen sammeln und ausgeben.<br />
Die Konfiguration von xinetd gestaltet sich sehr einfach, in dem Verzeichnis /etc/xinetd.d einfach eine neue Datei für das Script erstellen(z.B. &#8220;botinfo&#8221;):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">service botinfo
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    disable         = no
    port            = <span style="color: #000000;">8888</span>
    socket_type     = stream
    protocol        = tcp
    <span style="color: #7a0874; font-weight: bold;">wait</span>            = no
    user            = apoc
    server          = <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>botinfo.rb
    <span style="color: #7a0874; font-weight: bold;">type</span>            = unlisted
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Den Port, User und den Skript Pfad entsprechend anpassen und xinetd neu starten. Mit netcat kann es man danach testen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">$ nc localhost <span style="color: #000000;">8888</span>
DASGEHEIMEPASSWORT
<span style="color: #000000;">18</span>:<span style="color: #000000;">40</span>:<span style="color: #000000;">35</span> up <span style="color: #000000;">3</span> days, <span style="color: #000000;">41</span> min,  <span style="color: #000000;">4</span> <span style="color: #c20cb9; font-weight: bold;">users</span>,  load average: <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.02</span>, <span style="color: #000000;">0.20</span>
sda: <span style="color: #000000;">30.0</span><span style="color: #000000; font-weight: bold;">*</span>C <span style="color: #7a0874; font-weight: bold;">&#40;</span>10GB<span style="color: #7a0874; font-weight: bold;">&#41;</span> sdb: <span style="color: #000000;">29.0</span><span style="color: #000000; font-weight: bold;">*</span>C <span style="color: #7a0874; font-weight: bold;">&#40;</span>10GB<span style="color: #7a0874; font-weight: bold;">&#41;</span>
System: <span style="color: #000000;">39.0</span><span style="color: #000000; font-weight: bold;">*</span>C <span style="color: #000000;">38.0</span><span style="color: #000000; font-weight: bold;">*</span>C <span style="color: #000000; font-weight: bold;">|</span> CPUs: <span style="color: #000000;">37.0</span><span style="color: #000000; font-weight: bold;">*</span>C <span style="color: #000000;">36.0</span><span style="color: #000000; font-weight: bold;">*</span>C</pre></td></tr></table></div>

<p>Der Port muss ggf. vom Router geforwarded werden damit ein Entfernter Server darauf zugreifen kann. Auch ein dyndns ist hilfreich, sofern man über keine statische IP verfügt. Ein einfaches rbot-Plugin um diese Daten vom irc aus abzufragen sieht z.B. so aus:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'socket'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> BotinfoPlugin <span style="color:#006600; font-weight:bold;">&lt;</span> Plugin
  <span style="color:#9966CC; font-weight:bold;">def</span> help<span style="color:#006600; font-weight:bold;">&#40;</span>plugin, topic=<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#996600;">'info =&gt; return system information'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> info<span style="color:#006600; font-weight:bold;">&#40;</span>m, params<span style="color:#006600; font-weight:bold;">&#41;</span>
    sock = TCPSocket.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'heimserver.dyndns.org'</span>, <span style="color:#006666;">8888</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    sock.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'DASGEHEIMEPASSWORT'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    m.<span style="color:#9900CC;">reply</span> sock.<span style="color:#9900CC;">recv</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1024</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    sock.<span style="color:#9900CC;">close</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
plugin = BotinfoPlugin.<span style="color:#9900CC;">new</span>
plugin.<span style="color:#9900CC;">map</span> <span style="color:#996600;">'info'</span></pre></td></tr></table></div>

<p>Die Daten können ebenfalls von einem PHP-Script aus abgefragt werden. Keines Beispiel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$socket</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_create</span><span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_STREAM<span style="color: #339933;">,</span> SOL_TCP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">socket_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$socket</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;heimserver.dyndns.org&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;8888&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DASGEHEIMEPASSWORT<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$socket</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">socket_read</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$socket</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2048</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Vielleicht findet das ja irgendjemand interessant <img src='http://sixserv.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2009/01/22/xinetd-info-script/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rbot Remote</title>
		<link>http://sixserv.org/2008/09/15/rbot-remote/</link>
		<comments>http://sixserv.org/2008/09/15/rbot-remote/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 21:14:48 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[RBot]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[drb]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=90</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/>Ich setze schon lange den grandiosen IRC-Bot &#8220;rbot&#8221; ein. Ein überaus vielseitiger und einfach zu erweiternder Bot, der dazu auch noch in meiner Lieblings Script-Sprache Ruby geschrieben ist. Vielleicht komme ich dazu einige meiner Plugins, die ich für ihn geschrieben habe, hier zu Veröffentlichen. Eines der Features von rbot welches ich erst kürzlich entdeckt habe [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/><p>Ich setze schon lange den grandiosen IRC-Bot &#8220;<a href="http://ruby-rbot.org/">rbot</a>&#8221; ein. Ein überaus vielseitiger und einfach zu erweiternder Bot, der dazu auch noch in meiner Lieblings Script-Sprache <a href="http://www.ruby-lang.org/">Ruby</a> geschrieben ist. Vielleicht komme ich dazu einige meiner Plugins, die ich für ihn geschrieben habe, hier zu Veröffentlichen.</p>
<p>Eines der Features von rbot welches ich erst kürzlich entdeckt habe und was absolut genial ist, ist das <a href="http://ruby-rbot.org/rbot-trac/wiki/RbotRemote">Rbot Remote Interface</a>. Per Default hört der Bot nämlich auf Port 7268(127.0.0.1) und stellt dort ein <a href="http://segment7.net/projects/ruby/drb/">DRb(Distributed Ruby)</a> Interface zur Verfügung. Dieses erlaubt es von Außen den Bot zu steuern und beispielsweise Funktionen eines Plugins auszuführen. RbotRemote kann z.B. dazu genutzt werden bei neuen SVN-Commits im RSS Plugin das Updaten eines Commit-Feeds zu starten. Denkbar ist dies natürlich auch für neue Blog-Posts innerhalb von WordPress etc.</p>
<p>Da DRb logischerweise nur für Ruby zur Verfügung steht muss bspw. eine PHP-Webapplikation ein Ruby-Skript starten welches die gewünschte Aktionen am rbot auslöst. Hier als Beispiel-Skript wird eine Nachricht an #sixserv(btw: im Freenode) gesendet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb'</span>
&nbsp;
rbot = DRbObject.<span style="color:#9900CC;">new_with_uri</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;druby://localhost:7268&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
id = rbot.<span style="color:#9900CC;">delegate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">&quot;remote login owner [Owner/Auth Passwort]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:return</span><span style="color:#006600; font-weight:bold;">&#93;</span>
rbot.<span style="color:#9900CC;">delegate</span><span style="color:#006600; font-weight:bold;">&#40;</span>id, <span style="color:#996600;">&quot;dispatch say #sixserv Hallo Welt!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In <a href="http://ruby-rbot.org/rbot-trac/browser/bin/rbot-remote">/bin/rbot-remote</a> gibt es ein ähnliches, etwas komplexeres Beispiel welches die Eingaben von stdin erwartet.</p>
<p>Eine weitere Möglichkeit ist wie schon erwähnt eine Methode eines Plugins zu starten. Hier ein ganz einfaches rbot-(Remote)Plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SimplePlugin <span style="color:#006600; font-weight:bold;">&lt;</span> Plugin
  <span style="color:#9966CC; font-weight:bold;">include</span> RemotePlugin
  <span style="color:#9966CC; font-weight:bold;">def</span> sayfoo<span style="color:#006600; font-weight:bold;">&#40;</span>m, params<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@bot</span>.<span style="color:#9900CC;">say</span> <span style="color:#996600;">'#sixserv'</span>, <span style="color:#996600;">'foo'</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> params.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bar</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">then</span>
      <span style="color:#0066ff; font-weight:bold;">@bot</span>.<span style="color:#9900CC;">say</span> <span style="color:#996600;">'#sixserv'</span>, params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bar</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
plugin = SimplePlugin.<span style="color:#9900CC;">new</span>
plugin.<span style="color:#9900CC;">remote_map</span> <span style="color:#996600;">'sayfoo'</span>
plugin.<span style="color:#9900CC;">remote_map</span> <span style="color:#996600;">'sayfoo :bar'</span></pre></div></div>

<p>Wird die Methode extern aufgerufen kann man natürlich kein m.reply verwenden, wenn die Nachricht im Channel landen soll, ich habe das hier ganz einfach gelöst indem ich den Channel fest eingebunden habe. Hier noch das Skript welches sayfoo mit Parameter aufruft:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'drb'</span>
&nbsp;
rbot = DRbObject.<span style="color:#9900CC;">new_with_uri</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;druby://localhost:7268&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
id = rbot.<span style="color:#9900CC;">delegate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#996600;">&quot;remote login owner [Owner/Auth Passwort]&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:return</span><span style="color:#006600; font-weight:bold;">&#93;</span>
rbot.<span style="color:#9900CC;">delegate</span><span style="color:#006600; font-weight:bold;">&#40;</span>id, <span style="color:#996600;">&quot;sayfoo bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Update: Das Plugin auf remote_map geändert, thanks for the hint tango!</p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2008/09/15/rbot-remote/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
