File: session.c

package info (click to toggle)
imapfilter 1%3A2.0.10-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 424 kB
  • ctags: 479
  • sloc: ansic: 4,048; sh: 218; makefile: 137
file content (101 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#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->username = NULL;
	ssn->socket = -1;
#ifndef NO_SSLTLS
	ssn->ssl = NULL;
#endif
	ssn->protocol = PROTOCOL_NONE;
	ssn->capabilities = CAPABILITY_NONE;
	ssn->ns.prefix = NULL;
	ssn->ns.delim = '\0';
}


/*
 * Remove session from sessions linked list.
 */
void
session_destroy(session *ssn)
{

	sessions = list_remove(sessions, ssn);

	session_free(ssn);
}


/*
 * Free session allocated memory.
 */
void
session_free(session *ssn)
{

	if (ssn->server)
		xfree(ssn->server);
	if (ssn->port)
		xfree(ssn->port);
	if (ssn->username)
		xfree(ssn->username);
	if (ssn->ns.prefix)
		xfree(ssn->ns.prefix);
	xfree(ssn);
}


/*
 * 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;
}