summaryrefslogtreecommitdiff
path: root/twit.c
diff options
context:
space:
mode:
Diffstat (limited to 'twit.c')
-rw-r--r--twit.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/twit.c b/twit.c
new file mode 100644
index 0000000..74b61e1
--- /dev/null
+++ b/twit.c
@@ -0,0 +1,168 @@
+#include "twit.h"
+
+#ifdef CONFIG_TWIT
+
+int bbs_twit( term_screen *ts, char *fn )
+{
+ int ret = 0;
+ char *fname = CONFIG_TWIT_DEFAULT_FILE;
+ int menu_input = 0;
+ char menu_cmd;
+ int row;
+ int quit_loop = 0;
+
+ if ( fn != NULL )
+ fname = fn;
+
+ List *twit_list = llist_new();
+ f_file *file = f_file_open( fname, F_FILE_READ );
+ if ( file != NULL )
+ {
+ const int l_s = 128;
+ int r_v;
+ {
+ cycle0:;
+ char *l = malloc( l_s );
+ r_v = f_file_readl( file, l_s, l );
+ if ( r_v < 0 ) goto exit_cycle0;
+ l[r_v] = '\0';
+ llist_push( twit_list, l );
+ goto cycle0;
+ exit_cycle0:;
+ if ( l != NULL ) free( l );
+ }
+
+ }
+ f_file_close( file );
+
+ //log that someone use twit
+ bbs_log( NULL, "visited TWIT" );
+
+ while( (quit_loop == 0) )
+ {
+ term_clr_scr( ts );
+ term_cur_set_c( ts, 0 );
+ printf("(A)dd,(Q)uit"); fflush( stdout );
+
+ //dispaly todo list
+ row = term_get_maxrow( ts );
+ term_cur_set_c( ts, 0 );
+ {
+ struct ListNode *iter=twit_list->first;
+ int cnt = 1;
+ while ( (iter != NULL) || ( cnt > row - 2))
+ {
+ term_cur_set_c( ts, 0 );
+ term_cur_set_r( ts, 1+cnt );
+ printf( "[%02d] -> %s\n", cnt, (char *)iter->val);
+ cnt += 1;
+ iter = iter->next;
+ }
+ }
+
+
+ term_cur_set_c( ts, 0 );
+ term_cur_set_r( ts, term_get_maxrow( ts ) );
+ printf(":"); fflush( stdout );
+
+ menu_input = term_getc( ts );
+ //if something whent wrong dont know why, need to get some test case
+ if ( menu_input == -1 )
+ continue;
+ menu_cmd = (char)menu_input;
+
+ switch( menu_cmd )
+ {
+ case 'q':
+ case 'Q':
+ quit_loop = 1;
+ break;
+
+ case 'a':
+ case 'A':
+ bbs_twit_add( ts, twit_list, fname );
+ break;
+
+ default:
+ printf("Try more\n");
+ }
+ }
+
+ llist_free( twit_list );
+
+ return ret;
+}
+
+int bbs_twit_add( term_screen *ts, List *twit, const char *fname)
+{
+ int ret = -1;
+ int fret = -1;
+ const int buf_size = 64;
+ char buf[buf_size];
+
+ if ( ts == NULL )
+ return -1;
+
+ if ( fname == NULL )
+ return -1;
+
+ term_clr_scr( ts );
+ term_cur_set_c( ts, 0 );
+ term_cur_set_r( ts, 0 );
+ printf("Put new twit:\n"); fflush( stdout );
+
+ memset( buf, 0, buf_size );
+ term_cur_set_c( ts, 0 );
+ fret = term_readline( ts, buf, buf_size, READLINE_ALPHA );
+ if ( fret > 0 )
+ {
+ char *l = malloc( fret+1 );
+ memcpy( l, buf, fret );
+ l[fret]='\0';
+ llist_push( twit, l );
+ bbs_twit_save( fname, twit );
+ ret = 0;
+ } else
+ {
+ return -1;
+ }
+
+ if ( ret == 0 )
+ if ( llist_length(twit) > TWIT_MAX_MESSAGES )
+ {
+ char *ptr = llist_popf( twit );
+ if ( ptr != NULL )
+ free( ptr );
+ }
+
+ return ret;
+}
+
+int bbs_twit_save( const char *fname, List *twit )
+{
+ int ret=-1;
+
+ f_file *f=NULL;
+ struct ListNode *iter=twit->first;
+
+ f = f_file_open( fname, F_FILE_WRITE );
+
+ while ( iter != NULL )
+ {
+ size_t str_size=strlen( iter->val )+2;
+ char *str = malloc( str_size );
+ memcpy( str, iter->val, str_size-2);
+ str[str_size-1] = '\0';
+ str[str_size-2] = '\n';
+ f_file_write( f, strlen(str), str );
+ printf( "%s\n", str ); fflush( stdout );
+ free( str );
+ iter = iter->next;
+ }
+ f_file_close( f );
+
+ return ret;
+}
+
+
+#endif