Pastes:104575  Views:2159364 

Languages:

   ASP
   C#
   C++
   HTML
   Java
   PHP
   VB.NET
   Visual Basic 6

more languages available...


Looking to freelance?


Warning: file_get_contents(http://www.getafreelancer.com/affiliates/itsdan/6f.txt) [function.file-get-contents]: failed to open stream: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? in /home/nmp/public_html/support/googlereferrals.php on line 21
( bids)

Source: GetAFreelancer.com


Free 3D Engine

 

 

 

 

 

 

Here's the requested code from the pastebin:

Wrap Lines   Use Line Numbers
#
#
# Filename: miniclient.py
# Author: astigmatik a.k.a. blue-elf
# Date: 4 July 2007
#
#
 
 
from java.awt import *
from java.awt.event import ActionListener
from javax.swing import UIManager, JFrame, JScrollPane, JTextField, JTextArea
 
import socket
 
try:
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
except:
    pass
 
class IRCclient(JFrame):
 
    def __init__(self):
        JFrame.__init__(self, "MiniClient", defaultCloseOperation=3, size=(500, 400))
        f = ('monospaced', Font.PLAIN, 13)
        self.textarea = JTextArea('', font=f, editable=0, lineWrap=1, wrapStyleWord=1)
        self.editbox = JTextField('', font=f)
        self.getContentPane().add(JScrollPane(self.textarea), BorderLayout.CENTER)
        self.getContentPane().add(self.editbox, BorderLayout.SOUTH)
        self.show()
        self.editbox.requestFocus()
 
    def newSession(self):
        s = IRCsession(self)
        s.connect()
 
 
class AActionListener(ActionListener):
    """
    I know this could have been written better,
    but I started writing this when I was a newbie to Jython.
    """
    def __init__(self, args):
        self.docommand = args
 
    def actionPerformed(self, event):
        self.docommand(event.getSource() , event.getSource().getText())
 
class IRCsession:
 
    def __init__(self, client):
        self.sockname = None
        self.client = client
        self.client.editbox.addActionListener(AActionListener(self.inputparser))
 
    def connect(self):
        self.echo("Attempting to connect")
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('localhost', 6667))
            self.sockname = s
            self.sockwrite('USER %s %s %s :%s' % ('ident', 'server', 'ident', 'realname'))
            self.sockwrite('NICK mynick')
        except socket.error:
            self.sockname = None
            self.echo("Socket error in connect\(\)")
        if self.sockname is not None:
            self.sockread()
 
    def sockread(self):
        sockname = self.sockname
        readbuffer = ''
        while 1:
            try:
                readbuffer = readbuffer + sockname.recv(1024)
                data = readbuffer.split("\n")
                readbuffer = data.pop()
                if len(data) > 0:
                    self.sockparse(data)
                elif self.checkstatus() == 'ERR':
                    """
                     If len(data) is 0, I don't want to assume that I've been
                     disconnected. Hence, I do a checkstatus() which is a try/except.
                     If the exception occurs, only then I should be able to safely
                     conclude that I'm offline.
                    """
                    self.echo("Seems to be disconnected")
                    break
            except socket.error:
                self.sockname = None
                self.echo("Socket error in sockread\(\)")
                break
 
    def echo(self, data):
        target = self.client.textarea
        target.append(data + '\n')
        target.setCaretPosition(target.getDocument().getLength())
 
    def sockparse(self, data):
            for line in data:
                line = line.rstrip()
                line_split = line.split(' ')
                if line_split[0] == 'PING':
                    self.sockwrite('PONG ' + line_split[1])
                self.echo(line)
 
    def sockwrite(self, data):
        self.echo('Sending data: ' + data)
        if self.sockname is not None:
            self.sockname.send(data + '\r\n')
 
 
    def checkstatus(self):
        """
           I still use self.sockname because during sockread,
           the self.sockname value is only set to None only if there is
           a socket exception. So by right, sockname SHOULD still have
           some value at this point.
        """ 
        try:
            self.sockname.sendall('PING\n')
            return 'OK'
        except socket.error, sockerr:
            """
               sockerr is there because I want to print it out, do stuff, etc.
            """
            self.sockname.close()
            return 'ERR'
 
    def inputparser(self, editbox, data):
        if data[0] == '/':
            parms = data.split(' ')
            parm1 = parms[0][1:].upper() + ' '
            self.sockwrite(parm1 + ' '.join(parms[1:]))
            editbox.setText('')
 
def main():
    c = IRCclient()
    c.newSession()
 
if __name__ == '__main__':
    main()
 
Syntax Highlighting:  



Copyright 2007 - NoMorePasting.com