comparison src/socket.c @ 3478:3da42b64304e

[gaim-migrate @ 3532] This isn't complete--but it's cool. gaim-remote is now built and installed. Eventually it will do lots of cool stuff--right now it just handles aim:// URI's. Try it out... when connected to OSCAR run: gaim-remote uri "aim://goim?screenname=seanegn&message=Good+job+Sean" Also, I made it so that if you're already running a Gaim session, and start a new one, it won't auto-login and disconnect all your accounts from the first instance. Useful if you accidentally hit the wrong button or something. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Fri, 30 Aug 2002 16:09:22 +0000
parents
children bb49f0c4f7cd
comparison
equal deleted inserted replaced
3477:db5dd21aa345 3478:3da42b64304e
1 /*
2 * gaim-remote
3 *
4 * Copyright (C) 2002, Sean Egan <bj91704@binghamton.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 /* Somewhat inspired by XMMS:
23 * Copyright (C) 1998-2002 Peter Alm, Mikael Alm, Olle Hallnas,
24 * Thomas Nilsson and 4Front Technologies
25 * Copyright (C) 1999-2002 Haavard Kvaalen
26 */
27
28 /* This provides code for connecting to a Gaim socket and communicating with
29 * it. It will eventually be made a library once the core and ui are split. */
30
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include "gaim.h"
34 #include "gaim-socket.h"
35
36 void cui_send_packet (int fd, struct gaim_cui_packet *p) {
37 int len = sizeof(p->type) + sizeof(p->subtype) + sizeof(p->length) + p->length;
38 char *pack = g_malloc(len);
39 char *a = pack;
40 memcpy (a, &(p->type), sizeof(p->type));
41 a = a + sizeof(p->type);
42 memcpy (a, &(p->subtype), sizeof(p->subtype));
43 a = a + sizeof(p->subtype);
44 memcpy (a, &(p->length), sizeof(p->length));
45 a = a + sizeof(p->length);
46 memcpy (a, p->data, p->length);
47 write(fd, pack, len);
48 g_free(pack);
49 }
50
51
52 void cui_packet_append_string(struct gaim_cui_packet *p, char *str) {
53 int len = p->length + strlen(str);
54 char *k = g_malloc(len);
55 memcpy(k, p->data, p->length);
56 memcpy(k + p->length, str, strlen(str));
57 if (p->data)
58 g_free(p->data);
59 p->data = k;
60 p->length = len;
61 }
62
63 void cui_packet_append_char(struct gaim_cui_packet *p, char c) {
64 int len = p->length + sizeof(char);
65 char *k = g_malloc(len);
66 memcpy(k, p->data, p->length);
67 k[p->length] = c;
68 if (p->data)
69 g_free(p->data);
70 p->data = k;
71 p->length = len;
72 }
73
74 void cui_packet_append_raw(struct gaim_cui_packet *p, char *str, int len) {
75 int lent = p->length + len;
76 char *k = g_malloc(lent);
77 memcpy(k, p->data, p->length);
78 memcpy(k + p->length, str, len);
79 if (p->data)
80 g_free(p->data);
81 p->data = k;
82 p->length = lent;
83 }
84
85 struct gaim_cui_packet *cui_packet_new(guchar type, guchar subtype) {
86 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
87 p->type = type;
88 p->subtype = subtype;
89 p->length = 0;
90 p->data = NULL;
91 return p;
92 }
93
94 void cui_packet_free(struct gaim_cui_packet *p) {
95 if (p->data)
96 g_free(p->data);
97 g_free(p);
98 }
99
100 struct gaim_cui_packet *cui_read_packet(int fd) {
101 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
102 char *data = NULL;
103
104 if (!(read(fd, p->type, sizeof(p->type)))) {
105 g_free(p);
106 return NULL;
107 }
108
109
110 if (!(read(fd, p->subtype, sizeof(p->subtype)))) {
111 g_free(p);
112 return NULL;
113 }
114
115
116 if (!(read(fd, p->length, sizeof(p->length)))) {
117 g_free(p);
118 return NULL;
119 }
120
121 if (p->length) {
122 data = g_malloc(p->length);
123 if (!(read(fd, data, p->length))) {
124 g_free(p);
125 return NULL;
126 }
127 }
128 p->data = data;
129 }
130
131 /* copied directly from xmms_connect_to_session */
132 gint gaim_connect_to_session(gint session)
133 {
134 gint fd;
135 uid_t stored_uid, euid;
136 struct sockaddr_un saddr;
137
138 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
139 {
140 saddr.sun_family = AF_UNIX;
141 stored_uid = getuid();
142 euid = geteuid();
143 setuid(euid);
144 sprintf(saddr.sun_path, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), session);
145 setreuid(stored_uid, euid);
146 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1)
147 return fd;
148 }
149 close(fd);
150 return -1;
151 }
152
153 gboolean gaim_session_exists(int sess)
154 {
155 struct gaim_cui_packet *pack = NULL;
156
157 int fd = gaim_connect_to_session(sess);
158 if (fd > 0) {
159 pack = cui_packet_new(CUI_TYPE_META, CUI_META_PING);
160 cui_send_packet(fd, pack);
161 cui_packet_free(pack);
162 close(fd);
163 } else {
164 return FALSE;
165 }
166 return TRUE;
167 }
168