Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Instruct a shared whiteboard to save and refresh

The following code used with the ProjectX API informs the client web browser that the whiteboard will be refreshed in 5 seconds. It then archives the current whiteboard information, formats it, and sends a message to each web browser to refresh their view.

<project name="whiteboardqueue">
  <methods>
    <method name="create">
      <params>
        <param var="type">ecmascript</param>
        <param var="body">startRefresh(5)</param>
        <param var="sender">system</param>
      </params>
    </method>
    <method name="timer">
      <params>
        <param var="timer">5</param>
      </params>
    </method>
    <method name="archive_and_format">
      <params/>
    </method>
    <method name="create">
      <params>
        <param var="type">ecmascript</param>
        <param var="body">reloadDocument()</param>
        <param var="sender">system</param>
      </params>
    </method>
  </methods>
</project>


*update 1:14am*
The whiteboard demo [rorbuilder.info] allows the user to draw using the mouse within the web browser which renders SVG. Tested on Flock and Firefox.

*update 4:42pm 28 Mar 08*
You can also view the whiteboard message queue [rorbuilder.info].

*update 6:29pm Mar 08*
I've created a short url (http://rubyurl.com/vxHD) (to demonstrate the cleaning of the whiteboard) which redirects to this http://rorbuilder.info/api/projectx.cgi?xml_project=<project name="whiteboardqueue"><methods><method name="create"><params><param var="type">ecmascript</param><param var="body">startRefresh(5)</param><param var="sender">system</param></params></method><method name="timer"><params><param var="timer">5</param></params></method><method name="archive_and_format"><params/></method><method name="create"><params><param var="type">ecmascript</param><param var="body">reloadDocument()</param><param var="sender">system</param></params></method></methods></project>

I've

Sending a message using XMPP and Ruby

This Ruby code shows how to send an instant message through XMPP [wikipedia.org] using XMPP4R [gna.org]. Source code origin: Ruby and XMPP/Jabber Part 2: Logging in and sending simple messages [famundo.com].

Preparation
- Installed eJabberd on my Gentoo server
- Created 2 user accounts using Pidgin Internet Messenger on my Ubuntu desktop.
- Tested sending messages using Jabber Instant Messaging (Gabber) and Pidgin Internet Messenger.

Installation
gem install xmpp4r

What we will need
require 'rubygems'
require 'xmpp4r'
include Jabber

Logging in
jid = JID::new('yourname@yourdomain.com/Testing')
password = 'yourpassword'
cl = Client::new(jid)
cl.connect
cl.auth(password)

Sending a simple message
to = "user2@yourdomain.com"
subject = "XMPP4R test"
body = "Hi, this is my first try from XMPP4R!!!"
m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)
cl.send m

References:
- Building a Twitter Agent with Ruby and Rails [rubyinside.com]
- http://gentoo-wiki.com/Ejabberd

* update 15:45 18-Feb-08 *
Here's a simpler example I found from Liminal Existence: Announcing Jabber::Simple [romeda.org]

sudo gem install xmpp4r-simple
require 'xmpp4r-simple'

jabber = Jabber::Simple.new('yourname@yourdomain.com', 'yourpassword')
jabber.deliver("user1@yourdomain.com", "Hey! I'm thinking of going Vegetarian - Any suggestions?")


see also: IM Integration With XMPP4r : Part 2 [rubyfleebie.com]

Send and receive SMS text messages with Ruby and a GSM/GPRS modem

require 'serialport'
require 'time'

class GSM
  
  SMSC = "+447785016005"  # SMSC for Vodafone UK - change for other networks

  def initialize(options = {})
    @port = SerialPort.new(options[:port] || 3, options[:baud] || 38400, options[:bits] || 8, options[:stop] || 1, SerialPort::NONE)
    @debug = options[:debug]
    cmd("AT")
    # Set to text mode
    cmd("AT+CMGF=1")
    # Set SMSC number
    cmd("AT+CSCA=\"#{SMSC}\"")    
  end
  
  def close
    @port.close
  end
  
  def cmd(cmd)
    @port.write(cmd + "\r")
    wait
  end
  
  def wait
    buffer = ''
    while IO.select([@port], [], [], 0.25)
      chr = @port.getc.chr;
      print chr if @debug == true
      buffer += chr
    end
    buffer
  end

  def send_sms(options)
    cmd("AT+CMGS=\"#{options[:number]}\"")
    cmd("#{options[:message][0..140]}#{26.chr}\r\r")
    sleep 3
    wait
    cmd("AT")
  end
  
  class SMS
    attr_accessor :id, :sender, :message, :connection
    attr_writer :time
    
    def initialize(params)
        @id = params[:id]; @sender = params[:sender]; @time = params[:time]; @message = params[:message]; @connection = params[:connection]
    end
    
    def delete
      @connection.cmd("AT+CMGD=#{@id}")
    end
    
    def time
      # This MAY need to be changed for non-UK situations, I'm not sure
      # how standardized SMS timestamps are..
      Time.parse(@time.sub(/(\d+)\D+(\d+)\D+(\d+)/, '\2/\3/20\1'))
    end
  end
  
  def messages
    sms = cmd("AT+CMGL=\"ALL\"")
    # Ugly, ugly, ugly!
    msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
    return nil unless msgs
    msgs.collect!{ |m| GSM::SMS.new(:connection => self, :id => m[0], :sender => m[1], :time => m[2], :message => m[3].chomp) } rescue nil
  end
end


destination_number = "+44 someone else"

p = GSM.new(:debug => false)

# Send a text message
p.send_sms(:number => destination_number, :message => "Test at #{Time.now}")

# Read text messages from phone
p.messages.each do |msg|
  puts "#{msg.id} - #{msg.time} - #{msg.sender} - #{msg.message}"
  # msg.delete
end

Send a message with zope

// description of your code here


      # get a correct mail

      searching_obj = context
      interesatuen_epostak = []

      while interesatuen_epostak == []:
            if searching_obj.interesatuen_epostak != []:
               for m in searching_obj.interesatuen_epostak:
                   if '@' in m:
                      interesatuen_epostak.append(m)
            searching_obj = searching_obj.aq_parent


      # send a mail

      try:
        mailhost=getattr(context, context.superValues('Mail Host')[0].id)
      except:
        raise AttributeError, "cant find a Mail Host object"

      for i in interesatuen_epostak: 
       mMsg = "To: " + i + "\n"
       mMsg = mMsg + "From: intranet@zenbaki.es\n"
       mMsg = mMsg + "Mime-Version: 1.0\n"
       mMsg = mMsg + "Content-Type: text/html; charset=ISO-8859-1\n\n"

       mMsg = mMsg + "<html><head></head><body bgcolor='#dddddd'>"
       mMsg = mMsg + "<h1>Aldaketak:</h1>"
       mMsg = mMsg + "<p>"+user.getUserName()

       if user.getUserName()[-1] in ['a','e','i','o','u']:
          mMsg = mMsg + "k,"
       else:
          mMsg = mMsg + "ek,"

       mMsg = mMsg + " '<a href='"+obj.absolute_url()+"'>"+obj.title+"</a>' HTML fitxategia gehitu zuen "
       mMsg = mMsg + "'<a href='"+container_folder.absolute_url()+"'>"+container_folder.id+"</a>' izendako karpetan.</p>"
       mMsg = mMsg + "</body></html>"

       mSubj = "[intranet] '"+obj.title+"' HTML fitxategia gehituta izan zen"
       mailhost.send(mMsg, subject=mSubj, encode='base64')

Showing message on status bar

There's an empty space below app.title that we can
use as a status bar. So, I modify this recipe and
make it into a module. (need fgimage.pyd from here)
# status.py
from graphics import *
import fgimage
__all__ = ["status_on", "status_off"]

bar = Image.new((119,13))
bgcolor = screenshot().getpixel((0,0))[0]
fg = fgimage.FGImage()

def status_on(message=None):
    if message is not None:
        bar.clear(bgcolor)
        bar.text((0,11), unicode(message), 0xffffff)
    fg.set(57,30, bar._bitmapapi())

def status_off():
    fg.unset()

You can use it easily this way
>>> from status import *
>>> status_on('Hello world')  # show it
>>> status_off()              # hide it
>>>

See a screenshot.
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS