Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, February 16, 2019

ROT Cipher in Python

Recently I was messing around with some stuff and kept needing a Caesar Cipher deciphered and kept using some not-so-good online ones. So what better thing to do than make one myself. Why even stop at a Caesar Cipher and just make an adjustable ROT tool. So rather than copying one that probably works better and has a nicer design, I made my own.

#!/usr/bin/python3
#
# Made by: Matthew DeSantis
#          www.anarchy46.net

import sys
import os

# Upper case limits
CAP_MIN = ord( 'A' )
CAP_MAX = ord( 'Z' )
# Lower case limits
LOW_MIN = ord( 'a' )
LOW_MAX = ord( 'z' )

# Rotate the characters
def rotate ( offset, c ):
    o = ord( c )
    # Uppercase
    if o >= CAP_MIN and o <= CAP_MAX:
        o = o + offset
        # Loop back to beginning
        if o > CAP_MAX:
            o = o - CAP_MAX + CAP_MIN - 1

        # Loop back to end for backwards
        elif o < CAP_MIN:
            o = CAP_MAX - CAP_MIN + o + 1

    # Lowercase
    elif o >= LOW_MIN and o <= LOW_MAX:
        o = o + offset
        # Loop back to beginning
        if o > LOW_MAX:
            o = o - LOW_MAX + LOW_MIN - 1

        # Loop back to the end
        elif o < LOW_MIN:
            o = LOW_MAX - LOW_MIN + o + 1

    return chr( o )

# Cycle through the string
def cipher ( offset, text ):
    output=''
    for t in text:
        output += rotate( offset, t )
    return output

if __name__ == '__main__':
    try:
        # Max rotation offset
        max_off = CAP_MAX - CAP_MIN + 1
        offset = int( sys.argv[1] )
        # Get the offset from max (like if 27 is in and max is 25, you get 2)
        # Then set positive or negative
        offset = offset and (abs( offset ) - abs( max_off * int( offset / max_off ) )) * (offset / abs( offset )) or offset

        # Text is passed via args
        if len( sys.argv ) > 2:
            text = " ".join( sys.argv[2:] )
            eol = os.linesep

        # Or text is passed via pipe (./rot.py 13 < example.txt)
        elif not sys.stdin.isatty():
            text = "".join( sys.stdin.readlines() )
            eol = ''

        # No text!
        else:
            raise Exception( "Missing text!" )
        print( cipher( int( offset ), text ), end=eol )

    # Not a valid number or the like.
    except ValueError:
        print( "Invalid type." )
        exit( 1 )

    # Catch anything else because oops.
    except Exception as err:
        print( err )
        exit( 1 )


So saving this as rot.py, I can use it for stuff in the terminal:

./rot.py 13 This is a secret message

Or I can use it to decrypt a file:

./rot.py -13 < secret.txt

I tried to make it as flexible as possible. Decryption can be done by using a negative, or you could cipher it negatively, whatever makes you happy. My math for doing the offset is a bit of evil. The idea is to rotate no more than once around while preserving which way around it goes. There are most certainly better ways to do it, but I was feeling "artistic."

Sunday, January 19, 2014

Bulk File Renaming

While in the process of transferring music and shows to a media server, I noticed an annoying problem. All the file names were formatted differently than I wanted. After manually changing files in one folder and realizing how long it took, I thought there must be an easier way. After a very short Google search, I figured what a better way to support what I need than to make such a program myself. So I created rename.py. It is a command line utility that allows bulk renaming of files and/or directories, supporting basic stripping, regular expression matching and Python string formatting.

You can find the download link for it here.

Now here is a quick rundown on ways to use it.

#strips first 4 chars off every file
./rename.py 4
#strips in the Music/5FDP folder
./rename.py -p Music/5FDP 4
# Takes any file in Music/RATM that looks like "something - somethingelse"
# and changes it to RATM.somethingelse
./rename.py -p Music/RATM -r ".+ - (.*)" "RATM.{0}"
# help for more info
./rename.py -h

There are other options like -d for including directories and -o for only directories. Named groupings are also supported, for more information, it would be best to look into regular expressions and Python's str.format() to fully utilize this tool.

Wednesday, April 10, 2013

Resistor Calculator wxPython

So recently I decided to take a serious attempt at programming with using a GUI. While it has not been easy, the results are very satisfying. This program was entirely from scratch to get my feet wet, but I think my next project will be converting an already existing program to a GUI.

I chose wx over Tkinter for a couple reasons. The first was wx looks native while Tkinter looks like a Java application. Also, much like a Java application, however the windows are made with Tkinter, they have a bad habit of glitching out and making pieces illegible. The downside to wx is that it is not included with Python, however the wx packages are easy enough to get a hold of (in many different languages) and easy to install.

I'm happy with the end result, both the GUI and the code. I spent some extra time afterwards cleaning up the code, expanding variable names and commenting. This proved useful since I deleted a bunch of code that didn't do anything. Granted it's not perfect, I'm happy that overall it works.

You can check it out on my site here, or just copy/paste the source code.

#!/usr/bin/env python
# Author: Matthew DeSantis
#
# Resistor Calc

import wx

# Colors aranged by value (index=value)
COLOR = [
    'black',
    'brown',
    'red',
    'orange',
    'yellow',
    'green',
    'blue',
    'violet',
    'grey',
    'white']

# Colors because the default ones suck
COLORCODE = {
    "black":    "#000000",
    "brown":    "#a52a2a",
    "red":      "#ff0000",
    "orange":   "#ffa500",
    "yellow":   "#ffff00",
    "green":    "#008000",
    "blue":     "#0000ff",
    "violet":   "#800080",
    "grey":     "#808080",
    "white":    "#ffffff",
    "silver":   "#c0c0c0",
    "gold":     "#d4a017"}

# Multipliers in a dictionary, organized by value for ease of reading
MULTIPLIER = {
    1e-2:   'silver',
    1e-1:   'gold',
    1e0:    'black',
    1e1:    'brown',
    1e2:    'red',
    1e3:    'orange',
    1e4:    'yellow',
    1e5:    'green',
    1e6:    'blue',
    1e7:    'violet',}

# Converts a number to a list of resistor colors of equivalent value
def getColors(num):
    # To-be return value
    returnVals = []
    failReturnVals = [COLOR[0], COLOR[0], MULTIPLIER[1e0]]
    # Process the number
    indexPlaceholder = 0
    numStr = str(num)
    bandValues = ""

    # For anything smaller than 0
    if num < 1 and num >= 0.01:
        while indexPlaceholder < len(numStr):
            # Discard leading 0's and decimal
            if numStr[indexPlaceholder] == "0" or numStr[indexPlaceholder] == ".":
                indexPlaceholder += 1
            else:
                break

        # It's all zeroes, time to leave
        else:
            return failReturnVals

        bandValues = numStr[indexPlaceholder:]

        # Short a value, needs a black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        # Ready return color values
        before = True

        for value in bandValues:
            if len(returnVals) < 2:
                returnVals.append(COLOR[int(value)])
            elif value != "0":
                ret.append(COLOR[int(value)])
            else:
                break

    # And everything else
    elif num >= 1 and num <= 9990000000:
        indexPlaceholder = 2
        bandValues = numStr[:indexPlaceholder]

        # If there's 4 stripes
        if (num / float(bandValues)) % 10:
            indexPlaceholder = 3
            bandValues = numStr[:indexPlaceholder]

        # If there's a third band to add
        if len(numStr) > indexPlaceholder:
            if numStr[indexPlaceholder] != "0":
                bandValues = numStr[:indexPlaceholder + 1]

        # Needs another black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        for value in bandValues:
            if value == ".":
                continue

            returnVals.append(COLOR[int(value)])

    # Outside the limit
    else:
        return failReturnVals

    # Get the multiplier
    returnVals.append(MULTIPLIER[round(num / float(bandValues.replace(".", "")), 2)])

    return returnVals

#List of colors to Ohms
def getOhms(colors):
    bandValues = ""
    # Key storage
    multiply = 0

    # Get the index numbers of the colors
    while len(colors) > 1:
        bandValues = bandValues + str(COLOR.index(colors.pop(0)))

    # Find the key (multiplier) based on color (no easy way to do this)
    for key, value in MULTIPLIER.items():
        if value == colors[0]:
            multiply = key
            break

    # Color value * multiplier
    return float(bandValues) * multiply

def main():
    # Initialize app
    app = wx.App()
    frame = Window(None, "Resistor Calc")
    frame.display()
    app.MainLoop()

# The GUI class setup
class Window(wx.Frame):
    def __init__(self, parent, title):
        # Initialize the frame
        super(Window, self).__init__(
            parent,
            title=title,
            style=wx.DEFAULT_FRAME_STYLE,
            size=(550, 200))

        # UI setup
        self.uiInit()
        self.evtInit()
        self.colorSet()

        # Control variables
        self.lock = False
        self.prvs = self.ohmstxt.GetValue()

    def uiInit(self):
        # Third band list (needs optional added)
        self.r3colors = COLOR[:]
        self.r3colors.insert(0, "-")

        # Fourth band has some extra as well (organize by values)
        self.r4colors = COLOR[:-2]
        self.r4colors.insert(0, "gold")
        self.r4colors.insert(0, "silver")

        # Create panel and sub panels for colored boxes
        self.panel = wx.Panel(self)

        self.pr1 = wx.Panel(self.panel)
        self.pr2 = wx.Panel(self.panel)
        self.pr3 = wx.Panel(self.panel)
        self.pr4 = wx.Panel(self.panel)

        # Initialize font
        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(12)

        # Boxes for resizing and aligning
        # 3 tall
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        # First row
        lblohms = wx.StaticText(self.panel, label="Ohms:")
        lblohms.SetFont(font)

        self.ohmstxt = wx.TextCtrl(self.panel, value="0.0")

        # Second row
        lblcolor = wx.StaticText(self.panel, label="Colors:")
        lblcolor.SetFont(font)

        self.r1 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r2 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r3 = wx.ComboBox(
            self.panel,
            choices=self.r3colors,
            style=wx.CB_READONLY)

        self.r4 = wx.ComboBox(
            self.panel,
            choices=self.r4colors,
            style=wx.CB_READONLY)

        # Create first row
        hbox1.Add(lblohms, flag=wx.RIGHT, border=8)
        hbox1.Add(self.ohmstxt, proportion=1)

        # Create second row
        hbox2.AddMany([
            (lblcolor, wx.RIGHT, 8),
            (self.r1,),
            (self.r2,),
            (self.r3,),
            (self.r4,)])

        # Create third row
        hbox3.AddMany([
            (self.pr1, 1, wx.EXPAND),
            (self.pr2, 1, wx.EXPAND),
            (self.pr3, 1, wx.EXPAND),
            (self.pr4, 1, wx.EXPAND)])

        # Insert rows with padding
        self.vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox3, 1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))

        # Sets everything in place on the window
        self.panel.SetSizer(self.vbox)

        # Set combobox selections (not intialized by default
        self.r1.SetSelection(0)
        self.r2.SetSelection(0)
        self.r3.SetSelection(0)
        self.r4.SetSelection(2)

    # Initialize events
    def evtInit(self):
        self.Bind(wx.EVT_COMBOBOX, self.onCBChange)
        self.Bind(wx.EVT_TEXT, self.onTextChange)

    # Combobox was changed
    def onCBChange(self, e):
        self.lock = True

        bandColors = []
        thirdBand = self.r3.GetValue()

        bandColors.append(self.r1.GetValue())
        bandColors.append(self.r2.GetValue())

        # Third band is optional, check to see if it's actually used
        if thirdBand != "-":
            bandColors.append(thirdBand)

        bandColors.append(self.r4.GetValue())

        self.ohmstxt.SetValue(str(getOhms(bandColors)))
        self.colorSet()

    # Text change
    def onTextChange(self, e):
        ohms = self.ohmstxt.GetValue()

        # This is because event fires twice for some unknown reason
        if self.lock or ohms == self.prvs:
            self.prvs = ohms
            self.lock = False
            return None

        # If no value given, default to zero
        if not ohms:
            ohms = "0"

        try:
            bandColors = getColors(float(ohms))
            self.r1.SetSelection(COLOR.index(bandColors[0]))
            self.r2.SetSelection(COLOR.index(bandColors[1]))

            # All 4 bands are present
            if len(bandColors) > 3:
                self.r3.SetSelection(self.r3colors.index(bandColors[2]))
                self.r4.SetSelection(self.r4colors.index(bandColors[3]))
            # Third band is not present
            else:
                self.r3.SetSelection(0)
                self.r4.SetSelection(self.r4colors.index(bandColors[2]))

            self.colorSet()

        # Not a valid number or possible value
        except (ValueError, KeyError):
            pass

    # Display the colors for the bands
    def colorSet(self):
        self.pr1.SetBackgroundColour(COLORCODE[self.r1.GetValue()])
        self.pr2.SetBackgroundColour(COLORCODE[self.r2.GetValue()])

        # If band 3 isn't used, don't show it
        if self.r3.GetValue() == "-":
            self.pr3.SetBackgroundColour(wx.NullColour)
        else:
            self.pr3.SetBackgroundColour(COLORCODE[self.r3.GetValue()])

        self.pr4.SetBackgroundColour(COLORCODE[self.r4.GetValue()])

    def display(self):
        self.Centre
        self.Show()

if __name__ == '__main__':
    main()

Tuesday, April 2, 2013

Arduino Wii Nunchuck on Linux

I came across a DIY project over on the website for Maker magazine to connected a Wii Nunchuck to your computer as a mouse using an Arduino board and Python. Problem was, they only made it for Windows (they also suck at Python programming, but whatever). So after quite a bit of work over the past three days, I finally got it working.

The problem is Windows is not very secure and anything running on it can really do whatever it pleases, like say move the mouse around or click on things, without much effort. Linux has a bit more to it.

I went in originally planning on using libX11.so through ctypes.cdll, but setting up an event on that was not only pure hell, I don't even know if it is possible. Plan B presented itself as Xlib. The documents are not that complete, examples seem to be scarce and I don't have much in the way of patience.

You can check everything out here, I'm going to skim over the details and then show the Python code, all commented.

First thing first, get the library for interfacing the Nunchuck here. The readme there should tell you how to use it.

Next you want to jam some wires into your Nunchuck (the jumper wires from the starter kit fit fine without breaking anything) like so and connect it to your board to the corresponding holes.


From there you want to upload the example code for the Wii Nunchuck library so we can just process the data as a string rather than a bytestream.

Now we just need to run some Python code and you get all the mouse control I could jam into the thing (two buttons, joystick and accelerometer gave me just enough room for everything a standard modern day mouse has).

#!/usr/bin/env python
import serial, sys, Xlib, Xlib.display, Xlib.ext.xtest

# This will be later on, setting it now
global d, root

# The port to which your Arduino board is connected
# Change for your system and board
port = '/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_64131383331351306291-if00'

# The baudrate of the Arduino program
baudrate = 19200

# Variables indicating the center position (no movement) of the controller
# Increase the amount of play if to sensitive, decrease if not sensitive enough
# Increase speed to cut it down, decrease to raise it (1 is the highest)
midAccelX   = 515 # Accelerometer X
midAccelY   = 590 # Accelerometer Y
midAccelZ   = 710 # Accelerometer Z (not used)
midAnalogY  = 134 # Analog Y
midAnalogX  = 127 # Analog X
tollerance  = 5   # Amount of play
accelToll   = 80  # Amout of accelerometer play
speed       = 4   # difference / speed = movement

# Generic mouse movement
def move_mouse(x,y):
    x = int((x - midAnalogX) / speed)
    y = int((midAnalogY - y) / speed)

    # Make sure it's not just white noise
    if abs(x) < tollerance:
        x = 0
    # Move in the right direction the right amount
    else:
        if x < 0:
            x += tollerance
        else:
            x -= tollerance

    # Same deal, filtering white noise
    if abs(y) < tollerance:
        y = 0
    # Move in the right direction the right amount
    else:
        if y < 0:
            y += tollerance
        else:
            y -= tollerance

    # Save the Xorg processing if we're not moving
    if x != 0 or y != 0:
        d.warp_pointer(x, y, src_window = root)
        d.flush()

# Simulated button down
def mouse_down(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonPress, button)
    d.sync()

#simulated button up
def mouse_up(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonRelease, button)
    d.sync()

def main():
    # Connect to the serial port
    try:
        ser = serial.Serial(port, baudrate)
    except Exception as err:
        sys.stderr.write(str(err) + "\n")
        return 0

    # Mouse buttons
    leftDown = False
    rightDown = False
    middleDown = False

    # While the serial port is open
    while ser.isOpen():
        # Read and process the line
        line = ser.readline()
        line = line.strip('\r\n')
        line = line.split(' ')

        # Not sane yet, nothing to see here
        if len(line) != 7:
            continue

        # Alias the info so we can make sense of the rest
        try:
            analogX = int(line[0])
            analogY = int(line[1])
            accelX  = int(line[2])
            accelY  = int(line[3])
            accelZ  = int(line[4])
            zButton = int(line[5])
            cButton = int(line[6])

        # It hiccuped, ignore and move on
        except ValueError:
            continue

        # Left Mouse Button (Z)
        # Button down, don't need to press again if it's already down
        if zButton and not leftDown:
            leftDown = True
            mouse_down(Xlib.X.Button1)
        # Release the button
        elif leftDown and not zButton:
            leftDown = False
            mouse_up(Xlib.X.Button1)

        # Gestures to map more buttons since we only have two
        # It's the C button
        if cButton:
            # Tilt right, right click
            if (not rightDown) and accelX - midAccelX > accelToll:
                rightDown = True
                mouse_down(Xlib.X.Button3)
            elif rightDown and accelX - midAccelX <= accelToll:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            # Tilt left, middle click
            if (not middleDown) and midAccelX - accelX > accelToll:
                middleDown = True
                mouse_down(Xlib.X.Button2)
            elif rightDown and accelX - midAccelX <= accelToll:
                middleDown = False
                mouse_up(Xlib.X.Button2)

            # Tilt forward, scroll up
            if accelY - midAccelY > accelToll:
                mouse_down(Xlib.X.Button4)
                mouse_up(Xlib.X.Button4)
            # Tilt back, scroll down
            elif midAccelY - accelY > accelToll:
                mouse_down(Xlib.X.Button5)
                mouse_up(Xlib.X.Button5)
        # Cleanup any buttons down when gesture stops
        else:
            if rightDown:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            if middleDown:
                middleDown = False
                mouse_up(Xlib.X.Button2)

        # Move the mouse
        move_mouse(analogX, analogY)

    # After the program is over, close the serial port connection
    ser.close()

if __name__ == '__main__':
    # Create display and get the root
    d = Xlib.display.Display(None)
    root = d.screen().root
    try:
        main()

    except KeyboardInterrupt:
        print ""

    except Exception as err:
        sys.stderr.write(str(err) + "\n")

    # Regardless of what happens, close the display
    finally:
        d.close()

Remember to change your port for your board and system and adjust any values to adjust to your sensitivity and any excess give or lack there of that it may have.

Wednesday, February 20, 2013

Fibonacci Generator

I was reading up on various types of morphism recently. While I was disappointed in the examples, as they were showing off bad practice, even if they were just an example, I got an idea. While I do have a post showing off a similar bad example with calculating a Fibonacci number, I have since refined it into a faster and more efficient method and an explanation to it you can find here.

My main focus of interest was looking at catamorphism and anamorphism. In the process of reading up, I came across other types of morphisms that while were mathematically interesting, I only saw bad programming being shown emerging from the ideas.

To explain in the simplest way possible, catamorphism is a generator and the opposite of anamorphism, which is a fold. Generators are functions that produce data on-call, using the generated data to provide more data upon the next call. This can create an infinite data type or have an end. Counting can be catamorphic in nature. For the explanation, let's look at a Python generator.

def counting(x):
    while 1:
        yield x
        x += 1

This is the equivalent to this Haskell code.

counting x = [x..]

The Python code has a bit of a more robust description to it, but the idea is the same for both, they will count from x on up until forever. Anamorphism includes things like the sum function, a list of stuff goes in, and it's all folded together with a function (in the case of sum, adding) until one result remains.

Now to the cool part and what this all means. This all means that with the magic of generators, there is an easy way to create a fibonacci generator. This is a simple one-liner in Haskell and as far as I can tell, can outdo the crappy version of the recursive Fibonacci shown everywhere else.

fibgen a b = a : (fibgen b (a + b))

That's all there is to it. The idea is you supply the first two numbers and it does the rest, forever and ever. So if you want the numbers from it, just take them. Now I suppose I can be nice and give something similar in Python, although to be honest it will look pretty much like the counting function I made.

def fibgen(a, b):
    while 1:
        yield a
        a, b = b, a + b

With only some very small tweaking, I turned the counting function into a Fibonacci generator. Now, like magic, we have an easy way to cycle through a sequence of Fibonacci numbers. I want to try to work on various problems more in this manor for when list processing or other related activities that require sequential computations, I feel it simplifies the process and gives a more elegant solution (or if nothing else, it looks cool).

Friday, July 27, 2012

Technical Speak (some thoughts and advice)

This post is not going to be technical so much as I really want to write about some tech things people say that are either completely wrong or just down right annoying. So take this how you will, but this is an opinion piece.

Something I once did before I knew much about computers but now that drives me crazy when I hear it, is when people talk about Java and call it Javascript or visa versa. Let us get a few things straight. Java is not related to Javascript nor the other way around. Javascript is a scripting language based on ECMAScript whereas Java is an interpreted language developed by Sun Microsystems. While Java can be used as a web technology, it is not the same as Javascript and while they have some similar naming conventions, any real depth into some code would reveal they are very dissimilar. So stop mixing them up, it is frustrating.

Another irritating point for me was created by the media, "Mac vs PC." The real goal of this is Mac OS vs Windows. Mac computers are PC's, as PC stands for personal computer. Furthermore, Mac computers can run Windows and if you're crafty, you can run a Mac OS on another PC, virtual machines being the easiest to get things going. Also some people throw Linux into the list. Well, OS X is Unix based as well. So they have some similar things to them. As to which is better, all three of them suck. It is just a matter of picking which one sucks less at what you personally need to accomplish. None of them are good at everything no matter what they claim.

Overuse of acronyms. In the technical world, many acronyms exist. Many acronyms overlap and it depends on what you are talking about. Overuse of acronyms in situations where it is not necessary is annoying and even a tech geek can get lost in such conversations. Granted there are exceptions where some technologies are explicitly referred to their acronym based name because some of the names are long and annoying. Some examples are XML (eXtensible Markup Language), HTML (HyperText Markup Language), or even php (Personal Home Page/Hypertext Preprocessor [it has changed over time]). Choosing to use all those acronyms in an environment outside of a situation that deals directly with such things is annoying, confusing and not many people can follow clearly. Use words.

Being able to navigate a website does not make you tech savvy, same goes for navigating software. I am relatively tech savvy and I get lost on so many websites and various user interfaces. One annoying example is of Microsoft Office. They keep changing the interface, and while I know what I want to do, I can spend a lot of time looking for various options. These things are esoteric and not standardized. While many try to keep similar conventions in an attempt to create better ease of use, they do not have to. Reliance on people trying to make similar and familiar interfaces is not possible.

Do not use tech terms you do not understand. If the media is to teach us anything, let it be this. For example the famous CIS line, "I'll make a GUI interface in visual basic, see if I can track an IP address" (GUI pronounced "gooey"). Taking this I would have to respond, okay, I will make an OS in Piet, see if I can make a super computer. Despite the lack of need to sum up why this is dumb, I am going to do it anyway. A GUI is not necessary for any of this. A good deal of tools for sniffing are done in command line interfaces because there is no point in the extra computational time and effort going to making a pretty window. Visual Basic is a dumb choice. I don't even know if you could make a sniffer from VB. If you can, it is probably some random .NET feature that is a waste of time. Now you do not "track" an ip, you can trace a route. Tracing would be following something along its path. That is a small thing to pick at, but tracking an ip would be like sniffing just that ip. Tracing would be finding the origin.

Using tech terms from one thing for a separate case.When drawing a comparison it is fine. However, using that comparison like the cases are equal is usually not fine.

Comparing things on levels that they are not comparable. I am sure at some point you have come across things like Perl vs Python, where they argue about dumb things like which is easier to read or application. Seeing as how Perl was more so made for text processing and Python is more of a general purpose language, they are obviously not going to be good at the same things. Benchmarking comparisons are one thing, but arguing about readability or which way of doing things is better is dumb because it depends on the application and the person.

Skids. Script kiddies. People that claim to hack when they are using premade tools without knowledge of the underlying innerworks of how or why things work they way they do. Downloading things like custom firmware to break a game system or opening up a tool to do some network scanning. Some of these can be dangerous if you do not have some understanding of how they work. In this light, the use is not that of a tool but a black box effect. Someone puts something in and gets the desired result out without understanding what just happened. So not everyone that uses these tools are skids or something to that effect, some actually understand in general how they work and use a premade tool to build something else faster.

So those are just some thoughts and opinions based off of things I have heard people said that just really catch me the wrong way and I have seen them criticized for saying such things as well. An easy way to keep yourself from saying something that will annoy others, whether you are right or wrong, is to remember you do not need to prove anything to anyone what you know, because you already know you know it.

Sunday, July 22, 2012

IRC Bot (logbot)

After my first post on IRC bots, I decided to make a bot for logging a chat that was a bit better designed than the one I posted. It is far from perfect, however it will write formatted logs to a file for you, won't sit around if it is kicked and if it sees a message it cannot format, it will log the raw data. Also to keep in mind, it only joins a channel ofter the motd finishes with a certain number code. If there is no motd or it does not send the right number code it won't join. The reason for this is I just wrote the thing today and don't have the time to check all possibilities. So use it as a skeleton and develop on it further. Or ignore it and move on with your day. All descriptions will be commented in the code.

#!/usr/bin/env python
# Import all the packages we will be using
import logging
import socket
import ssl
import os
import sys
import re

# Some global variables for configuration
HOST = "irc.69megabytes.com"
PORT = 6697
NICK = "PyLogBot46"
CHANNEL = "#testchat"
SSL = True
FLOG = os.path.join(sys.path[0], "ircbot.log")
PWD = "supersecretpassword"
# This is for use later on to know when to join the channel and do it only once
notInChan = True

# Regex parsing of a standard IRC message
def parse(data):
    temp=re.match(r"^(:(?P<prefix>\S+) )?(?P<command>\S+)( (?!:)(?P<params>.+?))?( :(?P<trail>.+))?$", data)
    if temp:
        temp=temp.groupdict()
    return temp

# Where the magic happens
if __name__ == '__main__':
    # Set up the logging format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt="%Y-%d-%m (%H:%M:%S %Z)", filename=FLOG)
    # Message format strings so we can easily alter them
    format_privmsg = "{1} <{0}>: {2}"
    format_notice = "(notice) <{0}>: {1}"
    format_join = "{0} has entered {1}"
    format_nick = "{0} is now known as {1}"
    format_mode = "{0} set mode {1}"
    format_modes = "Mode set {0}"
    format_kick = "{0} kicked from {1} by {2} ({3})"
    format_srvmsg = "-!- {0} {1} {2}"
    try:
        # Create socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Set up SSL if necessary
        if SSL:
            s = ssl.wrap_socket(s)

        # Connect to the server
        s.connect((HOST, PORT))

        # Authenticate
        s.send("NICK " + NICK + "\r\n")
        s.send("USER " + NICK + " www.anarchy46.net PY :Python LogBot\r\n")

        # Main loop
        while 1:
            # Reset information received and processed
            data = ""
            info = {}
            # Retreive data 1 byte at a time, stop at newline
            while data.find("\n") == -1:
                data += s.recv(1)
            # Remove unnecessary whitespace
            data = data.strip()
            # Parse the raw data
            info = parse(data)
            # Privmsg handler
            if info['command'] == "PRIVMSG":
                logging.info(format_privmsg.format(info['prefix'], info['params'], info['trail']))
            # Notices
            elif info['command'] == "NOTICE":
                # If to self from someone
                if info['params'] == NICK:
                    # To quit properly
                    if info['trail'] == PWD:
                        s.send("QUIT :Done logging.\r\n")
                        break
                    # So we don't log the password
                    logging.info(format_notice.format(info['prefix'], info['trail']))
                # Server notices
                else:
                    logging.info(format_srvmsg.format(info['prefix'], info['params'], info['trail']))
            # Respond to server pings (no need to log)
            elif info['command'] == "PING":
                s.send("PONG " + info['trail'] + "\r\n")
            # Someone joins
            elif info['command'] == "JOIN":
                logging.info(format_join.format(info['prefix'], info['trail']))
            # Someone changes nick
            elif info['command'] == "NICK":
                logging.info(format_join.format(info['prefix'], info['trail']))
            # Modes
            elif info['command'] == "MODE":
                # Normal mode changes
                if info['trail']:
                    logging.info(format_modes.format(info['trail']))
                # Modes for self (set by server)
                else:
                    logging.info(format_mode.format(info['prefix'], info['params']))
            # Someone gets kicked
            elif info['command'] == "KICK":
                # Quit if it's the bot
                temp = info['params'].split(' ')
                if temp[1] == NICK:
                    logging.info("Kicked from channel.")
                    s.send("QUIT :Bye bye...\r\n")
                    break
                # Log everyone else being kicked :D
                else:
                    logging.info(format_kick.format(temp[0], temp[1], info['prefix'], info['trail'])
            # Special server messages
            elif info['command'].isdigit():
                if info['trail']:
                    logging.info(format_srvmsg.format(info['prefix'], info['command'], info['trail']))
                else:
                    logging.info(format_srvmsg.format(info['prefix'], info['command'], info['params']))
                # End of MOTD
                if notInChan and info['command'] == "376":
                    s.send("JOIN " + CHANNEL + "\r\n")
                    notInChan = False
            else:
                logging.info(data)
    # Abort!
    except socket.error as err:
        logging.critical("Socket error: " + str(err.errno))
        sys.exit(err.errno)

It's not the prettiest of code, but it works. When launching from the command line, make sure to add an ampersand (&) at the end so you don't need to wait for it or leave a window open with it running. Enjoy, and remember this thing only took me a couple of hours to write and sort out some formats.

Saturday, July 21, 2012

Making an IRC Bot

A common question I get from people I know is about making an IRC bot. The problem I run into with people trying to do this is they are usually very new to programming and half the time they are struggling with the basics of simply how to write a program. So before getting into more detail, there are some prerequisites you need to know first.
  • Understanding the programming language syntax you want to use
  • Basic programming
  • Networking knowledge of basic sockets (nothing in depth but more so how network communication should be modeled)
  • Knowledge of the IRC protocol raw (access to an RFC is very useful)

Now f you are still reading, I will assume you at the very least plan on doing the research before actually trying anything. For this, I will be writing it in Python as it is very simple and easy to do. So let us work through this piece by piece.

The first thing we want to do is run our imports so we can have all the modules we need to work with. Rather than using some third party IRC module, we will work with raw sockets because I can and it gives more insight as to what is going on. We need the module for sockets, and regular expressions. We will also import time and I'll get to why later.

import socket
impor re
import time

So now that we have the packages we need, now we need to create a socket object. With a socket object, we can establish a connection over TCP/IP, among other types of socket connections. For our purposes we will use IPv4 and a stream. For this we use the variables AF_INET and SOCK_STREAM. The AF in AF_INET stands for "address family." Now let's create the socket.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Now we have the socket object s. In another post I will explain how to put an SSL wrapper on it to use a secure connection. As for now, just a socket will be used. Now we need to connect to a server.

s.connect((HOST, PORT))

Here the connection info is passed as a tuple, so yes the parentheses are on purpose. In this, HOST will be a string of either the IP address or domain and PORT is the port number you will connect to. Now that the connection is established, we need to send identifying information for the IRC. To simplify things we will have the program sleep for a second as some servers do not let you authenticate immediately and instead send a signal for it. However I do not feel like writing out something to check for that signal right away.

time.sleep(1>)
s.send("NICK MyIRCbotNick\r\n")
s.send("USER MyIRCbotNick anarchy46.net PY :Python Bot\r\n")

Okay, so now that we've authenticated, we will sleep again then join a channel because yet again, cannot join right away.

time.sleep(1)
s.send("JOIN #somechannel\r\n")

Now we set up the loop to read the server data. Because this will go on indefinitely, we will use an infinite loop.

while 1:
    data = "" # Reset data each time
    while data.find("\n") == -1:
        data += s.recv(1) # Receives data 1 byte at a time
    data = data.strip() # No need for leading or trailing whitespace
    print data
    # Watch for and respond to server pings
    if re.search(r"^PING", data):
        s.send("PONG" + data[4:])

Now you have all the code needed to connect to an IRC server, join a channel and stay connected. A few things to keep in mind. The socket is blocking, so the bot will wait on the recv() until it gets some data. Without blocking the program would be too CPU intensive without some extra work. Also note that printing data will print raw IRC data, which is a lot different than the messages you see in a client, and will include control characters like char 1 and 3. Furthermore, there is no error handling or autojoining should the bot be kicked. This is simply a small and poorly made IRC bot that is to demonstrate what is needed to connect to an IRC and I do not recommend this design as a base for a larger scale bot that will be used more often. Sometime later I will divulge a more robust bot that has the purpose of logging an IRC channel or channels.

Friday, June 15, 2012

Python Lambda Intro (with simple example)

While really a simple thing to do, I wanted to post an example of a very simple program using something a little less common. Instead of the more common way of making a function, I will instead use lambda. In Python, lambda is used for passing an anonymous functions to a method or can be used to have a function generate simple functions. For the latter, decorators can be used for a more complex function generation, however I have yet to fully grasp using it, so I'll save explaining that for when I know how to practically use it.

Now anonymous functions are cool because you can either pass them to a function without creating some permanent function that will only be used in that one particular instance or you can pass it to a variable reference. Passing to variable reference is as simple as defining any variable. The difference in this particular instance is that lambda is limited to very simple functions, and returning variables is not as clear as a return statement. Let's view a quick example.

import sys

ftoc = lambda(f): (f-32.0)*(5.0/9.0)
ctof = lambda(c): (9.0/5.0)*c+32.0

while(1):
    try:
        print """Pick a conversion (EOF to end ctrl+z win/ctrl+c *nix)
1 - Fahrenheit to Celsius
2 - Celsius to Fahrenheit
3 - Exit

>""",
        choice = raw_input()

        if choice == 3:
            break;
        else:
            print "What is the tempurature?",
            temp = raw_input()
            if choice == "1":
                print str(ftoc(float(temp)))+" Celsius"
            elif choice == "2":
                print str(ctof(float(temp)))+" Fahrenheit"
            else:
                print "Unknown option"

    except KeyboardInterrupt:
        break;

    except Exception as e:
        print "An error occured: "+str(e)
print "Goodbye."
exit(0)


The main point to focus on is the 2 lines following the import statements. To break it down, ftoc = lambda(f): (f-32.0)*(5.0/9.0) is actually very simple. It starts out with a normal variable declaration, variable name being ctof. We then assign it to the value of the lambda statement. The word lambda in Python means an anonymous function, then the (x) is the arguments the function accepts. This does not need to be in parenthesis can could just as easily be lambda x:. I use the parentheses to more clearly show that it's not the function name, but arguments being passed to it, I find it to be more clear for myself reading it. The next part is just a formula for converting the temperature. The question may arise, where is the return value? Quite simply, it's whatever the code in the lambda statement evaluates to at the very end.

Now some caveats. Lambda functions cannot span multiple lines, it's only a 1 liner function (with the exception being if a docstring is used). It also supports nested scope, so if it is within a scope where it uses a variable name, it will use that variable, however if the variable is the name of an argument variable, that value will be used instead. Finally, semicolons are ending delimiters and cannot be used to include multiple lines of code, it is only a single statement.

One more little catch I ran into, while I doubt anyone has a need for it, trying to define a list of lambda functions through list comprehension will not work to make different functions. Using something like y = [lambda (x): x+z for z in range(10)] will result in all 10 list items having z as 9 because it will use z as a reference to z and not replace the value, therefore the last value of z is the one that is used. However, if you replace the lambda function with a function that returns lambda functions, it works, and looks like this.

def a(x):
    return lambda(y): x+y

y = [a(z) for z in range(10)]


I was also informed of an alternative way to do this through default values, which looks a little uglier, but can condense things if you really want to. You can do something like y = [(lambda x, z=z: x+z) for z in range(10)], which will place the value of z as the default argument. Note however, that since it is a default argument, it can then be changed, which does not seem like a clean solution.

Tuesday, January 24, 2012

IRC message Regex

So I'm in the process of making an irc bot, and one of the problems I always seem to have is parsing the message. If you don't know what parsing means, the simplest explanation is changing the message, in this case breaking it down into some form of an object that organizes the data for it.

Normally I would split it and then check various information from it being split then check all the split parameters and try to organize it, however when searching recently I found an easier way. Now keep in mind, this is client side parsing. I found the original on Caleb Delnay, so original credit goes here, but I wanted to expand upon it and convert it to work with the other subtle changes in regex across other languages.

*** There were further improvements added by someone else who I found thanks to some referrals in the stats thanks to a lovely accreditation to this post. Check out more info at mybuddymichael.com. I'll try to integrate the changes into my post once I can take some time to give it a good lookover and try it out a bit (hopefully it will help out with a new bot I'm currently working on in Haskell).

The original .NET compatible regex is
^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$

Python (place in an r"" string so you won't need to escape backslashes), Perl, PHP, and AS3:
^(:(?P<prefix>\S+) )?(?P<command>\S+)( (?!:)(?P<params>.+?))?( :(?P<trail>.+))?$

Java (before 7 didn't support named groups, want to look at groups 2, 3, 5 and 7):
^(:(\\S+) )?(\\S+)( (?!:)(.+?))?( :(.+))?$

Java (7 and up supports named groups, have not tried this yet):
^(:(?<prefix>\\S+) )?(?<command>\\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$

JavaScript (no named grouping, use groups 2, 3, 5, and 7, does not need to be in a string):
/^(:(\S+) )?(\S+)( (?!:)(.+?))?( :(.+))?$/

The basic premise is under the assumption messages are formatted along the lines of :<prefix> <command> <params> :<trailing>, where any values are optional. If you know a better way to do any of them are know ways in languages I left out, let me know. As far as the regex methods and ways to work it out, that is up to you, I am just supplying the pattern and it is up to you so use it correctly.

Since regex can be complicated, hopefully this saves everyone some time, figuring out the needed methods to use it shouldn't be too hard.



***Edit:
After a comment about some stuff in the RFC, I played around with trying to make the regex work with that specification, I came up with a partially working version. Due to the complexity, my lack of knowledge and lack of benefit from this, I will only post the one edit I made and hopefully not bother with this again. While this is good for something quick and dirty, string methods seem to be more practical.

^(:(?P<prefix>\S+) )?(?P<command>\S+)( (?!:)(?P<params>\S{14} (:)?|.+ :?))?((?P<trail>.+))?$

The params section will end up with either a trailing space or a space and colon. That's the best I could do, and the last I'll do of this.