Commit 5563bfc9 authored by stettberger's avatar stettberger

Code cleanups

parent bc338540
......@@ -5,8 +5,8 @@ TARGET=si
SILCINCLUDE=/usr/include/silc-toolkit
SILCLIB=/usr/lib/silc-toolkit
FILES=si.c mit.c mm.c buddylist.c
CFLAGS=`pkg-config --cflags silcclient` -Wall -ggdb
LDFLAGS=`pkg-config --libs silcclient` -lpthread -ldl
CFLAGS=`pkg-config --cflags silc silcclient` -Wall -ggdb -DSILC_DIST_SKR -DSILC_DIST_TMA
LDFLAGS=`pkg-config --libs silc silcclient` -lpthread -ldl
all: ${TARGET}
......
......@@ -40,7 +40,9 @@ void add_pubkey_callback(SilcClient client, SilcClientConnection conn,
SilcStatus status, SilcDList clients,
void *context);
void refresh_buddylist() {
void
refresh_buddylist()
{
int i;
for (i=0; i < MAX_CONNS; i++){
if(!buddy_conns[i])
......@@ -51,10 +53,12 @@ void refresh_buddylist() {
}
void
add_buddy(SilcClientEntry client) {
add_buddy(SilcClientEntry client)
{
Buddy new = calloc(1, sizeof (Buddy)), tmp;
NULL_TEST(new);
new->client = client;
silc_client_ref_client(silc_client->client, silc_client->conn, client);
for (tmp = buddies; tmp; tmp = tmp->next)
if (tmp->client == client)
return;
......@@ -66,7 +70,10 @@ add_buddy(SilcClientEntry client) {
else
buddies = new;
}
void rm_buddy(SilcClientEntry client){
void
rm_buddy(SilcClientEntry client)
{
Buddy del, tmp;
if (buddies && buddies->client == client){
buddies = buddies->next;
......@@ -81,22 +88,14 @@ void rm_buddy(SilcClientEntry client){
}
}
void add_pubkey(char *pubkey) {
//FIXME
void
add_pubkey(char *pubkey)
{
SilcBuffer attr=NULL;
SilcAttributeObjPk obj;
SilcPublicKey pk;
/* Request all attributes except PUBKEY */
attr = silc_client_attributes_request(SILC_ATTRIBUTE_USER_INFO,
SILC_ATTRIBUTE_SERVICE,
SILC_ATTRIBUTE_STATUS_MOOD,
SILC_ATTRIBUTE_STATUS_FREETEXT,
SILC_ATTRIBUTE_STATUS_MESSAGE,
SILC_ATTRIBUTE_PREFERRED_LANGUAGE,
SILC_ATTRIBUTE_PREFERRED_CONTACT,
SILC_ATTRIBUTE_TIMEZONE,
SILC_ATTRIBUTE_GEOLOCATION,
SILC_ATTRIBUTE_DEVICE_INFO, 0);
attr = silc_client_attributes_request(SILC_ATTRIBUTE_DEVICE_INFO, 0);
/* Load the Public Key */
if (!silc_pkcs_load_public_key(pubkey, &pk)) {
......@@ -120,25 +119,30 @@ void add_pubkey(char *pubkey) {
silc_free(tmp);
}
void add_pubkey_callback(SilcClient client, SilcClientConnection conn,
SilcStatus status, SilcDList clients,
void *context){
void
add_pubkey_callback(SilcClient client, SilcClientConnection conn,
SilcStatus status, SilcDList clients,
void *context)
{
SilcClientEntry entry;
if (!clients) return;
silc_dlist_start(clients);
while ((entry = (SilcClientEntry)silc_dlist_get(clients))
!= SILC_LIST_END)
add_buddy(entry);
}
void update_buddylist() {
void
update_buddylist()
{
/* Update the buddylist itself by the Pubkey's in $HOME/.silc/buddylist*/
struct dirent *entry;
char *path;
silc_asprintf(&path, "%s/.silc/buddylist", (char *)getenv("HOME"));
create_dirtree(path);
DIR *dir=opendir(path);
while((entry=readdir(dir))) {
if(entry->d_name[0]=='.')
DIR *dir = opendir(path);
while((entry = readdir(dir))) {
if(entry->d_name[0] == '.')
continue;
char *pubkey;
silc_asprintf(&pubkey, "%s/%s", path, entry->d_name);
......@@ -149,15 +153,17 @@ void update_buddylist() {
free(path);
}
void init_buddylist() {
void
init_buddylist()
{
struct sockaddr_un strAddr;
socklen_t lenAddr;
int yes=1;
int i=0;
while(i<MAX_CONNS){
buddy_conns[i]=0;
while(i < MAX_CONNS){
buddy_conns[i] = 0;
i++;
}
......@@ -192,7 +198,8 @@ void init_buddylist() {
si_handle_buddyfd_input, &strAddr);
}
SILC_TASK_CALLBACK(si_handle_buddyfd_input) {
SILC_TASK_CALLBACK(si_handle_buddyfd_input)
{
int conn=0;
socklen_t lenAddr;
int i;
......@@ -202,58 +209,58 @@ SILC_TASK_CALLBACK(si_handle_buddyfd_input) {
perror("si: couldn't accept connection on buddylist");
return;
}
for(i=0; i < MAX_CONNS; i++)
if(!buddy_conns[i]){
for(i = 0; i < MAX_CONNS; i++)
if(! buddy_conns[i]){
buddy_conns[i] = conn;
break;
}
DEBUG("New Connection on Buddylist");
}
void si_handle_buddy_conn_input(int fd) {
int pos=0, len=1;
char buf[1], *data=malloc(len);
void
si_handle_buddy_conn_input(int fd)
{
int pos = 0, len = 1;
char buf[1], *data = emalloc(len);
int count=0;
NULL_TEST(data);
data[0]=0;
while(buf[0]!='\n' && (count = read(fd, buf, 1)) > 0){
len+=count;
data=realloc(data, len);
while(buf[0] != '\n' && (count = read(fd, buf, 1)) > 0){
len += count;
data = realloc(data, len);
NULL_TEST(data);
data=strcat(data, buf);
data = strcat(data, buf);
}
if(count <= 0) {
DEBUG("Closed connection on Buddylist");
pos=0;
while(pos<MAX_CONNS) {
if(buddy_conns[pos]==fd)
buddy_conns[pos]=0;
while(pos < MAX_CONNS) {
if(buddy_conns[pos] == fd)
buddy_conns[pos] = 0;
pos++;
}
close(fd);
return;
}
char *p=strchr(data, '\n');
p[0]=0;
char *p = strchr(data, '\n');
p[0] = 0;
/* Proc data */
if(!strncmp("getbuddylist", data, 12)) {
if(! strncmp("getbuddylist", data, 12))
send_buddy_list(fd);
}
free(data);
silc_free(data);
}
void send_buddy_list(int fd) {
void
send_buddy_list(int fd)
{
Buddy tmp;
char *umode, *line;
for(tmp=buddies; tmp; tmp=tmp->next) {
umode=silc_client_umode(tmp->client->mode);
line=malloc(strlen(umode)+strlen(tmp->client->nickname)+3);
NULL_TEST(line);
sprintf(line, "%s,%s\n", umode, tmp->client->nickname);
for(tmp = buddies; tmp; tmp = tmp->next) {
umode = silc_client_umode(tmp->client->mode);
silc_asprintf(&line, "%s,%s\n", umode, tmp->client->nickname);
write(fd, line, strlen(line));
free(line);
free(umode);
silc_free(line);
silc_free(umode);
fsync(fd);
}
write(fd, "END,nouser\n", 11);
......
......@@ -24,14 +24,18 @@
static char *lower(char *s) {
static char *
lower(char *s)
{
char *p;
for(p = s; p && *p; p++) *p = tolower(*p);
return s;
}
/* creates directories top-down, if necessary */
static void create_dirtree(const char *dir) {
static void
create_dirtree(const char *dir)
{
char tmp[256];
char *p;
size_t len;
......@@ -49,7 +53,8 @@ static void create_dirtree(const char *dir) {
mkdir(tmp, S_IRWXU);
}
static int get_filepath(char *filepath, size_t len, char *channel, char *file)
static int
get_filepath(char *filepath, size_t len, char *channel, char *file)
{
if(strlen(channel)!=0) {
if(!snprintf(filepath, len, "%s/%s", ircdir, lower(channel)))
......@@ -60,7 +65,8 @@ static int get_filepath(char *filepath, size_t len, char *channel, char *file)
return snprintf(filepath, len, "%s/%s", ircdir, file);
}
static void create_filepath(char *filepath, size_t len, char *channel, char *suffix)
static void
create_filepath(char *filepath, size_t len, char *channel, char *suffix)
{
if(!get_filepath(filepath, len, channel, suffix)) {
fprintf(stderr, "%s", "ii: path to irc directory too long\n");
......@@ -68,7 +74,8 @@ static void create_filepath(char *filepath, size_t len, char *channel, char *suf
}
}
static int open_channel(char *name)
static int
open_channel(char *name)
{
static char infile[256];
create_filepath(infile, sizeof(infile), name, "in");
......@@ -77,7 +84,8 @@ static int open_channel(char *name)
return open(infile, O_RDONLY | O_NONBLOCK, 0);
}
static int read_line(int fd, size_t res_len, char *buf)
static int
read_line(int fd, size_t res_len, char *buf)
{
size_t i = 0;
char c;
......@@ -91,7 +99,8 @@ static int read_line(int fd, size_t res_len, char *buf)
return 0;
}
static void rm_channel(Channel *c)
static void
rm_channel(Channel *c)
{
Channel *p;
if(channels == c) {
......@@ -106,7 +115,8 @@ static void rm_channel(Channel *c)
free(c);
}
static void handle_channels_input(Channel *c)
static void
handle_channels_input(Channel *c)
{
static char buf[PIPE_BUF];
......@@ -121,7 +131,8 @@ static void handle_channels_input(Channel *c)
proc_channels_input(c, buf);
}
static void add_channel(SilcChannelEntry channel)
static void
add_channel(SilcChannelEntry channel)
{
Channel *c;
int fd;
......@@ -143,22 +154,24 @@ static void add_channel(SilcChannelEntry channel)
channels = c;
}
c->fd = fd;
c->silc=channel;
c->silc = channel;
c->silc->context = c;
}
void rm_query(Query *peer) {
void
rm_query(Query *peer)
{
Query *q;
if (peer==queries) {
if (peer == queries) {
peer->silc->context = NULL;
queries=queries->next;
queries = queries->next;
}
else
for (q=queries; q; q=q->next)
for (q = queries; q; q = q->next)
if (SILC_ID_CLIENT_COMPARE(&peer->silc->id,
&q->next->silc->id)) {
q->silc->context = NULL;
q->next=peer->next;
q->next = peer->next;
}
free(peer);
}
......@@ -181,7 +194,9 @@ static void handle_query_input(Query *peer)
proc_queries_input(peer, buf);
}
void add_query(SilcClientEntry peer) {
void
add_query(SilcClientEntry peer)
{
Query *q;
int fd;
char *path;
......@@ -189,57 +204,62 @@ void add_query(SilcClientEntry peer) {
if (peer->context)
return; /* Already handled */
path=malloc(strlen(peer->nickname)+8);
NULL_TEST_ARG(path,exit(1));
sprintf(path, "query/%s", peer->nickname);
fd=open_channel(path);
silc_asprintf(&path, "query/%s", peer->nickname);
fd = open_channel(path);
if(!fd){
perror("si: Couldn't create Query");
return;
}
q=calloc(1, sizeof(Query));
q = calloc(1, sizeof(Query));
NULL_TEST_ARG(q, exit(1));
/* Write to the query */
print_out(path, "-!- You opened the query");
if (!queries) {
queries=q;
queries = q;
}
else {
q->next=queries;
queries=q;
q->next = queries;
queries = q;
}
q->silc=peer;
q->fd=fd;
q->silc = peer;
q->fd = fd;
q->silc->context = q;
silc_client_ref_client(silc_client->client, silc_client->conn, peer);
}
void add_transfer(int num, SilcUInt32 id, SilcClientEntry sender, int type) {
void
add_transfer(int num, SilcUInt32 id, SilcClientEntry sender, int type)
{
Transfer *t, *tmp;
t=calloc(1, sizeof(Transfer));
t = calloc(1, sizeof(Transfer));
NULL_TEST(t);
if(!transfers)
transfers=t;
transfers = t;
else{
tmp=transfers;
tmp = transfers;
while(tmp->next)
tmp=tmp->next;
tmp->next=t;
tmp = tmp->next;
tmp->next = t;
}
t->number=num;
t->id=id;
t->silc=sender;
t->type=type;
t->status=FILE_STATUS_WAIT;
t->number = num;
t->id = id;
t->silc = sender;
t->type = type;
t->status = FILE_STATUS_WAIT;
}
void rm_transfer(Transfer *t) {
void
rm_transfer(Transfer *t)
{
Transfer *tmp;
if (t==transfers) {
transfers=transfers->next;
if (t == transfers) {
transfers = transfers->next;
}
else
for (tmp=transfers; tmp; tmp=tmp->next)
if(tmp->next->id==t->id)
tmp->next=t->next;
for (tmp = transfers; tmp; tmp = tmp->next)
if(tmp->next->id == t->id)
tmp->next = t->next;
free(t);
}
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment