[twitter] Rework twitter_expand_entities
[friendica-addons.git/.git] / irc / irc.php
1 <?php
2 /**
3 * Name: IRC Chat Addon
4 * Description: add an Internet Relay Chat chatroom on freenode
5 * Version: 1.1
6 * Author: tony baldwin <https://free-haven.org/profile/tony>
7 * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
8 */
9
10 use Friendica\Core\Hook;
11 use Friendica\Core\Renderer;
12 use Friendica\DI;
13
14 function irc_install() {
15         Hook::register('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
16         Hook::register('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
17         Hook::register('addon_settings_post', 'addon/irc/irc.php', 'irc_addon_settings_post');
18 }
19
20 function irc_uninstall() {
21         Hook::unregister('app_menu', 'addon/irc/irc.php', 'irc_app_menu');
22         Hook::unregister('addon_settings', 'addon/irc/irc.php', 'irc_addon_settings');
23
24 }
25
26
27 function irc_addon_settings(&$a,&$s) {
28         if(! local_user())
29                 return;
30
31     /* Add our stylesheet to the page so we can make our settings look nice */
32
33 //      DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/irc/irc.css' . '" media="all" />' . "\r\n";
34
35     /* setting popular channels, auto connect channels */
36         $sitechats = DI::pConfig()->get( local_user(), 'irc','sitechats'); /* popular channels */
37         $autochans = DI::pConfig()->get( local_user(), 'irc','autochans');  /* auto connect chans */
38
39         $t = Renderer::getMarkupTemplate( "settings.tpl", "addon/irc/" );
40         $s .= Renderer::replaceMacros($t, [
41                 '$header' => DI::l10n()->t('IRC Settings'),
42                 '$info' => DI::l10n()->t('Here you can change the system wide settings for the channels to automatically join and access via the side bar. Note the changes you do here, only effect the channel selection if you are logged in.'),
43                 '$submit' => DI::l10n()->t('Save Settings'),
44                 '$autochans' => [ 'autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
45                 '$sitechats' => [ 'sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.') ]
46         ]);
47
48
49         return;
50
51 }
52
53 function irc_addon_settings_post(&$a, &$b) {
54         if(!local_user())
55                 return;
56
57         if(!empty($_POST['irc-submit'])) {
58                 if (isset($_POST['autochans'])) {
59                         DI::pConfig()->set(local_user(), 'irc', 'autochans', trim(($_POST['autochans'])));
60                 }
61                 if (isset($_POST['sitechats'])) {
62                         DI::pConfig()->set(local_user(), 'irc', 'sitechats', trim($_POST['sitechats']));
63                 }
64                 /* upid pop-up thing */
65                 info(DI::l10n()->t('IRC settings saved.') . EOL);
66         }
67 }
68
69 function irc_app_menu($a,&$b) {
70         $b['app_menu'][] = '<div class="app-title"><a href="irc">' . DI::l10n()->t('IRC Chatroom') . '</a></div>';
71 }
72
73
74 function irc_module() {
75         return;
76 }
77
78
79 function irc_content(&$a) {
80
81         $baseurl = DI::baseUrl()->get() . '/addon/irc';
82         $o = '';
83
84         /* set the list of popular channels */
85         if (local_user()) {
86             $sitechats = DI::pConfig()->get( local_user(), 'irc', 'sitechats');
87             if (!$sitechats)
88                 $sitechats = DI::config()->get('irc', 'sitechats');
89         } else {
90             $sitechats = DI::config()->get('irc','sitechats');
91         }
92         if($sitechats)
93                 $chats = explode(',',$sitechats);
94         else
95                 $chats = ['friendica','chat','chatback','hottub','ircbar','dateroom','debian'];
96
97
98         DI::page()['aside'] .= '<div class="widget"><h3>' . DI::l10n()->t('Popular Channels') . '</h3><ul>';
99         foreach($chats as $chat) {
100                 DI::page()['aside'] .= '<li><a href="' . DI::baseUrl()->get() . '/irc?channels=' . $chat . '" >' . '#' . $chat . '</a></li>';
101         }
102         DI::page()['aside'] .= '</ul></div>';
103
104         /* setting the channel(s) to auto connect */
105         if (local_user()) {
106             $autochans = DI::pConfig()->get(local_user(), 'irc', 'autochans');
107             if (!$autochans)
108                 $autochans = DI::config()->get('irc','autochans');
109         } else {
110             $autochans = DI::config()->get('irc','autochans');
111         }
112         if($autochans)
113                 $channels = $autochans;
114         else
115                 $channels = ($_GET['channels'] ?? '') ?: 'friendica';
116
117 /* add the chatroom frame and some html */
118   $o .= <<< EOT
119 <h2>IRC chat</h2>
120 <p><a href="http://tldp.org/HOWTO/IRC/beginners.html" target="_blank" rel="noopener noreferrer">A beginner's guide to using IRC. [en]</a></p>
121 <iframe src="//webchat.freenode.net?channels=$channels" style="width:100%; max-width:900px; height: 600px;"></iframe>
122 EOT;
123
124 return $o;
125
126 }
127
128 function irc_addon_admin_post (&$a) {
129         if(! is_site_admin())
130                 return;
131
132         if($_POST['irc-submit']) {
133                 DI::config()->set('irc','autochans',trim($_POST['autochans']));
134                 DI::config()->set('irc','sitechats',trim($_POST['sitechats']));
135                 /* stupid pop-up thing */
136                 info(DI::l10n()->t('IRC settings saved.') . EOL);
137         }
138 }
139 function irc_addon_admin (&$a, &$o) {
140         $sitechats = DI::config()->get('irc','sitechats'); /* popular channels */
141         $autochans = DI::config()->get('irc','autochans');  /* auto connect chans */
142         $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/irc/" );
143         $o = Renderer::replaceMacros($t, [
144                 '$submit' => DI::l10n()->t('Save Settings'),
145                 '$autochans' => [ 'autochans', DI::l10n()->t('Channel(s) to auto connect (comma separated)'), $autochans, DI::l10n()->t('List of channels that shall automatically connected to when the app is launched.')],
146                 '$sitechats' => [ 'sitechats', DI::l10n()->t('Popular Channels (comma separated)'), $sitechats, DI::l10n()->t('List of popular channels, will be displayed at the side and hotlinked for easy joining.') ]
147         ]);
148 }