Saturday, October 11, 2008

Conky: Display Last Pidgin Message

Recently, I removed the gnome-panels, added awn, and had a conky bar at the top. However, I missed having a system notification of when I would receive a message via Pidgin. So I browsed the pidgin wiki and create somewhat of a hack to display the last IM that received from Pidgin. This method is probably not the best way however I had some diffuculty.

The DBus method that listens for the received messages was chocking conky and not allowing the conky script to load correctly. So I had to create two files. getim.py would run at startup, listen for a new message and then write that message to a data file. lastim.py would be executed within conky and check the data file for the new message. I know its not real efficient and redundant but it just works. If you have any brilliant ideas please let me know.

Here is the getim.py file:
#!/usr/bin/python

# ----- GETIM.PY // LISTEN FOR AN RECEIVED MESSAGE ------------#
################################################################
# MOST OF THIS SCRIPT WAS TAKEN FROM THE PIDGIN DEVELOPER WIKI#
# PLEASE VISIT http://developer.pidgin.im/ FOR MORE INFORMATION#
################################################################

def conky_im(account, sender, message, conversation, flags):
reg = '<(.|\n)+?>' # REGEX FOR HTML TAGS
message = re.sub(reg,'',message) # REMOVE HTML TAGS
sender = sender.split("@")[0] # GET GCHAT NAME
message = message[0:24] # LIMIT TO 25 CHARS
file = ' ' # LOCATION OF DATA FILE
fim = open(file,"w")
IM = sender + " said: " + message
fim.write(IM)
fim.close()

import dbus, gobject, re
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()

bus.add_signal_receiver(conky_im,
dbus_interface="im.pidgin.purple.PurpleInterface",
signal_name="ReceivedImMsg")

loop = gobject.MainLoop()
loop.run()



Copy and paste the following and save as lastim.py:
#!/usr/bin/python

# SHOW LAST IM
f=" " # REPLACE WITH YOUR DATA FILE. ie.CONKYIM.DAT
fim = open(f,'r')
im = fim.read()
print im
fim.close


Finally, add this to your .conkyrc script
${execi 1 python ~/scripts/lastim.py}

Any questions/comments let me know. Here is a screen shot of what my conky looks like:

2 comments:

  1. good idea - and it works...

    I've edited one line in getim.py (line 16: ' IM = sender') and now i can see on my conky when the last message has been recived and who sent it:

    Conky:

    Last Message from:${alignr}${execpi 1 python ~/scripts/conky/lastim.py} (${execi 1 ls -lta | grep .pidgin.log | awk '{print $7}'})

    but i've got 2 issues:

    1. if the last message is form an icq-buddy there will only shown his/her UIN and not the nickname/alias...

    2. I don't have any idea how to show the number of unread messages in pidgin... maybe you got an idea?

    ReplyDelete
  2. To retrive last IM content is easier if you use tail command

    ${tail PATHTOYOURLOGFILE}

    So you don't need second python script

    ReplyDelete