aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/oscar/msgcookie.c
blob: efeb8cbf6412a90151522fbdc3daeda42d7ecead (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Cookie Caching stuff. Adam wrote this, apparently just some
 * derivatives of n's SNAC work. I cleaned it up, added comments.
 * 
 */

/*
 * I'm assuming that cookies are type-specific. that is, we can have
 * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we
 * lose some error checking. if we assume cookies are not type-specific and are
 * wrong, we get quirky behavior when cookies step on each others' toes.
 */

#include <aim.h>
#include "info.h"

/**
 * aim_cachecookie - appends a cookie to the cookie list
 * @sess: session to add to
 * @cookie: pointer to struct to append
 *
 * if cookie->cookie for type cookie->type is found, updates the
 * ->addtime of the found structure; otherwise adds the given cookie
 * to the cache
 *
 * returns -1 on error, 0 on append, 1 on update.  the cookie you pass
 * in may be free'd, so don't count on its value after calling this!
 * 
 */
int aim_cachecookie(aim_session_t *sess, aim_msgcookie_t *cookie)
{
	aim_msgcookie_t *newcook;

	if (!sess || !cookie)
		return -EINVAL;

	newcook = aim_checkcookie(sess, cookie->cookie, cookie->type);
	
	if (newcook == cookie) {
		newcook->addtime = time(NULL);
		return 1;
	} else if (newcook)
		aim_cookie_free(sess, newcook);

	cookie->addtime = time(NULL);  

	cookie->next = sess->msgcookies;
	sess->msgcookies = cookie;

	return 0;
}

/**
 * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list)
 * @sess: session to grab cookie from
 * @cookie: cookie string to look for
 * @type: cookie type to look for
 *
 * takes a cookie string and a cookie type and finds the cookie struct associated with that duple, removing it from the cookie list ikn the process.
 *
 * if found, returns the struct; if none found (or on error), returns NULL:
 */
aim_msgcookie_t *aim_uncachecookie(aim_session_t *sess, guint8 *cookie, int type)
{
	aim_msgcookie_t *cur, **prev;

	if (!cookie || !sess->msgcookies)
		return NULL;

	for (prev = &sess->msgcookies; (cur = *prev); ) {
		if ((cur->type == type) && 
				(memcmp(cur->cookie, cookie, 8) == 0)) {
			*prev = cur->next;
			return cur;
		}
		prev = &cur->next;
	}

	return NULL;
}

/**
 * aim_mkcookie - generate an aim_msgcookie_t *struct from a cookie string, a type, and a data pointer.
 * @c: pointer to the cookie string array
 * @type: cookie type to use
 * @data: data to be cached with the cookie
 *
 * returns NULL on error, a pointer to the newly-allocated cookie on
 * success.
 *
 */
aim_msgcookie_t *aim_mkcookie(guint8 *c, int type, void *data) 
{
	aim_msgcookie_t *cookie;

	if (!c)
		return NULL;

	if (!(cookie = g_new0(aim_msgcookie_t,1)))
		return NULL;

	cookie->data = data;
	cookie->type = type;
	memcpy(cookie->cookie, c, 8);

	return cookie;
}

/**
 * aim_checkcookie - check to see if a cookietuple has been cached
 * @sess: session to check for the cookie in
 * @cookie: pointer to the cookie string array
 * @type: type of the cookie to look for
 *
 * this returns a pointer to the cookie struct (still in the list) on
 * success; returns NULL on error/not found
 *
 */

aim_msgcookie_t *aim_checkcookie(aim_session_t *sess, const guint8 *cookie, int type)
{
	aim_msgcookie_t *cur;

	for (cur = sess->msgcookies; cur; cur = cur->next) {
		if ((cur->type == type) && 
				(memcmp(cur->cookie, cookie, 8) == 0))
			return cur;   
	}

	return NULL;
}

/**
 * aim_cookie_free - free an aim_msgcookie_t struct
 * @sess: session to remove the cookie from
 * @cookiep: the address of a pointer to the cookie struct to remove
 *
 * this function removes the cookie *cookie from teh list of cookies
 * in sess, and then frees all memory associated with it. including
 * its data! if you want to use the private data after calling this,
 * make sure you copy it first.
 *
 * returns -1 on error, 0 on success.
 *
 */
int aim_cookie_free(aim_session_t *sess, aim_msgcookie_t *cookie) 
{
	aim_msgcookie_t *cur, **prev;

	if (!sess || !cookie)
		return -EINVAL;

	for (prev = &sess->msgcookies; (cur = *prev); ) {
		if (cur == cookie)
			*prev = cur->next;
		else
			prev = &cur->next;
	}

	g_free(cookie->data);
	g_free(cookie);

	return 0;
} 
n>struct jabber_error *err; struct jabber_buddy *bud; char *room; room = xt_find_attr( orig, "to" ); bud = jabber_buddy_by_jid( ic, room, 0 ); err = jabber_error_parse( xt_find_node( node->children, "error" ), XMLNS_STANZA_ERROR ); if( err ) { imcb_error( ic, "Error joining groupchat %s: %s%s%s", room, err->code, err->text ? ": " : "", err->text ? err->text : "" ); jabber_error_free( err ); } if( bud ) jabber_chat_free( jabber_chat_by_jid( ic, bud->bare_jid ) ); return XT_HANDLED; } struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name ) { char *normalized = jabber_normalize( name ); struct groupchat *ret; struct jabber_chat *jc; for( ret = ic->groupchats; ret; ret = ret->next ) { jc = ret->data; if( strcmp( normalized, jc->name ) == 0 ) break; } g_free( normalized ); return ret; } void jabber_chat_free( struct groupchat *c ) { struct jabber_chat *jc = c->data; jabber_buddy_remove_bare( c->ic, jc->name ); g_free( jc->my_full_jid ); g_free( jc->name ); g_free( jc ); imcb_chat_free( c ); } int jabber_chat_msg( struct groupchat *c, char *message, int flags ) { struct im_connection *ic = c->ic; struct jabber_chat *jc = c->data; struct xt_node *node; jc->flags |= JCFLAG_MESSAGE_SENT; node = xt_new_node( "body", message, NULL ); node = jabber_make_packet( "message", "groupchat", jc->name, node ); if( !jabber_write_packet( ic, node ) ) { xt_free_node( node ); return 0; } xt_free_node( node ); return 1; } int jabber_chat_topic( struct groupchat *c, char *topic ) { struct im_connection *ic = c->ic; struct jabber_chat *jc = c->data; struct xt_node *node; node = xt_new_node( "subject", topic, NULL ); node = jabber_make_packet( "message", "groupchat", jc->name, node ); if( !jabber_write_packet( ic, node ) ) { xt_free_node( node ); return 0; } xt_free_node( node ); return 1; } int jabber_chat_leave( struct groupchat *c, const char *reason ) { struct im_connection *ic = c->ic; struct jabber_chat *jc = c->data; struct xt_node *node; node = xt_new_node( "x", NULL, NULL ); xt_add_attr( node, "xmlns", XMLNS_MUC ); node = jabber_make_packet( "presence", "unavailable", jc->my_full_jid, node ); if( !jabber_write_packet( ic, node ) ) { xt_free_node( node ); return 0; } xt_free_node( node ); return 1; } void jabber_chat_invite( struct groupchat *c, char *who, char *message ) { struct xt_node *node; struct im_connection *ic = c->ic; struct jabber_chat *jc = c->data; node = xt_new_node( "reason", message, NULL ); node = xt_new_node( "invite", NULL, node ); xt_add_attr( node, "to", who ); node = xt_new_node( "x", NULL, node ); xt_add_attr( node, "xmlns", XMLNS_MUC_USER ); node = jabber_make_packet( "message", NULL, jc->name, node ); jabber_write_packet( ic, node ); xt_free_node( node ); } /* Not really the same syntax as the normal pkt_ functions, but this isn't called by the xmltree parser directly and this way I can add some extra parameters so we won't have to repeat too many things done by the caller already. */ void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) { struct groupchat *chat; struct xt_node *c; char *type = xt_find_attr( node, "type" ); struct jabber_chat *jc; char *s; if( ( chat = jabber_chat_by_jid( ic, bud->bare_jid ) ) == NULL ) { /* How could this happen?? We could do kill( self, 11 ) now or just wait for the OS to do it. :-) */ return; } jc = chat->data; if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) ) { bud->flags |= JBFLAG_IS_CHATROOM; /* If this one wasn't set yet, this buddy just joined the chat. Slightly hackish way of finding out eh? ;-) */ /* This is pretty messy... Here it sets ext_jid to the real JID of the participant. Works for non-anonymized channels. Might break if someone joins a chat twice, though. */ for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) if( ( s = xt_find_attr( c, "xmlns" ) ) && ( strcmp( s, XMLNS_MUC_USER ) == 0 ) ) { struct xt_node *item; item = xt_find_node( c->children, "item" ); if( ( s = xt_find_attr( item, "jid" ) ) ) { /* Yay, found what we need. :-) */ bud->ext_jid = jabber_normalize( s ); break; } } /* Make up some other handle, if necessary. */ if( bud->ext_jid == NULL ) { if( bud == jc->me ) { bud->ext_jid = jabber_normalize( ic->acc->user ); } else { int i; /* Don't want the nick to be at the end, so let's think of some slightly different notation to use for anonymous groupchat participants in BitlBee. */ bud->ext_jid = g_strdup_printf( "%s=%s", bud->resource, bud->bare_jid ); /* And strip any unwanted characters. */ for( i = 0; bud->resource[i]; i ++ ) if( bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@' ) bud->ext_jid[i] = '_'; /* Some program-specific restrictions. */ imcb_clean_handle( ic, bud->ext_jid ); } bud->flags |= JBFLAG_IS_ANONYMOUS; } if( bud != jc->me ) { imcb_add_buddy( ic, bud->ext_jid, NULL ); imcb_buddy_nick_hint( ic, bud->ext_jid, bud->resource ); } s = strchr( bud->ext_jid, '/' ); if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */ imcb_chat_add_buddy( chat, bud->ext_jid ); if( s ) *s = '/'; } else if( type ) /* type can only be NULL or "unavailable" in this function */ { if( ( bud->flags & JBFLAG_IS_CHATROOM ) && bud->ext_jid ) { s = strchr( bud->ext_jid, '/' ); if( s ) *s = 0; imcb_chat_remove_buddy( chat, bud->ext_jid, NULL ); if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS ) imcb_remove_buddy( ic, bud->ext_jid, NULL ); if( s ) *s = '/'; } if( bud == jc->me ) jabber_chat_free( chat ); } } void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) { struct xt_node *subject = xt_find_node( node->children, "subject" ); struct xt_node *body = xt_find_node( node->children, "body" ); struct groupchat *chat = bud ? jabber_chat_by_jid( ic, bud->bare_jid ) : NULL; struct jabber_chat *jc = chat ? chat->data : NULL; char *s; if( bud == NULL || ( jc && ~jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me ) ) { char *nick; if( body == NULL || body->text_len == 0 ) /* Meh. Empty messages aren't very interesting, no matter how much some servers love to send them. */ return; s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */ nick = strchr( s, '/' ); if( nick ) { /* If this message included a resource/nick we don't know, we might still know the groupchat itself. */ *nick = 0; chat = jabber_chat_by_jid( ic, s ); *nick = '/'; nick ++; } else { /* message.c uses the EXACT_JID option, so bud should always be NULL here for bare JIDs. */ chat = jabber_chat_by_jid( ic, s ); } if( nick == NULL ) { /* This is fine, the groupchat itself isn't in jd->buddies. */ if( chat ) imcb_chat_log( chat, "From conference server: %s", body->text ); else imcb_log( ic, "System message from unknown groupchat %s: %s", s, body->text ); } else { /* This can happen too, at least when receiving a backlog when just joining a channel. */ if( chat ) imcb_chat_log( chat, "Message from unknown participant %s: %s", nick, body->text ); else imcb_log( ic, "Groupchat message from unknown JID %s: %s", s, body->text ); } return; } else if( chat == NULL ) { /* How could this happen?? We could do kill( self, 11 ) now or just wait for the OS to do it. :-) */ return; } if( subject ) { s = strchr( bud->ext_jid, '/' ); if( s ) *s = 0; imcb_chat_topic( chat, bud->ext_jid, subject->text_len > 0 ? subject->text : NULL, jabber_get_timestamp( node ) ); if( s ) *s = '/'; } if( body && body->text_len > 0 ) { s = strchr( bud->ext_jid, '/' ); if( s ) *s = 0; imcb_chat_msg( chat, bud->ext_jid, body->text, 0, jabber_get_timestamp( node ) ); if( s ) *s = '/'; } }