1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Added basic online list

This commit is contained in:
James Booth 2012-03-08 00:46:24 +00:00
parent 03b0c01849
commit 78bd151c48
9 changed files with 213 additions and 15 deletions

View File

@ -5,8 +5,8 @@ TESTLIB = -L ~/lib -l headunit
CPPLIB = -lstdc++
CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS)
OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \
profanity.o util.o command.o history.o main.o
TESTOBJS = test_history.o history.o
profanity.o util.o command.o history.o contact_list.o main.o
TESTOBJS = test_history.o history.o test_contact_list.o contact_list.o
profanity: $(OBJS)
$(CC) -o profanity $(OBJS) $(LIBS)
@ -16,14 +16,16 @@ windows.o: windows.h util.h
title_bar.o: windows.h
status_bar.o: windows.h util.h
input_win.o: windows.h
jabber.o: jabber.h log.h windows.h
jabber.o: jabber.h log.h windows.h contact_list.h
profanity.o: log.h windows.h jabber.h command.h history.h
util.o: util.h
command.o: command.h util.h history.h
command.o: command.h util.h history.h contact_list.h
history.o: history.h
contact_list.o: contact_list.h
main.o: profanity.h
test_history.o: history.h
test_contact_list.o: contact_list.h
testsuite: testsuite.h $(TESTOBJS)
$(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB)

View File

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <ncurses.h>
#include "command.h"
#include "contact_list.h"
#include "history.h"
#include "jabber.h"
#include "windows.h"
@ -33,6 +34,7 @@ static int _handle_command(char *command, char *inp);
static int _cmd_quit(void);
static int _cmd_help(void);
static int _cmd_who(void);
static int _cmd_pres(void);
static int _cmd_connect(char *inp);
static int _cmd_msg(char *inp);
static int _cmd_close(char *inp);
@ -72,6 +74,8 @@ static int _handle_command(char *command, char *inp)
result = _cmd_help();
} else if (strcmp(command, "/who") == 0) {
result = _cmd_who();
} else if (strcmp(command, "/pres") == 0) {
result = _cmd_pres();
} else if (strcmp(command, "/msg") == 0) {
result = _cmd_msg(inp);
} else if (strcmp(command, "/close") == 0) {
@ -142,6 +146,19 @@ static int _cmd_who(void)
return TRUE;
}
static int _cmd_pres(void)
{
struct contact_list *list = get_contact_list();
int i;
for (i = 0; i < list->size; i++) {
char *contact = list->contacts[i];
cons_show(contact);
}
return TRUE;
}
static int _cmd_msg(char *inp)
{
char *usr = NULL;

103
contact_list.c Normal file
View File

@ -0,0 +1,103 @@
#include <stdlib.h>
#include <string.h>
#include "contact_list.h"
// internal contact list
struct _contact_t {
char *contact;
struct _contact_t *next;
int last;
};
// the contact list
static struct _contact_t *_contact_list = NULL;
void contact_list_clear(void)
{
if (_contact_list == NULL) {
return;
}
else {
struct _contact_t *curr = _contact_list;
while(!curr->last) {
free(curr->contact);
curr = curr->next;
}
free(_contact_list);
}
_contact_list = NULL;
}
int contact_list_add(char *contact)
{
if (_contact_list == NULL) {
// create the new one
_contact_list = (struct _contact_t *) malloc(sizeof(struct _contact_t));
_contact_list->contact =
(char *) malloc((strlen(contact)+1) * sizeof(char));
strcpy(_contact_list->contact, contact);
_contact_list->last = 1;
// complete the circle
_contact_list->next = _contact_list;
} else {
// go through entries
struct _contact_t *curr = _contact_list;
while (!curr->last) {
// if already there, return without adding
if (strcmp(curr->contact, contact) == 0)
return 1;
// move on
curr = curr->next;
}
// create the new one
struct _contact_t *new =
(struct _contact_t *) malloc(sizeof(struct _contact_t));
new->contact = (char *) malloc((strlen(contact)+1) * sizeof(char));
strcpy(new->contact, contact);
new->last = 1;
// point the old one to it
curr->next = new;
curr->last = 0;
}
return 0;
}
struct contact_list *get_contact_list(void)
{
struct contact_list *list =
(struct contact_list *) malloc(sizeof(struct contact_list));
if (_contact_list == NULL) {
list->contacts = NULL;
list->size = 0;
return list;
} else {
struct _contact_t *curr = _contact_list;
list->contacts = (char **) malloc(sizeof(char **));
int count = 0;
while(1) {
list->contacts[count] =
(char *) malloc((strlen(curr->contact)+1) * sizeof(char));
strcpy(list->contacts[count], curr->contact);
count++;
if (curr->last)
break;
else {
curr = curr->next;
}
}
list->size = count;
return list;
}
}

13
contact_list.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef CONTACT_LIST_H
#define CONTACT_LIST_H
struct contact_list {
char **contacts;
int size;
};
void contact_list_clear(void);
int contact_list_add(char *contact);
struct contact_list *get_contact_list(void);
#endif

View File

@ -25,6 +25,7 @@
#include "jabber.h"
#include "log.h"
#include "contact_list.h"
#include "windows.h"
#define PING_INTERVAL 120000 // 2 minutes
@ -310,10 +311,12 @@ static int _jabber_presence_handler(xmpp_conn_t * const conn,
else
status_str = NULL;
if (type == NULL) // online
if (type == NULL) {// online
win_contact_online(short_from, show_str, status_str);
else // offline
contact_list_add(short_from);
} else {// offline
win_contact_offline(short_from, show_str, status_str);
}
win_page_off();

58
test_contact_list.c Normal file
View File

@ -0,0 +1,58 @@
#include <head-unit.h>
#include "contact_list.h"
static void beforetest(void)
{
contact_list_clear();
}
static void empty_list_when_none_added(void)
{
struct contact_list *list = get_contact_list();
assert_int_equals(0, list->size);
}
static void contains_one_element(void)
{
contact_list_add("James");
struct contact_list *list = get_contact_list();
assert_int_equals(1, list->size);
}
static void first_element_correct(void)
{
contact_list_add("James");
struct contact_list *list = get_contact_list();
assert_string_equals("James", list->contacts[0]);
}
static void contains_two_elements(void)
{
contact_list_add("James");
contact_list_add("Dave");
struct contact_list *list = get_contact_list();
assert_int_equals(2, list->size);
}
static void first_and_second_elements_correct(void)
{
contact_list_add("James");
contact_list_add("Dave");
struct contact_list *list = get_contact_list();
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
}
void register_contact_list_tests(void)
{
TEST_MODULE("contact_list tests");
BEFORETEST(beforetest);
TEST(empty_list_when_none_added);
TEST(contains_one_element);
TEST(first_element_correct);
TEST(contains_two_elements);
TEST(first_and_second_elements_correct);
}

View File

@ -29,7 +29,7 @@ static void append_then_previous_returns_appended(void)
{
history_append("try append");
char *prev = history_previous();
assert_string_equals(prev, "try append");
assert_string_equals("try append", prev);
}
static void append_then_next_returns_null(void)
@ -67,7 +67,7 @@ static void navigate_to_correct_item(void)
history_previous(); // cmd2
char *str = history_next(); // cmd3
assert_string_equals(str, "cmd3");
assert_string_equals("cmd3", str);
}
static void append_previous_item(void)
@ -91,25 +91,25 @@ static void append_previous_item(void)
history_append(str);
char *cmd3_1 = history_previous();
assert_string_equals(cmd3_1, "cmd3");
assert_string_equals("cmd3", cmd3_1);
char *cmd6 = history_previous();
assert_string_equals(cmd6, "cmd6");
assert_string_equals("cmd6", cmd6);
char *cmd5 = history_previous();
assert_string_equals(cmd5, "cmd5");
assert_string_equals("cmd5", cmd5);
char *cmd4 = history_previous();
assert_string_equals(cmd4, "cmd4");
assert_string_equals("cmd4", cmd4);
char *cmd3 = history_previous();
assert_string_equals(cmd3, "cmd3");
assert_string_equals("cmd3", cmd3);
char *cmd2 = history_previous();
assert_string_equals(cmd2, "cmd2");
assert_string_equals("cmd2", cmd2);
char *cmd1 = history_previous();
assert_string_equals(cmd1, "cmd1");
assert_string_equals("cmd1", cmd1);
char *end = history_previous();
assert_is_null(end);

View File

@ -4,6 +4,7 @@
int main(void)
{
register_history_tests();
register_contact_list_tests();
run_suite();
return 0;
}

View File

@ -2,5 +2,6 @@
#define TESTSUITE_H
void register_history_tests(void);
void register_contact_list_tests(void);
#endif