Permalink
Browse files

porzadki...

  • Loading branch information...
darkjames
darkjames committed Oct 21, 2008
1 parent 40ddd58 commit 6f71e39fb6f3f06e34fa049b341de9597f11878c
Showing with 79 additions and 161 deletions.
  1. +5 −82 dynstuff.c
  2. +1 −6 dynstuff.h
  3. +4 −6 msgqueue.c
  4. +49 −9 protocol.c
  5. +16 −50 usg.c
  6. +4 −0 usg.h
  7. +0 −6 xmalloc.c
  8. +0 −2 xmalloc.h
@@ -220,38 +220,6 @@ string_t string_init(const char *value) {
return tmp;
}

int string_append_c(string_t s, char c)
{
if (!s) {
errno = EFAULT;
return -1;
}

string_realloc(s, s->len + 1);

s->str[s->len + 1] = 0;
s->str[s->len++] = c;

return 0;
}

int string_append_n(string_t s, const char *str, int count)
{
if (!s || !str || count < 0) {
errno = EFAULT;
return -1;
}

string_realloc(s, s->len + count);

s->str[s->len + count] = 0;
strncpy(s->str + s->len, str, count);

s->len += count;

return 0;
}

int string_append_raw(string_t s, const char *str, int count)
{
if (!s || !str || count < 0) {
@@ -269,16 +237,6 @@ int string_append_raw(string_t s, const char *str, int count)
return 0;
}

int string_append(string_t s, const char *str)
{
if (!s || !str) {
errno = EFAULT;
return -1;
}

return string_append_n(s, str, strlen(str));
}

void string_remove(string_t s, int count)
{
if (!s || count <= 0)
@@ -296,46 +254,11 @@ void string_remove(string_t s, int count)
}
}

char *string_free(string_t s, int free_string)
void string_free(string_t s)
{
char *tmp = NULL;

if (!s)
return NULL;

if (free_string)
xfree(s->str);
else
tmp = s->str;

xfree(s);

return tmp;
}

/*
* itoa()
*
* prosta funkcja, która zwraca tekstow± reprezentacjê liczby. w obrêbie
* danego wywo³ania jakiej¶ funkcji lub wyra¿enia mo¿e byæ wywo³ania 10
* razy, poniewa¿ tyle mamy statycznych buforów. lepsze to ni¿ ci±g³e
* tworzenie tymczasowych buforów na stosie i sprintf()owanie.
*
* - i - liczba do zamiany.
*
* zwraca adres do bufora, którego _NIE_NALE¯Y_ zwalniaæ.
*/
const char *itoa(long int i)
{
static char bufs[10][16];
static int index = 0;
char *tmp = bufs[index++];

if (index > 9)
index = 0;

snprintf(tmp, 16, "%ld", i);

return tmp;
if (s) {
free(s->str);
free(s);
}
}

@@ -45,15 +45,10 @@ struct string {
typedef struct string * string_t;

struct string *string_init(const char *str);
int string_append(struct string *s, const char *str);
int string_append_n(struct string *s, const char *str, int count);
int string_append_c(struct string *s, char ch);
int string_append_raw(string_t s, const char *str, int count);
void string_remove(string_t s, int count);
char *string_free(struct string *s, int free_string);
void string_free(string_t s);

/* rozszerzenia libców */

const char *itoa(long int i);

#endif /* __DYNSTUFF_H */
@@ -16,8 +16,7 @@ int enqueue_message(int recipient, int sender, int seq, int msgclass, char *text
char buf[100];
int fd;

snprintf(buf, sizeof(buf), "queue/%d", recipient);
mkdir(buf, 0700);
mkdir(path_uin("queue", recipient), 0700);
snprintf(buf, sizeof(buf), "queue/%d/%.10ld-%d-%d-%d", recipient, time(NULL), sender, seq, msgclass);
if ((fd = open(buf, O_WRONLY | O_CREAT, 0600)) == -1)
return -1;
@@ -34,15 +33,14 @@ int enqueue_message(int recipient, int sender, int seq, int msgclass, char *text
int unqueue_message(int uin, msgqueue_t *m)
{
DIR *d;
char buf[100];
struct dirent *de;
int fd;

snprintf(buf, sizeof(buf), "queue/%d", uin);
if (!(d = opendir(buf)))
if (!(d = opendir(path_uin("queue", uin))))
return -1;

while ((de = readdir(d))) {
char buf[100];
struct stat st;

snprintf(buf, sizeof(buf), "queue/%d/%s", uin, de->d_name);
@@ -62,7 +60,7 @@ int unqueue_message(int uin, msgqueue_t *m)

m->text = xmalloc(st.st_size);
if (read(fd, m->text, st.st_size) < st.st_size) {
xfree(m->text);
free(m->text);
closedir(d);
return -1;
}
@@ -34,6 +34,41 @@
#include "auth.h"
#include "msgqueue.h"

client_t *get_client(client_t *c, int uin) {
client_t *f;

if (uin == c->uin)
return NULL;

/* jeśli nie jest połączony, zobacz, czy nie zostawił opisu */
if (!(f = find_client(uin))) {
static client_t dummy;
FILE *fd;

memset(&dummy, 0, sizeof(dummy));

f = &dummy;
f->uin = uin;
f->status = GG_STATUS_NOT_AVAIL;
f->status_descr = NULL;

if ((fd = fopen(path_uin("reasons", uin), "r"))) {
static char descr_buf[300];

if (fgets(descr_buf, sizeof(descr_buf), fd)) {
f->status = GG_STATUS_NOT_AVAIL_DESCR;
f->status_descr = descr_buf;
}
fclose(fd);
}
}

if (f->status == GG_STATUS_NOT_AVAIL || f->status == GG_STATUS_INVISIBLE)
return NULL;

return f;
}

static void gg77_notify_reply_data(client_t *ten, int uid) {
struct gg_header h;
struct gg_notify_reply77 n;
@@ -44,6 +79,10 @@ static void gg77_notify_reply_data(client_t *ten, int uid) {
if (!(c = get_client(ten, uid)))
return;

if (c->status_private) {
/* XXX */
}

if (c->status == GG_STATUS_INVISIBLE)
status = GG_STATUS_NOT_AVAIL;
else if (c->status == GG_STATUS_INVISIBLE_DESCR)
@@ -95,15 +134,19 @@ static void gg77_status_write(client_t *ten, client_t *c) {

int status = c->status;

h.type = GG_STATUS77;
h.length = sizeof(s) + ((c->status_descr) ? strlen(c->status_descr) : 0);
if (c->status_private) {
/* XXX */
}

if (status == GG_STATUS_INVISIBLE)
status = GG_STATUS_NOT_AVAIL;

if (status == GG_STATUS_INVISIBLE_DESCR)
status = GG_STATUS_NOT_AVAIL_DESCR;

h.type = GG_STATUS77;
h.length = sizeof(s) + ((c->status_descr) ? strlen(c->status_descr) : 0);

s.uin = c->uin;
s.status = status;
s.remote_ip = c->ip; /* XXX */
@@ -206,7 +249,7 @@ static void gg_login_ok(client_t *c, uint32_t uin) {

c->msg_send(c, &m);

xfree(m.text);
free(m.text);
}
}

@@ -468,7 +511,6 @@ static int gg_notify_remove_handler(client_t *c, void *data, uint32_t len) {
static int gg_new_status_handler(client_t *c, void *data, uint32_t len) {
struct gg_new_status *s = (struct gg_new_status *) data;
int status;
char buf[100];

status = s->status;

@@ -488,7 +530,7 @@ static int gg_new_status_handler(client_t *c, void *data, uint32_t len) {
c->status = status;

if (c->status_descr) {
xfree(c->status_descr);
free(c->status_descr);
c->status_descr = NULL;
}
if (len > sizeof(*s)) {
@@ -499,17 +541,15 @@ static int gg_new_status_handler(client_t *c, void *data, uint32_t len) {

printf("status change: uin=%d, status=%d, descr=\"%s\"\n", c->uin, c->status, c->status_descr);

snprintf(buf, sizeof(buf), "reasons/%d", c->uin);

if (c->status == GG_STATUS_NOT_AVAIL_DESCR) {
FILE *f;

if ((f = fopen(buf, "w"))) {
if ((f = fopen(path_uin("reasons", c->uin), "w"))) {
fputs(c->status_descr, f);
fclose(f);
}
} else
unlink(buf);
unlink(path_uin("reasons", c->uin));

changed_status(c);

Oops, something went wrong.

0 comments on commit 6f71e39

Please sign in to comment.