imapfilter/src/session.c

105 lines
1.6 KiB
C
Raw Normal View History

2011-03-06 06:58:58 -05:00
#include <stdio.h>
#include <string.h>
#include "imapfilter.h"
#include "session.h"
#include "list.h"
extern list *sessions;
/*
* Allocate memory for a new session and add it to the sessions linked list.
*/
session *
session_new(void)
{
session *s;
s = (session *)xmalloc(sizeof(session));
session_init(s);
sessions = list_append(sessions, s);
return s;
}
/*
* Set session variables to safe values.
*/
void
session_init(session *ssn)
{
ssn->server = NULL;
ssn->port = NULL;
ssn->ssl = NULL;
2011-03-06 06:58:58 -05:00
ssn->username = NULL;
ssn->password = NULL;
2011-03-06 06:58:58 -05:00
ssn->socket = -1;
#ifndef NO_SSLTLS
ssn->sslsocket = NULL;
2011-03-06 06:58:58 -05:00
#endif
ssn->protocol = PROTOCOL_NONE;
ssn->capabilities = CAPABILITY_NONE;
ssn->ns.prefix = NULL;
ssn->ns.delim = '\0';
ssn->selected = NULL;
2011-03-06 06:58:58 -05:00
}
/*
* Remove session from sessions linked list and free allocated memory.
2011-03-06 06:58:58 -05:00
*/
void
session_destroy(session *ssn)
{
if (!ssn)
return;
2011-03-06 06:58:58 -05:00
sessions = list_remove(sessions, ssn);
if (ssn->server)
xfree(ssn->server);
if (ssn->port)
xfree(ssn->port);
if (ssn->ssl)
xfree(ssn->ssl);
2011-03-06 06:58:58 -05:00
if (ssn->username)
xfree(ssn->username);
if (ssn->password)
xfree(ssn->password);
2011-03-06 06:58:58 -05:00
if (ssn->ns.prefix)
xfree(ssn->ns.prefix);
if (ssn->selected)
xfree(ssn->selected);
2011-03-06 06:58:58 -05:00
xfree(ssn);
ssn = NULL;
2011-03-06 06:58:58 -05:00
}
/*
* Based on the specified socket, find an active IMAP session.
*/
session *
session_find(const char *serv, const char *port, const char *user)
{
list *l;
session *s;
for (l = sessions; l; l = l->next) {
s = l->data;
if (!strcmp(s->server, serv) &&
!strcmp(s->port, port) &&
!strcmp(s->username, user))
return s;
}
return NULL;
}