Hallo erstmal, ich bin neu hier im Forum!
Ich programmiere seit Jahren in VB6, PHP/MYSQL, seit ca 2 Jahren mit VB.Net wobei ich eigl auf Windows Mobile spezialisiert bin. Etwas C hab ich eigl relativ wenig gemacht, schon garnicht ohne Laufzeitdebugging
vor Wochen habe ich mir ein Pidgin (Messenger: http://www.pidgin.im) geschrieben um die letzten Nachrichten, die letzten User die On kommen in eine Textdatei auszugeben, bzw ob ungelesene Nachrichten vorhanden sind. Die dateien nutze ich um alles in meiner Sidebar anzuzeigen.
Leider muss ich die Plugins unter Cygwin mit gcc("make"???). Ich schreibe alles im texteditor.
Leider habe ich kaum erfahrungen in C.
Mein Plugin stürzt regelmäßig ab, immer wenn ich mich ab und dann wieder abmelde mit Pidgin. Compilieren konnte ich ohne Probleme
Wäre nett wenn jemand über meinen Source lesen könnte und mir sagen könnte wo die Fehler liegen. Tipps für bestimmte Probleme die ich sicher amateurhaft gelöst habe wären auch nett. (2 Varieblen Kombienieren z.B.)
Vielen DANK!
ps: hier das C TUT http://developer.pidgin.im/wiki/CHowTo/BasicPluginHowto
Ich programmiere seit Jahren in VB6, PHP/MYSQL, seit ca 2 Jahren mit VB.Net wobei ich eigl auf Windows Mobile spezialisiert bin. Etwas C hab ich eigl relativ wenig gemacht, schon garnicht ohne Laufzeitdebugging
vor Wochen habe ich mir ein Pidgin (Messenger: http://www.pidgin.im) geschrieben um die letzten Nachrichten, die letzten User die On kommen in eine Textdatei auszugeben, bzw ob ungelesene Nachrichten vorhanden sind. Die dateien nutze ich um alles in meiner Sidebar anzuzeigen.
Leider muss ich die Plugins unter Cygwin mit gcc("make"???). Ich schreibe alles im texteditor.
Leider habe ich kaum erfahrungen in C.
Mein Plugin stürzt regelmäßig ab, immer wenn ich mich ab und dann wieder abmelde mit Pidgin. Compilieren konnte ich ohne Probleme
Wäre nett wenn jemand über meinen Source lesen könnte und mir sagen könnte wo die Fehler liegen. Tipps für bestimmte Probleme die ich sicher amateurhaft gelöst habe wären auch nett. (2 Varieblen Kombienieren z.B.)
Vielen DANK!
ps: hier das C TUT http://developer.pidgin.im/wiki/CHowTo/BasicPluginHowto
Code:
/*
* Exporter Plugin
*
* Copyright (C) 2004, Gary Kramlich <grim@guifications.org>,
* 2007, John Bailey <rekkanoryo@cpw.pidgin.im>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02111-1301, USA.
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* config.h may define PURPLE_PLUGINS; protect the definition here so that we
* don't get complaints about redefinition when it's not necessary. */
#ifndef PURPLE_PLUGINS
# define PURPLE_PLUGINS
#endif
#include <glib.h>
/* This will prevent compiler errors in some instances and is better explained in the
* how-to documents on the wiki */
#ifndef G_GNUC_NULL_TERMINATED
# if __GNUC__ >= 4
# define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
# else
# define G_GNUC_NULL_TERMINATED
# endif
#endif
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <notify.h>
#include <plugin.h>
#include <version.h>
#include <account.h>
#include "internal.h"
#include "buddyicon.h"
#include "cipher.h"
#include "connection.h"
#include "conversation.h"
#include "core.h"
#include "debug.h"
#include "ft.h"
#include "signals.h"
#include "version.h"
#include "status.h"
#include "sound.h"
#include "gtkconv.h"
static char *de_tag(char *string);
static gchar * displayName (PurpleBuddy *buddy);
PurplePlugin *exporter_plugin = NULL;
static char *de_tag(char *str)
{
// many thanks to Martin for this nice piece of code
// de_tag removes all html/xml from a string
// used for stripping down styled messages
char *p, *q;
while( (p=strchr(str, '<')) && (q=strchr(p, '>')) )
{
memmove(p, q+1, strlen(q));
}
return str;
}
static char *PidginPath(char *filename)
{
char path[4096];
/*
strcpy(path, purple_user_dir());
strcat(path, G_DIR_SEPARATOR_S);
strcat(path, filename);
*/
//sprintf(filename, "%s" G_DIR_SEPARATOR_S "pidglet-cmd", purple_user_dir());
g_snprintf(path, sizeof(path), "%s%s%s", purple_user_dir(), G_DIR_SEPARATOR_S, filename);
return path;
}
static gchar * displayName(PurpleBuddy *buddy)
{
if (buddy->alias) {
return buddy->alias;
} else if (buddy->server_alias) {
return buddy->server_alias;
} else if (buddy->name) {
return buddy->name;
} else {
return 'empty';
}
}
/* we're adding this here and assigning it in plugin_load because we need
* a valid plugin handle for our call to purple_notify_message() in the
* plugin_action_test_cb() callback function */
/* we tell libpurple in the PurplePluginInfo struct to call this function to
* get a list of plugin actions to use for the plugin. This function gives
* libpurple that list of actions. */
static GList *
plugin_actions (PurplePlugin * plugin, gpointer context)
{
/* some C89 (a.k.a. ANSI C) compilers will warn if any variable declaration
* includes an initilization that calls a function. To avoid that, we
* generally initialize our variables first with constant values like NULL
* or 0 and assign to them with function calls later */
GList *list = NULL;
PurplePluginAction *action = NULL;
/* libpurple requires a GList of plugin actions, even if there is only one
* action in the list. We append the action to a GList here. */
list = g_list_append (list, action);
/* Once the list is complete, we send it to libpurple. */
return list;
}
static void
buddy_signed_on_cb(PurpleBuddy *buddy, void *data)
{
char oldText[100];
char oldTextFixed[100];
bool duplicate=FALSE;
char newText[400];
char currentUsername[100];
if (buddy != NULL)
{
FILE *fileR;
fileR=fopen(PidginPath("LastLogons.txt"),"a+");
if (!fileR)
{
purple_debug_misc("Exporter Debugger", "Read Error in buddy_signed_on_cb");
return;
}
strcpy(newText, "");
strcpy(currentUsername, displayName(buddy));
int x=0;
while (x<3)
{
fgets(oldText, sizeof(oldText), fileR);
strcpy(oldTextFixed, oldText);
char *newline = strchr(oldTextFixed, 'n'); // check for trailing 'n'
if (newline)
{
*newline = '0'; // overwrite the 'n' with a terminating null
}
if (currentUsername[0] == oldTextFixed[0] && currentUsername[1] == oldTextFixed[1] && strlen(currentUsername) == strlen(oldTextFixed))
{
duplicate = TRUE;
int y=0;
while (y<strlen(currentUsername) && duplicate == TRUE) {
if (currentUsername[y] == oldTextFixed[y])
{
//duplicate = TRUE;
}
else
{
duplicate = FALSE;
}
y++;
}
}
purple_debug_misc("Exporter Debugger", "-Buddyname:"%s"- oldText:"%s"-n", currentUsername, oldTextFixed);
strcat(newText, oldText);
x++;
}
fclose(fileR);
if (duplicate == FALSE)
{
FILE *fileW;
fileW=fopen(PidginPath("LastLogons.txt"),"w+");
if (!fileW)
{
purple_debug_misc("Exporter Debugger", "Write Error in buddy_signed_on_cb");
return;
}
fprintf(fileW,"%s",displayName(buddy));
fprintf(fileW,"%s","n");
fprintf(fileW,"%s",newText);
fclose(fileW);
}
else
{
purple_debug_misc("Exporter Debugger", "Duplicate FOUND!");
}
}
}
GList *get_pending_list(guint max) {
GList *l_im = NULL;
l_im = pidgin_conversations_find_unseen_list(PURPLE_CONV_TYPE_IM, PIDGIN_UNSEEN_TEXT, FALSE, max);
//l_im = purple_get_ims;
if (l_im != NULL)
{
return l_im;
}
}
static void
NewMessage(gboolean state)
{
FILE *fileW;
fileW = fopen(PidginPath("NewMessage.txt"),"w+");
if (!fileW)
{
purple_debug_misc("Exporter Debugger", "Write Error in NewMessage");
return;
}
if(state)
{
fprintf(fileW,"%s","1");
//fputs("1", fileW);
}
else
{
fprintf(fileW,"%s","0");
//fputs("0", fileW);
}
fclose(fileW); /*done!*/
}
static void
conversation_updated_cb(PurpleConversation *conv, PurpleConvUpdateType type)
{
GList *list = NULL;
if(type == PURPLE_CONV_UPDATE_UNSEEN) {
list=get_pending_list(1);
if(list == NULL)
{
NewMessage(FALSE);
}
else if(list != NULL)
{
NewMessage(TRUE);
g_list_free(list);
}
}
}
static void
received_im_msg_cb(PurpleAccount *account, char *sender, char *buffer,
PurpleConversation *conv, PurpleMessageFlags flags, void *data)
{
char oldText[10000];
char newText[1000];
char myUsername[10000];
char *message=strdup(buffer);
struct tm *newtime;
time_t long_time;
time( &long_time );
newtime = localtime( &long_time );
//newtime->tm_hour, newtime->tm_min, newtime->tm_sec
de_tag(message);
FILE *fileR;
fileR=fopen(PidginPath("LastMessage.txt"),"a+");
if (!fileR)
{
purple_debug_misc("Exporter Debugger", "Read Error in received_im_msg_cb");
return;
}
char line [1000]; /* or other suitable maximum line size */
strcpy(oldText, "");
while (fgets(line, sizeof(line), fileR) != NULL) /* read a line */
{
strcat(oldText, line);
}
fclose(fileR); /*done!*/
int x=0;
while (x<1000)
{
newText[x] = oldText[x];
x++;
}
FILE *fileW;
fileW=fopen(PidginPath("LastMessage.txt"),"w+");
if (!fileW)
{
purple_debug_misc("Exporter Debugger", "Write Error in received_im_msg_cb");
return;
}
strcpy(myUsername, purple_conversation_get_title(conv));
if (myUsername == NULL)
{
strcpy(myUsername, '(null)');
}
if (newtime->tm_hour < 10)
{
fprintf(fileW,"0%d", newtime->tm_hour);
}
else
{
fprintf(fileW,"%d", newtime->tm_hour);
}
fprintf(fileW,":", newtime->tm_hour);
if (newtime->tm_min < 10)
{
fprintf(fileW,"0%d", newtime->tm_min);
}
else
{
fprintf(fileW,"%d", newtime->tm_min);
}
//fprintf(fileW,":", newtime->tm_hour);
if (newtime->tm_sec < 10)
{
//fprintf(fileW,"0%d", newtime->tm_sec);
}
else
{
//fprintf(fileW,"%d", newtime->tm_sec);
}
fprintf(fileW,"-");
//fprintf(fileW,"%s",sender); /*writes*/
fprintf(fileW,"%s", myUsername); /*writes*/
fprintf(fileW,"%s","n"); /*writes*/
fprintf(fileW,"%s", message); /*writes*/
fprintf(fileW,"%s","nn"); /*writes*/
fprintf(fileW, newText); /*writes*/
fclose(fileW); /*done!*/
free(message);
purple_debug_misc("Message Writer", "received-im-msg (%s, %s, %s, %s, %d)n",
purple_account_get_username(account), sender, buffer,
(conv != NULL) ? purple_conversation_get_title(conv) : "(null)", flags);
}
static gboolean
plugin_load (PurplePlugin * plugin)
{
//void *core_handle = purple_get_core();
void *blist_handle = purple_blist_get_handle();
//void *conn_handle = purple_connections_get_handle();
void *conv_handle = purple_conversations_get_handle();
//void *accounts_handle = purple_accounts_get_handle();
//void *ciphers_handle = purple_ciphers_get_handle();
//void *ft_handle = purple_xfers_get_handle();
//void *sound_handle = purple_sounds_get_handle();
//void *notify_handle = purple_notify_get_handle();
purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
purple_signal_connect(conv_handle, "conversation-updated", plugin, PURPLE_CALLBACK(conversation_updated_cb), NULL);
purple_signal_connect(blist_handle, "buddy-signed-on", plugin, PURPLE_CALLBACK(buddy_signed_on_cb), NULL);
exporter_plugin = plugin;
return TRUE;
}
static gboolean plugin_unload(PurplePlugin *plugin)
{
NewMessage(FALSE);
//void *core_handle = purple_get_core();
void *blist_handle = purple_blist_get_handle();
//void *conn_handle = purple_connections_get_handle();
void *conv_handle = purple_conversations_get_handle();
//void *accounts_handle = purple_accounts_get_handle();
//void *ciphers_handle = purple_ciphers_get_handle();
//void *ft_handle = purple_xfers_get_handle();
//void *sound_handle = purple_sounds_get_handle();
//void *notify_handle = purple_notify_get_handle();
purple_signal_disconnect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb));
purple_signal_disconnect(conv_handle, "conversation-updated", plugin, PURPLE_CALLBACK(conversation_updated_cb));
purple_signal_disconnect(blist_handle, "buddy-signed-on", plugin, PURPLE_CALLBACK(buddy_signed_on_cb));
return TRUE;
}
/* For specific notes on the meanings of each of these members, consult the C Plugin Howto
* on the website. */
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
"core-exporter",
"SciLor's MaLotF",
"0.4",
"SciLor's Message and Last online to File",
"Dumps new messages, a new message indicator and the last onlines to files in the .purple dir (LastMessages.txt, NewMessage.txt, LastLogons.txt)",
"SciLor", /* correct author */
"", /* Website */
plugin_load,
plugin_unload,
NULL,
NULL,
NULL,
NULL,
NULL, //plugin_actions, //this tells libpurple the address of the function to call
//to get the list of plugin actions.
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin (PurplePlugin * plugin)
{
}
PURPLE_INIT_PLUGIN (exporter, init_plugin, info)