noirchat/users.h

63 lines
2.0 KiB
C

/*
===============================================================================
Users
Defines User struct and functions for manipulating that struct.
===============================================================================
*/
#ifndef USERS_H
#define USERS_H
#include <netinet/in.h>
#include "macros.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h> // for intptr_t http://stackoverflow.com/questions/5701450/getting-the-warning-cast-to-pointer-from-integer-of-different-size-from-the-fo#5703367
/* struct for storing known/talked to users */
typedef struct {
uint8_t address_count;
struct timeval last_heard;
struct in_addr last_address;
//struct in_addr *last_address;
struct in_addr *addresses; /* dynamically allocated array via malloc */
// char nick[15];
char *nick; // dynamically allocated char array
} User;
/* struct for storing a list of Users, memory managed dynamically */
typedef struct {
int length;
User *users;
} UserList;
/* checkUser
Checks the given UserList for the occurrence of nick in the users property
*/
int checkUser(UserList *user_list, const char nick[], struct in_addr address);
/* addUser(UserList *user_list, const char nick[], struct in_addr address)
creates a new User struct refernced in user_list.users, mallocs User.nick to passed nick and memcpys, set address, and time
*/
int addUser(UserList *user_list, const char nick[], struct in_addr address);
/* updateUser
Updates the given nick in user_list, setting the User's last_address to address and changing last_heard to the current time
*/
int updateUser(UserList *user_list, const char nick[], struct in_addr address);
/* addUserAddress(User *user, struct in_addr address)
Takes a pointer to a User struct and adds the address to user.addresses, mallocing as appropriate.
*/
int addUserAddress(User *user, struct in_addr address);
/* freeUserList
frees all malloc'd values in user_list and the contained users(and their mallocs)
*/
void freeUserList(UserList *user_list);
#endif