ayttm-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Ayttm-commits] CVS: ayttm/src offline_queue_mgmt.c,NONE,1.1 offline_que


From: Philip S Tellis <address@hidden>
Subject: [Ayttm-commits] CVS: ayttm/src offline_queue_mgmt.c,NONE,1.1 offline_queue_mgmt.h,NONE,1.1 contact_util.c,NONE,1.1
Date: Thu, 06 Feb 2003 03:43:38 -0500

Update of /cvsroot/ayttm/ayttm/src
In directory subversions:/tmp/cvs-serv24125

Added Files:
        offline_queue_mgmt.c offline_queue_mgmt.h contact_util.c 
Log Message:
separate file for offline queue management - not yet linked in

--- NEW FILE: offline_queue_mgmt.c ---
#include <stdio.h>
#include "account.h"
#include "contact.h"
#include "globals.h"
#include "service.h"
#include "mem_util.h"
#include "offline_queue_mgmt.h"  /* so the compiler tells us about type 
mismatches */

/* FIXME: temporary */
eb_account * find_account_by_handle( char * handle, int type );
/* end FIXME */

void contact_mgmt_queue_add(const eb_account *ea, int action, const char 
*new_group)
{
        FILE *fp;
        char buff [NAME_MAX];
        snprintf(buff, NAME_MAX, "%s%ccontact_actions_queue", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        fp = fopen(buff,"a");
        if (!fp) {
                perror(buff);
                return;
        }
        
        fprintf(fp, "%s\t%d\t%s\t%s\t%s\n",
                get_service_name(ea->service_id),
                action,
                ea->handle,
                (action!=MGMT_ADD ? ea->account_contact->group->name:"NULL"),
                (new_group!=NULL ? new_group:"NULL"));
        
        fclose(fp);
}


void group_mgmt_queue_add(const eb_local_account *ela, const char *old_group, 
int action, const char *new_group)
{
        FILE *fp;
        char buff [NAME_MAX];
        snprintf(buff, NAME_MAX, "%s%cgroup_actions_queue", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        fp = fopen(buff,"a");
        if (!fp) {
                perror(buff);
                return;
        }
        
        fprintf(fp, "%s\t%d\t%s\t%s\n",
                get_service_name(ela->service_id),
                action,
                (old_group!=NULL ? old_group:"NULL"),
                (new_group!=NULL ? new_group:"NULL"));
        
        fclose(fp);
}

int contact_mgmt_flush(const eb_local_account *ela)
{
        FILE *queue, *temp;
        char buff[NAME_MAX], queue_name[NAME_MAX], temp_name[NAME_MAX];
        char **tokens = NULL;
        
        /* flush groups, too */
        group_mgmt_flush(ela);
        
        snprintf(queue_name, NAME_MAX, "%s%ccontact_actions_queue", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        
        queue = fopen(queue_name, "r");
        if (!queue)
                return 0;
        snprintf(temp_name, NAME_MAX, "%s%ccontact_actions_queue.new", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        
        temp = fopen(temp_name, "w");
        
        if (!temp) {
                perror(buff);
                return 0;
        }
        
        while( fgets(buff, sizeof(buff), queue)  != NULL )
        {               
                char buff_backup[NAME_MAX];
                strcpy(buff_backup, buff);
                tokens = ay_strsplit( buff, "\t", -1 );
                if(!strcmp(tokens[0],get_service_name(ela->service_id))) {
                        int action     = atoi(tokens[1]);
                        char *handle   = tokens[2];
                        char *oldgroup = tokens[3];
                        char *newgroup = tokens[4];
                        eb_account *ea = NULL;
                        int ea_recreated = TRUE;
                        
                        ea = find_account_by_handle(handle, 
                                                    ela->service_id);

                        if (ea && action == MGMT_ADD && CAN(ea, add_user)) {
                                RUN_SERVICE(ea)->add_user(ea);
                        }
                        if (ea && action == MGMT_MOV && CAN(ea, change_group)) {
                                char realgrp[NAME_MAX];
                                /* this is "a bit" ugly :-( */
                                
strcpy(realgrp,ea->account_contact->group->name);
                                
strcpy(ea->account_contact->group->name,oldgroup);
                                RUN_SERVICE(ea)->change_group(ea, newgroup);
                                strcpy(ea->account_contact->group->name, 
realgrp);
                        }
                        
                        if (ea && action == MGMT_REN && CAN(ea, 
change_user_name)) {
                                RUN_SERVICE(ea)->change_user_name(ea, newgroup);
                        }

                        if (!ea) {
                                ea_recreated = FALSE;
                                ea = dummy_account(handle, oldgroup, 
                                                   ela->service_id);
                        }

                        if (action == MGMT_DEL && CAN(ea, del_user)) {
                                /* some services syncing lists have
                                   automatically re-added our deleted 
                                   account */
                                if (ea_recreated) {
                                        if 
(l_list_length(ea->account_contact->accounts) == 1)
                                                
remove_contact(ea->account_contact);
                                        else
                                                remove_account(ea);
                                }
                                else
                                        RUN_SERVICE(ea)->del_user(ea);
                        }
                } else {
                        /* not for me */
                        fprintf(temp, "%s", buff_backup);
                }

                ay_strfreev(tokens);
        }
        
        fclose (temp);
        fclose (queue);
        rename (temp_name, queue_name);
        return 0;
}

int group_mgmt_check_moved(const char *groupname)
{
        FILE *queue, *temp;
        char buff[NAME_MAX], queue_name[NAME_MAX], temp_name[NAME_MAX];
        snprintf(queue_name, NAME_MAX, "%s%cgroup_actions_queue", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        
        queue = fopen(queue_name, "r");
        if (!queue)
                return 0;

        while( fgets(buff, sizeof(buff), queue)  != NULL )
        {               
                char **tokens  = ay_strsplit( buff, "\t", -1 );
                int action     = atoi(tokens[0]);
                char *oldgroup = tokens[1];
                char *newgroup = tokens[2];
                        
                if (!strcmp(oldgroup, groupname)) {
                        fclose(queue);
                        return 1;
                }
                ay_strfreev(tokens);
        }
        
        fclose (queue);
        return 0;
}

int group_mgmt_flush(const eb_local_account *ela)
{
        FILE *queue, *temp;
        char buff[NAME_MAX], queue_name[NAME_MAX], temp_name[NAME_MAX];
        char **tokens = NULL;
        snprintf(queue_name, NAME_MAX, "%s%cgroup_actions_queue", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        
        queue = fopen(queue_name, "r");
        if (!queue)
                return 0;
        snprintf(temp_name, NAME_MAX, "%s%cgroup_actions_queue.new", 
                                config_dir, 
                                G_DIR_SEPARATOR);
        
        temp = fopen(temp_name, "w");
        
        if (!temp) {
                perror(buff);
                return 0;
        }
        
        while( fgets(buff, sizeof(buff), queue)  != NULL )
        {               
                char buff_backup[NAME_MAX];
                strcpy(buff_backup, buff);
                tokens = ay_strsplit( buff, "\t", -1 );
                if(!strcmp(tokens[0], get_service_name(ela->service_id))) {
                        int action     = atoi(tokens[1]);
                        char *oldgroup = tokens[2];
                        char *newgroup = tokens[3];
                        
                        if (action == MGMT_GRP_ADD && CAN(ela, add_group)) {
                                RUN_SERVICE(ela)->add_group(newgroup);
                        }
                        if (action == MGMT_GRP_REN && CAN(ela, rename_group)) {
                                
RUN_SERVICE(ela)->rename_group(oldgroup,newgroup);                              
        
                        }
                        if (action == MGMT_GRP_DEL && CAN(ela, del_group)) {
                                RUN_SERVICE(ela)->del_group(oldgroup);
                        }
                } else {
                        /* not for me */
                        fprintf(temp, "%s", buff_backup);
                }
                ay_strfreev(tokens);
        }
        
        fclose (temp);
        fclose (queue);
        rename (temp_name, queue_name);
        return 0;
}




--- NEW FILE: offline_queue_mgmt.h ---
#ifndef __OFFLINE_QUEUE_MGMT__
#define __OFFLINE_QUEUE_MGMT__

enum
{
  MGMT_ADD,
  MGMT_DEL,
  MGMT_MOV,
  MGMT_REN,
  MGMT_GRP_ADD,
  MGMT_GRP_DEL,
  MGMT_GRP_REN
};

#ifdef __cplusplus
extern "C" {
#endif

void contact_mgmt_queue_add(const eb_account *ea, int action, const char 
*new_group);
void group_mgmt_queue_add(const eb_local_account *ela, const char *old_group, 
int action, const char *new_group);
int contact_mgmt_flush(const eb_local_account *ela);
int group_mgmt_check_moved(const char *groupname);
int group_mgmt_flush(const eb_local_account *ela);

#ifdef __cplusplus
}
#endif


#endif

--- NEW FILE: contact_util.c ---
/*
 * Ayttm 
 *
 * Copyright (C) 2003, the Ayttm team
 * 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * contact_util.c - utility functions for contact handling
 *
 */

#include "intl.h"
#include <stdlib.h>
#include <string.h>
#include "llist.h"
#include "account.h"
#include "contact.h"
#include "service.h"

static LList *groups = NULL;
extern LList *accounts;

#define ONLINE(ela) (ela->connected || ela->connecting)
/* compares two contact names */
static int contact_cmp(const void * a, const void * b)
{
        const struct contact *ca=a, *cb=b;
        return strcasecmp(ca->nick, cb->nick);
}

grouplist * find_grouplist_by_name(const char *name)
{
        LList *l1;

        if(name == NULL)
                return NULL;

        for(l1 = groups; l1; l1=l1->next )
                if(!strcasecmp(((grouplist *)l1->data)->name, name))
                        return l1->data;

        return NULL;
}

struct contact * find_contact_in_group_by_nick(const char *nick, grouplist 
*group)
{
        LList *l1;

        if(nick == NULL || group == NULL)
                return NULL;

        for(l1 = group->members; l1; l1=l1->next)
                if(!strcasecmp(((struct contact *)l1->data)->nick, nick))
                        return l1->data;

        return NULL;
}

struct contact * find_contact_by_nick(const char *nick)
{
        LList *l1;
        struct contact *contact=NULL;

        if(nick == NULL)
                return NULL;

        for(l1=groups; l1; l1=l1->next)
                if((contact = find_contact_in_group_by_nick(nick, l1->data)) != 
NULL)
                        return contact;

        return NULL;
}

struct contact * find_contact_by_handle( char * handle )
{
        LList *l1, *l2, *l3;

        if (handle == NULL) 
                return NULL;

        for(l1 = groups; l1; l1=l1->next ) {
                for(l2 = ((grouplist*)l1->data)->members; l2; l2=l2->next ) {
                        for(l3 = ((struct contact*)l2->data)->accounts; l3; 
l3=l3->next) {
                                eb_account * account = (eb_account*)l3->data;
                                if(!strcmp(account->handle, handle))
                                        return l2->data;
                        }
                }
        }
        return NULL;
}

eb_account * find_account_by_handle(const char *handle, const eb_local_account 
* ela)
{
        LList *l1, *l2, *l3;

        for(l1 = groups; l1; l1=l1->next)
                for(l2 = ((grouplist *)l1->data)->members; l2; l2=l2->next)
                        for(l3 = ((struct contact *)l2->data)->accounts; l3; 
l3=l3->next) {
                                eb_account * ea = l3->data;
                                if(ea->ela == ela && !strcmp(ea->handle, 
handle))
                                        return ea;
                        }

        return NULL;
}
                
grouplist * add_group(const char *group_name)
{
        LList *node = NULL;
        grouplist *gl;
        
        if(group_name == NULL || *group_name=='\0')
                return NULL;

        gl = calloc(1, sizeof(grouplist));
        strncpy(gl->name, group_name, sizeof(gl->name)-1);

        groups = l_list_append( groups, gl );

        for( node = accounts; node; node = node->next ) {
                eb_local_account *ela = node->data;
                if (CAN(ela, add_group)) {
                        if (ONLINE(ela))
                                RUN_SERVICE(ela)->add_group(group_name);
                        else
                                group_mgmt_queue_add(ela, NULL, MGMT_GRP_ADD, 
group_name);
                }
        }

        return gl;
}

struct contact * add_contact(const char *contact_name, const char *group_name)
{
        grouplist *group;
        struct contact *contact;
        
        if(contact_name == NULL || *contact_name=='\0')
                return NULL;

        if(group_name == NULL || *group_name=='\0')
                group_name = _("Unknown");

        group = find_grouplist_by_name(group_name);

        if(group == NULL)
                group = add_group(group_name);


        contact = calloc(1, sizeof(struct contact));
        strncpy(contact->nick, contact_name, sizeof(contact->nick)-1);
        
        group->members = l_list_insert_sorted(group->members, contact, 
contact_cmp );

        contact->group = group;

        return contact;
}

eb_account * add_account(const char *handle, struct contact *contact, 
eb_local_account *ela)
{
        eb_account *account;

        if(handle == NULL || *handle=='\0' || contact==NULL)
                return NULL;

        account = calloc(1, sizeof(eb_account));
        account->service_id = ela->service_id;
        account->ela = ela;
        account->account_contact = contact;

        if(CAN(ela, add_user)) {
                if (ONLINE(ela))
                        RUN_SERVICE(ela)->add_user(account);
                else
                        contact_mgmt_queue_add(account, MGMT_ADD, 
contact->group->name);
        }

        contact->accounts = l_list_append(contact->accounts, account);

        return account;
}

void destroy_account(eb_account * account)
{
        RUN_SERVICE(account)->free_account_data(account);
        free(account);
}

void destroy_contact(struct contact * contact)
{
        while(contact->accounts) {
                destroy_account(contact->accounts->data);
                contact->accounts = l_list_remove_link(contact->accounts, 
contact->accounts);
        }

        free(contact);
}

void destroy_group(grouplist * group)
{
        while(group->members) {
                destroy_contact(group->members->data);
                group->members = l_list_remove_link(group->members, 
group->members);
        }

        free(group);
}

void remove_account(eb_account * account)
{
        RUN_SERVICE(account)->del_user(account);
        account->account_contact->accounts = 
l_list_remove(account->account_contact->accounts, account);
        destroy_account(account);
}

void remove_contact(struct contact * contact)
{
        contact->group->members = l_list_remove(contact->group->members, 
contact);
        while(contact->accounts) {
                remove_account(contact->accounts->data);
                contact->accounts = l_list_remove_link(contact->accounts, 
contact->accounts);
        }
        destroy_contact(contact);
}

void remove_group(grouplist * group)
{
        LList *l;

        groups = l_list_remove(groups, group);

        while(group->members) {
                remove_contact(group->members->data);
                group->members = l_list_remove_link(group->members, 
group->members);
        }

        for(l=accounts; l; l=l->next) {
                eb_local_account * ela = l->data;
                if(CAN(ela, del_group)) {
                        if (ONLINE(ela))
                                RUN_SERVICE(ela)->del_group(group->name);
                        else
                                group_mgmt_queue_add(eal, group->name, 
MGMT_GRP_DEL, NULL);
                }
        }

        destroy_group(group);
}

static void handle_group_change(eb_account *ea, const char *og, const char *ng)
{
        /* if the groups are same, do nothing */
        if(!strcasecmp(ng, og))
                return;

        /* adding to ignore */
        if(!strcmp(ng, _("Ignore")) && CAN(ea, ignore_user))
                RUN_SERVICE(ea)->ignore_user(ea);

        /* remove from ignore */
        else if(!strcmp(og, _("Ignore")) && CAN(ea, unignore_user))
                RUN_SERVICE(ea)->unignore_user(ea, ng);
                
        /* just your regular group change */
        else if(CAN(ea, change_group)) {
                if (ONLINE(ea->ela))
                        RUN_SERVICE(ea)->change_group(ea, ng);
                else
                        contact_mgmt_queue_add(ea, MGMT_MOV, ng);
        }

}

void rename_group(grouplist * group, const char * new_name)
{
        LList *l;

        if(new_name == NULL)
                return;
        
        for(l=accounts; l; l=l->next) {
                eb_local_account * ela = l->data;
                if(CAN(ela, rename_group)) {
                        if (ONLINE(ela))
                                RUN_SERVICE(ela)->rename_group(group->name, 
new_name);
                        else
                                group_mgmt_queue_add(ela, NULL, MGMT_GRP_REN, 
new_name);
                } else {
                        LList *l1, *l2;
                        if(CAN(ela, add_group)) {
                                if (ONLINE(ela))
                                        RUN_SERVICE(ela)->add_group(new_name);
                                else
                                        group_mgmt_queue_add(ela, NULL, 
MGMT_GRP_ADD, new_name);
                        }
                        
                        for(l1=group->members; l1; l1=l1->next)
                                for(l2=((struct contact *)l1->data)->accounts; 
l2; l2=l2->next)
                                        handle_group_change(l2->data, 
group->name, new_name);

                        if(CAN(ela, del_group)) {
                                if (ONLINE(ela))
                                        
RUN_SERVICE(ela)->del_group(group->name);
                                else
                                        group_mgmt_queue_add(ela, group->name, 
MGMT_GRP_DEL, NULL);
                        }
                }
        }

        strncpy(group->name, new_name, sizeof(group->name)-1);
}

void rename_contact(struct contact * contact, const char * new_name)
{
        LList *l;

        if(new_name == NULL)
                return;

        for(l=contact->accounts; l; l=l->next) {
                eb_account * ea = l->data;
                if(CAN(ea, change_user_name)) {
                        if (ONLINE(ea->ela))
                                RUN_SERVICE(ea)->change_user_name(ea, new_name);
                        else
                                contact_mgmt_queue_add(ea, MGMT_REN, new_name);
                }
        }
}

void move_contact(struct contact * contact, grouplist * new_group)
{
        LList *l;

        for(l=contact->accounts; l; l=l->next)
                handle_group_change(l->data, contact->group->name, 
new_group->name);

        contact->group->members = l_list_remove(contact->group->members, 
contact);
        contact->group = new_group;
        new_group->members = l_list_insert_sorted(new_group->members, contact, 
contact_cmp);
}

void move_account(eb_account * account, struct contact * new_contact)
{
        struct contact * old_contact = account->account_contact;

        handle_group_change(account, old_contact->group->name, 
new_contact->group->name);

        old_contact->accounts = l_list_remove(old_contact->accounts, account);
        account->account_contact = new_contact;
        new_contact->accounts = l_list_append(new_contact->accounts, account);
}

LList * get_group_names( void )
{
        LList * g, g2;
        for(g2 = groups; g2; g2=g2->next)
                g = l_list_insert_sorted(g, ((grouplist *)g2->data)->name, 
strcasecmp);

        return g;
}

LList * get_group_contact_names( grouplist * group )
{
        LList * g, g2;
        for(g2 = group->members; g2; g2=g2->next)
                g = l_list_insert_sorted(g, ((struct contact *)g2->data)->nick, 
strcasecmp);

        return g;
}






reply via email to

[Prev in Thread] Current Thread [Next in Thread]