summary refs log blame commit diff stats
path: root/src/common/hexchat.h
blob: 7143f8abff29f943ca90e90ad78f918d0009f0ae (plain) (tree)


















                                                                            


                               
                         
      
 
                 

                        

                                                         

                 
 






                                                     






                           

                            

                







                                                                 















                                                                         

                                    










                                                                                          
                              







































                                                                                              
                                                                                                                     





                             
                   
 
                                                                           
 
                      

                                          

                                        
                                         






                                             




                                             
                                              
                                       
                                        
                                        





                                          
                                          





                                         
                                             




                                        
                                       
                                        


                                           
                                           
                                         
                                         









                                              















                                               

                                         
                                      








                                          
                                             














                                                  












                                                   
 



























                                                                               
                                          


                                        
                               





                                   
                                 
                                  



                               
                                   






                                   
                               







                                                                                                              







                                                                                             


















                                            







                                                            
                                             

                         
 
                                                                                                    
                                 
 

















                                                                              








                                                                           








                                                        











                                                                   
                          




































                                                                                                            


                                                                                                      









                                                                             
                                             









                                                                                                  





                       



















                                                                                 
                                                                



















































                                                                                                                 
                                                                                       



























                                                                                                            
                                                                                  



                                                                                                   
                                                                                     








                                                                                
                                                                                






                                                                                              
                                                                                  

                                                                                              
                                       

                                                                                   
                                                                             

                                                               
                                                                     
                                                                     
                                                               
                                                                  
                                                                


                                                                                              


                                                                             





































                                                                                                  

                                                       

      
/* HexChat
 * Copyright (C) 1998-2010 Peter Zelezny.
 * Copyright (C) 2009-2013 Berke Viktor.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef WIN32
#include "../../config-win32.h"
#else
#include "../../config.h"
#endif

#include <glib.h>
#include <glib/gstdio.h>

#include <time.h>			/* need time_t */

#ifndef HEXCHAT_H
#define HEXCHAT_H

#ifdef USE_OPENSSL
#ifdef __APPLE__
#define __AVAILABILITYMACROS__
#define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
#endif
#endif

#include "history.h"

#ifndef HAVE_SNPRINTF
#define snprintf g_snprintf
#endif

#ifndef HAVE_VSNPRINTF
#define vsnprintf _vsnprintf
#endif

#ifdef USE_DEBUG
#define malloc(n) hexchat_malloc(n, __FILE__, __LINE__)
#define realloc(n, m) hexchat_realloc(n, m, __FILE__, __LINE__)
#define free(n) hexchat_dfree(n, __FILE__, __LINE__)
#define strdup(n) hexchat_strdup(n, __FILE__, __LINE__)
void *hexchat_malloc (int size, char *file, int line);
void *hexchat_strdup (char *str, char *file, int line);
void hexchat_dfree (void *buf, char *file, int line);
void *hexchat_realloc (char *old, int len, char *file, int line);
#endif

#ifdef SOCKS
#ifdef __sgi
#include <sys/time.h>
#define INCLUDE_PROTOTYPES 1
#endif
#include <socks.h>
#endif

#ifdef USE_OPENSSL
#include <openssl/ssl.h>		  /* SSL_() */
#endif

#ifdef __EMX__						  /* for o/s 2 */
#define OFLAGS O_BINARY
#define g_ascii_strcasecmp stricmp
#define g_ascii_strncasecmp strnicmp
#define PATH_MAX MAXPATHLEN
#define FILEPATH_LEN_MAX MAXPATHLEN
#endif

/* force a 32bit CMP.L */
#define CMPL(a, c0, c1, c2, c3) (a == (guint32)(c0 | (c1 << 8) | (c2 << 16) | (c3 << 24)))
#define WORDL(c0, c1, c2, c3) (guint32)(c0 | (c1 << 8) | (c2 << 16) | (c3 << 24))
#define WORDW(c0, c1) (guint16)(c0 | (c1 << 8))

#ifdef WIN32						/* for win32 */
#define OFLAGS O_BINARY
#define sleep(t) Sleep(t*1000)
#include <direct.h>
#define	F_OK	0
#define	X_OK	1
#define	W_OK	2
#define	R_OK	4
#ifndef S_ISDIR
#define	S_ISDIR(m)	((m) & _S_IFDIR)
#endif
#define NETWORK_PRIVATE
#else									/* for unix */
#define OFLAGS 0
#endif

#define FONTNAMELEN	127
#define PATHLEN		255
#define DOMAINLEN	100
#define NICKLEN		64				/* including the NULL, so 63 really */
#define CHANLEN		300
#define PDIWORDS		32
#define USERNAMELEN 10
#define HIDDEN_CHAR	8			/* invisible character for xtext */

#if defined(ENABLE_NLS) && !defined(_)
#  include <libintl.h>
#  define _(x) gettext(x)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#endif
#if !defined(_)
#  define N_(String) (String)
#  define _(x) (x)
#endif

struct nbexec
{
	int myfd;
	int childpid;
	int tochannel;						/* making this int keeps the struct 4-byte aligned */
	int iotag;
	char *linebuf;
	int buffill;
	struct session *sess;
};

struct hexchatprefs
{
	/* these are the rebranded, consistent, sorted hexchat variables */

	/* BOOLEANS */
	unsigned int hex_away_auto_unmark;
	unsigned int hex_away_omit_alerts;
	unsigned int hex_away_show_once;
	unsigned int hex_away_track;
	unsigned int hex_completion_auto;
	unsigned int hex_dcc_auto_chat;
	unsigned int hex_dcc_auto_resume;
	unsigned int hex_dcc_fast_send;
	unsigned int hex_dcc_ip_from_server;
	unsigned int hex_dcc_remove;
	unsigned int hex_dcc_save_nick;
	unsigned int hex_dcc_send_fillspaces;
	unsigned int hex_gui_autoopen_chat;
	unsigned int hex_gui_autoopen_dialog;
	unsigned int hex_gui_autoopen_recv;
	unsigned int hex_gui_autoopen_send;
	unsigned int hex_gui_compact;
	unsigned int hex_gui_focus_omitalerts;
	unsigned int hex_gui_hide_menu;
	unsigned int hex_gui_input_attr;
	unsigned int hex_gui_input_icon;
	unsigned int hex_gui_input_nick;
	unsigned int hex_gui_input_spell;
	unsigned int hex_gui_input_style;
	unsigned int hex_gui_join_dialog;
	unsigned int hex_gui_mode_buttons;
	unsigned int hex_gui_quit_dialog;
	/* unsigned int hex_gui_single; */
	unsigned int hex_gui_slist_fav;
	unsigned int hex_gui_slist_skip;
	unsigned int hex_gui_tab_chans;
	unsigned int hex_gui_tab_dialogs;
	unsigned int hex_gui_tab_dots;
	unsigned int hex_gui_tab_icons;
	unsigned int hex_gui_tab_scrollchans;
	unsigned int hex_gui_tab_server;
	unsigned int hex_gui_tab_sort;
	unsigned int hex_gui_tab_utils;
	unsigned int hex_gui_topicbar;
	unsigned int hex_gui_tray;
	unsigned int hex_gui_tray_away;
	unsigned int hex_gui_tray_blink;
	unsigned int hex_gui_tray_close;
	unsigned int hex_gui_tray_minimize;
	unsigned int hex_gui_tray_quiet;
	unsigned int hex_gui_ulist_buttons;
	unsigned int hex_gui_ulist_color;
	unsigned int hex_gui_ulist_count;
	unsigned int hex_gui_ulist_hide;
	unsigned int hex_gui_ulist_icons;
	unsigned int hex_gui_ulist_resizable;
	unsigned int hex_gui_ulist_show_hosts;
	unsigned int hex_gui_ulist_style;
	unsigned int hex_gui_usermenu;
	unsigned int hex_gui_win_modes;
	unsigned int hex_gui_win_save;
	unsigned int hex_gui_win_swap;
	unsigned int hex_gui_win_ucount;
	unsigned int hex_identd;
	unsigned int hex_input_balloon_chans;
	unsigned int hex_input_balloon_hilight;
	unsigned int hex_input_balloon_priv;
	unsigned int hex_input_beep_chans;
	unsigned int hex_input_beep_hilight;
	unsigned int hex_input_beep_priv;
	unsigned int hex_input_filter_beep;
	unsigned int hex_input_flash_chans;
	unsigned int hex_input_flash_hilight;
	unsigned int hex_input_flash_priv;
	unsigned int hex_input_perc_ascii;
	unsigned int hex_input_perc_color;
	unsigned int hex_input_tray_chans;
	unsigned int hex_input_tray_hilight;
	unsigned int hex_input_tray_priv;
	unsigned int hex_irc_auto_rejoin;
	unsigned int hex_irc_conf_mode;
	unsigned int hex_irc_hidehost;
	unsigned int hex_irc_hide_version;
	unsigned int hex_irc_invisible;
	unsigned int hex_irc_logging;
	unsigned int hex_irc_raw_modes;
	unsigned int hex_irc_servernotice;
	unsigned int hex_irc_skip_motd;
	unsigned int hex_irc_wallops;
	unsigned int hex_irc_who_join;
	unsigned int hex_irc_whois_front;
	unsigned int hex_irc_cap_server_time;
	unsigned int hex_net_auto_reconnect;
	unsigned int hex_net_auto_reconnectonfail;
	unsigned int hex_net_proxy_auth;
	unsigned int hex_net_throttle;
	unsigned int hex_notify_whois_online;
	unsigned int hex_perl_warnings;
	unsigned int hex_stamp_log;
	unsigned int hex_stamp_text;
	unsigned int hex_text_autocopy_color;
	unsigned int hex_text_autocopy_stamp;
	unsigned int hex_text_autocopy_text;
	unsigned int hex_text_color_nicks;
	unsigned int hex_text_indent;
	unsigned int hex_text_replay;
	unsigned int hex_text_search_case_match;
	unsigned int hex_text_search_highlight_all;
	unsigned int hex_text_search_follow;
	unsigned int hex_text_search_regexp;
	unsigned int hex_text_show_marker;
	unsigned int hex_text_show_sep;
	unsigned int hex_text_stripcolor_msg;
	unsigned int hex_text_stripcolor_replay;
	unsigned int hex_text_stripcolor_topic;
	unsigned int hex_text_thin_sep;
	unsigned int hex_text_transparent;
	unsigned int hex_text_wordwrap;
	unsigned int hex_url_grabber;
	unsigned int hex_url_logging;

	/* NUMBERS */
	int hex_away_size_max;
	int hex_away_timeout;
	int hex_completion_amount;
	int hex_completion_sort;
	int hex_dcc_auto_recv;
	int hex_dcc_blocksize;
	int hex_dcc_global_max_get_cps;
	int hex_dcc_global_max_send_cps;
	int hex_dcc_max_get_cps;
	int hex_dcc_max_send_cps;
	int hex_dcc_permissions;
	int hex_dcc_port_first;
	int hex_dcc_port_last;
	int hex_dcc_stall_timeout;
	int hex_dcc_timeout;
	int hex_flood_ctcp_num;				/* flood */
	int hex_flood_ctcp_time;			/* seconds of floods */
	int hex_flood_msg_num;				/* same deal */
	int hex_flood_msg_time;
	int hex_gui_chanlist_maxusers;
	int hex_gui_chanlist_minusers;
	int hex_gui_dialog_height;
	int hex_gui_dialog_left;
	int hex_gui_dialog_top;
	int hex_gui_dialog_width;
	int hex_gui_lagometer;
	int hex_gui_lang;
	int hex_gui_pane_divider_position;
	int hex_gui_pane_left_size;
	int hex_gui_pane_right_size;
	int hex_gui_pane_right_size_min;
	int hex_gui_search_pos;
	int hex_gui_slist_select;
	int hex_gui_tab_layout;
	int hex_gui_tab_newtofront;
	int hex_gui_tab_pos;
	int hex_gui_tab_small;
	int hex_gui_tab_trunc;
	int hex_gui_transparency;
	int hex_gui_throttlemeter;
	int hex_gui_ulist_pos;
	int hex_gui_ulist_sort;
	int hex_gui_url_mod;
	int hex_gui_win_height;
	int hex_gui_win_fullscreen;
	int hex_gui_win_left;
	int hex_gui_win_state;
	int hex_gui_win_top;
	int hex_gui_win_width;
	int hex_input_balloon_time;
	int hex_irc_ban_type;
	int hex_irc_join_delay;
	int hex_irc_notice_pos;
	int hex_net_ping_timeout;
	int hex_net_proxy_port;
	int hex_net_proxy_type;				/* 0=disabled, 1=wingate 2=socks4, 3=socks5, 4=http */
	int hex_net_proxy_use;				/* 0=all 1=IRC_ONLY 2=DCC_ONLY */
	int hex_net_reconnect_delay;
	int hex_notify_timeout;
	int hex_text_max_indent;
	int hex_text_max_lines;
	int hex_url_grabber_limit;

	/* STRINGS */
	char hex_away_reason[256];
	char hex_completion_suffix[4];		/* Only ever holds a one-character string. */
	char hex_dcc_completed_dir[PATHLEN + 1];
	char hex_dcc_dir[PATHLEN + 1];
	char hex_dcc_ip[DOMAINLEN + 1];
	char hex_gui_ulist_doubleclick[256];
	char hex_input_command_char[4];
	char hex_irc_extra_hilight[300];
	char hex_irc_id_ntext[64];
	char hex_irc_id_ytext[64];
	char hex_irc_logmask[256];
	char hex_irc_nick1[NICKLEN];
	char hex_irc_nick2[NICKLEN];
	char hex_irc_nick3[NICKLEN];
	char hex_irc_nick_hilight[300];
	char hex_irc_no_hilight[300];
	char hex_irc_part_reason[256];
	char hex_irc_quit_reason[256];
	char hex_irc_real_name[127];
	char hex_irc_user_name[127];
	char hex_net_bind_host[127];
	char hex_net_proxy_host[64];
	char hex_net_proxy_pass[32];
	char hex_net_proxy_user[32];
	char hex_stamp_log_format[64];
	char hex_stamp_text_format[64];
	char hex_text_background[PATHLEN + 1];
	char hex_text_font[4 * FONTNAMELEN + 1];
	char hex_text_font_main[FONTNAMELEN + 1];
	char hex_text_font_alternative[3 * FONTNAMELEN + 1];
	char hex_text_spell_langs[64];

	/* these are the private variables */
	guint32 local_ip;
	guint32 dcc_ip;

	unsigned int wait_on_exit;	/* wait for logs to be flushed to disk IF we're connected */
	unsigned int utf8_locale;

	/* Tells us if we need to save, only when they've been edited.
		This is so that we continue using internal defaults (which can
		change in the next release) until the user edits them. */
	unsigned int save_pevents:1;
};

/* Session types */
#define SESS_SERVER	1
#define SESS_CHANNEL	2
#define SESS_DIALOG	3
#define SESS_NOTICES	4
#define SESS_SNOTICES	5

/* Per-Channel Settings */
#define SET_OFF 0
#define SET_ON 1
#define SET_DEFAULT 2 /* use global setting */

/* Priorities in the "interesting sessions" priority queue
 * (see xchat.c:sess_list_by_lastact) */
#define LACT_NONE		-1		/* no queues */
#define LACT_QUERY_HI	0		/* query with hilight */
#define LACT_QUERY		1		/* query with messages */
#define LACT_CHAN_HI	2		/* channel with hilight */
#define LACT_CHAN		3		/* channel with messages */
#define LACT_CHAN_DATA	4		/* channel with other data */

/* Moved from fe-gtk for use in outbound.c as well -- */
typedef enum gtk_xtext_search_flags_e {
	case_match = 1,
	backward = 2,
	highlight = 4,
	follow = 8,
	regexp = 16
} gtk_xtext_search_flags;

typedef struct session
{
	/* Per-Channel Alerts */
	/* use a byte, because we need a pointer to each element */
	guint8 alert_beep;
	guint8 alert_taskbar;
	guint8 alert_tray;

	/* Per-Channel Settings */
	guint8 text_hidejoinpart;
	guint8 text_logging;
	guint8 text_scrollback;
	guint8 text_strip;

	struct server *server;
	void *usertree_alpha;			/* pure alphabetical tree */
	void *usertree;					/* ordered with Ops first */
	struct User *me;					/* points to myself in the usertree */
	char channel[CHANLEN];
	char waitchannel[CHANLEN];		  /* waiting to join channel (/join sent) */
	char willjoinchannel[CHANLEN];	  /* will issue /join for this channel */
	char channelkey[64];			  /* XXX correct max length? */
	int limit;						  /* channel user limit */
	int logfd;
	int scrollfd;							/* scrollback filedes */
	int scrollwritten;					/* number of lines written */

	char lastnick[NICKLEN];			  /* last nick you /msg'ed */

	struct history history;

	int ops;								/* num. of ops in channel */
	int hops;						  /* num. of half-oped users */
	int voices;							/* num. of voiced people */
	int total;							/* num. of users in channel */

	char *quitreason;
	char *topic;
	char *current_modes;					/* free() me */

	int mode_timeout_tag;

	struct session *lastlog_sess;
	struct nbexec *running_exec;

	struct session_gui *gui;		/* initialized by fe_new_window */
	struct restore_gui *res;

	int type;					/* SESS_* */

	int lastact_idx;		/* the sess_list_by_lastact[] index of the list we're in.
							 * For valid values, see defines of LACT_*. */

	int new_data:1;			/* new data avail? (purple tab) */
	int nick_said:1;		/* your nick mentioned? (blue tab) */
	int msg_said:1;			/* new msg available? (red tab) */

	int ignore_date:1;
	int ignore_mode:1;
	int ignore_names:1;
	int end_of_names:1;
	int doing_who:1;		/* /who sent on this channel */
	int done_away_check:1;	/* done checking for away status changes */
	gtk_xtext_search_flags lastlog_flags;
} session;

struct msproxy_state_t
{
	gint32				clientid;
	gint32				serverid;
	unsigned char		seq_recv;		/* seq number of last packet recv.	*/
	unsigned char		seq_sent;		/* seq number of last packet sent.	*/
};

/* SASL Mechanisms */
#define MECH_PLAIN 0
#define MECH_BLOWFISH 1
#define MECH_AES 2
#define MECH_EXTERNAL 3

typedef struct server
{
	/*  server control operations (in server*.c) */
	void (*connect)(struct server *, char *hostname, int port, int no_login);
	void (*disconnect)(struct session *, int sendquit, int err);
	int  (*cleanup)(struct server *);
	void (*flush_queue)(struct server *);
	void (*auto_reconnect)(struct server *, int send_quit, int err);
	/* irc protocol functions (in proto*.c) */
	void (*p_inline)(struct server *, char *buf, int len);
	void (*p_invite)(struct server *, char *channel, char *nick);
	void (*p_cycle)(struct server *, char *channel, char *key);
	void (*p_ctcp)(struct server *, char *to, char *msg);
	void (*p_nctcp)(struct server *, char *to, char *msg);
	void (*p_quit)(struct server *, char *reason);
	void (*p_kick)(struct server *, char *channel, char *nick, char *reason);
	void (*p_part)(struct server *, char *channel, char *reason);
	void (*p_ns_identify)(struct server *, char *pass);
	void (*p_ns_ghost)(struct server *, char *usname, char *pass);
	void (*p_join)(struct server *, char *channel, char *key);
	void (*p_join_list)(struct server *, GSList *favorites);
	void (*p_login)(struct server *, char *user, char *realname);
	void (*p_join_info)(struct server *, char *channel);
	void (*p_mode)(struct server *, char *target, char *mode);
	void (*p_user_list)(struct server *, char *channel);
	void (*p_away_status)(struct server *, char *channel);
	void (*p_whois)(struct server *, char *nicks);
	void (*p_get_ip)(struct server *, char *nick);
	void (*p_get_ip_uh)(struct server *, char *nick);
	void (*p_set_back)(struct server *);
	void (*p_set_away)(struct server *, char *reason);
	void (*p_message)(struct server *, char *channel, char *text);
	void (*p_action)(struct server *, char *channel, char *act);
	void (*p_notice)(struct server *, char *channel, char *text);
	void (*p_topic)(struct server *, char *channel, char *topic);
	void (*p_list_channels)(struct server *, char *arg, int min_users);
	void (*p_change_nick)(struct server *, char *new_nick);
	void (*p_names)(struct server *, char *channel);
	void (*p_ping)(struct server *, char *to, char *timestring);
/*	void (*p_set_away)(struct server *);*/
	int (*p_raw)(struct server *, char *raw);
	int (*p_cmp)(const char *s1, const char *s2);

	int port;
	int sok;					/* is equal to sok4 or sok6 (the one we are using) */
	int sok4;					/* tcp4 socket */
	int sok6;					/* tcp6 socket */
	int proxy_type;
	int proxy_sok;				/* Additional information for MS Proxy beast */
	int proxy_sok4;
	int proxy_sok6;
	struct msproxy_state_t msp_state;
	int id;					/* unique ID number (for plugin API) */
#ifdef USE_OPENSSL
	SSL *ssl;
	int ssl_do_connect_tag;
#else
	void *ssl;
#endif
	int childread;
	int childwrite;
	int childpid;
	int iotag;
	int recondelay_tag;				/* reconnect delay timeout */
	int joindelay_tag;				/* waiting before we send JOIN */
	char hostname[128];				/* real ip number */
	char servername[128];			/* what the server says is its name */
	char password[86];
	char nick[NICKLEN];
	char linebuf[2048];				/* RFC says 512 chars including \r\n */
	char *last_away_reason;
	int pos;								/* current position in linebuf */
	int nickcount;
	int loginmethod;					/* see login_types[] */

	char *chantypes;					/* for 005 numeric - free me */
	char *chanmodes;					/* for 005 numeric - free me */
	char *nick_prefixes;				/* e.g. "*@%+" */
	char *nick_modes;					/* e.g. "aohv" */
	char *bad_nick_prefixes;		/* for ircd that doesn't give the modes */
	int modes_per_line;				/* 6 on undernet, 4 on efnet etc... */

	void *network;						/* points to entry in servlist.c or NULL! */

	GSList *outbound_queue;
	time_t next_send;						/* cptr->since in ircu */
	time_t prev_now;					/* previous now-time */
	int sendq_len;						/* queue size */
	int lag;								/* milliseconds */

	struct session *front_session;	/* front-most window/tab */
	struct session *server_session;	/* server window/tab */

	struct server_gui *gui;		  /* initialized by fe_new_server */

	unsigned int ctcp_counter;	  /*flood */
	time_t ctcp_last_time;

	unsigned int msg_counter;	  /*counts the msg tab opened in a certain time */
	time_t msg_last_time;

	/*time_t connect_time;*/				/* when did it connect? */
	unsigned long lag_sent;   /* we are still waiting for this ping response*/
	time_t ping_recv;					/* when we last got a ping reply */
	time_t away_time;					/* when we were marked away */

	char *encoding;					/* NULL for system */
	GSList *favlist;			/* list of channels & keys to join */

	unsigned int motd_skipped:1;
	unsigned int connected:1;
	unsigned int connecting:1;
	unsigned int no_login:1;
	unsigned int skip_next_userhost:1;/* used for "get my ip from server" */
	unsigned int skip_next_whois:1;	/* hide whois output */
	unsigned int inside_whois:1;
	unsigned int doing_dns:1;			/* /dns has been done */
	unsigned int retry_sasl:1;		/* retrying another sasl mech */
	unsigned int end_of_motd:1;		/* end of motd reached (logged in) */
	unsigned int sent_quit:1;			/* sent a QUIT already? */
	unsigned int use_listargs:1;		/* undernet and dalnet need /list >0,<10000 */
	unsigned int is_away:1;
	unsigned int reconnect_away:1;	/* whether to reconnect in is_away state */
	unsigned int dont_use_proxy:1;	/* to proxy or not to proxy */
	unsigned int supports_watch:1;	/* supports the WATCH command */
	unsigned int supports_monitor:1;	/* supports the MONITOR command */
	unsigned int bad_prefix:1;			/* gave us a bad PREFIX= 005 number */
	unsigned int have_namesx:1;		/* 005 tokens NAMESX and UHNAMES */
	unsigned int have_awaynotify:1;
	unsigned int have_uhnames:1;
	unsigned int have_whox:1;		/* have undernet's WHOX features */
	unsigned int have_idmsg:1;		/* freenode's IDENTIFY-MSG */
	unsigned int have_accnotify:1; /* cap account-notify */
	unsigned int have_extjoin:1;	/* cap extended-join */
	unsigned int have_server_time:1;	/* cap server-time */
	unsigned int have_sasl:1;		/* SASL capability */
	unsigned int have_except:1;	/* ban exemptions +e */
	unsigned int have_invite:1;	/* invite exemptions +I */
	unsigned int have_cert:1;	/* have loaded a cert */
	unsigned int using_cp1255:1;	/* encoding is CP1255/WINDOWS-1255? */
	unsigned int using_irc:1;		/* encoding is "IRC" (CP1252/UTF-8 hybrid)? */
	unsigned int use_who:1;			/* whether to use WHO command to get dcc_ip */
	unsigned int sasl_mech;			/* mechanism for sasl auth */
	unsigned int sent_saslauth:1;	/* have sent AUTHENICATE yet */
	unsigned int sent_capend:1;	/* have sent CAP END yet */
#ifdef USE_OPENSSL
	unsigned int use_ssl:1;				  /* is server SSL capable? */
	unsigned int accept_invalid_cert:1;/* ignore result of server's cert. verify */
#endif
} server;

typedef int (*cmd_callback) (struct session * sess, char *tbuf, char *word[],
									  char *word_eol[]);

struct commands
{
	char *name;
	cmd_callback callback;
	char needserver;
	char needchannel;
	gint16 handle_quotes;
	char *help;
};

struct away_msg
{
	struct server *server;
	char nick[NICKLEN];
	char *message;
};

/* not just for popups, but used for usercommands, ctcp replies,
   userlist buttons etc */

struct popup
{
	char *cmd;
	char *name;
};

/* CL: get a random int in the range [0..n-1]. DON'T use rand() % n, it gives terrible results. */
#define RAND_INT(n) ((int)(rand() / (RAND_MAX + 1.0) * (n)))

#define hexchat_filename_from_utf8 g_filename_from_utf8
#define hexchat_filename_to_utf8 g_filename_to_utf8

#endif
s="o">= readdir(DIR) ) ) { if ( -d "$dir/$_" && $_ ne "." && $_ ne ".." && ( $HiddenDirs || !/^\./ ) ) { # directory $Pages{$_} = "" unless defined $Pages{$_}; $Pages{$_} .= "$dir/$_:"; push( @subdirs, $_ ); } elsif (/\.pod\z/) { # .pod s/\.pod\z//; $Pages{$_} = "" unless defined $Pages{$_}; $Pages{$_} .= "$dir/$_.pod:"; push( @pods, "$dir/$_.pod" ); } elsif (/\.html\z/) { # .html s/\.html\z//; $Pages{$_} = "" unless defined $Pages{$_}; $Pages{$_} .= "$dir/$_.pod:"; } elsif (/\.pm\z/) { # .pm s/\.pm\z//; $Pages{$_} = "" unless defined $Pages{$_}; $Pages{$_} .= "$dir/$_.pm:"; push( @pods, "$dir/$_.pm" ); } elsif ( -T "$dir/$_" ) { # script(?) local *F; if ( open( F, "$dir/$_" ) ) { my $line; while ( defined( $line = <F> ) ) { if ( $line =~ /^=(?:pod|head1)/ ) { $Pages{$_} = "" unless defined $Pages{$_}; $Pages{$_} .= "$dir/$_.pod:"; last; } } close(F); } } } closedir(DIR); # recurse on the subdirectories if necessary if ($recurse) { foreach my $subdir (@subdirs) { scan_dir( "$dir/$subdir", $recurse ); } } } # # scan_headings - scan a pod file for head[1-6] tags, note the tags, and # build an index. # sub scan_headings { my ( $sections, @data ) = @_; my ( $tag, $which_head, $otitle, $listdepth, $index ); local $Ignore = 0; $listdepth = 0; $index = ""; # scan for =head directives, note their name, and build an index # pointing to each of them. foreach my $line (@data) { if ( $line =~ /^=(head)([1-6])\s+(.*)/ ) { ( $tag, $which_head, $otitle ) = ( $1, $2, $3 ); my $title = depod($otitle); my $name = anchorify($title); $$sections{$name} = 1; $title = process_text( \$otitle ); while ( $which_head != $listdepth ) { if ( $which_head > $listdepth ) { $index .= "\n" . ( "\t" x ($listdepth) ) . ( $listdepth > 0 ? qq{<li class="branch">\n} . "\t"x($listdepth + 1): "" ) . "<ul>"; $listdepth++; } elsif ( $which_head < $listdepth ) { $listdepth--; $index .= "\n" . ( "\t" x $listdepth ) . ( $listdepth > 0 ? "\t" : "" ) . "</ul>" . ( $listdepth >= 0 ? "\n" . ("\t"x$listdepth) . "</li>" : "" ) . "\n"; } } $index .= "\n" . ( "\t" x $listdepth ) . "<li>" . "<a href=\"#" . $name . "\">" . $title . "</a></li>"; } } # finish off the lists while ( $listdepth-- ) { $index .= "\n" . ( "\t" x $listdepth ) . ($listdepth > 0 ? "\t" : "") ."</ul>\n" . ($listdepth > 0 ? ("\t" x $listdepth) . "</li>" : "" ); } # get rid of bogus lists $index =~ s,\t*<ul>\s*</ul>\n,,g; return $index; } # # scan_items - scans the pod specified by $pod for =item directives. we # will use this information later on in resolving C<> links. # sub scan_items { my ( $itemref, $pod, @poddata ) = @_; my ( $i, $item ); local $_; $pod =~ s/\.pod\z//; $pod .= ".html" if $pod; foreach $i ( 0 .. $#poddata ) { my $txt = depod( $poddata[$i] ); # figure out what kind of item it is. # Build string for referencing this item. if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet next unless $1; $item = $1; } elsif ( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list $item = $1; } elsif ( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item $item = $1; } else { next; } my $fid = fragment_id($item); $$itemref{$fid} = "$pod" if $fid; } } # # process_head - convert a pod head[1-6] tag and convert it to HTML format. # sub process_head { my ( $tag, $heading, $hasindex ) = @_; # figure out the level of the =head $tag =~ /head([1-6])/; my $level = $1; if ($Listlevel) { warn "$0: $Podfile: unterminated list at =head in paragraph $Paragraph. ignoring.\n" unless $Quiet; while ($Listlevel) { process_back(); } } print HTML "<p>\n"; if ( $level == 1 && !$Top ) { print HTML "<a href=\"#__index__\"><small>$Backlink</small></a>\n" if $hasindex and $Backlink; print HTML "</p>\n<hr />\n"; } else { print HTML "</p>\n"; } my $name = anchorify( depod($heading) ); my $convert = process_text( \$heading ); $convert =~ s{</?a[^>]+>}{}g; print HTML "<h$level><a name=\"$name\" />$convert</h$level>\n"; } # # emit_item_tag - print an =item's text # Note: The global $EmittedItem is used for inhibiting self-references. # my $EmittedItem; sub emit_item_tag($$$) { my ( $otext, $text, $compact ) = @_; my $item = fragment_id( depod($text), -generate ); Carp::confess( "Undefined fragment '$text' (" . depod($text) . ") from fragment_id() in emit_item_tag() in $Podfile" ) if !defined $item; $EmittedItem = $item; ### print STDERR "emit_item_tag=$item ($text)\n"; print HTML '<strong>'; if ( $Items_Named{$item}++ ) { print HTML process_text( \$otext ); } else { my $name = $item; $name = anchorify($name); print HTML #qq{<a name="$name" class="item">}, process_text( \$otext ), # '</a>' ; } print HTML "</strong>\n"; undef($EmittedItem); } sub emit_li { my ($tag) = @_; if ( $Items_Seen[$Listlevel]++ == 0 ) { push( @Listend, "</$tag>" ); print HTML "<$tag>\n"; } my $emitted = $tag eq 'dl' ? 'dt' : 'li'; print HTML "<$emitted>"; return $emitted; } # # process_item - convert a pod item tag and convert it to HTML format. # sub process_item { my ($otext) = @_; my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item # lots of documents start a list without doing an =over. this is # bad! but, the proper thing to do seems to be to just assume # they did do an =over. so warn them once and then continue. if ( $Listlevel == 0 ) { warn "$0: $Podfile: unexpected =item directive in paragraph $Paragraph. ignoring.\n" unless $Quiet; process_over(); } # formatting: insert a paragraph if preceding item has >1 paragraph if ($After_Lpar) { print HTML $need_dd ? "</dd>\n" : "</li>\n" if $After_Lpar; $After_Lpar = 0; } # remove formatting instructions from the text my $text = depod($otext); my $emitted; # the tag actually emitted, used for closing # all the list variants: if ( $text =~ /\A\*/ ) { # bullet $emitted = emit_li('ul'); if ( $text =~ /\A\*\s+(\S.*)\Z/s ) { # with additional text my $tag = $1; $otext =~ s/\A\*\s+//; emit_item_tag( $otext, $tag, 1 ); } print HTML "</li>" } elsif ( $text =~ /\A\d+/ ) { # numbered list $emitted = emit_li('ol'); if ( $text =~ /\A(?>\d+\.?)\s*(\S.*)\Z/s ) { # with additional text my $tag = $1; $otext =~ s/\A\d+\.?\s*//; emit_item_tag( $otext, $tag, 1 ); } print HTML "</li>"; } else { # definition list $emitted = emit_li('dl'); if ( $text =~ /\A(.+)\Z/s ) { # should have text emit_item_tag( $otext, $text, 1 ); } $need_dd = 1; } print HTML "\n"; return $need_dd; } # # process_over - process a pod over tag and start a corresponding HTML list. # sub process_over { # start a new list $Listlevel++; push( @Items_Seen, 0 ); $After_Lpar = 0; } # # process_back - process a pod back tag and convert it to HTML format. # sub process_back { my $need_dd = shift; if ( $Listlevel == 0 ) { warn "$0: $Podfile: unexpected =back directive in paragraph $Paragraph. ignoring.\n" unless $Quiet; return; } # close off the list. note, I check to see if $Listend[$Listlevel] is # defined because an =item directive may have never appeared and thus # $Listend[$Listlevel] may have never been initialized. $Listlevel--; if ( defined $Listend[$Listlevel] ) { print HTML $need_dd ? "</dd>\n" : "</li>\n" if $After_Lpar; print HTML $Listend[$Listlevel]; print HTML "\n"; pop(@Listend); } $After_Lpar = 0; # clean up item count pop(@Items_Seen); } # # process_cut - process a pod cut tag, thus start ignoring pod directives. # sub process_cut { $Ignore = 1; } # # process_pod - process a pod tag, thus stop ignoring pod directives # until we see a corresponding cut. # sub process_pod { # no need to set $Ignore to 0 cause the main loop did it } # # process_for - process a =for pod tag. if it's for html, spit # it out verbatim, if illustration, center it, otherwise ignore it. # sub process_for { my ( $whom, $text ) = @_; if ( $whom =~ /^(pod2)?html$/i ) { print HTML $text; } elsif ( $whom =~ /^illustration$/i ) { 1 while chomp $text; for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) { $text .= $ext, last if -r "$text$ext"; } print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>}; } } # # process_begin - process a =begin pod tag. this pushes # whom we're beginning on the begin stack. if there's a # begin stack, we only print if it us. # sub process_begin { my ( $whom, $text ) = @_; $whom = lc($whom); push( @Begin_Stack, $whom ); if ( $whom =~ /^(pod2)?html$/ ) { print HTML $text if $text; } } # # process_end - process a =end pod tag. pop the # begin stack. die if we're mismatched. # sub process_end { my ( $whom, $text ) = @_; $whom = lc($whom); if ( !defined $Begin_Stack[-1] or $Begin_Stack[-1] ne $whom ) { Carp::confess( "Unmatched begin/end at chunk $Paragraph in pod $Podfile\n"); } pop(@Begin_Stack); } # # process_pre - indented paragraph, made into <pre></pre> # sub process_pre { my ($text) = @_; my ($rest); return if $Ignore; $rest = $$text; # insert spaces in place of tabs $rest =~ s#(.+)# my $line = $1; 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e; $line; #eg; # convert some special chars to HTML escapes $rest = html_escape($rest); # try and create links for all occurrences of perl.* within # the preformatted text. $rest =~ s{ (\s*)(perl\w+) }{ if ( defined $Pages{$2} ){ # is a link qq($1<a href="$Htmlroot/$Pages{$2}">$2</a>); } elsif (defined $Pages{dosify($2)}) { # is a link qq($1<a href="$Htmlroot/$Pages{dosify($2)}">$2</a>); } else { "$1$2"; } }xeg; $rest =~ s{ (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)? }{ my $url ; if ( $Htmlfileurl ne '' ){ # Here, we take advantage of the knowledge # that $Htmlfileurl ne '' implies $Htmlroot eq ''. # Since $Htmlroot eq '', we need to prepend $Htmldir # on the fron of the link to get the absolute path # of the link's target. We check for a leading '/' # to avoid corrupting links that are #, file:, etc. my $old_url = $3 ; $old_url = "$Htmldir$old_url" if $old_url =~ m{^\/}; $url = relativize_url( "$old_url.html", $Htmlfileurl ); } else { $url = "$3.html" ; } "$1$url" ; }xeg; # Look for embedded URLs and make them into links. We don't # relativize them since they are best left as the author intended. my $urls = '(' . join( '|', qw{ http telnet mailto news gopher file wais ftp } ) . ')'; my $ltrs = '\w'; my $gunk = '/#~:.?+=&%@!\-'; my $punc = '.:!?\-;'; my $any = "${ltrs}${gunk}${punc}"; $rest =~ s{ \b # start at word boundary ( # begin $1 { $urls : # need resource and a colon (?!:) # Ignore File::, among others. [$any] +? # followed by one or more of any valid # character, but be conservative and # take only what you need to.... ) # end $1 } (?= &quot; &gt; # maybe pre-quoted '<a href="...">' | # or: [$punc]* # 0 or more punctuation (?: # followed [^$any] # by a non-url char | # or $ # end of the string ) # | # or else $ # then end of the string ) }{<a href="$1">$1</a>}igox; # text should be as it is (verbatim) $$text = $rest; } # # pure text processing # # pure_text/inIS_text: differ with respect to automatic C<> recognition. # we don't want this to happen within IS # sub pure_text($) { my $text = shift(); process_puretext( $text, 1 ); } sub inIS_text($) { my $text = shift(); process_puretext( $text, 0 ); } # # process_puretext - process pure text (without pod-escapes) converting # double-quotes and handling implicit C<> links. # sub process_puretext { my ( $text, $notinIS ) = @_; ## Guessing at func() or [\$\@%&]*var references in plain text is destined ## to produce some strange looking ref's. uncomment to disable: ## $notinIS = 0; my ( @words, $lead, $trail ); # keep track of leading and trailing white-space $lead = ( $text =~ s/\A(\s+)//s ? $1 : "" ); $trail = ( $text =~ s/(\s+)\Z//s ? $1 : "" ); # split at space/non-space boundaries @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text ); # process each word individually foreach my $word (@words) { # skip space runs next if $word =~ /^\s*$/; # see if we can infer a link or a function call # # NOTE: This is a word based search, it won't automatically # mark "substr($var, 1, 2)" because the 1st word would be "substr($var" # User has to enclose those with proper C<> if ( $notinIS && $word =~ m/ ^([a-z_]{2,}) # The function name \( ([0-9][a-z]* # Manual page(1) or page(1M) |[^)]*[\$\@\%][^)]+ # ($foo), (1, @foo), (%hash) | # () ) \) ([.,;]?)$ # a possible punctuation follows /xi ) { # has parenthesis so should have been a C<> ref ## try for a pagename (perlXXX(1))? my ( $func, $args, $rest ) = ( $1, $2, $3 || '' ); if ( $args =~ /^\d+$/ ) { my $url = page_sect( $word, '' ); if ( defined $url ) { $word = qq(<a href="$url" class="man">the $word manpage</a>$rest); next; } } ## try function name for a link, append tt'ed argument list $word = emit_C( $func, '', "($args)" ) . $rest; #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing. ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) { ## # perl variables, should be a C<> ref ## $word = emit_C( $word ); } elsif ( $word =~ m,^\w+://\w, ) { # looks like a URL # Don't relativize it: leave it as the author intended $word = qq(<a href="$word">$word</a>); } elsif ( $word =~ /[\w.-]+\@[\w-]+\.\w/ ) { # looks like an e-mail address my ( $w1, $w2, $w3 ) = ( "", $word, "" ); ( $w1, $w2, $w3 ) = ( "(", $1, ")$2" ) if $word =~ /^\((.*?)\)(,?)/; ( $w1, $w2, $w3 ) = ( "&lt;", $1, "&gt;$2" ) if $word =~ /^<(.*?)>(,?)/; $word = qq($w1<a href="mailto:$w2">$w2</a>$w3); } else { $word = html_escape($word) if $word =~ /["&<>]/; } } # put everything back together return $lead . join( '', @words ) . $trail; } # # process_text - handles plaintext that appears in the input pod file. # there may be pod commands embedded within the text so those must be # converted to html commands. # sub process_text1($$;$$); sub pattern ($) { $_[0] ? '\s+' . ( '>' x ( $_[0] + 1 ) ) : '>' } sub closing ($) { local ($_) = shift; ( defined && s/\s+\z// ) ? length : 0 } sub process_text { return if $Ignore; my ($tref) = @_; my $res = process_text1( 0, $tref ); $res =~ s/\s+$//s; $$tref = $res; } sub process_text_rfc_links { my $text = shift; # For every "RFCnnnn" or "RFC nnn", link it to the authoritative # ource. Do not use the /i modifier here. Require "RFC" to be written in # in capital letters. $text =~ s{ (?<=[^<>[:alpha:]]) # Make sure this is not an URL already (RFC\s*([0-9]{1,5}))(?![0-9]) # max 5 digits } {<a href="http://www.ietf.org/rfc/rfc$2.txt" class="rfc">$1</a>}gx; $text; } sub process_text1($$;$$) { my ( $lev, $rstr, $func, $closing ) = @_; my $res = ''; unless ( defined $func ) { $func = ''; $lev++; } if ( $func eq 'B' ) { # B<text> - boldface $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>'; } elsif ( $func eq 'C' ) { # C<code> - can be a ref or <code></code> # need to extract text my $par = go_ahead( $rstr, 'C', $closing ); ## clean-up of the link target my $text = depod($par); ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ; ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n"; $res = emit_C( $text, $lev > 1 || ( $par =~ /[BI]</ ) ); } elsif ( $func eq 'E' ) { # E<x> - convert to character $$rstr =~ s/^([^>]*)>//; my $escape = $1; $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i; $res = "&$escape;"; } elsif ( $func eq 'F' ) { # F<filename> - italicize $res = '<em class="file">' . process_text1( $lev, $rstr ) . '</em>'; } elsif ( $func eq 'I' ) { # I<text> - italicize $res = '<em>' . process_text1( $lev, $rstr ) . '</em>'; } elsif ( $func eq 'L' ) { # L<link> - link ## L<text|cross-ref> => produce text, use cross-ref for linking ## L<cross-ref> => make text from cross-ref ## need to extract text my $par = go_ahead( $rstr, 'L', $closing ); # some L<>'s that shouldn't be: # a) full-blown URL's are emitted as-is if ( $par =~ m{^\w+://}s ) { return make_URL_href($par); } # b) C<...> is stripped and treated as C<> if ( $par =~ /^C<(.*)>$/ ) { my $text = depod($1); return emit_C( $text, $lev > 1 || ( $par =~ /[BI]</ ) ); } # analyze the contents $par =~ s/\n/ /g; # undo word-wrapped tags my $opar = $par; my $linktext; if ( $par =~ s{^([^|]+)\|}{} ) { $linktext = $1; } if( $par =~ m{^\w+://}s ) { return make_URL_href( $par, $linktext ); } # make sure sections start with a / $par =~ s{^"}{/"}; my ( $page, $section, $ident ); # check for link patterns if ( $par =~ m{^([^/]+?)/(?!")(.*?)$} ) { # name/ident # we've got a name/ident (no quotes) if ( length $2 ) { ( $page, $ident ) = ( $1, $2 ); } else { ( $page, $section ) = ( $1, $2 ); } ### print STDERR "--> L<$par> to page $page, ident $ident\n"; } elsif ( $par =~ m{^(.*?)/"?(.*?)"?$} ) { # [name]/"section" # even though this should be a "section", we go for ident first ( $page, $ident ) = ( $1, $2 ); ### print STDERR "--> L<$par> to page $page, section $section\n"; } elsif ( $par =~ /\s/ ) { # this must be a section with missing quotes ( $page, $section ) = ( '', $par ); ### print STDERR "--> L<$par> to void page, section $section\n"; } else { ( $page, $section ) = ( $par, '' ); ### print STDERR "--> L<$par> to page $par, void section\n"; } # now, either $section or $ident is defined. the convoluted logic # below tries to resolve L<> according to what the user specified. # failing this, we try to find the next best thing... my ( $url, $ltext, $fid ); RESOLVE: { if ( defined $ident ) { ## try to resolve $ident as an item ( $url, $fid ) = coderef( $page, $ident ); if ($url) { if ( !defined($linktext) ) { $linktext = $ident; $linktext .= " in " if $ident && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got coderef url=$url\n"; last RESOLVE; } ## no luck: go for a section (auto-quoting!) $section = $ident; } ## now go for a section my $htmlsection = htmlify($section); $url = page_sect( $page, $htmlsection ); if ($url) { if ( !defined($linktext) ) { $linktext = $section; $linktext .= " in " if $section && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got page/section url=$url\n"; last RESOLVE; } ## no luck: go for an ident if ($section) { $ident = $section; } else { $ident = $page; $page = undef(); } ( $url, $fid ) = coderef( $page, $ident ); if ($url) { if ( !defined($linktext) ) { $linktext = $ident; $linktext .= " in " if $ident && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got section=>coderef url=$url\n"; last RESOLVE; } # warning; show some text. $linktext = $opar unless defined $linktext; warn "$0: $Podfile: cannot resolve L<$opar> in paragraph $Paragraph.\n" unless $Quiet; } # now we have a URL or just plain code $$rstr = $linktext . '>' . $$rstr; if ( defined($url) ) { $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>'; } else { $res = '<em>' . process_text1( $lev, $rstr ) . '</em>'; } } elsif ( $func eq 'S' ) { # S<text> - non-breaking spaces $res = process_text1( $lev, $rstr ); $res =~ s/ /&nbsp;/g; } elsif ( $func eq 'X' ) { # X<> - ignore warn "$0: $Podfile: invalid X<> in paragraph $Paragraph.\n" unless $$rstr =~ s/^[^>]*>// or $Quiet; } elsif ( $func eq 'Z' ) { # Z<> - empty warn "$0: $Podfile: invalid Z<> in paragraph $Paragraph.\n" unless $$rstr =~ s/^>// or $Quiet; } else { my $term = pattern $closing; while ( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ) { # all others: either recurse into new function or # terminate at closing angle bracket(s) my $pt = $1; $pt .= $2 if !$3 && $lev == 1; $res .= $lev == 1 ? pure_text($pt) : inIS_text($pt); return $res if !$3 && $lev > 1; if ($3) { $res .= process_text1( $lev, $rstr, $3, closing $4 ); } } if ( $lev == 1 ) { $res .= pure_text($$rstr); } elsif ( !$Quiet ) { my $snippet = substr( $$rstr, 0, 60 ); warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph: '$snippet'.\n" } $res = process_text_rfc_links($res); } return $res; } # # go_ahead: extract text of an IS (can be nested) # sub go_ahead($$$) { my ( $rstr, $func, $closing ) = @_; my $res = ''; my @closing = ($closing); while ( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+\s+)?|@{[pattern $closing[0]]})//s ) { $res .= $1; unless ($3) { shift @closing; return $res unless @closing; } else { unshift @closing, closing $4; } $res .= $2; } unless ($Quiet) { my $snippet = substr( $$rstr, 0, 60 ); warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph (go_ahead): '$snippet'.\n"; } return $res; } # # emit_C - output result of C<text> # $text is the depod-ed text # sub emit_C($;$$) { my ( $text, $nocode, $args ) = @_; $args = '' unless defined $args; my $res; my ( $url, $fid ) = coderef( undef(), $text ); # need HTML-safe text my $linktext = html_escape("$text$args"); if ( $text !~ /^[\$@%]/ && defined($url) && ( !defined($EmittedItem) || $EmittedItem ne $fid ) ) { $res = "<a href=\"$url\"><code>$linktext</code></a>"; } elsif ( 0 && $nocode ) { $res = $linktext; } else { $res = "<code>$linktext</code>"; } return $res; } # # html_escape: make text safe for HTML # sub html_escape { my $rest = $_[0]; $rest =~ s/&/&amp;/g; $rest =~ s/</&lt;/g; $rest =~ s/>/&gt;/g; $rest =~ s/"/&quot;/g; # &apos; is only in XHTML, not HTML4. Be conservative #$rest =~ s/'/&apos;/g; return $rest; } # # dosify - convert filenames to 8.3 # sub dosify { my ($str) = @_; return lc($str) if $^O eq 'VMS'; # VMS just needs casing if ($Is83) { $str = lc $str; $str =~ s/(\.\w+)/substr ($1,0,4)/ge; $str =~ s/(\w+)/substr ($1,0,8)/ge; } return $str; } # # page_sect - make a URL from the text of a L<> # sub page_sect($$) { my ( $page, $section ) = @_; my ( $linktext, $page83, $link ); # work strings # check if we know that this is a section in this page if ( !defined $Pages{$page} && defined $Sections{$page} ) { $section = $page; $page = ""; ### print STDERR "reset page='', section=$section\n"; } $page83 = dosify($page); $page = $page83 if ( defined $Pages{$page83} ); if ( $page eq "" ) { $link = "#" . anchorify($section); } elsif ( $page =~ /::/ ) { $page =~ s,::,/,g; # Search page cache for an entry keyed under the html page name, # then look to see what directory that page might be in. NOTE: # this will only find one page. A better solution might be to produce # an intermediate page that is an index to all such pages. my $page_name = $page; $page_name =~ s,^.*/,,s; if ( defined( $Pages{$page_name} ) && $Pages{$page_name} =~ /([^:]*$page)\.(?:pod|pm):/ ) { $page = $1; } else { # NOTE: This branch assumes that all A::B pages are located in # $Htmlroot/A/B.html . This is often incorrect, since they are # often in $Htmlroot/lib/A/B.html or such like. Perhaps we could # analyze the contents of %Pages and figure out where any # cousins of A::B are, then assume that. So, if A::B isn't found, # but A::C is found in lib/A/C.pm, then A::B is assumed to be in # lib/A/B.pm. This is also limited, but it's an improvement. # Maybe a hints file so that the links point to the correct places # nonetheless? } $link = "$Htmlroot/$page.html"; $link .= "#" . anchorify($section) if ($section); } elsif ( !defined $Pages{$page} ) { $link = ""; } else { $section = anchorify($section) if $section ne ""; ### print STDERR "...section=$section\n"; # if there is a directory by the name of the page, then assume that an # appropriate section will exist in the subdirectory # if ($section ne "" && $Pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) { if ( $section ne "" && $Pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/ ) { $link = "$Htmlroot/$1/$section.html"; ### print STDERR "...link=$link\n"; # since there is no directory by the name of the page, the section will # have to exist within a .html of the same name. thus, make sure there # is a .pod or .pm that might become that .html } else { $section = "#$section" if $section; ### print STDERR "...section=$section\n"; # check if there is a .pod with the page name. # for L<Foo>, Foo.(pod|pm) is preferred to A/Foo.(pod|pm) if ( $Pages{$page} =~ /([^:]*)\.(?:pod|pm):/ ) { $link = "$Htmlroot/$1.html$section"; } else { $link = ""; } } } if ($link) { # Here, we take advantage of the knowledge that $Htmlfileurl ne '' # implies $Htmlroot eq ''. This means that the link in question # needs a prefix of $Htmldir if it begins with '/'. The test for # the initial '/' is done to avoid '#'-only links, and to allow # for other kinds of links, like file:, ftp:, etc. my $url; if ( $Htmlfileurl ne '' ) { $link = "$Htmldir$link" if $link =~ m{^/}s; $url = relativize_url( $link, $Htmlfileurl ); # print( " b: [$link,$Htmlfileurl,$url]\n" ); } else { $url = $link; } return $url; } else { return undef(); } } # # relativize_url - convert an absolute URL to one relative to a base URL. # Assumes both end in a filename. # sub relativize_url { my ( $dest, $source ) = @_; my ( $dest_volume, $dest_directory, $dest_file ) = File::Spec::Unix->splitpath($dest); $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ); my ( $source_volume, $source_directory, $source_file ) = File::Spec::Unix->splitpath($source); $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ); my $rel_path = ''; if ( $dest ne '' ) { $rel_path = File::Spec::Unix->abs2rel( $dest, $source ); } if ( $rel_path ne '' && substr( $rel_path, -1 ) ne '/' && substr( $dest_file, 0, 1 ) ne '#' ) { $rel_path .= "/$dest_file"; } else { $rel_path .= "$dest_file"; } return $rel_path; } # # coderef - make URL from the text of a C<> # sub coderef($$) { my ( $page, $item ) = @_; my ($url); my $fid = fragment_id($item); if ( defined($page) && $page ne "" ) { # we have been given a $page... $page =~ s{::}{/}g; Carp::confess( "Undefined fragment '$item' from fragment_id() in coderef() in $Podfile" ) if !defined $fid; # Do we take it? Item could be a section! my $base = $Items{$fid} || ""; $base =~ s{[^/]*/}{}; if ( $base ne "$page.html" ) { ### print STDERR "coderef( $page, $item ): items{$fid} = $Items{$fid} = $base => discard page!\n"; $page = undef(); } } else { # no page - local items precede cached items if ( defined($fid) ) { if ( exists $Local_Items{$fid} ) { $page = $Local_Items{$fid}; } else { $page = $Items{$fid}; } } } # if there was a pod file that we found earlier with an appropriate # =item directive, then create a link to that page. if ( defined $page ) { if ($page) { if ( exists $Pages{$page} and $Pages{$page} =~ /([^:.]*)\.[^:]*:/ ) { $page = $1 . '.html'; } my $link = "$Htmlroot/$page#" . anchorify($fid); # Here, we take advantage of the knowledge that $Htmlfileurl # ne '' implies $Htmlroot eq ''. if ( $Htmlfileurl ne '' ) { $link = "$Htmldir$link"; $url = relativize_url( $link, $Htmlfileurl ); } else { $url = $link; } } else { $url = "#" . anchorify($fid); } confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/; } return ( $url, $fid ); } # # Adapted from Nick Ing-Simmons' PodToHtml package. sub relative_url { my $source_file = shift; my $destination_file = shift; my $source = URI::file->new_abs($source_file); my $uo = URI::file->new( $destination_file, $source )->abs; return $uo->rel->as_string; } # # finish_list - finish off any pending HTML lists. this should be called # after the entire pod file has been read and converted. # sub finish_list { while ( $Listlevel > 0 ) { print HTML "</dl>\n"; $Listlevel--; } } # # htmlify - converts a pod section specification to a suitable section # specification for HTML. Note that we keep spaces and special characters # except ", ? (Netscape problem) and the hyphen (writer's problem...). # sub htmlify { my ($heading) = @_; $heading =~ s/(\s+)/ /g; $heading =~ s/\s+\Z//; $heading =~ s/\A\s+//; # The hyphen is a disgrace to the English language. # $heading =~ s/[-"?]//g; $heading =~ s/["?]//g; $heading = lc($heading); return $heading; } # # similar to htmlify, but turns non-alphanumerics into underscores # sub anchorify { my ($anchor) = @_; $anchor =~ s/\([^)]*\)//; $anchor = htmlify($anchor); $anchor =~ s/\W/_/g; $anchor =~ tr/_/_/s; return $anchor; } # # depod - convert text by eliminating all interior sequences # Note: can be called with copy or modify semantics # my %E2c; $E2c{lt} = '<'; $E2c{gt} = '>'; $E2c{sol} = '/'; $E2c{verbar} = '|'; $E2c{amp} = '&'; # in Tk's pods sub depod1($;$$); sub depod($) { my $string; if ( ref( $_[0] ) ) { $string = ${ $_[0] }; ${ $_[0] } = depod1( \$string ); } else { $string = $_[0]; depod1( \$string ); } } sub depod1($;$$) { my ( $rstr, $func, $closing ) = @_; my $res = ''; return $res unless defined $$rstr; if ( !defined($func) ) { # skip to next begin of an interior sequence while ( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?//s ) { # recurse into its text $res .= $1 . depod1( $rstr, $2, closing $3); } $res .= $$rstr; } elsif ( $func eq 'E' ) { # E<x> - convert to character $$rstr =~ s/^([^>]*)>//; $res .= $E2c{$1} || ""; } elsif ( $func eq 'X' ) { # X<> - ignore $$rstr =~ s/^[^>]*>//; } elsif ( $func eq 'Z' ) { # Z<> - empty $$rstr =~ s/^>//; } else { # all others: either recurse into new function or # terminate at closing angle bracket my $term = pattern $closing; while ( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ) { $res .= $1; last unless $3; $res .= depod1( $rstr, $3, closing $4 ); } ## If we're here and $2 ne '>': undelimited interior sequence. ## Ignored, as this is called without proper indication of where we are. ## Rely on process_text to produce diagnostics. } return $res; } { my %seen; # static fragment record hash sub fragment_id_readable { my $text = shift; my $generate = shift; # optional flag my $orig = $text; # leave the words for the fragment identifier, # change everything else to underbars. $text =~ s/[^A-Za-z0-9_]+/_/g; # do not use \W to avoid locale dependency. $text =~ s/_{2,}/_/g; $text =~ s/\A_//; $text =~ s/_\Z//; unless ($text) { # Nothing left after removing punctuation, so leave it as is # E.g. if option is named: "=item -#" $text = $orig; } if ($generate) { if ( exists $seen{$text} ) { # This already exists, make it unique $seen{$text}++; $text = $text . $seen{$text}; } else { $seen{$text} = 1; # first time seen this fragment } } $text; } } my @HC; sub fragment_id_obfuscated { # This was the old "_2d_2d__" my $text = shift; my $generate = shift; # optional flag # text? Normalize by obfuscating the fragment id to make it unique $text =~ s/\s+/_/sg; $text =~ s{(\W)}{ defined( $HC[ord($1)] ) ? $HC[ord($1)] : ( $HC[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe; $text = substr( $text, 0, 50 ); $text; } # # fragment_id - construct a fragment identifier from: # a) =item text # b) contents of C<...> # sub fragment_id { my $text = shift; my $generate = shift; # optional flag $text =~ s/\s+\Z//s; if ($text) { # a method or function? return $1 if $text =~ /(\w+)\s*\(/; return $1 if $text =~ /->\s*(\w+)\s*\(?/; # a variable name? return $1 if $text =~ /^([\$\@%*]\S+)/; # some pattern matching operator? return $1 if $text =~ m|^(\w+/).*/\w*$|; # fancy stuff... like "do { }" return $1 if $text =~ m|^(\w+)\s*{.*}$|; # honour the perlfunc manpage: func [PAR[,[ ]PAR]...] # and some funnies with ... Module ... return $1 if $text =~ m{^([a-z\d_]+)(\s+[A-Z,/& ][A-Z\d,/& ]*)?$}; return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$}; return fragment_id_readable( $text, $generate ); } else { return; } } # # make_URL_href - generate HTML href from URL # Special treatment for CGI queries. # sub make_URL_href($;$) { my ($url) = shift; my $linktext = shift || $url; if ( $url !~ s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ) { $url = "<a href=\"$url\">$linktext</a>"; } return $url; } 1;