aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/oscar/snac.c
blob: 8a75b2a0fb51bb200fe6a212d5486906ee7b1015 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 *
 * Various SNAC-related dodads... 
 *
 * outstanding_snacs is a list of aim_snac_t structs.  A SNAC should be added
 * whenever a new SNAC is sent and it should remain in the list until the
 * response for it has been receieved.  
 *
 * cleansnacs() should be called periodically by the client in order
 * to facilitate the aging out of unreplied-to SNACs. This can and does
 * happen, so it should be handled.
 *
 */

#include <aim.h>

static aim_snacid_t aim_newsnac(aim_session_t *sess, aim_snac_t *newsnac);

/*
 * Called from aim_session_init() to initialize the hash.
 */
void aim_initsnachash(aim_session_t *sess)
{
	int i;

	for (i = 0; i < AIM_SNAC_HASH_SIZE; i++)
		sess->snac_hash[i] = NULL;

	return;
}

aim_snacid_t aim_cachesnac(aim_session_t *sess, const guint16 family, const guint16 type, const guint16 flags, const void *data, const int datalen)
{
	aim_snac_t snac;

	snac.id = sess->snacid_next++;
	snac.family = family;
	snac.type = type;
	snac.flags = flags;

	if (datalen) {
		if (!(snac.data = g_malloc(datalen)))
			return 0; /* er... */
		memcpy(snac.data, data, datalen);
	} else
		snac.data = NULL;

	return aim_newsnac(sess, &snac);
}

/*
 * Clones the passed snac structure and caches it in the
 * list/hash.
 */
static aim_snacid_t aim_newsnac(aim_session_t *sess, aim_snac_t *newsnac)
{
	aim_snac_t *snac;
	int index;

	if (!newsnac)
		return 0;

	if (!(snac = g_malloc(sizeof(aim_snac_t))))
		return 0;
	memcpy(snac, newsnac, sizeof(aim_snac_t));
	snac->issuetime = time(NULL);

	index = snac->id % AIM_SNAC_HASH_SIZE;

	snac->next = (aim_snac_t *)sess->snac_hash[index];
	sess->snac_hash[index] = (void *)snac;

	return snac->id;
}

/*
 * Finds a snac structure with the passed SNAC ID, 
 * removes it from the list/hash, and returns a pointer to it.
 *
 * The returned structure must be freed by the caller.
 *
 */
aim_snac_t *aim_remsnac(aim_session_t *sess, aim_snacid_t id) 
{
	aim_snac_t *cur, **prev;
	int index;

	index = id % AIM_SNAC_HASH_SIZE;

	for (prev = (aim_snac_t **)&sess->snac_hash[index]; (cur = *prev); ) {
		if (cur->id == id) {
			*prev = cur->next;
			return cur;
		} else
			prev = &cur->next;
	}

	return cur;
}

/*
 * This is for cleaning up old SNACs that either don't get replies or
 * a reply was never received for.  Garabage collection. Plain and simple.
 *
 * maxage is the _minimum_ age in seconds to keep SNACs.
 *
 */
void aim_cleansnacs(aim_session_t *sess, int maxage)
{
	int i;

	for (i = 0; i < AIM_SNAC_HASH_SIZE; i++) {
		aim_snac_t *cur, **prev;
		time_t curtime;

		if (!sess->snac_hash[i])
			continue;

		curtime = time(NULL); /* done here in case we waited for the lock */

		for (prev = (aim_snac_t **)&sess->snac_hash[i]; (cur = *prev); ) {
			if ((curtime - cur->issuetime) > maxage) {

				*prev = cur->next;

				/* XXX should we have destructors here? */
				g_free(cur->data);
				g_free(cur);

			} else
				prev = &cur->next;
		}
	}

	return;
}

int aim_putsnac(aim_bstream_t *bs, guint16 family, guint16 subtype, guint16 flags, aim_snacid_t snacid)
{

	aimbs_put16(bs, family);
	aimbs_put16(bs, subtype);
	aimbs_put16(bs, flags);
	aimbs_put32(bs, snacid);

	return 10;
}
/span>when /^And:? / then @state.one_more_of_the_same(line) else @state.other(line) end end def init_story(title) @current_story_lines.clear add_story_line(title) end def add_story_line(line) @current_story_lines << line end def create_story() unless @current_story_lines.empty? @story_mediator.create_story(@current_story_lines[0].gsub("Story: ",""), @current_story_lines[1..-1].join("\n")) @current_story_lines.clear end end def create_scenario(title) @story_mediator.create_scenario(title.gsub("Scenario: ","")) end def create_given(name) @story_mediator.create_given(name) end def create_given_scenario(name) @story_mediator.create_given_scenario(name) end def create_when(name) @story_mediator.create_when(name) end def create_then(name) @story_mediator.create_then(name) end def transition_to(key) @state = states[key] end def states @states ||= { :starting_state => StartingState.new(self), :story_state => StoryState.new(self), :scenario_state => ScenarioState.new(self), :given_state => GivenState.new(self), :when_state => WhenState.new(self), :then_state => ThenState.new(self) } end class State def initialize(parser) @parser = parser end def story(line) @parser.init_story(line) @parser.transition_to(:story_state) end def scenario(line) @parser.create_scenario(line) @parser.transition_to(:scenario_state) end def given(line) @parser.create_given(remove_tag_from(:given, line)) @parser.transition_to(:given_state) end def given_scenario(line) @parser.create_given_scenario(remove_tag_from(:givenscenario, line)) @parser.transition_to(:given_state) end def event(line) @parser.create_when(remove_tag_from(:when, line)) @parser.transition_to(:when_state) end def outcome(line) @parser.create_then(remove_tag_from(:then, line)) @parser.transition_to(:then_state) end def remove_tag_from(tag, line) tokens = line.split # validation of tag can go here tokens[0].downcase.match(/#{tag.to_s}:?/) ? (tokens[1..-1].join(' ')) : line end def eof end def other(line) # no-op - supports header text before the first story in a file end end class StartingState < State def initialize(parser) @parser = parser end end class StoryState < State def one_more_of_the_same(line) other(line) end def story(line) @parser.create_story @parser.add_story_line(line) end def scenario(line) @parser.create_story @parser.create_scenario(line) @parser.transition_to(:scenario_state) end def given(line) other(line) end def event(line) other(line) end def outcome(line) other(line) end def other(line) @parser.add_story_line(line) end def eof @parser.create_story end end class ScenarioState < State def one_more_of_the_same(line) raise IllegalStepError.new("Scenario", "And") end def scenario(line) @parser.create_scenario(line) end end class GivenState < State def one_more_of_the_same(line) @parser.create_given(remove_tag_from(:and, line)) end def given(line) @parser.create_given(remove_tag_from(:given, line)) end end class WhenState < State def one_more_of_the_same(line) @parser.create_when(remove_tag_from(:and ,line)) end def event(line) @parser.create_when(remove_tag_from(:when ,line)) end end class ThenState < State def one_more_of_the_same(line) @parser.create_then(remove_tag_from(:and ,line)) end def outcome(line) @parser.create_then(remove_tag_from(:then ,line)) end end end end end end