/* MusicInfo plugin for gaim Displays whatever is currently playing in winamp in the user's profile in Gaim Written by Reuben Balik Copyright October 2005 By Reuben Balik Last Update: October 5, 2005 */ #include "internal.h" #include "connection.h" #include "pluginpref.h" #include "prpl.h" #include "signals.h" #include "version.h" #include #include static const char default_replace[] = "%mi%"; static char replace_text[512]; static const int maxLen = 2048; static const int interval = 30 * 1000; static char message[512]; static const char before[] = "I am listening to "; static const char between[] = " by "; static const char after[] = "."; static int timeoutint = 0; static gboolean getSong(char* title, char* artist) { HWND hwndWinamp = FindWindow("Winamp v1.x",NULL); char this_title[512]; int ret = GetWindowText(hwndWinamp,this_title,sizeof(this_title)); if(ret != 0) { if(strstr(this_title, "[Stopped]") != NULL) return FALSE; if(strstr(this_title, "[Paused]") != NULL) return FALSE; char temp[512]; char *firsthyphen, *secondhyphen, *period, *ptr; firsthyphen = strchr(this_title, '-'); if(firsthyphen == NULL) //when winamp starts up without playing it doesn't display the song name return FALSE; secondhyphen = strrchr(this_title, '-'); period = strchr(this_title, '.'); ptr = period+2; int count = 0; if(firsthyphen != secondhyphen) //there's two hyphens so we have an artist { while(ptr != firsthyphen) { temp[count] = *ptr; ptr++; count++; } temp[count-1] = '\0'; ptr+=2; strcpy(artist, temp); } else //no artist { strcpy(artist, "Unknown"); } count=0; while(ptr != secondhyphen) { temp[count] = *ptr; ptr++; count++; } temp[count-1] = '\0'; strcpy(title, temp); return TRUE; } else { return FALSE; } } static void updateInfo(GaimConnection *gc, gboolean forceMessage, gboolean updateServer) { GaimAccount* account; account = gaim_connection_get_account(gc); char curAcct[maxLen]; char newAcct[maxLen]; if(gaim_account_get_user_info(account) != NULL) strcpy(curAcct, gaim_account_get_user_info(account)); char title[256]; char artist[256]; char construct[512]; if(!forceMessage) { if(getSong(title, artist)) //getSong returns true if we're actually listening to something { strcat(construct, before); strcat(construct, title); strcat(construct, between); strcat(construct, artist); strcat(construct, after); } else { strcpy(construct, "I am not currently listening to any music."); } } char* pos = strstr(curAcct, replace_text); char* cur = curAcct; char* newcur = newAcct; int i; if(pos != NULL && strcmp(construct, message) != 0) { if(!forceMessage) strcpy(message, construct); //copy the part before what is being replaced while(cur != pos) { *newcur = *cur; newcur++; cur++; } //copy in the message for(i = 0; i < strlen(message); i++) { *newcur = message[i]; newcur++; } //copy everything after the replace_text //first advance the pointer for(i = 0; i < strlen(replace_text); i++) { cur++; } //then copy i = strlen(newAcct); while(*cur != '\0' && i < maxLen) { *newcur = *cur; newcur++; cur++; i++; } *newcur = '\0'; gaim_account_set_user_info(account, newAcct); if(updateServer) serv_set_info(gc, newAcct); strcpy(replace_text, message); } } static gboolean callUpdateInfo(gpointer gc) { updateInfo((GaimConnection *)gc, FALSE, TRUE); return TRUE; } static void online(GaimConnection *gc) { strcpy(replace_text, default_replace); updateInfo(gc, FALSE, TRUE); timeoutint = g_timeout_add(interval, callUpdateInfo, (gpointer)gc); } static void signoff(GaimConnection *gc) { g_source_remove(timeoutint); strcpy(message, default_replace); updateInfo(gc, TRUE, FALSE); strcpy(replace_text, default_replace); } static gboolean plugin_load(GaimPlugin *plugin) { gaim_signal_connect(gaim_connections_get_handle(), "signed-on", plugin, GAIM_CALLBACK(online), NULL); gaim_signal_connect(gaim_connections_get_handle(), "signing-off", plugin, GAIM_CALLBACK(signoff), NULL); return TRUE; } static gboolean plugin_unload(GaimPlugin *plugin) { gaim_signal_disconnect(gaim_connections_get_handle(), "signed-on", plugin, GAIM_CALLBACK(online)); gaim_signal_disconnect(gaim_connections_get_handle(), "signing-off", plugin, GAIM_CALLBACK(signoff)); return TRUE; } static GaimPluginInfo info = { GAIM_PLUGIN_MAGIC, GAIM_MAJOR_VERSION, GAIM_MINOR_VERSION, GAIM_PLUGIN_STANDARD, /**< type */ NULL, /**< ui_requirement */ 0, /**< flags */ NULL, /**< dependencies */ GAIM_PRIORITY_DEFAULT, /**< priority */ "core-rbalik-musicinfo", /**< id */ N_("MusicInfo"), /**< name */ "0.1", /**< version */ /** summary */ N_("Displays the current song in Winamp in your user info"), /** description */ N_("Displays the current song in Winamp in your user info"), "Reuben Balik ", /**< author */ "http://www.ews.uiuc.edu/~rbalik2/musicinfo/", /**< homepage */ plugin_load, /**< load */ plugin_unload, /**< unload */ NULL, /**< destroy */ NULL, /**< ui_info */ NULL, /**< extra_info */ NULL, /**< prefs_info */ NULL }; static void init_plugin(GaimPlugin *plugin) { } GAIM_INIT_PLUGIN(mig, init_plugin, info)