XChat physical notify

As I said before, I wanted to hook up my Arduino to work with XChat. I wrote a plug-in to raise a flag attached to my computer monitor when my nick is mentioned in the channel.

This is very simple and requires not a whole lot of code. So I am just going to post the code and tell only a little about it.

Arduino:

#include <Servo.h>

Servo leflag;
int pos = 0;
int buffbyte = 0;

void setup()
{
   Serial.begin(9600);
    leflag.attach(11);
    leflag.write(0);
}

void loop()
{
    //If we have something to read, then read it
    if (Serial.available() > 0)
    {
       buffbyte = Serial.read();
       Serial.println(buffbyte);
       //If ascii A, move servo
       if (buffbyte == 65)
       {
           leflag.write(0);
       }
       //If ascii B, move servo
       else if (buffbyte == 66) //ascii B
       {
           leflag.write(90);
       }
    }
}

And the XChat plugin to communicate with the Arduino. Your serial port my be different, use the Arduino IDE or the shell command “dmesg | grep tty” to see which one the Arduino is on:

import xchat, serial, os, time

__module_name__ = "ArduNotify"
__module_version__ = "1.0"
__module_description__ = "Turns a servo to let you know when somone said your name in channle. Uses Arduino."

s = serial.Serial("/dev/ttyUSB0")

#Check if file exists, if not, create it
#We use checkFile to ensure we get no errors incase we remove le logs
def checkFile():
    try:
        f = open("said.txt", "r")
        f.close()
    except:
        f = open("said.txt", "w")
        f.close()

def checkForName(words, words_eol, userdata):
    if "techb" in words[1].lower() or "techb_away" in words[1].lower():
        s.write("A")
        checkFile()
        f = open("said.txt", "a")
        f.write("%s %s" % (time.strftime("%I:%M", time.localtime()), words_eol[0][0:-1]+'\n\n'))
        f.close()

def acceptNotify(words, words_eol, userdata):
    s.write("B")
    return xchat.EAT_ALL

def checkNotify(words, words_eol, userdata):
    checkFile()
    os.system("gedit said.txt")
    return xchat.EAT_ALL

def rmNotify(words, words_eol, userdata):
    checkFile()
    os.system("rm said.txt")
    return xchat.EAT_ALL

def unloadHandler(userdata):
    #try close the serial port handle when this script gets unloaded
    #not really needed, but good for cleaning up and testing
    s.close()

xchat.hook_print("Channel Message", checkForName)
xchat.hook_print("Channel Msg Hilight", checkForName)
xchat.hook_unload(unloadHandler)
xchat.hook_command("gotit", acceptNotify)
xchat.hook_command("check", checkNotify)
xchat.hook_command("rm", rmNotify)

The Arduino code is self explanatory, so I wont go over it.

The plugin on the other hand has more going on with it. We have a function to check if there is a log file. If not, we create one. We do this check because there is a command I built-in to remove the log file.

The next function is to check if our nick was mentioned. We attach it to the channel highlight and channel message hooks because I use two different names, one for away and one for when I’m active. We want to check for both. If either name is mentioned, we log the message along with the time it was said, and send ASCII “A” to the Arduino to raise the flag.

The next function will lower the flag.

To check the log file, we simply use Python’s built-in module os and then send a shell command for Gedit to open the log file.

Next, the remove function does just like the check command, but removes the log file from the system. Again using the os module.

The last function is to handle an unload. If the plugin is unloaded, we will want to close the handle to the serial port. This isn’t ‘really’ needed, but is good practice incase XChat crashes or something.

Here is a bad quality video of the setup. Sorry for the bad quality, I have no money for a real cam, so I have to use my phone.